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\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
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
return $this->response('portal.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, 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
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
/**
|
|
|
|
* Show the form for viewing the specified resource.
|
|
|
|
*
|
|
|
|
* @param Document $invoice
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function finish(Document $invoice, Request $request)
|
|
|
|
{
|
|
|
|
$layout = $request->isPortal($invoice->company_id) ? 'portal' : 'signed';
|
|
|
|
|
|
|
|
return view('portal.invoices.finish', compact('invoice', 'layout'));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-06-11 01:34:57 +03:00
|
|
|
event(new \App\Events\Document\DocumentPrinting($invoice));
|
|
|
|
|
|
|
|
$view = view($invoice->template_path, compact('invoice'));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2021-06-11 01:34:57 +03:00
|
|
|
return mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
|
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
|
|
|
{
|
2021-06-11 01:34:57 +03:00
|
|
|
event(new \App\Events\Document\DocumentPrinting($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();
|
2021-06-11 01:34:57 +03:00
|
|
|
$html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
|
2018-03-06 17:44:44 +03:00
|
|
|
|
2021-06-11 01:34:57 +03:00
|
|
|
$pdf = app('dompdf.wrapper');
|
2018-03-06 17:44:44 +03:00
|
|
|
$pdf->loadHTML($html);
|
|
|
|
|
|
|
|
//$pdf->setPaper('A4', 'portrait');
|
|
|
|
|
2021-06-11 01:34:57 +03:00
|
|
|
$file_name = $this->getDocumentFileName($invoice);
|
2018-03-06 17:44:44 +03:00
|
|
|
|
|
|
|
return $pdf->download($file_name);
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
public function preview(Document $invoice)
|
|
|
|
{
|
|
|
|
if (empty($invoice)) {
|
|
|
|
return redirect()->route('login');
|
|
|
|
}
|
|
|
|
|
|
|
|
$payment_actions = [];
|
|
|
|
|
|
|
|
$payment_methods = Modules::getPaymentMethods();
|
|
|
|
|
|
|
|
foreach ($payment_methods as $payment_method_key => $payment_method_value) {
|
|
|
|
$codes = explode('.', $payment_method_key);
|
|
|
|
|
|
|
|
if (!isset($payment_actions[$codes[0]])) {
|
|
|
|
$payment_actions[$codes[0]] = URL::signedRoute('signed.' . $codes[0] . '.invoices.show', [$invoice->id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('portal.invoices.preview', compact('invoice', 'payment_methods', 'payment_actions'));
|
|
|
|
}
|
|
|
|
|
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)) {
|
2021-04-17 01:29:18 +03:00
|
|
|
return redirect()->route('login');
|
2018-12-11 18:31:23 +03:00
|
|
|
}
|
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]])) {
|
2021-04-17 01:29:18 +03:00
|
|
|
$payment_actions[$codes[0]] = URL::signedRoute('signed.' . $codes[0] . '.invoices.show', [$invoice->id]);
|
2018-10-15 16:25:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 00:59:43 +03:00
|
|
|
$print_action = URL::signedRoute('signed.invoices.print', [$invoice->id]);
|
|
|
|
$pdf_action = URL::signedRoute('signed.invoices.pdf', [$invoice->id]);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
// Guest or Invoice contact user track the invoice viewed.
|
|
|
|
if (empty(user()) || user()->id == $invoice->contact->user_id) {
|
|
|
|
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
|
|
|
}
|