update
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Enums\Order\OrderStatus;
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -12,7 +11,7 @@ use Illuminate\Support\Facades\Log;
|
||||
class CheckOverdueInvoices extends Command
|
||||
{
|
||||
protected $signature = 'invoices:check-overdue {--dry-run : Preview invoices without updating data}';
|
||||
protected $description = 'Mark unpaid invoices past due date as overdue and keep related orders unpaid.';
|
||||
protected $description = 'Mark unpaid invoices past due date as overdue.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
@@ -36,8 +35,6 @@ class CheckOverdueInvoices extends Command
|
||||
}
|
||||
|
||||
$invoices = Invoice::query()
|
||||
// customer_id must be selected too: OrderObserver::updated() reads it when
|
||||
// logging the status change below, and a NULL there violates log.customer_id NOT NULL.
|
||||
->with('order:id,status,customer_id')
|
||||
->whereNotNull('due_date')
|
||||
->whereDate('due_date', '<', $today)
|
||||
@@ -47,36 +44,28 @@ class CheckOverdueInvoices extends Command
|
||||
$this->info("invoices:check-overdue | Found {$invoices->count()} invoice(s) due before {$today}.");
|
||||
|
||||
$updatedInvoices = 0;
|
||||
$updatedOrders = 0;
|
||||
|
||||
foreach ($invoices as $invoice) {
|
||||
$needsInvoiceUpdate = (int) $invoice->status !== InvoiceStatus::OVERDUE->value;
|
||||
$order = $invoice->order;
|
||||
$needsOrderUpdate = $order
|
||||
&& ! in_array((int) $order->status, [OrderStatus::PENDING->value, OrderStatus::CANCELLED->value], true);
|
||||
|
||||
$this->line(sprintf(
|
||||
' [%s] Invoice #%s due %s%s',
|
||||
$dryRun ? 'dry-run' : 'check',
|
||||
$invoice->invoice_number,
|
||||
$invoice->due_date?->format('Y-m-d') ?? '-',
|
||||
$order ? " | order #{$invoice->order_id}" : ''
|
||||
$invoice->order_id ? " | order #{$invoice->order_id}" : ''
|
||||
));
|
||||
|
||||
if ($dryRun || (! $needsInvoiceUpdate && ! $needsOrderUpdate)) {
|
||||
if ($dryRun || ! $needsInvoiceUpdate) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($invoice, $order, $needsInvoiceUpdate, $needsOrderUpdate, &$updatedInvoices, &$updatedOrders) {
|
||||
if ($needsInvoiceUpdate) {
|
||||
$invoice->update(['status' => InvoiceStatus::OVERDUE->value]);
|
||||
$updatedInvoices++;
|
||||
}
|
||||
|
||||
if ($needsOrderUpdate && $order) {
|
||||
$order->update(['status' => OrderStatus::PENDING->value]);
|
||||
$updatedOrders++;
|
||||
}
|
||||
// Chỉ đổi trạng thái hoá đơn. Trước đây command còn đẩy order về PENDING, nhưng hoá đơn
|
||||
// gia hạn dùng lại order_id gốc nên một kỳ gia hạn quá hạn sẽ kéo đơn đang chạy về
|
||||
// "Chờ xử lý" — vừa hiển thị sai, vừa khiến hasPendingOrder() khoá khách đặt đơn mới.
|
||||
DB::transaction(function () use ($invoice, &$updatedInvoices) {
|
||||
$invoice->update(['status' => InvoiceStatus::OVERDUE->value]);
|
||||
$updatedInvoices++;
|
||||
});
|
||||
|
||||
Log::info('Invoice marked overdue', [
|
||||
@@ -86,7 +75,7 @@ class CheckOverdueInvoices extends Command
|
||||
]);
|
||||
}
|
||||
|
||||
$this->info("invoices:check-overdue | Updated {$updatedInvoices} invoice(s), {$updatedOrders} order(s).");
|
||||
$this->info("invoices:check-overdue | Updated {$updatedInvoices} invoice(s).");
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user