Merge branch 'master' into develop
diff --git a/erpnext/__init__.py b/erpnext/__init__.py
index f11555f..eb7f91d 100644
--- a/erpnext/__init__.py
+++ b/erpnext/__init__.py
@@ -2,7 +2,7 @@
 from __future__ import unicode_literals
 import frappe
 
-__version__ = '8.0.39'
+__version__ = '8.0.40'
 
 
 def get_default_company(user=None):
diff --git a/erpnext/accounts/page/pos/pos.js b/erpnext/accounts/page/pos/pos.js
index 6a2e0cf..dc524e1 100644
--- a/erpnext/accounts/page/pos/pos.js
+++ b/erpnext/accounts/page/pos/pos.js
@@ -709,7 +709,7 @@
 				filter: function (item, input) {
 					var value = item.value.toLowerCase();
 					if (value.indexOf('is_action') !== -1 ||
-						value.indexOf(input) !== -1) {
+						value.indexOf(input.toLowerCase()) !== -1) {
 						return true;
 					}
 				},
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index a1c7697..a6ca3b6 100644
--- a/erpnext/accounts/party.py
+++ b/erpnext/accounts/party.py
@@ -366,8 +366,10 @@
 	
 def get_dashboard_info(party_type, party):
 	current_fiscal_year = get_fiscal_year(nowdate(), as_dict=True)
-	party_account_currency = get_party_account_currency(party_type, party, frappe.db.get_default("company"))
-	company_default_currency = get_default_currency()
+	company = frappe.db.get_default("company") or frappe.get_all("Company")[0].name
+	party_account_currency = get_party_account_currency(party_type, party, company)
+	company_default_currency = get_default_currency() \
+		or frappe.db.get_value('Company', company, 'default_currency')
 		
 	if party_account_currency==company_default_currency:
 		total_field = "base_grand_total"
diff --git a/erpnext/hr/doctype/offer_letter/offer_letter.js b/erpnext/hr/doctype/offer_letter/offer_letter.js
index 643eaa8..125a425 100755
--- a/erpnext/hr/doctype/offer_letter/offer_letter.js
+++ b/erpnext/hr/doctype/offer_letter/offer_letter.js
@@ -5,8 +5,10 @@
 
 frappe.ui.form.on("Offer Letter", {
 	select_terms: function(frm) {
-		frappe.model.get_value("Terms and Conditions", frm.doc.select_terms, "terms", function(value) {
-			frm.set_value("terms", value.terms);
+		erpnext.utils.get_terms(frm.doc.select_terms, frm.doc, function(r) {
+			if(!r.exc) {
+				me.frm.set_value("terms", r.message);
+			}
 		});
 	},
 
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index 2d0d83b..6357a52 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -993,20 +993,12 @@
 
 	get_terms: function() {
 		var me = this;
-		if(this.frm.doc.tc_name) {
-			return frappe.call({
-				method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
-				args: {
-					template_name: this.frm.doc.tc_name,
-					doc: this.frm.doc
-				},
-				callback: function(r) {
-					if(!r.exc) {
-						me.frm.set_value("terms", r.message);
-					}
-				}
-			});
-		}
+
+		erpnext.utils.get_terms(this.frm.doc.tc_name, this.frm.doc, function(r) {
+			if(!r.exc) {
+				me.frm.set_value("terms", r.message);
+			}
+		});
 	},
 
 	taxes_and_charges: function() {
diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js
index 3a2254e..d618fbe 100644
--- a/erpnext/public/js/utils.js
+++ b/erpnext/public/js/utils.js
@@ -104,6 +104,21 @@
 			}
 		}
 		refresh_field(table_fieldname);
+	},
+
+	get_terms: function(tc_name, doc, callback) {
+		if(tc_name) {
+			return frappe.call({
+				method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
+				args: {
+					template_name: tc_name,
+					doc: doc
+				},
+				callback: function(r) {
+					callback(r)
+				}
+			});
+		}
 	}
 });
 
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 b97378f..3ae61b9 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 @@
 				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 0eaf5ba..a2d3606 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 @@
 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 @@
 		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 930896c..eb8f8b8 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 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 @@
 
 		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 @@
 	lcv.set("taxes", [{
 		"description": "Insurance Charges",
 		"account": "_Test Account Insurance Charges - _TC",
-		"amount": 50
+		"amount": charges
 	}])
 
 	lcv.insert()
@@ -159,11 +181,15 @@
 	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')