erp/app/Listeners/StockWarning.php

58 lines
1.6 KiB
PHP
Raw Normal View History

2022-08-16 21:02:31 +08:00
<?php
namespace App\Listeners;
2022-08-20 13:42:22 +08:00
use App\Models\GoodsSku;
2022-08-16 21:02:31 +08:00
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)
{
2022-08-20 15:45:40 +08:00
if (isset($event->goodsSku->stock)) {
$event->goodsSku->status = 2;
if (0 >= $event->goodsSku->stock) {
$event->goodsSku->status = 0;
}
if (5 < $event->goodsSku->stock) {
2022-08-20 15:45:40 +08:00
$event->goodsSku->status = 1;
}
2022-08-16 21:02:31 +08:00
$event->goodsSku->save();
}
if (isset($event->goodsSkus)) {
$warningIds = $normalIds = $downIds = [];
2022-08-16 21:02:31 +08:00
foreach ($event->goodsSkus as $goodsSku) {
if (0 >= $goodsSku['stock']) {
$downIds[] = $goodsSku['id'];
} elseif (5 < $goodsSku['stock']) {
2022-08-20 15:45:40 +08:00
$normalIds[] = $goodsSku['id'];
} else {
$warningIds[] = $goodsSku['id'];
2022-08-20 15:45:40 +08:00
}
}
if ($warningIds) {
2022-09-16 11:13:51 +08:00
GoodsSku::query()->whereIn('id', $warningIds)->update(['status' => 2]);
2022-08-16 21:02:31 +08:00
}
2022-08-20 15:45:40 +08:00
if ($normalIds) {
2022-09-16 11:13:51 +08:00
GoodsSku::query()->whereIn('id', $normalIds)->update(['status' => 1]);
2022-08-16 21:02:31 +08:00
}
if ($downIds) {
GoodsSku::query()->whereIn('id', $downIds)->update(['status' => 0]);
}
2022-08-16 21:02:31 +08:00
}
}
}