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;
|
2020-09-06 17:06:10 +03:00
|
|
|
use App\Http\Requests\Portal\InvoiceShow as Request;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Models\Document\Document;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Models\Setting\Category;
|
|
|
|
use App\Traits\Currencies;
|
|
|
|
use App\Traits\DateTime;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Traits\Documents;
|
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-12-24 01:28:38 +03:00
|
|
|
use DateTime, Currencies, Documents, Uploads;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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', 'histories', 'items', 'payments')
|
2019-11-16 10:21:14 +03:00
|
|
|
->accrued()->where('contact_id', user()->contact->id)
|
2020-12-24 01:28:38 +03:00
|
|
|
->collect(['document_number'=> 'desc']);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-05-03 11:15:56 +03:00
|
|
|
$categories = collect(Category::income()->enabled()->orderBy('name')->pluck('name', 'id'));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
$statuses = $this->getDocumentStatuses(Document::INVOICE_TYPE);
|
2019-03-25 16:10:22 +03:00
|
|
|
|
2020-11-06 00:43:46 +03:00
|
|
|
return $this->response('portal.invoices.index', compact('invoices', 'categories', 'statuses'));
|
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, Request $request)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
|
|
|
$payment_methods = Modules::getPaymentMethods();
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
event(new \App\Events\Document\DocumentViewed($invoice));
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-01-22 11:01:26 +03:00
|
|
|
return view('portal.invoices.show', compact('invoice', 'payment_methods'));
|
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 printInvoice(Document $invoice, Request $request)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2018-03-06 17:44:44 +03:00
|
|
|
$invoice = $this->prepareInvoice($invoice);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-02-13 13:05:33 +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.
|
|
|
|
*
|
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 pdfInvoice(Document $invoice, Request $request)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2018-03-06 17:44:44 +03:00
|
|
|
$invoice = $this->prepareInvoice($invoice);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-03-25 16:10:22 +03:00
|
|
|
$currency_style = true;
|
|
|
|
|
|
|
|
$view = view($invoice->template_path, compact('invoice', 'currency_style'))->render();
|
2019-02-13 13:05:33 +03:00
|
|
|
$html = mb_convert_encoding($view, 'HTML-ENTITIES');
|
2018-03-06 17:44:44 +03:00
|
|
|
|
|
|
|
$pdf = \App::make('dompdf.wrapper');
|
|
|
|
$pdf->loadHTML($html);
|
|
|
|
|
|
|
|
//$pdf->setPaper('A4', 'portrait');
|
|
|
|
|
|
|
|
$file_name = 'invoice_' . time() . '.pdf';
|
|
|
|
|
|
|
|
return $pdf->download($file_name);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
protected function prepareInvoice(Document $invoice)
|
2018-03-06 17:44:44 +03:00
|
|
|
{
|
2020-02-01 01:37:41 +03:00
|
|
|
$invoice->template_path = 'sales.invoices.print_' . setting('invoice.template' ,'default');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
event(new \App\Events\Document\DocumentPrinting($invoice));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-03-06 17:44:44 +03:00
|
|
|
return $invoice;
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
public function signed(Document $invoice)
|
2018-10-15 12:39:41 +03:00
|
|
|
{
|
2018-12-11 18:31:23 +03:00
|
|
|
if (empty($invoice)) {
|
|
|
|
redirect()->route('login');
|
|
|
|
}
|
2018-10-15 16:25:10 +03:00
|
|
|
|
|
|
|
$payment_actions = [];
|
|
|
|
|
2021-01-19 12:52:57 +03:00
|
|
|
$payment_methods = Modules::getPaymentMethods();
|
|
|
|
|
2018-10-15 16:25:10 +03:00
|
|
|
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')]);
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
event(new \App\Events\Document\DocumentViewed($invoice));
|
2018-10-15 16:25:10 +03:00
|
|
|
|
2020-01-22 11:01:26 +03:00
|
|
|
return view('portal.invoices.signed', compact('invoice', 'payment_methods', 'payment_actions', 'print_action', 'pdf_action'));
|
2018-10-15 12:39:41 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|