183 lines
5.5 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2019-11-16 10:21:14 +03:00
namespace App\Http\Controllers\Portal;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\Controller;
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;
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\Traits\Currencies;
use App\Traits\DateTime;
2020-01-11 16:57:32 +03:00
use App\Traits\Sales;
2017-09-14 22:21:00 +03:00
use App\Traits\Uploads;
use App\Utilities\Modules;
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-01-11 16:57:32 +03:00
use DateTime, Currencies, Sales, Uploads;
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2020-01-21 11:11:23 +03:00
$invoices = Invoice::with(['contact', 'items', 'payments', 'histories'])
2019-11-16 10:21:14 +03:00
->accrued()->where('contact_id', user()->contact->id)
->collect(['invoice_number'=> 'desc']);
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$categories = collect(Category::type('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();
2019-11-16 10:21:14 +03:00
return view('portal.invoices.index', compact('invoices', '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)
{
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +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
$account_currency_code = Account::where('id', setting('default.account'))->pluck('currency_code')->first();
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$customers = Contact::type('customer')->enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
$payment_methods = Modules::getPaymentMethods();
2019-12-31 15:49:09 +03:00
event(new \App\Events\Sale\InvoiceViewed($invoice));
2019-11-16 10:21:14 +03:00
return view('portal.invoices.show', compact('invoice', 'accounts', 'currencies', 'account_currency_code', 'customers', 'categories', 'payment_methods'));
2017-09-14 22:21:00 +03:00
}
/**
* Show the form for viewing the specified resource.
*
2017-12-11 11:01:44 +03:00
* @param Invoice $invoice
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
2017-11-18 15:23:20 +03:00
public function printInvoice(Invoice $invoice)
2017-09-14 22:21:00 +03:00
{
$invoice = $this->prepareInvoice($invoice);
2017-09-14 22:21:00 +03:00
return view($invoice->template_path, compact('invoice'));
2017-09-14 22:21:00 +03:00
}
/**
* Show the form for viewing the specified resource.
*
2017-12-11 11:01:44 +03:00
* @param Invoice $invoice
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
2017-11-18 15:23:20 +03:00
public function pdfInvoice(Invoice $invoice)
2017-09-14 22:21:00 +03:00
{
$invoice = $this->prepareInvoice($invoice);
2017-09-14 22:21:00 +03:00
$currency_style = true;
$view = view($invoice->template_path, compact('invoice', 'currency_style'))->render();
$html = mb_convert_encoding($view, 'HTML-ENTITIES');
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
//$pdf->setPaper('A4', 'portrait');
$file_name = 'invoice_' . time() . '.pdf';
return $pdf->download($file_name);
}
protected function prepareInvoice(Invoice $invoice)
{
$paid = 0;
2017-09-14 22:21:00 +03:00
foreach ($invoice->transactions as $item) {
2018-07-10 17:21:40 +03:00
$amount = $item->amount;
if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code;
2019-11-16 10:21:14 +03:00
$amount = $item->getAmountConvertedFromCustomDefault();
2018-07-10 17:21:40 +03:00
}
2017-09-14 22:21:00 +03:00
2018-07-10 17:21:40 +03:00
$paid += $amount;
2017-09-14 22:21:00 +03:00
}
$invoice->paid = $paid;
2019-12-31 15:49:09 +03:00
$invoice->template_path = 'sales.invoices.print';
2017-09-14 22:21:00 +03:00
2019-12-31 15:49:09 +03:00
event(new \App\Events\Sale\InvoicePrinting($invoice));
2017-09-14 22:21:00 +03:00
return $invoice;
}
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
public function signed(Invoice $invoice)
2018-10-15 12:39:41 +03:00
{
if (empty($invoice)) {
redirect()->route('login');
}
2018-10-15 16:25:10 +03:00
2018-10-15 12:39:41 +03:00
$paid = 0;
foreach ($invoice->transactions as $item) {
2018-10-15 12:39:41 +03:00
$amount = $item->amount;
if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code;
2019-11-16 10:21:14 +03:00
$amount = $item->getAmountConvertedFromCustomDefault();
2018-10-15 12:39:41 +03:00
}
$paid += $amount;
}
$invoice->paid = $paid;
$accounts = Account::enabled()->pluck('name', 'id');
$currencies = Currency::enabled()->pluck('name', 'code')->toArray();
2019-11-16 10:21:14 +03:00
$account_currency_code = Account::where('id', setting('default.account'))->pluck('currency_code')->first();
2018-10-15 12:39:41 +03:00
2019-11-16 10:21:14 +03:00
$customers = Contact::type('customer')->enabled()->pluck('name', 'id');
2018-10-15 12:39:41 +03:00
2019-11-16 10:21:14 +03:00
$categories = Category::type('income')->enabled()->pluck('name', 'id');
2018-10-15 12:39:41 +03:00
$payment_methods = Modules::getPaymentMethods();
2018-10-15 16:25:10 +03:00
$payment_actions = [];
foreach ($payment_methods as $payment_method_key => $payment_method_value) {
$codes = explode('.', $payment_method_key);
if (!isset($payment_actions[$codes[0]])) {
2019-11-16 10:21:14 +03:00
$payment_actions[$codes[0]] = URL::signedRoute('signed.invoices.' . $codes[0] . '.show', [$invoice->id, 'company_id' => session('company_id')]);
2018-10-15 16:25:10 +03:00
}
}
2019-11-16 10:21:14 +03:00
$print_action = URL::signedRoute('signed.invoices.print', [$invoice->id, 'company_id' => session('company_id')]);
$pdf_action = URL::signedRoute('signed.invoices.pdf', [$invoice->id, 'company_id' => session('company_id')]);
2019-12-31 15:49:09 +03:00
event(new \App\Events\Sale\InvoiceViewed($invoice));
2018-10-15 16:25:10 +03:00
2019-11-16 10:21:14 +03:00
return view('portal.invoices.signed', compact('invoice', 'accounts', 'currencies', 'account_currency_code', 'customers', 'categories', 'payment_methods', 'payment_actions', 'print_action', 'pdf_action'));
2018-10-15 12:39:41 +03:00
}
2017-09-14 22:21:00 +03:00
}