Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AdjustmentFactory.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Database\Factories\Accounting;
  3. use App\Enums\Accounting\AdjustmentCategory;
  4. use App\Enums\Accounting\AdjustmentComputation;
  5. use App\Enums\Accounting\AdjustmentScope;
  6. use App\Enums\Accounting\AdjustmentType;
  7. use App\Models\Accounting\Account;
  8. use App\Models\Accounting\Adjustment;
  9. use Illuminate\Database\Eloquent\Factories\Factory;
  10. use Illuminate\Support\Carbon;
  11. /**
  12. * @extends Factory<Adjustment>
  13. */
  14. class AdjustmentFactory extends Factory
  15. {
  16. /**
  17. * The name of the factory's corresponding model.
  18. */
  19. protected $model = Adjustment::class;
  20. /**
  21. * Define the model's default state.
  22. *
  23. * @return array<string, mixed>
  24. */
  25. public function definition(): array
  26. {
  27. $startDate = $this->faker->dateTimeBetween('now', '+1 year');
  28. $endDate = $this->faker->dateTimeBetween($startDate, Carbon::parse($startDate)->addYear());
  29. $computation = $this->faker->randomElement(AdjustmentComputation::class);
  30. $rate = $computation === AdjustmentComputation::Fixed
  31. ? $this->faker->numberBetween(5, 100) * 100 // $5 - $100 for fixed amounts
  32. : $this->faker->numberBetween(3, 25) * 10000; // 3% - 25% for percentages
  33. return [
  34. 'rate' => $rate,
  35. 'computation' => $computation,
  36. 'category' => $this->faker->randomElement(AdjustmentCategory::class),
  37. 'type' => $this->faker->randomElement(AdjustmentType::class),
  38. 'scope' => $this->faker->randomElement(AdjustmentScope::class),
  39. 'start_date' => $startDate,
  40. 'end_date' => $endDate,
  41. 'enabled' => false,
  42. ];
  43. }
  44. public function configure(): static
  45. {
  46. return $this->afterCreating(function (Adjustment $adjustment) {
  47. if ($adjustment->account_id === null) {
  48. $account = Account::factory()->create();
  49. $adjustment->account()->associate($account);
  50. }
  51. });
  52. }
  53. /**
  54. * Define a sales tax adjustment.
  55. */
  56. public function salesTax(?string $name = null, ?string $description = null): self
  57. {
  58. $name = $name ?? 'Sales Tax';
  59. $account = Account::factory()->forSalesTax($name, $description)->create();
  60. return $this->state([
  61. 'category' => AdjustmentCategory::Tax,
  62. 'type' => AdjustmentType::Sales,
  63. 'account_id' => $account->id,
  64. ]);
  65. }
  66. /**
  67. * Define a purchase tax adjustment.
  68. */
  69. public function purchaseTax(?string $name = null, ?string $description = null): self
  70. {
  71. $name = $name ?? 'Purchase Tax';
  72. $account = Account::factory()->forPurchaseTax($name, $description)->create();
  73. return $this->state([
  74. 'category' => AdjustmentCategory::Tax,
  75. 'type' => AdjustmentType::Purchase,
  76. 'account_id' => $account->id,
  77. ]);
  78. }
  79. /**
  80. * Define a sales discount adjustment.
  81. */
  82. public function salesDiscount(?string $name = null, ?string $description = null): self
  83. {
  84. $name = $name ?? 'Sales Discount';
  85. $account = Account::factory()->forSalesDiscount($name, $description)->create();
  86. return $this->state([
  87. 'category' => AdjustmentCategory::Discount,
  88. 'type' => AdjustmentType::Sales,
  89. 'account_id' => $account->id,
  90. ]);
  91. }
  92. /**
  93. * Define a purchase discount adjustment.
  94. */
  95. public function purchaseDiscount(?string $name = null, ?string $description = null): self
  96. {
  97. $name = $name ?? 'Purchase Discount';
  98. $account = Account::factory()->forPurchaseDiscount($name, $description)->create();
  99. return $this->state([
  100. 'category' => AdjustmentCategory::Discount,
  101. 'type' => AdjustmentType::Purchase,
  102. 'account_id' => $account->id,
  103. ]);
  104. }
  105. }