diff --git a/app/filters.php b/app/filters.php index 129ab5df25d2..fed0bd40712e 100755 --- a/app/filters.php +++ b/app/filters.php @@ -24,8 +24,18 @@ App::before(function($request) if (Input::has('lang')) { $locale = Input::get('lang'); - Session::set(SESSION_LOCALE, $locale); App::setLocale($locale); + Session::set(SESSION_LOCALE, $locale); + + if (Auth::check()) + { + if ($language = Language::whereLocale($locale)->first()) + { + $account = Auth::user()->account; + $account->language_id = $language->id; + $account->save(); + } + } } else if (Auth::check()) { diff --git a/app/ninja/repositories/AccountRepository.php b/app/ninja/repositories/AccountRepository.php index 9fc3d25f8202..984f2264cb5f 100755 --- a/app/ninja/repositories/AccountRepository.php +++ b/app/ninja/repositories/AccountRepository.php @@ -24,9 +24,7 @@ class AccountRepository if (Session::has(SESSION_LOCALE)) { $locale = Session::get(SESSION_LOCALE); - $language = Language::whereLocale($locale)->first(); - - if ($language) + if ($language = Language::whereLocale($locale)->first()) { $account->language_id = $language->id; } diff --git a/app/views/invoices/edit.blade.php b/app/views/invoices/edit.blade.php index 0f8540b7e96d..6c3e6d2666e3 100755 --- a/app/views/invoices/edit.blade.php +++ b/app/views/invoices/edit.blade.php @@ -606,17 +606,12 @@ }); function applyComboboxListeners() { - $('.invoice-table input, .invoice-table select, .invoice-table textarea').on('blur', function() { - //if (value != $(this).val()) refreshPDF(); + var selectorStr = '.invoice-table input, .invoice-table select, .invoice-table textarea'; + $(selectorStr).off('blur').on('blur', function() { refreshPDF(); }); - var value; - $('.datalist').on('focus', function() { - value = $(this).val(); - }).on('blur', function() { - if (value != $(this).val()) refreshPDF(); - }).on('input', function() { + $('.datalist').on('input', function() { var key = $(this).val(); for (var i=0; iisPro() }}; + invoice.is_pro = {{ Auth::user()->isPro() ? 'true' : 'false' }}; @if (file_exists($account->getLogoPath())) invoice.image = "{{ HTML::image_data($account->getLogoPath()) }}"; @@ -641,17 +636,18 @@ invoice.imageHeight = {{ $account->getLogoHeight() }}; @endif - invoice.imageLogo1 = "{{ HTML::image_data('images/report_logo1.jpg') }}"; - invoice.imageLogoWidth1 =120; - invoice.imageLogoHeight1 = 40 + window.logoImages = {}; + logoImages.imageLogo1 = "{{ HTML::image_data('images/report_logo1.jpg') }}"; + logoImages.imageLogoWidth1 =120; + logoImages.imageLogoHeight1 = 40 - invoice.imageLogo2 = "{{ HTML::image_data('images/report_logo2.jpg') }}"; - invoice.imageLogoWidth2 =325/2; - invoice.imageLogoHeight2 = 81/2; + logoImages.imageLogo2 = "{{ HTML::image_data('images/report_logo2.jpg') }}"; + logoImages.imageLogoWidth2 =325/2; + logoImages.imageLogoHeight2 = 81/2; - invoice.imageLogo3 = "{{ HTML::image_data('images/report_logo3.jpg') }}"; - invoice.imageLogoWidth3 =325/2; - invoice.imageLogoHeight3 = 81/2; + logoImages.imageLogo3 = "{{ HTML::image_data('images/report_logo3.jpg') }}"; + logoImages.imageLogoWidth3 =325/2; + logoImages.imageLogoHeight3 = 81/2; return invoice; @@ -672,44 +668,48 @@ var isRefreshing = false; var needsRefresh = false; - function getPDFString() { + function getPDFString() { var invoice = createInvoiceModel(); var doc = generatePDF(invoice, invoiceLabels); if (!doc) return; return doc.output('datauristring'); } + function refreshPDF() { + console.log('refreshPDF'); if ({{ Auth::user()->force_pdfjs ? 'false' : 'true' }} && (isFirefox || (isChrome && !isChromium))) { var string = getPDFString(); + if (!string) return; $('#theFrame').attr('src', string).show(); } else { if (isRefreshing) { needsRefresh = true; return; } - isRefreshing = true; var string = getPDFString(); + if (!string) return; + isRefreshing = true; var pdfAsArray = convertDataURIToBinary(string); - PDFJS.getDocument(pdfAsArray).then(function getPdfHelloWorld(pdf) { + PDFJS.getDocument(pdfAsArray).then(function getPdfHelloWorld(pdf) { - pdf.getPage(1).then(function getPageHelloWorld(page) { - var scale = 1.5; - var viewport = page.getViewport(scale); + pdf.getPage(1).then(function getPageHelloWorld(page) { + var scale = 1.5; + var viewport = page.getViewport(scale); - var canvas = document.getElementById('theCanvas'); - var context = canvas.getContext('2d'); - canvas.height = viewport.height; - canvas.width = viewport.width; + var canvas = document.getElementById('theCanvas'); + var context = canvas.getContext('2d'); + canvas.height = viewport.height; + canvas.width = viewport.width; - page.render({canvasContext: context, viewport: viewport}); - $('#theCanvas').show(); - isRefreshing = false; - if (needsRefresh) { - needsRefresh = false; - refreshPDF(); - } - }); - }); + page.render({canvasContext: context, viewport: viewport}); + $('#theCanvas').show(); + isRefreshing = false; + if (needsRefresh) { + needsRefresh = false; + refreshPDF(); + } + }); + }); } } diff --git a/app/views/invoices/view.blade.php b/app/views/invoices/view.blade.php index b717752393c7..c3e07ba323db 100755 --- a/app/views/invoices/view.blade.php +++ b/app/views/invoices/view.blade.php @@ -29,18 +29,19 @@ $(function() { window.invoice = {{ $invoice->toJson() }}; + window.logoImages = {}; - invoice.imageLogo1 = "{{ HTML::image_data('images/report_logo1.jpg') }}"; - invoice.imageLogoWidth1 =120; - invoice.imageLogoHeight1 = 40 + logoImages.imageLogo1 = "{{ HTML::image_data('images/report_logo1.jpg') }}"; + logoImages.imageLogoWidth1 =120; + logoImages.imageLogoHeight1 = 40 - invoice.imageLogo2 = "{{ HTML::image_data('images/report_logo2.jpg') }}"; - invoice.imageLogoWidth2 =325/2; - invoice.imageLogoHeight2 = 81/2; + logoImages.imageLogo2 = "{{ HTML::image_data('images/report_logo2.jpg') }}"; + logoImages.imageLogoWidth2 =325/2; + logoImages.imageLogoHeight2 = 81/2; - invoice.imageLogo3 = "{{ HTML::image_data('images/report_logo3.jpg') }}"; - invoice.imageLogoWidth3 =325/2; - invoice.imageLogoHeight3 = 81/2; + logoImages.imageLogo3 = "{{ HTML::image_data('images/report_logo3.jpg') }}"; + logoImages.imageLogoWidth3 =325/2; + logoImages.imageLogoHeight3 = 81/2; @if (file_exists($invoice->client->account->getLogoPath())) invoice.image = "{{ HTML::image_data($invoice->client->account->getLogoPath()) }}"; @@ -50,7 +51,7 @@ var doc = generatePDF(invoice, true); if (!doc) return; var string = doc.output('datauristring'); - + if (isFirefox || (isChrome && !isChromium)) { $('#theFrame').attr('src', string).show(); } else { diff --git a/public/css/style.css b/public/css/style.css index 093b3e532a00..a313771b9943 100755 --- a/public/css/style.css +++ b/public/css/style.css @@ -75,7 +75,7 @@ th:last-child { tr {border: none;} th {border-left: 1px solid #d26b26; } .table>thead>tr>th, .table>tbody>tr>th, .table>tfoot>tr>th, .table>thead>tr>td, .table>tbody>tr>td, .table>tfoot>tr>td { -vertical-align: top; +vertical-align: middle; border-top: none; border-bottom: 1px solid #dfe0e1; } diff --git a/public/js/script.js b/public/js/script.js index 4fec766b62e0..6e9f9192c3eb 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -106,13 +106,35 @@ function GetReportTemplate4(doc, invoice, layout, checkMath) { } +var invoiceOld; function generatePDF(invoice, checkMath) { - invoice = calculateAmounts(invoice); - report_id=invoice.invoice_design_id; - doc= GetPdf(invoice,checkMath,report_id); - return doc; + console.log('generatePDF: %s', (JSON.stringify(invoice) == invoiceOld)); + //console.log(JSON.stringify(invoice)); + //console.log(invoiceOld); + invoice = calculateAmounts(invoice); + + var a = copyInvoice(invoice); + var b = copyInvoice(invoiceOld); + + console.log(JSON.stringify(a)); + console.log(JSON.stringify(b)); + if (_.isEqual(a, b)) { + return; + } + invoiceOld = invoice; + report_id = invoice.invoice_design_id; + doc = GetPdf(invoice, checkMath, report_id); + return doc; } +function copyInvoice(orig) { + if (!orig) return false; + var copy = JSON.stringify(orig); + //console.log(copy); + var copy = JSON.parse(copy); + //console.log(copy); + return copy; +} /* Handle converting variables in the invoices (ie, MONTH+1) */ function processVariables(str) { @@ -689,11 +711,11 @@ function GetReportTemplate1(doc, invoice, layout, checkMath) doc.addImage(invoice.image, 'JPEG', layout.marginLeft, 30); } - if (!invoice.is_pro && invoice.imageLogo1) + if (!invoice.is_pro && logoImages.imageLogo1) { pageHeight=820; - y=pageHeight-invoice.imageLogoHeight1; - doc.addImage(invoice.imageLogo1, 'JPEG', layout.marginLeft, y, invoice.imageLogoWidth1, invoice.imageLogoHeight1); + y=pageHeight-logoImages.imageLogoHeight1; + doc.addImage(logoImages.imageLogo1, 'JPEG', layout.marginLeft, y, logoImages.imageLogoWidth1, logoImages.imageLogoHeight1); } @@ -1044,15 +1066,15 @@ function Report2AddFooter (invoice,doc) doc.rect(x1, y1, w2, h2, 'FD'); - if (!invoice.is_pro && invoice.imageLogo2) + if (!invoice.is_pro && logoImages.imageLogo2) { pageHeight=820; var left = 250;//headerRight ; - y=pageHeight-invoice.imageLogoHeight2; + y=pageHeight-logoImages.imageLogoHeight2; var headerRight=370; - var left = headerRight - invoice.imageLogoWidth2; - doc.addImage(invoice.imageLogo2, 'JPEG', left, y, invoice.imageLogoWidth2, invoice.imageLogoHeight2); + var left = headerRight - logoImages.imageLogoWidth2; + doc.addImage(logoImages.imageLogo2, 'JPEG', left, y, logoImages.imageLogoWidth2, logoImages.imageLogoHeight2); } @@ -1085,17 +1107,15 @@ function Report3AddFooter (invoice, account, doc, layout) doc.rect(x1, y1, w2, h2, 'FD'); - if (!invoice.is_pro && invoice.imageLogo3) + if (!invoice.is_pro && logoImages.imageLogo3) { pageHeight=820; // var left = 25;//250;//headerRight ; - y=pageHeight-invoice.imageLogoHeight3; + y=pageHeight-logoImages.imageLogoHeight3; //var headerRight=370; //var left = headerRight - invoice.imageLogoWidth3; - doc.addImage(invoice.imageLogo3, 'JPEG', 40, y, invoice.imageLogoWidth3, invoice.imageLogoHeight3); - - + doc.addImage(logoImages.imageLogo3, 'JPEG', 40, y, logoImages.imageLogoWidth3, logoImages.imageLogoHeight3); } @@ -1227,12 +1247,12 @@ function Report3AddHeader (invoice,account,doc) function Report1AddNewPage(invoice,account,doc) { doc.addPage(); - if (invoice.imageLogo1) + if (logoImages.imageLogo1) { pageHeight=820; - y=pageHeight-invoice.imageLogoHeight1; + y=pageHeight-logoImages.imageLogoHeight1; var left = 20;//headerRight - invoice.imageLogoWidth1; - doc.addImage(invoice.imageLogo1, 'JPEG', left, y, invoice.imageLogoWidth1, invoice.imageLogoHeight1); + doc.addImage(logoImages.imageLogo1, 'JPEG', left, y, logoImages.imageLogoWidth1, logoImages.imageLogoHeight1); } @@ -1649,4 +1669,29 @@ function displayInvoiceItems(doc, invoice, layout) { } return y; +} + +// http://stackoverflow.com/questions/1068834/object-comparison-in-javascript +function objectEquals(x, y) { + // if both are function + if (x instanceof Function) { + if (y instanceof Function) { + return x.toString() === y.toString(); + } + return false; + } + if (x === null || x === undefined || y === null || y === undefined) { return x === y; } + if (x === y || x.valueOf() === y.valueOf()) { return true; } + + // if one of them is date, they must had equal valueOf + if (x instanceof Date) { return false; } + if (y instanceof Date) { return false; } + + // if they are not function or strictly equal, they both need to be Objects + if (!(x instanceof Object)) { return false; } + if (!(y instanceof Object)) { return false; } + + var p = Object.keys(x); + return Object.keys(y).every(function (i) { return p.indexOf(i) !== -1; }) ? + p.every(function (i) { return objectEquals(x[i], y[i]); }) : false; } \ No newline at end of file