update
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Console;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Enums\Order\OrderStatus;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Order;
|
||||
use App\Services\InvoiceReminderService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InvoiceReminderTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
// EmailTemplateService::send() đẩy qua SendTemplateEmailJob — fake queue để test
|
||||
// không phụ thuộc template có trong DB hay không.
|
||||
Queue::fake();
|
||||
config(['billing.reminder_offsets' => [-7, -3, 0, 3]]);
|
||||
}
|
||||
|
||||
public function test_no_reminder_is_sent_before_the_first_offset(): void
|
||||
{
|
||||
$invoice = $this->makeInvoice(now()->addDays(30));
|
||||
|
||||
$this->artisan('invoices:send-reminders')->assertExitCode(0);
|
||||
|
||||
$this->assertSame(0, (int) $invoice->fresh()->reminder_step);
|
||||
}
|
||||
|
||||
public function test_reminder_step_advances_once_per_offset_reached(): void
|
||||
{
|
||||
// Hạn còn 5 ngày → đã qua mốc -7, chưa tới mốc -3.
|
||||
$invoice = $this->makeInvoice(now()->addDays(5));
|
||||
|
||||
$this->artisan('invoices:send-reminders')->assertExitCode(0);
|
||||
$this->assertSame(1, (int) $invoice->fresh()->reminder_step);
|
||||
|
||||
// Chạy lại trong ngày không được gửi trùng.
|
||||
$this->artisan('invoices:send-reminders')->assertExitCode(0);
|
||||
$this->assertSame(1, (int) $invoice->fresh()->reminder_step);
|
||||
}
|
||||
|
||||
public function test_an_overdue_invoice_reaches_the_later_offsets(): void
|
||||
{
|
||||
$invoice = $this->makeInvoice(now()->subDays(4), InvoiceStatus::OVERDUE->value);
|
||||
|
||||
$this->artisan('invoices:send-reminders')->assertExitCode(0);
|
||||
|
||||
// Đã qua cả 4 mốc: -7, -3, 0, +3.
|
||||
$this->assertSame(4, (int) $invoice->fresh()->reminder_step);
|
||||
}
|
||||
|
||||
public function test_a_paid_invoice_is_never_reminded(): void
|
||||
{
|
||||
$invoice = $this->makeInvoice(now()->subDays(10), InvoiceStatus::PAID->value);
|
||||
|
||||
$this->artisan('invoices:send-reminders')->assertExitCode(0);
|
||||
|
||||
$this->assertSame(0, (int) $invoice->fresh()->reminder_step);
|
||||
$this->assertFalse(app(InvoiceReminderService::class)->isRemindable($invoice->fresh()));
|
||||
}
|
||||
|
||||
/** Nút nhắc thủ công của kế toán gửi được kể cả khi mốc đó đã gửi rồi. */
|
||||
public function test_manual_reminder_sends_even_when_the_offset_was_already_used(): void
|
||||
{
|
||||
$invoice = $this->makeInvoice(now()->addDays(5));
|
||||
$reminders = app(InvoiceReminderService::class);
|
||||
|
||||
$this->assertTrue($reminders->remind($invoice));
|
||||
$this->assertFalse($reminders->remind($invoice->fresh()));
|
||||
$this->assertTrue($reminders->remind($invoice->fresh(), force: true));
|
||||
}
|
||||
|
||||
private function makeInvoice(\Carbon\Carbon $dueDate, int $status = InvoiceStatus::UNPAID->value): Invoice
|
||||
{
|
||||
$customer = Customer::factory()->create();
|
||||
$order = Order::create([
|
||||
'order_num' => 'ORD-' . uniqid(),
|
||||
'customer_id' => $customer->id,
|
||||
'sub_total' => 100_000,
|
||||
'total' => 100_000,
|
||||
'status' => OrderStatus::PENDING->value,
|
||||
]);
|
||||
|
||||
return Invoice::create([
|
||||
'invoice_number' => 'INV-' . uniqid(),
|
||||
'customer_id' => $customer->id,
|
||||
'order_id' => $order->id,
|
||||
'invoice_type' => 1,
|
||||
'sub_total' => 100_000,
|
||||
'tax' => 0,
|
||||
'total' => 100_000,
|
||||
'status' => $status,
|
||||
'due_date' => $dueDate->toDateString(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Console;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Enums\Order\OrderStatus;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderItem;
|
||||
use App\Models\Product;
|
||||
use App\Models\Service;
|
||||
use App\Repositories\Api\Order\OrderRepository;
|
||||
use App\Services\InvoiceReminderService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Dữ liệu import từ WHMCS mang theo hàng loạt hoá đơn quá hạn nhiều năm, đơn pending cũ và
|
||||
* dịch vụ đã hết hạn. Các test này chặn những hậu quả dây chuyền ngay sau lần sync đầu tiên.
|
||||
*/
|
||||
class PostWhmcsSyncSafetyTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Queue::fake();
|
||||
config(['billing.reminder_offsets' => [-7, -3, 0, 3, 7]]);
|
||||
}
|
||||
|
||||
/** Hoá đơn quá hạn nhiều năm KHÔNG được mở lại chuỗi nhắc nợ. */
|
||||
public function test_reminder_chain_does_not_reopen_for_long_overdue_legacy_invoices(): void
|
||||
{
|
||||
$invoice = $this->makeInvoice(now()->subYears(2), InvoiceStatus::OVERDUE->value);
|
||||
|
||||
$this->artisan('invoices:send-reminders')->assertExitCode(0);
|
||||
|
||||
$this->assertSame(0, (int) $invoice->fresh()->reminder_step);
|
||||
$this->assertSame(0, app(InvoiceReminderService::class)->dueStep($invoice->fresh()));
|
||||
}
|
||||
|
||||
/** Hoá đơn vừa quá hạn trong cửa sổ nhắc thì vẫn phải được nhắc như thường. */
|
||||
public function test_recently_overdue_invoice_is_still_reminded(): void
|
||||
{
|
||||
$invoice = $this->makeInvoice(now()->subDays(3), InvoiceStatus::OVERDUE->value);
|
||||
|
||||
$this->artisan('invoices:send-reminders')->assertExitCode(0);
|
||||
|
||||
$this->assertGreaterThan(0, (int) $invoice->fresh()->reminder_step);
|
||||
}
|
||||
|
||||
/** Admin vẫn nhắc tay được hoá đơn cũ dù chuỗi tự động đã đóng. */
|
||||
public function test_manual_reminder_still_works_on_an_old_invoice(): void
|
||||
{
|
||||
$invoice = $this->makeInvoice(now()->subYears(2), InvoiceStatus::OVERDUE->value);
|
||||
|
||||
$this->assertTrue(
|
||||
app(InvoiceReminderService::class)->remind($invoice->load('customer'), force: true)
|
||||
);
|
||||
}
|
||||
|
||||
/** Đơn import từ WHMCS ở trạng thái pending không được khoá khách đặt đơn mới. */
|
||||
public function test_legacy_pending_order_does_not_block_new_orders(): void
|
||||
{
|
||||
$customer = Customer::factory()->create();
|
||||
|
||||
Order::create([
|
||||
'legacy_id' => 90001,
|
||||
'order_num' => 'WHMCS-' . uniqid(),
|
||||
'customer_id' => $customer->id,
|
||||
'sub_total' => 500_000,
|
||||
'total' => 500_000,
|
||||
'status' => OrderStatus::PENDING->value,
|
||||
]);
|
||||
|
||||
$this->assertFalse(app(OrderRepository::class)->hasPendingOrder($customer->id));
|
||||
}
|
||||
|
||||
/** Đơn pending tạo trên portal thì vẫn phải chặn như cũ. */
|
||||
public function test_local_pending_order_still_blocks_new_orders(): void
|
||||
{
|
||||
$customer = Customer::factory()->create();
|
||||
|
||||
Order::create([
|
||||
'order_num' => 'ORD-' . uniqid(),
|
||||
'customer_id' => $customer->id,
|
||||
'sub_total' => 500_000,
|
||||
'total' => 500_000,
|
||||
'status' => OrderStatus::PENDING->value,
|
||||
]);
|
||||
|
||||
$this->assertTrue(app(OrderRepository::class)->hasPendingOrder($customer->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dịch vụ import có invoice_id NULL dù hoá đơn gia hạn của nó đã được đồng bộ —
|
||||
* không được xuất thêm hoá đơn trùng kỳ.
|
||||
*/
|
||||
public function test_expiry_does_not_duplicate_an_invoice_already_synced_on_the_order(): void
|
||||
{
|
||||
$customer = Customer::factory()->create();
|
||||
$product = Product::factory()->create(['product_type' => 'hosting', 'is_active' => true]);
|
||||
|
||||
$order = Order::create([
|
||||
'legacy_id' => 90002,
|
||||
'order_num' => 'WHMCS-' . uniqid(),
|
||||
'customer_id' => $customer->id,
|
||||
'sub_total' => 90_000,
|
||||
'total' => 90_000,
|
||||
'status' => OrderStatus::ACTIVE->value,
|
||||
]);
|
||||
|
||||
$orderItem = OrderItem::create([
|
||||
'order_id' => $order->id,
|
||||
'product_id' => $product->id,
|
||||
'billing_cycle' => 'monthly',
|
||||
'months' => 1,
|
||||
'quantity' => 1,
|
||||
'price' => 90_000,
|
||||
]);
|
||||
|
||||
// Hoá đơn gia hạn đã đồng bộ từ WHMCS, còn chưa thanh toán.
|
||||
Invoice::create([
|
||||
'legacy_id' => 90003,
|
||||
'invoice_number' => 'WHMCS-INV-' . uniqid(),
|
||||
'customer_id' => $customer->id,
|
||||
'order_id' => $order->id,
|
||||
'invoice_type' => 2,
|
||||
'sub_total' => 90_000,
|
||||
'tax' => 0,
|
||||
'total' => 90_000,
|
||||
'status' => InvoiceStatus::UNPAID->value,
|
||||
'due_date' => now()->addDays(3)->toDateString(),
|
||||
]);
|
||||
|
||||
// Service import: invoice_id để trống đúng như ServiceWriter ghi.
|
||||
$service = Service::create([
|
||||
'customer_id' => $customer->id,
|
||||
'product_id' => $product->id,
|
||||
'order_item_id' => $orderItem->id,
|
||||
'status' => 'active',
|
||||
'server_name' => 'legacy-server',
|
||||
'expires_at' => now()->addDays(3),
|
||||
]);
|
||||
$this->assertNull($service->invoice_id);
|
||||
|
||||
$this->artisan('service:expiry')->assertExitCode(0);
|
||||
|
||||
// Vẫn đúng 1 hoá đơn — và service đã được trỏ về hoá đơn đang mở.
|
||||
$this->assertSame(1, Invoice::where('order_id', $order->id)->count());
|
||||
$this->assertNotNull($service->fresh()->invoice_id);
|
||||
}
|
||||
|
||||
private function makeInvoice(\Carbon\Carbon $dueDate, int $status): Invoice
|
||||
{
|
||||
$customer = Customer::factory()->create();
|
||||
$order = Order::create([
|
||||
'order_num' => 'ORD-' . uniqid(),
|
||||
'customer_id' => $customer->id,
|
||||
'sub_total' => 100_000,
|
||||
'total' => 100_000,
|
||||
'status' => OrderStatus::PENDING->value,
|
||||
]);
|
||||
|
||||
return Invoice::create([
|
||||
'invoice_number' => 'INV-' . uniqid(),
|
||||
'customer_id' => $customer->id,
|
||||
'order_id' => $order->id,
|
||||
'invoice_type' => 1,
|
||||
'sub_total' => 100_000,
|
||||
'tax' => 0,
|
||||
'total' => 100_000,
|
||||
'status' => $status,
|
||||
'due_date' => $dueDate->toDateString(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -127,4 +127,50 @@ class ServiceExpiryTest extends TestCase
|
||||
// Vẫn đúng là OVERDUE — service:expiry không được đụng vào status của invoice.
|
||||
$this->assertSame(InvoiceStatus::OVERDUE->value, (int) $invoice->fresh()->status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hoá đơn gia hạn dùng lại order_id gốc. Khi nó quá hạn, cron KHÔNG được kéo đơn đang chạy
|
||||
* về PENDING — vừa hiển thị sai, vừa làm hasPendingOrder() khoá khách đặt đơn mới.
|
||||
*/
|
||||
public function test_overdue_renewal_invoice_does_not_reset_an_active_order(): void
|
||||
{
|
||||
$service = $this->createExpiringService(90_000);
|
||||
$order = $service->orderItem->order;
|
||||
$this->assertSame(OrderStatus::ACTIVE->value, (int) $order->status);
|
||||
|
||||
$this->artisan('service:expiry')->assertExitCode(0);
|
||||
Invoice::whereKey($service->refresh()->invoice_id)->update(['due_date' => now()->subDay()->toDateString()]);
|
||||
|
||||
$this->artisan('invoices:check-overdue')->assertExitCode(0);
|
||||
|
||||
$this->assertSame(OrderStatus::ACTIVE->value, (int) $order->fresh()->status);
|
||||
}
|
||||
|
||||
/** Giá gia hạn phải theo bảng giá hiện hành, không đóng băng theo giá lúc mua. */
|
||||
public function test_renewal_invoice_uses_the_current_price_list(): void
|
||||
{
|
||||
$service = $this->createExpiringService(90_000);
|
||||
|
||||
$service->orderItem->product->pricings()->create([
|
||||
'billing_cycle' => 'monthly',
|
||||
'price' => 120_000,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$this->artisan('service:expiry')->assertExitCode(0);
|
||||
|
||||
$invoice = Invoice::find($service->refresh()->invoice_id);
|
||||
$this->assertSame(120_000, (int) $invoice->total);
|
||||
}
|
||||
|
||||
/** Không có giá hiện hành (sản phẩm ngừng bán) thì lùi về giá cũ, vẫn xuất được hoá đơn. */
|
||||
public function test_renewal_falls_back_to_the_original_price_when_no_current_price_exists(): void
|
||||
{
|
||||
$service = $this->createExpiringService(90_000);
|
||||
|
||||
$this->artisan('service:expiry')->assertExitCode(0);
|
||||
|
||||
$invoice = Invoice::find($service->refresh()->invoice_id);
|
||||
$this->assertSame(90_000, (int) $invoice->total);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user