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
@@ -9,6 +9,7 @@ use App\Http\Controllers\Api\Admin\Transaction\AdminTransactionController;
use App\Models\Customer;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
@@ -33,8 +34,8 @@ class AdminTransactionApproveTest extends TestCase
$controller = app(AdminTransactionController::class);
$first = $controller->approve($transaction->uuid);
$second = $controller->approve($transaction->uuid);
$first = $controller->approve(new Request(), $transaction->uuid);
$second = $controller->approve(new Request(), $transaction->uuid);
$this->assertSame(200, $first->getStatusCode());
// Second call finds the transaction no longer PENDING → treated as not-approvable.
@@ -42,4 +43,61 @@ class AdminTransactionApproveTest extends TestCase
$this->assertSame(100_000, (int) $customer->fresh()->total);
}
/**
* Khách khai 5tr nhưng sao kê chỉ về 4tr: số dư phải cộng theo số THỰC NHẬN, phần chênh
* được ghi lại để kế toán đối chiếu. Trước đây luôn cộng theo số khách khai.
*/
public function test_approving_credits_the_received_amount_not_the_declared_one(): void
{
Mail::fake();
$customer = Customer::factory()->create(['total' => 0, 'credit' => 0, 'total_pending' => 5_000_000]);
$transaction = Transaction::create([
'uuid' => (string) \Illuminate\Support\Str::uuid(),
'public_code' => 'DEP-TEST-0002',
'trans' => 'DEP-TEST-0002',
'amount' => 5_000_000,
'customer_id' => $customer->id,
'type' => 'manual',
'status' => TransactionStatus::PENDING->value,
]);
$request = Request::create('/', 'POST', ['received_amount' => 4_000_000]);
$response = app(AdminTransactionController::class)->approve($request, $transaction->uuid);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame(4_000_000, (int) $customer->fresh()->total);
$this->assertSame(40_000, (int) $customer->fresh()->credit);
$this->assertSame(0, (int) $customer->fresh()->total_pending);
$transaction->refresh();
$this->assertSame(5_000_000, (int) $transaction->amount);
$this->assertSame(4_000_000, (int) $transaction->received_amount);
$this->assertStringContainsString('Lệch ' . number_format(1_000_000), (string) $transaction->note);
}
/** Bỏ trống received_amount = tin số khách khai, giữ nguyên hành vi cũ. */
public function test_approving_without_received_amount_credits_the_declared_amount(): void
{
Mail::fake();
$customer = Customer::factory()->create(['total' => 0, 'credit' => 0, 'total_pending' => 300_000]);
$transaction = Transaction::create([
'uuid' => (string) \Illuminate\Support\Str::uuid(),
'public_code' => 'DEP-TEST-0003',
'trans' => 'DEP-TEST-0003',
'amount' => 300_000,
'customer_id' => $customer->id,
'type' => 'manual',
'status' => TransactionStatus::PENDING->value,
]);
app(AdminTransactionController::class)->approve(new Request(), $transaction->uuid);
$this->assertSame(300_000, (int) $customer->fresh()->total);
$this->assertSame(300_000, (int) $transaction->fresh()->received_amount);
$this->assertStringNotContainsString('Lệch', (string) $transaction->fresh()->note);
}
}
@@ -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,
]);
}
}