Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

DocumentLineItemFactory.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Database\Factories\Accounting;
  3. use App\Models\Accounting\DocumentLineItem;
  4. use App\Models\Common\Offering;
  5. use Illuminate\Database\Eloquent\Factories\Factory;
  6. /**
  7. * @extends Factory<DocumentLineItem>
  8. */
  9. class DocumentLineItemFactory extends Factory
  10. {
  11. /**
  12. * The name of the factory's corresponding model.
  13. */
  14. protected $model = DocumentLineItem::class;
  15. /**
  16. * Define the model's default state.
  17. *
  18. * @return array<string, mixed>
  19. */
  20. public function definition(): array
  21. {
  22. $quantity = $this->faker->numberBetween(1, 10);
  23. return [
  24. 'company_id' => 1,
  25. 'description' => $this->faker->sentence,
  26. 'quantity' => $quantity,
  27. 'created_by' => 1,
  28. 'updated_by' => 1,
  29. ];
  30. }
  31. public function forInvoice(): static
  32. {
  33. return $this->state(function (array $attributes) {
  34. $offering = Offering::with(['salesTaxes', 'salesDiscounts'])
  35. ->where('sellable', true)
  36. ->inRandomOrder()
  37. ->first();
  38. return [
  39. 'offering_id' => $offering->id,
  40. 'unit_price' => $offering->price,
  41. ];
  42. })->afterCreating(function (DocumentLineItem $lineItem) {
  43. $offering = $lineItem->offering;
  44. if ($offering) {
  45. $lineItem->adjustments()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  46. $lineItem->adjustments()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  47. }
  48. $lineItem->refresh();
  49. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  50. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  51. $lineItem->updateQuietly([
  52. 'tax_total' => $taxTotal,
  53. 'discount_total' => $discountTotal,
  54. ]);
  55. });
  56. }
  57. public function forBill(): static
  58. {
  59. return $this->state(function (array $attributes) {
  60. $offering = Offering::with(['purchaseTaxes', 'purchaseDiscounts'])
  61. ->where('purchasable', true)
  62. ->inRandomOrder()
  63. ->first();
  64. return [
  65. 'offering_id' => $offering->id,
  66. 'unit_price' => $offering->price,
  67. ];
  68. })->afterCreating(function (DocumentLineItem $lineItem) {
  69. $offering = $lineItem->offering;
  70. if ($offering) {
  71. $lineItem->adjustments()->syncWithoutDetaching($offering->purchaseTaxes->pluck('id')->toArray());
  72. $lineItem->adjustments()->syncWithoutDetaching($offering->purchaseDiscounts->pluck('id')->toArray());
  73. }
  74. $lineItem->refresh();
  75. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  76. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  77. $lineItem->updateQuietly([
  78. 'tax_total' => $taxTotal,
  79. 'discount_total' => $discountTotal,
  80. ]);
  81. });
  82. }
  83. }