您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AdjustmentFactory.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. ];
  42. }
  43. public function configure(): static
  44. {
  45. return $this->afterCreating(function (Adjustment $adjustment) {
  46. if ($adjustment->account_id === null) {
  47. $account = Account::factory()->create();
  48. $adjustment->account()->associate($account);
  49. }
  50. });
  51. }
  52. /**
  53. * Define a sales tax adjustment.
  54. */
  55. public function salesTax(?string $name = null, ?string $description = null): self
  56. {
  57. $name = $name ?? 'Sales Tax';
  58. $account = Account::factory()->forSalesTax($name, $description)->create();
  59. return $this->state([
  60. 'category' => AdjustmentCategory::Tax,
  61. 'type' => AdjustmentType::Sales,
  62. 'account_id' => $account->id,
  63. ]);
  64. }
  65. /**
  66. * Define a purchase tax adjustment.
  67. */
  68. public function purchaseTax(?string $name = null, ?string $description = null): self
  69. {
  70. $name = $name ?? 'Purchase Tax';
  71. $account = Account::factory()->forPurchaseTax($name, $description)->create();
  72. return $this->state([
  73. 'category' => AdjustmentCategory::Tax,
  74. 'type' => AdjustmentType::Purchase,
  75. 'account_id' => $account->id,
  76. ]);
  77. }
  78. /**
  79. * Define a sales discount adjustment.
  80. */
  81. public function salesDiscount(?string $name = null, ?string $description = null): self
  82. {
  83. $name = $name ?? 'Sales Discount';
  84. $account = Account::factory()->forSalesDiscount($name, $description)->create();
  85. return $this->state([
  86. 'category' => AdjustmentCategory::Discount,
  87. 'type' => AdjustmentType::Sales,
  88. 'account_id' => $account->id,
  89. ]);
  90. }
  91. /**
  92. * Define a purchase discount adjustment.
  93. */
  94. public function purchaseDiscount(?string $name = null, ?string $description = null): self
  95. {
  96. $name = $name ?? 'Purchase Discount';
  97. $account = Account::factory()->forPurchaseDiscount($name, $description)->create();
  98. return $this->state([
  99. 'category' => AdjustmentCategory::Discount,
  100. 'type' => AdjustmentType::Purchase,
  101. 'account_id' => $account->id,
  102. ]);
  103. }
  104. }