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.

CompanyFactory.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Database\Factories;
  3. use App\Enums\Accounting\InvoiceStatus;
  4. use App\Models\Accounting\Invoice;
  5. use App\Models\Accounting\Transaction;
  6. use App\Models\Common\Client;
  7. use App\Models\Common\Offering;
  8. use App\Models\Common\Vendor;
  9. use App\Models\Company;
  10. use App\Models\Setting\CompanyProfile;
  11. use App\Models\User;
  12. use App\Services\CompanyDefaultService;
  13. use Illuminate\Database\Eloquent\Factories\Factory;
  14. class CompanyFactory extends Factory
  15. {
  16. /**
  17. * The name of the factory's corresponding model.
  18. *
  19. * @var string
  20. */
  21. protected $model = Company::class;
  22. /**
  23. * Define the model's default state.
  24. *
  25. * @return array<string, mixed>
  26. */
  27. public function definition(): array
  28. {
  29. return [
  30. 'name' => $this->faker->unique()->company(),
  31. 'user_id' => User::factory(),
  32. 'personal_company' => true,
  33. ];
  34. }
  35. public function withCompanyProfile(): self
  36. {
  37. return $this->afterCreating(function (Company $company) {
  38. CompanyProfile::factory()->forCompany($company)->withCountry('US')->create();
  39. });
  40. }
  41. /**
  42. * Set up default settings for the company after creation.
  43. */
  44. public function withCompanyDefaults(): self
  45. {
  46. return $this->afterCreating(function (Company $company) {
  47. $countryCode = $company->profile->country;
  48. $companyDefaultService = app(CompanyDefaultService::class);
  49. $companyDefaultService->createCompanyDefaults($company, $company->owner, 'USD', $countryCode, 'en');
  50. });
  51. }
  52. public function withTransactions(int $count = 2000): self
  53. {
  54. return $this->afterCreating(function (Company $company) use ($count) {
  55. $defaultBankAccount = $company->default->bankAccount;
  56. Transaction::factory()
  57. ->forCompanyAndBankAccount($company, $defaultBankAccount)
  58. ->count($count)
  59. ->create([
  60. 'created_by' => $company->user_id,
  61. 'updated_by' => $company->user_id,
  62. ]);
  63. });
  64. }
  65. public function withClients(int $count = 10): self
  66. {
  67. return $this->has(Client::factory()->count($count)->withPrimaryContact()->withAddresses());
  68. }
  69. public function withVendors(int $count = 10): self
  70. {
  71. return $this->has(Vendor::factory()->count($count)->withContact()->withAddress());
  72. }
  73. public function withOfferings(int $count = 10): self
  74. {
  75. return $this->afterCreating(function (Company $company) use ($count) {
  76. Offering::factory()
  77. ->count($count)
  78. ->sellable()
  79. ->withSalesAdjustments()
  80. ->purchasable()
  81. ->withPurchaseAdjustments()
  82. ->create([
  83. 'company_id' => $company->id,
  84. 'created_by' => $company->user_id,
  85. 'updated_by' => $company->user_id,
  86. ]);
  87. });
  88. }
  89. public function withInvoices(int $count = 10): self
  90. {
  91. return $this->afterCreating(function (Company $company) use ($count) {
  92. $draftCount = (int) floor($count * 0.2);
  93. $approvedCount = (int) floor($count * 0.2);
  94. $paidCount = (int) floor($count * 0.3);
  95. $partialCount = (int) floor($count * 0.2);
  96. $overpaidCount = $count - ($draftCount + $approvedCount + $paidCount + $partialCount);
  97. Invoice::factory()
  98. ->count($draftCount)
  99. ->withLineItems()
  100. ->create([
  101. 'company_id' => $company->id,
  102. 'created_by' => $company->user_id,
  103. 'updated_by' => $company->user_id,
  104. ]);
  105. Invoice::factory()
  106. ->count($approvedCount)
  107. ->withLineItems()
  108. ->approved()
  109. ->create([
  110. 'company_id' => $company->id,
  111. 'created_by' => $company->user_id,
  112. 'updated_by' => $company->user_id,
  113. ]);
  114. Invoice::factory()
  115. ->count($paidCount)
  116. ->withLineItems()
  117. ->approved()
  118. ->withPayments(max: 4)
  119. ->create([
  120. 'company_id' => $company->id,
  121. 'created_by' => $company->user_id,
  122. 'updated_by' => $company->user_id,
  123. ]);
  124. Invoice::factory()
  125. ->count($partialCount)
  126. ->withLineItems()
  127. ->approved()
  128. ->withPayments(max: 4, invoiceStatus: InvoiceStatus::Partial)
  129. ->create([
  130. 'company_id' => $company->id,
  131. 'created_by' => $company->user_id,
  132. 'updated_by' => $company->user_id,
  133. ]);
  134. Invoice::factory()
  135. ->count($overpaidCount)
  136. ->withLineItems()
  137. ->approved()
  138. ->withPayments(max: 4, invoiceStatus: InvoiceStatus::Overpaid)
  139. ->create([
  140. 'company_id' => $company->id,
  141. 'created_by' => $company->user_id,
  142. 'updated_by' => $company->user_id,
  143. ]);
  144. });
  145. }
  146. }