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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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\Transaction;
  7. use App\Models\Common\Client;
  8. use App\Models\Common\Offering;
  9. use App\Models\Common\Vendor;
  10. use App\Models\Company;
  11. use App\Models\Setting\CompanyProfile;
  12. use App\Models\User;
  13. use App\Services\CompanyDefaultService;
  14. use Illuminate\Database\Eloquent\Factories\Factory;
  15. class CompanyFactory extends Factory
  16. {
  17. /**
  18. * The name of the factory's corresponding model.
  19. *
  20. * @var string
  21. */
  22. protected $model = Company::class;
  23. /**
  24. * Define the model's default state.
  25. *
  26. * @return array<string, mixed>
  27. */
  28. public function definition(): array
  29. {
  30. return [
  31. 'name' => $this->faker->unique()->company(),
  32. 'user_id' => User::factory(),
  33. 'personal_company' => true,
  34. ];
  35. }
  36. public function withCompanyProfile(): self
  37. {
  38. return $this->afterCreating(function (Company $company) {
  39. CompanyProfile::factory()->forCompany($company)->withCountry('US')->create();
  40. });
  41. }
  42. /**
  43. * Set up default settings for the company after creation.
  44. */
  45. public function withCompanyDefaults(): self
  46. {
  47. return $this->afterCreating(function (Company $company) {
  48. $countryCode = $company->profile->country;
  49. $companyDefaultService = app(CompanyDefaultService::class);
  50. $companyDefaultService->createCompanyDefaults($company, $company->owner, 'USD', $countryCode, 'en');
  51. });
  52. }
  53. public function withTransactions(int $count = 2000): self
  54. {
  55. return $this->afterCreating(function (Company $company) use ($count) {
  56. $defaultBankAccount = $company->default->bankAccount;
  57. Transaction::factory()
  58. ->forCompanyAndBankAccount($company, $defaultBankAccount)
  59. ->count($count)
  60. ->create([
  61. 'created_by' => $company->user_id,
  62. 'updated_by' => $company->user_id,
  63. ]);
  64. });
  65. }
  66. public function withClients(int $count = 10): self
  67. {
  68. return $this->has(Client::factory()->count($count)->withPrimaryContact()->withAddresses());
  69. }
  70. public function withVendors(int $count = 10): self
  71. {
  72. return $this->has(Vendor::factory()->count($count)->withContact()->withAddress());
  73. }
  74. public function withOfferings(int $count = 10): self
  75. {
  76. return $this->afterCreating(function (Company $company) use ($count) {
  77. Offering::factory()
  78. ->count($count)
  79. ->sellable()
  80. ->withSalesAdjustments()
  81. ->purchasable()
  82. ->withPurchaseAdjustments()
  83. ->create([
  84. 'company_id' => $company->id,
  85. 'created_by' => $company->user_id,
  86. 'updated_by' => $company->user_id,
  87. ]);
  88. });
  89. }
  90. public function withInvoices(int $count = 10): self
  91. {
  92. return $this->afterCreating(function (Company $company) use ($count) {
  93. $draftCount = (int) floor($count * 0.2);
  94. $approvedCount = (int) floor($count * 0.2);
  95. $paidCount = (int) floor($count * 0.3);
  96. $partialCount = (int) floor($count * 0.1);
  97. $overpaidCount = (int) floor($count * 0.1);
  98. $overdueCount = $count - ($draftCount + $approvedCount + $paidCount + $partialCount + $overpaidCount);
  99. Invoice::factory()
  100. ->count($draftCount)
  101. ->create([
  102. 'company_id' => $company->id,
  103. 'created_by' => $company->user_id,
  104. 'updated_by' => $company->user_id,
  105. ]);
  106. Invoice::factory()
  107. ->count($approvedCount)
  108. ->approved()
  109. ->create([
  110. 'company_id' => $company->id,
  111. 'created_by' => $company->user_id,
  112. 'updated_by' => $company->user_id,
  113. ]);
  114. Invoice::factory()
  115. ->count($paidCount)
  116. ->paid()
  117. ->create([
  118. 'company_id' => $company->id,
  119. 'created_by' => $company->user_id,
  120. 'updated_by' => $company->user_id,
  121. ]);
  122. Invoice::factory()
  123. ->count($partialCount)
  124. ->partial()
  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($overpaidCount)
  132. ->overpaid()
  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($overdueCount)
  140. ->overdue()
  141. ->create([
  142. 'company_id' => $company->id,
  143. 'created_by' => $company->user_id,
  144. 'updated_by' => $company->user_id,
  145. ]);
  146. });
  147. }
  148. public function withEstimates(int $count = 10): self
  149. {
  150. return $this->afterCreating(function (Company $company) use ($count) {
  151. $draftCount = (int) floor($count * 0.2); // 20% drafts
  152. $approvedCount = (int) floor($count * 0.3); // 30% approved
  153. $acceptedCount = (int) floor($count * 0.2); // 20% accepted
  154. $declinedCount = (int) floor($count * 0.1); // 10% declined
  155. $convertedCount = (int) floor($count * 0.1); // 10% converted to invoices
  156. $expiredCount = $count - ($draftCount + $approvedCount + $acceptedCount + $declinedCount + $convertedCount); // remaining 10%
  157. Estimate::factory()
  158. ->count($draftCount)
  159. ->create([
  160. 'company_id' => $company->id,
  161. 'created_by' => $company->user_id,
  162. 'updated_by' => $company->user_id,
  163. ]);
  164. Estimate::factory()
  165. ->count($approvedCount)
  166. ->approved()
  167. ->create([
  168. 'company_id' => $company->id,
  169. 'created_by' => $company->user_id,
  170. 'updated_by' => $company->user_id,
  171. ]);
  172. Estimate::factory()
  173. ->count($acceptedCount)
  174. ->accepted()
  175. ->create([
  176. 'company_id' => $company->id,
  177. 'created_by' => $company->user_id,
  178. 'updated_by' => $company->user_id,
  179. ]);
  180. Estimate::factory()
  181. ->count($declinedCount)
  182. ->declined()
  183. ->create([
  184. 'company_id' => $company->id,
  185. 'created_by' => $company->user_id,
  186. 'updated_by' => $company->user_id,
  187. ]);
  188. Estimate::factory()
  189. ->count($convertedCount)
  190. ->converted()
  191. ->create([
  192. 'company_id' => $company->id,
  193. 'created_by' => $company->user_id,
  194. 'updated_by' => $company->user_id,
  195. ]);
  196. Estimate::factory()
  197. ->count($expiredCount)
  198. ->expired()
  199. ->create([
  200. 'company_id' => $company->id,
  201. 'created_by' => $company->user_id,
  202. 'updated_by' => $company->user_id,
  203. ]);
  204. });
  205. }
  206. public function withBills(int $count = 10): self
  207. {
  208. return $this->afterCreating(function (Company $company) use ($count) {
  209. $unpaidCount = (int) floor($count * 0.4);
  210. $paidCount = (int) floor($count * 0.3);
  211. $partialCount = (int) floor($count * 0.2);
  212. $overdueCount = $count - ($unpaidCount + $paidCount + $partialCount);
  213. Bill::factory()
  214. ->count($unpaidCount)
  215. ->create([
  216. 'company_id' => $company->id,
  217. 'created_by' => $company->user_id,
  218. 'updated_by' => $company->user_id,
  219. ]);
  220. Bill::factory()
  221. ->count($paidCount)
  222. ->paid()
  223. ->create([
  224. 'company_id' => $company->id,
  225. 'created_by' => $company->user_id,
  226. 'updated_by' => $company->user_id,
  227. ]);
  228. Bill::factory()
  229. ->count($partialCount)
  230. ->partial()
  231. ->create([
  232. 'company_id' => $company->id,
  233. 'created_by' => $company->user_id,
  234. 'updated_by' => $company->user_id,
  235. ]);
  236. Bill::factory()
  237. ->count($overdueCount)
  238. ->overdue()
  239. ->create([
  240. 'company_id' => $company->id,
  241. 'created_by' => $company->user_id,
  242. 'updated_by' => $company->user_id,
  243. ]);
  244. });
  245. }
  246. }