140 lines
3.7 KiB
PHP
140 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
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\SystemTool\Models\SysFileModel;
|
|
use Modules\SystemUser\Models\SysUserModel;
|
|
|
|
/**
|
|
* 支付记录模型(小程序选择门店账单合并付款,提交汇款凭证;后台审核通过后关联账单批量置已支付)
|
|
*/
|
|
class PaymentModel extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/** 支付方式:微信 */
|
|
public const int METHOD_WECHAT = 1;
|
|
/** 支付方式:支付宝 */
|
|
public const int METHOD_ALIPAY = 2;
|
|
/** 支付方式:对公汇款(银行卡) */
|
|
public const int METHOD_BANK = 3;
|
|
|
|
/** 支付方式中文名 */
|
|
public const array METHOD_NAMES = [
|
|
self::METHOD_WECHAT => '微信支付',
|
|
self::METHOD_ALIPAY => '支付宝',
|
|
self::METHOD_BANK => '对公汇款',
|
|
];
|
|
|
|
/** 状态:待审核 */
|
|
public const int STATUS_PENDING = 0;
|
|
/** 状态:已通过 */
|
|
public const int STATUS_APPROVED = 1;
|
|
/** 状态:已拒绝 */
|
|
public const int STATUS_REJECTED = 2;
|
|
|
|
/** 状态中文名 */
|
|
public const array STATUS_NAMES = [
|
|
self::STATUS_PENDING => '待审核',
|
|
self::STATUS_APPROVED => '已通过',
|
|
self::STATUS_REJECTED => '已拒绝',
|
|
];
|
|
|
|
protected $table = 'payment';
|
|
protected $primaryKey = 'id';
|
|
|
|
protected $fillable = [
|
|
'payment_no',
|
|
'store_id',
|
|
'user_id',
|
|
'amount',
|
|
'pay_method',
|
|
'voucher_ids',
|
|
'status',
|
|
'remark',
|
|
'audited_at',
|
|
'auditor_id',
|
|
'audit_remark',
|
|
];
|
|
|
|
protected $casts = [
|
|
'store_id' => 'integer',
|
|
'user_id' => 'integer',
|
|
'amount' => 'decimal:2',
|
|
'pay_method' => 'integer',
|
|
'status' => 'integer',
|
|
'audited_at' => 'datetime:Y-m-d H:i:s',
|
|
'auditor_id' => 'integer',
|
|
'created_at' => 'datetime:Y-m-d H:i:s',
|
|
];
|
|
|
|
/**
|
|
* 汇款凭证图片ID(逗号分隔字符串 ↔ 数组)
|
|
*/
|
|
public function voucherIds(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn ($value) => $value === '' || $value === null ? [] : explode(',', (string) $value),
|
|
set: fn ($value) => is_array($value) ? implode(',', $value) : $value,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 汇款凭证图片URL列表(保持提交顺序)
|
|
*
|
|
* @return string[]
|
|
*/
|
|
public function voucherUrls(): array
|
|
{
|
|
$ids = array_map('intval', $this->voucher_ids);
|
|
if ($ids === []) {
|
|
return [];
|
|
}
|
|
$urls = SysFileModel::query()->whereIn('id', $ids)->pluck('preview_url', 'id');
|
|
$result = [];
|
|
foreach ($ids as $id) {
|
|
if (isset($urls[$id])) {
|
|
$result[] = $urls[$id];
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 所属门店(含软删除门店,保证历史记录可见)
|
|
*/
|
|
public function store(): BelongsTo
|
|
{
|
|
return $this->belongsTo(StoreModel::class, 'store_id', 'id')->withTrashed();
|
|
}
|
|
|
|
/**
|
|
* 本支付记录合并付款的账单
|
|
*/
|
|
public function bills(): HasMany
|
|
{
|
|
return $this->hasMany(BillModel::class, 'payment_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* 提交人(小程序用户)
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(UserModel::class, 'user_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* 审核人(后台系统用户)
|
|
*/
|
|
public function auditor(): BelongsTo
|
|
{
|
|
return $this->belongsTo(SysUserModel::class, 'auditor_id', 'id');
|
|
}
|
|
}
|