From 31ca699f31c012171938c2c89395564a3673ee76 Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Wed, 18 May 2022 17:26:44 +0800 Subject: [PATCH] lis --- app/Admin/Forms/OrderPackage.php | 2 +- .../Controllers/Order/UnlineController.php | 2 +- app/Listeners/OrderPackage.php | 31 +++++++++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/Admin/Forms/OrderPackage.php b/app/Admin/Forms/OrderPackage.php index 392bdf52..ec7fa20b 100644 --- a/app/Admin/Forms/OrderPackage.php +++ b/app/Admin/Forms/OrderPackage.php @@ -70,7 +70,7 @@ class OrderPackage extends Form implements LazyRenderable 'operator_id' => $operator->id, 'source_type' => Order::class, 'source_id' => $order->id, - 'amount' => $amount, + 'amount' => 0-$amount, 'product_sku_id' => $product->id, 'remarks' => '后台发货', 'tag_id' => $tag->id diff --git a/app/Endpoint/Api/Http/Controllers/Order/UnlineController.php b/app/Endpoint/Api/Http/Controllers/Order/UnlineController.php index 3b172c2b..9e587365 100644 --- a/app/Endpoint/Api/Http/Controllers/Order/UnlineController.php +++ b/app/Endpoint/Api/Http/Controllers/Order/UnlineController.php @@ -90,7 +90,7 @@ class UnlineController extends Controller 'operator_id' => $user->id, 'source_type' => Order::class, 'source_id' => $order->id, - 'amount' => $amount, + 'amount' => 0-$amount, 'product_sku_id' => $product->id, 'remarks' => '店铺提货', 'tag_id' => $tag->id diff --git a/app/Listeners/OrderPackage.php b/app/Listeners/OrderPackage.php index ba453fd7..b570035d 100644 --- a/app/Listeners/OrderPackage.php +++ b/app/Listeners/OrderPackage.php @@ -5,9 +5,10 @@ namespace App\Listeners; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use App\Events\OrderPaid; -use App\Models\{Order, OrderPre}; +use App\Models\{Order, OrderPre, Tag, User}; use Illuminate\Support\Facades\DB; use Throwable; +use App\Exceptions\BizException; class OrderPackage { @@ -30,12 +31,18 @@ class OrderPackage public function handle(OrderPaid $event) { $order = Order::find($event->order->id); - + // 订单来源为 帮客户下单 order_pres $source = $order->source; if ($source instanceof OrderPre) { + $store = $order->store; + $inviter = $order->inviter_id ? User::find($order->inviter_id) : null; try { DB::beginTransaction(); + $tag = Tag::firstOrCreate([ + 'type' => Tag::TYPE_STORE_STOCK, + 'name' => '提货' + ]); $order_products = $order->products; // 根据 order_pres 发货数量, 自动发货 $service_package = new \App\Admin\Services\OrderPackageService(); @@ -51,6 +58,26 @@ class OrderPackage 'quantity' => $send ]); } + // 添加发货记录 + $sku_id = $order_product->sku_id; + $product = $store->productSkus()->findOrFail($sku_id); + if ($product->pivot->amount - $send < 0) { + throw new BizException('店铺的 ' . $product->name .' 库存不足'); + } + + $store->productSkus()->updateExistingPivot($product->id, [ + 'amount' => $product->pivot->amount - $send + ]); + $store->stockLogs()->create([ + 'operator_type' => $inviter ? get_class($inviter) : '', + 'operator_id' => $inviter ? $inviter->id : '', + 'source_type' => Order::class, + 'source_id' => $order->id, + 'amount' => 0-$send, + 'product_sku_id' => $product->id, + 'remarks' => '店铺提货', + 'tag_id' => $tag->id + ]); } // 发货 if (count($package_params) > 0) {