6
0
Fork 0
release
panliang 2022-05-18 17:26:44 +08:00
parent 9d156f4351
commit 31ca699f31
3 changed files with 31 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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) {