Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

EstimateFactory.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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): self
  44. {
  45. return $this->has(DocumentLineItem::factory()->forEstimate()->count($count), 'lineItems');
  46. }
  47. public function approved(): static
  48. {
  49. return $this->afterCreating(function (Estimate $estimate) {
  50. if (! $estimate->isDraft()) {
  51. return;
  52. }
  53. $this->recalculateTotals($estimate);
  54. $approvedAt = Carbon::parse($estimate->date)->addHours($this->faker->numberBetween(1, 24));
  55. $estimate->approveDraft($approvedAt);
  56. });
  57. }
  58. public function accepted(): static
  59. {
  60. return $this->afterCreating(function (Estimate $estimate) {
  61. if (! $estimate->isApproved()) {
  62. $this->approved()->create();
  63. }
  64. $acceptedAt = Carbon::parse($estimate->approved_at)
  65. ->addDays($this->faker->numberBetween(1, 7));
  66. $estimate->markAsAccepted($acceptedAt);
  67. });
  68. }
  69. public function converted(): static
  70. {
  71. return $this->afterCreating(function (Estimate $estimate) {
  72. if (! $estimate->isAccepted()) {
  73. $this->accepted()->create();
  74. }
  75. $convertedAt = Carbon::parse($estimate->accepted_at)
  76. ->addDays($this->faker->numberBetween(1, 7));
  77. $estimate->convertToInvoice($convertedAt);
  78. });
  79. }
  80. public function declined(): static
  81. {
  82. return $this->afterCreating(function (Estimate $estimate) {
  83. if (! $estimate->isApproved()) {
  84. $this->approved()->create();
  85. }
  86. $declinedAt = Carbon::parse($estimate->approved_at)
  87. ->addDays($this->faker->numberBetween(1, 7));
  88. $estimate->update([
  89. 'status' => EstimateStatus::Declined,
  90. 'declined_at' => $declinedAt,
  91. ]);
  92. });
  93. }
  94. public function sent(): static
  95. {
  96. return $this->afterCreating(function (Estimate $estimate) {
  97. if (! $estimate->isDraft()) {
  98. return;
  99. }
  100. $this->recalculateTotals($estimate);
  101. $sentAt = Carbon::parse($estimate->date)->addHours($this->faker->numberBetween(1, 24));
  102. $estimate->markAsSent($sentAt);
  103. });
  104. }
  105. public function configure(): static
  106. {
  107. return $this->afterCreating(function (Estimate $estimate) {
  108. $paddedId = str_pad((string) $estimate->id, 5, '0', STR_PAD_LEFT);
  109. $estimate->updateQuietly([
  110. 'estimate_number' => "EST-{$paddedId}",
  111. 'reference_number' => "REF-{$paddedId}",
  112. ]);
  113. $this->recalculateTotals($estimate);
  114. if ($estimate->approved_at && $estimate->is_currently_expired) {
  115. $estimate->updateQuietly([
  116. 'status' => EstimateStatus::Expired,
  117. ]);
  118. }
  119. });
  120. }
  121. protected function recalculateTotals(Estimate $estimate): void
  122. {
  123. if ($estimate->lineItems()->exists()) {
  124. $estimate->refresh();
  125. $subtotal = $estimate->lineItems()->sum('subtotal') / 100;
  126. $taxTotal = $estimate->lineItems()->sum('tax_total') / 100;
  127. $discountTotal = $estimate->lineItems()->sum('discount_total') / 100;
  128. $grandTotal = $subtotal + $taxTotal - $discountTotal;
  129. $estimate->update([
  130. 'subtotal' => $subtotal,
  131. 'tax_total' => $taxTotal,
  132. 'discount_total' => $discountTotal,
  133. 'total' => $grandTotal,
  134. ]);
  135. }
  136. }
  137. }