|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Modules\Projects\Tests\Feature; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use Modules\Clients\Models\Relation; |
| 7 | +use Modules\Core\Tests\AbstractCompanyPanelTestCase; |
| 8 | +use Modules\Invoices\Enums\InvoiceStatus; |
| 9 | +use Modules\Invoices\Models\Invoice; |
| 10 | +use Modules\Projects\Enums\TaskStatus; |
| 11 | +use Modules\Projects\Models\Project; |
| 12 | +use Modules\Projects\Models\Task; |
| 13 | +use Modules\Projects\Services\ProjectBillingService; |
| 14 | +use PHPUnit\Framework\Attributes\Group; |
| 15 | +use PHPUnit\Framework\Attributes\Test; |
| 16 | + |
| 17 | +class ProjectBillingTest extends AbstractCompanyPanelTestCase |
| 18 | +{ |
| 19 | + private ProjectBillingService $service; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + |
| 25 | + $this->actingAs($this->user); |
| 26 | + $this->service = app(ProjectBillingService::class); |
| 27 | + } |
| 28 | + |
| 29 | + #[Test] |
| 30 | + #[Group('crud')] |
| 31 | + public function it_bills_completed_tasks_to_a_new_draft_invoice(): void |
| 32 | + { |
| 33 | + /* Arrange */ |
| 34 | + $project = $this->createProject(); |
| 35 | + $taskA = $this->createTask($project, ['task_name' => 'Design', 'task_price' => 100]); |
| 36 | + $taskB = $this->createTask($project, ['task_name' => 'Build', 'task_price' => 250.5]); |
| 37 | + |
| 38 | + /* Act */ |
| 39 | + $invoice = $this->service->billTasks($project, [$taskA->id, $taskB->id]); |
| 40 | + |
| 41 | + /* Assert */ |
| 42 | + $this->assertSame(InvoiceStatus::DRAFT, $invoice->invoice_status); |
| 43 | + $this->assertSame($project->customer_id, $invoice->customer_id); |
| 44 | + $this->assertNull($invoice->invoice_number); |
| 45 | + $this->assertCount(2, $invoice->invoiceItems); |
| 46 | + $this->assertEqualsWithDelta(350.5, (float) $invoice->invoice_item_subtotal, 0.001); |
| 47 | + |
| 48 | + $this->assertDatabaseHas('invoice_items', [ |
| 49 | + 'invoice_id' => $invoice->id, |
| 50 | + 'task_id' => $taskA->id, |
| 51 | + 'item_name' => 'Design', |
| 52 | + ]); |
| 53 | + } |
| 54 | + |
| 55 | + #[Test] |
| 56 | + #[Group('crud')] |
| 57 | + public function it_appends_tasks_to_an_existing_draft_invoice(): void |
| 58 | + { |
| 59 | + /* Arrange */ |
| 60 | + $project = $this->createProject(); |
| 61 | + $taskA = $this->createTask($project, ['task_price' => 100]); |
| 62 | + $taskB = $this->createTask($project, ['task_price' => 50]); |
| 63 | + |
| 64 | + $first = $this->service->billTasks($project, [$taskA->id]); |
| 65 | + |
| 66 | + /* Act */ |
| 67 | + $second = $this->service->billTasks($project, [$taskB->id]); |
| 68 | + |
| 69 | + /* Assert */ |
| 70 | + $this->assertSame($first->id, $second->id); |
| 71 | + $this->assertCount(2, $second->invoiceItems); |
| 72 | + $this->assertSame(1, Invoice::query()->count()); |
| 73 | + } |
| 74 | + |
| 75 | + #[Test] |
| 76 | + #[Group('crud')] |
| 77 | + public function it_does_not_bill_the_same_task_twice(): void |
| 78 | + { |
| 79 | + /* Arrange */ |
| 80 | + $project = $this->createProject(); |
| 81 | + $task = $this->createTask($project, ['task_price' => 100]); |
| 82 | + |
| 83 | + $this->service->billTasks($project, [$task->id]); |
| 84 | + |
| 85 | + /* Assert */ |
| 86 | + $this->expectException(InvalidArgumentException::class); |
| 87 | + |
| 88 | + /* Act */ |
| 89 | + $this->service->billTasks($project, [$task->id]); |
| 90 | + } |
| 91 | + |
| 92 | + #[Test] |
| 93 | + #[Group('crud')] |
| 94 | + public function it_rejects_tasks_that_are_not_completed(): void |
| 95 | + { |
| 96 | + /* Arrange */ |
| 97 | + $project = $this->createProject(); |
| 98 | + $task = $this->createTask($project, ['task_status' => TaskStatus::IN_PROGRESS->value]); |
| 99 | + |
| 100 | + /* Assert */ |
| 101 | + $this->expectException(InvalidArgumentException::class); |
| 102 | + |
| 103 | + /* Act */ |
| 104 | + $this->service->billTasks($project, [$task->id]); |
| 105 | + } |
| 106 | + |
| 107 | + #[Test] |
| 108 | + #[Group('crud')] |
| 109 | + public function it_only_offers_completed_unbilled_tasks_as_options(): void |
| 110 | + { |
| 111 | + /* Arrange */ |
| 112 | + $project = $this->createProject(); |
| 113 | + $completed = $this->createTask($project, ['task_name' => 'Done']); |
| 114 | + $open = $this->createTask($project, [ |
| 115 | + 'task_name' => 'Open', |
| 116 | + 'task_status' => TaskStatus::OPEN->value, |
| 117 | + ]); |
| 118 | + $billed = $this->createTask($project, ['task_name' => 'Billed']); |
| 119 | + $this->service->billTasks($project, [$billed->id]); |
| 120 | + |
| 121 | + /* Act */ |
| 122 | + $options = $this->service->billableTaskOptions($project); |
| 123 | + |
| 124 | + /* Assert */ |
| 125 | + $this->assertSame([$completed->id => 'Done'], $options); |
| 126 | + } |
| 127 | + |
| 128 | + private function createProject(): Project |
| 129 | + { |
| 130 | + $customer = Relation::factory()->for($this->company)->customer()->create(); |
| 131 | + |
| 132 | + return Project::factory()->for($this->company)->create([ |
| 133 | + 'customer_id' => $customer->id, |
| 134 | + ]); |
| 135 | + } |
| 136 | + |
| 137 | + private function createTask(Project $project, array $attributes = []): Task |
| 138 | + { |
| 139 | + return Task::factory()->for($this->company)->create(array_merge([ |
| 140 | + 'customer_id' => $project->customer_id, |
| 141 | + 'project_id' => $project->id, |
| 142 | + 'task_status' => TaskStatus::COMPLETED->value, |
| 143 | + 'task_price' => 100, |
| 144 | + ], $attributes)); |
| 145 | + } |
| 146 | +} |
0 commit comments