refactor: single fetch and unlinking logic for JE and PE

This commit is contained in:
ruthra kumar 2023-08-30 11:02:03 +05:30
parent 285963acdb
commit de910ab152
2 changed files with 38 additions and 40 deletions

View File

@ -6,6 +6,9 @@
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"account",
"party_type",
"party",
"reference_doctype",
"reference_name",
"allocated_amount",
@ -39,12 +42,27 @@
"in_list_view": 1,
"label": "Reference Type",
"options": "DocType"
},
{
"fieldname": "account",
"fieldtype": "Data",
"label": "Account"
},
{
"fieldname": "party_type",
"fieldtype": "Data",
"label": "Party Type"
},
{
"fieldname": "party",
"fieldtype": "Data",
"label": "Party"
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2023-08-24 14:48:10.018574",
"modified": "2023-08-30 10:58:45.322668",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Unreconcile Payment Entries",

View File

@ -20,38 +20,26 @@ class UnreconcilePayments(Document):
@frappe.whitelist()
def get_allocations_from_payment(self):
allocated_references = []
if self.voucher_type == "Payment Entry":
per = qb.DocType("Payment Entry Reference")
allocated_references = (
qb.from_(per)
.select(
per.reference_doctype, per.reference_name, Sum(per.allocated_amount).as_("allocated_amount")
)
.where((per.docstatus == 1) & (per.parent == self.voucher_no))
.groupby(per.reference_name)
.run(as_dict=True)
ple = qb.DocType("Payment Ledger Entry")
allocated_references = (
qb.from_(ple)
.select(
ple.account,
ple.party_type,
ple.party,
ple.against_voucher_type.as_("reference_doctype"),
ple.against_voucher_no.as_("reference_name"),
Abs(Sum(ple.amount_in_account_currency)).as_("allocated_amount"),
)
elif self.voucher_type == "Journal Entry":
# for journals, using payment ledger to fetch allocation.
# this way we can avoid vaildating account type and reference details individually on child table
ple = qb.DocType("Payment Ledger Entry")
allocated_references = (
qb.from_(ple)
.select(
ple.against_voucher_type.as_("reference_doctype"),
ple.against_voucher_no.as_("reference_name"),
Abs(Sum(ple.amount_in_account_currency)).as_("allocated_amount"),
)
.where(
(ple.docstatus == 1)
& (ple.voucher_type == self.voucher_type)
& (ple.voucher_no == self.voucher_no)
& (ple.voucher_no != ple.against_voucher_no)
)
.groupby(ple.against_voucher_type, ple.against_voucher_no)
.run(as_dict=True)
.where(
(ple.docstatus == 1)
& (ple.voucher_type == self.voucher_type)
& (ple.voucher_no == self.voucher_no)
& (ple.voucher_no != ple.against_voucher_no)
)
.groupby(ple.against_voucher_type, ple.against_voucher_no)
.run(as_dict=True)
)
return allocated_references
@ -65,19 +53,11 @@ class UnreconcilePayments(Document):
# todo: add more granular unlinking
# different amounts for same invoice should be individually unlinkable
payment_type, paid_from, paid_to, party_type, party = frappe.db.get_all(
self.voucher_type,
filters={"name": self.voucher_no},
fields=["payment_type", "paid_from", "paid_to", "party_type", "party"],
as_list=1,
)[0]
account = paid_from if payment_type == "Receive" else paid_to
for alloc in self.allocations:
doc = frappe.get_doc(alloc.reference_doctype, alloc.reference_name)
unlink_ref_doc_from_payment_entries(doc)
update_voucher_outstanding(
alloc.reference_doctype, alloc.reference_name, account, party_type, party
alloc.reference_doctype, alloc.reference_name, alloc.account, alloc.party_type, alloc.party
)
frappe.db.set_value("Unreconcile Payment Entries", alloc.name, "unlinked", True)