added viewed status to invoice factory

This commit is contained in:
denisdulici
2020-01-23 17:54:39 +03:00
parent 967849bda7
commit 80f0aee1fc
5 changed files with 41 additions and 28 deletions

View File

@ -43,7 +43,6 @@ class Invoices extends Controller
*/ */
public function show(Invoice $invoice) public function show(Invoice $invoice)
{ {
$payment_methods = Modules::getPaymentMethods(); $payment_methods = Modules::getPaymentMethods();
event(new \App\Events\Sale\InvoiceViewed($invoice)); event(new \App\Events\Sale\InvoiceViewed($invoice));

View File

@ -3,10 +3,13 @@
namespace App\Listeners\Sale; namespace App\Listeners\Sale;
use App\Events\Sale\InvoiceSent as Event; use App\Events\Sale\InvoiceSent as Event;
use App\Models\Sale\InvoiceHistory; use App\Jobs\Sale\CreateInvoiceHistory;
use App\Traits\Jobs;
class MarkInvoiceSent class MarkInvoiceSent
{ {
use Jobs;
/** /**
* Handle the event. * Handle the event.
* *
@ -15,20 +18,12 @@ class MarkInvoiceSent
*/ */
public function handle(Event $event) public function handle(Event $event)
{ {
// Mark invoice as sent
if ($event->invoice->status != 'partial') { if ($event->invoice->status != 'partial') {
$event->invoice->status = 'sent'; $event->invoice->status = 'sent';
$event->invoice->save(); $event->invoice->save();
} }
// Add invoice history $this->dispatch(new CreateInvoiceHistory($event->invoice, 0, trans('invoices.mark_sent')));
InvoiceHistory::create([
'company_id' => $event->invoice->company_id,
'invoice_id' => $event->invoice->id,
'status' => 'sent',
'notify' => 0,
'description' => trans('invoices.mark_sent'),
]);
} }
} }

View File

@ -3,9 +3,13 @@
namespace App\Listeners\Sale; namespace App\Listeners\Sale;
use App\Events\Sale\InvoiceViewed as Event; use App\Events\Sale\InvoiceViewed as Event;
use App\Jobs\Sale\CreateInvoiceHistory;
use App\Traits\Jobs;
class MarkInvoiceViewed class MarkInvoiceViewed
{ {
use Jobs;
/** /**
* Handle the event. * Handle the event.
* *
@ -24,5 +28,7 @@ class MarkInvoiceViewed
$invoice->status = 'viewed'; $invoice->status = 'viewed';
$invoice->save(); $invoice->save();
$this->dispatch(new CreateInvoiceHistory($event->invoice, 0, trans('invoices.mark_viewed')));
} }
} }

View File

@ -2,6 +2,7 @@
use App\Events\Sale\InvoiceCreated; use App\Events\Sale\InvoiceCreated;
use App\Events\Sale\InvoiceSent; use App\Events\Sale\InvoiceSent;
use App\Events\Sale\InvoiceViewed;
use App\Events\Sale\PaymentReceived; use App\Events\Sale\PaymentReceived;
use App\Jobs\Sale\UpdateInvoice; use App\Jobs\Sale\UpdateInvoice;
use App\Models\Auth\User; 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(); $contact = factory(Contact::class)->states('customer')->create();
} }
$statuses = ['draft', 'sent', 'partial', 'paid']; $statuses = ['draft', 'sent', 'viewed', 'partial', 'paid'];
return [ return [
'company_id' => $company->id, '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, 'sent', ['status' => 'sent']);
$factory->state(Invoice::class, 'viewed', ['status' => 'viewed']);
$factory->state(Invoice::class, 'partial', ['status' => 'partial']); $factory->state(Invoice::class, 'partial', ['status' => 'partial']);
$factory->state(Invoice::class, 'paid', ['status' => 'paid']); $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]); session(['company_id' => $company->id]);
setting()->setExtraColumns(['company_id' => $company->id]); setting()->setExtraColumns(['company_id' => $company->id]);
$status = $invoice->status; $init_status = $invoice->status;
$invoice->status = 'draft'; $invoice->status = 'draft';
event(new InvoiceCreated($invoice)); event(new InvoiceCreated($invoice));
$invoice->status = $init_status;
$invoice->status = $status;
if ($invoice->status == 'sent') {
event(new InvoiceSent($invoice));
}
$amount = $faker->randomFloat(2, 1, 1000); $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)); $updated_invoice = dispatch_now(new UpdateInvoice($invoice, $request));
if (in_array($invoice->status, ['partial', 'paid'])) { switch ($init_status) {
$payment_request = [ case 'sent':
'paid_at' => $invoice->due_at, event(new InvoiceSent($invoice));
];
if ($invoice->status == 'partial') { break;
$payment_request['amount'] = (double) $amount / 2; 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;
} }
}); });

View File

@ -29,6 +29,7 @@ return [
'add_payment' => 'Add Payment', 'add_payment' => 'Add Payment',
'mark_paid' => 'Mark Paid', 'mark_paid' => 'Mark Paid',
'mark_sent' => 'Mark Sent', 'mark_sent' => 'Mark Sent',
'mark_viewed' => 'Mark Viewed',
'download_pdf' => 'Download PDF', 'download_pdf' => 'Download PDF',
'send_mail' => 'Send Email', 'send_mail' => 'Send Email',
'all_invoices' => 'Login to view all invoices', 'all_invoices' => 'Login to view all invoices',