库存更新同步修改

This commit is contained in:
赵世界 2023-11-21 16:27:14 +08:00
parent b2b3def740
commit b242208992
14 changed files with 122 additions and 78 deletions

View File

@ -16,20 +16,17 @@ class BatchStockUpdateEvent
use Dispatchable, InteractsWithSockets, SerializesModels;
public $goodsSkus;
public $stockWarning;
public $combinationGoodsUpdate;
/**
* Create a new event instance.
*
* @param $goodsSkuIds
* @param bool $stockWarning
* @param bool $combinationGoodsUpdate
*/
public function __construct($goodsSkuIds, bool $stockWarning = true, bool $combinationGoodsUpdate = true)
public function __construct($goodsSkuIds, bool $combinationGoodsUpdate = true)
{
$this->goodsSkus = GoodsSku::query()->whereIn('id', $goodsSkuIds)->get();
$this->stockWarning = $stockWarning;
$this->combinationGoodsUpdate = $combinationGoodsUpdate;
}

View File

@ -2,7 +2,9 @@
namespace App\Events;
use App\Models\DailyStockRecord;
use App\Models\GoodsSku;
use App\Utils\DateTimeUtils;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
@ -38,10 +40,29 @@ class BusinessOrdersUpdate
$this->goodsSku = GoodsSku::query()
->where('external_sku_id', $this->businessGoodSku['external_sku_id'])
->first();
if ($this->goodsSku) {
$this->goodsSku->stock += $this->num;
$this->goodsSku->save();
if (is_null($this->goodsSku)) {
return false;
}
$stock = $this->goodsSku->stock + $this->num;
if (0 >= $stock) {
$this->goodsSku->status = GoodsSku::$STATUS_DOWN;
} else {
$this->goodsSku->status = GoodsSku::$STATUS_ON_SALE;
}
// 今日到货 + 1T 大于20,且当前剩余库存小于4时 直接下架
$arrivedTodayNum = DailyStockRecord::query()
->where('day', DateTimeUtils::getToday())
->where('sku_id', $this->goodsSku->id)
->value('arrived_today_num');
if (20 < $arrivedTodayNum + $this->goodsSku->yesterday_num && 4 > $stock) {
$this->goodsSku->status = GoodsSku::$STATUS_DOWN;
$stock = 0;
}
$this->goodsSku->stock = $stock;
$this->goodsSku->save();
}
/**

View File

@ -2,6 +2,9 @@
namespace App\Events;
use App\Models\DailyStockRecord;
use App\Models\GoodsSku;
use App\Utils\DateTimeUtils;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
@ -13,6 +16,7 @@ use Illuminate\Queue\SerializesModels;
class StockUpdateEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $goodsSku;
/**
@ -23,6 +27,28 @@ class StockUpdateEvent
public function __construct($goodsSku)
{
$this->goodsSku = $goodsSku;
$this->checkStatusAndStock($goodsSku);
}
private function checkStatusAndStock($goodsSku)
{
$stock = $goodsSku->stock;
if (0 >= $goodsSku->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 > $goodsSku->stock) {
$status = GoodsSku::$STATUS_DOWN;
$stock = 0;
}
$goodsSku->status = $status;
$goodsSku->stock = $stock;
$goodsSku->save();
}
/**

View File

@ -2,7 +2,7 @@
namespace App\Imports;
use App\Events\BatchImportStockEvent;
use App\Events\BatchStockUpdateEvent;
use App\Jobs\SyncCostToMiaoXuan;
use App\Models\DailyStockRecord;
use App\Models\GoodsSku;
@ -99,6 +99,6 @@ class InventoryImport implements ToArray, SkipsEmptyRows
}
sleep(2);
// 批量更新
event(new BatchImportStockEvent($onSkuIds));
event(new BatchStockUpdateEvent($onSkuIds));
}
}

View File

@ -2,7 +2,7 @@
namespace App\Imports;
use App\Events\BatchImportStockEvent;
use App\Events\BatchStockUpdateEvent;
use App\Jobs\SyncCostToMiaoXuan;
use App\Models\DailyStockRecord;
use App\Models\GoodsSku;
@ -53,12 +53,14 @@ class NewSetImport implements ToArray, SkipsEmptyRows
SyncCostToMiaoXuan::dispatch($row[0], $row[3]);
$updateIds[] = $goodsSku['id'];
// 今日到货
$record = DailyStockRecord::query()->where('sku_id', $goodsSku['id'])->where('day', $day)->first(['id', 'arrived_today_num']);
$record = DailyStockRecord::query()
->where('sku_id', $goodsSku['id'])
->where('day', $day)->first(['id', 'arrived_today_num']);
$record->arrived_today_num += $row[2];
$record->save();
}
sleep(2);
// 批量更新
event(new BatchImportStockEvent($updateIds));
event(new BatchStockUpdateEvent($updateIds));
}
}

View File

@ -14,7 +14,7 @@ class BusinessGoodsSkuIncrQuantity implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $shop;
public $businessGoods;
public $businessGoodsSku;
public $num;
public $isIncremental = true;
@ -23,10 +23,10 @@ class BusinessGoodsSkuIncrQuantity implements ShouldQueue
*
* @return void
*/
public function __construct($shop, $businessGoods, $num, $isIncremental)
public function __construct($shop, $businessGoodsSku, $num, $isIncremental)
{
$this->shop = $shop;
$this->businessGoods = $businessGoods;
$this->businessGoodsSku = $businessGoodsSku;
$this->num = $num;
$this->isIncremental = $isIncremental;
}
@ -38,8 +38,8 @@ class BusinessGoodsSkuIncrQuantity implements ShouldQueue
*/
public function handle()
{
if ($this->businessGoods) {
BusinessFactory::init()->make($this->shop['plat_id'])->setShopWithId($this->shop['id'])->incrQuantity($this->businessGoods, $this->num, $this->isIncremental);
if ($this->businessGoodsSku) {
BusinessFactory::init()->make($this->shop['plat_id'])->setShopWithId($this->shop['id'])->incrQuantity($this->businessGoodsSku, $this->num, $this->isIncremental);
}
}
}

