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

DocumentLineItemFactory.php 4.6KB

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