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

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