View File

@ -32,7 +32,7 @@ class BatchStockUpdateListener
{
$shops = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP])->get(['id', 'plat_id']);
if (empty($shops)) {
return;
return false;
}
foreach ($shops as $shop) {
foreach ($event->goodsSkus as $goodsSku) {

View File

@ -3,7 +3,9 @@
namespace App\Listeners;
use App\Events\BatchStockUpdateEvent;
use App\Models\DailyStockRecord;
use App\Models\GoodsSku;
use App\Utils\DateTimeUtils;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
@ -26,18 +28,22 @@ class BatchStockWarning implements ShouldQueue
if (!$event->stockWarning) {
return;
}
$warningIds = $normalIds = $downIds = [];
$dailyStockRecord = DailyStockRecord::query()
->where('day', DateTimeUtils::getToday())
->whereIn('sku_id', $event->goodsSkus->pluck('id'))
->pluck('arrived_today_num', 'sku_id')
->toArray();
$normalIds = $downIds = [];
foreach ($event->goodsSkus as $goodsSku) {
if (0 >= $goodsSku['stock']) {
$downIds[] = $goodsSku['id'];
} elseif (5 < $goodsSku['stock']) {
$normalIds[] = $goodsSku['id'];
} else {
$warningIds[] = $goodsSku['id'];
$normalIds[] = $goodsSku['id'];
}
if (20 < $dailyStockRecord[$goodsSku['id']] + $goodsSku->yesterday_num && 4 > $goodsSku['stock']) {
$downIds[] = $goodsSku['id'];
}
if ($warningIds) {
GoodsSku::query()->whereIn('id', $warningIds)->update(['status' => 2]);
}
if ($normalIds) {
GoodsSku::query()->whereIn('id', $normalIds)->update(['status' => 1]);

View File

@ -3,7 +3,9 @@
namespace App\Listeners;
use App\Models\CombinationGood;
use App\Models\DailyStockRecord;
use App\Models\GoodsSku;
use App\Utils\DateTimeUtils;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Events\BatchStockUpdateEvent;
@ -29,7 +31,7 @@ class CombinationGoodsStockUpdateListener
public function handle($event)
{
if (!$event->combinationGoodsUpdate) {
return;
return false;
}
$updateIds = $combinationGoodsIds = $combinationGoodsItemIds = [];
if (!empty($event->goodsSku)) {
@ -39,6 +41,7 @@ class CombinationGoodsStockUpdateListener
$combinationGoodsItemIds[] = $event->goodsSku->id;
}
}
if (!empty($event->goodsSkus)) {
foreach ($event->goodsSkus as $sku) {
if ($sku->is_combination) {
@ -55,21 +58,20 @@ class CombinationGoodsStockUpdateListener
->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;
$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;
}
}
// 计算主商品库存
if ($combinationGoodsItemIds) {
$goodsSkuIds = CombinationGood::query()
->whereIn('item_id', $combinationGoodsItemIds)
->pluck('goods_sku_id')
->toArray();
if (empty($goodsSkuIds)) {
return;
}
->pluck('goods_sku_id');
foreach ($goodsSkuIds as $goodsSkuId) {
$combinationGoods = CombinationGood::query()
->with('goodsSkuItem:id,stock')
@ -80,7 +82,11 @@ class CombinationGoodsStockUpdateListener
$stock[] = (int)($goods['goodsSkuItem']['stock'] / $goods['item_num']);
}
$stock = min($stock);
GoodsSku::query()->where('id', $goodsSkuId)->update(['stock' => $stock]);
$goodsSku = GoodsSku::query()->find($goodsSkuId);
[$status, $stock] = $this->checkStatusAndStock($goodsSku, $stock);
$goodsSku->status = $status;
$goodsSku->stock = $stock;
$goodsSku->save();
$updateIds[] = $goodsSkuId;
}
}
@ -90,4 +96,23 @@ class CombinationGoodsStockUpdateListener
event(new BatchStockUpdateEvent($updateIds, false));
}
}
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];
}
}

