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

DocumentLineItemFactory.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  52. $lineItem->refresh();
  53. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  54. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  55. $lineItem->updateQuietly([
  56. 'tax_total' => $taxTotal,
  57. 'discount_total' => $discountTotal,
  58. ]);
  59. });
  60. }
  61. public function forEstimate(Estimate $estimate): static
  62. {
  63. return $this
  64. ->for($estimate, 'documentable')
  65. ->for($estimate->company, 'company')
  66. ->afterCreating(function (DocumentLineItem $lineItem) {
  67. $offering = Offering::query()
  68. ->where('company_id', $lineItem->company_id)
  69. ->where('sellable', true)
  70. ->inRandomOrder()
  71. ->firstOrFail();
  72. $lineItem->updateQuietly([
  73. 'offering_id' => $offering->id,
  74. 'unit_price' => $offering->price,
  75. ]);
  76. $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  77. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  78. $lineItem->refresh();
  79. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  80. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  81. $lineItem->updateQuietly([
  82. 'tax_total' => $taxTotal,
  83. 'discount_total' => $discountTotal,
  84. ]);
  85. });
  86. }
  87. public function forBill(Bill $bill): static
  88. {
  89. return $this
  90. ->for($bill, 'documentable')
  91. ->for($bill->company, 'company')
  92. ->afterCreating(function (DocumentLineItem $lineItem) {
  93. $offering = Offering::query()
  94. ->where('company_id', $lineItem->company_id)
  95. ->where('purchasable', true)
  96. ->inRandomOrder()
  97. ->firstOrFail();
  98. $lineItem->updateQuietly([
  99. 'offering_id' => $offering->id,
  100. 'unit_price' => $offering->price,
  101. ]);
  102. $lineItem->purchaseTaxes()->syncWithoutDetaching($offering->purchaseTaxes->pluck('id')->toArray());
  103. $lineItem->purchaseDiscounts()->syncWithoutDetaching($offering->purchaseDiscounts->pluck('id')->toArray());
  104. $lineItem->refresh();
  105. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  106. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  107. $lineItem->updateQuietly([
  108. 'tax_total' => $taxTotal,
  109. 'discount_total' => $discountTotal,
  110. ]);
  111. });
  112. }
  113. }