diff --git a/app/Admin/Actions/Grid/OrderPackageEdit.php b/app/Admin/Actions/Grid/OrderPackageEdit.php new file mode 100644 index 00000000..70edeaa6 --- /dev/null +++ b/app/Admin/Actions/Grid/OrderPackageEdit.php @@ -0,0 +1,38 @@ +title) { + return $this->title; + } + return ' 修改数量  '; + } + + /** + * @param Model|Authenticatable|HasPermissions|null $user + * + * @return bool + */ + protected function authorize($user): bool + { + return $user->can('dcat.admin.order_packages.edit_custom'); + } + + public function render() + { + $form = OrderPackageEditForm::make()->payload(['id'=>$this->getKey()]); + return Modal::make() + ->lg() + ->title($this->title()) + ->body($form) + ->button($this->title()); + } +} diff --git a/app/Admin/Controllers/OrderPackageController.php b/app/Admin/Controllers/OrderPackageController.php index a473106b..3dd3262f 100644 --- a/app/Admin/Controllers/OrderPackageController.php +++ b/app/Admin/Controllers/OrderPackageController.php @@ -3,6 +3,7 @@ namespace App\Admin\Controllers; use App\Admin\Actions\Grid\KuaidiInfo; +use App\Admin\Actions\Grid\OrderPackageEdit; use App\Admin\Actions\Grid\OrderPackageFailed; use App\Admin\Actions\Grid\OrderPackageSetTag; use App\Admin\Renderable\PackageProductSimpleTable; @@ -113,6 +114,11 @@ class OrderPackageController extends AdminController if (Admin::user()->can('dcat.admin.order_packages.tags')) { $actions->append(new OrderPackageSetTag()); } + + //修改发货单商品数量 + if (!$actions->row->is_settle && Admin::user()->can('dcat.admin.order_packages.edit')) { + $actions->append(new OrderPackageEdit()); + } }); /** 查询 **/ @@ -161,7 +167,7 @@ class OrderPackageController extends AdminController */ protected function form() { - $builder = OrderPackage::with('order'); + $builder = OrderPackage::with(['order']); return Form::make($builder, function (Form $form) { $form->display('id'); $form->text('order.sn')->disable(); diff --git a/app/Admin/Forms/AfterSaleShippingFill.php b/app/Admin/Forms/AfterSaleShippingFill.php index 42c4e1c4..978510c0 100644 --- a/app/Admin/Forms/AfterSaleShippingFill.php +++ b/app/Admin/Forms/AfterSaleShippingFill.php @@ -2,7 +2,11 @@ namespace App\Admin\Forms; +use App\Admin\Services\OrderPackageService; +use App\Exceptions\BizException; use App\Models\AfterSale; +use App\Models\Order; +use App\Models\OrderPackageProduct; use App\Services\AfterSaleService; use Dcat\Admin\Contracts\LazyRenderable; use Dcat\Admin\Traits\LazyWidget; @@ -39,8 +43,43 @@ class AfterSaleShippingFill extends Form implements LazyRenderable try { DB::beginTransaction(); $afterSale = AfterSale::where('state', AfterSale::STATE_SHIPPING)->findOrFail($this->payload['id']); - $afterSaleService->shippingFill($afterSale, $input['remarks']); - + //处理售后 + $afterSaleService->shippingFill($afterSale, $input['shipping_number']); + /** 处理补发逻辑 **/ + //获取售后商品数量 + $fillQuantity = $afterSale->num; + $packageProduct = OrderPackageProduct::where('order_product_id', $afterSale->order_product_id)->where('quantity', '>', $fillQuantity)->first(); + if (!$packageProduct) { + throw new BizException('补发包裹未找到或者售后补发数量异常'); + } + //调整已发货包裹商品数量 + $packageProduct->decrement('quantity', $fillQuantity); + //回滚对应订单商品待发货数量; + $packageProduct->orderProduct->increment('remain_quantity', $fillQuantity); + //回滚订单状态 + if ($afterSale->order->isCompleted()) { + $afterSale->order->update([ + 'status'=>Order::STATUS_PAID, + 'shipping_state'=>Order::SHIPPING_STATE_PROCESSING, + 'completed_at'=>null, + ]); + } elseif ($afterSale->order->isShipped()) { + $afterSale->order->update([ + 'shipping_state'=>Order::SHIPPING_STATE_PROCESSING, + ]); + } + //自动帮他生成新的发货单 + $orderPackageService = new OrderPackageService(); + $orderPackageService->createPackage($afterSale->order, [ + 'shipping_company'=>$input['shipping_company'], + 'shipping_number'=>$input['shipping_number'], + 'packages'=>[ + [ + 'order_product_id'=>$afterSale->order_product_id, + 'quantity' =>$fillQuantity, + ], + ], + ]); DB::commit(); } catch (Throwable $th) { DB::rollBack(); @@ -58,7 +97,18 @@ class AfterSaleShippingFill extends Form implements LazyRenderable */ public function form() { - $this->text('remarks', '填写补货物流单号')->required(); + $this->select('shipping_company', '快递公司')->options([ + '圆通速递'=>'圆通速递', + '韵达快递'=>'韵达快递', + '中通快递'=>'中通快递', + '申通快递'=>'申通快递', + '百世快递'=>'百世快递', + 'EMS'=>'EMS', + '顺丰速运'=>'顺丰速运', + '德邦快递'=>'德邦快递', + ])->required(); + $this->text('shipping_number', '快递单号')->required(); + // $this->text('remarks', '填写补货物流单号')->required(); $this->confirm('是否确认补货完成', '该操作不可逆,确认后将完成该售后单。'); } } diff --git a/app/Admin/Forms/OrderPackageEdit.php b/app/Admin/Forms/OrderPackageEdit.php new file mode 100644 index 00000000..f7a440ae --- /dev/null +++ b/app/Admin/Forms/OrderPackageEdit.php @@ -0,0 +1,96 @@ +can('dcat.admin.order_packages.edit_custom'); + } + + /** + * Handle the form request. + * + * @param array $input + * + * @return mixed + */ + public function handle(array $input) + { + $id = $this->payload['id'] ?? 0; + $packageProductId = $input['package_product_id']; + $quantity = $input['quantity']??0; + + $orderPackage = OrderPackage::with('order')->findOrFail($id); + //如果订单已分润则不处理 + if ($orderPackage->order->is_settle) { + throw new BizException('该发货单已无法修改数量'); + } + //修改的数量不能大于当前数量,只能变小 + $packageProduct = OrderPackageProduct::findOrFail($packageProductId); + if ($quantity == 0) { + throw new BizException('修改的数量不能为0'); + } + if ($packageProduct->quantity <= $quantity) { + throw new BizException('修改的数量不能大于等于当前数量'); + } + try { + DB::beginTransaction(); + //更新当前包裹这个商品数量; + $packageProduct->update([ + 'quantity'=>$quantity, + ]); + //回滚对应订单商品待发货数量; + $packageProduct->orderProduct->increment('remain_quantity', $packageProduct->quantity - $quantity); + + //如果订单是已确认收货状态,则回滚到发货中; + if ($orderPackage->order->isCompleted()) { + $orderPackage->order()->update([ + 'status'=>Order::STATUS_PAID, + 'shipping_state'=>Order::SHIPPING_STATE_PROCESSING, + 'completed_at'=>null, + ]); + } + + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + return $this->response()->error('操作失败:'.$th->getMessage()); + } + + return $this->response() + ->success(__('admin.update_succeeded')) + ->refresh(); + } + + /** + * Build a form here. + */ + public function form() + { + $id = $this->payload['id'] ?? 0; + $this->select('package_product_id', '包裹商品') + ->options(OrderPackageProduct::with('orderProduct')->where('order_package_id', $id)->get()->pluck('orderProduct.name', 'id')) + ->required(); + $this->number('quantity', '发货数量')->default(1)->min(1)->required(); + } +} diff --git a/app/Services/AfterSaleService.php b/app/Services/AfterSaleService.php index 7e3b5c09..219696be 100644 --- a/app/Services/AfterSaleService.php +++ b/app/Services/AfterSaleService.php @@ -25,19 +25,6 @@ class AfterSaleService */ public function create(User $user, OrderProduct $orderProduct, int $type, int $num, array $params): AfterSale { - // if (is_null($orderProduct->after_expire_at)) { - // throw new BizException('该订单商品还未签收无法发起售后'); - // } - - // // 校验这个订单商品是否还能发起售后-待完成 - // if ($orderProduct->after_expire_at < now()) { - // throw new BizException('该订单商品已过售后时间'); - // } - - // //校验该订单商品不能有已完成、处理中的售后记录 - // if ($orderProduct->after_sale_state > 0) { - // throw new BizException('该订单商品已申请售后'); - // } if (!$orderProduct->can_after_sale) { throw new BizException('该订单商品无法发起售后'); } diff --git a/database/seeders/AdAddressSeeder.php b/database/seeders/AdAddressSeeder.php index dbfb7c81..822d45b1 100644 --- a/database/seeders/AdAddressSeeder.php +++ b/database/seeders/AdAddressSeeder.php @@ -60,6 +60,11 @@ class AdAddressSeeder extends Seeder 'dimensions'=> '', 'is_show' => true, ], + 'app_start_page_banner'=>[ + 'name' => 'APP启动页广告位', + 'dimensions'=> '', + 'is_show' => true, + ], ] as $key => $values) { AdAddress::firstOrCreate(['key' => $key], $values); }