Skip to content

Commit

Permalink
chore: complete renaming loan security pledge and unpledge
Browse files Browse the repository at this point in the history
  • Loading branch information
anandbaburajan committed Oct 24, 2023
1 parent 8dfe196 commit 077713b
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_data(
frappe.db.sql(
"""
SELECT u.loan_security, sum(u.qty) as qty
FROM `tabLoan Security Unpledge` up, `tabUnpledge` u
FROM `tabLoan Security Release` up, `tabUnpledge` u
WHERE u.parent = up.name
AND up.status = 'Approved'
{conditions}
Expand All @@ -64,7 +64,7 @@ def get_data(
frappe.db.sql(
"""
SELECT p.loan_security, sum(p.qty) as qty
FROM `tabLoan Security Pledge` lp, `tabPledge`p
FROM `tabLoan Security Assignment` lp, `tabPledge`p
WHERE p.parent = lp.name
AND lp.status = 'Pledged'
{conditions}
Expand Down
8 changes: 4 additions & 4 deletions lending/loan_management/doctype/loan/loan.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ frappe.ui.form.on('Loan', {
setup: function(frm) {
frm.make_methods = {
'Loan Disbursement': function() { frm.trigger('make_loan_disbursement') },
'Loan Security Release': function() { frm.trigger('create_loan_security_unpledge') },
'Loan Security Release': function() { frm.trigger('create_loan_security_release') },
'Loan Write Off': function() { frm.trigger('make_loan_write_off_entry') }
}
},
onload: function (frm) {
// Ignore loan security pledge on cancel of loan
// Ignore Loan Security Assignment on cancel of loan
frm.ignore_doctypes_on_cancel_all = ["Loan Security Assignment", "Loan Repayment Schedule"];

frm.set_query("loan_application", function () {
Expand Down Expand Up @@ -83,7 +83,7 @@ frappe.ui.form.on('Loan', {

if (frm.doc.status == "Loan Closure Requested") {
frm.add_custom_button(__('Loan Security Release'), function() {
frm.trigger("create_loan_security_unpledge");
frm.trigger("create_loan_security_release");
},__('Create'));
}

Expand Down Expand Up @@ -219,7 +219,7 @@ frappe.ui.form.on('Loan', {
);
},

create_loan_security_unpledge: function(frm) {
create_loan_security_release: function(frm) {
frappe.call({
method: "lending.loan_management.doctype.loan.loan.unpledge_security",
args : {
Expand Down
32 changes: 19 additions & 13 deletions lending/loan_management/doctype/loan/loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ def set_cyclic_date(self):
self.repayment_start_date = cyclic_date

def on_submit(self):
self.link_loan_security_pledge()
self.link_loan_security_assignment()
# Interest accrual for backdated term loans
self.accrue_loan_interest()
self.submit_draft_schedule()

def on_cancel(self):
self.unlink_loan_security_pledge()
self.unlink_loan_security_assignment()
self.cancel_and_delete_repayment_schedule()
self.ignore_linked_doctypes = ["GL Entry", "Payment Ledger Entry"]

Expand Down Expand Up @@ -221,7 +221,7 @@ def validate_loan_amount(self):
if not self.loan_amount:
frappe.throw(_("Loan amount is mandatory"))

def link_loan_security_pledge(self):
def link_loan_security_assignment(self):
if self.is_secured_loan and self.loan_application:
maximum_loan_value = frappe.db.get_value(
"Loan Security Assignment",
Expand All @@ -232,7 +232,7 @@ def link_loan_security_pledge(self):
if maximum_loan_value:
frappe.db.sql(
"""
UPDATE `tabLoan Security Pledge`
UPDATE `tabLoan Security Assignment`
SET loan = %s, pledge_time = %s, status = 'Pledged'
WHERE status = 'Requested' and loan_application = %s
""",
Expand All @@ -251,14 +251,14 @@ def accrue_loan_interest(self):
posting_date=getdate(), loan_product=self.loan_product, loan=self.name
)

def unlink_loan_security_pledge(self):
def unlink_loan_security_assignment(self):
pledges = frappe.get_all(
"Loan Security Assignment", fields=["name"], filters={"loan": self.name}
)
pledge_list = [d.name for d in pledges]
if pledge_list:
frappe.db.sql(
"""UPDATE `tabLoan Security Pledge` SET
"""UPDATE `tabLoan Security Assignment` SET
loan = '', status = 'Unpledged'
where name in (%s) """
% (", ".join(["%s"] * len(pledge_list))),
Expand Down Expand Up @@ -460,7 +460,13 @@ def make_loan_write_off(loan, company=None, posting_date=None, amount=0, as_dict

@frappe.whitelist()
def unpledge_security(
loan=None, loan_security_pledge=None, security_map=None, as_dict=0, save=0, submit=0, approve=0
loan=None,
loan_security_assignment=None,
security_map=None,
as_dict=0,
save=0,
submit=0,
approve=0,
):
# if no security_map is passed it will be considered as full unpledge
if security_map and isinstance(security_map, str):
Expand All @@ -469,17 +475,17 @@ def unpledge_security(
if loan:
pledge_qty_map = security_map or get_pledged_security_qty(loan)
loan_doc = frappe.get_doc("Loan", loan)
unpledge_request = create_loan_security_unpledge(
unpledge_request = create_loan_security_release(
pledge_qty_map, loan_doc.name, loan_doc.company, loan_doc.applicant_type, loan_doc.applicant
)
# will unpledge qty based on loan security pledge
elif loan_security_pledge:
# will unpledge qty based on Loan Security Assignment
elif loan_security_assignment:
security_map = {}
pledge_doc = frappe.get_doc("Loan Security Assignment", loan_security_pledge)
pledge_doc = frappe.get_doc("Loan Security Assignment", loan_security_assignment)
for security in pledge_doc.securities:
security_map.setdefault(security.loan_security, security.qty)

unpledge_request = create_loan_security_unpledge(
unpledge_request = create_loan_security_release(
security_map,
pledge_doc.loan,
pledge_doc.company,
Expand All @@ -506,7 +512,7 @@ def unpledge_security(
return unpledge_request


def create_loan_security_unpledge(unpledge_map, loan, company, applicant_type, applicant):
def create_loan_security_release(unpledge_map, loan, company, applicant_type, applicant):
unpledge_request = frappe.new_doc("Loan Security Release")
unpledge_request.applicant_type = applicant_type
unpledge_request.applicant = applicant
Expand Down
12 changes: 7 additions & 5 deletions lending/loan_management/doctype/loan/test_loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ def test_sanctioned_amount_limit(self):
# Clear loan docs before checking
frappe.db.sql("DELETE FROM `tabLoan` where applicant = '_Test Loan Customer 1'")
frappe.db.sql("DELETE FROM `tabLoan Application` where applicant = '_Test Loan Customer 1'")
frappe.db.sql("DELETE FROM `tabLoan Security Pledge` where applicant = '_Test Loan Customer 1'")
frappe.db.sql(
"DELETE FROM `tabLoan Security Assignment` where applicant = '_Test Loan Customer 1'"
)

if not frappe.db.get_value(
"Sanctioned Loan Amount",
Expand Down Expand Up @@ -458,7 +460,7 @@ def test_security_shortfall(self):
self.assertEqual(loan_security_shortfall.status, "Completed")
self.assertEqual(loan_security_shortfall.shortfall_amount, 0)

def test_loan_security_unpledge(self):
def test_loan_security_release(self):
pledge = [{"loan_security": "Test Security 1", "qty": 4000.00}]

loan_application = create_loan_application(
Expand Down Expand Up @@ -515,7 +517,7 @@ def test_loan_security_unpledge(self):
self.assertEqual(amounts["payable_principal_amount"], 0.0)
self.assertEqual(amounts["interest_amount"], 0)

def test_partial_loan_security_unpledge(self):
def test_partial_loan_security_release(self):
pledge = [
{"loan_security": "Test Security 1", "qty": 2000.00},
{"loan_security": "Test Security 2", "qty": 4000.00},
Expand Down Expand Up @@ -554,7 +556,7 @@ def test_partial_loan_security_unpledge(self):
unpledge_request.load_from_db()
self.assertEqual(unpledge_request.docstatus, 1)

def test_sanctioned_loan_security_unpledge(self):
def test_sanctioned_loan_security_release(self):
pledge = [{"loan_security": "Test Security 1", "qty": 4000.00}]

loan_application = create_loan_application(
Expand Down Expand Up @@ -1144,7 +1146,7 @@ def create_loan_security():
).insert(ignore_permissions=True)


def create_loan_security_pledge(applicant, pledges, loan_application=None, loan=None):
def create_loan_security_assignment(applicant, pledges, loan_application=None, loan=None):

lsp = frappe.new_doc("Loan Security Assignment")
lsp.applicant_type = "Customer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ frappe.ui.form.on('Loan Application', {
setup: function(frm) {
frm.make_methods = {
'Loan': function() { frm.trigger('create_loan') },
'Loan Security Assignment': function() { frm.trigger('create_loan_security_pledge') },
'Loan Security Assignment': function() { frm.trigger('create_loan_security_assignment') },
}
},
refresh: function(frm) {
Expand Down Expand Up @@ -42,7 +42,7 @@ frappe.ui.form.on('Loan Application', {
frappe.db.get_value("Loan Security Assignment", {"loan_application": frm.doc.name, "docstatus": 1}, "name", (r) => {
if (Object.keys(r).length === 0) {
frm.add_custom_button(__('Loan Security Assignment'), function() {
frm.trigger('create_loan_security_pledge');
frm.trigger('create_loan_security_assignment');
},__('Create'))
}
});
Expand All @@ -69,7 +69,7 @@ frappe.ui.form.on('Loan Application', {
frm: frm
});
},
create_loan_security_pledge: function(frm) {
create_loan_security_assignment: function(frm) {

if(!frm.doc.is_secured_loan) {
frappe.throw(__("Loan Security Assignment can only be created for secured loans"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
create_loan_application,
create_loan_product,
create_loan_security,
create_loan_security_pledge,
create_loan_security_assignment,
create_loan_security_price,
create_loan_security_type,
create_repayment_entry,
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_loan_topup_with_additional_pledge(self):

pledge1 = [{"loan_security": "Test Security 1", "qty": 2000.00}]

create_loan_security_pledge(self.applicant, pledge1, loan=loan.name)
create_loan_security_assignment(self.applicant, pledge1, loan=loan.name)

# Topup 500000
make_loan_disbursement_entry(loan.name, 500000, disbursement_date=add_days(last_date, 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def get_pledged_security_qty(loan):
frappe.db.sql(
"""
SELECT u.loan_security, sum(u.qty) as qty
FROM `tabLoan Security Unpledge` up, `tabUnpledge` u
FROM `tabLoan Security Release` up, `tabUnpledge` u
WHERE up.loan = %s
AND u.parent = up.name
AND up.status = 'Approved'
Expand All @@ -162,7 +162,7 @@ def get_pledged_security_qty(loan):
frappe.db.sql(
"""
SELECT p.loan_security, sum(p.qty) as qty
FROM `tabLoan Security Pledge` lp, `tabPledge`p
FROM `tabLoan Security Assignment` lp, `tabPledge`p
WHERE lp.loan = %s
AND p.parent = lp.name
AND lp.status = 'Pledged'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def add_security(loan):
"Loan", loan, ["applicant", "company", "applicant_type"], as_dict=1
)

loan_security_pledge = frappe.new_doc("Loan Security Assignment")
loan_security_pledge.loan = loan
loan_security_pledge.company = loan_details.company
loan_security_pledge.applicant_type = loan_details.applicant_type
loan_security_pledge.applicant = loan_details.applicant
loan_security_assignment = frappe.new_doc("Loan Security Assignment")
loan_security_assignment.loan = loan
loan_security_assignment.company = loan_details.company
loan_security_assignment.applicant_type = loan_details.applicant_type
loan_security_assignment.applicant = loan_details.applicant

return loan_security_pledge.as_dict()
return loan_security_assignment.as_dict()


def check_for_ltv_shortfall(process_loan_security_shortfall):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"read_only": 1
},
{
"fetch_from": "loan_security_pledge.qty",
"fetch_from": "loan_security_assignment.qty",
"fieldname": "qty",
"fieldtype": "Float",
"in_list_view": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def get_applicant_wise_total_loan_security_qty(filters, loan_security_details):
unpledges = frappe.db.sql(
"""
SELECT up.applicant, u.loan_security, sum(u.qty) as qty
FROM `tabLoan Security Unpledge` up, `tabUnpledge` u
FROM `tabLoan Security Release` up, `tabUnpledge` u
WHERE u.parent = up.name
AND up.status = 'Approved'
{conditions}
Expand All @@ -208,7 +208,7 @@ def get_applicant_wise_total_loan_security_qty(filters, loan_security_details):
pledges = frappe.db.sql(
"""
SELECT lp.applicant_type, lp.applicant, p.loan_security, sum(p.qty) as qty
FROM `tabLoan Security Pledge` lp, `tabPledge`p
FROM `tabLoan Security Assignment` lp, `tabPledge`p
WHERE p.parent = lp.name
AND lp.status = 'Pledged'
{conditions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def get_loan_wise_pledges(filters):
unpledges = frappe.db.sql(
"""
SELECT up.loan, u.loan_security, sum(u.qty) as qty
FROM `tabLoan Security Unpledge` up, `tabUnpledge` u
FROM `tabLoan Security Release` up, `tabUnpledge` u
WHERE u.parent = up.name
AND up.status = 'Approved'
{conditions}
Expand All @@ -345,7 +345,7 @@ def get_loan_wise_pledges(filters):
pledges = frappe.db.sql(
"""
SELECT lp.loan, p.loan_security, sum(p.qty) as qty
FROM `tabLoan Security Pledge` lp, `tabPledge`p
FROM `tabLoan Security Assignment` lp, `tabPledge`p
WHERE p.parent = lp.name
AND lp.status = 'Pledged'
{conditions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_columns(filters):
{
"label": _("Loan Security Assignment"),
"fieldtype": "Link",
"fieldname": "loan_security_pledge",
"fieldname": "loan_security_assignment",
"options": "Loan Security Assignment",
"width": 200,
},
Expand Down Expand Up @@ -65,13 +65,13 @@ def get_data(filters):
data = []
conditions = get_conditions(filters)

loan_security_pledges = frappe.db.sql(
loan_security_assignments = frappe.db.sql(
"""
SELECT
p.name, p.applicant, p.loan, p.status, p.pledge_time,
c.loan_security, c.qty, c.loan_security_price, c.amount
FROM
`tabLoan Security Pledge` p, `tabPledge` c
`tabLoan Security Assignment` p, `tabPledge` c
WHERE
p.docstatus = 1
AND c.parent = p.name
Expand All @@ -86,9 +86,9 @@ def get_data(filters):

default_currency = frappe.get_cached_value("Company", filters.get("company"), "default_currency")

for pledge in loan_security_pledges:
for pledge in loan_security_assignments:
row = {}
row["loan_security_pledge"] = pledge.name
row["loan_security_assignment"] = pledge.name
row["loan"] = pledge.loan
row["applicant"] = pledge.applicant
row["status"] = pledge.status
Expand Down

0 comments on commit 077713b

Please sign in to comment.