Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

EstimateFactory.php 4.9KB

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