From 766f6a43d5c434dc1369ad283b7ab41cd03b2703 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Wed, 23 Dec 2015 13:04:28 +0530 Subject: [PATCH] [fixes] test case for Payment request --- .../payment_gateway/payment_gateway.json | 51 ++++++++++++++- .../payment_gateway/payment_gateway.py | 10 ++- .../payment_request/payment_request.json | 32 ++++++++-- .../payment_request/payment_request.py | 42 ++++++++----- .../payment_request/test_payment_request.py | 63 ++++++++++++++++++- 5 files changed, 174 insertions(+), 24 deletions(-) diff --git a/erpnext/accounts/doctype/payment_gateway/payment_gateway.json b/erpnext/accounts/doctype/payment_gateway/payment_gateway.json index ccee2bbf9b..4f81632b73 100644 --- a/erpnext/accounts/doctype/payment_gateway/payment_gateway.json +++ b/erpnext/accounts/doctype/payment_gateway/payment_gateway.json @@ -9,6 +9,30 @@ "doctype": "DocType", "document_type": "", "fields": [ + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "fieldname": "payment_gateway_details", + "fieldtype": "Section Break", + "hidden": 0, + "ignore_user_permissions": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Payment Gateway Details", + "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, @@ -81,6 +105,31 @@ "search_index": 0, "set_only_once": 0, "unique": 0 + }, + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "fieldname": "currency", + "fieldtype": "Read Only", + "hidden": 0, + "ignore_user_permissions": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Currency", + "length": 0, + "no_copy": 0, + "options": "payment_account.account_currency", + "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 } ], "hide_heading": 0, @@ -92,7 +141,7 @@ "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2015-12-16 13:20:49.584949", + "modified": "2015-12-22 19:07:36.491109", "modified_by": "Administrator", "module": "Accounts", "name": "Payment Gateway", diff --git a/erpnext/accounts/doctype/payment_gateway/payment_gateway.py b/erpnext/accounts/doctype/payment_gateway/payment_gateway.py index a0b8944735..f756a86723 100644 --- a/erpnext/accounts/doctype/payment_gateway/payment_gateway.py +++ b/erpnext/accounts/doctype/payment_gateway/payment_gateway.py @@ -9,9 +9,13 @@ from frappe.model.document import Document class PaymentGateway(Document): def validate(self): self.update_default_payment_gateway() + self.set_as_default() def update_default_payment_gateway(self): - frappe.db.sql("""update `tabPayment Gateway` set is_default = 0 - where is_default = 1 """) - + if self.is_default: + frappe.db.sql("""update `tabPayment Gateway` set is_default = 0 + where is_default = 1 """) + def set_as_default(self): + if not frappe.db.get_value("Payment Gateway", {"is_default": 1, "name": ("!=", self.name)}, "name"): + self.is_default = 1 diff --git a/erpnext/accounts/doctype/payment_request/payment_request.json b/erpnext/accounts/doctype/payment_request/payment_request.json index 40d2fc9ae6..13eeb8bb7f 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.json +++ b/erpnext/accounts/doctype/payment_request/payment_request.json @@ -148,7 +148,7 @@ "precision": "", "print_hide": 0, "print_hide_if_no_value": 0, - "read_only": 0, + "read_only": 1, "report_hide": 0, "reqd": 0, "search_index": 0, @@ -156,7 +156,7 @@ "unique": 0 }, { - "allow_on_submit": 1, + "allow_on_submit": 0, "bold": 0, "collapsible": 0, "default": "", @@ -232,7 +232,31 @@ "unique": 0 }, { - "allow_on_submit": 1, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "fieldname": "mute_email", + "fieldtype": "Check", + "hidden": 1, + "ignore_user_permissions": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Mute Email", + "length": 0, + "no_copy": 1, + "permlevel": 0, + "precision": "", + "print_hide": 1, + "print_hide_if_no_value": 0, + "read_only": 1, + "report_hide": 1, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, + { + "allow_on_submit": 0, "bold": 0, "collapsible": 0, "fieldname": "email_to", @@ -435,7 +459,7 @@ "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2015-12-21 10:57:48.107213", + "modified": "2015-12-22 20:18:08.853333", "modified_by": "Administrator", "module": "Accounts", "name": "Payment Request", diff --git a/erpnext/accounts/doctype/payment_request/payment_request.py b/erpnext/accounts/doctype/payment_request/payment_request.py index 83c947d6d3..bac19ec19f 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.py +++ b/erpnext/accounts/doctype/payment_request/payment_request.py @@ -11,7 +11,6 @@ from erpnext.accounts.doctype.journal_entry.journal_entry import get_payment_ent from erpnext.accounts.party import get_party_account from erpnext.accounts.utils import get_account_currency, get_balance_on from itertools import chain -from paypal_integration.express_checkout import set_express_checkout class PaymentRequest(Document): def validate(self): @@ -23,8 +22,10 @@ class PaymentRequest(Document): frappe.throw(_("Payment Request already exist")) def on_submit(self): - self.send_payment_request() - self.send_email() + if not self.mute_email: + self.send_payment_request() + self.send_email() + self.make_communication_entry() def on_cancel(self): @@ -37,7 +38,8 @@ class PaymentRequest(Document): pass def send_payment_request(self): - if self.payment_gateway == "PayPal": + if self.payment_gateway == "PayPal": + from paypal_integration.express_checkout import set_express_checkout self.payment_url = set_express_checkout(self.amount, self.currency, {"doctype": self.doctype, "docname": self.name}) @@ -48,7 +50,7 @@ class PaymentRequest(Document): if frappe.session.user == "Guest": frappe.set_user("Administrator") - self.create_journal_voucher_entry() + return self.create_journal_voucher_entry() def create_journal_voucher_entry(self): """create voucher entry""" @@ -99,6 +101,8 @@ class PaymentRequest(Document): #set status as paid for Payment Request frappe.db.set_value(self.doctype, self.name, "status", "Paid") + return jv + def send_email(self): """send email with payment link""" frappe.sendmail(recipients=self.email_to, sender=None, subject=self.subject, @@ -128,24 +132,31 @@ class PaymentRequest(Document): comm.insert(ignore_permissions=True) @frappe.whitelist() -def make_payment_request(dt, dn, recipient_id=None): +def make_payment_request(**args): """Make payment request""" - ref_doc = get_reference_doc_details(dt, dn) - payment_gateway, payment_account = get_gateway_details() + args = frappe._dict(args) + ref_doc = get_reference_doc_details(args.dt, args.dn) + payment_gateway, payment_account = get_gateway_details(args) pr = frappe.new_doc("Payment Request") pr.update({ "payment_gateway": payment_gateway, "payment_account": payment_account, "currency": ref_doc.currency, - "amount": get_amount(ref_doc, dt), - "email_to": recipient_id, - "subject": "Payment Request for %s"%dn, + "amount": get_amount(ref_doc, args.dt), + "mute_email": args.mute_email or 0, + "email_to": args.recipient_id or "", + "subject": "Payment Request for %s"%args.dn, "message": frappe.get_doc("Accounts Settings").message, - "reference_doctype": dt, - "reference_name": dn + "reference_doctype": args.dt, + "reference_name": args.dn }) + if args.submit_doc: + pr.insert(ignore_permissions=True) + pr.submit() + return pr + return pr.as_dict() def get_reference_doc_details(dt, dn): @@ -171,8 +182,11 @@ def get_amount(ref_doc, dt): else: frappe.throw(_("Payment Entry is already created")) -def get_gateway_details(): +def get_gateway_details(args): """return gateway and payment account of default payment gateway""" + if args.payemnt_gateway: + frappe.db.get_value("Payment Gateway", args.payemnt_gateway, ["gateway", "payment_account"]) + return frappe.db.get_value("Payment Gateway", {"is_default": 1}, ["gateway", "payment_account"]) @frappe.whitelist() diff --git a/erpnext/accounts/doctype/payment_request/test_payment_request.py b/erpnext/accounts/doctype/payment_request/test_payment_request.py index 1f8cadfacf..bfc54f917b 100644 --- a/erpnext/accounts/doctype/payment_request/test_payment_request.py +++ b/erpnext/accounts/doctype/payment_request/test_payment_request.py @@ -5,8 +5,67 @@ from __future__ import unicode_literals import frappe import unittest - +from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order +from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request +from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice # test_records = frappe.get_test_records('Payment Request') +test_dependencies = ["Currency Exchange", "Journal Entry", "Contact", "Address"] + +payment_method = [ + { + "doctype": "Payment Gateway", + "is_default": 1, + "gateway": "_Test Gateway INR", + "payment_account": "_Test Bank - _TC", + "currency": "INR" + }, + { + "doctype": "Payment Gateway", + "gateway": "_Test Gateway USD", + "payment_account": "_Test Bank - _TC", + "currency": "USD" + } +] + class TestPaymentRequest(unittest.TestCase): - pass + def setUp(self): + for gateway in payment_method: + if not frappe.db.get_value("Payment Gateway", gateway["gateway"], "name"): + frappe.get_doc(gateway).insert(ignore_permissions=True) + + def test_payment_request_linkings(self): + SO_INR = make_sales_order(currency="INR") + pr = make_payment_request(dt="Sales Order", dn=SO_INR.name, recipient_id="saurabh@erpnext.com") + + self.assertEquals(pr.reference_doctype, "Sales Order") + self.assertEquals(pr.reference_name, SO_INR.name) + self.assertEquals(pr.currency, "INR") + + SI_USD = create_sales_invoice(currency="USD", conversion_rate=50) + pr = make_payment_request(dt="Sales Invoice", dn=SI_USD.name, recipient_id="saurabh@erpnext.com") + + self.assertEquals(pr.reference_doctype, "Sales Invoice") + self.assertEquals(pr.reference_name, SI_USD.name) + self.assertEquals(pr.currency, "USD") + + def test_payment_entry(self): + SO_INR = make_sales_order(currency="INR") + pr = make_payment_request(dt="Sales Order", dn=SO_INR.name, recipient_id="saurabh@erpnext.com", + mute_email=1, submit_doc=1) + jv = pr.set_paid() + + SO_INR = frappe.get_doc("Sales Order", SO_INR.name) + + self.assertEquals(SO_INR.advance_paid, jv.total_debit) + + SI_USD = create_sales_invoice(customer="_Test Customer USD", debit_to="_Test Receivable USD - _TC", + currency="USD", conversion_rate=50) + + pr = make_payment_request(dt="Sales Invoice", dn=SI_USD.name, recipient_id="saurabh@erpnext.com", + mute_email=1, submit_doc=1, payemnt_gateway="_Test Gateway USD") + jv = pr.set_paid() + + self.assertEquals(jv.accounts[0].account, "_Test Receivable USD - _TC") + self.assertEquals(jv.accounts[0].account_currency, "USD") + self.assertEquals(jv.accounts[1].account, pr.payment_account)