2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
2019-12-31 15:49:09 +03:00
|
|
|
namespace App\BulkActions\Sales;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
use App\Abstracts\BulkAction;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Events\Document\DocumentCancelled;
|
|
|
|
use App\Events\Document\DocumentCreated;
|
|
|
|
use App\Events\Document\DocumentSent;
|
|
|
|
use App\Events\Document\PaymentReceived;
|
2020-12-25 19:25:38 +03:00
|
|
|
use App\Exports\Sales\Invoices as Export;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Jobs\Document\DeleteDocument;
|
|
|
|
use App\Models\Document\Document;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
class Invoices extends BulkAction
|
|
|
|
{
|
2020-12-24 01:28:38 +03:00
|
|
|
public $model = Document::class;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
public $actions = [
|
|
|
|
'paid' => [
|
2019-12-23 12:46:00 +03:00
|
|
|
'name' => 'invoices.mark_paid',
|
|
|
|
'message' => 'bulk_actions.message.paid',
|
2019-12-31 15:49:09 +03:00
|
|
|
'permission' => 'update-sales-invoices',
|
2019-11-16 10:21:14 +03:00
|
|
|
],
|
|
|
|
'sent' => [
|
2020-01-10 13:11:32 +03:00
|
|
|
'name' => 'invoices.mark_sent',
|
2019-12-23 12:46:00 +03:00
|
|
|
'message' => 'bulk_actions.message.sent',
|
2019-12-31 15:49:09 +03:00
|
|
|
'permission' => 'update-sales-invoices',
|
2019-11-16 10:21:14 +03:00
|
|
|
],
|
2020-03-28 17:54:36 +03:00
|
|
|
'cancelled' => [
|
|
|
|
'name' => 'general.cancel',
|
|
|
|
'message' => 'bulk_actions.message.cancelled',
|
|
|
|
'permission' => 'update-sales-invoices',
|
|
|
|
],
|
2019-12-23 12:46:00 +03:00
|
|
|
'delete' => [
|
|
|
|
'name' => 'general.delete',
|
|
|
|
'message' => 'bulk_actions.message.delete',
|
2019-12-31 15:49:09 +03:00
|
|
|
'permission' => 'delete-sales-invoices',
|
2019-11-16 10:21:14 +03:00
|
|
|
],
|
|
|
|
'export' => [
|
|
|
|
'name' => 'general.export',
|
2019-12-23 12:46:00 +03:00
|
|
|
'message' => 'bulk_actions.message.export',
|
2020-09-22 00:37:00 +03:00
|
|
|
'type' => 'download',
|
2019-12-27 15:42:22 +03:00
|
|
|
],
|
2019-11-16 10:21:14 +03:00
|
|
|
];
|
|
|
|
|
2019-12-23 12:46:00 +03:00
|
|
|
public function paid($request)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2019-12-23 12:46:00 +03:00
|
|
|
$invoices = $this->getSelectedRecords($request);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
foreach ($invoices as $invoice) {
|
2021-01-19 14:59:03 +03:00
|
|
|
// Already in transactions
|
|
|
|
if ($invoice->status == 'paid') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-25 18:19:16 +03:00
|
|
|
event(new PaymentReceived($invoice, ['type' => 'income']));
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 12:46:00 +03:00
|
|
|
public function sent($request)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2019-12-23 12:46:00 +03:00
|
|
|
$invoices = $this->getSelectedRecords($request);
|
|
|
|
|
|
|
|
foreach ($invoices as $invoice) {
|
2020-12-24 01:28:38 +03:00
|
|
|
event(new DocumentSent($invoice));
|
2019-12-23 12:46:00 +03:00
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2020-03-28 17:54:36 +03:00
|
|
|
public function cancelled($request)
|
|
|
|
{
|
|
|
|
$invoices = $this->getSelectedRecords($request);
|
|
|
|
|
|
|
|
foreach ($invoices as $invoice) {
|
2020-12-24 01:28:38 +03:00
|
|
|
event(new DocumentCancelled($invoice));
|
2020-03-28 17:54:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 12:46:00 +03:00
|
|
|
public function duplicate($request)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2019-12-23 12:46:00 +03:00
|
|
|
$invoices = $this->getSelectedRecords($request);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
foreach ($invoices as $invoice) {
|
2019-12-23 12:46:00 +03:00
|
|
|
$clone = $invoice->duplicate();
|
|
|
|
|
2021-01-12 15:34:20 +03:00
|
|
|
event(new DocumentCreated($clone, $request));
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 12:46:00 +03:00
|
|
|
public function delete($request)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2019-12-23 12:46:00 +03:00
|
|
|
$this->destroy($request);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2019-12-23 12:46:00 +03:00
|
|
|
public function destroy($request)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2019-12-23 12:46:00 +03:00
|
|
|
$invoices = $this->getSelectedRecords($request);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
foreach ($invoices as $invoice) {
|
2019-12-23 12:46:00 +03:00
|
|
|
try {
|
2020-12-24 01:28:38 +03:00
|
|
|
$this->dispatch(new DeleteDocument($invoice));
|
2019-12-23 12:46:00 +03:00
|
|
|
} catch (\Exception $e) {
|
2021-02-12 19:26:38 +03:00
|
|
|
flash($e->getMessage())->error()->important();
|
2019-12-23 12:46:00 +03:00
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 12:46:00 +03:00
|
|
|
public function export($request)
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2019-12-23 12:46:00 +03:00
|
|
|
$selected = $this->getSelectedInput($request);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-12-25 19:25:38 +03:00
|
|
|
return \Excel::download(new Export($selected), \Str::filename(trans_choice('general.invoices', 2)) . '.xlsx');
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
}
|