From b2678220c3ad20c033b8e63c57fe88d63ecd7e28 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 23 Apr 2015 00:40:21 +0300 Subject: [PATCH] Working on PDF attachments --- .gitignore | 1 + app/Http/Controllers/ActivityController.php | 10 ++++- app/Http/Controllers/InvoiceController.php | 13 ++----- app/Models/Invoice.php | 6 +++ app/Ninja/Mailers/ContactMailer.php | 2 +- app/Ninja/Mailers/Mailer.php | 11 ++---- public/css/built.css | 8 +++- public/css/style.css | 8 +++- resources/views/header.blade.php | 6 ++- resources/views/invoices/edit.blade.php | 43 +++++++++++---------- 10 files changed, 66 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index dd49935a1d..d50f41640c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ /public/build /public/packages /public/vendor +/storage /bootstrap/compiled.php /bootstrap/environment.php /vendor diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index 95883aa68d..bf37d1cb9a 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -4,6 +4,7 @@ use Auth; use DB; use Datatable; use Utils; +use View; class ActivityController extends BaseController { @@ -19,7 +20,14 @@ class ActivityController extends BaseController ->addColumn('id', function ($model) { return Utils::timestampToDateTimeString(strtotime($model->created_at)); }) ->addColumn('message', function ($model) { return Utils::decodeActivity($model->message); }) ->addColumn('balance', function ($model) { return Utils::formatMoney($model->balance, $model->currency_id); }) - ->addColumn('adjustment', function ($model) { return $model->adjustment != 0 ? Utils::formatMoney($model->adjustment, $model->currency_id) : ''; }) + ->addColumn('adjustment', function ($model) { return $model->adjustment != 0 ? self::wrapAdjustment($model->adjustment, $model->currency_id) : ''; }) ->make(); } + + private function wrapAdjustment($adjustment, $currencyId) + { + $class = $adjustment <= 0 ? 'success' : 'default'; + $adjustment = Utils::formatMoney($adjustment, $currencyId); + return "

$adjustment

"; + } } diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 244df9183b..b283b68b7d 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -11,6 +11,7 @@ use DB; use Event; use URL; use Datatable; +use finfo; use App\Models\Invoice; use App\Models\Invitation; @@ -440,12 +441,10 @@ class InvoiceController extends BaseController Utils::trackViewed($client->getDisplayName(), ENTITY_CLIENT, $url); } - /* - This causes an error message. Commenting out. will return later. if (!empty(Input::get('pdfupload')) && strpos(Input::get('pdfupload'), 'data:application/pdf;base64,') === 0) { $this->storePDF(Input::get('pdfupload'), $invoice->id); } - */ + if ($action == 'clone') { return $this->cloneInvoice($publicId); } elseif ($action == 'convert') { @@ -594,12 +593,6 @@ class InvoiceController extends BaseController $uploadsDir = storage_path().'/pdfcache/'; $encodedString = str_replace('data:application/pdf;base64,', '', $encodedString); $name = 'cache-'.$invoiceId.'.pdf'; - - if (file_put_contents($uploadsDir.$name, base64_decode($encodedString)) !== false) { - $finfo = new finfo(FILEINFO_MIME); - if ($finfo->file($uploadsDir.$name) !== 'application/pdf; charset=binary') { - unlink($uploadsDir.$name); - } - } + file_put_contents($uploadsDir.$name, base64_decode($encodedString)); } } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index db86b9c209..fe1156c4e4 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -51,6 +51,12 @@ class Invoice extends EntityModel return $this->invoice_number; } + public function getFileName() + { + $entityType = $this->getEntityType(); + return trans("texts.$entityType") . '_' . $this->invoice_number . '.pdf'; + } + public function getLink() { return link_to('invoices/'.$this->public_id, $this->invoice_number); diff --git a/app/Ninja/Mailers/ContactMailer.php b/app/Ninja/Mailers/ContactMailer.php index 8ac55cb281..4df49bb956 100644 --- a/app/Ninja/Mailers/ContactMailer.php +++ b/app/Ninja/Mailers/ContactMailer.php @@ -44,7 +44,7 @@ class ContactMailer extends Mailer $data['body'] = str_replace(array_keys($variables), array_values($variables), $emailTemplate); $data['link'] = $invitation->getLink(); $data['entityType'] = $entityType; - $data['id'] = $invoice->getAttributes()['id']; + $data['invoice_id'] = $invoice->id; $fromEmail = $invitation->user->email; $this->sendTo($invitation->contact->email, $fromEmail, $accountName, $subject, $view, $data); diff --git a/app/Ninja/Mailers/Mailer.php b/app/Ninja/Mailers/Mailer.php index a11ff7c978..1edb56be3a 100644 --- a/app/Ninja/Mailers/Mailer.php +++ b/app/Ninja/Mailers/Mailer.php @@ -21,16 +21,13 @@ class Mailer $fromEmail = NINJA_FROM_EMAIL; } - if(isset($data['id'])) { - $invoice = Invoice::find($data['id']); - $invoice->load('account'); - $accountAttributes = $invoice->account()->getParent()->getRelations()['account']->getAttributes(); + if(isset($data['invoice_id'])) { + $invoice = Invoice::scope($data['invoice_id'])->with(['account'])->first(); $pdfPath = storage_path().'/pdfcache/cache-'.$invoice->id.'.pdf'; - - if($accountAttributes['pdf_email_attachment'] === 1 && file_exists($pdfPath)) { + if($invoice->account->pdf_email_attachment && file_exists($pdfPath)) { $message->attach( $pdfPath, - array('as' => $accountAttributes['name'].'_'.$accountAttributes['invoice_number_prefix'].$invoice->getName().'.pdf', 'mime' => 'application/pdf') + array('as' => $invoice->getFileName(), 'mime' => 'application/pdf') ); } } diff --git a/public/css/built.css b/public/css/built.css index 5115288e1f..e6c0c77226 100644 --- a/public/css/built.css +++ b/public/css/built.css @@ -2443,4 +2443,10 @@ div.checkbox > label { .panel-default { border-color: #e37329 !important; } -*/ \ No newline at end of file +*/ + +.alert-hide { + position: absolute; + margin-left: 25%; + z-index: 9999; +} \ No newline at end of file diff --git a/public/css/style.css b/public/css/style.css index 243f095041..266e2f7f21 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -846,4 +846,10 @@ div.checkbox > label { .panel-default { border-color: #e37329 !important; } -*/ \ No newline at end of file +*/ + +.alert-hide { + position: absolute; + margin-left: 25%; + z-index: 9999; +} \ No newline at end of file diff --git a/resources/views/header.blade.php b/resources/views/header.blade.php index a0b892bdc4..10b275ab7f 100644 --- a/resources/views/header.blade.php +++ b/resources/views/header.blade.php @@ -213,6 +213,10 @@ $(function() { + window.setTimeout(function() { + $(".alert-hide").fadeOut(500); + }, 3000); + $('#search').focus(function(){ if (!window.hasOwnProperty('searchData')) { $.get('{{ URL::route('getSearchData') }}', function(data) { @@ -410,7 +414,7 @@ @endif @if (Session::has('message')) -
+
{{ Session::get('message') }}
@elseif (Session::has('news_feed_message')) diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index 1df700acf2..bf2dcfcd6a 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -35,7 +35,7 @@
-
+
@@ -129,8 +129,6 @@

 

- {!! Former::hidden('data')->data_bind("value: ko.mapping.toJSON(model)") !!} -
@@ -151,8 +149,8 @@
- {!! Former::text('product_key')->useDatalist($products->toArray(), 'product_key')->onkeyup('onItemChange()') - ->raw()->data_bind("value: product_key, valueUpdate: 'afterkeydown'")->addClass('datalist') !!} + {!! Former::text('product_key')->useDatalist($products->toArray(), 'product_key')->onkeyup('onItemChange()') + ->raw()->data_bind("value: product_key, valueUpdate: 'afterkeydown'")->addClass('datalist') !!} @@ -299,6 +297,8 @@ {!! Former::populateField('entityType', $entityType) !!} {!! Former::text('entityType') !!} {!! Former::text('action') !!} + {!! Former::text('data')->data_bind("value: ko.mapping.toJSON(model)") !!} + {!! Former::text('pdfupload') !!} @if ($invoice && $invoice->id) {!! Former::populateField('id', $invoice->public_id) !!} @@ -308,9 +308,9 @@ @if (!Utils::isPro() || \App\Models\InvoiceDesign::count() == COUNT_FREE_DESIGNS) - {!! Former::select('invoice_design_id')->style('display:inline;width:150px;background-color:white !important')->raw()->fromQuery($invoiceDesigns, 'name', 'id')->data_bind("value: invoice_design_id")->addOption(trans('texts.more_designs') . '...', '-1') !!} + {!! Former::select('invoice_design_id')->style('display:'.($account->utf8_invoices ? 'none' : 'inline').';width:150px;background-color:white !important')->raw()->fromQuery($invoiceDesigns, 'name', 'id')->data_bind("value: invoice_design_id")->addOption(trans('texts.more_designs') . '...', '-1') !!} @else - {!! Former::select('invoice_design_id')->style('display:inline;width:150px;background-color:white !important')->raw()->fromQuery($invoiceDesigns, 'name', 'id')->data_bind("value: invoice_design_id") !!} + {!! Former::select('invoice_design_id')->style('display:'.($account->utf8_invoices ? 'none' : 'inline').';width:150px;background-color:white !important')->raw()->fromQuery($invoiceDesigns, 'name', 'id')->data_bind("value: invoice_design_id") !!} @endif {!! Button::primary(trans('texts.download_pdf'))->withAttributes(array('onclick' => 'onDownloadClick()'))->appendIcon(Icon::create('download-alt')) !!} @@ -674,7 +674,7 @@ }); @if (Auth::user()->account->fill_products) - $('.datalist').on('input', function() { + $('.datalist').on('change', function() { var key = $(this).val(); for (var i=0; i'); - submitAction(''); - }); - + preparePdfData(''); } } + function preparePdfData(action) { + var invoice = createInvoiceModel(); + var design = getDesignJavascript(); + if (!design) return; + + doc = generatePDF(invoice, design, true); + doc.getDataUrl( function(pdfString){ + $('#pdfupload').val(pdfString); + submitAction(action); + }); + } + function submitAction(value) { if (!isSaveValid()) { model.showClientForm(); @@ -1116,7 +1119,7 @@ self.invoice_items = ko.observableArray(); self.amount = ko.observable(0); self.balance = ko.observable(0); - self.invoice_design_id = ko.observable({{ $account->invoice_design_id }}); + self.invoice_design_id = ko.observable({{ $account->utf8_invoices ? 1 : $account->invoice_design_id }}); self.partial = ko.observable(0); self.is_partial = ko.observable(false);