484 lines
13 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;
2019-12-31 15:49:09 +03:00
use App\Exports\Sales\Invoices as Export;
2019-11-16 10:21:14 +03:00
use App\Http\Requests\Common\Import as ImportRequest;
2019-12-31 15:49:09 +03:00
use App\Http\Requests\Sale\Invoice as Request;
use App\Http\Requests\Sale\InvoiceAddItem as ItemRequest;
use App\Imports\Sales\Invoices as Import;
use App\Jobs\Sale\CreateInvoice;
use App\Jobs\Sale\DeleteInvoice;
use App\Jobs\Sale\DuplicateInvoice;
use App\Jobs\Sale\UpdateInvoice;
2017-09-14 22:21:00 +03:00
use App\Models\Banking\Account;
2019-11-16 10:21:14 +03:00
use App\Models\Common\Contact;
2018-11-05 01:25:43 +03:00
use App\Models\Common\Item;
2019-12-31 15:49:09 +03:00
use App\Models\Sale\Invoice;
2017-09-14 22:21:00 +03:00
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Models\Setting\Tax;
2019-12-31 15:49:09 +03:00
use App\Notifications\Sale\Invoice as Notification;
2017-09-14 22:21:00 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
2019-12-31 16:03:20 +03:00
use App\Traits\Sales;
2017-09-14 22:21:00 +03:00
use App\Utilities\Modules;
2017-11-16 20:27:30 +03:00
use File;
2019-11-16 10:21:14 +03:00
use Illuminate\Support\Facades\URL;
2017-09-14 22:21:00 +03:00
class Invoices extends Controller
{
2020-04-07 14:39:35 +03:00
use Currencies, DateTime, Sales;
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2020-06-07 12:11:37 +03:00
$invoices = Invoice::with('contact', 'transactions')->collect(['invoice_number'=> 'desc']);
2017-09-14 22:21:00 +03:00
2020-04-07 14:39:35 +03:00
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');
2017-09-27 21:40:25 +03:00
2020-05-03 11:15:56 +03:00
$categories = Category::income()->enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2020-01-11 16:57:32 +03:00
$statuses = $this->getInvoiceStatuses();
2018-11-02 11:32:42 +03:00
2019-12-31 15:49:09 +03:00
return view('sales.invoices.index', compact('invoices', 'customers', 'categories', 'statuses'));
2017-09-14 22:21:00 +03:00
}
/**
* Show the form for viewing the specified resource.
*
* @param Invoice $invoice
*
* @return Response
*/
public function show(Invoice $invoice)
{
2018-06-29 14:42:15 +03:00
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
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
2019-11-16 10:21:14 +03:00
$account_currency_code = Account::where('id', setting('default.account'))->pluck('currency_code')->first();
2017-09-14 22:21:00 +03:00
2020-04-07 14:39:35 +03:00
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');
2019-11-16 10:21:14 +03:00
2020-05-03 11:15:56 +03:00
$categories = Category::income()->enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
$payment_methods = Modules::getPaymentMethods();
2019-11-16 10:21:14 +03:00
$signed_url = URL::signedRoute('signed.invoices.show', [$invoice->id, 'company_id' => session('company_id')]);
$date_format = $this->getCompanyDateFormat();
2018-10-15 16:10:49 +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);
}
2019-12-31 15:49:09 +03:00
return view('sales.invoices.show', compact('invoice', 'accounts', 'currencies', 'currency', 'account_currency_code', 'customers', 'categories', 'payment_methods', 'signed_url', 'date_format'));
2017-09-14 22:21:00 +03:00
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
2020-04-07 14:39:35 +03:00
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$currency = Currency::where('code', setting('default.currency'))->first();
2020-01-21 15:26:21 +03:00
$items = Item::enabled()->orderBy('name')->get();
2017-09-14 22:21:00 +03:00
$taxes = Tax::enabled()->orderBy('name')->get();
2017-09-14 22:21:00 +03:00
2020-05-03 11:15:56 +03:00
$categories = Category::income()->enabled()->orderBy('name')->pluck('name', 'id');
2018-04-23 22:17:20 +03:00
2017-11-26 15:20:17 +03:00
$number = $this->getNextInvoiceNumber();
2017-10-31 22:41:06 +03:00
2019-12-31 15:49:09 +03:00
return view('sales.invoices.create', compact('customers', 'currencies', 'currency', 'items', 'taxes', 'categories', 'number'));
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)
{
2019-11-16 10:21:14 +03:00
$response = $this->ajaxDispatch(new CreateInvoice($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.
*
* @param Invoice $invoice
*
* @return Response
*/
public function duplicate(Invoice $invoice)
{
2019-11-16 10:21:14 +03:00
$clone = $this->dispatch(new DuplicateInvoice($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'));
} 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.
*
2019-11-16 10:21:14 +03:00
* @param Invoice $invoice
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
public function edit(Invoice $invoice)
{
2020-04-07 14:39:35 +03:00
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$currency = Currency::where('code', $invoice->currency_code)->first();
2018-08-17 17:17:56 +03:00
2020-01-21 15:26:21 +03:00
$items = Item::enabled()->orderBy('name')->get();
2017-09-14 22:21:00 +03:00
$taxes = Tax::enabled()->orderBy('name')->get();
2017-09-14 22:21:00 +03:00
2020-05-03 11:15:56 +03:00
$categories = Category::income()->enabled()->orderBy('name')->pluck('name', 'id');
2018-04-23 22:17:20 +03:00
2019-12-31 15:49:09 +03:00
return view('sales.invoices.edit', compact('invoice', 'customers', 'currencies', 'currency', 'items', 'taxes', 'categories'));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
2019-11-16 10:21:14 +03:00
* @param Invoice $invoice
* @param Request $request
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
public function update(Invoice $invoice, Request $request)
{
2019-11-16 10:21:14 +03:00
$response = $this->ajaxDispatch(new UpdateInvoice($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.
*
2019-11-16 10:21:14 +03:00
* @param Invoice $invoice
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
public function destroy(Invoice $invoice)
{
2019-11-16 10:21:14 +03:00
$response = $this->ajaxDispatch(new DeleteInvoice($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()
{
2020-02-12 12:18:08 +03:00
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.
*
2017-11-08 17:22:04 +03:00
* @param Invoice $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2017-11-08 17:22:04 +03:00
public function markSent(Invoice $invoice)
2017-11-07 05:11:03 +03:00
{
2019-12-31 15:49:09 +03:00
event(new \App\Events\Sale\InvoiceSent($invoice));
2019-11-16 10:21:14 +03:00
$message = trans('invoices.messages.marked_sent');
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.
*
* @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();
}
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
*
2017-11-08 17:22:04 +03:00
* @param Invoice $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2017-11-16 20:27:30 +03:00
public function emailInvoice(Invoice $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
$file_name = $this->getInvoiceFileName($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
2019-12-31 15:49:09 +03:00
event(new \App\Events\Sale\InvoiceSent($invoice));
2017-11-16 20:27:30 +03:00
flash(trans('invoices.messages.email_sent'))->success();
return redirect()->back();
}
/**
* Print the invoice.
*
* @param Invoice $invoice
*
* @return Response
*/
public function printInvoice(Invoice $invoice)
{
$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
*
2017-11-08 17:22:04 +03:00
* @param Invoice $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2017-11-15 21:53:40 +03:00
public function pdfInvoice(Invoice $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');
$file_name = $this->getInvoiceFileName($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
*
2017-11-08 17:22:04 +03:00
* @param Invoice $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2017-11-15 21:53:40 +03:00
public function markPaid(Invoice $invoice)
2017-11-07 05:11:03 +03:00
{
2019-11-16 10:21:14 +03:00
try {
2020-01-19 00:41:27 +03:00
event(new \App\Events\Sale\PaymentReceived($invoice));
2017-11-07 05:11:03 +03:00
2019-11-16 10:21:14 +03:00
$message = trans('invoices.messages.marked_paid');
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
}
public function addItem(ItemRequest $request)
{
$item_row = $request['item_row'];
$currency_code = $request['currency_code'];
2019-11-16 10:21:14 +03:00
$taxes = Tax::enabled()->orderBy('name')->get()->pluck('title', 'id');
2019-11-16 10:21:14 +03:00
$currency = Currency::where('code', $currency_code)->first();
if (empty($currency)) {
2019-11-16 10:21:14 +03:00
$currency = Currency::where('code', setting('default.currency', 'USD'))->first();
}
if ($currency) {
// it should be integer for amount mask
$currency->precision = (int) $currency->precision;
}
2019-12-31 15:49:09 +03:00
$html = view('sales.invoices.item', compact('item_row', 'taxes', 'currency'))->render();
return response()->json([
'success' => true,
'error' => false,
'data' => [
'currency' => $currency
],
'message' => 'null',
'html' => $html,
]);
}
2017-11-16 20:27:30 +03:00
protected function prepareInvoice(Invoice $invoice)
{
$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;
2019-12-31 15:49:09 +03:00
$invoice->template_path = 'sales.invoices.print_' . setting('invoice.template' ,'default');
2017-11-16 20:27:30 +03:00
2019-12-31 15:49:09 +03:00
event(new \App\Events\Sale\InvoicePrinting($invoice));
2017-11-16 20:27:30 +03:00
return $invoice;
}
2017-09-14 22:21:00 +03:00
}