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 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 forBill(): static
  57. {
  58. return $this->state(function (array $attributes) {
  59. $offering = Offering::where('purchasable', 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->purchaseTaxes()->syncWithoutDetaching($offering->purchaseTaxes->pluck('id')->toArray());
  70. $lineItem->purchaseDiscounts()->syncWithoutDetaching($offering->purchaseDiscounts->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. }