[fixes] test case for Payment request

This commit is contained in:
Saurabh 2015-12-23 13:04:28 +05:30
parent 1664668b0e
commit 766f6a43d5
5 changed files with 174 additions and 24 deletions

View File

@ -9,6 +9,30 @@
"doctype": "DocType", "doctype": "DocType",
"document_type": "", "document_type": "",
"fields": [ "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, "allow_on_submit": 0,
"bold": 0, "bold": 0,
@ -81,6 +105,31 @@
"search_index": 0, "search_index": 0,
"set_only_once": 0, "set_only_once": 0,
"unique": 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, "hide_heading": 0,
@ -92,7 +141,7 @@
"issingle": 0, "issingle": 0,
"istable": 0, "istable": 0,
"max_attachments": 0, "max_attachments": 0,
"modified": "2015-12-16 13:20:49.584949", "modified": "2015-12-22 19:07:36.491109",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Accounts", "module": "Accounts",
"name": "Payment Gateway", "name": "Payment Gateway",

View File

@ -9,9 +9,13 @@ from frappe.model.document import Document
class PaymentGateway(Document): class PaymentGateway(Document):
def validate(self): def validate(self):
self.update_default_payment_gateway() self.update_default_payment_gateway()
self.set_as_default()
def update_default_payment_gateway(self): def update_default_payment_gateway(self):
frappe.db.sql("""update `tabPayment Gateway` set is_default = 0 if self.is_default:
where is_default = 1 """) 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

View File

@ -148,7 +148,7 @@
"precision": "", "precision": "",
"print_hide": 0, "print_hide": 0,
"print_hide_if_no_value": 0, "print_hide_if_no_value": 0,
"read_only": 0, "read_only": 1,
"report_hide": 0, "report_hide": 0,
"reqd": 0, "reqd": 0,
"search_index": 0, "search_index": 0,
@ -156,7 +156,7 @@
"unique": 0 "unique": 0
}, },
{ {
"allow_on_submit": 1, "allow_on_submit": 0,
"bold": 0, "bold": 0,
"collapsible": 0, "collapsible": 0,
"default": "", "default": "",
@ -232,7 +232,31 @@
"unique": 0 "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, "bold": 0,
"collapsible": 0, "collapsible": 0,
"fieldname": "email_to", "fieldname": "email_to",
@ -435,7 +459,7 @@
"issingle": 0, "issingle": 0,
"istable": 0, "istable": 0,
"max_attachments": 0, "max_attachments": 0,
"modified": "2015-12-21 10:57:48.107213", "modified": "2015-12-22 20:18:08.853333",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Accounts", "module": "Accounts",
"name": "Payment Request", "name": "Payment Request",

View File

@ -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.party import get_party_account
from erpnext.accounts.utils import get_account_currency, get_balance_on from erpnext.accounts.utils import get_account_currency, get_balance_on
from itertools import chain from itertools import chain
from paypal_integration.express_checkout import set_express_checkout
class PaymentRequest(Document): class PaymentRequest(Document):
def validate(self): def validate(self):
@ -23,8 +22,10 @@ class PaymentRequest(Document):
frappe.throw(_("Payment Request already exist")) frappe.throw(_("Payment Request already exist"))
def on_submit(self): def on_submit(self):
self.send_payment_request() if not self.mute_email:
self.send_email() self.send_payment_request()
self.send_email()
self.make_communication_entry() self.make_communication_entry()
def on_cancel(self): def on_cancel(self):
@ -37,7 +38,8 @@ class PaymentRequest(Document):
pass pass
def send_payment_request(self): 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, self.payment_url = set_express_checkout(self.amount, self.currency, {"doctype": self.doctype,
"docname": self.name}) "docname": self.name})
@ -48,7 +50,7 @@ class PaymentRequest(Document):
if frappe.session.user == "Guest": if frappe.session.user == "Guest":
frappe.set_user("Administrator") frappe.set_user("Administrator")
self.create_journal_voucher_entry() return self.create_journal_voucher_entry()
def create_journal_voucher_entry(self): def create_journal_voucher_entry(self):
"""create voucher entry""" """create voucher entry"""
@ -99,6 +101,8 @@ class PaymentRequest(Document):
#set status as paid for Payment Request #set status as paid for Payment Request
frappe.db.set_value(self.doctype, self.name, "status", "Paid") frappe.db.set_value(self.doctype, self.name, "status", "Paid")
return jv
def send_email(self): def send_email(self):
"""send email with payment link""" """send email with payment link"""
frappe.sendmail(recipients=self.email_to, sender=None, subject=self.subject, frappe.sendmail(recipients=self.email_to, sender=None, subject=self.subject,
@ -128,24 +132,31 @@ class PaymentRequest(Document):
comm.insert(ignore_permissions=True) comm.insert(ignore_permissions=True)
@frappe.whitelist() @frappe.whitelist()
def make_payment_request(dt, dn, recipient_id=None): def make_payment_request(**args):
"""Make payment request""" """Make payment request"""
ref_doc = get_reference_doc_details(dt, dn) args = frappe._dict(args)
payment_gateway, payment_account = get_gateway_details() 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 = frappe.new_doc("Payment Request")
pr.update({ pr.update({
"payment_gateway": payment_gateway, "payment_gateway": payment_gateway,
"payment_account": payment_account, "payment_account": payment_account,
"currency": ref_doc.currency, "currency": ref_doc.currency,
"amount": get_amount(ref_doc, dt), "amount": get_amount(ref_doc, args.dt),
"email_to": recipient_id, "mute_email": args.mute_email or 0,
"subject": "Payment Request for %s"%dn, "email_to": args.recipient_id or "",
"subject": "Payment Request for %s"%args.dn,
"message": frappe.get_doc("Accounts Settings").message, "message": frappe.get_doc("Accounts Settings").message,
"reference_doctype": dt, "reference_doctype": args.dt,
"reference_name": dn "reference_name": args.dn
}) })
if args.submit_doc:
pr.insert(ignore_permissions=True)
pr.submit()
return pr
return pr.as_dict() return pr.as_dict()
def get_reference_doc_details(dt, dn): def get_reference_doc_details(dt, dn):
@ -171,8 +182,11 @@ def get_amount(ref_doc, dt):
else: else:
frappe.throw(_("Payment Entry is already created")) 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""" """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"]) return frappe.db.get_value("Payment Gateway", {"is_default": 1}, ["gateway", "payment_account"])
@frappe.whitelist() @frappe.whitelist()

View File

@ -5,8 +5,67 @@ from __future__ import unicode_literals
import frappe import frappe
import unittest 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_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): 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)