- 機台系統設定:取物單模式(pickup_sheet)下新增「領藥單」開關 pharmacy_pickup_enabled,B014 下發 FunctionSet.PharmacyPickup - 權限:銷售管理→領藥單 menu.sales.pharmacy-pickup(角色/帳號可勾選授權,租戶模板預設關) - 資料模型:orders 加 order_type/pricing_slip_no/created_by(領藥單序號=flow_id)、pickup_codes.slot_no 可空 - 後台:PharmacyPickupController + PharmacyPickupService(以商品為單位建單、庫存不預扣)+ 建單/列表/列印頁(QR內嵌,不顯姓名) - B660:領藥單回多貨道 items[](保留 slot_no 向後相容)+ 模式 gate + 標記已領 - 出貨回報:finalizePharmacyDispense(獨立於銷售/發票/閉環)+ flow_id 終態冪等 + 庫存只扣一次 - 修正 PickupCode::isValid() 對 null expires_at 的 NPE、status 補入 fillable Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
104 lines
2.2 KiB
PHP
104 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Transaction;
|
|
|
|
use App\Models\Machine\Machine;
|
|
use App\Models\System\User;
|
|
use App\Traits\TenantScoped;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class PickupCode extends Model
|
|
{
|
|
use TenantScoped;
|
|
|
|
protected $fillable = [
|
|
'company_id',
|
|
'machine_id',
|
|
'slot_no',
|
|
'code',
|
|
'slug',
|
|
'status',
|
|
'expires_at',
|
|
'used_at',
|
|
'usage_limit',
|
|
'usage_count',
|
|
'created_by',
|
|
'order_id',
|
|
];
|
|
|
|
/**
|
|
* 獲取公開取貨連結
|
|
*/
|
|
public function getTicketUrlAttribute()
|
|
{
|
|
return $this->slug ? route('pickup.ticket', $this->slug) : route('pickup.ticket', $this->code);
|
|
}
|
|
|
|
/**
|
|
* Boot the model.
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function ($pickupCode) {
|
|
$pickupCode->slug = \Illuminate\Support\Str::random(16);
|
|
});
|
|
}
|
|
|
|
protected $casts = [
|
|
'expires_at' => 'datetime',
|
|
'used_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* 關聯機台
|
|
*/
|
|
public function machine(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Machine::class);
|
|
}
|
|
|
|
/**
|
|
* 關聯建立者
|
|
*/
|
|
public function creator(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
/**
|
|
* 關聯訂單
|
|
*/
|
|
public function order(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Order::class);
|
|
}
|
|
|
|
/**
|
|
* 判斷是否可用
|
|
*/
|
|
public function isValid(): bool
|
|
{
|
|
return $this->status === 'active'
|
|
&& ($this->usage_count < $this->usage_limit)
|
|
&& ($this->expires_at?->isFuture() ?? true); // null expires_at = 無到期限制
|
|
}
|
|
|
|
/**
|
|
* 產生唯一的取貨碼 (8位)
|
|
*/
|
|
public static function generateUniqueCode(int $machineId): string
|
|
{
|
|
do {
|
|
$code = str_pad(rand(0, 99999999), 8, '0', STR_PAD_LEFT);
|
|
$exists = self::where('machine_id', $machineId)
|
|
->where('code', $code)
|
|
->where('status', 'active')
|
|
->where('expires_at', '>', now())
|
|
->exists();
|
|
} while ($exists);
|
|
|
|
return $code;
|
|
}
|
|
}
|