| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 | 
							- <?php
 - 
 - namespace Database\Factories;
 - 
 - use App\Enums\Accounting\InvoiceStatus;
 - use App\Models\Accounting\Invoice;
 - 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.2);
 -             $overpaidCount = $count - ($draftCount + $approvedCount + $paidCount + $partialCount);
 - 
 -             Invoice::factory()
 -                 ->count($draftCount)
 -                 ->withLineItems()
 -                 ->create([
 -                     'company_id' => $company->id,
 -                     'created_by' => $company->user_id,
 -                     'updated_by' => $company->user_id,
 -                 ]);
 - 
 -             Invoice::factory()
 -                 ->count($approvedCount)
 -                 ->withLineItems()
 -                 ->approved()
 -                 ->create([
 -                     'company_id' => $company->id,
 -                     'created_by' => $company->user_id,
 -                     'updated_by' => $company->user_id,
 -                 ]);
 - 
 -             Invoice::factory()
 -                 ->count($paidCount)
 -                 ->withLineItems()
 -                 ->approved()
 -                 ->withPayments(max: 4)
 -                 ->create([
 -                     'company_id' => $company->id,
 -                     'created_by' => $company->user_id,
 -                     'updated_by' => $company->user_id,
 -                 ]);
 - 
 -             Invoice::factory()
 -                 ->count($partialCount)
 -                 ->withLineItems()
 -                 ->approved()
 -                 ->withPayments(max: 4, invoiceStatus: InvoiceStatus::Partial)
 -                 ->create([
 -                     'company_id' => $company->id,
 -                     'created_by' => $company->user_id,
 -                     'updated_by' => $company->user_id,
 -                 ]);
 - 
 -             Invoice::factory()
 -                 ->count($overpaidCount)
 -                 ->withLineItems()
 -                 ->approved()
 -                 ->withPayments(max: 4, invoiceStatus: InvoiceStatus::Overpaid)
 -                 ->create([
 -                     'company_id' => $company->id,
 -                     'created_by' => $company->user_id,
 -                     'updated_by' => $company->user_id,
 -                 ]);
 -         });
 -     }
 - }
 
 
  |