2022-08-11 11:18:21 +08:00
|
|
|
<?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();
|
2022-08-11 11:18:21 +08:00
|
|
|
$this->num = $num;
|
|
|
|
|
$this->updateStock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function updateStock()
|
|
|
|
|
{
|
2022-08-17 20:50:03 +08:00
|
|
|
[$goodsCode, $skuCode] = explode('_', $this->businessOrderItem['external_sku_id']);
|
2022-08-11 11:18:21 +08:00
|
|
|
$this->goodsSku = GoodsSku::query()->where('sku_code', $skuCode)
|
2022-08-18 14:57:38 +08:00
|
|
|
->whereHas('goods', function ($query) use ($goodsCode) {
|
2022-08-11 11:18:21 +08:00
|
|
|
$query->where('goods_code', $goodsCode);
|
2022-08-18 14:57:38 +08:00
|
|
|
})
|
2022-08-11 11:18:21 +08:00
|
|
|
->first();
|
|
|
|
|
if ($this->goodsSku) {
|
|
|
|
|
$this->goodsSku->stock += $this->num;
|
|
|
|
|
$this->goodsSku->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the channels the event should broadcast on.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Broadcasting\Channel|array
|
|
|
|
|
*/
|
|
|
|
|
public function broadcastOn()
|
|
|
|
|
{
|
|
|
|
|
return new PrivateChannel('channel-name');
|
|
|
|
|
}
|
|
|
|
|
}
|