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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. ->state(function (array $attributes) {
  41. $offering = Offering::where('sellable', true)
  42. ->inRandomOrder()
  43. ->first();
  44. return [
  45. 'offering_id' => $offering->id,
  46. 'unit_price' => $offering->price,
  47. ];
  48. })
  49. ->afterCreating(function (DocumentLineItem $lineItem) {
  50. $offering = $lineItem->offering;
  51. if ($offering) {
  52. $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  53. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  54. }
  55. $lineItem->refresh();
  56. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  57. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  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. ->state(function (array $attributes) {
  70. $offering = Offering::where('sellable', true)
  71. ->inRandomOrder()
  72. ->first();
  73. return [
  74. 'offering_id' => $offering->id,
  75. 'unit_price' => $offering->price,
  76. ];
  77. })
  78. ->afterCreating(function (DocumentLineItem $lineItem) {
  79. $offering = $lineItem->offering;
  80. if ($offering) {
  81. $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  82. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  83. }
  84. $lineItem->refresh();
  85. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  86. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  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. ->state(function (array $attributes) {
  99. $offering = Offering::where('purchasable', true)
  100. ->inRandomOrder()
  101. ->first();
  102. return [
  103. 'offering_id' => $offering->id,
  104. 'unit_price' => $offering->price,
  105. ];
  106. })
  107. ->afterCreating(function (DocumentLineItem $lineItem) {
  108. $offering = $lineItem->offering;
  109. if ($offering) {
  110. $lineItem->purchaseTaxes()->syncWithoutDetaching($offering->purchaseTaxes->pluck('id')->toArray());
  111. $lineItem->purchaseDiscounts()->syncWithoutDetaching($offering->purchaseDiscounts->pluck('id')->toArray());
  112. }
  113. $lineItem->refresh();
  114. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  115. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  116. $lineItem->updateQuietly([
  117. 'tax_total' => $taxTotal,
  118. 'discount_total' => $discountTotal,
  119. ]);
  120. });
  121. }
  122. }