This commit is contained in:
baodev0
2026-09-23 15:17:18 +07:00
parent cd38c5d6d0
commit 82a71244ca
107 changed files with 3332 additions and 3306 deletions
@@ -81,4 +81,109 @@ class InvoicePaymentServiceTest extends TestCase
$this->assertNull($invoice->fresh()->paid_at);
}
}
/** Guard mới: admin không được bật hoá đơn đã huỷ sang đã thanh toán. */
public function test_admin_cannot_mark_a_cancelled_invoice_as_paid(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$invoice = $this->makeInvoice($customer, 100_000, InvoiceStatus::CANCELLED->value);
$this->expectException(\InvalidArgumentException::class);
try {
app(InvoicePaymentService::class)->markPaidByAdmin($invoice, null);
} finally {
$this->assertSame(InvoiceStatus::CANCELLED->value, (int) $invoice->fresh()->status);
$this->assertNull($invoice->fresh()->paid_at);
}
}
/** Trả thiếu: cộng dồn amount_paid, hoá đơn vẫn chưa thanh toán, dịch vụ chưa kích hoạt. */
public function test_partial_payment_records_amount_paid_and_keeps_invoice_unpaid(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$invoice = $this->makeInvoice($customer, 100_000);
$service = app(InvoicePaymentService::class);
$invoice = $service->payWithBalance($invoice, $customer, 'balance', 40_000);
$this->assertSame(40_000, (int) $invoice->amount_paid);
$this->assertSame(InvoiceStatus::UNPAID->value, (int) $invoice->status);
$this->assertNull($invoice->paid_at);
$this->assertSame(60_000, InvoicePaymentService::outstanding($invoice));
$this->assertSame(160_000, (int) $customer->fresh()->total);
$this->assertSame(OrderStatus::PENDING->value, (int) $invoice->order->fresh()->status);
}
/** Trả nốt phần còn thiếu thì hoá đơn mới chuyển PAID và đơn hàng mới được kích hoạt. */
public function test_paying_the_remainder_settles_the_invoice(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$invoice = $this->makeInvoice($customer, 100_000);
$service = app(InvoicePaymentService::class);
$service->payWithBalance($invoice, $customer, 'balance', 40_000);
$invoice = $service->payWithBalance($invoice->fresh(), $customer->fresh(), 'balance');
$this->assertSame(100_000, (int) $invoice->amount_paid);
$this->assertSame(InvoiceStatus::PAID->value, (int) $invoice->status);
$this->assertNotNull($invoice->paid_at);
$this->assertSame(100_000, (int) $customer->fresh()->total);
$this->assertSame(OrderStatus::ACTIVE->value, (int) $invoice->order->fresh()->status);
// Mỗi lần trả là một dòng sổ riêng, không gộp cũng không ghi đè.
$this->assertSame(2, Transaction::where('customer_id', $customer->id)->where('type', 'invoice')->count());
}
/** Trả quá số còn thiếu thì chỉ trừ đúng phần còn thiếu, không thu dư. */
public function test_overpaying_only_charges_the_outstanding_amount(): void
{
$customer = Customer::factory()->create(['total' => 500_000]);
$invoice = $this->makeInvoice($customer, 100_000);
$invoice = app(InvoicePaymentService::class)->payWithBalance($invoice, $customer, 'balance', 300_000);
$this->assertSame(100_000, (int) $invoice->amount_paid);
$this->assertSame(400_000, (int) $customer->fresh()->total);
}
/** Hoàn tiền: cộng lại số dư, ghi Transaction âm, hoá đơn sang REFUNDED. */
public function test_refund_returns_balance_and_records_a_negative_transaction(): void
{
$customer = Customer::factory()->create(['total' => 200_000]);
$invoice = $this->makeInvoice($customer, 100_000);
$service = app(InvoicePaymentService::class);
$invoice = $service->payWithBalance($invoice, $customer, 'balance');
$this->assertSame(100_000, (int) $customer->fresh()->total);
$invoice = $service->refund($invoice, null, null, 'Khách huỷ dịch vụ');
$this->assertSame(InvoiceStatus::REFUNDED->value, (int) $invoice->status);
$this->assertSame(200_000, (int) $customer->fresh()->total);
$this->assertSame(
-100_000,
(int) Transaction::where('customer_id', $customer->id)->where('type', 'refund')->value('amount'),
);
}
private function makeInvoice(Customer $customer, int $total, int $status = InvoiceStatus::UNPAID->value): Invoice
{
$order = Order::create([
'order_num' => 'ORD-' . uniqid(),
'customer_id' => $customer->id,
'sub_total' => $total,
'total' => $total,
'status' => OrderStatus::PENDING->value,
]);
return Invoice::create([
'invoice_number' => 'INV-' . uniqid(),
'customer_id' => $customer->id,
'order_id' => $order->id,
'invoice_type' => 1,
'sub_total' => $total,
'tax' => 0,
'total' => $total,
'status' => $status,
]);
}
}