From 22ee37622e2f4f04424407436d95498f3e45d16c Mon Sep 17 00:00:00 2001 From: denisdulici Date: Mon, 13 Jan 2020 15:16:35 +0300 Subject: [PATCH] invoice factory --- database/factories/Invoice.php | 125 +++++++++++++++++++++++++++ tests/Feature/Sales/InvoicesTest.php | 49 +++-------- 2 files changed, 135 insertions(+), 39 deletions(-) create mode 100644 database/factories/Invoice.php diff --git a/database/factories/Invoice.php b/database/factories/Invoice.php new file mode 100644 index 000000000..57e70e48f --- /dev/null +++ b/database/factories/Invoice.php @@ -0,0 +1,125 @@ +companies()->first(); + +$factory->define(Invoice::class, function (Faker $faker) use ($company) { + session(['company_id' => $company->id]); + setting()->setExtraColumns(['company_id' => $company->id]); + + $invoiced_at = $faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d'); + $due_at = Date::parse($invoiced_at)->addDays(setting('invoice.payment_terms'))->format('Y-m-d'); + + $contacts = Contact::type('customer')->enabled()->get(); + + if ($contacts->count()) { + $contact = $contacts->random(1)->first(); + } else { + $contact = factory(Contact::class)->states('customer')->create(); + } + + $statuses = ['draft', 'sent', 'paid']; + + return [ + 'company_id' => $company->id, + 'invoiced_at' => $invoiced_at, + 'due_at' => $due_at, + 'invoice_number' => setting('invoice.number_prefix') . $faker->randomNumber(setting('invoice.number_digit')), + 'currency_code' => setting('default.currency', 'USD'), + 'currency_rate' => '1', + 'notes' => $faker->text(5), + 'category_id' => $company->categories()->type('income')->get()->random(1)->pluck('id')->first(), + 'contact_id' => $contact->id, + 'contact_name' => $contact->name, + 'contact_email' =>$contact->email, + 'contact_tax_number' => $contact->tax_number, + 'contact_phone' => $contact->phone, + 'contact_address' => $contact->address, + 'status' => $faker->randomElement($statuses), + 'amount' => '0', + ]; +}); + +$factory->state(Invoice::class, 'draft', ['status' => 'draft']); + +$factory->state(Invoice::class, 'sent', ['status' => 'sent']); + +$factory->state(Invoice::class, 'paid', ['status' => 'paid']); + +$factory->state(Invoice::class, 'recurring', function (Faker $faker) { + $frequencies = ['monthly', 'weekly']; + + return [ + 'recurring_frequency' => 'yes', + 'recurring_interval' => '1', + 'recurring_custom_frequency' => $faker->randomElement($frequencies), + 'recurring_count' => '1', + ]; +}); + +$factory->state(Invoice::class, 'items', function (Faker $faker) use ($company) { + session(['company_id' => $company->id]); + setting()->setExtraColumns(['company_id' => $company->id]); + + $amount = $faker->randomFloat(2, 10, 1000); + + $items = Item::enabled()->get(); + + if ($items->count()) { + $item = $items->random(1)->first(); + } else { + $item = factory(Item::class)->create(); + } + + $items = [['name' => $item->name, 'item_id' => $item->id, 'quantity' => '1', 'price' => $amount, 'currency' => 'USD']]; + + return [ + 'items' => $items, + 'recurring_frequency' => 'no', + ]; +}); + +$factory->afterCreating(Invoice::class, function ($invoice, $faker) use ($company) { + session(['company_id' => $company->id]); + setting()->setExtraColumns(['company_id' => $company->id]); + + event(new InvoiceCreated($invoice)); + + if ($invoice->status == 'sent') { + event(new InvoiceSent($invoice)); + } + + $amount = $faker->randomFloat(2, 10, 1000); + + $items = Item::enabled()->get(); + + if ($items->count()) { + $item = $items->random(1)->first(); + } else { + $item = factory(Item::class)->create(); + } + + $items = [['name' => $item->name, 'item_id' => $item->id, 'quantity' => '1', 'price' => $amount, 'currency' => 'USD']]; + + $request = [ + 'items' => $items, + 'recurring_frequency' => 'no', + ]; + + $updated_invoice = dispatch_now(new UpdateInvoice($invoice, $request)); + + if ($invoice->status == 'paid') { + event(new PaymentReceived($updated_invoice, [])); + } +}); diff --git a/tests/Feature/Sales/InvoicesTest.php b/tests/Feature/Sales/InvoicesTest.php index 7a0efdf48..34baf57e1 100644 --- a/tests/Feature/Sales/InvoicesTest.php +++ b/tests/Feature/Sales/InvoicesTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Sales; use App\Jobs\Sale\CreateInvoice; +use App\Models\Sale\Invoice; use Tests\Feature\FeatureTestCase; class InvoicesTest extends FeatureTestCase @@ -26,7 +27,7 @@ class InvoicesTest extends FeatureTestCase public function testItShouldCreateInvoice() { $this->loginAs() - ->post(route('invoices.store'), $this->getInvoiceRequest()) + ->post(route('invoices.store'), $this->getRequest()) ->assertStatus(200); $this->assertFlashLevel('success'); @@ -35,7 +36,7 @@ class InvoicesTest extends FeatureTestCase public function testItShouldCreateInvoiceWithRecurring() { $this->loginAs() - ->post(route('invoices.store'), $this->getInvoiceRequest(true)) + ->post(route('invoices.store'), $this->getRequest(true)) ->assertStatus(200); $this->assertFlashLevel('success'); @@ -43,7 +44,7 @@ class InvoicesTest extends FeatureTestCase public function testItShouldSeeInvoiceUpdatePage() { - $invoice = $this->dispatch(new CreateInvoice($this->getInvoiceRequest())); + $invoice = $this->dispatch(new CreateInvoice($this->getRequest())); $this->loginAs() ->get(route('invoices.edit', $invoice->id)) @@ -53,7 +54,7 @@ class InvoicesTest extends FeatureTestCase public function testItShouldUpdateInvoice() { - $request = $this->getInvoiceRequest(); + $request = $this->getRequest(); $invoice = $this->dispatch(new CreateInvoice($request)); @@ -68,7 +69,7 @@ class InvoicesTest extends FeatureTestCase public function testItShouldDeleteInvoice() { - $invoice = $this->dispatch(new CreateInvoice($this->getInvoiceRequest())); + $invoice = $this->dispatch(new CreateInvoice($this->getRequest())); $this->loginAs() ->delete(route('invoices.destroy', $invoice->id)) @@ -77,42 +78,12 @@ class InvoicesTest extends FeatureTestCase $this->assertFlashLevel('success'); } - private function getInvoiceRequest($recurring = false) + public function getRequest($recurring = false) { - $amount = $this->faker->randomFloat(2, 2); + $factory = factory(Invoice::class); - $items = [['name' => $this->faker->text(5), 'item_id' => null, 'quantity' => '1', 'price' => $amount, 'currency' => 'USD']]; + $recurring ? $factory->states('items', 'recurring') : $factory->states('items'); - $data = [ - 'company_id' => $this->company->id, - 'invoiced_at' => $this->faker->date(), - 'due_at' => $this->faker->date(), - 'invoice_number' => '1', - 'order_number' => '1', - 'currency_code' => setting('default.currency', 'USD'), - 'currency_rate' => '1', - 'items' => $items, - 'discount' => '0', - 'notes' => $this->faker->text(5), - 'category_id' => $this->company->categories()->type('income')->pluck('id')->first(), - 'recurring_frequency' => 'no', - 'contact_id' => '0', - 'contact_name' => $this->faker->name, - 'contact_email' =>$this->faker->email, - 'contact_tax_number' => null, - 'contact_phone' => null, - 'contact_address' => $this->faker->address, - 'status' => 'draft', - 'amount' => $amount, - ]; - - if ($recurring) { - $data['recurring_frequency'] = 'yes'; - $data['recurring_interval'] = '1'; - $data['recurring_custom_frequency'] = $this->faker->randomElement(['monthly', 'weekly']); - $data['recurring_count'] = '1'; - } - - return $data; + return $factory->raw(); } }