Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DocumentLineItemFactory.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. ->for($invoice->company, 'company')
  40. ->afterCreating(function (DocumentLineItem $lineItem) {
  41. $offering = Offering::query()
  42. ->where('company_id', $lineItem->company_id)
  43. ->where('sellable', true)
  44. ->inRandomOrder()
  45. ->firstOrFail();
  46. $lineItem->updateQuietly([
  47. 'offering_id' => $offering->id,
  48. 'unit_price' => $offering->price,
  49. ]);
  50. $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  51. // Only sync discounts if the discount method is per_line_item
  52. if ($lineItem->documentable->discount_method?->isPerLineItem() ?? true) {
  53. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  54. }
  55. $lineItem->refresh();
  56. $taxTotal = $lineItem->calculateTaxTotalAmount();
  57. $discountTotal = $lineItem->calculateDiscountTotalAmount();
  58. $lineItem->updateQuietly([
  59. 'tax_total' => $taxTotal,
  60. 'discount_total' => $discountTotal,
  61. ]);
  62. });
  63. }
  64. public function forEstimate(Estimate $estimate): static
  65. {
  66. return $this
  67. ->for($estimate, 'documentable')
  68. ->for($estimate->company, 'company')
  69. ->afterCreating(function (DocumentLineItem $lineItem) {
  70. $offering = Offering::query()
  71. ->where('company_id', $lineItem->company_id)
  72. ->where('sellable', true)
  73. ->inRandomOrder()
  74. ->firstOrFail();
  75. $lineItem->updateQuietly([
  76. 'offering_id' => $offering->id,
  77. 'unit_price' => $offering->price,
  78. ]);
  79. $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  80. // Only sync discounts if the discount method is per_line_item
  81. if ($lineItem->documentable->discount_method?->isPerLineItem() ?? true) {
  82. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  83. }
  84. $lineItem->refresh();
  85. $taxTotal = $lineItem->calculateTaxTotalAmount();
  86. $discountTotal = $lineItem->calculateDiscountTotalAmount();
  87. $lineItem->updateQuietly([
  88. 'tax_total' => $taxTotal,
  89. 'discount_total' => $discountTotal,
  90. ]);
  91. });
  92. }
  93. public function forBill(Bill $bill): static
  94. {
  95. return $this
  96. ->for($bill, 'documentable')
  97. ->for($bill->company, 'company')
  98. ->afterCreating(function (DocumentLineItem $lineItem) {
  99. $offering = Offering::query()
  100. ->where('company_id', $lineItem->company_id)
  101. ->where('purchasable', true)
  102. ->inRandomOrder()
  103. ->firstOrFail();
  104. $lineItem->updateQuietly([
  105. 'offering_id' => $offering->id,
  106. 'unit_price' => $offering->price,
  107. ]);
  108. $lineItem->purchaseTaxes()->syncWithoutDetaching($offering->purchaseTaxes->pluck('id')->toArray());
  109. // Only sync discounts if the discount method is per_line_item
  110. if ($lineItem->documentable->discount_method?->isPerLineItem() ?? true) {
  111. $lineItem->purchaseDiscounts()->syncWithoutDetaching($offering->purchaseDiscounts->pluck('id')->toArray());
  112. }
  113. $lineItem->refresh();
  114. $taxTotal = $lineItem->calculateTaxTotalAmount();
  115. $discountTotal = $lineItem->calculateDiscountTotalAmount();
  116. $lineItem->updateQuietly([
  117. 'tax_total' => $taxTotal,
  118. 'discount_total' => $discountTotal,
  119. ]);
  120. });
  121. }
  122. }