Files
xin-procurement/app/Models/CustomerLevelModel.php
T
2026-08-17 16:27:15 +08:00

84 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Modules\SystemTool\Models\SysFileModel;
/**
* 客户等级模型(同一商品按等级上浮比例定价:售价 = 成本价 × (100 + percent) / 100
*/
class CustomerLevelModel extends Model
{
use HasFactory;
/** 状态:停用 */
public const int STATUS_DISABLED = 0;
/** 状态:正常 */
public const int STATUS_NORMAL = 1;
protected $table = 'customer_level';
protected $primaryKey = 'id';
protected $fillable = [
'name',
'percent',
'sort',
'status',
'icon_id'
];
protected $casts = [
'percent' => 'decimal:2',
'sort' => 'integer',
'status' => 'integer',
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
];
protected $appends = ['icon_url'];
/**
* 关联图标
*/
public function icon(): HasOne
{
return $this->hasOne(SysFileModel::class, 'id', 'icon_id');
}
// 图标链接
public function getIconUrlAttribute()
{
if($this->icon) {
return $this->icon->preview_url;
}
return null;
}
/**
* 该等级下的门店
*/
public function stores(): HasMany
{
return $this->hasMany(StoreModel::class, 'level_id', 'id');
}
/**
* 按等级上浮比例计算售价(统一换算入口,金额走 bcmath 保证两位小数精度)
*
* 售价 = 成本价 × (100 + percent) / 100(四舍五入保留两位)。
*
* @param string|int|float $costPrice 商品成本价(decimal cast 字符串)
* @param string|int|float $percent 价格上浮比例(30 = 上浮 30%)
* @return string 两位小数字符串,如 '13.05'
*/
public static function calcLevelPrice(string|int|float $costPrice, string|int|float $percent): string
{
$multiplier = bcadd('100', (string) $percent, 4);
return bcdiv(bcmul((string) $costPrice, $multiplier, 4), '100', 2);
}
}