322 lines
8.0 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace App\Http\Controllers\Sales;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\Controller;
use App\Exports\Sales\Invoices as Export;
2019-11-16 10:21:14 +03:00
use App\Http\Requests\Common\Import as ImportRequest;
2020-12-24 01:28:38 +03:00
use App\Http\Requests\Document\Document as Request;
use App\Imports\Sales\Invoices as Import;
2020-12-24 01:28:38 +03:00
use App\Jobs\Document\CreateDocument;
use App\Jobs\Document\DeleteDocument;
use App\Jobs\Document\DuplicateDocument;
use App\Jobs\Document\SendDocument;
2020-12-24 01:28:38 +03:00
use App\Jobs\Document\UpdateDocument;
use App\Models\Document\Document;
2019-12-31 15:49:09 +03:00
use App\Notifications\Sale\Invoice as Notification;
2020-12-24 01:28:38 +03:00
use App\Traits\Documents;
2017-09-14 22:21:00 +03:00
class Invoices extends Controller
{
2020-12-24 01:28:38 +03:00
use Documents;
/**
* @var string
*/
public $type = Document::INVOICE_TYPE;
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2023-04-13 11:35:31 +03:00
$invoices = Document::invoice()->with('contact', 'items', 'last_history', 'transactions')->collect(['document_number'=> 'desc']);
2017-09-14 22:21:00 +03:00
return $this->response('sales.invoices.index', compact('invoices'));
2017-09-14 22:21:00 +03:00
}
/**
* Show the form for viewing the specified resource.
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function show(Document $invoice)
2017-09-14 22:21:00 +03:00
{
2020-12-24 01:28:38 +03:00
return view('sales.invoices.show', compact('invoice'));
2017-09-14 22:21:00 +03:00
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
2020-12-24 01:28:38 +03:00
return view('sales.invoices.create');
2017-09-14 22:21:00 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return Response
*/
public function store(Request $request)
{
2020-12-24 01:28:38 +03:00
$response = $this->ajaxDispatch(new CreateDocument($request));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
if ($response['success']) {
$paramaters = ['invoice' => $response['data']->id];
if ($request->has('senddocument')) {
$paramaters['senddocument'] = true;
}
$response['redirect'] = route('invoices.show', $paramaters);
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$message = trans('messages.success.added', ['type' => trans_choice('general.invoices', 1)]);
flash($message)->success();
} else {
$response['redirect'] = route('invoices.create');
$message = $response['message'];
flash($message)->error()->important();
2019-11-16 10:21:14 +03:00
}
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return response()->json($response);
2017-09-14 22:21:00 +03:00
}
2017-11-26 15:20:17 +03:00
/**
* Duplicate the specified resource.
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
2017-11-26 15:20:17 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function duplicate(Document $invoice)
2017-11-26 15:20:17 +03:00
{
2020-12-24 01:28:38 +03:00
$clone = $this->dispatch(new DuplicateDocument($invoice));
2017-11-26 15:20:17 +03:00
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.invoices', 1)]);
flash($message)->success();
2019-11-16 10:21:14 +03:00
return redirect()->route('invoices.edit', $clone->id);
2017-11-26 15:20:17 +03:00
}
2017-11-30 11:47:56 +03:00
/**
* Import the specified resource.
*
2019-11-16 10:21:14 +03:00
* @param ImportRequest $request
2017-11-30 11:47:56 +03:00
*
* @return Response
*/
2019-11-16 10:21:14 +03:00
public function import(ImportRequest $request)
2017-11-30 11:47:56 +03:00
{
2021-04-16 00:59:43 +03:00
$response = $this->importExcel(new Import, $request, trans_choice('general.invoices', 2));
2018-06-23 15:59:13 +03:00
2021-02-11 18:08:06 +03:00
if ($response['success']) {
$response['redirect'] = route('invoices.index');
2017-11-30 11:47:56 +03:00
2021-04-16 00:59:43 +03:00
flash($response['message'])->success();
2021-02-11 18:08:06 +03:00
} else {
$response['redirect'] = route('import.create', ['sales', 'invoices']);
2017-11-30 11:47:56 +03:00
2021-04-16 00:59:43 +03:00
flash($response['message'])->error()->important();
2021-02-11 18:08:06 +03:00
}
return response()->json($response);
2017-11-30 11:47:56 +03:00
}
2017-09-14 22:21:00 +03:00
/**
* Show the form for editing the specified resource.
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function edit(Document $invoice)
2017-09-14 22:21:00 +03:00
{
2020-12-24 01:28:38 +03:00
return view('sales.invoices.edit', compact('invoice'));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
* @param Request $request
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function update(Document $invoice, Request $request)
2017-09-14 22:21:00 +03:00
{
2020-12-24 01:28:38 +03:00
$response = $this->ajaxDispatch(new UpdateDocument($invoice, $request));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
if ($response['success']) {
$response['redirect'] = route('invoices.show', $response['data']->id);
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$message = trans('messages.success.updated', ['type' => trans_choice('general.invoices', 1)]);
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
flash($message)->success();
} else {
$response['redirect'] = route('invoices.edit', $invoice->id);
$message = $response['message'];
flash($message)->error()->important();
2019-11-16 10:21:14 +03:00
}
return response()->json($response);
2017-09-14 22:21:00 +03:00
}
/**
* Remove the specified resource from storage.
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function destroy(Document $invoice)
2017-09-14 22:21:00 +03:00
{
2020-12-24 01:28:38 +03:00
$response = $this->ajaxDispatch(new DeleteDocument($invoice));
2019-02-27 15:00:16 +03:00
2019-11-16 10:21:14 +03:00
$response['redirect'] = route('invoices.index');
2019-02-27 15:00:16 +03:00
2019-11-16 10:21:14 +03:00
if ($response['success']) {
$message = trans('messages.success.deleted', ['type' => trans_choice('general.invoices', 1)]);
2019-02-27 15:00:16 +03:00
2019-11-16 10:21:14 +03:00
flash($message)->success();
} else {
$message = $response['message'];
2017-09-14 22:21:00 +03:00
flash($message)->error()->important();
2019-11-16 10:21:14 +03:00
}
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return response()->json($response);
2017-09-14 22:21:00 +03:00
}
2018-06-11 03:47:32 +03:00
/**
* Export the specified resource.
*
* @return Response
*/
public function export()
{
return $this->exportExcel(new Export, trans_choice('general.invoices', 2));
2018-06-11 03:47:32 +03:00
}
2017-11-07 05:11:03 +03:00
/**
* Mark the invoice as sent.
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function markSent(Document $invoice)
2017-11-07 05:11:03 +03:00
{
2022-06-14 01:18:34 +03:00
event(new \App\Events\Document\DocumentMarkedSent($invoice));
$message = trans('documents.messages.marked_sent', ['type' => trans_choice('general.invoices', 1)]);
2017-11-07 05:11:03 +03:00
2019-11-16 10:21:14 +03:00
flash($message)->success();
2017-11-07 05:11:03 +03:00
return redirect()->back();
}
2020-03-28 17:54:36 +03:00
/**
* Mark the invoice as cancelled.
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
2020-03-28 17:54:36 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function markCancelled(Document $invoice)
2020-03-28 17:54:36 +03:00
{
2020-12-24 01:28:38 +03:00
event(new \App\Events\Document\DocumentCancelled($invoice));
2020-03-28 17:54:36 +03:00
$message = trans('documents.messages.marked_cancelled', ['type' => trans_choice('general.invoices', 1)]);
2020-03-28 17:54:36 +03:00
flash($message)->success();
return redirect()->back();
}
2017-11-07 05:11:03 +03:00
/**
2017-11-16 20:27:30 +03:00
* Download the PDF file of invoice.
2017-11-07 05:11:03 +03:00
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function emailInvoice(Document $invoice)
2017-11-07 05:11:03 +03:00
{
2019-11-16 10:21:14 +03:00
if (empty($invoice->contact_email)) {
return redirect()->back();
}
$response = $this->ajaxDispatch(new SendDocument($invoice));
2017-11-16 20:27:30 +03:00
if ($response['success']) {
$message = trans('documents.messages.email_sent', ['type' => trans_choice('general.invoices', 1)]);
2017-11-16 20:27:30 +03:00
flash($message)->success();
} else {
$message = $response['message'];
flash($message)->error()->important();
}
2017-11-16 20:27:30 +03:00
return redirect()->back();
}
/**
* Print the invoice.
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
2017-11-16 20:27:30 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function printInvoice(Document $invoice)
2017-11-16 20:27:30 +03:00
{
2021-04-29 18:12:37 +03:00
event(new \App\Events\Document\DocumentPrinting($invoice));
2017-11-07 05:11:03 +03:00
2019-12-13 11:04:17 +03:00
$view = view($invoice->template_path, compact('invoice'));
return mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
2017-11-07 05:11:03 +03:00
}
/**
2017-11-15 21:53:40 +03:00
* Download the PDF file of invoice.
2017-11-07 05:11:03 +03:00
*
2020-12-24 01:28:38 +03:00
* @param Document $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2020-12-24 01:28:38 +03:00
public function pdfInvoice(Document $invoice)
2022-06-01 10:15:55 +03:00
{
2021-04-29 18:12:37 +03:00
event(new \App\Events\Document\DocumentPrinting($invoice));
2017-11-07 05:11:03 +03:00
$currency_style = true;
$view = view($invoice->template_path, compact('invoice', 'currency_style'))->render();
2022-06-01 10:15:55 +03:00
2021-09-06 13:35:41 +03:00
$html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
2017-11-15 21:53:40 +03:00
$pdf = app('dompdf.wrapper');
$pdf->loadHTML($html);
2017-11-15 21:53:40 +03:00
2021-09-07 20:57:25 +03:00
//$pdf->setPaper('A4', 'portrait');
$file_name = $this->getDocumentFileName($invoice);
return $pdf->download($file_name);
2017-11-07 05:11:03 +03:00
}
2017-09-14 22:21:00 +03:00
}