From bac9b8eef6bcec20b0304cc8d4e527574a048762 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 31 Aug 2015 19:01:11 +0530 Subject: [PATCH] Payment Tool changes due to multi currency --- .../journal_entry_account.json | 22 ++++---- .../doctype/payment_tool/payment_tool.js | 29 ++++++++--- .../doctype/payment_tool/payment_tool.json | 28 +++++++++- .../doctype/payment_tool/payment_tool.py | 51 ++++++++++++------- .../payment_tool_detail.json | 5 +- .../purchase_invoice/purchase_invoice.js | 4 +- .../purchase_invoice/purchase_invoice.py | 5 +- .../doctype/sales_invoice/sales_invoice.js | 4 +- .../doctype/sales_invoice/sales_invoice.py | 5 +- .../public/js/controllers/taxes_and_totals.js | 2 +- 10 files changed, 111 insertions(+), 44 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json b/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json index 11c92e2ea0..c866181446 100644 --- a/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json +++ b/erpnext/accounts/doctype/journal_entry_account/journal_entry_account.json @@ -93,7 +93,7 @@ "in_list_view": 0, "label": "Balance in Account Currency", "no_copy": 1, - "options": "currency", + "options": "account_currency", "permlevel": 0, "precision": "", "print_hide": 1, @@ -231,7 +231,7 @@ "in_list_view": 0, "label": "Party Balance in Account Currency", "no_copy": 1, - "options": "currency", + "options": "account_currency", "permlevel": 0, "precision": "", "print_hide": 1, @@ -269,13 +269,13 @@ "collapsible": 0, "fieldname": "debit_in_account_currency", "fieldtype": "Currency", - "hidden": 1, + "hidden": 0, "ignore_user_permissions": 0, "in_filter": 0, - "in_list_view": 0, + "in_list_view": 1, "label": "Debit in Account Currency", "no_copy": 0, - "options": "currency", + "options": "account_currency", "permlevel": 0, "precision": "", "print_hide": 0, @@ -295,7 +295,7 @@ "hidden": 0, "ignore_user_permissions": 0, "in_filter": 0, - "in_list_view": 1, + "in_list_view": 0, "label": "Debit in Company Currency", "no_copy": 1, "oldfieldname": "debit", @@ -336,13 +336,13 @@ "collapsible": 0, "fieldname": "credit_in_account_currency", "fieldtype": "Currency", - "hidden": 1, + "hidden": 0, "ignore_user_permissions": 0, "in_filter": 0, - "in_list_view": 0, + "in_list_view": 1, "label": "Credit in Account Currency", "no_copy": 0, - "options": "currency", + "options": "account_currency", "permlevel": 0, "precision": "", "print_hide": 0, @@ -362,7 +362,7 @@ "hidden": 0, "ignore_user_permissions": 0, "in_filter": 0, - "in_list_view": 1, + "in_list_view": 0, "label": "Credit in Company Currency", "no_copy": 1, "oldfieldname": "credit", @@ -520,7 +520,7 @@ "is_submittable": 0, "issingle": 0, "istable": 1, - "modified": "2015-08-28 17:15:25.180681", + "modified": "2015-08-31 17:50:04.145031", "modified_by": "Administrator", "module": "Accounts", "name": "Journal Entry Account", diff --git a/erpnext/accounts/doctype/payment_tool/payment_tool.js b/erpnext/accounts/doctype/payment_tool/payment_tool.js index 6eefdb936c..5904b99d0a 100644 --- a/erpnext/accounts/doctype/payment_tool/payment_tool.js +++ b/erpnext/accounts/doctype/payment_tool/payment_tool.js @@ -55,6 +55,25 @@ frappe.ui.form.on("Payment Tool", "party", function(frm) { } }) +frappe.ui.form.on("Payment Tool", "party_account", function(frm) { + if(frm.doc.party_account) { + frm.call({ + method: "frappe.client.get_value", + args: { + doctype: "Account", + fieldname: "account_currency", + filters: { name: frm.doc.party_account }, + }, + callback: function(r, rt) { + if(r.message) { + frm.set_value("party_account_currency", r.message.account_currency); + erpnext.payment_tool.check_mandatory_to_set_button(frm); + } + } + }); + } +}) + frappe.ui.form.on("Payment Tool", "company", function(frm) { erpnext.payment_tool.check_mandatory_to_set_button(frm); }); @@ -63,10 +82,6 @@ frappe.ui.form.on("Payment Tool", "received_or_paid", function(frm) { erpnext.payment_tool.check_mandatory_to_set_button(frm); }); -frappe.ui.form.on("Payment Tool", "party", function(frm) { - erpnext.payment_tool.check_mandatory_to_set_button(frm); -}); - // Fetch bank/cash account based on payment mode frappe.ui.form.on("Payment Tool", "payment_mode", function(frm) { return frappe.call({ @@ -158,7 +173,9 @@ frappe.ui.form.on("Payment Tool Detail", "against_voucher_no", function(frm, cdt method: 'erpnext.accounts.doctype.payment_tool.payment_tool.get_against_voucher_amount', args: { "against_voucher_type": row.against_voucher_type, - "against_voucher_no": row.against_voucher_no + "against_voucher_no": row.against_voucher_no, + "party_account": self.party_account, + "company": self.company }, callback: function(r) { if(!r.exc) { @@ -216,4 +233,4 @@ erpnext.payment_tool.check_mandatory_to_fetch = function(doc) { $.each(["Company", "Party Type", "Party", "Received or Paid"], function(i, field) { if(!doc[frappe.model.scrub(field)]) frappe.throw(__("Please select {0} first", [field])); }); -} +} \ No newline at end of file diff --git a/erpnext/accounts/doctype/payment_tool/payment_tool.json b/erpnext/accounts/doctype/payment_tool/payment_tool.json index e00d9e29ab..55e70481e2 100644 --- a/erpnext/accounts/doctype/payment_tool/payment_tool.json +++ b/erpnext/accounts/doctype/payment_tool/payment_tool.json @@ -106,7 +106,7 @@ "ignore_user_permissions": 0, "in_filter": 0, "in_list_view": 0, - "label": "Column Break 1", + "label": "", "no_copy": 0, "permlevel": 0, "print_hide": 0, @@ -162,6 +162,29 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "fieldname": "party_account_currency", + "fieldtype": "Link", + "hidden": 1, + "ignore_user_permissions": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Party Account Currency", + "no_copy": 1, + "options": "Currency", + "permlevel": 0, + "precision": "", + "print_hide": 1, + "read_only": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, { "allow_on_submit": 0, "bold": 0, @@ -306,6 +329,7 @@ "in_list_view": 0, "label": "Total Payment Amount", "no_copy": 0, + "options": "party_account_currency", "permlevel": 0, "print_hide": 0, "read_only": 1, @@ -450,7 +474,7 @@ "is_submittable": 0, "issingle": 1, "istable": 0, - "modified": "2015-06-05 11:17:33.843334", + "modified": "2015-08-31 18:58:21.813054", "modified_by": "Administrator", "module": "Accounts", "name": "Payment Tool", diff --git a/erpnext/accounts/doctype/payment_tool/payment_tool.py b/erpnext/accounts/doctype/payment_tool/payment_tool.py index 4c001c5b3c..924fd1e1c6 100644 --- a/erpnext/accounts/doctype/payment_tool/payment_tool.py +++ b/erpnext/accounts/doctype/payment_tool/payment_tool.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import frappe -from frappe import _ +from frappe import _, scrub from frappe.utils import flt from frappe.model.document import Document import json @@ -38,7 +38,8 @@ class PaymentTool(Document): d1.set("reference_type", v.against_voucher_type) d1.set("reference_name", v.against_voucher_no) d1.set('is_advance', 'Yes' if v.against_voucher_type in ['Sales Order', 'Purchase Order'] else 'No') - total_payment_amount = flt(total_payment_amount) + flt(d1.debit) - flt(d1.credit) + total_payment_amount = flt(total_payment_amount) + \ + flt(d1.debit_in_account_currency) - flt(d1.credit_in_account_currency) d2 = jv.append("accounts") d2.account = self.payment_account @@ -57,11 +58,14 @@ def get_outstanding_vouchers(args): frappe.throw(_("No permission to use Payment Tool"), frappe.PermissionError) args = json.loads(args) + + party_account_currency = frappe.db.get_value("Account", args.get("party_account"), "account_currency") + company_currency = frappe.db.get_value("Company", args.get("company"), "default_currency") if args.get("party_type") == "Customer" and args.get("received_or_paid") == "Received": - amount_query = "ifnull(debit, 0) - ifnull(credit, 0)" + amount_query = "ifnull(debit_in_account_currency, 0) - ifnull(credit_in_account_currency, 0)" elif args.get("party_type") == "Supplier" and args.get("received_or_paid") == "Paid": - amount_query = "ifnull(credit, 0) - ifnull(debit, 0)" + amount_query = "ifnull(credit_in_account_currency, 0) - ifnull(debit_in_account_currency, 0)" else: frappe.throw(_("Please enter the Against Vouchers manually")) @@ -70,27 +74,34 @@ def get_outstanding_vouchers(args): args.get("party_type"), args.get("party")) # Get all SO / PO which are not fully billed or aginst which full advance not paid - orders_to_be_billed = get_orders_to_be_billed(args.get("party_type"), args.get("party")) + orders_to_be_billed = get_orders_to_be_billed(args.get("party_type"), args.get("party"), + party_account_currency, company_currency) return outstanding_invoices + orders_to_be_billed -def get_orders_to_be_billed(party_type, party): +def get_orders_to_be_billed(party_type, party, party_account_currency, company_currency): voucher_type = 'Sales Order' if party_type == "Customer" else 'Purchase Order' + + ref_field = "base_grand_total" if party_account_currency == company_currency else "grand_total" + orders = frappe.db.sql(""" select name as voucher_no, - ifnull(base_grand_total, 0) as invoice_amount, - (ifnull(base_grand_total, 0) - ifnull(advance_paid, 0)) as outstanding_amount, + ifnull({ref_field}, 0) as invoice_amount, + (ifnull({ref_field}, 0) - ifnull(advance_paid, 0)) as outstanding_amount, transaction_date as posting_date from - `tab%s` + `tab{voucher_type}` where - %s = %s + {party_type} = %s and docstatus = 1 and ifnull(status, "") != "Stopped" - and ifnull(base_grand_total, 0) > ifnull(advance_paid, 0) + and ifnull({ref_field}, 0) > ifnull(advance_paid, 0) and abs(100 - ifnull(per_billed, 0)) > 0.01 - """ % (voucher_type, 'customer' if party_type == "Customer" else 'supplier', '%s'), - party, as_dict = True) + """.format(**{ + "ref_field": ref_field, + "voucher_type": voucher_type, + "party_type": scrub(party_type) + }), party, as_dict = True) order_list = [] for d in orders: @@ -100,13 +111,19 @@ def get_orders_to_be_billed(party_type, party): return order_list @frappe.whitelist() -def get_against_voucher_amount(against_voucher_type, against_voucher_no): +def get_against_voucher_amount(against_voucher_type, against_voucher_no, party_account, company): + party_account_currency = frappe.db.get_value("Account", party_account, "account_currency") + company_currency = frappe.db.get_value("Company", company, "default_currency") + ref_field = "base_grand_total" if party_account_currency == company_currency else "grand_total" + if against_voucher_type in ["Sales Order", "Purchase Order"]: - select_cond = "base_grand_total as total_amount, ifnull(base_grand_total, 0) - ifnull(advance_paid, 0) as outstanding_amount" + select_cond = "{0} as total_amount, ifnull({0}, 0) - ifnull(advance_paid, 0) as outstanding_amount"\ + .format(ref_field) elif against_voucher_type in ["Sales Invoice", "Purchase Invoice"]: - select_cond = "base_grand_total as total_amount, outstanding_amount" + select_cond = "{0} as total_amount, outstanding_amount".format(ref_field) elif against_voucher_type == "Journal Entry": - select_cond = "total_debit as total_amount" + ref_field = "total_debit" if party_account_currency == company_currency else "total_debit/exchange_rate" + select_cond = "{0} as total_amount".format(ref_field) details = frappe.db.sql("""select {0} from `tab{1}` where name = %s""" .format(select_cond, against_voucher_type), against_voucher_no, as_dict=1) diff --git a/erpnext/accounts/doctype/payment_tool_detail/payment_tool_detail.json b/erpnext/accounts/doctype/payment_tool_detail/payment_tool_detail.json index 5221f35a3f..7e1460863a 100644 --- a/erpnext/accounts/doctype/payment_tool_detail/payment_tool_detail.json +++ b/erpnext/accounts/doctype/payment_tool_detail/payment_tool_detail.json @@ -87,6 +87,7 @@ "in_list_view": 1, "label": "Total Amount", "no_copy": 0, + "options": "party_account_currency", "permlevel": 0, "print_hide": 0, "read_only": 1, @@ -108,6 +109,7 @@ "in_list_view": 1, "label": "Outstanding Amount", "no_copy": 0, + "options": "party_account_currency", "permlevel": 0, "print_hide": 0, "read_only": 1, @@ -129,6 +131,7 @@ "in_list_view": 1, "label": "Payment Amount", "no_copy": 0, + "options": "party_account_currency", "permlevel": 0, "print_hide": 0, "read_only": 0, @@ -146,7 +149,7 @@ "is_submittable": 0, "issingle": 0, "istable": 1, - "modified": "2014-09-11 08:55:34.384017", + "modified": "2015-08-31 18:58:35.537060", "modified_by": "Administrator", "module": "Accounts", "name": "Payment Tool Detail", diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index e0a8245bba..2e3794a238 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -83,12 +83,12 @@ erpnext.accounts.PurchaseInvoice = erpnext.buying.BuyingController.extend({ method: "frappe.client.get_value", args: { doctype: "Account", - fieldname: "currency", + fieldname: "account_currency", filters: { name: me.frm.doc.credit_to }, }, callback: function(r, rt) { if(r.message) { - me.frm.set_value("party_account_currency", r.message.currency); + me.frm.set_value("party_account_currency", r.message.account_currency); me.set_dynamic_labels(); } } diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index b640ddae8c..4fac12129e 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -91,13 +91,16 @@ class PurchaseInvoice(BuyingController): throw(_("Conversion rate cannot be 0 or 1")) def validate_credit_to_acc(self): - account = frappe.db.get_value("Account", self.credit_to, ["account_type", "report_type"], as_dict=True) + account = frappe.db.get_value("Account", self.credit_to, + ["account_type", "report_type", "account_currency"], as_dict=True) if account.report_type != "Balance Sheet": frappe.throw(_("Credit To account must be a Balance Sheet account")) if self.supplier and account.account_type != "Payable": frappe.throw(_("Credit To account must be a Payable account")) + + self.party_account_currency = account.account_currency def check_for_stopped_status(self): check_list = [] diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 1d44e5410f..5e8d3a5d9b 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -183,12 +183,12 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte method: "frappe.client.get_value", args: { doctype: "Account", - fieldname: "currency", + fieldname: "account_currency", filters: { name: me.frm.doc.debit_to }, }, callback: function(r, rt) { if(r.message) { - me.frm.set_value("party_account_currency", r.message.currency); + me.frm.set_value("party_account_currency", r.message.account_currency); me.set_dynamic_labels(); } } diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 0871a66c75..bce8fe214e 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -271,13 +271,16 @@ class SalesInvoice(SellingController): reconcile_against_document(lst) def validate_debit_to_acc(self): - account = frappe.db.get_value("Account", self.debit_to, ["account_type", "report_type"], as_dict=True) + account = frappe.db.get_value("Account", self.debit_to, + ["account_type", "report_type", "account_currency"], as_dict=True) if account.report_type != "Balance Sheet": frappe.throw(_("Debit To account must be a Balance Sheet account")) if self.customer and account.account_type != "Receivable": frappe.throw(_("Debit To account must be a Receivable account")) + + self.party_account_currency = account.account_currency def validate_fixed_asset_account(self): """Validate Fixed Asset and whether Income Account Entered Exists""" diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js index ad794d6e19..b1b24cf38a 100644 --- a/erpnext/public/js/controllers/taxes_and_totals.js +++ b/erpnext/public/js/controllers/taxes_and_totals.js @@ -525,7 +525,7 @@ erpnext.taxes_and_totals = erpnext.stock.StockController.extend({ var outstanding_amount = flt(total_amount_to_pay, precision("outstanding_amount")); } - if(this.frm.doc.party_account_currency == this.frm.doc.currency) { + if(this.frm.doc.party_account_currency == this.frm.doc.currency) { this.frm.set_value("outstanding_amount", outstanding_amount); } else { this.frm.set_value("outstanding_amount",