You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EstimateFactory.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace Database\Factories\Accounting;
  3. use App\Enums\Accounting\EstimateStatus;
  4. use App\Models\Accounting\DocumentLineItem;
  5. use App\Models\Accounting\Estimate;
  6. use App\Models\Common\Client;
  7. use Illuminate\Database\Eloquent\Factories\Factory;
  8. use Illuminate\Support\Carbon;
  9. /**
  10. * @extends Factory<Estimate>
  11. */
  12. class EstimateFactory extends Factory
  13. {
  14. /**
  15. * The name of the factory's corresponding model.
  16. */
  17. protected $model = Estimate::class;
  18. /**
  19. * Define the model's default state.
  20. *
  21. * @return array<string, mixed>
  22. */
  23. public function definition(): array
  24. {
  25. $estimateDate = $this->faker->dateTimeBetween('-1 year');
  26. return [
  27. 'company_id' => 1,
  28. 'client_id' => Client::inRandomOrder()->value('id'),
  29. 'header' => 'Estimate',
  30. 'subheader' => 'Estimate',
  31. 'estimate_number' => $this->faker->unique()->numerify('EST-#####'),
  32. 'reference_number' => $this->faker->unique()->numerify('REF-#####'),
  33. 'date' => $estimateDate,
  34. 'expiration_date' => Carbon::parse($estimateDate)->addDays($this->faker->numberBetween(14, 30)),
  35. 'status' => EstimateStatus::Draft,
  36. 'currency_code' => 'USD',
  37. 'terms' => $this->faker->sentence,
  38. 'footer' => $this->faker->sentence,
  39. 'created_by' => 1,
  40. 'updated_by' => 1,
  41. ];
  42. }
  43. public function withLineItems(int $count = 3): static
  44. {
  45. return $this->afterCreating(function (Estimate $estimate) use ($count) {
  46. DocumentLineItem::factory()
  47. ->count($count)
  48. ->forEstimate($estimate)
  49. ->create();
  50. $this->recalculateTotals($estimate);
  51. });
  52. }
  53. public function approved(): static
  54. {
  55. return $this->afterCreating(function (Estimate $estimate) {
  56. $this->ensureLineItems($estimate);
  57. if (! $estimate->canBeApproved()) {
  58. return;
  59. }
  60. $approvedAt = Carbon::parse($estimate->date)
  61. ->addHours($this->faker->numberBetween(1, 24));
  62. $estimate->approveDraft($approvedAt);
  63. });
  64. }
  65. public function accepted(): static
  66. {
  67. return $this->afterCreating(function (Estimate $estimate) {
  68. $this->ensureSent($estimate);
  69. $acceptedAt = Carbon::parse($estimate->last_sent_at)
  70. ->addDays($this->faker->numberBetween(1, 7));
  71. $estimate->markAsAccepted($acceptedAt);
  72. });
  73. }
  74. public function converted(): static
  75. {
  76. return $this->afterCreating(function (Estimate $estimate) {
  77. if (! $estimate->wasAccepted()) {
  78. $this->accepted()->callAfterCreating(collect([$estimate]));
  79. }
  80. $convertedAt = Carbon::parse($estimate->accepted_at)
  81. ->addDays($this->faker->numberBetween(1, 7));
  82. $estimate->convertToInvoice($convertedAt);
  83. });
  84. }
  85. public function declined(): static
  86. {
  87. return $this->afterCreating(function (Estimate $estimate) {
  88. $this->ensureSent($estimate);
  89. $declinedAt = Carbon::parse($estimate->last_sent_at)
  90. ->addDays($this->faker->numberBetween(1, 7));
  91. $estimate->markAsDeclined($declinedAt);
  92. });
  93. }
  94. public function sent(): static
  95. {
  96. return $this->afterCreating(function (Estimate $estimate) {
  97. $this->ensureApproved($estimate);
  98. $sentAt = Carbon::parse($estimate->approved_at)
  99. ->addHours($this->faker->numberBetween(1, 24));
  100. $estimate->markAsSent($sentAt);
  101. });
  102. }
  103. public function viewed(): static
  104. {
  105. return $this->afterCreating(function (Estimate $estimate) {
  106. $this->ensureSent($estimate);
  107. $viewedAt = Carbon::parse($estimate->last_sent_at)
  108. ->addHours($this->faker->numberBetween(1, 24));
  109. $estimate->markAsViewed($viewedAt);
  110. });
  111. }
  112. public function expired(): static
  113. {
  114. return $this
  115. ->state([
  116. 'expiration_date' => now()->subDays($this->faker->numberBetween(1, 30)),
  117. ])
  118. ->afterCreating(function (Estimate $estimate) {
  119. $this->ensureApproved($estimate);
  120. });
  121. }
  122. public function configure(): static
  123. {
  124. return $this->afterCreating(function (Estimate $estimate) {
  125. $this->ensureLineItems($estimate);
  126. $paddedId = str_pad((string) $estimate->id, 5, '0', STR_PAD_LEFT);
  127. $estimate->updateQuietly([
  128. 'estimate_number' => "EST-{$paddedId}",
  129. 'reference_number' => "REF-{$paddedId}",
  130. ]);
  131. if ($estimate->wasApproved() && $estimate->is_currently_expired) {
  132. $estimate->updateQuietly([
  133. 'status' => EstimateStatus::Expired,
  134. ]);
  135. }
  136. });
  137. }
  138. protected function ensureLineItems(Estimate $estimate): void
  139. {
  140. if (! $estimate->hasLineItems()) {
  141. $this->withLineItems()->callAfterCreating(collect([$estimate]));
  142. }
  143. }
  144. protected function ensureApproved(Estimate $estimate): void
  145. {
  146. if (! $estimate->wasApproved()) {
  147. $this->approved()->callAfterCreating(collect([$estimate]));
  148. }
  149. }
  150. protected function ensureSent(Estimate $estimate): void
  151. {
  152. if (! $estimate->hasBeenSent()) {
  153. $this->sent()->callAfterCreating(collect([$estimate]));
  154. }
  155. }
  156. protected function recalculateTotals(Estimate $estimate): void
  157. {
  158. $estimate->refresh();
  159. if (! $estimate->hasLineItems()) {
  160. return;
  161. }
  162. $subtotal = $estimate->lineItems()->sum('subtotal') / 100;
  163. $taxTotal = $estimate->lineItems()->sum('tax_total') / 100;
  164. $discountTotal = $estimate->lineItems()->sum('discount_total') / 100;
  165. $grandTotal = $subtotal + $taxTotal - $discountTotal;
  166. $estimate->update([
  167. 'subtotal' => $subtotal,
  168. 'tax_total' => $taxTotal,
  169. 'discount_total' => $discountTotal,
  170. 'total' => $grandTotal,
  171. ]);
  172. }
  173. }