400 lines
10 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\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\Models\Setting\Currency;
use App\Traits\Documents;
2017-11-16 20:27:30 +03:00
use File;
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()
{
2020-12-24 01:28:38 +03:00
$invoices = Document::invoice()->with('contact', '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
{
2019-11-16 10:21:14 +03:00
$currency = Currency::where('code', $invoice->currency_code)->first();
2017-09-14 22:21:00 +03:00
// Get Invoice Totals
2020-04-18 16:20:11 +03:00
foreach ($invoice->totals_sorted as $invoice_total) {
$invoice->{$invoice_total->code} = $invoice_total->amount;
}
$total = money($invoice->total, $currency->code, true)->format();
$invoice->grand_total = money($total, $currency->code)->getAmount();
2020-01-22 00:47:27 +03:00
if (!empty($invoice->paid)) {
$invoice->grand_total = round($invoice->total - $invoice->paid, $currency->precision);
}
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']) {
$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.added', ['type' => trans_choice('general.invoices', 1)]);
flash($message)->success();
} else {
$response['redirect'] = route('invoices.create');
$message = $response['message'];
flash($message)->error();
}
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
{
2020-02-29 15:31:35 +03:00
try {
\Excel::import(new Import(), $request->file('import'));
2020-02-29 15:31:35 +03:00
} catch (\Maatwebsite\Excel\Exceptions\SheetNotFoundException $e) {
flash($e->getMessage())->error()->important();
2017-11-30 11:47:56 +03:00
2019-12-31 16:03:20 +03:00
return redirect()->route('import.create', ['sales', 'invoices']);
2018-06-23 15:59:13 +03:00
}
2017-11-30 11:47:56 +03:00
$message = trans('messages.success.imported', ['type' => trans_choice('general.invoices', 2)]);
flash($message)->success();
2020-02-29 15:31:35 +03:00
return redirect()->route('invoices.index');
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();
}
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
2019-11-16 10:21:14 +03:00
flash($message)->error();
}
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 \Excel::download(new Export(), \Str::filename(trans_choice('general.invoices', 2)) . '.xlsx');
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
{
2020-12-24 01:28:38 +03:00
event(new \App\Events\Document\DocumentSent($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('general.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();
}
2017-11-16 20:27:30 +03:00
$invoice = $this->prepareInvoice($invoice);
2017-11-08 17:22:04 +03:00
$view = view($invoice->template_path, compact('invoice'))->render();
$html = mb_convert_encoding($view, 'HTML-ENTITIES');
2017-11-07 05:11:03 +03:00
2019-11-16 10:21:14 +03:00
$pdf = app('dompdf.wrapper');
2017-11-16 20:27:30 +03:00
$pdf->loadHTML($html);
2017-11-07 05:11:03 +03:00
2020-12-24 01:28:38 +03:00
$file_name = $this->getDocumentFileName($invoice);
$file = storage_path('app/temp/' . $file_name);
2017-11-07 05:11:03 +03:00
2017-11-16 20:27:30 +03:00
$invoice->pdf_path = $file;
2017-11-07 05:11:03 +03:00
2017-11-16 20:27:30 +03:00
// Save the PDF file into temp folder
$pdf->save($file);
// Notify the customer
2019-11-16 10:21:14 +03:00
$invoice->contact->notify(new Notification($invoice, 'invoice_new_customer'));
2017-11-16 20:27:30 +03:00
// Delete temp file
File::delete($file);
unset($invoice->paid);
unset($invoice->template_path);
unset($invoice->pdf_path);
unset($invoice->reconciled);
2017-11-16 20:27:30 +03:00
2020-12-24 01:28:38 +03:00
event(new \App\Events\Document\DocumentSent($invoice));
2017-11-16 20:27:30 +03:00
flash(trans('documents.messages.email_sent', ['type' => trans_choice('general.invoices', 1)]))->success();
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
{
$invoice = $this->prepareInvoice($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)
2017-11-07 05:11:03 +03:00
{
2017-11-16 20:27:30 +03:00
$invoice = $this->prepareInvoice($invoice);
2017-11-07 05:11:03 +03:00
$currency_style = true;
$view = view($invoice->template_path, compact('invoice', 'currency_style'))->render();
2019-12-13 11:04:17 +03:00
$html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
2017-11-15 21:53:40 +03:00
2018-06-10 15:38:07 +03:00
$pdf = app('dompdf.wrapper');
2017-11-15 21:53:40 +03:00
$pdf->loadHTML($html);
2017-11-16 20:27:30 +03:00
//$pdf->setPaper('A4', 'portrait');
2020-12-24 01:28:38 +03:00
$file_name = $this->getDocumentFileName($invoice);
2017-11-15 21:53:40 +03:00
return $pdf->download($file_name);
2017-11-07 05:11:03 +03:00
}
/**
2017-11-15 21:53:40 +03:00
* Mark the invoice as paid.
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 markPaid(Document $invoice)
2017-11-07 05:11:03 +03:00
{
2019-11-16 10:21:14 +03:00
try {
event(new \App\Events\Document\PaymentReceived($invoice, ['type' => 'income']));
2017-11-07 05:11:03 +03:00
$message = trans('general.messages.marked_paid', ['type' => trans_choice('general.invoices', 1)]);
2019-11-16 10:21:14 +03:00
flash($message)->success();
} catch(\Exception $e) {
$message = $e->getMessage();
2017-11-07 05:11:03 +03:00
2019-11-16 10:21:14 +03:00
flash($message)->error();
}
2017-11-07 05:11:03 +03:00
2017-11-15 21:53:40 +03:00
return redirect()->back();
2017-11-07 05:11:03 +03:00
}
2020-12-24 01:28:38 +03:00
protected function prepareInvoice(Document $invoice)
2017-11-16 20:27:30 +03:00
{
$paid = 0;
foreach ($invoice->transactions as $item) {
2018-07-10 17:21:40 +03:00
$amount = $item->amount;
2017-11-16 20:27:30 +03:00
2018-07-10 17:21:40 +03:00
if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code;
2020-03-09 18:07:16 +03:00
$amount = $item->getAmountConvertedFromDefault();
2018-07-10 17:21:40 +03:00
}
$paid += $amount;
2017-11-16 20:27:30 +03:00
}
$invoice->paid = $paid;
2020-12-24 01:28:38 +03:00
$invoice->template_path = 'sales.invoices.print_' . setting('invoice.template');
2017-11-16 20:27:30 +03:00
2020-12-24 01:28:38 +03:00
event(new \App\Events\Document\DocumentPrinting($invoice));
2017-11-16 20:27:30 +03:00
return $invoice;
}
2017-09-14 22:21:00 +03:00
}