97 lines
2.1 KiB
PHP
97 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use app\common\model\Storage;
|
|
use RuLong\Order\Contracts\Orderable;
|
|
|
|
class GoodsParams extends Model implements Orderable
|
|
{
|
|
public static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
self::created(function ($model) {
|
|
$model->paramStocks()->create(
|
|
[
|
|
'amount' => $model->stock,
|
|
'goods_id' => $model->goods_id ?? 0,
|
|
'rule_sign' => 'SHELVE_MANAGER',
|
|
]
|
|
);
|
|
});
|
|
|
|
self::updated(function ($model) {
|
|
$before_stock = $model->getOriginal('stock');
|
|
$after_stock = $model->stock;
|
|
$change_stock = $after_stock - $before_stock;
|
|
if ($change_stock != 0) {
|
|
$rule = $change_stock > 0 ? 'PLUS_MANAGER' : 'MINUS_MANAGER';
|
|
$model->paramStocks()->create(
|
|
[
|
|
'amount' => $change_stock,
|
|
'goods_id' => $model->goods_id ?? 0,
|
|
'rule_sign' => $rule,
|
|
]
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function goods()
|
|
{
|
|
return $this->belongsTo(Goods::class, 'goods_id', 'id');
|
|
}
|
|
|
|
public function getTitle()
|
|
{
|
|
return $this->goods->title . '-' . $this->value;
|
|
}
|
|
|
|
public function getPrice()
|
|
{
|
|
return $this->price;
|
|
}
|
|
|
|
public function getScore()
|
|
{
|
|
return $this->score;
|
|
}
|
|
|
|
public function getStock()
|
|
{
|
|
return $this->stock;
|
|
}
|
|
|
|
public function getSellerPrice()
|
|
{
|
|
return $this->cost;
|
|
}
|
|
|
|
public function deductStock($stock)
|
|
{
|
|
$this->decrement('stock', $stock);
|
|
}
|
|
|
|
public function addStock($stock)
|
|
{
|
|
$this->increment('stock', $stock);
|
|
}
|
|
|
|
protected function getStorageAttribute()
|
|
{
|
|
return $this->goods->storage;
|
|
}
|
|
|
|
public function paramStocks()
|
|
{
|
|
return $this->hasMany(GoodsParamStock::class, 'goods_param_id', 'id');
|
|
}
|
|
|
|
public function getStorage()
|
|
{
|
|
return $this->goods->storage;
|
|
}
|
|
|
|
}
|