Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CompanyFactory.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. ->create([
  124. 'company_id' => $company->id,
  125. 'created_by' => $company->user_id,
  126. 'updated_by' => $company->user_id,
  127. ]);
  128. Invoice::factory()
  129. ->count($approvedCount)
  130. ->approved()
  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($paidCount)
  138. ->paid()
  139. ->create([
  140. 'company_id' => $company->id,
  141. 'created_by' => $company->user_id,
  142. 'updated_by' => $company->user_id,
  143. ]);
  144. Invoice::factory()
  145. ->count($partialCount)
  146. ->partial()
  147. ->create([
  148. 'company_id' => $company->id,
  149. 'created_by' => $company->user_id,
  150. 'updated_by' => $company->user_id,
  151. ]);
  152. Invoice::factory()
  153. ->count($overpaidCount)
  154. ->overpaid()
  155. ->create([
  156. 'company_id' => $company->id,
  157. 'created_by' => $company->user_id,
  158. 'updated_by' => $company->user_id,
  159. ]);
  160. Invoice::factory()
  161. ->count($overdueCount)
  162. ->overdue()
  163. ->create([
  164. 'company_id' => $company->id,
  165. 'created_by' => $company->user_id,
  166. 'updated_by' => $company->user_id,
  167. ]);
  168. });
  169. }
  170. public function withRecurringInvoices(int $count = 10): self
  171. {
  172. return $this->afterCreating(function (Company $company) use ($count) {
  173. $draftCount = (int) floor($count * 0.2); // 20% drafts without schedule
  174. $scheduledCount = (int) floor($count * 0.2); // 20% drafts with schedule
  175. $activeCount = (int) floor($count * 0.4); // 40% active and generating
  176. $endedCount = (int) floor($count * 0.1); // 10% manually ended
  177. $completedCount = $count - ($draftCount + $scheduledCount + $activeCount + $endedCount); // 10% completed by end conditions
  178. // Draft recurring invoices (no schedule)
  179. RecurringInvoice::factory()
  180. ->count($draftCount)
  181. ->create([
  182. 'company_id' => $company->id,
  183. 'created_by' => $company->user_id,
  184. 'updated_by' => $company->user_id,
  185. ]);
  186. // Draft recurring invoices with schedule
  187. RecurringInvoice::factory()
  188. ->count($scheduledCount)
  189. ->withSchedule()
  190. ->create([
  191. 'company_id' => $company->id,
  192. 'created_by' => $company->user_id,
  193. 'updated_by' => $company->user_id,
  194. ]);
  195. // Active recurring invoices with various schedules and historical invoices
  196. RecurringInvoice::factory()
  197. ->count($activeCount)
  198. ->active()
  199. ->create([
  200. 'company_id' => $company->id,
  201. 'created_by' => $company->user_id,
  202. 'updated_by' => $company->user_id,
  203. ]);
  204. // Manually ended recurring invoices
  205. RecurringInvoice::factory()
  206. ->count($endedCount)
  207. ->ended()
  208. ->create([
  209. 'company_id' => $company->id,
  210. 'created_by' => $company->user_id,
  211. 'updated_by' => $company->user_id,
  212. ]);
  213. // Completed recurring invoices (reached end conditions)
  214. RecurringInvoice::factory()
  215. ->count($completedCount)
  216. ->active()
  217. ->endAfter($this->faker->numberBetween(5, 12))
  218. ->create([
  219. 'company_id' => $company->id,
  220. 'created_by' => $company->user_id,
  221. 'updated_by' => $company->user_id,
  222. ]);
  223. });
  224. }
  225. public function withEstimates(int $count = 10): self
  226. {
  227. return $this->afterCreating(function (Company $company) use ($count) {
  228. $draftCount = (int) floor($count * 0.2); // 20% drafts
  229. $approvedCount = (int) floor($count * 0.3); // 30% approved
  230. $acceptedCount = (int) floor($count * 0.2); // 20% accepted
  231. $declinedCount = (int) floor($count * 0.1); // 10% declined
  232. $convertedCount = (int) floor($count * 0.1); // 10% converted to invoices
  233. $expiredCount = $count - ($draftCount + $approvedCount + $acceptedCount + $declinedCount + $convertedCount); // remaining 10%
  234. Estimate::factory()
  235. ->count($draftCount)
  236. ->create([
  237. 'company_id' => $company->id,
  238. 'created_by' => $company->user_id,
  239. 'updated_by' => $company->user_id,
  240. ]);
  241. Estimate::factory()
  242. ->count($approvedCount)
  243. ->approved()
  244. ->create([
  245. 'company_id' => $company->id,
  246. 'created_by' => $company->user_id,
  247. 'updated_by' => $company->user_id,
  248. ]);
  249. Estimate::factory()
  250. ->count($acceptedCount)
  251. ->accepted()
  252. ->create([
  253. 'company_id' => $company->id,
  254. 'created_by' => $company->user_id,
  255. 'updated_by' => $company->user_id,
  256. ]);
  257. Estimate::factory()
  258. ->count($declinedCount)
  259. ->declined()
  260. ->create([
  261. 'company_id' => $company->id,
  262. 'created_by' => $company->user_id,
  263. 'updated_by' => $company->user_id,
  264. ]);
  265. Estimate::factory()
  266. ->count($convertedCount)
  267. ->converted()
  268. ->create([
  269. 'company_id' => $company->id,
  270. 'created_by' => $company->user_id,
  271. 'updated_by' => $company->user_id,
  272. ]);
  273. Estimate::factory()
  274. ->count($expiredCount)
  275. ->expired()
  276. ->create([
  277. 'company_id' => $company->id,
  278. 'created_by' => $company->user_id,
  279. 'updated_by' => $company->user_id,
  280. ]);
  281. });
  282. }
  283. public function withBills(int $count = 10): self
  284. {
  285. return $this->afterCreating(function (Company $company) use ($count) {
  286. $unpaidCount = (int) floor($count * 0.4);
  287. $paidCount = (int) floor($count * 0.3);
  288. $partialCount = (int) floor($count * 0.2);
  289. $overdueCount = $count - ($unpaidCount + $paidCount + $partialCount);
  290. Bill::factory()
  291. ->count($unpaidCount)
  292. ->create([
  293. 'company_id' => $company->id,
  294. 'created_by' => $company->user_id,
  295. 'updated_by' => $company->user_id,
  296. ]);
  297. Bill::factory()
  298. ->count($paidCount)
  299. ->paid()
  300. ->create([
  301. 'company_id' => $company->id,
  302. 'created_by' => $company->user_id,
  303. 'updated_by' => $company->user_id,
  304. ]);
  305. Bill::factory()
  306. ->count($partialCount)
  307. ->partial()
  308. ->create([
  309. 'company_id' => $company->id,
  310. 'created_by' => $company->user_id,
  311. 'updated_by' => $company->user_id,
  312. ]);
  313. Bill::factory()
  314. ->count($overdueCount)
  315. ->overdue()
  316. ->create([
  317. 'company_id' => $company->id,
  318. 'created_by' => $company->user_id,
  319. 'updated_by' => $company->user_id,
  320. ]);
  321. });
  322. }
  323. }