From 3a2682919a6f106c67653bcf26423b172aab7cea Mon Sep 17 00:00:00 2001 From: Saurabh Date: Mon, 22 Feb 2016 15:18:40 +0530 Subject: [PATCH] [enhance] set customer payable account to new customer as per payment gateway account currency via cart if cart and checkout is enable --- .../payment_request/payment_request.json | 115 +++++++++--------- .../payment_request/payment_request.py | 25 ++-- erpnext/accounts/utils.py | 14 +++ erpnext/shopping_cart/cart.py | 44 ++++++- .../shopping_cart_settings.js | 14 +++ 5 files changed, 143 insertions(+), 69 deletions(-) diff --git a/erpnext/accounts/doctype/payment_request/payment_request.json b/erpnext/accounts/doctype/payment_request/payment_request.json index 39330a59e7..14daba7f2f 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.json +++ b/erpnext/accounts/doctype/payment_request/payment_request.json @@ -33,54 +33,6 @@ "set_only_once": 0, "unique": 0 }, - { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "fieldname": "base_rounded_total", - "fieldtype": "Currency", - "hidden": 0, - "ignore_user_permissions": 0, - "in_filter": 0, - "in_list_view": 0, - "label": "Base Rounded Total", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "2", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, - { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "fieldname": "rounded_total", - "fieldtype": "Float", - "hidden": 0, - "ignore_user_permissions": 0, - "in_filter": 0, - "in_list_view": 0, - "label": "Rounded Total", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "2", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, { "allow_on_submit": 0, "bold": 0, @@ -91,7 +43,7 @@ "ignore_user_permissions": 0, "in_filter": 0, "in_list_view": 0, - "label": "Currency", + "label": "Transaction Currency", "length": 0, "no_copy": 0, "options": "Currency", @@ -110,18 +62,44 @@ "allow_on_submit": 0, "bold": 0, "collapsible": 0, - "depends_on": "eval:doc.reference_doctype==\"Sales Order\"", - "fieldname": "make_sales_invoice", - "fieldtype": "Check", + "description": "Amount in customer's currency", + "fieldname": "grand_total", + "fieldtype": "Currency", "hidden": 0, "ignore_user_permissions": 0, "in_filter": 0, "in_list_view": 0, - "label": "Make Sales Invoice", + "label": "Grand Total", + "length": 0, + "no_copy": 0, + "options": "currency", + "permlevel": 0, + "precision": "2", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "description": "Amount in company's currency", + "fieldname": "base_grand_total", + "fieldtype": "Currency", + "hidden": 0, + "ignore_user_permissions": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Base Grand Total", "length": 0, "no_copy": 0, "permlevel": 0, - "precision": "", + "precision": "2", "print_hide": 0, "print_hide_if_no_value": 0, "read_only": 0, @@ -180,6 +158,31 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "depends_on": "eval:doc.reference_doctype==\"Sales Order\"", + "fieldname": "make_sales_invoice", + "fieldtype": "Check", + "hidden": 0, + "ignore_user_permissions": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Make Sales Invoice", + "length": 0, + "no_copy": 0, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, { "allow_on_submit": 0, "bold": 0, @@ -603,8 +606,8 @@ "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2016-02-16 12:29:45.290729", - "modified_by": "saurabh@erpnext.com", + "modified": "2016-02-19 16:19:55.933694", + "modified_by": "Administrator", "module": "Accounts", "name": "Payment Request", "name_case": "", diff --git a/erpnext/accounts/doctype/payment_request/payment_request.py b/erpnext/accounts/doctype/payment_request/payment_request.py index 8418567e5f..0ef6aa37fc 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.py +++ b/erpnext/accounts/doctype/payment_request/payment_request.py @@ -87,12 +87,12 @@ class PaymentRequest(Document): debit_in_account_currency = 0.0 if party_account_currency == ref_doc.company_currency: - amount = self.base_rounded_total + amount = self.base_grand_total if self.currency != ref_doc.company_currency: - debit_in_account_currency = self.rounded_total + debit_in_account_currency = self.grand_total else: - amount = self.rounded_total + amount = self.grand_total if self.reference_doctype == "Sales Order": jv = get_payment_entry_against_order(self.reference_doctype, self.reference_name, @@ -160,7 +160,8 @@ def make_payment_request(**args): gateway_account = get_gateway_details(args) - base_rounded_total, rounded_total = get_amount(ref_doc, args.dt) + base_grand_total, grand_total = get_amount(ref_doc, args.dt) + existing_payment_request = frappe.db.get_value("Payment Request", {"reference_doctype": args.dt, "reference_name": args.dn}) @@ -175,8 +176,8 @@ def make_payment_request(**args): "payment_account": gateway_account.payment_account, "currency": ref_doc.currency, "make_sales_invoice": args.cart or 0, - "base_rounded_total": base_rounded_total, - "rounded_total": rounded_total, + "base_grand_total": base_grand_total, + "grand_total": grand_total, "mute_email": args.mute_email or 0, "email_to": args.recipient_id or "", "subject": "Payment Request for %s"%args.dn, @@ -207,15 +208,15 @@ def make_payment_request(**args): def get_amount(ref_doc, dt): """get amount based on doctype""" if dt == "Sales Order": - base_rounded_total = flt(ref_doc.base_grand_total) - rounded_total = flt(ref_doc.grand_total) - flt(ref_doc.advance_paid) + base_grand_total = flt(ref_doc.base_grand_total) + grand_total = flt(ref_doc.grand_total) - flt(ref_doc.advance_paid) if dt == "Sales Invoice": - base_rounded_total = flt(ref_doc.base_grand_total) - rounded_total = flt(ref_doc.grand_total) - flt(ref_doc.advance_paid) + base_grand_total = flt(ref_doc.base_grand_total) + grand_total = flt(ref_doc.grand_total) - flt(ref_doc.advance_paid) - if base_rounded_total > 0 and rounded_total > 0 : - return base_rounded_total, rounded_total + if base_grand_total > 0 and grand_total > 0 : + return base_grand_total, grand_total else: frappe.throw(_("Payment Entry is already created")) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index d59b179fd4..53a1b6a083 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -477,3 +477,17 @@ def get_outstanding_invoices(party_type, party, account, condition=None): }) return outstanding_invoices + + +def get_account(account_type=None, root_type=None, is_group=None, account_name=None): + """return account based on matching conditions""" + if account_name: + if not frappe.db.get_value("Account", {"account_name": account_name}): + return False + else: + return frappe.get_doc("Account", { + "account_type": account_type or '', + "root_type": root_type or '', + "is_group": is_group or 0, + "account_name": account_name or '' + }) \ No newline at end of file diff --git a/erpnext/shopping_cart/cart.py b/erpnext/shopping_cart/cart.py index 561a50cd72..b4df27dca2 100644 --- a/erpnext/shopping_cart/cart.py +++ b/erpnext/shopping_cart/cart.py @@ -9,6 +9,7 @@ from frappe.utils import cint, flt, get_fullname, cstr from erpnext.utilities.doctype.address.address import get_address_display from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings import get_shopping_cart_settings from frappe.utils.nestedset import get_root_of +from erpnext.accounts.utils import get_account class WebsitePriceListMissingError(frappe.ValidationError): pass @@ -280,7 +281,13 @@ def get_customer(user=None): user = frappe.session.user customer = frappe.db.get_value("Contact", {"email_id": user}, "customer") - + cart_settings = frappe.get_doc("Shopping Cart Settings") + + debtors_account = '' + + if cart_settings.enable_checkout: + debtors_account = get_debtors_account(cart_settings) + if customer: return frappe.get_doc("Customer", customer) @@ -293,6 +300,15 @@ def get_customer(user=None): "customer_group": get_shopping_cart_settings().default_customer_group, "territory": get_root_of("Territory") }) + + if debtors_account: + customer.update({ + "accounts": [{ + "company": cart_settings.company, + "account": debtors_account.name + }] + }) + customer.flags.ignore_mandatory = True customer.insert(ignore_permissions=True) @@ -307,6 +323,32 @@ def get_customer(user=None): return customer +def get_debtors_account(cart_settings): + payment_gateway_account_currency = \ + frappe.get_doc("Payment Gateway Account", cart_settings.payment_gateway_account).currency + + account_name = _("Debtors ({0})".format(payment_gateway_account_currency)) + + debtors_account = get_account("Receivable", "Asset", is_group=0, account_name=account_name) + + if not debtors_account: + debtors_account = frappe.get_doc({ + "doctype": "Account", + "account_type": "Receivable", + "root_type": "Asset", + "is_group": 0, + "parent_account": get_account(root_type="Asset", is_group=1, \ + account_name=_("Accounts Receivable")).name, + "account_name": account_name, + "currency": payment_gateway_account_currency + }).insert(ignore_permissions=True) + + return debtors_account + + else: + return debtors_account + + def get_address_docs(doctype=None, txt=None, filters=None, limit_start=0, limit_page_length=20, party=None): if not party: party = get_customer() diff --git a/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.js b/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.js index 550e34d504..3219d7a5cd 100644 --- a/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.js +++ b/erpnext/shopping_cart/doctype/shopping_cart_settings/shopping_cart_settings.js @@ -6,5 +6,19 @@ $.extend(cur_frm.cscript, { if(cur_frm.doc.__onload && cur_frm.doc.__onload.quotation_series) { cur_frm.fields_dict.quotation_series.df.options = cur_frm.doc.__onload.quotation_series; } + }, + refresh: function(){ + toggle_mandatory(cur_frm) + }, + enable_checkout: function(){ + toggle_mandatory(cur_frm) } }); + + +function toggle_mandatory (cur_frm){ + cur_frm.toggle_reqd("payment_gateway_account", false); + if(cur_frm.doc.enabled && cur_frm.doc.enable_checkout) { + cur_frm.toggle_reqd("payment_gateway_account", true); + } +}