mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-11-30 22:20:45 +00:00
75 lines
2.7 KiB
PHP
75 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Listeners;
|
|
|
|
use App\Events\StockUpdateEvent;
|
|
use App\Jobs\BusinessGoodsSkuIncrQuantity;
|
|
use App\Models\BusinessGoodsSku;
|
|
use App\Models\Shop;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use App\Services\Business\BusinessFactory;
|
|
|
|
class StockUpdateListener
|
|
{
|
|
/**
|
|
* Create the event listener.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Handle the event.
|
|
*
|
|
* @param StockUpdateEvent $event
|
|
* @return void
|
|
*/
|
|
public function handle(StockUpdateEvent $event)
|
|
{
|
|
$shops = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP])->get(['id', 'plat_id']);
|
|
if (empty($shops)) {
|
|
return;
|
|
}
|
|
foreach ($shops as $shop) {
|
|
if (isset($event->goodsSku)) {
|
|
$num = $event->goodsSku->stock;
|
|
$businessGoodsSkus = BusinessGoodsSku::query()
|
|
->select(['goods_id', 'sku_id'])
|
|
->where('shop_id', $shop->id)
|
|
->where('is_sync', 1)
|
|
->where('external_sku_id', $event->goodsSku->goods['goods_code'] . '_' . $event->goodsSku->sku_code)
|
|
->get();
|
|
if ($event->isBatch) {
|
|
BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false);
|
|
} else {
|
|
foreach ($businessGoodsSkus as $businessGoodsSku) {
|
|
BusinessGoodsSkuIncrQuantity::dispatch($shop, $businessGoodsSku->toArray(), $num, false);
|
|
}
|
|
}
|
|
}
|
|
if (isset($event->goodsSkus)) {
|
|
foreach ($event->goodsSkus as $goodsSku) {
|
|
$num = $goodsSku->stock;
|
|
$businessGoodsSkus = BusinessGoodsSku::query()
|
|
->select(['goods_id', 'sku_id'])
|
|
->where('shop_id', $shop->id)
|
|
->where('is_sync', 1)
|
|
->where('external_sku_id', $goodsSku->goods['goods_code'] . '_' . $goodsSku->sku_code)
|
|
->get();
|
|
if ($event->isBatch) {
|
|
BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false);
|
|
} else {
|
|
foreach ($businessGoodsSkus as $businessGoodsSku) {
|
|
BusinessGoodsSkuIncrQuantity::dispatch($shop, $businessGoodsSku->toArray(), $num, false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|