Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clarify codepath around mute email and make invoice #41704

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 16 additions & 31 deletions erpnext/accounts/doctype/payment_request/payment_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,7 @@ def on_submit(self):

if self.payment_request_type == "Inward":
send_mail = self.payment_gateway_validation() if self.payment_gateway else None
ref_doc = frappe.get_doc(self.reference_doctype, self.reference_name)

if (
hasattr(ref_doc, "order_type") and ref_doc.order_type == "Shopping Cart"
) or self.flags.mute_email:
send_mail = False

if send_mail and self.payment_channel != "Phone":
if send_mail and not (self.mute_email or self.flags.mute_email):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use state instead of "logic on the leaves" (of the code branches)

self.set_payment_request_url()
self.send_email()
self.make_communication_entry()
Expand Down Expand Up @@ -220,14 +213,12 @@ def on_cancel(self):
self.set_as_cancelled()

def make_invoice(self):
ref_doc = frappe.get_doc(self.reference_doctype, self.reference_name)
if hasattr(ref_doc, "order_type") and ref_doc.order_type == "Shopping Cart":
from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice
from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice

si = make_sales_invoice(self.reference_name, ignore_permissions=True)
si.allocate_advances_automatically = True
si = si.insert(ignore_permissions=True)
si.submit()
si = make_sales_invoice(self.reference_name, ignore_permissions=True)
si.allocate_advances_automatically = True
si = si.insert(ignore_permissions=True)
si.submit()
Comment on lines 215 to +221
Copy link
Collaborator Author

@blaggacao blaggacao May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shifting control to de caller for a more "atomic" make_invoice function (useful later)


def payment_gateway_validation(self):
try:
Expand Down Expand Up @@ -283,7 +274,8 @@ def set_as_paid(self):

else:
payment_entry = self.create_payment_entry()
self.make_invoice()
if self.make_sales_invoice:
self.make_invoice()

return payment_entry

Expand Down Expand Up @@ -412,9 +404,6 @@ def make_communication_entry(self):
)
comm.insert(ignore_permissions=True)

def get_payment_success_url(self):
return self.payment_success_url

Comment on lines -415 to -417
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no field payment_success_url

def create_subscription(self, payment_provider, gateway_controller, data):
if payment_provider == "stripe":
with payment_app_import_guard():
Expand Down Expand Up @@ -499,6 +488,10 @@ def make_payment_request(**args):
"party_type": args.get("party_type") or "Customer",
"party": args.get("party") or ref_doc.get("customer"),
"bank_account": bank_account,
"make_sales_invoice": args.order_type == "Shopping Cart",
"mute_email": args.mute_email
or args.order_type == "Shopping Cart"
or gateway_account.get("payment_channel", "Email") != "Email",
Comment on lines +491 to +494
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure intent is properly reflected in state

}
)

Expand All @@ -513,9 +506,6 @@ def make_payment_request(**args):
for dimension in get_accounting_dimensions():
pr.update({dimension: ref_doc.get(dimension)})

if args.order_type == "Shopping Cart" or args.mute_email:
pr.flags.mute_email = True

pr.insert(ignore_permissions=True)
if args.submit_doc:
pr.submit()
Expand Down Expand Up @@ -586,19 +576,14 @@ def get_gateway_details(args): # nosemgrep
Return gateway and payment account of default payment gateway
"""
gateway_account = args.get("payment_gateway_account", {"is_default": 1})
if gateway_account:
return get_payment_gateway_account(gateway_account)

gateway_account = get_payment_gateway_account({"is_default": 1})

return gateway_account
return get_payment_gateway_account(gateway_account)
Comment on lines -589 to +579
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundand logic



def get_payment_gateway_account(args):
def get_payment_gateway_account(filter):
return frappe.db.get_value(
"Payment Gateway Account",
args,
["name", "payment_gateway", "payment_account", "message"],
filter,
["name", "payment_gateway", "payment_account", "payment_channel", "message"],
Comment on lines -597 to +586
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter

as_dict=1,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_payment_channels(self):
pr.reload()

self.assertEqual(pr.payment_channel, "Phone")
self.assertEqual(pr.mute_email, False)
self.assertEqual(pr.mute_email, True)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally, make the test semantically more correct.


self.assertIsNone(pr.payment_url)
self.assertEqual(self.send_email.call_count, 0) # no increment on phone channel
Expand Down Expand Up @@ -178,7 +178,7 @@ def test_payment_channels(self):
pr.reload()

self.assertEqual(pr.payment_channel, "Email")
self.assertEqual(pr.mute_email, False)
self.assertEqual(pr.mute_email, True)

self.assertIsNone(pr.payment_url)
self.assertEqual(self.send_email.call_count, 1) # no increment on shopping cart
Expand Down
Loading