Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DocumentLineItemFactory.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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::where('sellable', true)
  35. ->inRandomOrder()
  36. ->first();
  37. return [
  38. 'offering_id' => $offering->id,
  39. 'unit_price' => $offering->price,
  40. ];
  41. })->afterCreating(function (DocumentLineItem $lineItem) {
  42. $offering = $lineItem->offering;
  43. if ($offering) {
  44. $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  45. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  46. }
  47. $lineItem->refresh();
  48. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  49. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  50. $lineItem->updateQuietly([
  51. 'tax_total' => $taxTotal,
  52. 'discount_total' => $discountTotal,
  53. ]);
  54. });
  55. }
  56. public function forEstimate(): static
  57. {
  58. return $this->state(function (array $attributes) {
  59. $offering = Offering::where('sellable', true)
  60. ->inRandomOrder()
  61. ->first();
  62. return [
  63. 'offering_id' => $offering->id,
  64. 'unit_price' => $offering->price,
  65. ];
  66. })->afterCreating(function (DocumentLineItem $lineItem) {
  67. $offering = $lineItem->offering;
  68. if ($offering) {
  69. $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
  70. $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
  71. }
  72. $lineItem->refresh();
  73. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  74. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  75. $lineItem->updateQuietly([
  76. 'tax_total' => $taxTotal,
  77. 'discount_total' => $discountTotal,
  78. ]);
  79. });
  80. }
  81. public function forBill(): static
  82. {
  83. return $this->state(function (array $attributes) {
  84. $offering = Offering::where('purchasable', true)
  85. ->inRandomOrder()
  86. ->first();
  87. return [
  88. 'offering_id' => $offering->id,
  89. 'unit_price' => $offering->price,
  90. ];
  91. })->afterCreating(function (DocumentLineItem $lineItem) {
  92. $offering = $lineItem->offering;
  93. if ($offering) {
  94. $lineItem->purchaseTaxes()->syncWithoutDetaching($offering->purchaseTaxes->pluck('id')->toArray());
  95. $lineItem->purchaseDiscounts()->syncWithoutDetaching($offering->purchaseDiscounts->pluck('id')->toArray());
  96. }
  97. $lineItem->refresh();
  98. $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
  99. $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
  100. $lineItem->updateQuietly([
  101. 'tax_total' => $taxTotal,
  102. 'discount_total' => $discountTotal,
  103. ]);
  104. });
  105. }
  106. }