create(['total' => 0, 'credit' => 0, 'total_pending' => 100_000]); $transaction = Transaction::create([ 'uuid' => (string) \Illuminate\Support\Str::uuid(), 'public_code' => 'DEP-TEST-0001', 'trans' => 'DEP-TEST-0001', 'amount' => 100_000, 'customer_id' => $customer->id, 'type' => 'manual', 'status' => TransactionStatus::PENDING->value, 'proof_image' => 'deposit-proofs/test.jpg', ]); $controller = app(AdminTransactionController::class); $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. $this->assertSame(404, $second->getStatusCode()); $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, 'proof_image' => 'deposit-proofs/test.jpg', ]); $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, 'proof_image' => 'deposit-proofs/test.jpg', ]); 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); } }