fix: Add filters and currency in reports

This commit is contained in:
Deepesh Garg 2021-01-17 14:40:32 +05:30
parent addea9697d
commit 914ab7e4b0
6 changed files with 93 additions and 35 deletions

View File

@ -30,7 +30,7 @@
],
"is_submittable": 1,
"links": [],
"modified": "2020-02-01 08:14:05.845161",
"modified": "2021-01-17 03:59:14.494557",
"modified_by": "Administrator",
"module": "Loan Management",
"name": "Process Loan Security Shortfall",
@ -45,7 +45,9 @@
"read": 1,
"report": 1,
"role": "System Manager",
"select": 1,
"share": 1,
"submit": 1,
"write": 1
},
{
@ -57,7 +59,9 @@
"read": 1,
"report": 1,
"role": "Loan Manager",
"select": 1,
"share": 1,
"submit": 1,
"write": 1
}
],

View File

@ -4,6 +4,13 @@
frappe.query_reports["Applicant-Wise Loan Security Exposure"] = {
"filters": [
{
"fieldname":"company",
"label": __("Company"),
"fieldtype": "Link",
"options": "Company",
"default": frappe.defaults.get_user_default("Company"),
"reqd": 1
}
]
};

View File

@ -3,6 +3,7 @@
from __future__ import unicode_literals
import frappe
import erpnext
from frappe import _
from frappe.utils import get_datetime, flt
from six import iteritems
@ -24,9 +25,10 @@ def get_columns(filters):
{"label": _("Loan Security Type"), "fieldname": "loan_security_type", "fieldtype": "Link", "options": "Loan Security Type", "width": 120},
{"label": _("Disabled"), "fieldname": "disabled", "fieldtype": "Check", "width": 80},
{"label": _("Total Qty"), "fieldname": "total_qty", "fieldtype": "Float", "width": 100},
{"label": _("Latest Price"), "fieldname": "latest_price", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Current Value"), "fieldname": "current_value", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("% Of Applicant Portfolio"), "fieldname": "portfolio_percent", "fieldtype": "Percentage", "width": 100}
{"label": _("Latest Price"), "fieldname": "latest_price", "fieldtype": "Currency", "options": "currency", "width": 100},
{"label": _("Current Value"), "fieldname": "current_value", "fieldtype": "Currency", "options": "currency", "width": 100},
{"label": _("% Of Applicant Portfolio"), "fieldname": "portfolio_percent", "fieldtype": "Percentage", "width": 100},
{"label": _("Currency"), "fieldname": "currency", "fieldtype": "Currency", "options": "Currency", "hidden": 1, "width": 100},
]
return columns
@ -34,7 +36,10 @@ def get_columns(filters):
def get_data(filters):
data = []
loan_security_details = get_loan_security_details(filters)
pledge_values, total_value_map, applicant_type_map = get_applicant_wise_total_loan_security_qty(loan_security_details)
pledge_values, total_value_map, applicant_type_map = get_applicant_wise_total_loan_security_qty(filters,
loan_security_details)
currency = erpnext.get_company_currency(filters.get('company'))
for key, qty in iteritems(pledge_values):
row = {}
@ -43,9 +48,10 @@ def get_data(filters):
row.update({
'applicant_type': applicant_type_map.get(key[0]),
'applicant_name': key[0],
'total_qty': qty,
'total_qty': qty,
'current_value': current_value,
'portfolio_percent': current_value * 100 / total_value_map.get(key[0])
'portfolio_percent': flt(current_value * 100 / total_value_map.get(key[0]), 2),
'currency': currency
})
data.append(row)
@ -73,19 +79,24 @@ def get_loan_security_details(filters):
return security_detail_map
def get_applicant_wise_total_loan_security_qty(loan_security_details):
def get_applicant_wise_total_loan_security_qty(filters, loan_security_details):
current_pledges = {}
total_value_map = {}
applicant_type_map = {}
applicant_wise_unpledges = {}
conditions = ""
if filters.get('company'):
conditions = "AND company = %(company)s"
unpledges = frappe.db.sql("""
SELECT up.applicant, u.loan_security, sum(u.qty) as qty
FROM `tabLoan Security Unpledge` up, `tabUnpledge` u
WHERE u.parent = up.name
AND up.status = 'Approved'
{conditions}
GROUP BY up.applicant, u.loan_security
""", as_dict=1)
""".format(conditions=conditions), filters, as_dict=1)
for unpledge in unpledges:
applicant_wise_unpledges.setdefault((unpledge.applicant, unpledge.loan_security), unpledge.qty)
@ -95,8 +106,9 @@ def get_applicant_wise_total_loan_security_qty(loan_security_details):
FROM `tabLoan Security Pledge` lp, `tabPledge`p
WHERE p.parent = lp.name
AND lp.status = 'Pledged'
{conditions}
GROUP BY lp.applicant, p.loan_security
""", as_dict=1)
""".format(conditions=conditions), filters, as_dict=1)
for security in pledges:
current_pledges.setdefault((security.applicant, security.loan_security), security.qty)

View File

@ -4,6 +4,13 @@
frappe.query_reports["Loan Interest Report"] = {
"filters": [
{
"fieldname":"company",
"label": __("Company"),
"fieldtype": "Link",
"options": "Company",
"default": frappe.defaults.get_user_default("Company"),
"reqd": 1
}
]
};

View File

@ -3,6 +3,7 @@
from __future__ import unicode_literals
import frappe
import erpnext
from frappe import _
from frappe.utils import flt, get_first_day, getdate
@ -19,33 +20,41 @@ def get_columns(filters):
{"label": _("Applicant Type"), "fieldname": "applicant_type", "options": "DocType", "width": 100},
{"label": _("Applicant Name"), "fieldname": "applicant_name", "fieldtype": "Dynamic Link", "options": "applicant_type", "width": 150},
{"label": _("Loan Type"), "fieldname": "loan_type", "fieldtype": "Link", "options": "Loan Type", "width": 100},
{"label": _("Sanctioned Amount"), "fieldname": "sanctioned_amount", "fieldtype": "Currency", "options": "Currency", "width": 120},
{"label": _("Disbursed Amount"), "fieldname": "disbursed_amount", "fieldtype": "Currency", "options": "Currency", "width": 120},
{"label": _("Interest For The Month"), "fieldname": "month_interest", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Penalty For The Month"), "fieldname": "month_penalty", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Accrued Interest"), "fieldname": "accrued_interest", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Total Repayment"), "fieldname": "total_repayment", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Principal Outstanding"), "fieldname": "principal_outstanding", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Interest Outstanding"), "fieldname": "interest_outstanding", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Total Outstanding"), "fieldname": "total_payment", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Sanctioned Amount"), "fieldname": "sanctioned_amount", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Disbursed Amount"), "fieldname": "disbursed_amount", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Interest For The Month"), "fieldname": "month_interest", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Penalty For The Month"), "fieldname": "penalty", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Accrued Interest"), "fieldname": "accrued_interest", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Total Repayment"), "fieldname": "total_repayment", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Principal Outstanding"), "fieldname": "principal_outstanding", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Interest Outstanding"), "fieldname": "interest_outstanding", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Total Outstanding"), "fieldname": "total_outstanding", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Interest %"), "fieldname": "rate_of_interest", "fieldtype": "Percent", "width": 100},
{"label": _("Penalty Interest %"), "fieldname": "precentage_percentage", "fieldtype": "Percent", "width": 100},
{"label": _("Penalty Interest %"), "fieldname": "penalty_interest", "fieldtype": "Percent", "width": 100},
{"label": _("Currency"), "fieldname": "currency", "fieldtype": "Currency", "options": "Currency", "hidden": 1, "width": 100},
]
return columns
def get_active_loan_details(filters):
filter_obj = {"status": ("!=", "Closed")}
if filters.get('company'):
filter_obj.update({'company': filters.get('company')})
loan_details = frappe.get_all("Loan",
fields=["name as loan", "applicant_type", "applicant as applicant_name", "loan_type",
"disbursed_amount", "rate_of_interest", "total_payment", "total_principal_paid",
"total_interest_payable", "written_off_amount", "status"],
filters={"status": ("!=", "Closed")})
filters=filter_obj)
loan_list = [d.loan for d in loan_details]
sanctioned_amount_map = get_sanctioned_amount_map()
penal_interest_rate_map = get_penal_interest_rate_map()
payments = get_payments(loan_list)
accrual_map = get_interest_accruals(loan_list)
currency = erpnext.get_company_currency(filters.get('company'))
for loan in loan_details:
loan.update({
@ -54,8 +63,16 @@ def get_active_loan_details(filters):
- flt(loan.total_interest_payable) - flt(loan.written_off_amount),
"total_repayment": flt(payments.get(loan.loan)),
"month_interest": flt(accrual_map.get(loan.loan, {}).get("month_interest")),
"accrued_interest": flt(accrual_map.get(loan.loan, {}).get("accrued_interest"))
"accrued_interest": flt(accrual_map.get(loan.loan, {}).get("accrued_interest")),
"interest_outstanding": flt(accrual_map.get(loan.loan, {}).get("interest_outstanding")),
"penalty": flt(accrual_map.get(loan.loan, {}).get("penalty")),
"penalty_interest": penal_interest_rate_map.get(loan.loan_type),
"currency": currency
})
loan['total_outstanding'] = loan['principal_outstanding'] + loan['interest_outstanding'] \
+ loan['penalty']
return loan_details
def get_sanctioned_amount_map():
@ -71,18 +88,25 @@ def get_interest_accruals(loans):
current_month_start = get_first_day(getdate())
interest_accruals = frappe.get_all("Loan Interest Accrual",
fields=["loan", "interest_amount", "posting_date", "penalty_amount"],
filters={"loan": ("in", loans)})
fields=["loan", "interest_amount", "posting_date", "penalty_amount",
"paid_interest_amount"], filters={"loan": ("in", loans)}, order_by="posting_date")
for entry in interest_accruals:
accrual_map.setdefault(entry.loan, {
'month_interest': 0.0,
'accrued_interest': 0.0
"month_interest": 0.0,
"accrued_interest": 0.0,
"interest_outstanding": 0.0
})
if getdate(entry.posting_date) < getdate(current_month_start):
accrual_map[entry.loan]['accrued_interest'] += entry.interest_amount
accrual_map[entry.loan]["accrued_interest"] += entry.interest_amount
else:
accrual_map[entry.loan]['month_interest'] += entry.interest_amount
accrual_map[entry.loan]["month_interest"] += entry.interest_amount
return accrual_map
accrual_map[entry.loan]["interest_outstanding"] += entry.interest_amount - entry.paid_interest_amount
accrual_map[entry.loan]["penalty"] = entry.penalty_amount
return accrual_map
def get_penal_interest_rate_map():
return frappe._dict(frappe.get_all("Loan Type", fields=["name", "penalty_interest_rate"], as_list=1))

View File

@ -3,6 +3,7 @@
from __future__ import unicode_literals
import frappe
import erpnext
from frappe import _
from frappe.utils import get_datetime, flt
from six import iteritems
@ -23,10 +24,11 @@ def get_columns(filters):
{"label": _("Loan Security Type"), "fieldname": "loan_security_type", "fieldtype": "Link", "options": "Loan Security Type", "width": 120},
{"label": _("Disabled"), "fieldname": "disabled", "fieldtype": "Check", "width": 80},
{"label": _("Total Qty"), "fieldname": "total_qty", "fieldtype": "Float", "width": 100},
{"label": _("Latest Price"), "fieldname": "latest_price", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Current Value"), "fieldname": "current_value", "fieldtype": "Currency", "options": "Currency", "width": 100},
{"label": _("Latest Price"), "fieldname": "latest_price", "fieldtype": "Currency", "options": "currency", "width": 100},
{"label": _("Current Value"), "fieldname": "current_value", "fieldtype": "Currency", "options": "currency", "width": 100},
{"label": _("% Of Total Portfolio"), "fieldname": "portfolio_percent", "fieldtype": "Percentage", "width": 100},
{"label": _("Pledged Applicant Count"), "fieldname": "pledged_applicant_count", "fieldtype": "Percentage", "width": 100},
{"label": _("Currency"), "fieldname": "currency", "fieldtype": "Currency", "options": "Currency", "hidden": 1, "width": 100},
]
return columns
@ -35,6 +37,7 @@ def get_data(filters):
data = []
loan_security_details = get_loan_security_details(filters)
current_pledges, total_portfolio_value = get_company_wise_loan_security_details(filters, loan_security_details)
currency = erpnext.get_company_currency(filters.get('company'))
for security, value in iteritems(current_pledges):
row = {}
@ -43,8 +46,9 @@ def get_data(filters):
row.update({
'total_qty': value['qty'],
'current_value': current_value,
'portfolio_percent': current_value * 100 / total_portfolio_value,
'pledged_applicant_count': value['applicant_count']
'portfolio_percent': flt(current_value * 100 / total_portfolio_value, 2),
'pledged_applicant_count': value['applicant_count'],
'currency': currency
})
data.append(row)