cancel invoice/bill

This commit is contained in:
denisdulici
2020-03-28 17:54:36 +03:00
parent 889be5fd98
commit 75413a0496
30 changed files with 445 additions and 104 deletions

View File

@ -22,7 +22,7 @@ abstract class DocumentModel extends Model
public function scopeAccrued($query)
{
return $query->where('status', '<>', 'draft');
return $query->whereNotIn('status', ['draft', 'cancelled']);
}
public function scopePaid($query)
@ -146,6 +146,9 @@ abstract class DocumentModel extends Model
case 'viewed':
$label = 'warning';
break;
case 'cancelled':
$label = 'darker';
break;
default:
$label = 'primary';
break;

View File

@ -3,6 +3,8 @@
namespace App\BulkActions\Purchases;
use App\Abstracts\BulkAction;
use App\Events\Purchase\BillCancelled;
use App\Events\Purchase\BillReceived;
use App\Exports\Purchases\Bills as Export;
use App\Jobs\Purchase\CreateBillHistory;
use App\Jobs\Purchase\DeleteBill;
@ -18,6 +20,11 @@ class Bills extends BulkAction
'message' => 'bulk_actions.message.received',
'permission' => 'update-purchases-bills',
],
'cancelled' => [
'name' => 'general.cancel',
'message' => 'bulk_actions.message.cancelled',
'permission' => 'update-purchases-bills',
],
'delete' => [
'name' => 'general.delete',
'message' => 'bulk_actions.message.delete',
@ -34,12 +41,16 @@ class Bills extends BulkAction
$bills = $this->getSelectedRecords($request);
foreach ($bills as $bill) {
$bill->status = 'received';
$bill->save();
event(new BillReceived($bill));
}
}
$description = trans('bills.mark_recevied');
public function cancelled($request)
{
$bills = $this->getSelectedRecords($request);
$this->dispatch(new CreateBillHistory($bill, 0, $description));
foreach ($bills as $bill) {
event(new BillCancelled($bill));
}
}

View File

