diff --git a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js index b97378f6d8..3ae61b9020 100644 --- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js +++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js @@ -102,9 +102,17 @@ erpnext.stock.LandedCostVoucher = erpnext.stock.StockController.extend({ total_item_cost += flt(d[based_on]) }); + var total_charges = 0.0; $.each(this.frm.doc.items || [], function(i, item) { item.applicable_charges = flt(item[based_on]) * flt(me.frm.doc.total_taxes_and_charges) / flt(total_item_cost) + item.applicable_charges = flt(item.applicable_charges, precision("applicable_charges", item)) + total_charges += item.applicable_charges }); + + if (total_charges != this.frm.doc.total_taxes_and_charges){ + var diff = this.frm.doc.total_taxes_and_charges - flt(total_charges) + this.frm.doc.items.slice(-1)[0].applicable_charges += diff + } refresh_field("items"); } }, diff --git a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py index 0eaf5ba8ca..a2d3606a89 100644 --- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py +++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals import frappe from frappe import _ from frappe.utils import flt +from frappe.model.meta import get_field_precision from frappe.model.document import Document from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos @@ -71,7 +72,17 @@ class LandedCostVoucher(Document): if not total: frappe.throw(_("Total {0} for all items is zero, may be you should change 'Distribute Charges Based On'").format(based_on)) - if self.total_taxes_and_charges != sum([flt(d.applicable_charges) for d in self.get("items")]): + total_applicable_charges = sum([flt(d.applicable_charges) for d in self.get("items")]) + + precision = get_field_precision(frappe.get_meta("Landed Cost Item").get_field("applicable_charges"), + currency=frappe.db.get_value("Company", self.company, "default_currency", cache=True)) + + diff = flt(self.total_taxes_and_charges) - flt(total_applicable_charges) + diff = flt(diff, precision) + + if abs(diff) < (2.0 / (10**precision)): + self.items[-1].applicable_charges += diff + else: frappe.throw(_("Total Applicable Charges in Purchase Receipt Items table must be same as Total Taxes and Charges")) diff --git a/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py index 930896c91c..eb8f8b84f0 100644 --- a/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py +++ b/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py @@ -7,7 +7,7 @@ import unittest import frappe from frappe.utils import flt from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt \ - import set_perpetual_inventory, get_gl_entries, test_records as pr_test_records + import set_perpetual_inventory, get_gl_entries, test_records as pr_test_records, make_purchase_receipt from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice class TestLandedCostVoucher(unittest.TestCase): @@ -133,7 +133,29 @@ class TestLandedCostVoucher(unittest.TestCase): set_perpetual_inventory(0) -def submit_landed_cost_voucher(receipt_document_type, receipt_document): + def test_landed_cost_voucher_for_odd_numbers (self): + set_perpetual_inventory(1) + + pr = make_purchase_receipt(do_not_save=True) + pr.items[0].cost_center = "_Test Company - _TC" + for x in range(2): + pr.append("items", { + "item_code": "_Test Item", + "warehouse": "_Test Warehouse - _TC", + "cost_center": "_Test Company - _TC", + "qty": 5, + "rate": 50 + }) + pr.submit() + + lcv = submit_landed_cost_voucher("Purchase Receipt", pr.name, 123.22) + + self.assertEquals(lcv.items[0].applicable_charges, 41.07) + self.assertEquals(lcv.items[2].applicable_charges, 41.08) + + set_perpetual_inventory(0) + +def submit_landed_cost_voucher(receipt_document_type, receipt_document, charges=50): ref_doc = frappe.get_doc(receipt_document_type, receipt_document) lcv = frappe.new_doc("Landed Cost Voucher") @@ -151,7 +173,7 @@ def submit_landed_cost_voucher(receipt_document_type, receipt_document): lcv.set("taxes", [{ "description": "Insurance Charges", "account": "_Test Account Insurance Charges - _TC", - "amount": 50 + "amount": charges }]) lcv.insert() @@ -159,11 +181,15 @@ def submit_landed_cost_voucher(receipt_document_type, receipt_document): distribute_landed_cost_on_items(lcv) lcv.submit() + + return lcv def distribute_landed_cost_on_items(lcv): based_on = lcv.distribute_charges_based_on.lower() total = sum([flt(d.get(based_on)) for d in lcv.get("items")]) + for item in lcv.get("items"): item.applicable_charges = flt(item.get(based_on)) * flt(lcv.total_taxes_and_charges) / flt(total) + item.applicable_charges = flt(item.applicable_charges, lcv.precision("applicable_charges", item)) test_records = frappe.get_test_records('Landed Cost Voucher')