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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.1); // 10% declined
  156. $convertedCount = (int) floor($count * 0.1); // 10% converted to invoices
  157. $expiredCount = $count - ($draftCount + $approvedCount + $acceptedCount + $declinedCount + $convertedCount); // remaining 10%
  158. // Create draft estimates
  159. Estimate::factory()
  160. ->count($draftCount)
  161. ->withLineItems()
  162. ->create([
  163. 'company_id' => $company->id,
  164. 'created_by' => $company->user_id,
  165. 'updated_by' => $company->user_id,
  166. ]);
  167. // Create pending (approved) estimates
  168. Estimate::factory()
  169. ->count($approvedCount)
  170. ->withLineItems()
  171. ->approved()
  172. ->create([
  173. 'company_id' => $company->id,
  174. 'created_by' => $company->user_id,
  175. 'updated_by' => $company->user_id,
  176. ]);
  177. // Create accepted estimates
  178. Estimate::factory()
  179. ->count($acceptedCount)
  180. ->withLineItems()
  181. ->accepted()
  182. ->create([
  183. 'company_id' => $company->id,
  184. 'created_by' => $company->user_id,
  185. 'updated_by' => $company->user_id,
  186. ]);
  187. // Create declined estimates
  188. Estimate::factory()
  189. ->count($declinedCount)
  190. ->withLineItems()
  191. ->declined()
  192. ->create([
  193. 'company_id' => $company->id,
  194. 'created_by' => $company->user_id,
  195. 'updated_by' => $company->user_id,
  196. ]);
  197. // Create converted estimates
  198. Estimate::factory()
  199. ->count($convertedCount)
  200. ->withLineItems()
  201. ->accepted()
  202. ->converted()
  203. ->create([
  204. 'company_id' => $company->id,
  205. 'created_by' => $company->user_id,
  206. 'updated_by' => $company->user_id,
  207. ]);
  208. // Create expired estimates (approved but past expiration date)
  209. Estimate::factory()
  210. ->count($expiredCount)
  211. ->withLineItems()
  212. ->approved()
  213. ->state([
  214. 'expiration_date' => now()->subDays(rand(1, 30)),
  215. ])
  216. ->create([
  217. 'company_id' => $company->id,
  218. 'created_by' => $company->user_id,
  219. 'updated_by' => $company->user_id,
  220. ]);
  221. });
  222. }
  223. public function withBills(int $count = 10): self
  224. {
  225. return $this->afterCreating(function (Company $company) use ($count) {
  226. $unpaidCount = (int) floor($count * 0.4);
  227. $paidCount = (int) floor($count * 0.4);
  228. $partialCount = $count - ($unpaidCount + $paidCount);
  229. // Create unpaid bills
  230. Bill::factory()
  231. ->count($unpaidCount)
  232. ->withLineItems()
  233. ->initialized()
  234. ->create([
  235. 'company_id' => $company->id,
  236. 'created_by' => $company->user_id,
  237. 'updated_by' => $company->user_id,
  238. ]);
  239. // Create paid bills
  240. Bill::factory()
  241. ->count($paidCount)
  242. ->withLineItems()
  243. ->initialized()
  244. ->withPayments(max: 4)
  245. ->create([
  246. 'company_id' => $company->id,
  247. 'created_by' => $company->user_id,
  248. 'updated_by' => $company->user_id,
  249. ]);
  250. // Create partially paid bills
  251. Bill::factory()
  252. ->count($partialCount)
  253. ->withLineItems()
  254. ->initialized()
  255. ->withPayments(max: 4, billStatus: BillStatus::Partial)
  256. ->create([
  257. 'company_id' => $company->id,
  258. 'created_by' => $company->user_id,
  259. 'updated_by' => $company->user_id,
  260. ]);
  261. });
  262. }
  263. }