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.6KB

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