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", "fieldname": "unlinked",
"fieldtype": "Check", "fieldtype": "Check",
"in_list_view": 1, "in_list_view": 1,
"label": "Unlinked" "label": "Unlinked",
"read_only": 1
}, },
{ {
"fieldname": "reference_doctype", "fieldname": "reference_doctype",
@ -43,7 +44,7 @@
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"istable": 1, "istable": 1,
"links": [], "links": [],
"modified": "2023-08-22 15:00:33.203161", "modified": "2023-08-24 14:48:10.018574",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Accounts", "module": "Accounts",
"name": "Unreconcile Payment Entries", "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", "company",
"voucher_type", "voucher_type",
"voucher_no", "voucher_no",
"references", "get_allocations",
"allocations",
"amended_from" "amended_from"
], ],
"fields": [ "fields": [
@ -43,16 +44,21 @@
"options": "voucher_type" "options": "voucher_type"
}, },
{ {
"fieldname": "references", "fieldname": "get_allocations",
"fieldtype": "Button",
"label": "Get Allocations"
},
{
"fieldname": "allocations",
"fieldtype": "Table", "fieldtype": "Table",
"label": "References", "label": "Allocations",
"options": "Unreconcile Payment Entries" "options": "Unreconcile Payment Entries"
} }
], ],
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"is_submittable": 1, "is_submittable": 1,
"links": [], "links": [],
"modified": "2023-08-22 14:11:13.073414", "modified": "2023-08-24 16:53:50.767700",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Accounts", "module": "Accounts",
"name": "Unreconcile Payments", "name": "Unreconcile Payments",

View File

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