erp/app/Listeners/CombinationGoodsStockUpdateListener.php

119 lines
3.8 KiB
PHP
Raw Normal View History

2023-04-21 19:43:17 +08:00
<?php
namespace App\Listeners;
use App\Models\CombinationGood;
2023-11-21 16:27:14 +08:00
use App\Models\DailyStockRecord;
2023-04-21 19:43:17 +08:00
use App\Models\GoodsSku;
2023-11-21 16:27:14 +08:00
use App\Utils\DateTimeUtils;
2023-04-21 19:43:17 +08:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
2023-11-18 14:35:19 +08:00
use App\Events\BatchStockUpdateEvent;
2023-04-21 19:43:17 +08:00
class CombinationGoodsStockUpdateListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle($event)
{
2023-11-18 14:35:19 +08:00
if (!$event->combinationGoodsUpdate) {
2023-11-21 16:27:14 +08:00
return false;
2023-04-21 19:43:17 +08:00
}
$updateIds = $combinationGoodsIds = $combinationGoodsItemIds = [];
2023-11-18 14:35:19 +08:00
if (!empty($event->goodsSku)) {
2023-04-21 19:43:17 +08:00
if ($event->goodsSku->is_combination) {
$combinationGoodsIds[] = $event->goodsSku->id;
} else {
2023-04-26 11:43:25 +08:00
$combinationGoodsItemIds[] = $event->goodsSku->id;
2023-04-21 19:43:17 +08:00
}
}
2023-11-21 16:27:14 +08:00
2023-11-18 14:35:19 +08:00
if (!empty($event->goodsSkus)) {
2023-04-21 19:43:17 +08:00
foreach ($event->goodsSkus as $sku) {
if ($sku->is_combination) {
$combinationGoodsIds[] = $sku->id;
} else {
2023-04-26 11:44:14 +08:00
$combinationGoodsItemIds[] = $sku->id;
2023-04-21 19:43:17 +08:00
}
}
}
// 减子商品库存
if ($combinationGoodsIds) {
$combinationGoods = CombinationGood::query()
->with('goodsSku:id,stock')
->whereIn('goods_sku_id', $combinationGoodsIds)
->get();
foreach ($combinationGoods as $item) {
2023-11-21 16:27:14 +08:00
$goodsSku = GoodsSku::query()->find($item['item_id']);
$stock = $goodsSku->stock - $item['item_num'];
[$status, $stock] = $this->checkStatusAndStock($goodsSku, $stock);
$goodsSku->status = $status;
$goodsSku->stock = $stock;
$goodsSku->save();
$updateIds[] = $goodsSku->id;
2023-04-21 19:43:17 +08:00
}
}
// 计算主商品库存
if ($combinationGoodsItemIds) {
2023-04-26 11:43:25 +08:00
$goodsSkuIds = CombinationGood::query()
->whereIn('item_id', $combinationGoodsItemIds)
2023-11-21 16:27:14 +08:00
->pluck('goods_sku_id');
2023-04-26 11:43:25 +08:00
foreach ($goodsSkuIds as $goodsSkuId) {
$combinationGoods = CombinationGood::query()
->with('goodsSkuItem:id,stock')
->where('goods_sku_id', $goodsSkuId)
->get();
$stock = [];
2023-04-26 11:43:25 +08:00
foreach ($combinationGoods as $goods) {
$stock[] = (int)($goods['goodsSkuItem']['stock'] / $goods['item_num']);
2023-04-21 19:43:17 +08:00
}
$stock = min($stock);
2023-11-21 16:27:14 +08:00
$goodsSku = GoodsSku::query()->find($goodsSkuId);
[$status, $stock] = $this->checkStatusAndStock($goodsSku, $stock);
$goodsSku->status = $status;
$goodsSku->stock = $stock;
$goodsSku->save();
2023-04-26 11:43:25 +08:00
$updateIds[] = $goodsSkuId;
2023-04-21 19:43:17 +08:00
}
}
if ($updateIds) {
2023-04-26 10:51:15 +08:00
$updateIds = array_unique($updateIds);
2023-11-18 14:35:19 +08:00
// 批量更新
event(new BatchStockUpdateEvent($updateIds, false));
2023-04-21 19:43:17 +08:00
}
}
2023-11-21 16:27:14 +08:00
private function checkStatusAndStock($goodsSku, $stock)
{
if (0 >= $stock) {
$status = GoodsSku::$STATUS_DOWN;
} else {
$status = GoodsSku::$STATUS_ON_SALE;
}
$arrivedTodayNum = DailyStockRecord::query()
->where('day', DateTimeUtils::getToday())
->where('sku_id', $goodsSku->id)
->value('arrived_today_num');
if (20 < $arrivedTodayNum + $goodsSku->yesterday_num && 4 > $stock) {
$status = GoodsSku::$STATUS_DOWN;
$stock = 0;
}
return [$status, $stock];
}
2023-04-21 19:43:17 +08:00
}