Files
xin-procurement/app/Models/BillModel.php
T
2026-08-29 13:54:06 +08:00

158 lines
4.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Modules\SystemUser\Models\SysUserModel;
/**
* 门店账单模型(采购单完成后按门店生成;商品金额由订单汇总快照,生成后不可修改)
*/
class BillModel extends Model
{
use HasFactory;
/** 支付状态:未支付 */
public const int STATUS_UNPAID = 0;
/** 支付状态:已支付 */
public const int STATUS_PAID = 1;
/** 支付状态中文名 */
public const array STATUS_NAMES = [
self::STATUS_UNPAID => '未支付',
self::STATUS_PAID => '已支付',
];
/** 支付进度(小程序端):待支付(可发起合并付款) */
public const int PAY_STATE_UNPAID = 0;
/** 支付进度:审核中(已提交合并付款凭证,待后台审核;审核拒绝后释放回待支付) */
public const int PAY_STATE_REVIEWING = 1;
/** 支付进度:已支付 */
public const int PAY_STATE_PAID = 2;
/** 支付进度中文名 */
public const array PAY_STATE_NAMES = [
self::PAY_STATE_UNPAID => '待支付',
self::PAY_STATE_REVIEWING => '审核中',
self::PAY_STATE_PAID => '已支付',
];
/**
* 支付进度推导:已支付 > 审核中(payment_id 锁定中)> 待支付
* (支付审核拒绝后 payment_id 释放为 0,回到待支付;线下收款直接置已支付)
*/
public function payState(): int
{
if ($this->status === self::STATUS_PAID) {
return self::PAY_STATE_PAID;
}
return $this->payment_id > 0 ? self::PAY_STATE_REVIEWING : self::PAY_STATE_UNPAID;
}
protected $table = 'bill';
protected $primaryKey = 'id';
protected $fillable = [
'bill_no',
'purchase_id',
'store_id',
'bill_date',
'product_amount',
'delivery_fee',
'box_num',
'tray_num',
'box_price',
'tray_price',
'added_amount',
'total_amount',
'status',
'payment_id',
'paid_at',
'pay_remark',
'paid_operator_id',
'operator_id',
'remark',
'after_sale',
];
protected $casts = [
'purchase_id' => 'integer',
'store_id' => 'integer',
'bill_date' => 'date:Y-m-d',
'product_amount' => 'decimal:2',
'delivery_fee' => 'decimal:2',
'box_num' => 'integer',
'tray_num' => 'integer',
'box_price' => 'decimal:2',
'tray_price' => 'decimal:2',
'added_amount' => 'decimal:2',
'total_amount' => 'decimal:2',
'after_sale' => 'decimal:2',
'status' => 'integer',
'payment_id' => 'integer',
'paid_at' => 'datetime:Y-m-d H:i:s',
'paid_operator_id' => 'integer',
'operator_id' => 'integer',
'created_at' => 'datetime:Y-m-d H:i:s',
];
/**
* 所属门店(含软删除门店,保证历史账单可见)
*/
public function store(): BelongsTo
{
return $this->belongsTo(StoreModel::class, 'store_id', 'id')->withTrashed();
}
/**
* 关联采购单
*/
public function purchase(): BelongsTo
{
return $this->belongsTo(PurchaseOrderModel::class, 'purchase_id', 'id');
}
/**
* 生成人(后台系统用户)
*/
public function operator(): BelongsTo
{
return $this->belongsTo(SysUserModel::class, 'operator_id', 'id');
}
/**
* 收款操作人(后台系统用户,线下收款登记)
*/
public function paidOperator(): BelongsTo
{
return $this->belongsTo(SysUserModel::class, 'paid_operator_id', 'id');
}
/**
* 关联支付记录(小程序合并付款)
*/
public function payment(): BelongsTo
{
return $this->belongsTo(PaymentModel::class, 'payment_id', 'id');
}
/**
* 本账单关联的门店订单
*/
public function orders(): HasMany
{
return $this->hasMany(StoreOrderModel::class, 'bill_id', 'id');
}
/**
* 本账单关联的门店订单明细
*/
public function items(): HasMany
{
return $this->hasMany(StoreOrderItemModel::class, 'bill_id', 'id');
}
}