Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CompanyFactory.php 13KB

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