close #222 Fixed: invoice logo in customer account

This commit is contained in:
cuneytsenturk 2018-03-06 17:44:44 +03:00
parent 0f87195254
commit 71e5c80296
5 changed files with 122 additions and 99 deletions

View File

@ -3,16 +3,21 @@
namespace App\Http\Controllers\Customers; namespace App\Http\Controllers\Customers;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Events\InvoicePrinting;
use App\Models\Banking\Account; use App\Models\Banking\Account;
use App\Models\Income\Customer; use App\Models\Income\Customer;
use App\Models\Income\Invoice; use App\Models\Income\Invoice;
use App\Models\Income\InvoiceStatus; use App\Models\Income\InvoiceStatus;
use App\Models\Setting\Category; use App\Models\Setting\Category;
use App\Models\Setting\Currency; use App\Models\Setting\Currency;
use App\Models\Common\Media;
use App\Traits\Currencies; use App\Traits\Currencies;
use App\Traits\DateTime; use App\Traits\DateTime;
use App\Traits\Uploads; use App\Traits\Uploads;
use App\Utilities\Modules; use App\Utilities\Modules;
use File;
use Image;
use Storage;
class Invoices extends Controller class Invoices extends Controller
{ {
@ -42,25 +47,15 @@ class Invoices extends Controller
*/ */
public function show(Invoice $invoice) public function show(Invoice $invoice)
{ {
$sub_total = 0;
$tax_total = 0;
$paid = 0; $paid = 0;
foreach ($invoice->items as $item) {
$sub_total += ($item->price * $item->quantity);
$tax_total += ($item->tax * $item->quantity);
}
foreach ($invoice->payments as $item) { foreach ($invoice->payments as $item) {
$item->default_currency_code = $invoice->currency_code; $item->default_currency_code = $invoice->currency_code;
$paid += $item->getDynamicConvertedAmount(); $paid += $item->getDynamicConvertedAmount();
} }
$invoice->sub_total = $sub_total;
$invoice->tax_total = $tax_total;
$invoice->paid = $paid; $invoice->paid = $paid;
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
$accounts = Account::enabled()->pluck('name', 'id'); $accounts = Account::enabled()->pluck('name', 'id');
@ -86,27 +81,11 @@ class Invoices extends Controller
*/ */
public function printInvoice(Invoice $invoice) public function printInvoice(Invoice $invoice)
{ {
$sub_total = 0; $invoice = $this->prepareInvoice($invoice);
$tax_total = 0;
$paid = 0;
foreach ($invoice->items as $item) { $logo = $this->getLogo();
$sub_total += ($item->price * $item->quantity);
$tax_total += ($item->tax * $item->quantity);
}
foreach ($invoice->payments as $item) { return view($invoice->template_path, compact('invoice', 'logo'));
$item->default_currency_code = $invoice->currency_code;
$paid += $item->getDynamicConvertedAmount();
}
$invoice->sub_total = $sub_total;
$invoice->tax_total = $tax_total;
$invoice->paid = $paid;
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
return view('customers.invoices.invoice', compact('invoice'));
} }
/** /**
@ -118,14 +97,25 @@ class Invoices extends Controller
*/ */
public function pdfInvoice(Invoice $invoice) public function pdfInvoice(Invoice $invoice)
{ {
$sub_total = 0; $invoice = $this->prepareInvoice($invoice);
$tax_total = 0;
$paid = 0;
foreach ($invoice->items as $item) { $logo = $this->getLogo();
$sub_total += ($item->price * $item->quantity);
$tax_total += ($item->tax * $item->quantity); $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;
foreach ($invoice->payments as $item) { foreach ($invoice->payments as $item) {
$item->default_currency_code = $invoice->currency_code; $item->default_currency_code = $invoice->currency_code;
@ -133,18 +123,47 @@ class Invoices extends Controller
$paid += $item->getDynamicConvertedAmount(); $paid += $item->getDynamicConvertedAmount();
} }
$invoice->sub_total = $sub_total;
$invoice->tax_total = $tax_total;
$invoice->paid = $paid; $invoice->paid = $paid;
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
$html = view('incomes.invoices.invoice', compact('invoice'))->render(); $invoice->template_path = 'incomes.invoices.invoice';
$pdf = \App::make('dompdf.wrapper'); event(new InvoicePrinting($invoice));
$pdf->loadHTML($html);
$file_name = 'invoice_'.time().'.pdf'; return $invoice;
}
return $pdf->download($file_name); 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;
} }
} }

View File

@ -793,14 +793,14 @@ class Invoices extends Controller
$media = Media::find($media_id); $media = Media::find($media_id);
if (empty($media)) { if (!empty($media)) {
return $logo; $path = Storage::path($media->getDiskPath());
}
$path = Storage::path($media->getDiskPath()); if (!is_file($path)) {
return $logo;
if (!is_file($path)) { }
return $logo; } else {
$path = asset('public/img/company.png');
} }
$image = Image::make($path)->encode()->getEncoded(); $image = Image::make($path)->encode()->getEncoded();

View File

@ -6,10 +6,8 @@
<section class="invoice"> <section class="invoice">
<div class="row invoice-header"> <div class="row invoice-header">
<div class="col-xs-7"> <div class="col-xs-7">
@if (setting('general.invoice_logo')) @if ($logo)
<img src="{{ asset(setting('general.invoice_logo')) }}" class="invoice-logo" /> <img src="{{ $logo }}" class="invoice-logo" />
@else
<img src="{{ asset(setting('general.company_logo')) }}" class="invoice-logo" />
@endif @endif
</div> </div>
<div class="col-xs-5 invoice-company"> <div class="col-xs-5 invoice-company">
@ -92,7 +90,7 @@
</td> </td>
<td class="text-center">{{ $item->quantity }}</td> <td class="text-center">{{ $item->quantity }}</td>
<td class="text-right">@money($item->price, $invoice->currency_code, true)</td> <td class="text-right">@money($item->price, $invoice->currency_code, true)</td>
<td class="text-right">@money($item->total - $item->tax, $invoice->currency_code, true)</td> <td class="text-right">@money($item->total, $invoice->currency_code, true)</td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
@ -114,24 +112,25 @@
<div class="table-responsive"> <div class="table-responsive">
<table class="table"> <table class="table">
<tbody> <tbody>
<tr> @foreach($invoice->totals as $total)
<th style="max-width: 214px">{{ trans('invoices.sub_total') }}:</th> @if($total->code != 'total')
<td class="text-right">@money($invoice->sub_total, $invoice->currency_code, true)</td> <tr>
</tr> <th>{{ trans($total['name']) }}:</th>
<tr> <td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
<th>{{ trans('invoices.tax_total') }}:</th> </tr>
<td class="text-right">@money($invoice->tax_total, $invoice->currency_code, true)</td> @else
</tr> @if ($invoice->paid)
@if($invoice->paid) <tr class="text-success">
<tr> <th>{{ trans('invoices.paid') }}:</th>
<th>{{ trans('invoices.paid') }}:</th> <td class="text-right">- @money($invoice->paid, $invoice->currency_code, true)</td>
<td class="text-right">@money('-' . $invoice->paid, $invoice->currency_code, true)</td> </tr>
</tr> @endif
@endif <tr>
<tr> <th>{{ trans($total['name']) }}:</th>
<th>{{ trans('invoices.total') }}:</th> <td class="text-right">@money($total->amount - $invoice->paid, $invoice->currency_code, true)</td>
<td class="text-right">@money($invoice->grand_total, $invoice->currency_code, true)</td> </tr>
</tr> @endif
@endforeach
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -10,9 +10,11 @@
<div class="row invoice-header"> <div class="row invoice-header">
<div class="col-xs-7"> <div class="col-xs-7">
@if (setting('general.invoice_logo')) @if (setting('general.invoice_logo'))
<img src="{{ asset(setting('general.invoice_logo')) }}" class="invoice-logo" /> <img src="{{ Storage::url(setting('general.invoice_logo')) }}" class="invoice-logo" />
@elseif (setting('general.company_logo'))
<img src="{{ Storage::url(setting('general.company_logo')) }}" class="invoice-logo" />
@else @else
<img src="{{ asset(setting('general.company_logo')) }}" class="invoice-logo" /> <img src="{{ asset('public/img/company.png') }}" class="invoice-logo" />
@endif @endif
</div> </div>
<div class="col-xs-5 invoice-company"> <div class="col-xs-5 invoice-company">
@ -95,7 +97,7 @@
</td> </td>
<td class="text-center">{{ $item->quantity }}</td> <td class="text-center">{{ $item->quantity }}</td>
<td class="text-right">@money($item->price, $invoice->currency_code, true)</td> <td class="text-right">@money($item->price, $invoice->currency_code, true)</td>
<td class="text-right">@money($item->total - $item->tax, $invoice->currency_code, true)</td> <td class="text-right">@money($item->total, $invoice->currency_code, true)</td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
@ -117,24 +119,25 @@
<div class="table-responsive"> <div class="table-responsive">
<table class="table"> <table class="table">
<tbody> <tbody>
<tr> @foreach($invoice->totals as $total)
<th>{{ trans('invoices.sub_total') }}:</th> @if($total->code != 'total')
<td class="text-right">@money($invoice->sub_total, $invoice->currency_code, true)</td> <tr>
</tr> <th>{{ trans($total['name']) }}:</th>
<tr> <td class="text-right">@money($total->amount, $invoice->currency_code, true)</td>
<th>{{ trans('invoices.tax_total') }}:</th> </tr>
<td class="text-right">@money($invoice->tax_total, $invoice->currency_code, true)</td> @else
</tr> @if ($invoice->paid)
@if ($invoice->paid) <tr class="text-success">
<tr> <th>{{ trans('invoices.paid') }}:</th>
<th>{{ trans('invoices.paid') }}:</th> <td class="text-right">- @money($invoice->paid, $invoice->currency_code, true)</td>
<td class="text-right">@money('-' . $invoice->paid, $invoice->currency_code, true)</td> </tr>
</tr> @endif
@endif <tr>
<tr> <th>{{ trans($total['name']) }}:</th>
<th>{{ trans('invoices.total') }}:</th> <td class="text-right">@money($total->amount - $invoice->paid, $invoice->currency_code, true)</td>
<td class="text-right">@money($invoice->grand_total, $invoice->currency_code, true)</td> </tr>
</tr> @endif
@endforeach
</tbody> </tbody>
</table> </table>
</div> </div>
@ -143,10 +146,10 @@
<div class="box-footer row no-print"> <div class="box-footer row no-print">
<div class="col-md-10"> <div class="col-md-10">
<a href="{{ url('incomes/invoices/' . $invoice->id . '/print') }}" target="_blank" class="btn btn-default"> <a href="{{ url('customers/invoices/' . $invoice->id . '/print') }}" target="_blank" class="btn btn-default">
<i class="fa fa-print"></i>&nbsp; {{ trans('general.print') }} <i class="fa fa-print"></i>&nbsp; {{ trans('general.print') }}
</a> </a>
<a href="{{ url('incomes/invoices/' . $invoice->id . '/pdf') }}" class="btn btn-default" data-toggle="tooltip" title="{{ trans('invoices.download_pdf') }}"> <a href="{{ url('customers/invoices/' . $invoice->id . '/pdf') }}" class="btn btn-default" data-toggle="tooltip" title="{{ trans('invoices.download_pdf') }}">
<i class="fa fa-file-pdf-o"></i>&nbsp; {{ trans('general.download') }} <i class="fa fa-file-pdf-o"></i>&nbsp; {{ trans('general.download') }}
</a> </a>
</div> </div>

View File

@ -10,9 +10,11 @@
<div class="row invoice-header"> <div class="row invoice-header">
<div class="col-xs-7"> <div class="col-xs-7">
@if (setting('general.invoice_logo')) @if (setting('general.invoice_logo'))
<img src="{{ Storage::url(setting('general.invoice_logo')) }}" class="invoice-logo" /> <img src="{{ Storage::url(setting('general.invoice_logo')) }}" class="invoice-logo" />
@elseif (setting('general.company_logo'))
<img src="{{ Storage::url(setting('general.company_logo')) }}" class="invoice-logo" />
@else @else
<img src="{{ Storage::url(setting('general.company_logo')) }}" class="invoice-logo" /> <img src="{{ asset('public/img/company.png') }}" class="invoice-logo" />
@endif @endif
</div> </div>
<div class="col-xs-5 invoice-company"> <div class="col-xs-5 invoice-company">