View File

@ -31,7 +31,7 @@ class StockUpdateListener
{
$shops = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP])->get(['id', 'plat_id']);
if (empty($shops)) {
return;
return false;
}
foreach ($shops as $shop) {
$num = $event->goodsSku->stock;

View File

@ -1,34 +0,0 @@
<?php
namespace App\Listeners;
use App\Models\GoodsSku;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class StockWarning implements ShouldQueue
{
use InteractsWithQueue;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
public function handle($event)
{
$event->goodsSku->status = 2;
if (0 >= $event->goodsSku->stock) {
$event->goodsSku->status = 0;
}
if (5 < $event->goodsSku->stock) {
$event->goodsSku->status = 1;
}
$event->goodsSku->save();
}
}

View File

@ -61,7 +61,11 @@ class UpdateBusinessGoodsStock implements ShouldQueue
foreach ($shops as $shop) {
$num = $event->goodsSku->stock;
$businessGoodsSkus = BusinessGoodsSku::query()->where('shop_id', $shop->id)->where('is_sync', 1)->where('external_sku_id', $event->businessGoodSku['external_sku_id'])->get();
$businessGoodsSkus = BusinessGoodsSku::query()
->where('shop_id', $shop->id)
->where('is_sync', 1)
->where('external_sku_id', $event->businessGoodSku['external_sku_id'])
->get();
foreach ($businessGoodsSkus as $businessGoodsSku) {
BusinessGoodsSkuIncrQuantity::dispatch($shop, $businessGoodsSku->toArray(), $num, false);
}

View File

@ -36,6 +36,9 @@ class GoodsSku extends Model
protected $hidden = ['created_at'];
public static $STATUS_ON_SALE = 1;
public static $STATUS_DOWN = 0;
/**
* 获取状态
*

View File

@ -2,17 +2,14 @@
namespace App\Providers;
use App\Events\BatchImportStockEvent;
use App\Events\BusinessOrdersUpdate;
use App\Events\StockUpdateEvent;
use App\Events\GroupSetEvent;
use App\Events\BatchStockUpdateEvent;
use App\Listeners\BatchStockUpdateListener;
use App\Listeners\BatchStockWarning;
use App\Listeners\CreateLogisticListener;
use App\Listeners\GroupQueryListener;
use App\Listeners\StockUpdateListener;
use App\Listeners\StockWarning;
use App\Listeners\CombinationGoodsStockUpdateListener;
use App\Listeners\UpdateBusinessGoodsStock;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@ -30,17 +27,14 @@ class EventServiceProvider extends ServiceProvider
protected $listen = [
BusinessOrdersUpdate::class => [
UpdateBusinessGoodsStock::class,
StockWarning::class,
CombinationGoodsStockUpdateListener::class,
],
BatchStockUpdateEvent::class => [
BatchStockUpdateListener::class,
BatchStockWarning::class,
CombinationGoodsStockUpdateListener::class,
],
StockUpdateEvent::class => [
StockUpdateListener::class,
StockWarning::class,
CombinationGoodsStockUpdateListener::class,
],
GroupSetEvent::class => [