fix: always update the advance payment status

This commit is contained in:
David Arnold 2024-01-22 22:15:49 +01:00
parent c88ce55242
commit b1aef01a1f
No known key found for this signature in database
GPG Key ID: AB15A6AF1101390D
2 changed files with 46 additions and 52 deletions

View File

@ -169,20 +169,12 @@ class PaymentRequest(Document):
elif self.payment_channel == "Phone":
self.request_phone_payment()
if (
self.reference_doctype in ["Sales Order"] and ref_doc.advance_payment_status == "Not Requested"
):
ref_doc.db_set("advance_payment_status", "Requested")
ref_doc.set_status(update=True)
ref_doc.notify_update()
if (
self.reference_doctype in ["Purchase Order"]
and ref_doc.advance_payment_status == "Not Initiated"
):
ref_doc.db_set("advance_payment_status", "Initiated")
ref_doc.set_status(update=True)
ref_doc.notify_update()
advance_payment_doctypes = frappe.get_hooks(
"advance_payment_customer_doctypes"
) + frappe.get_hooks("advance_payment_supplier_doctypes")
if self.reference_doctype in advance_payment_doctypes:
# set advance payment status
ref_doc.set_total_advance_paid()
def request_phone_payment(self):
controller = _get_payment_gateway_controller(self.payment_gateway)
@ -222,38 +214,13 @@ class PaymentRequest(Document):
self.check_if_payment_entry_exists()
self.set_as_cancelled()
if self.reference_doctype in ["Sales Order", "Purchase Order"]:
ref_doc = frappe.get_doc(self.reference_doctype, self.reference_name)
if self.reference_doctype in ["Sales Order"] and ref_doc.advance_payment_status == "Requested":
peer_pr = frappe.db.count(
"Payment Request",
{
"reference_doctype": self.reference_doctype,
"reference_name": self.reference_name,
"docstatus": 1,
},
)
if not peer_pr:
ref_doc.db_set("advance_payment_status", "Not Requested")
ref_doc.set_status(update=True)
ref_doc.notify_update()
if (
self.reference_doctype in ["Purchase Order"] and ref_doc.advance_payment_status == "Initiated"
):
peer_pr = frappe.db.count(
"Payment Request",
{
"reference_doctype": self.reference_doctype,
"reference_name": self.reference_name,
"docstatus": 1,
},
)
if not peer_pr:
ref_doc.db_set("advance_payment_status", "Not Initiated")
ref_doc.set_status(update=True)
ref_doc.notify_update()
advance_payment_doctypes = frappe.get_hooks(
"advance_payment_customer_doctypes"
) + frappe.get_hooks("advance_payment_supplier_doctypes")
if self.reference_doctype in advance_payment_doctypes:
# set advance payment status
ref_doc.set_total_advance_paid()
def make_invoice(self):
ref_doc = frappe.get_doc(self.reference_doctype, self.reference_name)

View File

@ -1766,6 +1766,8 @@ class AccountsController(TransactionBase):
.run(as_dict=True)
)
advance_paid, order_total = None, None
if advance:
advance = advance[0]
@ -1794,13 +1796,38 @@ class AccountsController(TransactionBase):
).format(formatted_advance_paid, self.name, formatted_order_total)
)
frappe.db.set_value(self.doctype, self.name, "advance_paid", advance_paid)
frappe.db.set_value(
self.doctype,
self.name,
"advance_payment_status",
"Partially Paid" if advance_paid < order_total else "Paid",
self.db_set("advance_paid", advance_paid)
self.set_advance_payment_status(advance_paid, order_total)
def set_advance_payment_status(
self, advance_paid: float | None = None, order_total: float | None = None
):
new_status = None
# if money is paid set the paid states
if advance_paid:
new_status = "Partially Paid" if advance_paid < order_total else "Paid"
if not new_status:
prs = frappe.db.count(
"Payment Request",
{
"reference_doctype": self.doctype,
"reference_name": self.name,
"docstatus": 1,
},
)
if self.doctype in frappe.get_hooks("advance_payment_customer_doctypes"):
new_status = "Requested" if prs else "Not Requested"
if self.doctype in frappe.get_hooks("advance_payment_supplier_doctypes"):
new_status = "Initiated" if prs else "Not Initiated"
if new_status == self.advance_payment_status:
return
self.db_set("advance_payment_status", new_status)
self.set_status(update=True)
self.notify_update()
@property
def company_abbr(self):