127 lines
3.6 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;
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);
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
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
{
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
$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');
2021-06-11 01:34:57 +03:00
$pdf = app('dompdf.wrapper');
$pdf->loadHTML($html);
//$pdf->setPaper('A4', 'portrait');
2021-06-11 01:34:57 +03:00
$file_name = $this->getDocumentFileName($invoice);
return $pdf->download($file_name);
}
2020-12-24 01:28:38 +03:00
public function signed(Document $invoice)
2018-10-15 12:39:41 +03:00
{
if (empty($invoice)) {
2021-04-17 01:29:18 +03:00
return 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]])) {
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
2020-12-24 01:28:38 +03:00
event(new \App\Events\Document\DocumentViewed($invoice));
2018-10-15 16:25:10 +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
}