mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-11-30 22:20:45 +00:00
commit
0f9d4e0d65
@ -36,7 +36,7 @@ class CombinationGoodsStockUpdateListener
|
||||
if ($event->goodsSku->is_combination) {
|
||||
$combinationGoodsIds[] = $event->goodsSku->id;
|
||||
} else {
|
||||
$combinationGoodsItemIds[$event->goodsSku->id] = $event->goodsSku->stock;
|
||||
$combinationGoodsItemIds[] = $event->goodsSku->id;
|
||||
}
|
||||
}
|
||||
if ($event->goodsSkus) {
|
||||
@ -44,7 +44,7 @@ class CombinationGoodsStockUpdateListener
|
||||
if ($sku->is_combination) {
|
||||
$combinationGoodsIds[] = $sku->id;
|
||||
} else {
|
||||
$combinationGoodsItemIds[$sku->id] = $sku->stock;
|
||||
$combinationGoodsItemIds[] = $sku->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -63,23 +63,31 @@ class CombinationGoodsStockUpdateListener
|
||||
}
|
||||
// 计算主商品库存
|
||||
if ($combinationGoodsItemIds) {
|
||||
$combinationGoodsItems = CombinationGood::query()
|
||||
->whereIn('item_id', array_keys($combinationGoodsItemIds))
|
||||
->get();
|
||||
if ($combinationGoodsItems->isEmpty()){
|
||||
$goodsSkuIds = CombinationGood::query()
|
||||
->whereIn('item_id', $combinationGoodsItemIds)
|
||||
->pluck('goods_sku_id')
|
||||
->toArray();
|
||||
if (empty($goodsSkuIds)) {
|
||||
return false;
|
||||
}
|
||||
$goodsSkuIds = array_column($combinationGoodsItems->toArray(), 'goods_sku_id');
|
||||
$goodsSkus = GoodsSku::query()
|
||||
->whereIn('id', $goodsSkuIds)
|
||||
->pluck('stock', 'id')
|
||||
->toArray();
|
||||
foreach ($combinationGoodsItems as $item){
|
||||
$stock = $combinationGoodsItemIds[$item['item_id']] / $item['item_num'];
|
||||
if ($stock < $goodsSkus[$item['goods_sku_id']]) {
|
||||
GoodsSku::query()->where('id', $item['goods_sku_id'])->update(['stock' => $stock]);
|
||||
$updateIds[] = $item['goods_sku_id'];
|
||||
foreach ($goodsSkuIds as $goodsSkuId) {
|
||||
$combinationGoods = CombinationGood::query()
|
||||
->with('goodsSkuItem:id,stock')
|
||||
->where('goods_sku_id', $goodsSkuId)
|
||||
->get();
|
||||
$stock = 0;
|
||||
foreach ($combinationGoods as $goods) {
|
||||
$num = $goods['goodsSkuItem']['stock'] / $goods['item_num'];
|
||||
if (0 === $stock) {
|
||||
$stock = $num;
|
||||
continue;
|
||||
}
|
||||
if ($num < $stock) {
|
||||
$stock = $num;
|
||||
}
|
||||
}
|
||||
GoodsSku::query()->where('id', $goodsSkuId)->update(['stock' => $stock]);
|
||||
$updateIds[] = $goodsSkuId;
|
||||
}
|
||||
}
|
||||
if ($updateIds) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user