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.

DocumentLineItemFactory.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Database\Factories\Accounting;
  3. use App\Models\Accounting\DocumentLineItem;
  4. use App\Models\Common\Offering;
  5. use Illuminate\Database\Eloquent\Factories\Factory;
  6. /**
  7. * @extends Factory<DocumentLineItem>
  8. */
  9. class DocumentLineItemFactory extends Factory
  10. {
  11. /**
  12. * The name of the factory's corresponding model.
  13. */
  14. protected $model = DocumentLineItem::class;
  15. /**
  16. * Define the model's default state.
  17. *
  18. * @return array<string, mixed>
  19. */
  20. public function definition(): array
  21. {
  22. $offering = Offering::with(['salesTaxes', 'salesDiscounts'])->inRandomOrder()->first();
  23. $quantity = $this->faker->numberBetween(1, 10);
  24. $unitPrice = $offering->price;
  25. return [
  26. 'company_id' => 1,
  27. 'offering_id' => $offering->id,
  28. 'description' => $this->faker->sentence,
  29. 'quantity' => $quantity,
  30. 'unit_price' => $unitPrice,
  31. 'created_by' => 1,
  32. 'updated_by' => 1,
  33. ];
  34. }
  35. public function configure(): static
  36. {
  37. return $this->afterCreating(function (DocumentLineItem $lineItem) {
  38. $offering = $lineItem->offering;
  39. if ($offering) {
  40. $lineItem->salesTaxes()->sync($offering->salesTaxes->pluck('id')->toArray());
  41. $lineItem->salesDiscounts()->sync($offering->salesDiscounts->pluck('id')->toArray());
  42. }
  43. $lineItem->refresh();
  44. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  45. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  46. $lineItem->updateQuietly([
  47. 'tax_total' => $taxTotal,
  48. 'discount_total' => $discountTotal,
  49. ]);
  50. });
  51. }
  52. }