Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CompanyFactory.php 13KB

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