erp/app/Listeners/StockUpdateListener.php

50 lines
1.3 KiB
PHP
Raw Normal View History

2022-08-16 21:02:31 +08:00
<?php
namespace App\Listeners;
use App\Events\StockUpdateEvent;
use App\Models\BusinessGoodsSku;
use App\Models\Shop;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
2022-11-04 15:01:55 +08:00
use App\Services\Business\BusinessFactory;
2022-08-16 21:02:31 +08:00
2024-03-23 11:28:58 +08:00
class StockUpdateListener implements ShouldQueue
2022-08-16 21:02:31 +08:00
{
2024-03-23 11:28:58 +08:00
use InteractsWithQueue;
2022-08-16 21:02:31 +08:00
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param StockUpdateEvent $event
* @return void
*/
public function handle(StockUpdateEvent $event)
{
2022-10-25 10:47:36 +08:00
$shops = Shop::query()->whereNotIn('status', [Shop::$STATUS_UNAUTHORIZED, Shop::$STATUS_STOP])->get(['id', 'plat_id']);
2022-08-16 21:02:31 +08:00
if (empty($shops)) {
2024-03-23 11:28:58 +08:00
return;
2022-08-16 21:02:31 +08:00
}
foreach ($shops as $shop) {
2023-11-18 14:35:19 +08:00
$num = $event->goodsSku->stock;
$businessGoodsSkus = BusinessGoodsSku::query()
->select(['goods_id', 'sku_id', 'external_sku_id'])
->where('shop_id', $shop->id)
->where('is_sync', 1)
->where('external_sku_id', $event->goodsSku->external_sku_id)
->get();
BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id'])->batchIncrQuantity($businessGoodsSkus->toArray(), $num, false);
2022-08-16 21:02:31 +08:00
}
}
}