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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Database\Factories\Accounting;
  3. use App\Models\Accounting\Bill;
  4. use App\Models\Accounting\DocumentLineItem;
  5. use App\Models\Accounting\Estimate;
  6. use App\Models\Accounting\Invoice;
  7. use App\Models\Accounting\RecurringInvoice;
  8. use App\Models\Common\Offering;
  9. use Illuminate\Database\Eloquent\Factories\Factory;
  10. /**
  11. * @extends Factory<DocumentLineItem>
  12. */
  13. class DocumentLineItemFactory extends Factory
  14. {
  15. /**
  16. * The name of the factory's corresponding model.
  17. */
  18. protected $model = DocumentLineItem::class;
  19. /**
  20. * Define the model's default state.
  21. *
  22. * @return array<string, mixed>
  23. */
  24. public function definition(): array
  25. {
  26. $quantity = $this->faker->numberBetween(1, 10);
  27. return [
  28. 'company_id' => 1,
  29. 'description' => $this->faker->sentence,
  30. 'quantity' => $quantity,
  31. 'created_by' => 1,
  32. 'updated_by' => 1,
  33. ];
  34. }
  35. public function forInvoice(Invoice | RecurringInvoice $invoice): static
  36. {
  37. return $this
  38. ->for($invoice, 'documentable')
  39. ->state(function (array $attributes) {
  40. $offering = Offering::where('sellable', true)
  41. ->inRandomOrder()
  42. ->first();
  43. return [
  44. 'offering_id' => $offering->id,
  45. 'unit_price' => $offering->price,
  46. ];
  47. })
  48. ->afterCreating(function (DocumentLineItem $lineItem) {
  49. $offering = $lineItem->offering;
  50. if ($offering) {
  51. $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  52. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  53. }
  54. $lineItem->refresh();
  55. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  56. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  57. $lineItem->updateQuietly([
  58. 'tax_total' => $taxTotal,
  59. 'discount_total' => $discountTotal,
  60. ]);
  61. });
  62. }
  63. public function forEstimate(Estimate $estimate): static
  64. {
  65. return $this
  66. ->for($estimate, 'documentable')
  67. ->state(function (array $attributes) {
  68. $offering = Offering::where('sellable', true)
  69. ->inRandomOrder()
  70. ->first();
  71. return [
  72. 'offering_id' => $offering->id,
  73. 'unit_price' => $offering->price,
  74. ];
  75. })
  76. ->afterCreating(function (DocumentLineItem $lineItem) {
  77. $offering = $lineItem->offering;
  78. if ($offering) {
  79. $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  80. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  81. }
  82. $lineItem->refresh();
  83. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  84. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  85. $lineItem->updateQuietly([
  86. 'tax_total' => $taxTotal,
  87. 'discount_total' => $discountTotal,
  88. ]);
  89. });
  90. }
  91. public function forBill(Bill $bill): static
  92. {
  93. return $this
  94. ->for($bill, 'documentable')
  95. ->state(function (array $attributes) {
  96. $offering = Offering::where('purchasable', true)
  97. ->inRandomOrder()
  98. ->first();
  99. return [
  100. 'offering_id' => $offering->id,
  101. 'unit_price' => $offering->price,
  102. ];
  103. })
  104. ->afterCreating(function (DocumentLineItem $lineItem) {
  105. $offering = $lineItem->offering;
  106. if ($offering) {
  107. $lineItem->purchaseTaxes()->syncWithoutDetaching($offering->purchaseTaxes->pluck('id')->toArray());
  108. $lineItem->purchaseDiscounts()->syncWithoutDetaching($offering->purchaseDiscounts->pluck('id')->toArray());
  109. }
  110. $lineItem->refresh();
  111. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  112. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  113. $lineItem->updateQuietly([
  114. 'tax_total' => $taxTotal,
  115. 'discount_total' => $discountTotal,
  116. ]);
  117. });
  118. }
  119. }