123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
-
- namespace Database\Factories;
-
- use App\Enums\Accounting\Frequency;
- use App\Models\Accounting\Bill;
- use App\Models\Accounting\Estimate;
- use App\Models\Accounting\Invoice;
- use App\Models\Accounting\RecurringInvoice;
- use App\Models\Accounting\Transaction;
- use App\Models\Common\Client;
- use App\Models\Common\Offering;
- use App\Models\Common\Vendor;
- use App\Models\Company;
- use App\Models\Setting\CompanyProfile;
- use App\Models\User;
- use App\Services\CompanyDefaultService;
- use Illuminate\Database\Eloquent\Factories\Factory;
-
- class CompanyFactory extends Factory
- {
- /**
- * The name of the factory's corresponding model.
- *
- * @var string
- */
- protected $model = Company::class;
-
- /**
- * Define the model's default state.
- *
- * @return array<string, mixed>
- */
- public function definition(): array
- {
- return [
- 'name' => $this->faker->unique()->company(),
- 'user_id' => User::factory(),
- 'personal_company' => true,
- ];
- }
-
- public function withCompanyProfile(): self
- {
- return $this->afterCreating(function (Company $company) {
- CompanyProfile::factory()->forCompany($company)->withCountry('US')->create();
- });
- }
-
- /**
- * Set up default settings for the company after creation.
- */
- public function withCompanyDefaults(): self
- {
- return $this->afterCreating(function (Company $company) {
- $countryCode = $company->profile->country;
- $companyDefaultService = app(CompanyDefaultService::class);
- $companyDefaultService->createCompanyDefaults($company, $company->owner, 'USD', $countryCode, 'en');
- });
- }
-
- public function withTransactions(int $count = 2000): self
- {
- return $this->afterCreating(function (Company $company) use ($count) {
- $defaultBankAccount = $company->default->bankAccount;
-
- Transaction::factory()
- ->forCompanyAndBankAccount($company, $defaultBankAccount)
- ->count($count)
- ->create([
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
- });
- }
-
- public function withClients(int $count = 10): self
- {
- return $this->has(Client::factory()->count($count)->withPrimaryContact()->withAddresses());
- }
-
- public function withVendors(int $count = 10): self
- {
- return $this->has(Vendor::factory()->count($count)->withContact()->withAddress());
- }
-
- public function withOfferings(int $count = 10): self
- {
- return $this->afterCreating(function (Company $company) use ($count) {
- Offering::factory()
- ->count($count)
- ->sellable()
- ->withSalesAdjustments()
- ->purchasable()
- ->withPurchaseAdjustments()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
- });
- }
-
- public function withInvoices(int $count = 10): self
- {
- return $this->afterCreating(function (Company $company) use ($count) {
- $draftCount = (int) floor($count * 0.2);
- $approvedCount = (int) floor($count * 0.2);
- $paidCount = (int) floor($count * 0.3);
- $partialCount = (int) floor($count * 0.1);
- $overpaidCount = (int) floor($count * 0.1);
- $overdueCount = $count - ($draftCount + $approvedCount + $paidCount + $partialCount + $overpaidCount);
-
- Invoice::factory()
- ->count($draftCount)
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Invoice::factory()
- ->count($approvedCount)
- ->approved()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Invoice::factory()
- ->count($paidCount)
- ->paid()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Invoice::factory()
- ->count($partialCount)
- ->partial()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Invoice::factory()
- ->count($overpaidCount)
- ->overpaid()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Invoice::factory()
- ->count($overdueCount)
- ->overdue()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
- });
- }
-
- public function withRecurringInvoices(int $count = 10): self
- {
- return $this->afterCreating(function (Company $company) use ($count) {
- $draftCount = (int) floor($count * 0.2); // 20% drafts without schedule
- $scheduledCount = (int) floor($count * 0.2); // 20% drafts with schedule
- $activeCount = (int) floor($count * 0.4); // 40% active and generating
- $endedCount = (int) floor($count * 0.1); // 10% manually ended
- $completedCount = $count - ($draftCount + $scheduledCount + $activeCount + $endedCount); // 10% completed by end conditions
-
- // Draft recurring invoices (no schedule)
- RecurringInvoice::factory()
- ->count($draftCount)
- ->withLineItems()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- // Draft recurring invoices with schedule
- RecurringInvoice::factory()
- ->count($scheduledCount)
- ->withLineItems()
- ->withSchedule()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- // Active recurring invoices with various schedules and historical invoices
- $frequencies = [
- Frequency::Daily,
- Frequency::Weekly,
- Frequency::Monthly,
- Frequency::Yearly,
- Frequency::Custom,
- ];
-
- foreach (array_chunk(range(1, $activeCount), (int) ceil($activeCount / count($frequencies))) as $chunk) {
- RecurringInvoice::factory()
- ->count(count($chunk))
- ->withLineItems()
- ->withSchedule(fake()->randomElement($frequencies)) // Randomize frequency
- ->active()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
- }
-
- // Manually ended recurring invoices
- RecurringInvoice::factory()
- ->count($endedCount)
- ->withLineItems()
- ->withSchedule()
- ->ended()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- // Completed recurring invoices (reached end conditions)
- RecurringInvoice::factory()
- ->count($completedCount)
- ->withLineItems()
- ->withSchedule()
- ->endAfter($this->faker->numberBetween(5, 12))
- ->active()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
- });
- }
-
- public function withEstimates(int $count = 10): self
- {
- return $this->afterCreating(function (Company $company) use ($count) {
- $draftCount = (int) floor($count * 0.2); // 20% drafts
- $approvedCount = (int) floor($count * 0.3); // 30% approved
- $acceptedCount = (int) floor($count * 0.2); // 20% accepted
- $declinedCount = (int) floor($count * 0.1); // 10% declined
- $convertedCount = (int) floor($count * 0.1); // 10% converted to invoices
- $expiredCount = $count - ($draftCount + $approvedCount + $acceptedCount + $declinedCount + $convertedCount); // remaining 10%
-
- Estimate::factory()
- ->count($draftCount)
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Estimate::factory()
- ->count($approvedCount)
- ->approved()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Estimate::factory()
- ->count($acceptedCount)
- ->accepted()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Estimate::factory()
- ->count($declinedCount)
- ->declined()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Estimate::factory()
- ->count($convertedCount)
- ->converted()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Estimate::factory()
- ->count($expiredCount)
- ->expired()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
- });
- }
-
- public function withBills(int $count = 10): self
- {
- return $this->afterCreating(function (Company $company) use ($count) {
- $unpaidCount = (int) floor($count * 0.4);
- $paidCount = (int) floor($count * 0.3);
- $partialCount = (int) floor($count * 0.2);
- $overdueCount = $count - ($unpaidCount + $paidCount + $partialCount);
-
- Bill::factory()
- ->count($unpaidCount)
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Bill::factory()
- ->count($paidCount)
- ->paid()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Bill::factory()
- ->count($partialCount)
- ->partial()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
-
- Bill::factory()
- ->count($overdueCount)
- ->overdue()
- ->create([
- 'company_id' => $company->id,
- 'created_by' => $company->user_id,
- 'updated_by' => $company->user_id,
- ]);
- });
- }
- }
|