erp/app/Events/BusinessOrdersUpdate.php

66 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Events;
use App\Models\BusinessOrderItem;
use App\Models\GoodsSku;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class BusinessOrdersUpdate
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $num;
public $businessOrderItem;
public $goodsSku;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(BusinessOrderItem $item, $num)
{
2022-08-17 20:50:03 +08:00
$this->businessOrderItem = $item->toArray();
$this->num = $num;
$this->updateStock();
}
private function updateStock()
{
2022-08-19 10:54:13 +08:00
try {
[$goodsCode, $skuCode] = explode('_', $this->businessOrderItem['external_sku_id']);
$this->goodsSku = GoodsSku::query()->where('sku_code', $skuCode)
->whereHas('goods', function ($query) use ($goodsCode) {
$query->where('goods_code', $goodsCode);
})
->first();
} catch (\Exception $e) {
return false;
}
if ($this->goodsSku) {
2022-08-22 19:55:21 +08:00
$this->goodsSku->stock += $this->num;
if ($this->goodsSku->stock <= 0) {
2022-08-20 15:45:40 +08:00
$this->goodsSku->status = 0;
}
$this->goodsSku->save();
}
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}