|
3 | 3 |
|
4 | 4 | import frappe |
5 | 5 | from frappe import _ |
| 6 | +from frappe.query_builder import functions as fn |
| 7 | +from frappe.query_builder.custom import ConstantColumn |
6 | 8 |
|
7 | 9 |
|
8 | 10 | def execute(filters: dict | None = None): |
@@ -64,29 +66,50 @@ def get_columns(filters) -> list[dict]: |
64 | 66 |
|
65 | 67 | def get_data(filters) -> list[list]: |
66 | 68 | loan = filters.get("loan") |
| 69 | + applicant = filters.get("applicant") |
| 70 | + loan_security = filters.get("loan_security") |
| 71 | + |
| 72 | + if not (loan or applicant): |
| 73 | + frappe.throw(_("Please select at least Loan or Applicant to view the ledger")) |
| 74 | + |
67 | 75 | balance_qty = 0 |
68 | 76 |
|
69 | | - unpledges = frappe.db.sql( |
70 | | - """ |
71 | | - SELECT up.loan, "Loan Security Release" as doctype, u.loan_security, u.loan_security_type, u.qty, up.unpledge_time as date |
72 | | - FROM `tabLoan Security Release` up, `tabUnpledge` u |
73 | | - WHERE up.loan = %s |
74 | | - AND u.parent = up.name |
75 | | - AND up.status = 'Approved' |
76 | | - GROUP BY u.loan_security |
77 | | - """, (loan), as_dict=1 |
78 | | - ) |
79 | | - |
80 | | - pledges = frappe.db.sql( |
81 | | - """ |
82 | | - SELECT lsa.loan, "Loan Security Assignment" as doctype, p.loan_security, p.loan_security_type, p.qty, lsa.pledge_time as date |
83 | | - FROM `tabLoan Security Assignment` lsa, `tabPledge` p |
84 | | - WHERE lsa.loan = %s |
85 | | - AND p.parent = lsa.name |
86 | | - AND lsa.status = 'Pledged' |
87 | | - GROUP BY p.loan_security |
88 | | - """, (loan), as_dict=1 |
89 | | - ) |
| 77 | + unpldge_doctype = frappe.qb.DocType("Unpledge") |
| 78 | + loan_security_release_doctype = frappe.qb.DocType("Loan Security Release") |
| 79 | + |
| 80 | + pledge_doctype = frappe.qb.DocType("Pledge") |
| 81 | + loan_security_assignment_doctype = frappe.qb.DocType("Loan Security Assignment") |
| 82 | + |
| 83 | + unpledge_query = frappe.qb.from_(unpldge_doctype).inner_join(loan_security_release_doctype).on( |
| 84 | + unpldge_doctype.parent == loan_security_release_doctype.name |
| 85 | + ).select( |
| 86 | + loan_security_release_doctype.loan, ConstantColumn("Loan Security Release").as_("doctype"), unpldge_doctype.loan_security, fn.Sum(unpldge_doctype.qty).as_("qty"), loan_security_release_doctype.unpledge_time.as_("date"), unpldge_doctype.loan_security_type |
| 87 | + ).where(loan_security_release_doctype.docstatus == 1).where(loan_security_release_doctype.status == "Approved") |
| 88 | + |
| 89 | + pledge_query = frappe.qb.from_(pledge_doctype).inner_join(loan_security_assignment_doctype).on( |
| 90 | + pledge_doctype.parent == loan_security_assignment_doctype.name |
| 91 | + ).select( |
| 92 | + loan_security_assignment_doctype.loan, ConstantColumn("Loan Security Assignment").as_("doctype"), pledge_doctype.loan_security, fn.Sum(pledge_doctype.qty).as_("qty"), loan_security_assignment_doctype.pledge_time.as_("date"), pledge_doctype.loan_security_type |
| 93 | + ).where(loan_security_assignment_doctype.docstatus == 1).where(loan_security_assignment_doctype.status == "Pledged") |
| 94 | + |
| 95 | + if loan: |
| 96 | + unpledge_query = unpledge_query.where(loan_security_release_doctype.loan == loan) |
| 97 | + pledge_query = pledge_query.where(loan_security_assignment_doctype.loan == loan) |
| 98 | + |
| 99 | + if applicant: |
| 100 | + unpledge_query = unpledge_query.where(loan_security_release_doctype.applicant == applicant) |
| 101 | + pledge_query = pledge_query.where(loan_security_assignment_doctype.applicant == applicant) |
| 102 | + |
| 103 | + if loan_security: |
| 104 | + unpledge_query = unpledge_query.where(unpldge_doctype.loan_security == loan_security) |
| 105 | + pledge_query = pledge_query.where(pledge_doctype.loan_security == loan_security) |
| 106 | + |
| 107 | + if loan_security: |
| 108 | + unpledge_query = unpledge_query.where(unpldge_doctype.loan_security == loan_security) |
| 109 | + pledge_query = pledge_query.where(pledge_doctype.loan_security == loan_security) |
| 110 | + |
| 111 | + unpledges = unpledge_query.groupby(unpldge_doctype.loan_security).run(as_dict=True) |
| 112 | + pledges = pledge_query.groupby(pledge_doctype.loan_security).run(as_dict=True) |
90 | 113 |
|
91 | 114 | result = pledges + unpledges |
92 | 115 |
|
|
0 commit comments