182 lines
4.6 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Customers;
use App\Http\Controllers\Controller;
use App\Events\InvoicePrinting;
2017-09-14 22:21:00 +03:00
use App\Models\Banking\Account;
use App\Models\Income\Customer;
use App\Models\Income\Invoice;
use App\Models\Income\InvoiceStatus;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Models\Common\Media;
2017-09-14 22:21:00 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Uploads;
use App\Utilities\Modules;
use File;
use Image;
use Storage;
2017-09-14 22:21:00 +03:00
class Invoices extends Controller
{
use DateTime, Currencies, Uploads;
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2017-12-11 11:01:44 +03:00
$invoices = Invoice::with('status')->accrued()->where('customer_id', auth()->user()->customer->id)->paginate();
2017-09-14 22:21:00 +03:00
$status = collect(InvoiceStatus::all()->pluck('name', 'code'))
2017-09-27 21:40:25 +03:00
->prepend(trans('general.all_type', ['type' => trans_choice('general.statuses', 2)]), '');
2017-09-14 22:21:00 +03:00
return view('customers.invoices.index', compact('invoices', 'status'));
}
/**
* Show the form for viewing the specified resource.
*
* @param Invoice $invoice
*
* @return Response
*/
public function show(Invoice $invoice)
{
$paid = 0;
foreach ($invoice->payments as $item) {
2018-07-10 17:21:40 +03:00
$amount = $item->amount;
2017-09-14 22:21:00 +03:00
2018-07-10 17:21:40 +03:00
if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code;
$amount = $item->getDynamicConvertedAmount();
}
$paid += $amount;
2017-09-14 22:21:00 +03:00
}
$invoice->paid = $paid;
$accounts = Account::enabled()->pluck('name', 'id');
$currencies = Currency::enabled()->pluck('name', 'code')->toArray();
$account_currency_code = Account::where('id', setting('general.default_account'))->pluck('currency_code')->first();
$customers = Customer::enabled()->pluck('name', 'id');
$categories = Category::enabled()->type('income')->pluck('name', 'id');
$payment_methods = Modules::getPaymentMethods();
return view('customers.invoices.show', compact('invoice', 'accounts', 'currencies', 'account_currency_code', 'customers', 'categories', 'payment_methods'));
}
/**
* 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
$logo = $this->getLogo();
2017-09-14 22:21:00 +03:00
return view($invoice->template_path, compact('invoice', 'logo'));
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
$logo = $this->getLogo();
$html = view($invoice->template_path, compact('invoice', 'logo'))->render();
$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->payments 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;
$amount = $item->getDynamicConvertedAmount();
}
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;
$invoice->template_path = 'incomes.invoices.invoice';
2017-09-14 22:21:00 +03:00
event(new InvoicePrinting($invoice));
2017-09-14 22:21:00 +03:00
return $invoice;
}
2017-09-14 22:21:00 +03:00
protected function getLogo()
{
$logo = '';
$media_id = setting('general.company_logo');
if (setting('general.invoice_logo')) {
$media_id = setting('general.invoice_logo');
}
$media = Media::find($media_id);
if (!empty($media)) {
$path = Storage::path($media->getDiskPath());
if (!is_file($path)) {
return $logo;
}
} else {
$path = asset('public/img/company.png');
}
$image = Image::make($path)->encode()->getEncoded();
if (empty($image)) {
return $logo;
}
$extension = File::extension($path);
$logo = 'data:image/' . $extension . ';base64,' . base64_encode($image);
return $logo;
2017-09-14 22:21:00 +03:00
}
}