1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/database/seeds/RandomDataSeeder.php

260 lines
8.7 KiB
PHP
Raw Normal View History

<?php
use App\DataMapper\ClientSettings;
2019-09-10 04:30:43 +02:00
use App\DataMapper\CompanySettings;
use App\DataMapper\DefaultSettings;
use App\Events\Invoice\InvoiceWasMarkedSent;
2019-09-20 07:13:58 +02:00
use App\Events\Invoice\InvoiceWasUpdated;
2019-10-04 08:22:22 +02:00
use App\Events\Payment\PaymentWasCreated;
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
2019-10-01 11:59:32 +02:00
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
2019-10-04 08:22:22 +02:00
use App\Jobs\Invoice\UpdateInvoicePayment;
2019-10-01 07:54:21 +02:00
use App\Listeners\Invoice\CreateInvoiceInvitation;
use App\Models\Account;
use App\Models\Client;
use App\Models\ClientContact;
2019-09-14 14:34:05 +02:00
use App\Models\CompanyGateway;
2019-03-27 05:50:13 +01:00
use App\Models\CompanyToken;
2019-09-14 14:34:05 +02:00
use App\Models\GatewayType;
2019-09-10 04:30:43 +02:00
use App\Models\GroupSetting;
2019-09-20 07:13:58 +02:00
use App\Models\Invoice;
2019-10-10 13:08:02 +02:00
use App\Models\Payment;
2019-10-04 08:22:22 +02:00
use App\Models\PaymentType;
use App\Models\User;
use App\Models\UserAccount;
2019-10-01 07:54:21 +02:00
use App\Repositories\InvoiceRepository;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
class RandomDataSeeder extends Seeder
{
use \App\Utils\Traits\MakesHash;
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
/* Warm up the cache !*/
$cached_tables = config('ninja.cached_tables');
foreach ($cached_tables as $name => $class) {
if (! Cache::has($name)) {
// check that the table exists in case the migration is pending
if (! Schema::hasTable((new $class())->getTable())) {
continue;
}
if ($name == 'payment_terms') {
$orderBy = 'num_days';
} elseif ($name == 'fonts') {
$orderBy = 'sort_order';
} elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) {
$orderBy = 'name';
} else {
$orderBy = 'id';
}
$tableData = $class::orderBy($orderBy)->get();
if ($tableData->count()) {
Cache::forever($name, $tableData);
}
}
}
2019-10-01 08:03:57 +02:00
$this->command->info('Running RandomDataSeeder');
Eloquent::unguard();
$faker = Faker\Factory::create();
$account = factory(\App\Models\Account::class)->create();
$company = factory(\App\Models\Company::class)->create([
'account_id' => $account->id,
]);
$account->default_company_id = $company->id;
$account->save();
$user = factory(\App\Models\User::class)->create([
'email' => $faker->email,
2019-05-10 08:08:33 +02:00
// 'account_id' => $account->id,
'confirmation_code' => $this->createDbHash(config('database.default'))
]);
2019-03-27 05:50:13 +01:00
$company_token = CompanyToken::create([
'user_id' => $user->id,
'company_id' => $company->id,
'account_id' => $account->id,
'name' => 'test token',
2019-09-26 15:00:51 +02:00
'token' => \Illuminate\Support\Str::random(64),
2019-03-27 05:50:13 +01:00
]);
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'is_locked' => 0,
'permissions' => '',
'settings' => DefaultSettings::userSettings(),
]);
$client = factory(\App\Models\Client::class)->create([
'user_id' => $user->id,
'company_id' => $company->id
]);
ClientContact::create([
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
2019-08-29 00:13:26 +02:00
'email' => config('ninja.testvars.username'),
'company_id' => $company->id,
'password' => Hash::make(config('ninja.testvars.password')),
'email_verified_at' => now(),
'client_id' =>$client->id,
'contact_key' => \Illuminate\Support\Str::random(40),
]);
factory(\App\Models\Client::class, 10)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class,5)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id
]);
});
2019-04-03 02:09:22 +02:00
/** Product Factory */
factory(\App\Models\Product::class,20)->create(['user_id' => $user->id, 'company_id' => $company->id]);
2019-04-03 02:09:22 +02:00
2019-04-04 03:38:17 +02:00
/** Invoice Factory */
factory(\App\Models\Invoice::class,20)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]);
$invoices = Invoice::cursor();
2019-10-01 07:54:21 +02:00
$invoice_repo = new InvoiceRepository();
2019-09-20 07:13:58 +02:00
2019-10-04 08:22:22 +02:00
$invoices->each(function ($invoice) use($invoice_repo, $user, $company, $client){
2019-10-17 00:51:05 +02:00
$invoice_calc = null;
if($invoice->uses_inclusive_taxes)
$invoice_calc = new InvoiceSumInclusive($invoice);
else
$invoice_calc = new InvoiceSum($invoice);
2019-09-20 07:13:58 +02:00
2019-10-01 07:54:21 +02:00
$invoice = $invoice_calc->build()->getInvoice();
2019-10-01 07:54:21 +02:00
$invoice->save();
event(new CreateInvoiceInvitation($invoice));
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company);
2019-10-01 07:54:21 +02:00
$invoice_repo->markSent($invoice);
event(new InvoiceWasMarkedSent($invoice, $company));
2019-10-04 08:22:22 +02:00
if(rand(0, 1)) {
$payment = App\Models\Payment::create([
'date' => now(),
'user_id' => $user->id,
'company_id' => $company->id,
'client_id' => $client->id,
'amount' => $invoice->balance,
'transaction_reference' => rand(0,500),
'type_id' => PaymentType::CREDIT_CARD_OTHER,
2019-10-10 13:08:02 +02:00
'status_id' => Payment::STATUS_COMPLETED,
]);
$payment->invoices()->save($invoice);
event(new PaymentWasCreated($payment, $payment->company));
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
}
2019-09-20 07:13:58 +02:00
});
2019-08-15 13:10:02 +02:00
/** Recurring Invoice Factory */
factory(\App\Models\RecurringInvoice::class,10)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]);
2019-08-15 13:10:02 +02:00
// factory(\App\Models\Payment::class,20)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id, 'settings' => ClientSettings::buildClientSettings($company->settings, $client->settings)]);
2019-08-15 13:10:02 +02:00
2019-05-10 08:08:33 +02:00
$clients = Client::all();
2019-05-10 08:08:33 +02:00
foreach($clients as $client)
{
2019-08-15 13:10:02 +02:00
//$client->getNextClientNumber($client);
$client->id_number = $client->getNextClientNumber($client);
2019-05-10 08:08:33 +02:00
$client->save();
}
2019-06-03 07:31:20 +02:00
2019-09-10 04:30:43 +02:00
GroupSetting::create([
'company_id' => $company->id,
'user_id' => $user->id,
2019-09-19 07:50:05 +02:00
'settings' => ClientSettings::buildClientSettings(CompanySettings::defaults(), ClientSettings::defaults()),
2019-09-10 04:30:43 +02:00
'name' => 'Default Client Settings',
]);
2019-09-14 14:34:05 +02:00
if(config('ninja.testvars.stripe'))
{
2019-09-15 13:40:46 +02:00
2019-09-14 14:34:05 +02:00
$cg = new CompanyGateway;
$cg->company_id = $company->id;
$cg->user_id = $user->id;
$cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23';
2019-09-14 14:34:05 +02:00
$cg->require_cvv = true;
2019-10-17 10:26:46 +02:00
$cg->show_billing_address = true;
2019-09-14 14:34:05 +02:00
$cg->show_shipping_address = true;
$cg->update_details = true;
2019-09-16 04:05:30 +02:00
$cg->config = encrypt(config('ninja.testvars.stripe'));
2019-09-14 14:34:05 +02:00
$cg->save();
2019-09-15 13:40:46 +02:00
2019-09-18 14:43:37 +02:00
$cg = new CompanyGateway;
$cg->company_id = $company->id;
$cg->user_id = $user->id;
$cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23';
2019-09-18 14:43:37 +02:00
$cg->require_cvv = true;
2019-10-17 10:26:46 +02:00
$cg->show_billing_address = true;
2019-09-18 14:43:37 +02:00
$cg->show_shipping_address = true;
$cg->update_details = true;
$cg->config = encrypt(config('ninja.testvars.stripe'));
2019-09-30 01:26:37 +02:00
$cg->save();
}
if(config('ninja.testvars.paypal'))
{
$cg = new CompanyGateway;
$cg->company_id = $company->id;
$cg->user_id = $user->id;
$cg->gateway_key = '38f2c48af60c7dd69e04248cbb24c36e';
$cg->require_cvv = true;
2019-10-17 10:26:46 +02:00
$cg->show_billing_address = true;
2019-09-30 01:26:37 +02:00
$cg->show_shipping_address = true;
$cg->update_details = true;
$cg->config = encrypt(config('ninja.testvars.paypal'));
2019-09-18 14:43:37 +02:00
$cg->save();
2019-09-14 14:34:05 +02:00
}
}
2019-09-14 14:34:05 +02:00
}