Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

OfferingFactory.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Database\Factories\Common;
  3. use App\Enums\Accounting\AccountCategory;
  4. use App\Enums\Accounting\AccountType;
  5. use App\Enums\Accounting\AdjustmentType;
  6. use App\Enums\Common\OfferingType;
  7. use App\Models\Accounting\Account;
  8. use App\Models\Accounting\Adjustment;
  9. use App\Models\Common\Offering;
  10. use Illuminate\Database\Eloquent\Factories\Factory;
  11. /**
  12. * @extends Factory<Offering>
  13. */
  14. class OfferingFactory extends Factory
  15. {
  16. /**
  17. * The name of the factory's corresponding model.
  18. */
  19. protected $model = Offering::class;
  20. /**
  21. * Define the model's default state.
  22. *
  23. * @return array<string, mixed>
  24. */
  25. public function definition(): array
  26. {
  27. return [
  28. 'company_id' => 1,
  29. 'name' => $this->faker->words(3, true),
  30. 'description' => $this->faker->sentence,
  31. 'type' => $this->faker->randomElement(OfferingType::cases()),
  32. 'price' => $this->faker->numberBetween(5, 1000),
  33. 'sellable' => $this->faker->boolean(80),
  34. 'purchasable' => $this->faker->boolean(80),
  35. 'income_account_id' => function (array $attributes) {
  36. return $attributes['sellable'] ? 10 : null;
  37. },
  38. 'expense_account_id' => function (array $attributes) {
  39. return $attributes['purchasable'] ? $this->faker->numberBetween(17, 35) : null;
  40. },
  41. 'created_by' => 1,
  42. 'updated_by' => 1,
  43. ];
  44. }
  45. public function sellable(): self
  46. {
  47. $incomeAccount = Account::query()
  48. ->where('category', AccountCategory::Revenue)
  49. ->where('type', AccountType::OperatingRevenue)
  50. ->inRandomOrder()
  51. ->first();
  52. return $this->state(function (array $attributes) use ($incomeAccount) {
  53. return [
  54. 'sellable' => true,
  55. 'income_account_id' => $incomeAccount?->id ?? 10,
  56. ];
  57. });
  58. }
  59. public function purchasable(): self
  60. {
  61. $expenseAccount = Account::query()
  62. ->where('category', AccountCategory::Expense)
  63. ->where('type', AccountType::OperatingExpense)
  64. ->inRandomOrder()
  65. ->first();
  66. return $this->state(function (array $attributes) use ($expenseAccount) {
  67. return [
  68. 'purchasable' => true,
  69. 'expense_account_id' => $expenseAccount?->id ?? $this->faker->numberBetween(17, 35),
  70. ];
  71. });
  72. }
  73. public function withSalesAdjustments(): self
  74. {
  75. return $this->afterCreating(function (Offering $offering) {
  76. if ($offering->sellable) {
  77. $adjustments = $offering->company?->adjustments()
  78. ->where('type', AdjustmentType::Sales)
  79. ->pluck('id');
  80. $adjustmentsToAttach = $adjustments->isNotEmpty()
  81. ? $adjustments->random(min(2, $adjustments->count()))
  82. : Adjustment::factory()->salesTax()->count(2)->create()->pluck('id');
  83. $offering->salesAdjustments()->attach($adjustmentsToAttach);
  84. }
  85. });
  86. }
  87. public function withPurchaseAdjustments(): self
  88. {
  89. return $this->afterCreating(function (Offering $offering) {
  90. if ($offering->purchasable) {
  91. $adjustments = $offering->company?->adjustments()
  92. ->where('type', AdjustmentType::Purchase)
  93. ->pluck('id');
  94. $adjustmentsToAttach = $adjustments->isNotEmpty()
  95. ? $adjustments->random(min(2, $adjustments->count()))
  96. : Adjustment::factory()->purchaseTax()->count(2)->create()->pluck('id');
  97. $offering->purchaseAdjustments()->attach($adjustmentsToAttach);
  98. }
  99. });
  100. }
  101. }