@ -3,6 +3,7 @@
namespace App\BulkActions\Sales;
use App\Abstracts\BulkAction;
use App\Events\Sale\InvoiceCancelled;
use App\Events\Sale\InvoiceCreated;
use App\Events\Sale\InvoiceSent;
use App\Events\Sale\PaymentReceived;
@ -25,6 +26,11 @@ class Invoices extends BulkAction
'message' => 'bulk_actions.message.sent',
'permission' => 'update-sales-invoices',
],
'cancelled' => [
'name' => 'general.cancel',
'message' => 'bulk_actions.message.cancelled',
'permission' => 'update-sales-invoices',
],
'delete' => [
'name' => 'general.delete',
'message' => 'bulk_actions.message.delete',
@ -54,6 +60,15 @@ class Invoices extends BulkAction
}
}
public function cancelled($request)
{
$invoices = $this->getSelectedRecords($request);
foreach ($invoices as $invoice) {
event(new InvoiceCancelled($invoice));
}
}
public function duplicate($request)
{
$invoices = $this->getSelectedRecords($request);

View File

@ -0,0 +1,22 @@
<?php
namespace App\Events\Purchase;
use Illuminate\Queue\SerializesModels;
class BillCancelled
{
use SerializesModels;
public $bill;
/**
* Create a new event instance.
*
* @param $bill
*/
public function __construct($bill)
{
$this->bill = $bill;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Events\Sale;
use Illuminate\Queue\SerializesModels;
class InvoiceCancelled
{
use SerializesModels;
public $invoice;
/**
* Create a new event instance.
*
* @param $invoice
*/
public function __construct($invoice)
{
$this->invoice = $invoice;
}
}

View File

@ -284,7 +284,25 @@ class Bills extends Controller
{
event(new \App\Events\Purchase\BillReceived($bill));
$message = trans('bills.messages.received');
$message = trans('bills.messages.marked_received');
flash($message)->success();
return redirect()->back();
}
/**
* Mark the bill as cancelled.
*
* @param Bill $bill
*
* @return Response
*/
public function markCancelled(Bill $bill)
{
event(new \App\Events\Purchase\BillCancelled($bill));
$message = trans('bills.messages.marked_cancelled');
flash($message)->success();

View File

@ -294,6 +294,24 @@ class Invoices extends Controller
return redirect()->back();
}
/**
* Mark the invoice as cancelled.
*
* @param Invoice $invoice
*
* @return Response
*/
public function markCancelled(Invoice $invoice)
{
event(new \App\Events\Sale\InvoiceCancelled($invoice));
$message = trans('invoices.messages.marked_cancelled');
flash($message)->success();
return redirect()->back();
}
/**
* Download the PDF file of invoice.
*

View File

@ -0,0 +1,38 @@
<?php
namespace App\Jobs\Purchase;
use App\Abstracts\Job;
use App\Models\Purchase\Bill;
class CancelBill extends Job
{
protected $bill;
/**
* Create a new job instance.
*
* @param $bill
*/
public function __construct($bill)
{
$this->bill = $bill;
}
/**
* Execute the job.
*
* @return Bill
*/
public function handle()
{
$this->deleteRelationships($this->bill, [
'transactions', 'recurring'
]);
$this->bill->status = 'cancelled';
$this->bill->save();
return true;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Jobs\Sale;
use App\Abstracts\Job;
use App\Models\Sale\Invoice;
class CancelInvoice extends Job
{
protected $invoice;
/**
* Create a new job instance.
*
* @param $invoice
*/
public function __construct($invoice)
{
$this->invoice = $invoice;
}
/**
* Execute the job.
*
* @return Invoice
*/
public function handle()
{
$this->deleteRelationships($this->invoice, [
'transactions', 'recurring'
]);
$this->invoice->status = 'cancelled';
$this->invoice->save();
return true;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Listeners\Purchase;
use App\Events\Purchase\BillCancelled as Event;
use App\Jobs\Purchase\CancelBill;
use App\Jobs\Purchase\CreateBillHistory;
use App\Traits\Jobs;
class MarkBillCancelled
{
use Jobs;
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(Event $event)
{
$this->dispatch(new CancelBill($event->bill));
$this->dispatch(new CreateBillHistory($event->bill, 0, trans('bills.messages.marked_cancelled')));
}
}

View File

@ -24,6 +24,6 @@ class MarkBillReceived
$event->bill->save();
}
$this->dispatch(new CreateBillHistory($event->bill, 0, trans('bills.mark_received')));
$this->dispatch(new CreateBillHistory($event->bill, 0, trans('bills.messages.marked_received')));
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Listeners\Sale;
use App\Events\Sale\InvoiceCancelled as Event;
use App\Jobs\Sale\CancelInvoice;
use App\Jobs\Sale\CreateInvoiceHistory;
use App\Traits\Jobs;
class MarkInvoiceCancelled
{
use Jobs;
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(Event $event)
{
$this->dispatch(new CancelInvoice($event->invoice));
$this->dispatch(new CreateInvoiceHistory($event->invoice, 0, trans('invoices.messages.marked_cancelled')));
}
}

View File

@ -24,6 +24,6 @@ class MarkInvoiceSent
$event->invoice->save();
}
$this->dispatch(new CreateInvoiceHistory($event->invoice, 0, trans('invoices.mark_sent')));
$this->dispatch(new CreateInvoiceHistory($event->invoice, 0, trans('invoices.messages.marked_sent')));
}
}

View File

@ -29,6 +29,6 @@ class MarkInvoiceViewed
$invoice->status = 'viewed';
$invoice->save();
$this->dispatch(new CreateInvoiceHistory($event->invoice, 0, trans('invoices.mark_viewed')));
$this->dispatch(new CreateInvoiceHistory($event->invoice, 0, trans('invoices.messages.marked_viewed')));
}
}

View File

@ -33,6 +33,9 @@ class Event extends Provider
'App\Events\Purchase\BillReceived' => [
'App\Listeners\Purchase\MarkBillReceived',
],
'App\Events\Purchase\BillCancelled' => [
'App\Listeners\Purchase\MarkBillCancelled',
],
'App\Events\Purchase\BillRecurring' => [
'App\Listeners\Purchase\SendBillRecurringNotification',
],
@ -47,6 +50,9 @@ class Event extends Provider
'App\Events\Sale\InvoiceSent' => [
'App\Listeners\Sale\MarkInvoiceSent',
],
'App\Events\Sale\InvoiceCancelled' => [
'App\Listeners\Sale\MarkInvoiceCancelled',
],
'App\Events\Sale\InvoiceViewed' => [
'App\Listeners\Sale\MarkInvoiceViewed',
],

View File

@ -45,6 +45,7 @@ trait Purchases
'paid',
'overdue',
'unpaid',
'cancelled',
];
$statuses = collect($list)->each(function ($code) {

View File

@ -47,6 +47,7 @@ trait Sales
'paid',
'overdue',
'unpaid',
'cancelled',
];
$statuses = collect($list)->each(function ($code) {