Skip to content

Commit

Permalink
moved logo method to view composer
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Apr 26, 2018
1 parent 88ba196 commit c283796
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 94 deletions.
43 changes: 2 additions & 41 deletions app/Http/Controllers/Expenses/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,7 @@ public function printBill(Bill $bill)
{
$bill = $this->prepareBill($bill);

$logo = $this->getLogo($bill);

return view($bill->template_path, compact('bill', 'logo'));
return view($bill->template_path, compact('bill'));
}

/**
Expand All @@ -527,9 +525,7 @@ public function pdfBill(Bill $bill)
{
$bill = $this->prepareBill($bill);

$logo = $this->getLogo($bill);

$html = view($bill->template_path, compact('bill', 'logo'))->render();
$html = view($bill->template_path, compact('bill'))->render();

$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
Expand Down Expand Up @@ -736,39 +732,4 @@ protected function addTotals($bill, $request, $taxes, $sub_total, $discount_tota
'sort_order' => $sort_order,
]);
}

protected function getLogo($bill)
{
$logo = '';

$media_id = setting('general.company_logo');

if (isset($bill->vendor->logo) && !empty($bill->vendor->logo->id)) {
$media_id = $bill->vendor->logo->id;
}

$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;
}
}
57 changes: 4 additions & 53 deletions app/Http/Controllers/Incomes/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,9 @@ public function create()

$categories = Category::enabled()->type('income')->pluck('name', 'id');

$recurrings = [
'0' => trans('general.no'),
'1' => trans('recurring.weekly'),
'2' => trans('recurring.monthly'),
'3' => trans('recurring.yearly'),
'4' => trans('recurring.custom'),
];

$number = $this->getNextInvoiceNumber();

return view('incomes.invoices.create', compact('customers', 'currencies', 'items', 'taxes', 'categories', 'recurrings', 'number'));
return view('incomes.invoices.create', compact('customers', 'currencies', 'items', 'taxes', 'categories', 'number'));
}

/**
Expand Down Expand Up @@ -554,9 +546,7 @@ public function emailInvoice(Invoice $invoice)

$invoice = $this->prepareInvoice($invoice);

$logo = $this->getLogo();

$html = view($invoice->template_path, compact('invoice', 'logo'))->render();
$html = view($invoice->template_path, compact('invoice'))->render();

$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
Expand Down Expand Up @@ -610,9 +600,7 @@ public function printInvoice(Invoice $invoice)
{
$invoice = $this->prepareInvoice($invoice);

$logo = $this->getLogo();

return view($invoice->template_path, compact('invoice', 'logo'));
return view($invoice->template_path, compact('invoice'));
}

/**
Expand All @@ -626,9 +614,7 @@ public function pdfInvoice(Invoice $invoice)
{
$invoice = $this->prepareInvoice($invoice);

$logo = $this->getLogo();

$html = view($invoice->template_path, compact('invoice', 'logo'))->render();
$html = view($invoice->template_path, compact('invoice'))->render();

$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
Expand Down Expand Up @@ -877,39 +863,4 @@ protected function addTotals($invoice, $request, $taxes, $sub_total, $discount_t
'sort_order' => $sort_order,
]);
}

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;
}
}
54 changes: 54 additions & 0 deletions app/Http/ViewComposers/Logo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Http\ViewComposers;

use App\Models\Common\Media;
use Illuminate\View\View;
use File;
use Image;
use Storage;

class Logo
{

/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$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);

$view->with(['logo' => $logo]);
}
}
5 changes: 5 additions & 0 deletions app/Providers/ViewComposerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function boot()
View::composer(
'modules.*', 'App\Http\ViewComposers\Modules'
);

// Add logo
View::composer(
['incomes.invoices.invoice', 'expenses.bills.bill'], 'App\Http\ViewComposers\Logo'
);
}

/**
Expand Down

0 comments on commit c283796

Please sign in to comment.