feat(services): partial/unpaid payment on admin create, payment history per invoice, zip attachments
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
02daefe4c2
commit
fe68171aa7
@@ -35,7 +35,7 @@ class OrderInvoiceExportTest extends TestCase
|
||||
foreach (['first.vn', 'second.vn'] as $serverName) {
|
||||
$this->actingAs($this->admin, 'admin')->postJson(
|
||||
"/api/v1/administrator/customers/{$customer->uuid}/services",
|
||||
['product_id' => $product->id, 'payment_method' => 'transfer', 'server_name' => $serverName, 'amount_paid' => 0],
|
||||
['product_id' => $product->id, 'payment_method' => 'transfer', 'server_name' => $serverName, 'price' => 0],
|
||||
)->assertCreated();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ class CreateDomainServicePricingTest extends TestCase
|
||||
$this->createService(['server_name' => 'example.xyz'])->assertStatus(422);
|
||||
$this->assertSame(0, Service::count());
|
||||
|
||||
$this->createService(['server_name' => 'example.xyz', 'amount_paid' => 250_000])->assertCreated();
|
||||
$this->createService(['server_name' => 'example.xyz', 'price' => 250_000])->assertCreated();
|
||||
$this->assertDatabaseHas('order_items', ['domain_name' => 'example.xyz', 'price' => 250_000]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Api\Admin\Customer;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Product;
|
||||
use App\Models\Transaction;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CreateServicePaymentStatusTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private User $admin;
|
||||
|
||||
private Customer $customer;
|
||||
|
||||
private Product $product;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
|
||||
$this->admin = User::factory()->create();
|
||||
$this->admin->assignRole('Full Administrator');
|
||||
$this->customer = Customer::factory()->create(['total' => 1_000_000]);
|
||||
$this->product = Product::factory()->create(['product_type' => 'hosting']);
|
||||
}
|
||||
|
||||
private function createService(array $data)
|
||||
{
|
||||
return $this->actingAs($this->admin, 'admin')->postJson(
|
||||
"/api/v1/administrator/customers/{$this->customer->uuid}/services",
|
||||
['product_id' => $this->product->id, 'server_name' => 'site.vn', 'price' => 500_000, ...$data],
|
||||
);
|
||||
}
|
||||
|
||||
public function test_paid_in_full_marks_invoice_paid(): void
|
||||
{
|
||||
$this->createService(['payment_status' => 'paid', 'payment_method' => 'balance'])->assertCreated();
|
||||
|
||||
$invoice = Invoice::sole();
|
||||
$this->assertSame(InvoiceStatus::PAID->value, (int) $invoice->status);
|
||||
$this->assertSame(500_000, (int) $invoice->amount_paid);
|
||||
$this->assertSame(500_000, (int) $this->customer->fresh()->total);
|
||||
}
|
||||
|
||||
public function test_partial_payment_keeps_invoice_unpaid_with_prepaid_amount(): void
|
||||
{
|
||||
$this->createService(['payment_status' => 'partial', 'payment_method' => 'balance', 'amount_paid' => 200_000])->assertCreated();
|
||||
|
||||
$invoice = Invoice::sole();
|
||||
$this->assertSame(InvoiceStatus::UNPAID->value, (int) $invoice->status);
|
||||
$this->assertSame(500_000, (int) $invoice->total);
|
||||
$this->assertSame(200_000, (int) $invoice->amount_paid);
|
||||
$this->assertSame(200_000, (int) Transaction::sole()->amount);
|
||||
$this->assertSame(800_000, (int) $this->customer->fresh()->total);
|
||||
}
|
||||
|
||||
public function test_service_detail_lists_every_payment_installment_of_an_invoice(): void
|
||||
{
|
||||
$serviceId = $this->createService(['payment_status' => 'partial', 'payment_method' => 'balance', 'amount_paid' => 200_000])
|
||||
->assertCreated()
|
||||
->json('data.id');
|
||||
$invoice = Invoice::sole();
|
||||
$admin = $this->actingAs($this->admin, 'admin');
|
||||
|
||||
$admin->postJson("/api/v1/administrator/invoices/{$invoice->id}/paid", ['received_amount' => 100_000])->assertOk();
|
||||
$admin->postJson("/api/v1/administrator/invoices/{$invoice->id}/paid", ['note' => 'Đợt cuối'])->assertOk();
|
||||
|
||||
$payments = $admin->getJson("/api/v1/administrator/services/{$serviceId}")
|
||||
->assertOk()
|
||||
->json('data.linked_invoices.0.payments');
|
||||
|
||||
$this->assertSame([200_000, 100_000, 200_000], array_column($payments, 'amount'));
|
||||
$this->assertSame(InvoiceStatus::PAID->value, (int) $invoice->fresh()->status);
|
||||
}
|
||||
|
||||
public function test_decimal_amounts_are_rounded_to_whole_dong(): void
|
||||
{
|
||||
$this->createService(['price' => 731.33, 'payment_status' => 'partial', 'payment_method' => 'balance', 'amount_paid' => 400.6])->assertCreated();
|
||||
|
||||
$invoice = Invoice::sole();
|
||||
$this->assertSame(731, (int) $invoice->total);
|
||||
$this->assertSame(401, (int) $invoice->amount_paid);
|
||||
}
|
||||
|
||||
public function test_partial_payment_must_be_less_than_price(): void
|
||||
{
|
||||
$this->createService(['payment_status' => 'partial', 'payment_method' => 'balance', 'amount_paid' => 500_000])->assertStatus(422);
|
||||
$this->createService(['payment_status' => 'partial', 'payment_method' => 'balance'])->assertStatus(422);
|
||||
$this->assertSame(0, Invoice::count());
|
||||
}
|
||||
|
||||
public function test_unpaid_creates_unpaid_invoice_without_transaction(): void
|
||||
{
|
||||
$this->createService(['payment_status' => 'unpaid'])->assertCreated();
|
||||
|
||||
$invoice = Invoice::sole();
|
||||
$this->assertSame(InvoiceStatus::UNPAID->value, (int) $invoice->status);
|
||||
$this->assertSame(0, (int) $invoice->amount_paid);
|
||||
$this->assertSame(0, Transaction::count());
|
||||
$this->assertSame(1_000_000, (int) $this->customer->fresh()->total);
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ class DeleteCustomerServiceOrdersTest extends TestCase
|
||||
{
|
||||
$this->actingAs($this->admin, 'admin')->postJson(
|
||||
"/api/v1/administrator/customers/{$this->customer->uuid}/services",
|
||||
['product_id' => $this->product->id, 'payment_method' => 'transfer', 'server_name' => $serverName, 'amount_paid' => 0],
|
||||
['product_id' => $this->product->id, 'payment_method' => 'transfer', 'server_name' => $serverName, 'price' => 0],
|
||||
)->assertCreated();
|
||||
|
||||
return Service::where('server_name', $serverName)->firstOrFail();
|
||||
|
||||
@@ -127,7 +127,7 @@ class MarkTest extends TestCase
|
||||
foreach (['a.vn', 'b.vn'] as $serverName) {
|
||||
$this->actingAs($admin, 'admin')->postJson(
|
||||
"/api/v1/administrator/customers/{$customer->uuid}/services",
|
||||
['product_id' => $product->id, 'payment_method' => 'transfer', 'server_name' => $serverName, 'amount_paid' => 0],
|
||||
['product_id' => $product->id, 'payment_method' => 'transfer', 'server_name' => $serverName, 'price' => 0],
|
||||
)->assertCreated();
|
||||
}
|
||||
$tag = Tag::create(['title' => 'Gấp', 'color' => '#fa8c16']);
|
||||
|
||||
@@ -73,6 +73,19 @@ class ServiceAttachmentTest extends TestCase
|
||||
Storage::disk('public')->assertExists($copy->path);
|
||||
}
|
||||
|
||||
public function test_upload_accepts_zip_and_rejects_executables(): void
|
||||
{
|
||||
$base = "/api/v1/administrator/services/{$this->service->id}/attachments";
|
||||
$admin = $this->actingAs($this->admin, 'admin');
|
||||
|
||||
$admin->post($base, ['file' => UploadedFile::fake()->create('source.zip', 10, 'application/zip')], ['Accept' => 'application/json'])
|
||||
->assertCreated();
|
||||
$admin->post($base, ['file' => UploadedFile::fake()->create('run.exe', 10, 'application/x-msdownload')], ['Accept' => 'application/json'])
|
||||
->assertUnprocessable();
|
||||
|
||||
$this->assertDatabaseCount('service_attachments', 1);
|
||||
}
|
||||
|
||||
public function test_list_and_detail_expose_linked_order_and_invoices(): void
|
||||
{
|
||||
$admin = $this->actingAs($this->admin, 'admin');
|
||||
@@ -85,7 +98,8 @@ class ServiceAttachmentTest extends TestCase
|
||||
|
||||
$admin->getJson("/api/v1/administrator/services/{$this->service->id}")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.linked_invoices.0.id', $this->invoice->id);
|
||||
->assertJsonPath('data.linked_invoices.0.id', $this->invoice->id)
|
||||
->assertJsonPath('data.linked_invoices.0.amount_paid', 0);
|
||||
}
|
||||
|
||||
public function test_rejects_unsupported_file_type(): void
|
||||
|
||||
@@ -26,7 +26,7 @@ class ServiceListProductTypeFilterTest extends TestCase
|
||||
foreach (['email' => 'mail.vn', 'hardware' => 'box.vn'] as $type => $serverName) {
|
||||
$this->actingAs($admin, 'admin')->postJson(
|
||||
"/api/v1/administrator/customers/{$customer->uuid}/services",
|
||||
['product_id' => Product::factory()->create(['product_type' => $type])->id, 'payment_method' => 'transfer', 'server_name' => $serverName, 'amount_paid' => 0],
|
||||
['product_id' => Product::factory()->create(['product_type' => $type])->id, 'payment_method' => 'transfer', 'server_name' => $serverName, 'price' => 0],
|
||||
)->assertCreated();
|
||||
}
|
||||
Service::where('server_name', 'box.vn')->update(['type' => null]);
|
||||
|
||||
Reference in New Issue
Block a user