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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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\Estimate;
  7. use App\Models\Accounting\Invoice;
  8. use App\Models\Accounting\Transaction;
  9. use App\Models\Common\Client;
  10. use App\Models\Common\Offering;
  11. use App\Models\Common\Vendor;
  12. use App\Models\Company;
  13. use App\Models\Setting\CompanyProfile;
  14. use App\Models\User;
  15. use App\Services\CompanyDefaultService;
  16. use Illuminate\Database\Eloquent\Factories\Factory;
  17. class CompanyFactory extends Factory
  18. {
  19. /**
  20. * The name of the factory's corresponding model.
  21. *
  22. * @var string
  23. */
  24. protected $model = Company::class;
  25. /**
  26. * Define the model's default state.
  27. *
  28. * @return array<string, mixed>
  29. */
  30. public function definition(): array
  31. {
  32. return [
  33. 'name' => $this->faker->unique()->company(),
  34. 'user_id' => User::factory(),
  35. 'personal_company' => true,
  36. ];
  37. }
  38. public function withCompanyProfile(): self
  39. {
  40. return $this->afterCreating(function (Company $company) {
  41. CompanyProfile::factory()->forCompany($company)->withCountry('US')->create();
  42. });
  43. }
  44. /**
  45. * Set up default settings for the company after creation.
  46. */
  47. public function withCompanyDefaults(): self
  48. {
  49. return $this->afterCreating(function (Company $company) {
  50. $countryCode = $company->profile->country;
  51. $companyDefaultService = app(CompanyDefaultService::class);
  52. $companyDefaultService->createCompanyDefaults($company, $company->owner, 'USD', $countryCode, 'en');
  53. });
  54. }
  55. public function withTransactions(int $count = 2000): self
  56. {
  57. return $this->afterCreating(function (Company $company) use ($count) {
  58. $defaultBankAccount = $company->default->bankAccount;
  59. Transaction::factory()
  60. ->forCompanyAndBankAccount($company, $defaultBankAccount)
  61. ->count($count)
  62. ->create([
  63. 'created_by' => $company->user_id,
  64. 'updated_by' => $company->user_id,
  65. ]);
  66. });
  67. }
  68. public function withClients(int $count = 10): self
  69. {
  70. return $this->has(Client::factory()->count($count)->withPrimaryContact()->withAddresses());
  71. }
  72. public function withVendors(int $count = 10): self
  73. {
  74. return $this->has(Vendor::factory()->count($count)->withContact()->withAddress());
  75. }
  76. public function withOfferings(int $count = 10): self
  77. {
  78. return $this->afterCreating(function (Company $company) use ($count) {
  79. Offering::factory()
  80. ->count($count)
  81. ->sellable()
  82. ->withSalesAdjustments()
  83. ->purchasable()
  84. ->withPurchaseAdjustments()
  85. ->create([
  86. 'company_id' => $company->id,
  87. 'created_by' => $company->user_id,
  88. 'updated_by' => $company->user_id,
  89. ]);
  90. });
  91. }
  92. public function withInvoices(int $count = 10): self
  93. {
  94. return $this->afterCreating(function (Company $company) use ($count) {
  95. $draftCount = (int) floor($count * 0.2);
  96. $approvedCount = (int) floor($count * 0.2);
  97. $paidCount = (int) floor($count * 0.3);
  98. $partialCount = (int) floor($count * 0.2);
  99. $overpaidCount = $count - ($draftCount + $approvedCount + $paidCount + $partialCount);
  100. Invoice::factory()
  101. ->count($draftCount)
  102. ->withLineItems()
  103. ->create([
  104. 'company_id' => $company->id,
  105. 'created_by' => $company->user_id,
  106. 'updated_by' => $company->user_id,
  107. ]);
  108. Invoice::factory()
  109. ->count($approvedCount)
  110. ->withLineItems()
  111. ->approved()
  112. ->create([
  113. 'company_id' => $company->id,
  114. 'created_by' => $company->user_id,
  115. 'updated_by' => $company->user_id,
  116. ]);
  117. Invoice::factory()
  118. ->count($paidCount)
  119. ->withLineItems()
  120. ->approved()
  121. ->withPayments(max: 4)
  122. ->create([
  123. 'company_id' => $company->id,
  124. 'created_by' => $company->user_id,
  125. 'updated_by' => $company->user_id,
  126. ]);
  127. Invoice::factory()
  128. ->count($partialCount)
  129. ->withLineItems()
  130. ->approved()
  131. ->withPayments(max: 4, invoiceStatus: InvoiceStatus::Partial)
  132. ->create([
  133. 'company_id' => $company->id,
  134. 'created_by' => $company->user_id,
  135. 'updated_by' => $company->user_id,
  136. ]);
  137. Invoice::factory()
  138. ->count($overpaidCount)
  139. ->withLineItems()
  140. ->approved()
  141. ->withPayments(max: 4, invoiceStatus: InvoiceStatus::Overpaid)
  142. ->create([
  143. 'company_id' => $company->id,
  144. 'created_by' => $company->user_id,
  145. 'updated_by' => $company->user_id,
  146. ]);
  147. });
  148. }
  149. public function withEstimates(int $count = 10): self
  150. {
  151. return $this->afterCreating(function (Company $company) use ($count) {
  152. $draftCount = (int) floor($count * 0.2); // 20% drafts
  153. $approvedCount = (int) floor($count * 0.3); // 30% approved
  154. $acceptedCount = (int) floor($count * 0.2); // 20% accepted
  155. $declinedCount = (int) floor($count * 0.2); // 20% declined
  156. $expiredCount = $count - ($draftCount + $approvedCount + $acceptedCount + $declinedCount); // remaining as expired
  157. // Create draft estimates
  158. Estimate::factory()
  159. ->count($draftCount)
  160. ->withLineItems()
  161. ->create([
  162. 'company_id' => $company->id,
  163. 'created_by' => $company->user_id,
  164. 'updated_by' => $company->user_id,
  165. ]);
  166. // Create pending (approved) estimates
  167. Estimate::factory()
  168. ->count($approvedCount)
  169. ->withLineItems()
  170. ->approved()
  171. ->create([
  172. 'company_id' => $company->id,
  173. 'created_by' => $company->user_id,
  174. 'updated_by' => $company->user_id,
  175. ]);
  176. // Create accepted estimates
  177. Estimate::factory()
  178. ->count($acceptedCount)
  179. ->withLineItems()
  180. ->accepted()
  181. ->create([
  182. 'company_id' => $company->id,
  183. 'created_by' => $company->user_id,
  184. 'updated_by' => $company->user_id,
  185. ]);
  186. // Create declined estimates
  187. Estimate::factory()
  188. ->count($declinedCount)
  189. ->withLineItems()
  190. ->declined()
  191. ->create([
  192. 'company_id' => $company->id,
  193. 'created_by' => $company->user_id,
  194. 'updated_by' => $company->user_id,
  195. ]);
  196. // Create expired estimates (approved but past expiration date)
  197. Estimate::factory()
  198. ->count($expiredCount)
  199. ->withLineItems()
  200. ->approved()
  201. ->state([
  202. 'expiration_date' => now()->subDays(rand(1, 30)),
  203. ])
  204. ->create([
  205. 'company_id' => $company->id,
  206. 'created_by' => $company->user_id,
  207. 'updated_by' => $company->user_id,
  208. ]);
  209. });
  210. }
  211. public function withBills(int $count = 10): self
  212. {
  213. return $this->afterCreating(function (Company $company) use ($count) {
  214. $unpaidCount = (int) floor($count * 0.4);
  215. $paidCount = (int) floor($count * 0.4);
  216. $partialCount = $count - ($unpaidCount + $paidCount);
  217. // Create unpaid bills
  218. Bill::factory()
  219. ->count($unpaidCount)
  220. ->withLineItems()
  221. ->initialized()
  222. ->create([
  223. 'company_id' => $company->id,
  224. 'created_by' => $company->user_id,
  225. 'updated_by' => $company->user_id,
  226. ]);
  227. // Create paid bills
  228. Bill::factory()
  229. ->count($paidCount)
  230. ->withLineItems()
  231. ->initialized()
  232. ->withPayments(max: 4)
  233. ->create([
  234. 'company_id' => $company->id,
  235. 'created_by' => $company->user_id,
  236. 'updated_by' => $company->user_id,
  237. ]);
  238. // Create partially paid bills
  239. Bill::factory()
  240. ->count($partialCount)
  241. ->withLineItems()
  242. ->initialized()
  243. ->withPayments(max: 4, billStatus: BillStatus::Partial)
  244. ->create([
  245. 'company_id' => $company->id,
  246. 'created_by' => $company->user_id,
  247. 'updated_by' => $company->user_id,
  248. ]);
  249. });
  250. }
  251. }