1. 領藥單後台:新增 PharmacyPickupController 與 PharmacyPickupService,提供領藥單列表/建立/作廢/列印 QR;routes/web.php 新增受 menu.sales.pharmacy-pickup 權限控管的路由群組,RoleSeeder 補該權限、sidebar 新增選單。 2. 訂單模型:Order 新增 order_type(sale/pharmacy_pickup)、pricing_slip_no、created_by 欄位與 AWAITING_PICKUP 狀態,新增 sales/pharmacyPickup scope 及 creator/pickupCode 關聯。 3. 機台設定:系統設定新增「領藥單」開關,僅在取物單(pickup_sheet)模式下可啟用、其他模式強制關閉;含後端防呆、toggle UI 與啟用功能標籤顯示。 4. 機台 API:getSettings FunctionSet 增加 PharmacyPickup 旗標;B660 取貨核銷擴充領藥單多貨道出貨清單回傳,庫存不足/無可用貨道時嚴格擋關且不消耗領藥碼。 5. 出貨流程:TransactionService finalizeTransaction 以 order_no(RX) 對應預建領藥單,走 finalizePharmacyDispense 獨立冪等出貨流程,並標記領藥碼已領與核銷日誌。 6. 防呆修正:recordDispense 庫存<=0 時不再扣減以避免負庫存;syncSlots 上報清單為空時不執行全量刪除,避免誤清貨道。 7. 資料庫:新增 2 個 migration(orders 增領藥單欄位、pickup_codes 之 slot_no 改 nullable);PickupCode 開放 status 可填、isValid 容許 null expires_at。 8. 多語系/文件:zh_TW/en/ja 三語系對齊補齊領藥單相關詞彙(27 個);config/api-docs.php 補 PharmacyPickup 旗標說明。
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;
|
|
}
|
|
}
|