mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-12-01 14:40:44 +00:00
96 lines
3.0 KiB
PHP
96 lines
3.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Listeners;
|
||
|
|
|
||
|
|
use App\Models\CombinationGood;
|
||
|
|
use App\Models\GoodsSku;
|
||
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
|
use Illuminate\Queue\InteractsWithQueue;
|
||
|
|
use App\Events\StockUpdateEvent;
|
||
|
|
|
||
|
|
class CombinationGoodsStockUpdateListener
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Create the event listener.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
//
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle the event.
|
||
|
|
*
|
||
|
|
* @param $event
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function handle($event)
|
||
|
|
{
|
||
|
|
if ($event->combinationGoodsUpdate) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
$updateIds = $combinationGoodsIds = $combinationGoodsItemIds = [];
|
||
|
|
if ($event->goodsSku) {
|
||
|
|
if ($event->goodsSku->is_combination) {
|
||
|
|
$combinationGoodsIds[] = $event->goodsSku->id;
|
||
|
|
} else {
|
||
|
|
$combinationGoodsItemIds[$event->goodsSku->id] = $event->goodsSku->stock;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if ($event->goodsSkus) {
|
||
|
|
foreach ($event->goodsSkus as $sku) {
|
||
|
|
if ($sku->is_combination) {
|
||
|
|
$combinationGoodsIds[] = $sku->id;
|
||
|
|
} else {
|
||
|
|
$combinationGoodsItemIds[$sku->id] = $sku->stock;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// 减子商品库存
|
||
|
|
if ($combinationGoodsIds) {
|
||
|
|
$combinationGoods = CombinationGood::query()
|
||
|
|
->with('goodsSku:id,stock')
|
||
|
|
->whereIn('goods_sku_id', $combinationGoodsIds)
|
||
|
|
->get();
|
||
|
|
foreach ($combinationGoods as $item) {
|
||
|
|
$sku = GoodsSku::query()->find($item['item_id']);
|
||
|
|
$sku->stock -= $item['item_num'];
|
||
|
|
$sku->save();
|
||
|
|
$updateIds[] = $sku->id;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// 计算主商品库存
|
||
|
|
if ($combinationGoodsItemIds) {
|
||
|
|
$itemIds = array_keys($combinationGoodsItemIds);
|
||
|
|
$goodsSkuIds = CombinationGood::query()
|
||
|
|
->whereIn('item_id', $itemIds)
|
||
|
|
->pluck('goods_sku_id')
|
||
|
|
->toArray();
|
||
|
|
if (empty($goodsSkuIds)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
$goodsSkus = GoodsSku::query()
|
||
|
|
->whereIn('id', $goodsSkuIds)
|
||
|
|
->pluck('stock', 'id')
|
||
|
|
->toArray();
|
||
|
|
foreach ($combinationGoodsItemIds as $itemId => $stock) {
|
||
|
|
$combinationGoods = CombinationGood::query()
|
||
|
|
->where('item_id', $itemId)
|
||
|
|
->get();
|
||
|
|
foreach ($combinationGoods as $goods) {
|
||
|
|
$stock = $combinationGoodsItemIds[$goods['item_id']] / $goods['item_num'];
|
||
|
|
if ($stock < $goodsSkus[$goods['goods_sku_id']]) {
|
||
|
|
GoodsSku::query()->where('id', $goods['goods_sku_id'])->update(['stock' => $stock]);
|
||
|
|
$updateIds[] = $goods['goods_sku_id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if ($updateIds) {
|
||
|
|
event(new StockUpdateEvent($updateIds, 1, true));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|