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.

OfferingFactory.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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(500, 50000), // $5.00 to $500.00
  33. 'sellable' => false,
  34. 'purchasable' => false,
  35. 'income_account_id' => null,
  36. 'expense_account_id' => null,
  37. 'created_by' => 1,
  38. 'updated_by' => 1,
  39. ];
  40. }
  41. public function withSalesAdjustments(): self
  42. {
  43. return $this->afterCreating(function (Offering $offering) {
  44. $incomeAccount = Account::query()
  45. ->where('company_id', $offering->company_id)
  46. ->where('category', AccountCategory::Revenue)
  47. ->where('type', AccountType::OperatingRevenue)
  48. ->inRandomOrder()
  49. ->firstOrFail();
  50. $offering->updateQuietly([
  51. 'sellable' => true,
  52. 'income_account_id' => $incomeAccount->id,
  53. ]);
  54. $adjustments = $offering->company?->adjustments()
  55. ->where('type', AdjustmentType::Sales)
  56. ->pluck('id');
  57. $adjustmentsToAttach = $adjustments->isNotEmpty()
  58. ? $adjustments->random(min(2, $adjustments->count()))
  59. : Adjustment::factory()->salesTax()->count(2)->create()->pluck('id');
  60. $offering->salesAdjustments()->attach($adjustmentsToAttach);
  61. });
  62. }
  63. public function withPurchaseAdjustments(): self
  64. {
  65. return $this->afterCreating(function (Offering $offering) {
  66. $expenseAccount = Account::query()
  67. ->where('company_id', $offering->company_id)
  68. ->where('category', AccountCategory::Expense)
  69. ->where('type', AccountType::OperatingExpense)
  70. ->inRandomOrder()
  71. ->firstOrFail();
  72. $offering->updateQuietly([
  73. 'purchasable' => true,
  74. 'expense_account_id' => $expenseAccount->id,
  75. ]);
  76. $adjustments = $offering->company?->adjustments()
  77. ->where('type', AdjustmentType::Purchase)
  78. ->pluck('id');
  79. $adjustmentsToAttach = $adjustments->isNotEmpty()
  80. ? $adjustments->random(min(2, $adjustments->count()))
  81. : Adjustment::factory()->purchaseTax()->count(2)->create()->pluck('id');
  82. $offering->purchaseAdjustments()->attach($adjustmentsToAttach);
  83. });
  84. }
  85. }