Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CompanyFactory.php 6.9KB

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