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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace Database\Factories;
  3. use App\Models\Accounting\Bill;
  4. use App\Models\Accounting\Estimate;
  5. use App\Models\Accounting\Invoice;
  6. use App\Models\Accounting\RecurringInvoice;
  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(?string $countryCode = null): self
  38. {
  39. return $this->afterCreating(function (Company $company) use ($countryCode) {
  40. CompanyProfile::factory()
  41. ->forCompany($company)
  42. ->withAddress($countryCode)
  43. ->create();
  44. });
  45. }
  46. /**
  47. * Set up default settings for the company after creation.
  48. */
  49. public function withCompanyDefaults(string $currencyCode = 'USD', string $locale = 'en'): self
  50. {
  51. return $this->afterCreating(function (Company $company) use ($currencyCode, $locale) {
  52. $countryCode = $company->profile->address->country_code;
  53. $companyDefaultService = app(CompanyDefaultService::class);
  54. $companyDefaultService->createCompanyDefaults($company, $company->owner, $currencyCode, $countryCode, $locale);
  55. });
  56. }
  57. public function withTransactions(int $count = 2000): self
  58. {
  59. return $this->afterCreating(function (Company $company) use ($count) {
  60. $defaultBankAccount = $company->default->bankAccount;
  61. Transaction::factory()
  62. ->forCompanyAndBankAccount($company, $defaultBankAccount)
  63. ->count($count)
  64. ->create([
  65. 'created_by' => $company->user_id,
  66. 'updated_by' => $company->user_id,
  67. ]);
  68. });
  69. }
  70. public function withClients(int $count = 10): self
  71. {
  72. return $this->afterCreating(function (Company $company) use ($count) {
  73. Client::factory()
  74. ->count($count)
  75. ->withPrimaryContact()
  76. ->withAddresses()
  77. ->create([
  78. 'company_id' => $company->id,
  79. 'created_by' => $company->user_id,
  80. 'updated_by' => $company->user_id,
  81. ]);
  82. });
  83. }
  84. public function withVendors(int $count = 10): self
  85. {
  86. return $this->afterCreating(function (Company $company) use ($count) {
  87. Vendor::factory()
  88. ->count($count)
  89. ->withContact()
  90. ->withAddress()
  91. ->create([
  92. 'company_id' => $company->id,
  93. 'created_by' => $company->user_id,
  94. 'updated_by' => $company->user_id,
  95. ]);
  96. });
  97. }
  98. public function withOfferings(int $count = 10): self
  99. {
  100. return $this->afterCreating(function (Company $company) use ($count) {
  101. Offering::factory()
  102. ->count($count)
  103. ->withSalesAdjustments()
  104. ->withPurchaseAdjustments()
  105. ->create([
  106. 'company_id' => $company->id,
  107. 'created_by' => $company->user_id,
  108. 'updated_by' => $company->user_id,
  109. ]);
  110. });
  111. }
  112. public function withInvoices(int $count = 10): self
  113. {
  114. return $this->afterCreating(function (Company $company) use ($count) {
  115. $draftCount = (int) floor($count * 0.2);
  116. $approvedCount = (int) floor($count * 0.2);
  117. $paidCount = (int) floor($count * 0.3);
  118. $partialCount = (int) floor($count * 0.1);
  119. $overpaidCount = (int) floor($count * 0.1);
  120. $overdueCount = $count - ($draftCount + $approvedCount + $paidCount + $partialCount + $overpaidCount);
  121. Invoice::factory()
  122. ->count($draftCount)
  123. ->withLineItems()
  124. ->create([
  125. 'company_id' => $company->id,
  126. 'created_by' => $company->user_id,
  127. 'updated_by' => $company->user_id,
  128. ]);
  129. Invoice::factory()
  130. ->count($approvedCount)
  131. ->approved()
  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($paidCount)
  139. ->paid()
  140. ->create([
  141. 'company_id' => $company->id,
  142. 'created_by' => $company->user_id,
  143. 'updated_by' => $company->user_id,
  144. ]);
  145. Invoice::factory()
  146. ->count($partialCount)
  147. ->partial()
  148. ->create([
  149. 'company_id' => $company->id,
  150. 'created_by' => $company->user_id,
  151. 'updated_by' => $company->user_id,
  152. ]);
  153. Invoice::factory()
  154. ->count($overpaidCount)
  155. ->overpaid()
  156. ->create([
  157. 'company_id' => $company->id,
  158. 'created_by' => $company->user_id,
  159. 'updated_by' => $company->user_id,
  160. ]);
  161. Invoice::factory()
  162. ->count($overdueCount)
  163. ->overdue()
  164. ->create([
  165. 'company_id' => $company->id,
  166. 'created_by' => $company->user_id,
  167. 'updated_by' => $company->user_id,
  168. ]);
  169. });
  170. }
  171. public function withRecurringInvoices(int $count = 10): self
  172. {
  173. return $this->afterCreating(function (Company $company) use ($count) {
  174. $draftCount = (int) floor($count * 0.2); // 20% drafts without schedule
  175. $scheduledCount = (int) floor($count * 0.2); // 20% drafts with schedule
  176. $activeCount = (int) floor($count * 0.4); // 40% active and generating
  177. $endedCount = (int) floor($count * 0.1); // 10% manually ended
  178. $completedCount = $count - ($draftCount + $scheduledCount + $activeCount + $endedCount); // 10% completed by end conditions
  179. // Draft recurring invoices (no schedule)
  180. RecurringInvoice::factory()
  181. ->count($draftCount)
  182. ->create([
  183. 'company_id' => $company->id,
  184. 'created_by' => $company->user_id,
  185. 'updated_by' => $company->user_id,
  186. ]);
  187. // Draft recurring invoices with schedule
  188. RecurringInvoice::factory()
  189. ->count($scheduledCount)
  190. ->withSchedule()
  191. ->create([
  192. 'company_id' => $company->id,
  193. 'created_by' => $company->user_id,
  194. 'updated_by' => $company->user_id,
  195. ]);
  196. // Active recurring invoices with various schedules and historical invoices
  197. RecurringInvoice::factory()
  198. ->count($activeCount)
  199. ->active()
  200. ->create([
  201. 'company_id' => $company->id,
  202. 'created_by' => $company->user_id,
  203. 'updated_by' => $company->user_id,
  204. ]);
  205. // Manually ended recurring invoices
  206. RecurringInvoice::factory()
  207. ->count($endedCount)
  208. ->ended()
  209. ->create([
  210. 'company_id' => $company->id,
  211. 'created_by' => $company->user_id,
  212. 'updated_by' => $company->user_id,
  213. ]);
  214. // Completed recurring invoices (reached end conditions)
  215. RecurringInvoice::factory()
  216. ->count($completedCount)
  217. ->active()
  218. ->endAfter($this->faker->numberBetween(5, 12))
  219. ->create([
  220. 'company_id' => $company->id,
  221. 'created_by' => $company->user_id,
  222. 'updated_by' => $company->user_id,
  223. ]);
  224. });
  225. }
  226. public function withEstimates(int $count = 10): self
  227. {
  228. return $this->afterCreating(function (Company $company) use ($count) {
  229. $draftCount = (int) floor($count * 0.2); // 20% drafts
  230. $approvedCount = (int) floor($count * 0.3); // 30% approved
  231. $acceptedCount = (int) floor($count * 0.2); // 20% accepted
  232. $declinedCount = (int) floor($count * 0.1); // 10% declined
  233. $convertedCount = (int) floor($count * 0.1); // 10% converted to invoices
  234. $expiredCount = $count - ($draftCount + $approvedCount + $acceptedCount + $declinedCount + $convertedCount); // remaining 10%
  235. Estimate::factory()
  236. ->count($draftCount)
  237. ->create([
  238. 'company_id' => $company->id,
  239. 'created_by' => $company->user_id,
  240. 'updated_by' => $company->user_id,
  241. ]);
  242. Estimate::factory()
  243. ->count($approvedCount)
  244. ->approved()
  245. ->create([
  246. 'company_id' => $company->id,
  247. 'created_by' => $company->user_id,
  248. 'updated_by' => $company->user_id,
  249. ]);
  250. Estimate::factory()
  251. ->count($acceptedCount)
  252. ->accepted()
  253. ->create([
  254. 'company_id' => $company->id,
  255. 'created_by' => $company->user_id,
  256. 'updated_by' => $company->user_id,
  257. ]);
  258. Estimate::factory()
  259. ->count($declinedCount)
  260. ->declined()
  261. ->create([
  262. 'company_id' => $company->id,
  263. 'created_by' => $company->user_id,
  264. 'updated_by' => $company->user_id,
  265. ]);
  266. Estimate::factory()
  267. ->count($convertedCount)
  268. ->converted()
  269. ->create([
  270. 'company_id' => $company->id,
  271. 'created_by' => $company->user_id,
  272. 'updated_by' => $company->user_id,
  273. ]);
  274. Estimate::factory()
  275. ->count($expiredCount)
  276. ->expired()
  277. ->create([
  278. 'company_id' => $company->id,
  279. 'created_by' => $company->user_id,
  280. 'updated_by' => $company->user_id,
  281. ]);
  282. });
  283. }
  284. public function withBills(int $count = 10): self
  285. {
  286. return $this->afterCreating(function (Company $company) use ($count) {
  287. $unpaidCount = (int) floor($count * 0.4);
  288. $paidCount = (int) floor($count * 0.3);
  289. $partialCount = (int) floor($count * 0.2);
  290. $overdueCount = $count - ($unpaidCount + $paidCount + $partialCount);
  291. Bill::factory()
  292. ->count($unpaidCount)
  293. ->create([
  294. 'company_id' => $company->id,
  295. 'created_by' => $company->user_id,
  296. 'updated_by' => $company->user_id,
  297. ]);
  298. Bill::factory()
  299. ->count($paidCount)
  300. ->paid()
  301. ->create([
  302. 'company_id' => $company->id,
  303. 'created_by' => $company->user_id,
  304. 'updated_by' => $company->user_id,
  305. ]);
  306. Bill::factory()
  307. ->count($partialCount)
  308. ->partial()
  309. ->create([
  310. 'company_id' => $company->id,
  311. 'created_by' => $company->user_id,
  312. 'updated_by' => $company->user_id,
  313. ]);
  314. Bill::factory()
  315. ->count($overdueCount)
  316. ->overdue()
  317. ->create([
  318. 'company_id' => $company->id,
  319. 'created_by' => $company->user_id,
  320. 'updated_by' => $company->user_id,
  321. ]);
  322. });
  323. }
  324. }