From 39384d36ea619687e6f984d5beba10b68570d1b9 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Sat, 11 May 2013 19:39:53 +0530 Subject: [PATCH] [precision] server side cleanup and client side precision method --- .../purchase_invoice/purchase_invoice.py | 6 +-- controllers/buying_controller.py | 48 +++++++++---------- controllers/selling_controller.py | 38 ++++++++------- public/js/utils.js | 5 +- 4 files changed, 48 insertions(+), 49 deletions(-) diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py index f7220bf214..b63a176eba 100644 --- a/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -466,9 +466,9 @@ class DocType(BuyingController): # expense will be booked in sales invoice stock_item_and_auto_inventory_accounting = True - valuation_amt = (flt(item.amount, self.precision("amount", item.parentfield)) + - flt(item.item_tax_amount, self.precision("item_tax_amount", item.parentfield)) + - flt(item.rm_supp_cost, self.precision("rm_supp_cost", item.parentfield))) + valuation_amt = (flt(item.amount, self.precision("amount", item)) + + flt(item.item_tax_amount, self.precision("item_tax_amount", item)) + + flt(item.rm_supp_cost, self.precision("rm_supp_cost", item))) gl_entries.append( self.get_gl_dict({ diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py index bc219ff184..30c3dbbefa 100644 --- a/controllers/buying_controller.py +++ b/controllers/buying_controller.py @@ -137,8 +137,8 @@ class BuyingController(StockController): def _set_base(item, print_field, base_field): """set values in base currency""" item.fields[base_field] = flt((flt(item.fields[print_field], - self.precision(print_field, item.parentfield)) * self.doc.conversion_rate), - self.precision(base_field, item.parentfield)) + self.precision(print_field, item)) * self.doc.conversion_rate), + self.precision(base_field, item)) # hack! - cleaned up in _cleanup() if self.doc.doctype != "Purchase Invoice": @@ -150,7 +150,7 @@ class BuyingController(StockController): if self.doc.doctype != "Purchase Invoice": item.rate = item.purchase_rate - self.round_floats_in_doc(item, item.parentfield) + self.round_floats_in(item) if item.discount_rate == 100: item.import_ref_rate = item.import_ref_rate or item.import_rate @@ -158,14 +158,14 @@ class BuyingController(StockController): else: if item.import_ref_rate: item.import_rate = flt(item.import_ref_rate * (1.0 - (item.discount_rate / 100.0)), - self.precision("import_rate", item.parentfield)) + self.precision("import_rate", item)) else: # assume that print rate and discount_rate are specified item.import_ref_rate = flt(item.import_rate / (1.0 - (item.discount_rate / 100.0)), - self.precision("import_ref_rate", item.parentfield)) + self.precision("import_ref_rate", item)) item.import_amount = flt(item.import_rate * item.qty, - self.precision("import_amount", item.parentfield)) + self.precision("import_amount", item)) _set_base(item, "import_ref_rate", "purchase_ref_rate") _set_base(item, "import_rate", "rate") @@ -183,7 +183,7 @@ class BuyingController(StockController): self.validate_on_previous_row(tax) - self.round_floats_in_doc(tax, tax.parentfield) + self.round_floats_in(tax) def calculate_net_total(self): self.doc.net_total = 0 @@ -213,7 +213,7 @@ class BuyingController(StockController): # and tax.grand_total_for_current_item for the first such iteration if not (current_tax_amount or self.doc.net_total or tax.tax_amount) and \ tax.charge_type=="Actual": - zero_net_total_adjustment = flt(tax.rate, self.precision("tax_amount", tax.parentfield)) + zero_net_total_adjustment = flt(tax.rate, self.precision("tax_amount", tax)) current_tax_amount += zero_net_total_adjustment # store tax_amount for current item as it will be used for @@ -235,12 +235,12 @@ class BuyingController(StockController): # item's amount, previously applied tax and the current tax on that item if i==0: tax.grand_total_for_current_item = flt(item.amount + - current_tax_amount, self.precision("total", tax.parentfield)) + current_tax_amount, self.precision("total", tax)) else: tax.grand_total_for_current_item = \ flt(self.tax_doclist[i-1].grand_total_for_current_item + - current_tax_amount, self.precision("total", tax.parentfield)) + current_tax_amount, self.precision("total", tax)) # in tax.total, accumulate grand total of each item tax.total += tax.grand_total_for_current_item @@ -324,7 +324,7 @@ class BuyingController(StockController): if tax.charge_type == "Actual": # distribute the tax amount proportionally to each item row - actual = flt(tax.rate, self.precision("tax_amount", tax.parentfield)) + actual = flt(tax.rate, self.precision("tax_amount", tax)) current_tax_amount = (self.doc.net_total and ((item.amount / self.doc.net_total) * actual) or 0) @@ -337,11 +337,11 @@ class BuyingController(StockController): current_tax_amount = (tax_rate / 100.0) * \ self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item - return flt(current_tax_amount, self.precision("tax_amount", tax.parentfield)) + return flt(current_tax_amount, self.precision("tax_amount", tax)) def _get_tax_rate(self, tax, item_tax_map): if item_tax_map.has_key(tax.account_head): - return flt(item_tax_map.get(tax.account_head), self.precision("rate", tax.parentfield)) + return flt(item_tax_map.get(tax.account_head), self.precision("rate", tax)) else: return tax.rate @@ -355,7 +355,7 @@ class BuyingController(StockController): if tax.category in ["Valuation", "Valuation and Total"] and \ item.item_code in self.stock_items: item.item_tax_amount += flt(current_tax_amount, - self.precision("item_tax_amount", item.parentfield)) + self.precision("item_tax_amount", item)) # update valuation rate def update_valuation_rate(self, parentfield): @@ -363,21 +363,17 @@ class BuyingController(StockController): item.conversion_factor = item.conversion_factor or flt(webnotes.conn.get_value( "UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom}, "conversion_factor")) or 1 + if item.item_code and item.qty: - if self.doc.doctype == "Purchase Invoice": - purchase_rate = flt(item.rate, self.precision("rate", item.parentfield)) - else: - purchase_rate = flt(item.purchase_rate, self.precision("purchase_rate", item.parentfield)) - + self.round_floats_in(item) + + purchase_rate = item.rate if self.doc.doctype == "Purchase Invoice" else item.purchase_rate + # if no item code, which is sometimes the case in purchase invoice, # then it is not possible to track valuation against it - item.valuation_rate = flt( - (purchase_rate + \ - (flt(item.item_tax_amount, self.precision("item_tax_amount", item.parentfield)) + - flt(item.rm_supp_cost, self.precision("rm_supp_cost", item.parentfield)) - ) / flt(item.qty, self.precision("qty", item.parentfield)) - ) / flt(item.conversion_factor, self.precision("conversion_factor", item.parentfield)), - self.precision("valuation_rate", item.parentfield)) + item.valuation_rate = flt((purchase_rate + + (item.item_tax_amount + item.rm_supp_cost) / item.qty) / item.conversion_factor, + self.precision("valuation_rate", item)) else: item.valuation_rate = 0.0 diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py index 020e2af315..36d9d8ed7c 100644 --- a/controllers/selling_controller.py +++ b/controllers/selling_controller.py @@ -92,7 +92,11 @@ class SellingController(StockController): self.calculate_taxes() self.calculate_totals() # self.calculate_outstanding_amount() - # + + # TODO + # allocated amount of sales person + # total commission + self._cleanup() def determin_exclusive_rate(self): @@ -122,12 +126,12 @@ class SellingController(StockController): if cumulated_tax_fraction: item.basic_rate = flt((item.export_rate * self.doc.conversion_rate) / - (1 + cumulated_tax_fraction), self.precision("basic_rate", item.parentfield)) + (1 + cumulated_tax_fraction), self.precision("basic_rate", item)) - item.amount = flt(item.basic_rate * item.qty, self.precision("amount", item.parentfield)) + item.amount = flt(item.basic_rate * item.qty, self.precision("amount", item)) item.base_ref_rate = flt(item.basic_rate / (1 - (item.adj_rate / 100.0)), - self.precision("base_ref_rate", item.parentfield)) + self.precision("base_ref_rate", item)) def get_current_tax_fraction(self, tax, item_tax_map): """ @@ -156,11 +160,11 @@ class SellingController(StockController): def _set_base(item, print_field, base_field): """set values in base currency""" item.fields[base_field] = flt((flt(item.fields[print_field], - self.precision(print_field, item.parentfield)) * self.doc.conversion_rate), - self.precision(base_field, item.parentfield)) + self.precision(print_field, item)) * self.doc.conversion_rate), + self.precision(base_field, item)) for item in self.item_doclist: - self.round_floats_in_doc(item, item.parentfield) + self.round_floats_in(item) if item.adj_rate == 100: item.ref_rate = item.ref_rate or item.export_rate @@ -168,14 +172,14 @@ class SellingController(StockController): else: if item.ref_rate: item.export_rate = flt(item.ref_rate * (1.0 - (item.adj_rate / 100.0)), - self.precision("export_rate", item.parentfield)) + self.precision("export_rate", item)) else: # assume that print rate and discount are specified item.ref_rate = flt(item.export_rate / (1.0 - (item.adj_rate / 100.0)), - self.precision("ref_rate", item.parentfield)) + self.precision("ref_rate", item)) item.export_amount = flt(item.export_rate * item.qty, - self.precision("export_amount", item.parentfield)) + self.precision("export_amount", item)) _set_base(item, "ref_rate", "base_ref_rate") _set_base(item, "export_rate", "basic_rate") @@ -189,7 +193,7 @@ class SellingController(StockController): tax.item_wise_tax_detail = {} self.validate_on_previous_row(tax) self.validate_inclusive_tax(tax) - self.round_floats_in_doc(tax, tax.parentfield) + self.round_floats_in(tax) def calculate_net_total(self): self.doc.net_total = 0 @@ -216,7 +220,7 @@ class SellingController(StockController): # and tax.grand_total_for_current_item for the first such iteration if not (current_tax_amount or self.doc.net_total or tax.tax_amount) and \ tax.charge_type=="Actual": - zero_net_total_adjustment = flt(tax.rate, self.precision("tax_amount", tax.parentfield)) + zero_net_total_adjustment = flt(tax.rate, self.precision("tax_amount", tax)) current_tax_amount += zero_net_total_adjustment # store tax_amount for current item as it will be used for @@ -231,12 +235,12 @@ class SellingController(StockController): # item's amount, previously applied tax and the current tax on that item if i==0: tax.grand_total_for_current_item = flt(item.amount + - current_tax_amount, self.precision("total", tax.parentfield)) + current_tax_amount, self.precision("total", tax)) else: tax.grand_total_for_current_item = \ flt(self.tax_doclist[i-1].grand_total_for_current_item + - current_tax_amount, self.precision("total", tax.parentfield)) + current_tax_amount, self.precision("total", tax)) # in tax.total, accumulate grand total of each item tax.total += tax.grand_total_for_current_item @@ -259,7 +263,7 @@ class SellingController(StockController): if tax.charge_type == "Actual": # distribute the tax amount proportionally to each item row - actual = flt(tax.rate, self.precision("tax_amount", tax.parentfield)) + actual = flt(tax.rate, self.precision("tax_amount", tax)) current_tax_amount = (self.doc.net_total and ((item.amount / self.doc.net_total) * actual) or 0) @@ -272,7 +276,7 @@ class SellingController(StockController): current_tax_amount = (tax_rate / 100.0) * \ self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item - return flt(current_tax_amount, self.precision("tax_amount", tax.parentfield)) + return flt(current_tax_amount, self.precision("tax_amount", tax)) def validate_on_previous_row(self, tax): """ @@ -334,7 +338,7 @@ class SellingController(StockController): def _get_tax_rate(self, tax, item_tax_map): if item_tax_map.has_key(tax.account_head): - return flt(item_tax_map.get(tax.account_head), self.precision("rate", tax.parentfield)) + return flt(item_tax_map.get(tax.account_head), self.precision("rate", tax)) else: return tax.rate diff --git a/public/js/utils.js b/public/js/utils.js index 4df9555ae9..0284670761 100644 --- a/public/js/utils.js +++ b/public/js/utils.js @@ -13,8 +13,7 @@ // // You should have received a copy of the GNU General Public License // along with this program. If not, see . - -wn.provide('erpnext.utils'); +wn.provide("erpnext.utils"); erpnext.get_currency = function(company) { if(!company && cur_frm) @@ -23,4 +22,4 @@ erpnext.get_currency = function(company) { return wn.model.get(":Company", company).default_currency || wn.boot.sysdefaults.currency; else return wn.boot.sysdefaults.currency; -} \ No newline at end of file +}