Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

CompanyFactory.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Database\Factories;
  3. use App\Models\Accounting\Transaction;
  4. use App\Models\Common\Client;
  5. use App\Models\Common\Offering;
  6. use App\Models\Common\Vendor;
  7. use App\Models\Company;
  8. use App\Models\Setting\CompanyProfile;
  9. use App\Models\User;
  10. use App\Services\CompanyDefaultService;
  11. use Illuminate\Database\Eloquent\Factories\Factory;
  12. class CompanyFactory extends Factory
  13. {
  14. /**
  15. * The name of the factory's corresponding model.
  16. *
  17. * @var string
  18. */
  19. protected $model = Company::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. 'name' => $this->faker->unique()->company(),
  29. 'user_id' => User::factory(),
  30. 'personal_company' => true,
  31. ];
  32. }
  33. public function withCompanyProfile(): self
  34. {
  35. return $this->afterCreating(function (Company $company) {
  36. CompanyProfile::factory()->forCompany($company)->withCountry('US')->create();
  37. });
  38. }
  39. /**
  40. * Set up default settings for the company after creation.
  41. */
  42. public function withCompanyDefaults(): self
  43. {
  44. return $this->afterCreating(function (Company $company) {
  45. $countryCode = $company->profile->country;
  46. $companyDefaultService = app(CompanyDefaultService::class);
  47. $companyDefaultService->createCompanyDefaults($company, $company->owner, 'USD', $countryCode, 'en');
  48. });
  49. }
  50. public function withTransactions(int $count = 2000): self
  51. {
  52. return $this->afterCreating(function (Company $company) use ($count) {
  53. $defaultBankAccount = $company->default->bankAccount;
  54. Transaction::factory()
  55. ->forCompanyAndBankAccount($company, $defaultBankAccount)
  56. ->count($count)
  57. ->create([
  58. 'created_by' => $company->user_id,
  59. 'updated_by' => $company->user_id,
  60. ]);
  61. });
  62. }
  63. public function withClients(int $count = 10): self
  64. {
  65. return $this->has(Client::factory()->count($count)->withPrimaryContact()->withAddresses());
  66. }
  67. public function withVendors(int $count = 10): self
  68. {
  69. return $this->has(Vendor::factory()->count($count)->withContact()->withAddress());
  70. }
  71. public function withOfferings(int $count = 10): self
  72. {
  73. return $this->afterCreating(function (Company $company) use ($count) {
  74. Offering::factory()
  75. ->count($count)
  76. ->sellable()
  77. ->withSalesAdjustments()
  78. ->purchasable()
  79. ->withPurchaseAdjustments()
  80. ->create([
  81. 'company_id' => $company->id,
  82. 'created_by' => $company->user_id,
  83. 'updated_by' => $company->user_id,
  84. ]);
  85. });
  86. }
  87. }