From 36886699632d83df908eda5e22e319d60f55a8ea Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 1 Jun 2014 16:35:19 +0300 Subject: [PATCH] Bug fixes --- app/controllers/AccountController.php | 2 +- app/controllers/ClientController.php | 2 +- app/controllers/InvoiceController.php | 2 +- app/controllers/QuoteController.php | 2 +- app/database/seeds/ConstantsSeeder.php | 2 + app/ninja/repositories/InvoiceRepository.php | 4 +- app/views/invoices/edit.blade.php | 76 ++++++++++---------- public/built.js | 13 ++-- public/js/script.js | 13 ++-- 9 files changed, 63 insertions(+), 53 deletions(-) diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index 406c48540d18..1e90afc3f953 100755 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -86,7 +86,7 @@ class AccountController extends \BaseController { 'account' => Account::with('users')->findOrFail(Auth::user()->account_id), 'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), 'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), - 'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), + 'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), 'timezones' => Timezone::remember(DEFAULT_QUERY_CACHE)->orderBy('location')->get(), 'dateFormats' => DateFormat::remember(DEFAULT_QUERY_CACHE)->get(), 'datetimeFormats' => DatetimeFormat::remember(DEFAULT_QUERY_CACHE)->get(), diff --git a/app/controllers/ClientController.php b/app/controllers/ClientController.php index 08b878c8bd22..e4a69711bac7 100755 --- a/app/controllers/ClientController.php +++ b/app/controllers/ClientController.php @@ -154,7 +154,7 @@ class ClientController extends \BaseController { return [ 'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), 'paymentTerms' => PaymentTerm::remember(DEFAULT_QUERY_CACHE)->orderBy('num_days')->get(['name', 'num_days']), - 'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), + 'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), 'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), 'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), 'customLabel1' => Auth::user()->account->custom_client_label1, diff --git a/app/controllers/InvoiceController.php b/app/controllers/InvoiceController.php index 265f7cd5b990..83aaa340f0dd 100755 --- a/app/controllers/InvoiceController.php +++ b/app/controllers/InvoiceController.php @@ -241,7 +241,7 @@ class InvoiceController extends \BaseController { 'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), 'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), 'paymentTerms' => PaymentTerm::remember(DEFAULT_QUERY_CACHE)->orderBy('num_days')->get(['name', 'num_days']), - 'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), + 'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), 'invoiceDesigns' => InvoiceDesign::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), 'invoiceLabels' => Auth::user()->account->getInvoiceLabels(), 'frequencies' => array( diff --git a/app/controllers/QuoteController.php b/app/controllers/QuoteController.php index 202472412398..dbdc7b63a643 100644 --- a/app/controllers/QuoteController.php +++ b/app/controllers/QuoteController.php @@ -94,7 +94,7 @@ class QuoteController extends \BaseController { 'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), 'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), 'paymentTerms' => PaymentTerm::remember(DEFAULT_QUERY_CACHE)->orderBy('num_days')->get(['name', 'num_days']), - 'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), + 'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(), 'invoiceDesigns' => InvoiceDesign::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(), 'invoiceLabels' => Auth::user()->account->getInvoiceLabels() ]; diff --git a/app/database/seeds/ConstantsSeeder.php b/app/database/seeds/ConstantsSeeder.php index 17671a6fbe07..e729e7212938 100755 --- a/app/database/seeds/ConstantsSeeder.php +++ b/app/database/seeds/ConstantsSeeder.php @@ -105,6 +105,7 @@ class ConstantsSeeder extends Seeder Industry::create(array('name' => 'Transportation')); Industry::create(array('name' => 'Travel & Luxury')); Industry::create(array('name' => 'Other')); + Industry::create(array('name' => 'Photography')); Size::create(array('name' => '1 - 3')); Size::create(array('name' => '4 - 10')); @@ -119,6 +120,7 @@ class ConstantsSeeder extends Seeder PaymentTerm::create(array('num_days' => 15, 'name' => 'Net 15')); PaymentTerm::create(array('num_days' => 30, 'name' => 'Net 30')); PaymentTerm::create(array('num_days' => 60, 'name' => 'Net 60')); + PaymentTerm::create(array('num_days' => 90, 'name' => 'Net 90')); Currency::create(array('name' => 'US Dollar', 'code' => 'USD', 'symbol' => '$', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.')); Currency::create(array('name' => 'Pound Sterling', 'code' => 'GBP', 'symbol' => '£', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.')); diff --git a/app/ninja/repositories/InvoiceRepository.php b/app/ninja/repositories/InvoiceRepository.php index c9152e3802df..9b855782b44a 100755 --- a/app/ninja/repositories/InvoiceRepository.php +++ b/app/ninja/repositories/InvoiceRepository.php @@ -251,7 +251,8 @@ class InvoiceRepository } $lineTotal = $invoiceItemCost * $invoiceItemQty; - $total += $lineTotal + ($lineTotal * $invoiceItemTaxRate / 100); + + $total += round($lineTotal + ($lineTotal * $invoiceItemTaxRate / 100), 2); } if ($invoice->discount > 0) @@ -260,6 +261,7 @@ class InvoiceRepository } $total += $total * $invoice->tax_rate / 100; + $total = round($total, 2); if ($publicId) { diff --git a/app/views/invoices/edit.blade.php b/app/views/invoices/edit.blade.php index 735c2f2c708a..7d5fa82d5822 100755 --- a/app/views/invoices/edit.blade.php +++ b/app/views/invoices/edit.blade.php @@ -1095,7 +1095,7 @@ var total = 0; for(var p=0; p < self.invoice_items().length; ++p) { var item = self.invoice_items()[p]; - total += item.totals.rawTotal(); + total += item.totals.rawTotal(); } return total; }); @@ -1106,7 +1106,7 @@ }); this.totals.rawDiscounted = ko.computed(function() { - return self.totals.rawSubtotal() * (self.discount()/100); + return roundToTwo(self.totals.rawSubtotal() * (self.discount()/100)); }); this.totals.discounted = ko.computed(function() { @@ -1118,12 +1118,12 @@ var discount = parseFloat(self.discount()); if (discount > 0) { - total = total * ((100 - discount)/100); + total = roundToTwo(total * ((100 - discount)/100)); } var taxRate = parseFloat(self.tax_rate()); if (taxRate > 0) { - var tax = total * (taxRate/100); + var tax = roundToTwo(total * (taxRate/100)); return formatMoney(tax, self.client().currency_id()); } else { return formatMoney(0); @@ -1140,29 +1140,29 @@ }); this.totals.total = ko.computed(function() { - var total = accounting.toFixed(self.totals.rawSubtotal(),2); + var total = accounting.toFixed(self.totals.rawSubtotal(),2); - var discount = parseFloat(self.discount()); - if (discount > 0) { - total = total * ((100 - discount)/100); - } + var discount = parseFloat(self.discount()); + if (discount > 0) { + total = roundToTwo(total * ((100 - discount)/100)); + } var taxRate = parseFloat(self.tax_rate()); if (taxRate > 0) { - total = NINJA.parseFloat(total) + (total * (taxRate/100)); - } + total = NINJA.parseFloat(total) + roundToTwo((total * (taxRate/100))); + } - var paid = self.totals.rawPaidToDate(); - if (paid > 0) { - total -= paid; - } + var paid = self.totals.rawPaidToDate(); + if (paid > 0) { + total -= paid; + } - return total != 0 ? formatMoney(total, self.client().currency_id()) : ''; - }); + return total != 0 ? formatMoney(total, self.client().currency_id()) : ''; + }); - self.onDragged = function(item) { - refreshPDF(); - } + self.onDragged = function(item) { + refreshPDF(); + } } function ClientModel(data) { @@ -1392,12 +1392,12 @@ var cost = NINJA.parseFloat(self.cost()); var qty = NINJA.parseFloat(self.qty()); var taxRate = NINJA.parseFloat(self.tax_rate()); - var value = cost * qty; - if (taxRate > 0) { - value += value * (taxRate/100); - } - return value ? value : ''; - }); + var value = cost * qty; + if (taxRate > 0) { + value += value * (taxRate/100); + } + return value ? roundToTwo(value) : ''; + }); this.totals.total = ko.computed(function() { var total = self.totals.rawTotal(); @@ -1406,22 +1406,22 @@ } else { return total ? formatMoney(total, 1) : ''; } - }); + }); - this.hideActions = function() { - this.actionsVisible(false); - } + this.hideActions = function() { + this.actionsVisible(false); + } - this.showActions = function() { - this.actionsVisible(true); - } + this.showActions = function() { + this.actionsVisible(true); + } - this.isEmpty = function() { - return !self.product_key() && !self.notes() && !self.cost() && !self.qty(); - } + this.isEmpty = function() { + return !self.product_key() && !self.notes() && !self.cost() && !self.qty(); + } - this.onSelect = function(){ - } + this.onSelect = function(){ + } } function onItemChange() diff --git a/public/built.js b/public/built.js index 20206cde8c3d..ba92a50b794f 100644 --- a/public/built.js +++ b/public/built.js @@ -39195,7 +39195,7 @@ function calculateAmounts(invoice) { var lineTotal = NINJA.parseFloat(item.cost) * NINJA.parseFloat(item.qty); if (tax) { - lineTotal += lineTotal * tax / 100; + lineTotal += roundToTwo(lineTotal * tax / 100); } if (lineTotal) { total += lineTotal; @@ -39210,7 +39210,7 @@ function calculateAmounts(invoice) { if (invoice.discount > 0) { - var discount = total * (invoice.discount/100); + var discount = roundToTwo(total * (invoice.discount/100)); total -= discount; } @@ -39219,14 +39219,14 @@ function calculateAmounts(invoice) { tax = parseFloat(invoice.tax.rate); } else if (invoice.tax_rate && parseFloat(invoice.tax_rate)) { tax = parseFloat(invoice.tax_rate); - } + } if (tax) { - var tax = total * (tax/100); + var tax = roundToTwo(total * (tax/100)); total = parseFloat(total) + parseFloat(tax); } - invoice.balance_amount = accounting.toFixed(total,2) - (accounting.toFixed(invoice.amount,2) - accounting.toFixed(invoice.balance,2)); + invoice.balance_amount = roundToTwo(total) - roundToTwo(invoice.amount) - roundToTwo(invoice.balance); invoice.tax_amount = tax; invoice.discount_amount = discount; invoice.has_taxes = hasTaxes; @@ -39463,3 +39463,6 @@ function toggleDatePicker(field) { $('#'+field).datepicker('show'); } +function roundToTwo(num) { + return +(Math.round(num + "e+2") + "e-2"); +} diff --git a/public/js/script.js b/public/js/script.js index 433d44dd7c53..a8c1ab9cd909 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -1479,7 +1479,7 @@ function calculateAmounts(invoice) { var lineTotal = NINJA.parseFloat(item.cost) * NINJA.parseFloat(item.qty); if (tax) { - lineTotal += lineTotal * tax / 100; + lineTotal += roundToTwo(lineTotal * tax / 100); } if (lineTotal) { total += lineTotal; @@ -1494,7 +1494,7 @@ function calculateAmounts(invoice) { if (invoice.discount > 0) { - var discount = total * (invoice.discount/100); + var discount = roundToTwo(total * (invoice.discount/100)); total -= discount; } @@ -1503,14 +1503,14 @@ function calculateAmounts(invoice) { tax = parseFloat(invoice.tax.rate); } else if (invoice.tax_rate && parseFloat(invoice.tax_rate)) { tax = parseFloat(invoice.tax_rate); - } + } if (tax) { - var tax = total * (tax/100); + var tax = roundToTwo(total * (tax/100)); total = parseFloat(total) + parseFloat(tax); } - invoice.balance_amount = accounting.toFixed(total,2) - (accounting.toFixed(invoice.amount,2) - accounting.toFixed(invoice.balance,2)); + invoice.balance_amount = roundToTwo(total) - roundToTwo(invoice.amount) - roundToTwo(invoice.balance); invoice.tax_amount = tax; invoice.discount_amount = discount; invoice.has_taxes = hasTaxes; @@ -1747,3 +1747,6 @@ function toggleDatePicker(field) { $('#'+field).datepicker('show'); } +function roundToTwo(num) { + return +(Math.round(num + "e+2") + "e-2"); +}