|
|
|
@ -5,6 +5,7 @@ namespace App\AdminSupplier\Controllers; |
|
|
|
use App\AdminSupplier\Repositories\IndustryProduct; |
|
|
|
use App\Common\ProductStatus; |
|
|
|
use App\Models\Category; |
|
|
|
use App\Models\Order; |
|
|
|
use App\Models\Supplier; |
|
|
|
use App\Models\SystemSetting; |
|
|
|
use Dcat\Admin\Admin; |
|
|
|
@ -101,6 +102,7 @@ class IndustryProductController extends AdminController |
|
|
|
protected function form() |
|
|
|
{ |
|
|
|
Admin::user()->publish_type = json_decode(Admin::user()->publish_type, true); |
|
|
|
$change_deposit = 0; |
|
|
|
return Form::make(new IndustryProduct(), function (Form $form) { |
|
|
|
$form->disableDeleteButton(); |
|
|
|
|
|
|
|
@ -196,7 +198,7 @@ class IndustryProductController extends AdminController |
|
|
|
$form->text('extends.field_3.address', '地址'); |
|
|
|
$form->map('extends.field_3.latitude', 'extends.field_3.longitude', '位置'); |
|
|
|
}); |
|
|
|
})->saving(function (Form $form) { |
|
|
|
})->saving(function (Form $form) use (&$change_deposit) { |
|
|
|
//不允许编辑非自己数据
|
|
|
|
if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) { |
|
|
|
return $form->response()->error('数据不存在'); |
|
|
|
@ -228,35 +230,50 @@ class IndustryProductController extends AdminController |
|
|
|
return $form->response()->error('未设置单人头交易费,请联系管理员在后台设置'); |
|
|
|
} |
|
|
|
|
|
|
|
//TODO 还需要计算涉及用户数
|
|
|
|
|
|
|
|
//目前逻辑不考虑单人头交易费变动的情况
|
|
|
|
if ($form->isCreating() || $form->isEditing() && $form->model()->stock != $form->stock) { |
|
|
|
DB::beginTransaction(); |
|
|
|
|
|
|
|
$supplier = Supplier::query()->find(Admin::user()->id); //不能使用Admin::user()修改,必须使用Supplier模型才能正确记录资金变动日志
|
|
|
|
|
|
|
|
if ($form->isCreating()) { //新增处理
|
|
|
|
$change_deposit = $single * $form->service_persons * $form->stock; |
|
|
|
} else if ($form->isEditing() && $form->model()->stock != $form->stock) { //库存变动时冻结或解冻相应的保证金
|
|
|
|
$change_deposit = $single * $form->service_persons * ($form->stock - $form->model()->stock); |
|
|
|
$new_deposit = $single * $form->service_persons * $form->stock; |
|
|
|
$old_deposit = $single * $form->service_persons * $form->model()->stock; |
|
|
|
$change_deposit = $new_deposit - $old_deposit; //saved里面获取的$form->model()->stock是不对的
|
|
|
|
} |
|
|
|
|
|
|
|
if (Admin::user()->deposit_normal < $change_deposit) { |
|
|
|
DB::rollBack(); |
|
|
|
return $form->response()->error('交易金不足,请先联系管理员充值'); |
|
|
|
} |
|
|
|
} |
|
|
|
})->saved(function (Form $form, $result) use (&$change_deposit) { |
|
|
|
if ($form->isEditing()) { //有extends判断不准
|
|
|
|
//待审核
|
|
|
|
if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content'])) { |
|
|
|
DB::beginTransaction(); |
|
|
|
try { |
|
|
|
//将产品状态改为未审核
|
|
|
|
$form->model()->update(['status' => ProductStatus::UNAUDITED]); |
|
|
|
|
|
|
|
//如果产品未审核,重新计算交易金。通过或拒绝之后再扣掉 TODO 改变库存和涉及用户数要考虑。未审核也需要重新全部减掉
|
|
|
|
$supplier = Supplier::query()->find(Admin::user()->id); //不能使用Admin::user()修改,必须使用Supplier模型才能正确记录资金变动日志
|
|
|
|
|
|
|
|
$supplier->deposit_normal = $supplier->deposit_normal - $change_deposit; //正常交易金
|
|
|
|
$supplier->deposit_frozen = $supplier->deposit_frozen + $change_deposit; //冻结交易金
|
|
|
|
$supplier->save(); |
|
|
|
DB::commit(); |
|
|
|
} catch (\Exception $e) { |
|
|
|
DB::rollBack(); |
|
|
|
} |
|
|
|
})->saved(function (Form $form, $result) { |
|
|
|
if ($form->isEditing() && $form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content'])) { //有extends判断不准
|
|
|
|
$form->model()->update(['status' => ProductStatus::UNAUDITED]); |
|
|
|
} |
|
|
|
} else { //不是待审核
|
|
|
|
|
|
|
|
if ($result && ($form->isCreating() || $form->isEditing() && $form->model()->wasChanged('stock'))) { |
|
|
|
DB::commit(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*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)) { |
|
|
|
|