Skip to content

Commit

Permalink
chore: move avail and util value fields to loan security
Browse files Browse the repository at this point in the history
  • Loading branch information
anandbaburajan committed Nov 30, 2023
1 parent b785b2a commit 480b91b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 103 deletions.
27 changes: 26 additions & 1 deletion lending/loan_management/doctype/loan_security/loan_security.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
"loan_security_code",
"loan_security_name",
"haircut",
"original_security_value",
"utilized_security_value",
"column_break_3",
"loan_security_type",
"available_security_value",
"disabled"
],
"fields": [
Expand Down Expand Up @@ -53,11 +56,33 @@
"fieldname": "disabled",
"fieldtype": "Check",
"label": "Disabled"
},
{
"depends_on": "eval:doc.original_security_value",
"fieldname": "utilized_security_value",
"fieldtype": "Currency",
"label": "Utilized Security Value",
"options": "Company:company:default_currency",
"read_only": 1
},
{
"depends_on": "eval:doc.original_security_value",
"fieldname": "available_security_value",
"fieldtype": "Currency",
"label": "Available Security Value",
"options": "Company:company:default_currency",
"read_only": 1
},
{
"fieldname": "original_security_value",
"fieldtype": "Currency",
"label": "Original Security Value",
"options": "Company:company:default_currency"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2023-11-30 15:37:00.788174",
"modified": "2023-11-30 23:46:49.038702",
"modified_by": "Administrator",
"module": "Loan Management",
"name": "Loan Security",
Expand Down
22 changes: 20 additions & 2 deletions lending/loan_management/doctype/loan_security/loan_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@
# For license information, please see license.txt


# import frappe
import frappe
from frappe.model.document import Document

from lending.loan_management.doctype.loan_security_price.loan_security_price import (
get_loan_security_price,
)


class LoanSecurity(Document):
pass
def before_insert(self):
self.available_security_value = self.original_security_value


@frappe.whitelist()
def get_loan_security_price_or_value(loan_security):
loan_security_price = get_loan_security_price(loan_security)

if loan_security_price:
return {"qty": None, "value": loan_security_price}

return {
"qty": 1,
"value": frappe.db.get_value("Loan Security", loan_security, "available_security_value"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ frappe.ui.form.on("Pledge", {

if (row.loan_security) {
frappe.call({
method: "lending.loan_management.doctype.loan_security_price.loan_security_price.get_loan_security_price",
method: "lending.loan_management.doctype.loan_security.loan_security.get_loan_security_price_or_value",
args: {
loan_security: row.loan_security
},
callback: function(r) {
frappe.model.set_value(cdt, cdn, 'loan_security_price', r.message);
frappe.model.set_value(cdt, cdn, 'qty', r.message['qty']);
frappe.model.set_value(cdt, cdn, 'loan_security_price', r.message['value']);
frm.events.calculate_amounts(frm, cdt, cdn);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
"allocated_loan_applications",
"section_break_10",
"total_security_value",
"utilized_security_value",
"column_break_11",
"maximum_loan_value",
"available_security_value",
"more_information_section",
"reference_no",
"column_break_18",
Expand Down Expand Up @@ -165,20 +163,6 @@
"fieldtype": "Section Break",
"label": "Applicant Details"
},
{
"fieldname": "utilized_security_value",
"fieldtype": "Currency",
"label": "Utilized Security Value",
"options": "Company:company:default_currency",
"read_only": 1
},
{
"fieldname": "available_security_value",
"fieldtype": "Currency",
"label": "Available Security Value",
"options": "Company:company:default_currency",
"read_only": 1
},
{
"fieldname": "loan_applications_section",
"fieldtype": "Section Break",
Expand Down Expand Up @@ -216,7 +200,7 @@
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2023-10-26 16:39:55.670214",
"modified": "2023-11-30 23:47:18.667979",
"modified_by": "Administrator",
"module": "Loan Management",
"name": "Loan Security Assignment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,8 @@ def set_loan_and_security_values(self):
self.total_security_value = total_security_value
self.maximum_loan_value = maximum_loan_value

self.available_security_value = self.total_security_value

def check_loan_securities_capability_to_book_additional_loans(self):
total_security_value_needed = 0

for d in self.get("allocated_loans"):
loan_amount, status = frappe.db.get_value("Loan", d.loan, ["loan_amount", "status"])

Expand All @@ -125,15 +122,21 @@ def check_loan_securities_capability_to_book_additional_loans(self):

total_security_value_needed += loan_amount

if total_security_value_needed > self.available_security_value:
total_available_security_value = 0
for d in self.get("securities"):
total_available_security_value += frappe.db.get_value(
"Loan Security", d.loan_security, "available_security_value"
)

if total_security_value_needed > total_available_security_value:
frappe.throw(
_("Loan Securities worth {0} needed more to book the loan").format(
frappe.bold(flt(total_security_value_needed - self.available_security_value, 2)),
frappe.bold(flt(total_security_value_needed - total_available_security_value, 2)),
)
)

for d in self.get("allocated_loans"):
update_loan(d.loan, self.available_security_value)
update_loan(d.loan, total_available_security_value)


def update_loan(loan, maximum_value_against_pledge, cancel=0):
Expand Down Expand Up @@ -169,108 +172,57 @@ def update_loan_securities_values(
else False
)

sorted_loan_security_assignments = _get_sorted_loan_security_assignments(
loan, utilized_value_increased
)

_update_loan_securities_values(
sorted_loan_security_assignments,
amount,
utilized_value_increased,
)


def _get_sorted_loan_security_assignments(loan, utilized_value_increased):
loan_security_assignments_w_ratio = []

ls = frappe.qb.DocType("Loan Security")
lsa = frappe.qb.DocType("Loan Security Assignment")
lsald = frappe.qb.DocType("Loan Security Assignment Loan Detail")
pledge = frappe.qb.DocType("Pledge")

loan_security_assignments = (
loan_securities = (
frappe.qb.from_(lsa)
.inner_join(lsald)
.on(lsald.parent == lsa.name)
.inner_join(pledge)
.on(pledge.parent == lsa.name)
.inner_join(ls)
.on(pledge.loan_security == ls.name)
.select(
lsa.name,
lsa.total_security_value,
lsa.utilized_security_value,
lsa.available_security_value,
ls.name,
ls.available_security_value,
ls.utilized_security_value,
ls.original_security_value,
)
.where(lsa.status == "Pledged")
.where(lsald.loan == loan)
).run(as_dict=True)

for loan_security_assignment in loan_security_assignments:
utilized_to_original_value_ratio = flt(
loan_security_assignment.utilized_security_value / loan_security_assignment.total_security_value
)

loan_security_assignments_w_ratio.append(
frappe._dict(
{
"name": loan_security_assignment.name,
"total_security_value": loan_security_assignment.total_security_value,
"utilized_security_value": loan_security_assignment.utilized_security_value,
"available_security_value": loan_security_assignment.available_security_value,
"ratio": utilized_to_original_value_ratio,
}
)
)

sorted_loan_security_assignments = sorted(
loan_security_assignments_w_ratio,
key=lambda k: k["ratio"],
reverse=utilized_value_increased,
)

return sorted_loan_security_assignments


def _update_loan_securities_values(
sorted_loan_security_assignments,
amount,
utilized_value_increased,
):
for loan_security_assignment in sorted_loan_security_assignments:
for loan_security in loan_securities:
if amount <= 0:
break

if utilized_value_increased:
if (
loan_security_assignment.utilized_security_value + amount
> loan_security_assignment.total_security_value
):
new_utilized_security_value = loan_security_assignment.total_security_value
if loan_security.utilized_security_value + amount > loan_security.original_security_value:
new_utilized_security_value = loan_security.original_security_value
new_available_security_value = 0
amount = (
amount
+ loan_security_assignment.utilized_security_value
- loan_security_assignment.total_security_value
)
amount = amount + loan_security.utilized_security_value - loan_security.original_security_value
else:
new_utilized_security_value = loan_security_assignment.utilized_security_value + amount
new_available_security_value = loan_security_assignment.available_security_value - amount
new_utilized_security_value = loan_security.utilized_security_value + amount
new_available_security_value = loan_security.available_security_value - amount
amount = 0
else:
if (
loan_security_assignment.available_security_value + amount
> loan_security_assignment.total_security_value
):
new_available_security_value = loan_security_assignment.total_security_value
if loan_security.available_security_value + amount > loan_security.original_security_value:
new_available_security_value = loan_security.original_security_value
new_utilized_security_value = 0
amount = (
amount
+ loan_security_assignment.available_security_value
- loan_security_assignment.total_security_value
amount + loan_security.available_security_value - loan_security.original_security_value
)
else:
new_utilized_security_value = loan_security_assignment.utilized_security_value - amount
new_available_security_value = loan_security_assignment.available_security_value + amount
new_utilized_security_value = loan_security.utilized_security_value - amount
new_available_security_value = loan_security.available_security_value + amount
amount = 0

frappe.db.set_value(
"Loan Security Assignment",
loan_security_assignment.name,
"Loan Security",
loan_security.name,
{
"utilized_security_value": new_utilized_security_value,
"available_security_value": new_available_security_value,
Expand Down

0 comments on commit 480b91b

Please sign in to comment.