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

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