|
|
|
@ -120,7 +120,9 @@ class IndustryProductController extends AdminController |
|
|
|
$form->number('service_persons')->min(0)->required(); |
|
|
|
$form->number('stock')->required(); |
|
|
|
$form->number('min_sale')->min(1)->required(); |
|
|
|
$form->radio('status')->options([1 => '上架', -2 => '下架'])->default(1); |
|
|
|
if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE])) { |
|
|
|
$form->radio('status')->options([1 => '上架', -2 => '下架'])->default(1); |
|
|
|
} |
|
|
|
$form->multipleImage('pictures')->required(); |
|
|
|
$form->editor('know'); |
|
|
|
$form->editor('content')->required(); |
|
|
|
@ -216,69 +218,76 @@ class IndustryProductController extends AdminController |
|
|
|
$form->deleteInput('type'); |
|
|
|
} |
|
|
|
|
|
|
|
//忽略字段
|
|
|
|
$form->ignore(['id', 'sale', 'created_at', 'updated_at', 'deleted_at']); |
|
|
|
|
|
|
|
$form->hidden(['status', 'supplier_id']); |
|
|
|
$form->supplier_id = Admin::user()->id; |
|
|
|
if ($form->isCreating()) { |
|
|
|
$form->status = ProductStatus::UNAUDITED; |
|
|
|
} else if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE])) { //如果原来是下架或上架状态才允许修改
|
|
|
|
$form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT; |
|
|
|
} |
|
|
|
|
|
|
|
//处理交易金
|
|
|
|
$single = SystemSetting::val('single', 'price'); //单人头交易费
|
|
|
|
if (!$single) { |
|
|
|
return $form->response()->error('未设置单人头交易费,请联系管理员在后台设置'); |
|
|
|
} |
|
|
|
|
|
|
|
//用户可编辑的状态
|
|
|
|
$user_status = [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE]; |
|
|
|
|
|
|
|
//目前逻辑不考虑总后台修改单人头交易费的情况
|
|
|
|
if ($form->isCreating()) { |
|
|
|
$change_deposit = $single * $form->service_persons * $form->stock; |
|
|
|
|
|
|
|
$form->hidden(['status', 'supplier_id']); |
|
|
|
$form->supplier_id = Admin::user()->id; |
|
|
|
$form->status = ProductStatus::UNAUDITED; |
|
|
|
} |
|
|
|
//库存和涉及用户数变动时冻结或解冻相应的保证金
|
|
|
|
else if ($form->isEditing() && ($form->model()->stock != $form->stock || $form->model()->service_persons != $form->service_persons)) { |
|
|
|
$new_deposit = $single * $form->service_persons * $form->stock; //新的涉及用户数及库存计算出来的值
|
|
|
|
$old_deposit = $single * $form->model()->service_persons * $form->model()->stock; //原来的涉及用户数及库存计算出来的值
|
|
|
|
$change_deposit = $new_deposit - $old_deposit; //saved里面获取的$form->model()->stock是不对的
|
|
|
|
//库存和单库存服务用户数变动时冻结或解冻相应的保证金
|
|
|
|
else if ($form->isEditing()) { |
|
|
|
if (!in_array($form->model()->status, $user_status) || !in_array($form->status, $user_status)) { |
|
|
|
$form->deleteInput('status'); |
|
|
|
} |
|
|
|
|
|
|
|
//只处理原来状态是下架或上架用户可编辑的状态,否则后台未审核又多次编辑将出错
|
|
|
|
if (in_array($form->model()->status, $user_status)) { |
|
|
|
//原来的单库存服务用户数及库存计算出来的值,如果是未审核状态要返回去
|
|
|
|
$old_deposit = $single * $form->model()->service_persons * $form->model()->stock; |
|
|
|
|
|
|
|
//编辑库存或单库存服务用户数
|
|
|
|
if ($form->model()->stock != $form->stock || $form->model()->service_persons != $form->service_persons) { |
|
|
|
$new_deposit = $single * $form->service_persons * $form->stock; //新的单库存服务用户数及库存计算出来的值
|
|
|
|
$change_deposit = $new_deposit - $old_deposit; //saved里面获取的$form->model()->stock是不对的
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//判断可用交易金是否充足
|
|
|
|
if (Admin::user()->deposit_normal < $change_deposit) { |
|
|
|
return $form->response()->error('交易金不足,请先联系管理员充值'); |
|
|
|
} |
|
|
|
|
|
|
|
//忽略字段
|
|
|
|
$form->ignore(['id', 'sale', 'created_at', 'updated_at', 'deleted_at']); |
|
|
|
})->saved(function (Form $form, $result) use (&$change_deposit, &$old_deposit) { |
|
|
|
if ($form->isEditing()) { |
|
|
|
$supplier = Supplier::query()->find(Admin::user()->id); //不能使用Admin::user()修改,必须使用Supplier模型才能正确记录资金变动日志
|
|
|
|
|
|
|
|
//需要审核的情况
|
|
|
|
if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content'])) { //有extends判断不准
|
|
|
|
//如果修改了库存和单库存服务用户数
|
|
|
|
$is_change = $form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content']); //有extends判断不准
|
|
|
|
if ($is_change) { |
|
|
|
DB::beginTransaction(); |
|
|
|
try { |
|
|
|
//将产品状态改为未审核
|
|
|
|
$form->model()->update(['status' => ProductStatus::UNAUDITED]); |
|
|
|
|
|
|
|
//如果产品未审核,原来冻结过的交易金先全部返还,待总后台审核通过之后再做扣除处理
|
|
|
|
//如果改变的交易金不是0,原来冻结过的交易金先全部返还,待总后台审核通过之后再做扣除处理
|
|
|
|
$supplier->deposit_normal = $supplier->deposit_normal + $old_deposit; //正常交易金
|
|
|
|
$supplier->deposit_frozen = $supplier->deposit_frozen - $old_deposit; //冻结交易金
|
|
|
|
$supplier->save(); |
|
|
|
|
|
|
|
DB::commit(); |
|
|
|
} catch (\Exception $e) { |
|
|
|
DB::rollBack(); |
|
|
|
} |
|
|
|
} |
|
|
|
//不需要审核的情况,$change_deposit无论是正数还是负数都一样计算
|
|
|
|
else if ($change_deposit != 0) { |
|
|
|
} else if ($change_deposit != 0) { //修改库存或单库存服务用户数,但不修改关键信息,增减交易金,$change_deposit无论是正数还是负数都一样计算
|
|
|
|
$supplier->deposit_normal = $supplier->deposit_normal - $change_deposit; //正常交易金
|
|
|
|
$supplier->deposit_frozen = $supplier->deposit_frozen + $change_deposit; //冻结交易金
|
|
|
|
$supplier->save(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*if ($result && ($form->isCreating() || $form->isEditing() && $form->model()->wasChanged('stock'))) { |
|
|
|
|
|
|
|
}*/ |
|
|
|
})->deleting(function (Form $form) { |
|
|
|
//不允许删除非自己的数据
|
|
|
|
if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) { |
|
|
|
|