refactor: adding 'Get Allocations' button

This commit is contained in:
ruthra kumar 2023-08-24 14:52:26 +05:30
parent e48a90efe6
commit 5114a9580d
4 changed files with 62 additions and 20 deletions

View File

@ -30,7 +30,8 @@
"fieldname": "unlinked",
"fieldtype": "Check",
"in_list_view": 1,
"label": "Unlinked"
"label": "Unlinked",
"read_only": 1
},
{
"fieldname": "reference_doctype",
@ -43,7 +44,7 @@
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2023-08-22 15:00:33.203161",
"modified": "2023-08-24 14:48:10.018574",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Unreconcile Payment Entries",

View File

@ -22,4 +22,20 @@ frappe.ui.form.on("Unreconcile Payments", {
});
},
get_allocations: function(frm) {
frm.clear_table("allocations");
frappe.call({
method: "get_allocations_from_payment",
doc: frm.doc,
callback: function(r) {
if (r.message) {
r.message.forEach(x => {
frm.add_child("allocations", x)
})
frm.refresh_fields();
}
}
})
}
});

View File

@ -11,7 +11,8 @@
"company",
"voucher_type",
"voucher_no",
"references",
"get_allocations",
"allocations",
"amended_from"
],
"fields": [
@ -43,16 +44,21 @@
"options": "voucher_type"
},
{
"fieldname": "references",
"fieldname": "get_allocations",
"fieldtype": "Button",
"label": "Get Allocations"
},
{
"fieldname": "allocations",
"fieldtype": "Table",
"label": "References",
"label": "Allocations",
"options": "Unreconcile Payment Entries"
}
],
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2023-08-22 14:11:13.073414",
"modified": "2023-08-24 16:53:50.767700",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Unreconcile Payments",

View File

@ -2,25 +2,44 @@
# For license information, please see license.txt
import frappe
from frappe import qb
from frappe.model.document import Document
from frappe.query_builder.functions import Sum
from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries, update_voucher_outstanding
class UnreconcilePayments(Document):
def before_save(self):
if self.voucher_type == "Payment Entry":
references = frappe.db.get_all(
"Payment Entry Reference",
filters={"docstatus": 1, "parent": self.voucher_no},
fields=["reference_doctype", "reference_name", "allocated_amount"],
)
# def validate(self):
# parent = set([alloc.parent for alloc in self.allocations])
# if len(parent) != 1:
# pass
self.set("references", [])
for ref in references:
self.append("references", ref)
@frappe.whitelist()
def get_allocations_from_payment(self):
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)
)
return allocated_references
def add_references(self):
allocations = self.get_allocations_from_payment()
for alloc in allocations:
self.append("allocations", alloc)
def on_submit(self):
# 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},
@ -29,10 +48,10 @@ class UnreconcilePayments(Document):
)[0]
account = paid_from if payment_type == "Receive" else paid_to
for ref in self.references:
doc = frappe.get_doc(ref.reference_doctype, ref.reference_name)
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(
ref.reference_doctype, ref.reference_name, account, party_type, party
alloc.reference_doctype, alloc.reference_name, account, party_type, party
)
frappe.db.set_value("Unreconcile Payment Entries", ref.name, "unlinked", True)
frappe.db.set_value("Unreconcile Payment Entries", alloc.name, "unlinked", True)