From 1f0dd178dca9f2d124ee365d606a973420c700e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Tue, 21 Jan 2020 19:23:16 +0300 Subject: [PATCH 1/4] Add sample data seeder --- app/Console/Commands/SampleData.php | 47 ++++++++++++ database/seeds/SampleData.php | 109 ++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100755 app/Console/Commands/SampleData.php create mode 100755 database/seeds/SampleData.php diff --git a/app/Console/Commands/SampleData.php b/app/Console/Commands/SampleData.php new file mode 100755 index 000000000..e5017865a --- /dev/null +++ b/app/Console/Commands/SampleData.php @@ -0,0 +1,47 @@ +laravel->make(SampleDataSeeder::class); + + $seeder = $class->setContainer($this->laravel)->setCommand($this); + + $seeder->__invoke(); + } +} diff --git a/database/seeds/SampleData.php b/database/seeds/SampleData.php new file mode 100755 index 000000000..8183449b1 --- /dev/null +++ b/database/seeds/SampleData.php @@ -0,0 +1,109 @@ +command->option('count'); + + for ($i = 0; $i < $count; $i++) { + $this->dispatch(new CreateContact(factory(Contact::class)->raw())); + } + + for ($i = 0; $i < $count; $i++) { + $this->dispatch(new CreateItem(factory(Item::class)->raw())); + } + + for ($i = 0; $i < $count; $i++) { + $this->dispatch(new CreateAccount(factory(Account::class)->raw())); + } + + for ($i = 0; $i < $count; $i++) { + $this->dispatch(new CreateBill(factory(Bill::class)->state('items')->raw())); + } + + for ($i = 0; $i < $count; $i++) { + $this->dispatch(new CreateInvoice(factory(Invoice::class)->state('items')->raw())); + } + + for ($i = 0; $i < $count; $i++) { + $amount = $faker->randomFloat(2, 1, 1000); + $invoices = Invoice::where('status', 'sent')->get(); + + if (0 === $invoices->count()) { + continue; + } + + $invoice = $invoices->random(1)->first(); + + $this->dispatch( + new CreateDocumentTransaction( + $invoice, + factory(Transaction::class)->state('income')->raw( + [ + 'contact_id' => $invoice->contact_id, + 'document_id' => $invoice->id, + 'amount' => $amount > $invoice->amount ? $invoice->amount : $amount, + ] + ) + ) + ); + } + + for ($i = 0; $i < $count; $i++) { + $amount = $faker->randomFloat(2, 1, 1000); + $bills = Bill::where('status', 'received')->get(); + + if (0 === $bills->count()) { + continue; + } + + $bill = $bills->random(1)->first(); + + $this->dispatch( + new CreateDocumentTransaction( + $bill, + factory(Transaction::class)->state('expense')->raw( + [ + 'contact_id' => $bill->contact_id, + 'document_id' => $bill->id, + 'amount' => $amount > $bill->amount ? $bill->amount : $amount, + ] + ) + ) + ); + } + + Model::unguard(); + } +} From 1fd2774d77dca271621df4456f4aa61eb3f012d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Wed, 22 Jan 2020 00:37:05 +0300 Subject: [PATCH 2/4] Use factories directly --- database/seeds/SampleData.php | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/database/seeds/SampleData.php b/database/seeds/SampleData.php index 8183449b1..359cde306 100755 --- a/database/seeds/SampleData.php +++ b/database/seeds/SampleData.php @@ -36,25 +36,11 @@ class SampleData extends Seeder $count = $this->command->option('count'); - for ($i = 0; $i < $count; $i++) { - $this->dispatch(new CreateContact(factory(Contact::class)->raw())); - } - - for ($i = 0; $i < $count; $i++) { - $this->dispatch(new CreateItem(factory(Item::class)->raw())); - } - - for ($i = 0; $i < $count; $i++) { - $this->dispatch(new CreateAccount(factory(Account::class)->raw())); - } - - for ($i = 0; $i < $count; $i++) { - $this->dispatch(new CreateBill(factory(Bill::class)->state('items')->raw())); - } - - for ($i = 0; $i < $count; $i++) { - $this->dispatch(new CreateInvoice(factory(Invoice::class)->state('items')->raw())); - } + factory(Contact::class, (int)$count)->create(); + factory(Item::class, (int)$count)->create(); + factory(Account::class, (int)$count)->create(); + factory(Bill::class, (int)$count)->create(); + factory(Invoice::class, (int)$count)->create(); for ($i = 0; $i < $count; $i++) { $amount = $faker->randomFloat(2, 1, 1000); From 3c224c0f12933c6406e64e906235c8559caa2d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Wed, 22 Jan 2020 16:45:39 +0300 Subject: [PATCH 3/4] refs https://github.com/akaunting/akaunting/pull/1158#discussion_r369403225 --- database/seeds/SampleData.php | 57 ----------------------------------- 1 file changed, 57 deletions(-) diff --git a/database/seeds/SampleData.php b/database/seeds/SampleData.php index 359cde306..5cedc18d8 100755 --- a/database/seeds/SampleData.php +++ b/database/seeds/SampleData.php @@ -3,14 +3,7 @@ namespace Database\Seeds; use App\Abstracts\Model; -use App\Jobs\Banking\CreateAccount; -use App\Jobs\Banking\CreateDocumentTransaction; -use App\Jobs\Common\CreateContact; -use App\Jobs\Common\CreateItem; -use App\Jobs\Purchase\CreateBill; -use App\Jobs\Sale\CreateInvoice; use App\Models\Banking\Account; -use App\Models\Banking\Transaction; use App\Models\Common\Contact; use App\Models\Common\Item; use App\Models\Purchase\Bill; @@ -32,8 +25,6 @@ class SampleData extends Seeder { Model::reguard(); - $faker = Factory::create(); - $count = $this->command->option('count'); factory(Contact::class, (int)$count)->create(); @@ -42,54 +33,6 @@ class SampleData extends Seeder factory(Bill::class, (int)$count)->create(); factory(Invoice::class, (int)$count)->create(); - for ($i = 0; $i < $count; $i++) { - $amount = $faker->randomFloat(2, 1, 1000); - $invoices = Invoice::where('status', 'sent')->get(); - - if (0 === $invoices->count()) { - continue; - } - - $invoice = $invoices->random(1)->first(); - - $this->dispatch( - new CreateDocumentTransaction( - $invoice, - factory(Transaction::class)->state('income')->raw( - [ - 'contact_id' => $invoice->contact_id, - 'document_id' => $invoice->id, - 'amount' => $amount > $invoice->amount ? $invoice->amount : $amount, - ] - ) - ) - ); - } - - for ($i = 0; $i < $count; $i++) { - $amount = $faker->randomFloat(2, 1, 1000); - $bills = Bill::where('status', 'received')->get(); - - if (0 === $bills->count()) { - continue; - } - - $bill = $bills->random(1)->first(); - - $this->dispatch( - new CreateDocumentTransaction( - $bill, - factory(Transaction::class)->state('expense')->raw( - [ - 'contact_id' => $bill->contact_id, - 'document_id' => $bill->id, - 'amount' => $amount > $bill->amount ? $bill->amount : $amount, - ] - ) - ) - ); - } - Model::unguard(); } } From 1cba4c56afca0538d63f61fa3d0ffb7325f4fb1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Wed, 22 Jan 2020 16:54:34 +0300 Subject: [PATCH 4/4] Remove unneccesary class and trait --- database/seeds/SampleData.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/database/seeds/SampleData.php b/database/seeds/SampleData.php index 5cedc18d8..b05d5f994 100755 --- a/database/seeds/SampleData.php +++ b/database/seeds/SampleData.php @@ -8,14 +8,10 @@ use App\Models\Common\Contact; use App\Models\Common\Item; use App\Models\Purchase\Bill; use App\Models\Sale\Invoice; -use App\Traits\Jobs; -use Faker\Factory; use Illuminate\Database\Seeder; class SampleData extends Seeder { - use Jobs; - /** * Run the database seeds. *