From 80f0aee1fc2ecd83e0494aa9b462100e6120e94f Mon Sep 17 00:00:00 2001 From: denisdulici Date: Thu, 23 Jan 2020 17:54:39 +0300 Subject: [PATCH] added viewed status to invoice factory --- app/Http/Controllers/Portal/Invoices.php | 1 - app/Listeners/Sale/MarkInvoiceSent.php | 15 +++----- app/Listeners/Sale/MarkInvoiceViewed.php | 6 ++++ database/factories/Invoice.php | 46 +++++++++++++++--------- resources/lang/en-GB/invoices.php | 1 + 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Portal/Invoices.php b/app/Http/Controllers/Portal/Invoices.php index cfd702668..e05f38eba 100644 --- a/app/Http/Controllers/Portal/Invoices.php +++ b/app/Http/Controllers/Portal/Invoices.php @@ -43,7 +43,6 @@ class Invoices extends Controller */ public function show(Invoice $invoice) { - $payment_methods = Modules::getPaymentMethods(); event(new \App\Events\Sale\InvoiceViewed($invoice)); diff --git a/app/Listeners/Sale/MarkInvoiceSent.php b/app/Listeners/Sale/MarkInvoiceSent.php index a93a79e8b..7445e43ea 100644 --- a/app/Listeners/Sale/MarkInvoiceSent.php +++ b/app/Listeners/Sale/MarkInvoiceSent.php @@ -3,10 +3,13 @@ namespace App\Listeners\Sale; use App\Events\Sale\InvoiceSent as Event; -use App\Models\Sale\InvoiceHistory; +use App\Jobs\Sale\CreateInvoiceHistory; +use App\Traits\Jobs; class MarkInvoiceSent { + use Jobs; + /** * Handle the event. * @@ -15,20 +18,12 @@ class MarkInvoiceSent */ public function handle(Event $event) { - // Mark invoice as sent if ($event->invoice->status != 'partial') { $event->invoice->status = 'sent'; $event->invoice->save(); } - // Add invoice history - InvoiceHistory::create([ - 'company_id' => $event->invoice->company_id, - 'invoice_id' => $event->invoice->id, - 'status' => 'sent', - 'notify' => 0, - 'description' => trans('invoices.mark_sent'), - ]); + $this->dispatch(new CreateInvoiceHistory($event->invoice, 0, trans('invoices.mark_sent'))); } } diff --git a/app/Listeners/Sale/MarkInvoiceViewed.php b/app/Listeners/Sale/MarkInvoiceViewed.php index e7d57ad95..3dbf79ae7 100644 --- a/app/Listeners/Sale/MarkInvoiceViewed.php +++ b/app/Listeners/Sale/MarkInvoiceViewed.php @@ -3,9 +3,13 @@ namespace App\Listeners\Sale; use App\Events\Sale\InvoiceViewed as Event; +use App\Jobs\Sale\CreateInvoiceHistory; +use App\Traits\Jobs; class MarkInvoiceViewed { + use Jobs; + /** * Handle the event. * @@ -24,5 +28,7 @@ class MarkInvoiceViewed $invoice->status = 'viewed'; $invoice->save(); + + $this->dispatch(new CreateInvoiceHistory($event->invoice, 0, trans('invoices.mark_viewed'))); } } diff --git a/database/factories/Invoice.php b/database/factories/Invoice.php index 63723b7bc..2507cb18a 100644 --- a/database/factories/Invoice.php +++ b/database/factories/Invoice.php @@ -2,6 +2,7 @@ use App\Events\Sale\InvoiceCreated; use App\Events\Sale\InvoiceSent; +use App\Events\Sale\InvoiceViewed; use App\Events\Sale\PaymentReceived; use App\Jobs\Sale\UpdateInvoice; use App\Models\Auth\User; @@ -31,7 +32,7 @@ $factory->define(Invoice::class, function (Faker $faker) use ($company) { $contact = factory(Contact::class)->states('customer')->create(); } - $statuses = ['draft', 'sent', 'partial', 'paid']; + $statuses = ['draft', 'sent', 'viewed', 'partial', 'paid']; return [ 'company_id' => $company->id, @@ -57,6 +58,8 @@ $factory->state(Invoice::class, 'draft', ['status' => 'draft']); $factory->state(Invoice::class, 'sent', ['status' => 'sent']); +$factory->state(Invoice::class, 'viewed', ['status' => 'viewed']); + $factory->state(Invoice::class, 'partial', ['status' => 'partial']); $factory->state(Invoice::class, 'paid', ['status' => 'paid']); @@ -98,16 +101,11 @@ $factory->afterCreating(Invoice::class, function ($invoice, $faker) use ($compan session(['company_id' => $company->id]); setting()->setExtraColumns(['company_id' => $company->id]); - $status = $invoice->status; + $init_status = $invoice->status; + $invoice->status = 'draft'; - event(new InvoiceCreated($invoice)); - - $invoice->status = $status; - - if ($invoice->status == 'sent') { - event(new InvoiceSent($invoice)); - } + $invoice->status = $init_status; $amount = $faker->randomFloat(2, 1, 1000); @@ -128,15 +126,29 @@ $factory->afterCreating(Invoice::class, function ($invoice, $faker) use ($compan $updated_invoice = dispatch_now(new UpdateInvoice($invoice, $request)); - if (in_array($invoice->status, ['partial', 'paid'])) { - $payment_request = [ - 'paid_at' => $invoice->due_at, - ]; + switch ($init_status) { + case 'sent': + event(new InvoiceSent($invoice)); - if ($invoice->status == 'partial') { - $payment_request['amount'] = (double) $amount / 2; - } + break; + case 'viewed': + $invoice->status = 'sent'; + event(new InvoiceViewed($invoice)); + $invoice->status = $init_status; - event(new PaymentReceived($updated_invoice, $payment_request)); + break; + case 'partial': + case 'paid': + $payment_request = [ + 'paid_at' => $invoice->due_at, + ]; + + if ($init_status == 'partial') { + $payment_request['amount'] = (double) $amount / 3; + } + + event(new PaymentReceived($updated_invoice, $payment_request)); + + break; } }); diff --git a/resources/lang/en-GB/invoices.php b/resources/lang/en-GB/invoices.php index 7925ed2f4..10e56bca3 100644 --- a/resources/lang/en-GB/invoices.php +++ b/resources/lang/en-GB/invoices.php @@ -29,6 +29,7 @@ return [ 'add_payment' => 'Add Payment', 'mark_paid' => 'Mark Paid', 'mark_sent' => 'Mark Sent', + 'mark_viewed' => 'Mark Viewed', 'download_pdf' => 'Download PDF', 'send_mail' => 'Send Email', 'all_invoices' => 'Login to view all invoices',