Merge branch 'develop' of https://github.com/frappe/erpnext into lcv_multicurrency
diff --git a/erpnext/accounts/doctype/account/test_account.py b/erpnext/accounts/doctype/account/test_account.py
index 113bea0..533eda3 100644
--- a/erpnext/accounts/doctype/account/test_account.py
+++ b/erpnext/accounts/doctype/account/test_account.py
@@ -254,7 +254,8 @@
account_name = kwargs.get('account_name'),
account_type = kwargs.get('account_type'),
parent_account = kwargs.get('parent_account'),
- company = kwargs.get('company')
+ company = kwargs.get('company'),
+ account_currency = kwargs.get('account_currency')
))
account.save()
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 32c5d3a..49c9f8c 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -442,7 +442,7 @@
account_currency = get_account_currency(gl_dict.account)
if gl_dict.account and self.doctype not in ["Journal Entry",
- "Period Closing Voucher", "Payment Entry"]:
+ "Period Closing Voucher", "Payment Entry", "Purchase Receipt", "Purchase Invoice"]:
self.validate_account_currency(gl_dict.account, account_currency)
set_balance_in_account_currency(gl_dict, account_currency, self.get("conversion_rate"),
self.company_currency)
diff --git a/erpnext/stock/doctype/landed_cost_taxes_and_charges/landed_cost_taxes_and_charges.json b/erpnext/stock/doctype/landed_cost_taxes_and_charges/landed_cost_taxes_and_charges.json
index 64331c7..0861224 100644
--- a/erpnext/stock/doctype/landed_cost_taxes_and_charges/landed_cost_taxes_and_charges.json
+++ b/erpnext/stock/doctype/landed_cost_taxes_and_charges/landed_cost_taxes_and_charges.json
@@ -6,8 +6,11 @@
"engine": "InnoDB",
"field_order": [
"expense_account",
+ "account_currency",
+ "exchange_rate",
"description",
"col_break3",
+ "base_amount",
"amount"
],
"fields": [
@@ -28,7 +31,7 @@
"fieldtype": "Currency",
"in_list_view": 1,
"label": "Amount",
- "options": "Company:company:default_currency",
+ "options": "account_currency",
"reqd": 1
},
{
@@ -39,9 +42,32 @@
"label": "Expense Account",
"mandatory_depends_on": "eval:cint(erpnext.is_perpetual_inventory_enabled(parent.company))",
"options": "Account",
- "print_hide": 1
+ "reqd": 1
+ },
+ {
+ "fieldname": "account_currency",
+ "fieldtype": "Link",
+ "label": "Account Currency",
+ "options": "Currency",
+ "read_only": 1,
+ "reqd": 1
+ },
+ {
+ "fieldname": "exchange_rate",
+ "fieldtype": "Float",
+ "label": "Exchange Rate",
+ "precision": "9"
+ },
+ {
+ "fieldname": "base_amount",
+ "fieldtype": "Currency",
+ "label": "Base Amount",
+ "options": "Company:company:default_currency",
+ "read_only": 1,
+ "reqd": 1
}
],
+ "index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2020-12-04 00:22:14.373312",
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 5de1352..c817f05 100644
--- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js
+++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.js
@@ -32,9 +32,8 @@
this.frm.set_query("expense_account", "taxes", function() {
return {
- query: "erpnext.controllers.queries.tax_account_query",
filters: {
- "account_type": ["Tax", "Chargeable", "Income Account", "Expenses Included In Valuation", "Expenses Included In Asset Valuation"],
+ "account_type": ['in', ["Tax", "Chargeable", "Income Account", "Expenses Included In Valuation", "Expenses Included In Asset Valuation"]],
"company": me.frm.doc.company
}
};
@@ -97,9 +96,9 @@
set_total_taxes_and_charges: function() {
var total_taxes_and_charges = 0.0;
$.each(this.frm.doc.taxes || [], function(i, d) {
- total_taxes_and_charges += flt(d.amount)
+ total_taxes_and_charges += flt(d.base_amount);
});
- cur_frm.set_value("total_taxes_and_charges", total_taxes_and_charges);
+ this.frm.set_value("total_taxes_and_charges", total_taxes_and_charges);
},
set_applicable_charges_for_item: function() {
@@ -134,7 +133,63 @@
items_remove: () => {
this.trigger('set_applicable_charges_for_item');
}
-
});
cur_frm.script_manager.make(erpnext.stock.LandedCostVoucher);
+
+frappe.ui.form.on('Landed Cost Voucher', {
+ set_account_currency: function(frm, cdt, cdn) {
+ let row = locals[cdt][cdn];
+ if (row.expense_account) {
+ frappe.db.get_value('Account', row.expense_account, 'account_currency', function(value) {
+ frappe.model.set_value(cdt, cdn, "account_currency", value.account_currency);
+ frm.events.set_exchange_rate(frm, cdt, cdn);
+ });
+ }
+ },
+
+ onload: function(frm) {
+ let company_currency = frappe.get_doc(":Company", frm.doc.company).default_currency;
+ frm.set_currency_labels(["total_taxes_and_charges"], company_currency);
+ },
+
+ set_exchange_rate: function(frm, cdt, cdn) {
+ let row = locals[cdt][cdn];
+ let company_currency = frappe.get_doc(":Company", frm.doc.company).default_currency;
+
+ if (row.account_currency == company_currency) {
+ row.exchange_rate = 1;
+ } else if (!row.exchange_rate || row.exchange_rate == 1) {
+ frappe.call({
+ method: "erpnext.accounts.doctype.journal_entry.journal_entry.get_exchange_rate",
+ args: {
+ posting_date: frm.doc.posting_date,
+ account: row.expense_account,
+ account_currency: row.account_currency,
+ company: frm.doc.company
+ },
+ callback: function(r) {
+ if (r.message) {
+ frappe.model.set_value(cdt, cdn, "exchange_rate", r.message);
+ }
+ }
+ });
+ }
+ },
+
+ set_base_amount: function(frm, cdt, cdn) {
+ let row = locals[cdt][cdn];
+ frappe.model.set_value(cdt, cdn, "base_amount",
+ flt(flt(row.amount)*row.exchange_rate, precision("base_amount", row)));
+ }
+});
+
+frappe.ui.form.on('Landed Cost Taxes and Charges', {
+ expense_account: function(frm, cdt, cdn) {
+ frm.events.set_account_currency(frm, cdt, cdn);
+ },
+
+ amount: function(frm, cdt, cdn) {
+ frm.events.set_base_amount(frm, cdt, cdn);
+ }
+});
diff --git a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.json b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.json
index 0149280..6a7994b 100644
--- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.json
+++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.json
@@ -1,4 +1,5 @@
{
+ "actions": [],
"autoname": "naming_series:",
"creation": "2014-07-11 11:33:42.547339",
"doctype": "DocType",
@@ -7,6 +8,9 @@
"field_order": [
"naming_series",
"company",
+ "column_break_2",
+ "posting_date",
+ "section_break_5",
"purchase_receipts",
"purchase_receipt_items",
"get_items_from_purchase_receipts",
@@ -86,7 +90,7 @@
{
"fieldname": "total_taxes_and_charges",
"fieldtype": "Currency",
- "label": "Total Taxes and Charges",
+ "label": "Total Taxes and Charges (Company Currency)",
"options": "Company:company:default_currency",
"read_only": 1,
"reqd": 1
@@ -119,11 +123,29 @@
"fieldname": "landed_cost_help",
"fieldtype": "HTML",
"label": "Landed Cost Help"
+ },
+ {
+ "fieldname": "column_break_2",
+ "fieldtype": "Column Break"
+ },
+ {
+ "default": "Today",
+ "fieldname": "posting_date",
+ "fieldtype": "Date",
+ "label": "Posting Date",
+ "reqd": 1
+ },
+ {
+ "fieldname": "section_break_5",
+ "fieldtype": "Section Break",
+ "hide_border": 1
}
],
"icon": "icon-usd",
+ "index_web_pages_for_search": 1,
"is_submittable": 1,
- "modified": "2019-11-21 15:34:10.846093",
+ "links": [],
+ "modified": "2020-12-13 23:18:47.442466",
"modified_by": "Administrator",
"module": "Stock",
"name": "Landed Cost Voucher",
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 9ec6b89..2d10e6a 100644
--- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
+++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py
@@ -9,6 +9,7 @@
from frappe.model.document import Document
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
from erpnext.accounts.doctype.account.account import get_account_currency
+from erpnext.accounts.doctype.journal_entry.journal_entry import get_exchange_rate
class LandedCostVoucher(Document):
def get_items_from_purchase_receipts(self):
@@ -39,13 +40,17 @@
def validate(self):
self.check_mandatory()
+ self.validate_purchase_receipts()
+ self.set_account_currency()
+ self.set_exchange_rate()
+ self.set_amounts_in_company_currency()
+ self.set_total_taxes_and_charges()
if not self.get("items"):
self.get_items_from_purchase_receipts()
- else:
- self.validate_applicable_charges_for_item()
- self.validate_purchase_receipts()
- self.validate_expense_accounts()
- self.set_total_taxes_and_charges()
+
+ self.set_applicable_charges_on_item()
+ self.validate_applicable_charges_for_item()
+
def check_mandatory(self):
if not self.get("purchase_receipts"):
@@ -73,16 +78,52 @@
frappe.throw(_("Row {0}: Cost center is required for an item {1}")
.format(item.idx, item.item_code))
- def validate_expense_accounts(self):
- company_currency = erpnext.get_company_currency(self.company)
- for account in self.taxes:
- if get_account_currency(account.expense_account) != company_currency:
- frappe.throw(_("Row {}: Expense account currency should be same as company's default currency.").format(account.idx)
- + _("Please select expense account with account currency as {}.").format(frappe.bold(company_currency)),
- title=_("Invalid Account Currency"))
-
def set_total_taxes_and_charges(self):
- self.total_taxes_and_charges = sum([flt(d.amount) for d in self.get("taxes")])
+ self.total_taxes_and_charges = sum([flt(d.base_amount) for d in self.get("taxes")])
+
+ def set_applicable_charges_on_item(self):
+ if self.get('taxes'):
+ total_item_cost = 0.0
+ total_charges = 0.0
+ item_count = 0
+ based_on_field = frappe.scrub(self.distribute_charges_based_on)
+
+ for item in self.get('items'):
+ total_item_cost += item.get(based_on_field)
+
+ for item in self.get('items'):
+ item.applicable_charges = flt(flt(item.get(based_on_field)) * flt(self.total_taxes_and_charges) / flt(total_item_cost),
+ item.precision('applicable_charges'))
+ total_charges += item.applicable_charges
+ item_count += 1
+
+ if total_charges != self.total_taxes_and_charges:
+ diff = self.total_taxes_and_charges - total_charges
+ self.get('items')[item_count - 1].applicable_charges += diff
+
+ def set_account_currency(self):
+ company_currency = erpnext.get_company_currency(self.company)
+ for d in self.get('taxes'):
+ if not d.account_currency:
+ account_currency = frappe.db.get_value('Account', d.expense_account, 'account_currency')
+ d.account_currency = account_currency or company_currency
+
+ def set_exchange_rate(self):
+ company_currency = erpnext.get_company_currency(self.company)
+ for d in self.get('taxes'):
+ if d.account_currency == company_currency:
+ d.exchange_rate = 1
+ elif not d.exchange_rate or d.exchange_rate == 1 or self.posting_date:
+ d.exchange_rate = get_exchange_rate(self.posting_date, account=d.expense_account,
+ account_currency=d.account_currency, company=self.company)
+
+ if not d.exchange_rate:
+ frappe.throw(_("Row {0}: Exchange Rate is mandatory").format(d.idx))
+
+ def set_amounts_in_company_currency(self):
+ for d in self.get('taxes'):
+ d.amount = flt(d.amount, d.precision("amount"))
+ d.base_amount = flt(d.amount * flt(d.exchange_rate), d.precision("base_amount"))
def validate_applicable_charges_for_item(self):
based_on = self.distribute_charges_based_on.lower()
@@ -153,13 +194,13 @@
docs = frappe.db.get_all('Asset', filters={ receipt_document_type: item.receipt_document,
'item_code': item.item_code }, fields=['name', 'docstatus'])
if not docs or len(docs) != item.qty:
- frappe.throw(_('There are not enough asset created or linked to {0}.').format(item.receipt_document)
- + _('Please create or link {0} Assets with respective document.').format(item.qty))
+ frappe.throw(_('There are not enough asset created or linked to {0}. Please create or link {1} Assets with respective document.').format(
+ item.receipt_document, item.qty))
if docs:
for d in docs:
if d.docstatus == 1:
- frappe.throw(_('{0} {1} has submitted Assets. Remove Item {2} from table to continue.')
- .format(item.receipt_document_type, frappe.bold(item.receipt_document), frappe.bold(item.item_code)))
+ frappe.throw(_('{2} <b>{0}</b> has submitted Assets. Remove Item <b>{1}</b> from table to continue.').format(
+ item.receipt_document, item.item_code, item.receipt_document_type))
def update_rate_in_serial_no_for_non_asset_items(self, receipt_document):
for item in receipt_document.get("items"):
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 b97213e..cd4b33e 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
@@ -10,6 +10,7 @@
import 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
from erpnext.accounts.doctype.account.test_account import get_inventory_account
+from erpnext.accounts.doctype.account.test_account import create_account
class TestLandedCostVoucher(unittest.TestCase):
def test_landed_cost_voucher(self):
@@ -206,6 +207,46 @@
self.assertEqual(pr.items[0].landed_cost_voucher_amount, 100)
self.assertEqual(pr.items[1].landed_cost_voucher_amount, 100)
+ def test_multi_currency_lcv(self):
+ ## Create USD Shipping charges_account
+ usd_shipping = create_account(account_name="Shipping Charges USD",
+ parent_account="Duties and Taxes - TCP1", company="_Test Company with perpetual inventory",
+ account_currency="USD")
+
+ pr = make_purchase_receipt(company="_Test Company with perpetual inventory", warehouse = "Stores - TCP1",
+ supplier_warehouse = "Stores - TCP1")
+ pr.submit()
+
+ lcv = make_landed_cost_voucher(company = pr.company, receipt_document_type = "Purchase Receipt",
+ receipt_document=pr.name, charges=100, do_not_save=True)
+
+ lcv.append("taxes", {
+ "description": "Shipping Charges",
+ "expense_account": usd_shipping,
+ "amount": 10
+ })
+
+ lcv.save()
+ lcv.submit()
+ pr.load_from_db()
+
+ # Considering exchange rate from USD to INR as 62.9
+ self.assertEqual(lcv.total_taxes_and_charges, 729)
+ self.assertEqual(pr.items[0].landed_cost_voucher_amount, 729)
+
+ gl_entries = frappe.get_all("GL Entry", fields=["account", "credit", "credit_in_account_currency"],
+ filters={"voucher_no": pr.name, "account": ("in", ["Shipping Charges USD - TCP1", "Expenses Included In Valuation - TCP1"])})
+
+ expected_gl_entries = {
+ "Shipping Charges USD - TCP1": [629, 10],
+ "Expenses Included In Valuation - TCP1": [100, 100]
+ }
+
+ for entry in gl_entries:
+ amounts = expected_gl_entries.get(entry.account)
+ self.assertEqual(entry.credit, amounts[0])
+ self.assertEqual(entry.credit_in_account_currency, amounts[1])
+
def make_landed_cost_voucher(** args):
args = frappe._dict(args)
ref_doc = frappe.get_doc(args.receipt_document_type, args.receipt_document)
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index 226064b..e2f9ff3 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -281,12 +281,15 @@
# Amount added through landed-cost-voucher
if d.landed_cost_voucher_amount and landed_cost_entries:
for account, amount in iteritems(landed_cost_entries[(d.item_code, d.name)]):
+ account_currency = get_account_currency(account)
gl_entries.append(self.get_gl_dict({
"account": account,
+ "account_currency": account_currency,
"against": warehouse_account[d.warehouse]["account"],
"cost_center": d.cost_center,
"remarks": self.get("remarks") or _("Accounting Entry for Stock"),
- "credit": flt(amount),
+ "credit": flt(amount["base_amount"]),
+ "credit_in_account_currency": flt(amount["amount"]),
"project": d.project
}, item=d))
@@ -731,9 +734,16 @@
if item.receipt_document == purchase_document:
for account in landed_cost_voucher_doc.taxes:
item_account_wise_cost.setdefault((item.item_code, item.purchase_receipt_item), {})
- item_account_wise_cost[(item.item_code, item.purchase_receipt_item)].setdefault(account.expense_account, 0.0)
- item_account_wise_cost[(item.item_code, item.purchase_receipt_item)][account.expense_account] += \
+ item_account_wise_cost[(item.item_code, item.purchase_receipt_item)].setdefault(account.expense_account, {
+ "amount": 0.0,
+ "base_amount": 0.0
+ })
+
+ item_account_wise_cost[(item.item_code, item.purchase_receipt_item)][account.expense_account]["amount"] += \
account.amount * item.get(based_on_field) / total_item_cost
+ item_account_wise_cost[(item.item_code, item.purchase_receipt_item)][account.expense_account]["base_amount"] += \
+ account.base_amount * item.get(based_on_field) / total_item_cost
+
return item_account_wise_cost