Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CompanyFactory.php 12KB

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