From 5e69f4a5587e7fb05cbc728a06c9d99010f1e221 Mon Sep 17 00:00:00 2001 From: John Woodlock Date: Fri, 22 Feb 2013 12:19:56 +0000 Subject: [PATCH] fixed mifosx-192 & altered base reference DDL --- ...data.sql => 0002-mifosx-base-reference-data-utf8.sql} | 8 -------- mifosng-db/0003-mifosx-core-reports-utf8.sql | 9 +++++++++ mifosng-db/MifosXCoreReportsUTF8_WIP.sql | 8 -------- 3 files changed, 9 insertions(+), 16 deletions(-) rename mifosng-db/{0002-mifosng-base-reference-data.sql => 0002-mifosx-base-reference-data-utf8.sql} (57%) create mode 100644 mifosng-db/0003-mifosx-core-reports-utf8.sql delete mode 100644 mifosng-db/MifosXCoreReportsUTF8_WIP.sql diff --git a/mifosng-db/0002-mifosng-base-reference-data.sql b/mifosng-db/0002-mifosx-base-reference-data-utf8.sql similarity index 57% rename from mifosng-db/0002-mifosng-base-reference-data.sql rename to mifosng-db/0002-mifosx-base-reference-data-utf8.sql index cfc5cb222..70ca81bd1 100644 --- a/mifosng-db/0002-mifosng-base-reference-data.sql +++ b/mifosng-db/0002-mifosx-base-reference-data-utf8.sql @@ -495,14 +495,6 @@ VALUES INSERT INTO `m_appuser_role` (`appuser_id`, `role_id`) VALUES (1,1); --- ========= reports ========= - -INSERT INTO `stretchy_parameter` VALUES (1,'FullReportList',NULL,'n/a','n/a','n/a','n/a','Y',NULL,NULL,'select r.report_id, r.report_name, r.report_type, r.report_subtype, r.report_category,\r\n \n\nrp.parameter_id, rp.report_parameter_name, p.parameter_name\r\n from stretchy_report r\r\n left join stretchy_report_parameter rp on rp.report_id = r.report_id\r\n \n\nleft join stretchy_parameter p on p.parameter_id = rp.parameter_id\r\n where r.use_report is true\r\n and exists\r\n (\r\n select \'f\'\r\n from m_appuser_role ur \n\n\r\n join m_role r on r.id = ur.role_id\r\n join m_role_permission rp on rp.role_id = r.id\r\n join m_permission p on p.id = rp.permission_id\r\n where \n\nur.appuser_id = ${currentUserId}\r\n and (p.code in (\'ALL_FUNCTIONS_READ\', \'ALL_FUNCTIONS\') or p.code = concat(\"READ_\", r.report_name))\r\n )\r\n order by \n\nr.report_category, r.report_name, rp.parameter_id'),(2,'FullParameterList',NULL,'n/a','n/a','n/a','n/a','Y',NULL,NULL,'select parameter_name, parameter_variable, \n\nparameter_label, parameter_displayType, \r\nparameter_FormatType, parameter_default, selectOne, selectAll\r\nfrom stretchy_parameter p\r\nwhere special is null\r\n\n\norder by parameter_id'),(3,'reportCategoryList',NULL,'n/a','n/a','n/a','n/a','Y',NULL,NULL,'select r.report_id, r.report_name, r.report_type, r.report_subtype, \n\nr.report_category,\r\n rp.parameter_id, rp.report_parameter_name, p.parameter_name\r\n from stretchy_report r\r\n left join stretchy_report_parameter rp on \n\nrp.report_id = r.report_id\r\n left join stretchy_parameter p on p.parameter_id = rp.parameter_id\r\n where r.report_category = \'${reportCategory}\'\r\n and \n\nr.use_report is true\r\n and exists\r\n (\r\n select \'f\'\r\n from m_appuser_role ur \r\n join m_role r on r.id = ur.role_id\r\n join m_role_permission rp on \n\nrp.role_id = r.id\r\n join m_permission p on p.id = rp.permission_id\r\n where ur.appuser_id = ${currentUserId}\r\n and (p.code in (\'ALL_FUNCTIONS_READ\', \n\n\'ALL_FUNCTIONS\') or p.code = concat(\"READ_\", r.report_name))\r\n )\r\n order by r.report_category, r.report_name, rp.parameter_id'),(5,'OfficeIdSelectOne','officeId','Office','select','number','0',NULL,'Y',NULL,'select id, \r\nconcat(substring(\"........................................\", 1, \r\n \n\n((LENGTH(`hierarchy`) - LENGTH(REPLACE(`hierarchy`, \'.\', \'\')) - 1) * 4)), \r\n `name`) as tc\r\nfrom m_office\r\nwhere hierarchy like concat\n\n(\'${currentUserHierarchy}\', \'%\')\r\norder by hierarchy'),(6,'loanOfficerIdSelectAll','loanOfficerId','Loan Officer','select','number','0',NULL,'Y','Y','(select id, \n\ndisplay_name as `Name` from m_staff\nwhere is_loan_officer = true)\r\nunion all\r\n(select -10, \'-\')\r\norder by 2'),(10,'currencyIdSelectAll','currencyId','Currency','select','number','0',NULL,'Y','Y','select `code`, `name`\r\nfrom m_organisation_currency\r\norder by `code`'),(11,'currencyIdSelectOne','currencyId','Currency','select','number','0',NULL,'Y',NULL,'select `code`, `name`\r\nfrom m_organisation_currency\r\norder by `code`'),(20,'fundIdSelectAll','fundId','Fund','select','number','0',NULL,'Y','Y','(select id, `name`\r\nfrom m_fund)\r\nunion all\r\n(select -10, \'-\')\r\norder by 2'),(25,'loanProductIdSelectAll','loanProductId','Product','select','number','0',NULL,'Y','Y','select id, `name`\r\nfrom m_product_loan\r\norder by 2'),(40,'startDateSelect','startDate','startDate','date','date','today',NULL,NULL,NULL,NULL),(41,'endDateSelect','endDate','endDate','date','date','today',NULL,NULL,NULL,NULL); -INSERT INTO `stretchy_report` VALUES (1,'Client Listing','Table',NULL,'Client','select ounder.`name` as \"Office/Branch\", c.account_no as \"Client Account No.\", \r\nc.display_name as \"Name\", \n\nc.joined_date as \"Joined\", c.external_id as \"External Id\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand \n\nounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\nwhere o.id = ${officeId}\r\nand c.is_deleted=0\r\n\n\norder by ounder.hierarchy, c.account_no','Individual Client Report\r\n\r\nLists the small number of defined fields on the client table. Would expect to copy this \n\nreport and add any \'one to one\' additional data for specific tenant needs.\r\n\r\nCan be run for any size MFI but you\'d expect it only to be run within a branch for \n\nlarger ones. Depending on how many columns are displayed, there is probably is a limit of about 20/50k clients returned for html display (export to excel doesn\'t \n\nhave that client browser/memory impact).',1,1),(2,'Client Loans Listing','Table',NULL,'Client','select ounder.`name` as \"Office/Branch\", c.account_no as \"Client \n\nAccount No.\", \r\nc.display_name as \"Name\", \r\nlo.display_name as \"Loan Officer\", l.account_no as \"Loan Account No.\", l.external_id as \"External Id\", \r\n\n\np.name as Loan, st.enum_message_property as \"Status\", \r\nf.`name` as Fund,\r\nifnull(cur.display_symbol, l.currency_code) as Currency, \r\nl.principal_amount,\n\n\r\nl.arrearstolerance_amount as \"Arrears Tolerance Amount\",\r\nl.number_of_repayments as \"Expected No. Repayments\",\r\nl.annual_nominal_interest_rate as \" Annual \n\nNominal Interest Rate\", \r\nl.nominal_interest_rate_per_period as \"Nominal Interest Rate Per Period\",\r\n\r\nipf.enum_message_property as \"Interest Rate Frequency\n\n\",\r\nim.enum_message_property as \"Interest Method\",\r\nicp.enum_message_property as \"Interest Calculated in Period\",\r\nl.term_frequency as \"Term Frequency\",\n\n\r\ntf.enum_message_property as \"Term Frequency Period\",\r\nl.repay_every as \"Repayment Frequency\",\r\nrf.enum_message_property as \"Repayment Frequency Period\",\n\n\r\nam.enum_message_property as \"Amortization\",\r\n\r\nl.total_charges_due_at_disbursement_derived as \"Total Charges Due At Disbursement\",\r\n\r\ndate( \n\nl.submittedon_date) as Submitted, date(l.approvedon_date) Approved, l.expected_disbursedon_date As \"Expected Disbursal\",\r\ndate(l.expected_firstrepaymenton_date) as \n\n\"Expected First Repayment\", date(l.interest_calculated_from_date) as \"Interest Calculated From\" ,\r\ndate(l.disbursedon_date) as Disbursed, date\n\n(l.expected_maturedon_date) \"Expected Maturity\",\r\ndate(l.maturedon_date) as \"Matured On\", date(l.closedon_date) as Closed,\r\ndate(l.rejectedon_date) as \n\nRejected, date(l.rescheduledon_date) as Rescheduled, \r\ndate(l.withdrawnon_date) as Withdrawn, date(l.writtenoffon_date) \"Written Off\"\r\nfrom m_office o \r\njoin \n\nm_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on \n\nc.office_id = ounder.id\r\nleft join m_loan l on l.client_id = c.id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_product_loan p on p.id = \n\nl.product_id\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join r_enum_value st on st.enum_name = \"loan_status_id\" and st.enum_id = l.loan_status_id\r\nleft join \n\nr_enum_value ipf on ipf.enum_name = \"interest_period_frequency_enum\" and ipf.enum_id = l.interest_period_frequency_enum\r\nleft join r_enum_value im on im.enum_name \n\n= \"interest_method_enum\" and im.enum_id = l.interest_method_enum\r\nleft join r_enum_value tf on tf.enum_name = \"term_period_frequency_enum\" and tf.enum_id = \n\nl.term_period_frequency_enum\r\nleft join r_enum_value icp on icp.enum_name = \"interest_calculated_in_period_enum\" and icp.enum_id = \n\nl.interest_calculated_in_period_enum\r\nleft join r_enum_value rf on rf.enum_name = \"repayment_period_frequency_enum\" and rf.enum_id = \n\nl.repayment_period_frequency_enum\r\nleft join r_enum_value am on am.enum_name = \"amortization_method_enum\" and am.enum_id = l.amortization_method_enum\r\n\r\nleft \n\njoin m_currency cur on cur.code = l.currency_code\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand \n\n(l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \n\n\"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\norder by ounder.hierarchy, 2 , l.id','Individual Client Report\r\n\r\nPretty \n\nwide report that lists the basic details of client loans. \r\n\r\nCan be run for any size MFI but you\'d expect it only to be run within a branch for larger ones. \n\nThere is probably is a limit of about 20/50k clients returned for html display (export to excel doesn\'t have that client browser/memory impact).',1,1),(5,'Loans Awaiting Disbursal','Table',NULL,'Loan Portfolio','SELECT ounder.`name` as \"Office/Branch\", lo.display_name as \"Loan Officer\", \r\nc.display_name as \"Name\", l.account_no as \"Loan Account No.\", pl.`name` as \"Product\", \r\nf.`name` as Fund, ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nl.principal_amount as Principal, \r\ndate(l.approvedon_date) \"Approved\", l.expected_disbursedon_date \"Expected Disbursal\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand l.loan_status_id = 200\r\norder by ounder.hierarchy, l.expected_disbursedon_date, c.display_name','Individual Client Report',1,1),(6,'Loans Awaiting Disbursal Summary','Table',NULL,'Loan Portfolio','SELECT ounder.`name` \n\nas \"Office/Branch\", pl.`name` as \"Product\", \r\nifnull(cur.display_symbol, l.currency_code) as Currency, f.`name` as Fund,\r\nsum(l.principal_amount) as \n\nPrincipal\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat\n\n(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = \n\nl.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\n\n\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \n\n\"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 \n\n= ${fundId})\r\nand l.loan_status_id = 200\r\ngroup by ounder.hierarchy, pl.`name`, l.currency_code, f.`name`\r\norder by ounder.hierarchy, pl.`name`, \n\nl.currency_code, f.`name`','Individual Client Report',1,1),(7,'Loans Awaiting Disbursal Summary by Month','Table',NULL,'Loan Portfolio','SELECT ounder.`name` as \n\n\"Office/Branch\", pl.`name` as \"Product\", \r\nifnull(cur.display_symbol, l.currency_code) as Currency, \r\nyear(l.expected_disbursedon_date) as \"Year\", \n\nmonthname(l.expected_disbursedon_date) as \"Month\",\r\nsum(l.principal_amount) as Principal\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like \n\nconcat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on \n\nl.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = \n\nl.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand \n\n(l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \n\n\"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand l.loan_status_id = 200\r\ngroup by ounder.hierarchy, pl.`name`, \n\nl.currency_code, year(l.expected_disbursedon_date), month(l.expected_disbursedon_date)\r\norder by ounder.hierarchy, pl.`name`, l.currency_code, year\n\n(l.expected_disbursedon_date), month(l.expected_disbursedon_date)','Individual Client Report',1,1),(10,'Active Loans Portfolio Status','Table',NULL,'Loan','select \n\nounder.`name` as \"Office/Branch\", lo.display_name as \"Loan Officer\", c.display_name as \"Name\", \r\np.`name` as Loan, f.`name` as Fund, l.account_no as \"Loan \n\nAccount No\",\r\nl.disbursedon_date as Disbursed, ifnull(cur.display_symbol, l.currency_code) as Currency,\r\nsum(r.principal_amount - ifnull\n\n(r.principal_completed_derived, 0)) as \"Principal Outstanding\",\r\nsum(r.interest_amount - ifnull(r.interest_completed_derived, 0)) as \"Interest Outstanding\",\r\n\n\n\r\nif(datediff(curdate(), min(r.duedate)) < 0, 0, datediff(curdate(), min(r.duedate))) as \"Days Overdue\", \r\nmin(r.installment) as \"First Overdue Installment\n\n\",\r\nmin(r.duedate) as \"First Overdue Installment Date\",\r\nsum(if(r.duedate <= curdate(), \r\n (r.principal_amount - ifnull(r.principal_completed_derived, \n\n0))\r\n , 0)) as \"Principal Overdue\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount, 0) - ifnull(r.interest_completed_derived, \n\n0))\r\n , 0)) as \"Interest Overdue\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand \n\nounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join \n\nm_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_product_loan \n\np on p.id = l.product_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n and r.completed_derived is false\r\n\n\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \n\n\"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 \n\n= ${fundId})\r\nand l.loan_status_id = 300\r\ngroup by l.id\r\norder by ounder.hierarchy, p.`name`, l.currency_code, c.display_name, l.account_no','Individual Client \n\nReport',1,1),(11,'Active Loans Summary per Branch','Table',NULL,'Loan Portfolio','select ounder.`name` as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\ncount(distinct(c.id)) as \"No. of Clients\", count(distinct(l.id)) as \"No. of Active Loans\",\r\ncount(distinct(if(r.duedate <= curdate(), \r\n if(r.principal_amount - ifnull(r.principal_completed_derived, 0) > 0, l.id, null), null)\r\n )) as \"No. of Loans in Arrears\",\r\n\r\nsum(l.principal_amount) as \"Total Loans Disbursed\",\r\nsum(ifnull(r.principal_completed_derived, 0)) as \"Total Principal Repaid\",\r\nsum(ifnull(r.interest_completed_derived, 0)) as \"Total Interest Repaid\",\r\nsum(r.principal_amount - ifnull(r.principal_completed_derived, 0)) as \"Total Principal Outstanding\",\r\nsum(ifnull(r.interest_amount, 0) - ifnull(r.interest_completed_derived, 0)) as \"Total Interest utstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (r.principal_amount - ifnull(r.principal_completed_derived, 0))\r\n , 0)) as \"Total Principal in Arrears\",\r\ncast(round((sum(if(r.duedate <= curdate(), \r\n (r.principal_amount - ifnull(r.principal_completed_derived, 0))\r\n , 0)) * 100) / \r\n sum(r.principal_amount - ifnull(r.principal_completed_derived, 0))\r\n , 2) as char)\r\n as \"Portfolio at Risk %\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand l.loan_status_id = 300\r\ngroup by ounder.hierarchy, l.currency_code\r\norder by ounder.hierarchy, l.currency_code',NULL,1,1),(15,'Portfolio at Risk','Table',NULL,'Loan Portfolio','select ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nsum(r.principal_amount - ifnull(r.principal_completed_derived, 0)) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (r.principal_amount - ifnull(r.principal_completed_derived, 0))\r\n , 0)) as \"Principal Overdue\",\r\n cast(round(\r\n (sum(if(r.duedate <= curdate(), \r\n (r.principal_amount - ifnull(r.principal_completed_derived, 0))\r\n , 0)) * 100) / sum(r.principal_amount - ifnull(r.principal_completed_derived, 0))\r\n , 2) as char)\r\n as \"Portfolio at Risk %\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_product_loan p on p.id = l.product_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n and r.completed_derived is false\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand l.loan_status_id = 300\r\ngroup by l.currency_code\r\norder by l.currency_code',NULL,1,1),(16,'Portfolio at Risk by Branch','Table',NULL,'Loan Portfolio','select concat(substring(\"........................................\", 1, \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1) * 4)), \r\nounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency, \r\nsum(r.principal_amount - ifnull(r.principal_completed_derived, 0)) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (r.principal_amount - ifnull(r.principal_completed_derived, 0))\r\n , 0)) as \"Principal Overdue\",\r\n cast(round(\r\n (sum(if(r.duedate <= curdate(), \r\n (r.principal_amount - ifnull(r.principal_completed_derived, 0)) , 0)) * 100) / \r\n sum(r.principal_amount - ifnull(r.principal_completed_derived, 0)), 2) as char) \r\n as \"Portfolio at Risk %\"\r\n \r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_product_loan p on p.id = l.product_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n and r.completed_derived is false\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand l.loan_status_id = 300\r\ngroup by ounder.hierarchy, l.currency_code\r\norder by ounder.hierarchy, l.currency_code',NULL,1,1),(20,'Funds Disbursed Between Dates Summary','Table',NULL,'Fund','select ifnull(f.`name`, \'-\') as Fund, ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nround(sum(l.principal_amount), 4) as disbursed_amount\r\nfrom m_office ounder \r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_currency cur on cur.`code` = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nwhere disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (l.currency_code = \'${currencyId}\' or \'-1\' = \'${currencyId}\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\ngroup by ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, l.currency_code)\r\norder by ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, l.currency_code)',NULL,1,1),(21,'Funds Disbursed Between Dates Summary by Office','Table',NULL,'Fund','select ounder.`name` as \"Office/Branch\", \n\nifnull(f.`name`, \'-\') as Fund, ifnull(cur.display_symbol, l.currency_code) as Currency, round(sum(l.principal_amount), 4) as disbursed_amount\r\nfrom m_office o\r\n\n\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c \n\non c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_currency cur on cur.`code` = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\n\n\nwhere disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\nand o.id = ${officeId}\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand \n\n(l.currency_code = \'${currencyId}\' or \'-1\' = \'${currencyId}\')\r\ngroup by ounder.`name`, ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, \n\nl.currency_code)\r\norder by ounder.`name`, ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, l.currency_code)',NULL,1,1),(48,'Balance Sheet','Pentaho',NULL,'Accounting',NULL,'Balance Sheet',1,0),(49,'Income Statement','Pentaho',NULL,'Accounting',NULL,'Profit and Loss Statement',1,0),(50,'Trial Balance','Pentaho',NULL,'Accounting',NULL,'Trial Balance Report',1,0); -INSERT INTO `stretchy_report_parameter` VALUES (1,5,NULL),(2,5,NULL),(2,6,NULL),(2,10,NULL),(2,20,NULL),(2,25,NULL),(5,5,NULL),(5,6,NULL),(5,10,NULL),(5,20,NULL),(5,25,NULL),(6,5,NULL),(6,6,NULL),(6,10,NULL),(6,20,NULL),(6,25,NULL),(7,5,NULL),(7,6,NULL),(7,10,NULL),(7,20,NULL),(7,25,NULL),(10,5,NULL),(10,6,NULL),(10,10,NULL),(10,20,NULL),(10,25,NULL),(11,5,NULL),(11,10,NULL),(15,5,NULL),(15,6,NULL),(15,10,NULL),(15,20,NULL),(15,25,NULL),(16,5,NULL),(16,6,NULL),(16,10,NULL),(16,20,NULL),(16,25,NULL),(20,10,NULL),(20,20,NULL),(20,40,NULL),(20,41,NULL),(21,5,NULL),(21,10,NULL),(21,20,NULL),(21,40,NULL),(21,41,NULL),(48,5,'branch'),(48,41,'date'),(49,5,'branch'),(49,40,'fromDate'),(49,41,'toDate'),(50,5,'branch'),(50,40,'fromDate'),(50,41,'toDate'); - - - -- Add in permissions for any special datatables or reports added in base reference data -- This needs to always happen at end of the script diff --git a/mifosng-db/0003-mifosx-core-reports-utf8.sql b/mifosng-db/0003-mifosx-core-reports-utf8.sql new file mode 100644 index 000000000..fc60fb0e6 --- /dev/null +++ b/mifosng-db/0003-mifosx-core-reports-utf8.sql @@ -0,0 +1,9 @@ +-- MySQL dump 10.13 Distrib 5.1.45, for Win32 (ia32) +truncate table stretchy_report; +truncate table stretchy_parameter; +truncate table stretchy_report_parameter; + +INSERT INTO `stretchy_report` VALUES (1,'Client Listing','Table',NULL,'Client','select \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\n c.account_no as \"Client Account No.\", \r\nc.display_name as \"Name\", \n\nc.joined_date as \"Joined\", c.external_id as \"External Id\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand \n\nounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\nwhere o.id = ${officeId}\r\nand c.is_deleted=0\r\n\n\norder by ounder.hierarchy, c.account_no','Individual Client Report\r\n\r\nLists the small number of defined fields on the client table. Would expect to copy this \n\nreport and add any \'one to one\' additional data for specific tenant needs.\r\n\r\nCan be run for any size MFI but you\'d expect it only to be run within a branch for \n\nlarger ones. Depending on how many columns are displayed, there is probably is a limit of about 20/50k clients returned for html display (export to excel doesn\'t \n\nhave that client browser/memory impact).',1,1),(2,'Client Loans Listing','Table',NULL,'Client','select \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nc.account_no as \"Client \n\nAccount No.\", \r\nc.display_name as \"Name\", \r\nlo.display_name as \"Loan Officer\", l.account_no as \"Loan Account No.\", l.external_id as \"External Id\", \r\n\n\np.name as Loan, st.enum_message_property as \"Status\", \r\nf.`name` as Fund, purp.code_value as \"Loan Purpose\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency, \r\nl.principal_amount,\n\n\r\nl.arrearstolerance_amount as \"Arrears Tolerance Amount\",\r\nl.number_of_repayments as \"Expected No. Repayments\",\r\nl.annual_nominal_interest_rate as \" Annual \n\nNominal Interest Rate\", \r\nl.nominal_interest_rate_per_period as \"Nominal Interest Rate Per Period\",\r\n\r\nipf.enum_message_property as \"Interest Rate Frequency\n\n\",\r\nim.enum_message_property as \"Interest Method\",\r\nicp.enum_message_property as \"Interest Calculated in Period\",\r\nl.term_frequency as \"Term Frequency\",\n\n\r\ntf.enum_message_property as \"Term Frequency Period\",\r\nl.repay_every as \"Repayment Frequency\",\r\nrf.enum_message_property as \"Repayment Frequency Period\",\n\n\r\nam.enum_message_property as \"Amortization\",\r\n\r\nl.total_charges_due_at_disbursement_derived as \"Total Charges Due At Disbursement\",\r\n\r\ndate( \n\nl.submittedon_date) as Submitted, date(l.approvedon_date) Approved, l.expected_disbursedon_date As \"Expected Disbursal\",\r\ndate(l.expected_firstrepaymenton_date) as \n\n\"Expected First Repayment\", date(l.interest_calculated_from_date) as \"Interest Calculated From\" ,\r\ndate(l.disbursedon_date) as Disbursed, date\n\n(l.expected_maturedon_date) \"Expected Maturity\",\r\ndate(l.maturedon_date) as \"Matured On\", date(l.closedon_date) as Closed,\r\ndate(l.rejectedon_date) as \n\nRejected, date(l.rescheduledon_date) as Rescheduled, \r\ndate(l.withdrawnon_date) as Withdrawn, date(l.writtenoffon_date) \"Written Off\"\r\nfrom m_office o \r\njoin \n\nm_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on \n\nc.office_id = ounder.id\r\nleft join m_loan l on l.client_id = c.id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_product_loan p on p.id = \n\nl.product_id\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join r_enum_value st on st.enum_name = \"loan_status_id\" and st.enum_id = l.loan_status_id\r\nleft join \n\nr_enum_value ipf on ipf.enum_name = \"interest_period_frequency_enum\" and ipf.enum_id = l.interest_period_frequency_enum\r\nleft join r_enum_value im on im.enum_name \n\n= \"interest_method_enum\" and im.enum_id = l.interest_method_enum\r\nleft join r_enum_value tf on tf.enum_name = \"term_period_frequency_enum\" and tf.enum_id = \n\nl.term_period_frequency_enum\r\nleft join r_enum_value icp on icp.enum_name = \"interest_calculated_in_period_enum\" and icp.enum_id = \n\nl.interest_calculated_in_period_enum\r\nleft join r_enum_value rf on rf.enum_name = \"repayment_period_frequency_enum\" and rf.enum_id = \n\nl.repayment_period_frequency_enum\r\nleft join r_enum_value am on am.enum_name = \"amortization_method_enum\" and am.enum_id = l.amortization_method_enum\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\n\r\nleft \n\njoin m_currency cur on cur.code = l.currency_code\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand \n\n(l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \n\n\"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\norder by ounder.hierarchy, 2 , l.id','Individual Client Report\r\n\r\nPretty \n\nwide report that lists the basic details of client loans. \r\n\r\nCan be run for any size MFI but you\'d expect it only to be run within a branch for larger ones. \n\nThere is probably is a limit of about 20/50k clients returned for html display (export to excel doesn\'t have that client browser/memory impact).',1,1),(5,'Loans Awaiting Disbursal','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nc.account_no as \"Client Account No\", c.display_name as \"Name\", l.account_no as \"Loan Account No.\", pl.`name` as \"Product\", \r\nf.`name` as Fund, ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nl.principal_amount as Principal, \r\nl.term_frequency as \"Term Frequency\",\n\n\r\ntf.enum_message_property as \"Term Frequency Period\",\r\nl.annual_nominal_interest_rate as \" Annual Nominal Interest Rate\",\r\ndate(l.approvedon_date) \"Approved\",\r\ndatediff(l.expected_disbursedon_date, curdate()) as \"Days to Disbursal\",\r\ndate(l.expected_disbursedon_date) \"Expected Disbursal\",\r\npurp.code_value as \"Loan Purpose\",\r\n lo.display_name as \"Loan Officer\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nleft join r_enum_value tf on tf.enum_name = \"term_period_frequency_enum\" and tf.enum_id = l.term_period_frequency_enum\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 200\r\norder by ounder.hierarchy, datediff(l.expected_disbursedon_date, curdate()), c.account_no','Individual Client Report',1,1),(6,'Loans Awaiting Disbursal Summary','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\npl.`name` as \"Product\", \r\nifnull(cur.display_symbol, l.currency_code) as Currency, f.`name` as Fund,\r\nsum(l.principal_amount) as Principal\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 200\r\ngroup by ounder.hierarchy, pl.`name`, l.currency_code, f.`name`\r\norder by ounder.hierarchy, pl.`name`, l.currency_code, f.`name`','Individual Client Report',1,1),(7,'Loans Awaiting Disbursal Summary by Month','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\npl.`name` as \"Product\", \r\nifnull(cur.display_symbol, l.currency_code) as Currency, \r\nyear(l.expected_disbursedon_date) as \"Year\", \r\nmonthname(l.expected_disbursedon_date) as \"Month\",\r\nsum(l.principal_amount) as Principal\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 200\r\ngroup by ounder.hierarchy, pl.`name`, l.currency_code, year(l.expected_disbursedon_date), month(l.expected_disbursedon_date)\r\norder by ounder.hierarchy, pl.`name`, l.currency_code, year(l.expected_disbursedon_date), month(l.expected_disbursedon_date)','Individual Client Report',1,1),(8,'Loans Pending Approval','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nc.account_no as \"Client Account No.\", c.display_name as \"Client Name\", \r\nifnull(cur.display_symbol, l.currency_code) as Currency, pl.`name` as \"Product\", \r\nl.account_no as \"Loan Account No.\", \r\nl.principal_amount as \"Loan Amount\", \r\nl.term_frequency as \"Term Frequency\",\n\n\r\ntf.enum_message_property as \"Term Frequency Period\",\r\nl.annual_nominal_interest_rate as \" Annual \n\nNominal Interest Rate\", \r\ndatediff(curdate(), l.submittedon_date) \"Days Pending Approval\", \r\npurp.code_value as \"Loan Purpose\",\r\nlo.display_name as \"Loan Officer\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nleft join r_enum_value tf on tf.enum_name = \"term_period_frequency_enum\" and tf.enum_id = l.term_period_frequency_enum\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 100 /*Submitted and awaiting approval */\r\norder by ounder.hierarchy, l.submittedon_date, l.account_no','Individual Client Report',1,1),(11,'Active Loans - Summary','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(mo.`hierarchy`) - LENGTH(REPLACE(mo.`hierarchy`, \'.\', \'\')) - 1))), mo.`name`) as \"Office/Branch\", x.currency as Currency,\r\n x.client_count as \"No. of Clients\", x.active_loan_count as \"No. Active Loans\", x. principal_arrears_loan_count as \"No. of Loans in Arrears\",\r\nx.principal as \"Total Loans Disbursed\", x.principal_repaid as \"Principal Repaid\", x.principal_outstanding as \"Principal Outstanding\", x.principal_overdue as \"Principal Overdue\",\r\nx.interest as \"Total Interest\", x.interest_repaid as \"Interest Repaid\", x.interest_outstanding as \"Interest Outstanding\", x.interest_overdue as \"Interest Overdue\",\r\nx.fees as \"Total Fees\", x.fees_repaid as \"Fees Repaid\", x.fees_outstanding as \"Fees Outstanding\", x.fees_overdue as \"Fees Overdue\",\r\nx.penalties as \"Total Penalties\", x.penalties_repaid as \"Penalties Repaid\", x.penalties_outstanding as \"Penalties Outstanding\", x.penalties_overdue as \"Penalties Overdue\",\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.principal_overdue * 100) / x.principal_outstanding, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.principal_overdue + x.interest_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue + x.penalties_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding + x.penalties_overdue), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from m_office mo\r\njoin \r\n(select ounder.id as branch,\r\nifnull(cur.display_symbol, l.currency_code) as currency,\r\ncount(distinct(c.id)) as client_count, \r\ncount(distinct(l.id)) as active_loan_count,\r\ncount(distinct(if(r.duedate <= curdate(), \r\n if(ifnull(r.principal_amount,0) - ifnull(r.principal_completed_derived, 0), l.id, null), null) )) as principal_arrears_loan_count,\r\n\r\nsum(ifnull(r.principal_amount,0)) as principal,\r\nsum(ifnull(r.principal_completed_derived, 0)) as principal_repaid,\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as principal_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as principal_overdue,\r\n\r\nsum(ifnull(r.interest_amount,0)) as interest,\r\nsum(ifnull(r.interest_completed_derived, 0)) as interest_repaid,\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as interest_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as interest_overdue,\r\n\r\nsum(ifnull(r.fee_charges_amount,0)) as fees,\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as fees_repaid,\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as fees_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as fees_overdue,\r\n\r\nsum(ifnull(r.penalty_charges_amount,0)) as penalties,\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as penalties_repaid,\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as penalties_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as penalties_overdue\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\ngroup by ounder.id, l.currency_code) x on x.branch = mo.id\r\norder by mo.hierarchy, x.Currency',NULL,1,1),(12,'Active Loans - Details','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nlo.display_name as \"Loan Officer\", \r\nc.display_name as \"Client\", l.account_no as \"Loan Account No.\", pl.`name` as \"Product\", \r\nf.`name` as Fund, \r\nl.principal_amount as \"Loan Amount\", \r\nl.annual_nominal_interest_rate as \" Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed Date\", \r\ndate(l.expected_maturedon_date) as \"Expected Matured On\",\r\n\r\nsum(ifnull(r.principal_completed_derived, 0)) as \"Principal Repaid\",\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_completed_derived, 0)) as \"Interest Repaid\",\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as \"Fees Repaid\",\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as \"Penalties Repaid\",\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\ngroup by l.id\r\norder by ounder.hierarchy, l.currency_code, c.account_no, l.account_no','Individual Client \n\nReport',1,1),(13,'Obligation Met Loans Details','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nc.account_no as \"Client Account No.\", c.display_name as \"Client\",\r\nl.account_no as \"Loan Account No.\", pl.`name` as \"Product\", \r\nf.`name` as Fund, \r\nl.principal_amount as \"Loan Amount\", \r\n(sum(ifnull(r.principal_completed_derived, 0)) + \r\n sum(ifnull(r.interest_completed_derived, 0)) + \r\n sum(ifnull(r.fee_charges_completed_derived, 0)) + \r\n sum(ifnull(r.penalty_charges_completed_derived, 0))) as \"Total Repaid\", \r\nl.annual_nominal_interest_rate as \" Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed\", \r\ndate(l.closedon_date) as \"Closed\",\r\n\r\nsum(ifnull(r.principal_completed_derived, 0)) as \"Principal Repaid\",\r\nsum(ifnull(r.interest_completed_derived, 0)) as \"Interest Repaid\",\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as \"Fees Repaid\",\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as \"Penalties Repaid\",\r\nlo.display_name as \"Loan Officer\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand (case\r\n when ${obligDateType} = 1 then\r\n l.closedon_date between \'${startDate}\' and \'${endDate}\'\r\n when ${obligDateType} = 2 then\r\n l.disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\n else 1 = 1\r\n end)\r\nand l.loan_status_id = 600\r\ngroup by l.id\r\norder by ounder.hierarchy, l.currency_code, c.account_no, l.account_no','Individual Client \n\nReport',1,1),(14,'Obligation Met Loans Summary','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\ncount(distinct(c.id)) as \"No. of Clients\",\r\ncount(distinct(l.id)) as \"No. of Loans\",\r\nsum(l.principal_amount) as \"Total Loan Amount\", \r\n\"wait for derived field\" as \"Total Principal Repaid\",\r\n\"wait for derived field\" as \"Total Interest Repaid\",\r\n\"wait for derived field\" as \"Total Fees Repaid\",\r\n\"wait for derived field\" as \"Total Interest Waived\",\r\n\"wait for derived field\" as \"Total Fees Waived\",\r\n\"wait for derived field\" as \"Total Penalties Waived\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand (case\r\n when ${obligDateType} = 1 then\r\n l.closedon_date between \'${startDate}\' and \'${endDate}\'\r\n when ${obligDateType} = 2 then\r\n l.disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\n else 1 = 1\r\n end)\r\nand l.loan_status_id = 600\r\ngroup by o.hierarchy, l.currency_code\r\norder by ounder.hierarchy, l.currency_code','Individual Client \n\nReport',1,1),(15,'Portfolio at Risk','Table',NULL,'Loan','select x.Currency, x.`Principal Outstanding`, x.`Principal Overdue`, x.`Interest Outstanding`, x.`Interest Overdue`, \r\nx.`Fees Outstanding`, x.`Fees Overdue`, x.`Penalties Outstanding`, x.`Penalties Overdue`,\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.`Principal Overdue` * 100) / x.`Principal Outstanding`, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding`), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue` + x.`Fees Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding` + x.`Fees Outstanding`), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue` + x.`Fees Overdue` + x.`Penalties Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding` + x.`Fees Outstanding` + x.`Penalties Overdue`), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from \r\n(select ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nleft join m_product_loan p on p.id = l.product_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n and r.completed_derived is false\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\ngroup by l.currency_code\r\norder by l.currency_code) x','Covers all loans.\r\n\r\nFor larger MFIs … we should add some derived fields on loan (or a 1:1 loan related table like mifos 2.x does)\r\nPrinciple, Interest, Fees, Penalties Outstanding and Overdue (possibly waived and written off too)',1,1),(16,'Portfolio at Risk by Branch','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(mo.`hierarchy`) - LENGTH(REPLACE(mo.`hierarchy`, \'.\', \'\')) - 1))), mo.`name`) as \"Office/Branch\",\r\nx.Currency, x.`Principal Outstanding`, x.`Principal Overdue`, x.`Interest Outstanding`, x.`Interest Overdue`, \r\nx.`Fees Outstanding`, x.`Fees Overdue`, x.`Penalties Outstanding`, x.`Penalties Overdue`,\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.`Principal Overdue` * 100) / x.`Principal Outstanding`, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding`), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue` + x.`Fees Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding` + x.`Fees Outstanding`), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue` + x.`Fees Overdue` + x.`Penalties Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding` + x.`Fees Outstanding` + x.`Penalties Overdue`), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from m_office mo\r\njoin \r\n(select ounder.id as \"branch\", ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nleft join m_product_loan p on p.id = l.product_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n and r.completed_derived is false\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\ngroup by ounder.id, l.currency_code) x on x.branch = mo.id\r\norder by mo.hierarchy, x.Currency','Covers all loans.\r\n\r\nFor larger MFIs … we should add some derived fields on loan (or a 1:1 loan related table like mifos 2.x does)\r\nPrinciple, Interest, Fees, Penalties Outstanding and Overdue (possibly waived and written off too)',1,1),(20,'Funds Disbursed Between Dates Summary','Table',NULL,'Fund','select ifnull(f.`name`, \'-\') as Fund, ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nround(sum(l.principal_amount), 4) as disbursed_amount\r\nfrom m_office ounder \r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_currency cur on cur.`code` = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nwhere disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (l.currency_code = \'${currencyId}\' or \'-1\' = \'${currencyId}\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\ngroup by ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, l.currency_code)\r\norder by ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, l.currency_code)',NULL,1,1),(21,'Funds Disbursed Between Dates Summary by Office','Table',NULL,'Fund','select \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\n \n\nifnull(f.`name`, \'-\') as Fund, ifnull(cur.display_symbol, l.currency_code) as Currency, round(sum(l.principal_amount), 4) as disbursed_amount\r\nfrom m_office o\r\n\n\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c \n\non c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_currency cur on cur.`code` = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\n\n\nwhere disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\nand o.id = ${officeId}\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand \n\n(l.currency_code = \'${currencyId}\' or \'-1\' = \'${currencyId}\')\r\ngroup by ounder.`name`, ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, \n\nl.currency_code)\r\norder by ounder.`name`, ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, l.currency_code)',NULL,1,1),(48,'Balance Sheet','Pentaho',NULL,'Accounting',NULL,'Balance Sheet',1,0),(49,'Income Statement','Pentaho',NULL,'Accounting',NULL,'Profit and Loss Statement',1,0),(50,'Trial Balance','Pentaho',NULL,'Accounting',NULL,'Trial Balance Report',1,0),(51,'Written-Off Loans','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, ml.currency_code) as Currency, \r\nc.account_no as \"Client Account No.\",\r\nc.display_name AS \'Client Name\',\r\nml.account_no AS \'Loan Account No.\',\r\nmpl.name AS \'Product Name\',\r\nml.disbursedon_date AS \'Disbursed Date\',\r\nlt.transaction_date AS \'Written Off date\',\r\nml.principal_amount as \"Loan Amount\",\r\nifnull(lt.principal_portion_derived, 0) AS \'Written-Off Principal\',\r\nifnull(lt.interest_portion_derived, 0) AS \'Written-Off Interest\',\r\nifnull(lt.fee_charges_portion_derived,0) AS \'Written-Off Fees\',\r\nifnull(lt.penalty_charges_portion_derived,0) AS \'Written-Off Penalties\',\r\nn.note AS \'Reason For Write-Off\',\r\nIFNULL(ms.display_name,\'-\') AS \'Loan Officer Name\'\r\nFROM m_office o\r\nJOIN m_office ounder ON ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nAND ounder.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\nJOIN m_client c ON c.office_id = ounder.id\r\nJOIN m_loan ml ON ml.client_id = c.id\r\nJOIN m_product_loan mpl ON mpl.id=ml.product_id\r\nLEFT JOIN m_staff ms ON ms.id=ml.loan_officer_id\r\nJOIN m_loan_transaction lt ON lt.loan_id = ml.id\r\nLEFT JOIN m_note n ON n.loan_transaction_id = lt.id\r\nLEFT JOIN m_currency cur on cur.code = ml.currency_code\r\nWHERE lt.transaction_type_enum = 6 /*write-off */\r\nAND lt.is_reversed is false \r\nAND ml.loan_status_id=601\r\nAND o.id=${officeId}\r\nAND (mpl.id=${loanProductId} OR ${loanProductId}=-1)\r\nAND lt.transaction_date BETWEEN \'${startDate}\' AND \'${endDate}\'\r\nAND (ml.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nORDER BY ounder.hierarchy, ifnull(cur.display_symbol, ml.currency_code), ml.account_no','Individual Lending Report. Written Off Loans',1,1),(52,'Aging Summary (Arrears in weeks) - review','Table',NULL,'Loan','SELECT \r\n arr.clientName AS \"Client Name\",\r\n arr.accNo AS \"Account Number\",\r\n arr.loanAmt AS \"Loan Amount\",\r\n loan.principal AS \"Original Principal\",\r\n loan.interest AS \"Original Interest\",\r\n loan.prinPaid AS \"Principal Paid\",\r\n loan.intPaid AS \"Interest Paid\",\r\n arr.prinOverdue AS \"Principal Overdue\",\r\n arr.intOverdue AS \"Interest Overdue\",\r\n arr.arrWeek AS \"Weeks In Arrears\"\r\nFROM \r\n (SELECT mr.loan_id loanId,\r\n mc.display_name AS clientName,\r\n ml.account_no AS accNo,\r\n ml.principal_amount AS loanAmt,\r\n SUM(mr.principal_amount - IFNULL(mr.principal_completed_derived, 0)) AS prinOverdue, \r\n SUM(mr.interest_amount - IFNULL(mr.interest_completed_derived, 0)) AS intOverdue,\r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<7, \'Less than a week in arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<8, \'1 Week in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<15, \'2 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<22, \'3 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<29, \'4 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<36, \'5 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<43, \'6 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<50, \'7 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<57, \'8 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<64, \'9 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<71, \'10 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<78, \'11 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<85, \'12 Weeks in Arrears\', \'12+ Weeks in Arrears\'))))))))))))) AS arrWeek\r\n FROM m_office mo \r\n JOIN m_office ounder ON ounder.hierarchy like concat(mo.hierarchy, \'%\')\r\n AND ounder.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\n INNER JOIN m_client mc ON mc.office_id=ounder.id\r\n INNER JOIN m_loan ml ON ml.client_id = mc.id\r\n INNER JOIN r_enum_value rev ON rev.enum_id=ml.loan_status_id\r\n INNER JOIN m_loan_repayment_schedule mr ON mr.loan_id=ml.id\r\n WHERE mr.completed_derived=0 \r\n AND mr.duedate <= CURDATE() \r\n AND ml.loan_status_id=300\r\n AND mo.id=${officeId}\r\n GROUP BY mr.loan_id\r\n ORDER BY mc.display_name) arr\r\nINNER JOIN \r\n (SELECT mr.loan_id loanId, \r\n SUM(mr.principal_amount) principal,\r\n SUM(mr.interest_amount) interest,\r\n SUM(IFNULL(mr.principal_completed_derived,0)) prinPaid,\r\n SUM(IFNULL(mr.interest_completed_derived,0)) intPaid\r\n FROM m_loan_repayment_schedule mr\r\n GROUP BY mr.loan_id) loan ON arr.loanId=loan.loanId','Loan arrears aging (Weeks)',1,1),(53,'Amount In Arrears','Table',NULL,'Loan','SELECT \r\n IFNULL(weeks.currencyName, weeks.currency) as currency, \r\n weeks.week_no \'Weeks In Arrears\', \r\n IFNULL(ars.loanId, 0) \'No Of Loans\', \r\n IFNULL(ars.principal,0.0) \'Original Principal\', \r\n IFNULL(ars.interest,0.0) \'Original Interest\', \r\n IFNULL(ars.prinPaid,0.0) \'Principal Paid\', \r\n IFNULL(ars.intPaid,0.0) \'Interest Paid\', \r\n IFNULL(ars.prinOverdue,0.0) \'Principal Overdue\', \r\n IFNULL(ars.intOverdue,0.0)\'Interest Overdue\'\r\nFROM \r\n /* full table of aging weeks/currencies used combo to ensure each line represented */\r\n (SELECT curs.code as currency, curs.name as currencyName, wks.* from\r\n (SELECT \'On Schedule\' week_no,0 wid UNION\r\n SELECT \'0 to 6 days arrears\',1 UNION\r\n SELECT \'1 Week in Arrears\',2 UNION\r\n SELECT \'2 Weeks in Arrears\',3 UNION\r\n SELECT \'3 Weeks in Arrears\',4 UNION\r\n SELECT \'4 Weeks in Arrears\',5 UNION\r\n SELECT \'5 Weeks in Arrears\',6 UNION\r\n SELECT \'6 Weeks in Arrears\',7 UNION\r\n SELECT \'7 Weeks in Arrears\',8 UNION\r\n SELECT \'8 Weeks in Arrears\',9 UNION\r\n SELECT \'9 Weeks in Arrears\',10 UNION\r\n SELECT \'10 Weeks in Arrears\',11 UNION\r\n SELECT \'11 Weeks in Arrears\',12 UNION\r\n SELECT \'12 Weeks in Arrears\',13 UNION\r\n SELECT \'12+ Weeks in Arrears\',14) wks,\r\n (SELECT distinctrow moc.code, moc.name\r\n FROM m_office mo2\r\n INNER JOIN m_office ounder2 ON ounder2.hierarchy \r\n LIKE CONCAT(mo2.hierarchy, \'%\')\r\nAND ounder2.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\n INNER JOIN m_client mc2 ON mc2.office_id=ounder2.id\r\n INNER JOIN m_loan ml2 ON ml2.client_id = mc2.id\r\n INNER JOIN m_organisation_currency moc ON moc.code = ml2.currency_code\r\n WHERE ml2.loan_status_id=300 /* active */\r\n AND mo2.id=${officeId}) curs) weeks\r\n\r\n\r\nLEFT JOIN /* table of aging weeks per currency with gaps if no applicable loans */\r\n(SELECT \r\n z.currency, z.arrWeek, \r\n COUNT(z.loanId) as loanId, SUM(z.principal) as principal, SUM(z.interest) as interest, \r\n SUM(z.prinPaid) as prinPaid, SUM(z.intPaid) as intPaid, \r\n SUM(z.prinOverdue) as prinOverdue, SUM(z.intOverdue) as intOverdue\r\nFROM\r\n /*derived table just used to get arrWeek value (was much slower to\r\n duplicate calc of minOverdueDate in inner query) */\r\n (SELECT x.loanId, x.currency, x.principal, x.interest, x.prinPaid, x.intPaid, x.prinOverdue, x.intOverdue,\r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<0, \'On Schedule\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<7, \'0 to 6 days arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<8, \'1 Week in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<15, \'2 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<22, \'3 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<29, \'4 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<36, \'5 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<43, \'6 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<50, \'7 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<57, \'8 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<64, \'9 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<71, \'10 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<78, \'11 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<85, \'12 Weeks in Arrears\',\r\n \'12+ Weeks in Arrears\')))))))))))))) AS arrWeek\r\n\r\n FROM /* get the individual loan details */\r\n (SELECT ml.id AS loanId, ml.currency_code as currency,\r\n SUM(mr.principal_amount) principal, \r\n SUM(mr.interest_amount) interest, \r\n SUM(IFNULL(mr.principal_completed_derived,0)) prinPaid, \r\n SUM(IFNULL(mr.interest_completed_derived,0)) intPaid,\r\n\r\n SUM(IF((mr.completed_derived is false) AND (mr.duedate <= CURDATE()), \r\n (mr.principal_amount - ifnull(mr.principal_completed_derived, 0))\r\n , 0)) as prinOverdue,\r\n SUM(IF((mr.completed_derived is false) AND (mr.duedate <= CURDATE()), \r\n (mr.interest_amount - ifnull(mr.interest_completed_derived, 0))\r\n , 0)) as intOverdue,\r\n\r\n CAST(MIN(\r\n IF((mr.completed_derived is false) AND (mr.duedate <= CURDATE()), mr.duedate, CURDATE() + 1)) as date) as minOverdueDate\r\n \r\n FROM m_office mo\r\n INNER JOIN m_office ounder ON ounder.hierarchy \r\n LIKE CONCAT(mo.hierarchy, \'%\')\r\nAND ounder.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\n INNER JOIN m_client mc ON mc.office_id=ounder.id\r\n INNER JOIN m_loan ml ON ml.client_id = mc.id\r\n INNER JOIN m_loan_repayment_schedule mr on mr.loan_id = ml.id\r\n WHERE ml.loan_status_id=300 /* active */\r\n AND mo.id=${officeId}\r\n GROUP BY ml.id) x\r\n ) z \r\nGROUP BY z.currency, z.arrWeek ) ars ON ars.arrWeek=weeks.week_no and ars.currency = weeks.currency\r\nORDER BY weeks.currency, weeks.wid','Loan amount in arrears by branch',1,1),(54,'Rescheduled Loans','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, ml.currency_code) as Currency, \r\nc.account_no as \"Client Account No.\",\r\nc.display_name AS \'Client Name\',\r\nml.account_no AS \'Loan Account No.\',\r\nmpl.name AS \'Product Name\',\r\nml.disbursedon_date AS \'Disbursed Date\',\r\nlt.transaction_date AS \'Written Off date\',\r\nml.principal_amount as \"Loan Amount\",\r\nifnull(lt.principal_portion_derived, 0) AS \'Rescheduled Principal\',\r\nifnull(lt.interest_portion_derived, 0) AS \'Rescheduled Interest\',\r\nifnull(lt.fee_charges_portion_derived,0) AS \'Rescheduled Fees\',\r\nifnull(lt.penalty_charges_portion_derived,0) AS \'Rescheduled Penalties\',\r\nn.note AS \'Reason For Rescheduling\',\r\nIFNULL(ms.display_name,\'-\') AS \'Loan Officer Name\'\r\nFROM m_office o\r\nJOIN m_office ounder ON ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nAND ounder.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\nJOIN m_client c ON c.office_id = ounder.id\r\nJOIN m_loan ml ON ml.client_id = c.id\r\nJOIN m_product_loan mpl ON mpl.id=ml.product_id\r\nLEFT JOIN m_staff ms ON ms.id=ml.loan_officer_id\r\nJOIN m_loan_transaction lt ON lt.loan_id = ml.id\r\nLEFT JOIN m_note n ON n.loan_transaction_id = lt.id\r\nLEFT JOIN m_currency cur on cur.code = ml.currency_code\r\nWHERE lt.transaction_type_enum = 7 /*marked for rescheduling */\r\nAND lt.is_reversed is false \r\nAND ml.loan_status_id=602\r\nAND o.id=${officeId}\r\nAND (mpl.id=${loanProductId} OR ${loanProductId}=-1)\r\nAND lt.transaction_date BETWEEN \'${startDate}\' AND \'${endDate}\'\r\nAND (ml.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nORDER BY ounder.hierarchy, ifnull(cur.display_symbol, ml.currency_code), ml.account_no','Individual Lending Report. Rescheduled Loans. The ability to reschedule (or mark that you have rescheduled the loan elsewhere) is a legacy of the older Mifos product. Needed for migration.',1,1),(55,'Active Loans Passed Final Maturity','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nlo.display_name as \"Loan Officer\", \r\nc.display_name as \"Client\", l.account_no as \"Loan Account No.\", pl.`name` as \"Product\", \r\nf.`name` as Fund, \r\nl.principal_amount as \"Loan Amount\", \r\nl.annual_nominal_interest_rate as \" Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed Date\", \r\ndate(l.expected_maturedon_date) as \"Expected Matured On\",\r\n\r\nsum(ifnull(r.principal_completed_derived, 0)) as \"Principal Repaid\",\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_completed_derived, 0)) as \"Interest Repaid\",\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as \"Fees Repaid\",\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as \"Penalties Repaid\",\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\nand l.expected_maturedon_date < curdate()\r\ngroup by l.id\r\norder by ounder.hierarchy, l.currency_code, c.account_no, l.account_no','Individual Client \n\nReport',1,1),(56,'Active Loans Passed Final Maturity Summary','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(mo.`hierarchy`) - LENGTH(REPLACE(mo.`hierarchy`, \'.\', \'\')) - 1))), mo.`name`) as \"Office/Branch\", x.currency as Currency,\r\n x.client_count as \"No. of Clients\", x.active_loan_count as \"No. Active Loans\", x. principal_arrears_loan_count as \"No. of Loans in Arrears\",\r\nx.principal as \"Total Loans Disbursed\", x.principal_repaid as \"Principal Repaid\", x.principal_outstanding as \"Principal Outstanding\", x.principal_overdue as \"Principal Overdue\",\r\nx.interest as \"Total Interest\", x.interest_repaid as \"Interest Repaid\", x.interest_outstanding as \"Interest Outstanding\", x.interest_overdue as \"Interest Overdue\",\r\nx.fees as \"Total Fees\", x.fees_repaid as \"Fees Repaid\", x.fees_outstanding as \"Fees Outstanding\", x.fees_overdue as \"Fees Overdue\",\r\nx.penalties as \"Total Penalties\", x.penalties_repaid as \"Penalties Repaid\", x.penalties_outstanding as \"Penalties Outstanding\", x.penalties_overdue as \"Penalties Overdue\",\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.principal_overdue * 100) / x.principal_outstanding, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.principal_overdue + x.interest_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue + x.penalties_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding + x.penalties_overdue), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from m_office mo\r\njoin \r\n(select ounder.id as branch,\r\nifnull(cur.display_symbol, l.currency_code) as currency,\r\ncount(distinct(c.id)) as client_count, \r\ncount(distinct(l.id)) as active_loan_count,\r\ncount(distinct(if(r.duedate <= curdate(), \r\n if(ifnull(r.principal_amount,0) - ifnull(r.principal_completed_derived, 0), l.id, null), null) )) as principal_arrears_loan_count,\r\n\r\nsum(ifnull(r.principal_amount,0)) as principal,\r\nsum(ifnull(r.principal_completed_derived, 0)) as principal_repaid,\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as principal_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as principal_overdue,\r\n\r\nsum(ifnull(r.interest_amount,0)) as interest,\r\nsum(ifnull(r.interest_completed_derived, 0)) as interest_repaid,\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as interest_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as interest_overdue,\r\n\r\nsum(ifnull(r.fee_charges_amount,0)) as fees,\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as fees_repaid,\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as fees_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as fees_overdue,\r\n\r\nsum(ifnull(r.penalty_charges_amount,0)) as penalties,\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as penalties_repaid,\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as penalties_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as penalties_overdue\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\nand l.expected_maturedon_date < curdate()\r\ngroup by ounder.id, l.currency_code) x on x.branch = mo.id\r\norder by mo.hierarchy, x.Currency',NULL,1,1),(57,'Active Loans in last installment','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(lastInstallment.`hierarchy`) - LENGTH(REPLACE(lastInstallment.`hierarchy`, \'.\', \'\')) - 1))), lastInstallment.branch) as \"Office/Branch\",\r\nlastInstallment.Currency,\r\nlastInstallment.`Loan Officer`, \r\nlastInstallment.`Client Account No`, lastInstallment.`Client`, \r\nlastInstallment.`Loan Account No`, lastInstallment.`Product`, \r\nlastInstallment.`Fund`, lastInstallment.`Loan Amount`, \r\nlastInstallment.`Annual Nominal Interest Rate`, \r\nlastInstallment.`Disbursed`, lastInstallment.`Expected Matured On` ,\r\n\r\nsum(ifnull(r.principal_completed_derived, 0)) as \"Principal Repaid\",\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_completed_derived, 0)) as \"Interest Repaid\",\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as \"Fees Repaid\",\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as \"Penalties Repaid\",\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom \r\n(select l.id as loanId, l.number_of_repayments, min(r.installment), \r\nounder.id, ounder.hierarchy, ounder.`name` as branch, \r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nlo.display_name as \"Loan Officer\", c.account_no as \"Client Account No\",\r\nc.display_name as \"Client\", l.account_no as \"Loan Account No\", pl.`name` as \"Product\", \r\nf.`name` as Fund, l.principal_amount as \"Loan Amount\", \r\nl.annual_nominal_interest_rate as \"Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed\", date(l.expected_maturedon_date) as \"Expected Matured On\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\nand r.completed_derived is false\r\nand r.duedate >= curdate()\r\ngroup by l.id\r\nhaving l.number_of_repayments = min(r.installment)) lastInstallment\r\njoin m_loan_repayment_schedule r on r.loan_id = lastInstallment.loanId\r\ngroup by lastInstallment.loanId\r\norder by lastInstallment.hierarchy, lastInstallment.Currency, lastInstallment.`Client Account No`, lastInstallment.`Loan Account No`','Individual Client \n\nReport',1,1),(58,'Active Loans in last installment Summary','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(mo.`hierarchy`) - LENGTH(REPLACE(mo.`hierarchy`, \'.\', \'\')) - 1))), mo.`name`) as \"Office/Branch\", x.currency as Currency,\r\n x.client_count as \"No. of Clients\", x.active_loan_count as \"No. Active Loans\", x. principal_arrears_loan_count as \"No. of Loans in Arrears\",\r\nx.principal as \"Total Loans Disbursed\", x.principal_repaid as \"Principal Repaid\", x.principal_outstanding as \"Principal Outstanding\", x.principal_overdue as \"Principal Overdue\",\r\nx.interest as \"Total Interest\", x.interest_repaid as \"Interest Repaid\", x.interest_outstanding as \"Interest Outstanding\", x.interest_overdue as \"Interest Overdue\",\r\nx.fees as \"Total Fees\", x.fees_repaid as \"Fees Repaid\", x.fees_outstanding as \"Fees Outstanding\", x.fees_overdue as \"Fees Overdue\",\r\nx.penalties as \"Total Penalties\", x.penalties_repaid as \"Penalties Repaid\", x.penalties_outstanding as \"Penalties Outstanding\", x.penalties_overdue as \"Penalties Overdue\",\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.principal_overdue * 100) / x.principal_outstanding, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.principal_overdue + x.interest_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue + x.penalties_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding + x.penalties_overdue), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from m_office mo\r\njoin \r\n(select lastInstallment.branchId as branchId,\r\nlastInstallment.Currency,\r\ncount(distinct(lastInstallment.clientId)) as client_count, \r\ncount(distinct(lastInstallment.loanId)) as active_loan_count,\r\ncount(distinct(if(r.duedate <= curdate(), \r\n if(ifnull(r.principal_amount,0) - ifnull(r.principal_completed_derived, 0),lastInstallment.loanId, null), null) )) as principal_arrears_loan_count,\r\n\r\nsum(ifnull(r.principal_amount,0)) as principal,\r\nsum(ifnull(r.principal_completed_derived, 0)) as principal_repaid,\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as principal_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as principal_overdue,\r\n\r\nsum(ifnull(r.interest_amount,0)) as interest,\r\nsum(ifnull(r.interest_completed_derived, 0)) as interest_repaid,\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as interest_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as interest_overdue,\r\n\r\nsum(ifnull(r.fee_charges_amount,0)) as fees,\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as fees_repaid,\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as fees_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as fees_overdue,\r\n\r\nsum(ifnull(r.penalty_charges_amount,0)) as penalties,\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as penalties_repaid,\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as penalties_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as penalties_overdue\r\n\r\nfrom \r\n(select l.id as loanId, l.number_of_repayments, min(r.installment), \r\nounder.id as branchId, ounder.hierarchy, ounder.`name` as branch, \r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nlo.display_name as \"Loan Officer\", c.id as clientId, c.account_no as \"Client Account No\",\r\nc.display_name as \"Client\", l.account_no as \"Loan Account No\", pl.`name` as \"Product\", \r\nf.`name` as Fund, l.principal_amount as \"Loan Amount\", \r\nl.annual_nominal_interest_rate as \"Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed\", date(l.expected_maturedon_date) as \"Expected Matured On\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\nand r.completed_derived is false\r\nand r.duedate >= curdate()\r\ngroup by l.id\r\nhaving l.number_of_repayments = min(r.installment)) lastInstallment\r\njoin m_loan_repayment_schedule r on r.loan_id = lastInstallment.loanId\r\ngroup by lastInstallment.branchId) x on x.branchId = mo.id\r\norder by mo.hierarchy, x.Currency','Individual Client \n\nReport',1,1),(59,'Active Loans by Disbursal Period','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nc.account_no as \"Client Account No\", c.display_name as \"Client\", l.account_no as \"Loan Account No\", pl.`name` as \"Product\", \r\nf.`name` as Fund, \r\nl.principal_amount as \"Loan Principal Amount\", \r\nl.annual_nominal_interest_rate as \" Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed Date\", \r\n(sum(ifnull(r.principal_amount, 0)) + sum(ifnull(r.interest_amount, 0)) + \r\nsum(ifnull(r.fee_charges_amount, 0)) + sum(ifnull(r.penalty_charges_amount, 0))) as \"Total Loan (P+I+F+Pen)\",\r\n\r\n(sum(ifnull(r.principal_completed_derived, 0)) + sum(ifnull(r.interest_completed_derived, 0)) +\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) + sum(ifnull(r.penalty_charges_completed_derived, 0))) as \"Total Repaid (P+I+F+Pen)\",\r\nlo.display_name as \"Loan Officer\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\nand l.loan_status_id = 300\r\ngroup by l.id\r\norder by ounder.hierarchy, l.currency_code, c.account_no, l.account_no','Individual Client \n\nReport',1,1); +INSERT INTO `stretchy_parameter` VALUES (1,'startDateSelect','startDate','startDate','date','date','today',NULL,NULL,NULL,NULL),(2,'endDateSelect','endDate','endDate','date','date','today',NULL,NULL,NULL,NULL),(3,'obligDateTypeSelect','obligDateType','obligDateType','select','number','0',NULL,NULL,NULL,'select * from\r\n(select 1 as id, \"Closed\" as `name` union all\r\nselect 2, \"Disbursal\" ) x\r\norder by x.`id`'),(5,'OfficeIdSelectOne','officeId','Office','select','number','0',NULL,'Y',NULL,'select id, \r\nconcat(substring(\"........................................\", 1, \r\n \n\n((LENGTH(`hierarchy`) - LENGTH(REPLACE(`hierarchy`, \'.\', \'\')) - 1) * 4)), \r\n `name`) as tc\r\nfrom m_office\r\nwhere hierarchy like concat\n\n(\'${currentUserHierarchy}\', \'%\')\r\norder by hierarchy'),(6,'loanOfficerIdSelectAll','loanOfficerId','Loan Officer','select','number','0',NULL,NULL,'Y','(select id, \n\ndisplay_name as `Name` from m_staff\nwhere is_loan_officer = true)\r\nunion all\r\n(select -10, \'-\')\r\norder by 2'),(10,'currencyIdSelectAll','currencyId','Currency','select','number','0',NULL,NULL,'Y','select `code`, `name`\r\nfrom m_organisation_currency\r\norder by `code`'),(11,'currencyIdSelectOne','currencyId','Currency','select','number','0',NULL,'Y',NULL,'select `code`, `name`\r\nfrom m_organisation_currency\r\norder by `code`'),(20,'fundIdSelectAll','fundId','Fund','select','number','0',NULL,NULL,'Y','(select id, `name`\r\nfrom m_fund)\r\nunion all\r\n(select -10, \'-\')\r\norder by 2'),(25,'loanProductIdSelectAll','loanProductId','Product','select','number','0',NULL,NULL,'Y','select id, `name`\r\nfrom m_product_loan\r\norder by 2'),(26,'loanPurposeIdSelectAll','loanPurposeId','Loan Purpose','select','number','0',NULL,NULL,'Y','select -10 as id, \'-\' as code_value\r\nunion all\r\nselect * from (select v.id, v.code_value\r\nfrom m_code c\r\njoin m_code_value v on v.code_id = c.id\r\nwhere c.code_name = \"loanPurpose\"\r\norder by v.order_position) x'),(100,'parTypeSelect','parType','parType','select','number','0',NULL,NULL,NULL,'select * from\r\n(select 1 as id, \"Principal Only\" as `name` union all\r\nselect 2, \"Principal + Interest\" union all\r\nselect 3, \"Principal + Interest + Fees\" union all\r\nselect 4, \"Principal + Interest + Fees + Penalties\") x\r\norder by x.`id`'),(1001,'FullReportList',NULL,'n/a','n/a','n/a','n/a','Y',NULL,NULL,'select r.report_id, r.report_name, r.report_type, r.report_subtype, r.report_category,\r\n \n\nrp.parameter_id, rp.report_parameter_name, p.parameter_name\r\n from stretchy_report r\r\n left join stretchy_report_parameter rp on rp.report_id = r.report_id\r\n \n\nleft join stretchy_parameter p on p.parameter_id = rp.parameter_id\r\n where r.use_report is true\r\n and exists\r\n (\r\n select \'f\'\r\n from m_appuser_role ur \n\n\r\n join m_role r on r.id = ur.role_id\r\n join m_role_permission rp on rp.role_id = r.id\r\n join m_permission p on p.id = rp.permission_id\r\n where \n\nur.appuser_id = ${currentUserId}\r\n and (p.code in (\'ALL_FUNCTIONS_READ\', \'ALL_FUNCTIONS\') or p.code = concat(\"READ_\", r.report_name))\r\n )\r\n order by \n\nr.report_category, r.report_name, rp.parameter_id'),(1002,'FullParameterList',NULL,'n/a','n/a','n/a','n/a','Y',NULL,NULL,'select parameter_name, parameter_variable, \n\nparameter_label, parameter_displayType, \r\nparameter_FormatType, parameter_default, selectOne, selectAll\r\nfrom stretchy_parameter p\r\nwhere special is null\r\n\n\norder by parameter_id'),(1003,'reportCategoryList',NULL,'n/a','n/a','n/a','n/a','Y',NULL,NULL,'select r.report_id, r.report_name, r.report_type, r.report_subtype, \n\nr.report_category,\r\n rp.parameter_id, rp.report_parameter_name, p.parameter_name\r\n from stretchy_report r\r\n left join stretchy_report_parameter rp on \n\nrp.report_id = r.report_id\r\n left join stretchy_parameter p on p.parameter_id = rp.parameter_id\r\n where r.report_category = \'${reportCategory}\'\r\n and \n\nr.use_report is true\r\n and exists\r\n (\r\n select \'f\'\r\n from m_appuser_role ur \r\n join m_role r on r.id = ur.role_id\r\n join m_role_permission rp on \n\nrp.role_id = r.id\r\n join m_permission p on p.id = rp.permission_id\r\n where ur.appuser_id = ${currentUserId}\r\n and (p.code in (\'ALL_FUNCTIONS_READ\', \n\n\'ALL_FUNCTIONS\') or p.code = concat(\"READ_\", r.report_name))\r\n )\r\n order by r.report_category, r.report_name, rp.parameter_id'); +INSERT INTO `stretchy_report_parameter` VALUES (1,5,NULL),(2,5,NULL),(2,6,NULL),(2,10,NULL),(2,20,NULL),(2,25,NULL),(2,26,NULL),(5,5,NULL),(5,6,NULL),(5,10,NULL),(5,20,NULL),(5,25,NULL),(5,26,NULL),(6,5,NULL),(6,6,NULL),(6,10,NULL),(6,20,NULL),(6,25,NULL),(6,26,NULL),(7,5,NULL),(7,6,NULL),(7,10,NULL),(7,20,NULL),(7,25,NULL),(7,26,NULL),(8,5,NULL),(8,6,NULL),(8,10,NULL),(8,25,NULL),(8,26,NULL),(11,5,NULL),(11,6,NULL),(11,10,NULL),(11,20,NULL),(11,25,NULL),(11,26,NULL),(11,100,NULL),(12,5,NULL),(12,6,NULL),(12,10,NULL),(12,20,NULL),(12,25,NULL),(12,26,NULL),(13,1,NULL),(13,2,NULL),(13,3,NULL),(13,5,NULL),(13,6,NULL),(13,10,NULL),(13,20,NULL),(13,25,NULL),(13,26,NULL),(14,1,NULL),(14,2,NULL),(14,3,NULL),(14,5,NULL),(14,6,NULL),(14,10,NULL),(14,20,NULL),(14,25,NULL),(14,26,NULL),(15,5,NULL),(15,6,NULL),(15,10,NULL),(15,20,NULL),(15,25,NULL),(15,26,NULL),(15,100,NULL),(16,5,NULL),(16,6,NULL),(16,10,NULL),(16,20,NULL),(16,25,NULL),(16,26,NULL),(16,100,NULL),(20,1,NULL),(20,2,NULL),(20,10,NULL),(20,20,NULL),(21,1,NULL),(21,2,NULL),(21,5,NULL),(21,10,NULL),(21,20,NULL),(48,5,'branch'),(48,2,'date'),(49,5,'branch'),(49,1,'fromDate'),(49,2,'toDate'),(50,5,'branch'),(50,1,'fromDate'),(50,2,'toDate'),(51,1,NULL),(51,2,NULL),(51,5,NULL),(51,10,NULL),(51,25,NULL),(52,5,NULL),(53,5,NULL),(54,1,NULL),(54,2,NULL),(54,5,NULL),(54,10,NULL),(54,25,NULL),(55,5,NULL),(55,6,NULL),(55,10,NULL),(55,20,NULL),(55,25,NULL),(55,26,NULL),(56,5,NULL),(56,6,NULL),(56,10,NULL),(56,20,NULL),(56,25,NULL),(56,26,NULL),(56,100,NULL),(57,5,NULL),(57,6,NULL),(57,10,NULL),(57,20,NULL),(57,25,NULL),(57,26,NULL),(58,5,NULL),(58,6,NULL),(58,10,NULL),(58,20,NULL),(58,25,NULL),(58,26,NULL),(58,100,NULL),(59,1,NULL),(59,2,NULL),(59,5,NULL),(59,6,NULL),(59,10,NULL),(59,20,NULL),(59,25,NULL),(59,26,NULL); + diff --git a/mifosng-db/MifosXCoreReportsUTF8_WIP.sql b/mifosng-db/MifosXCoreReportsUTF8_WIP.sql deleted file mode 100644 index 9d3991a0a..000000000 --- a/mifosng-db/MifosXCoreReportsUTF8_WIP.sql +++ /dev/null @@ -1,8 +0,0 @@ -truncate table stretchy_report; -truncate table stretchy_parameter; -truncate table stretchy_report_parameter; - -INSERT INTO `stretchy_report` VALUES (1,'Client Listing','Table',NULL,'Client','select \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\n c.account_no as \"Client Account No.\", \r\nc.display_name as \"Name\", \n\nc.joined_date as \"Joined\", c.external_id as \"External Id\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand \n\nounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\nwhere o.id = ${officeId}\r\nand c.is_deleted=0\r\n\n\norder by ounder.hierarchy, c.account_no','Individual Client Report\r\n\r\nLists the small number of defined fields on the client table. Would expect to copy this \n\nreport and add any \'one to one\' additional data for specific tenant needs.\r\n\r\nCan be run for any size MFI but you\'d expect it only to be run within a branch for \n\nlarger ones. Depending on how many columns are displayed, there is probably is a limit of about 20/50k clients returned for html display (export to excel doesn\'t \n\nhave that client browser/memory impact).',1,1),(2,'Client Loans Listing','Table',NULL,'Client','select \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nc.account_no as \"Client \n\nAccount No.\", \r\nc.display_name as \"Name\", \r\nlo.display_name as \"Loan Officer\", l.account_no as \"Loan Account No.\", l.external_id as \"External Id\", \r\n\n\np.name as Loan, st.enum_message_property as \"Status\", \r\nf.`name` as Fund, purp.code_value as \"Loan Purpose\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency, \r\nl.principal_amount,\n\n\r\nl.arrearstolerance_amount as \"Arrears Tolerance Amount\",\r\nl.number_of_repayments as \"Expected No. Repayments\",\r\nl.annual_nominal_interest_rate as \" Annual \n\nNominal Interest Rate\", \r\nl.nominal_interest_rate_per_period as \"Nominal Interest Rate Per Period\",\r\n\r\nipf.enum_message_property as \"Interest Rate Frequency\n\n\",\r\nim.enum_message_property as \"Interest Method\",\r\nicp.enum_message_property as \"Interest Calculated in Period\",\r\nl.term_frequency as \"Term Frequency\",\n\n\r\ntf.enum_message_property as \"Term Frequency Period\",\r\nl.repay_every as \"Repayment Frequency\",\r\nrf.enum_message_property as \"Repayment Frequency Period\",\n\n\r\nam.enum_message_property as \"Amortization\",\r\n\r\nl.total_charges_due_at_disbursement_derived as \"Total Charges Due At Disbursement\",\r\n\r\ndate( \n\nl.submittedon_date) as Submitted, date(l.approvedon_date) Approved, l.expected_disbursedon_date As \"Expected Disbursal\",\r\ndate(l.expected_firstrepaymenton_date) as \n\n\"Expected First Repayment\", date(l.interest_calculated_from_date) as \"Interest Calculated From\" ,\r\ndate(l.disbursedon_date) as Disbursed, date\n\n(l.expected_maturedon_date) \"Expected Maturity\",\r\ndate(l.maturedon_date) as \"Matured On\", date(l.closedon_date) as Closed,\r\ndate(l.rejectedon_date) as \n\nRejected, date(l.rescheduledon_date) as Rescheduled, \r\ndate(l.withdrawnon_date) as Withdrawn, date(l.writtenoffon_date) \"Written Off\"\r\nfrom m_office o \r\njoin \n\nm_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on \n\nc.office_id = ounder.id\r\nleft join m_loan l on l.client_id = c.id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_product_loan p on p.id = \n\nl.product_id\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join r_enum_value st on st.enum_name = \"loan_status_id\" and st.enum_id = l.loan_status_id\r\nleft join \n\nr_enum_value ipf on ipf.enum_name = \"interest_period_frequency_enum\" and ipf.enum_id = l.interest_period_frequency_enum\r\nleft join r_enum_value im on im.enum_name \n\n= \"interest_method_enum\" and im.enum_id = l.interest_method_enum\r\nleft join r_enum_value tf on tf.enum_name = \"term_period_frequency_enum\" and tf.enum_id = \n\nl.term_period_frequency_enum\r\nleft join r_enum_value icp on icp.enum_name = \"interest_calculated_in_period_enum\" and icp.enum_id = \n\nl.interest_calculated_in_period_enum\r\nleft join r_enum_value rf on rf.enum_name = \"repayment_period_frequency_enum\" and rf.enum_id = \n\nl.repayment_period_frequency_enum\r\nleft join r_enum_value am on am.enum_name = \"amortization_method_enum\" and am.enum_id = l.amortization_method_enum\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\n\r\nleft \n\njoin m_currency cur on cur.code = l.currency_code\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand \n\n(l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \n\n\"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\norder by ounder.hierarchy, 2 , l.id','Individual Client Report\r\n\r\nPretty \n\nwide report that lists the basic details of client loans. \r\n\r\nCan be run for any size MFI but you\'d expect it only to be run within a branch for larger ones. \n\nThere is probably is a limit of about 20/50k clients returned for html display (export to excel doesn\'t have that client browser/memory impact).',1,1),(5,'Loans Awaiting Disbursal','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nc.account_no as \"Client Account No\", c.display_name as \"Name\", l.account_no as \"Loan Account No.\", pl.`name` as \"Product\", \r\nf.`name` as Fund, ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nl.principal_amount as Principal, \r\nl.term_frequency as \"Term Frequency\",\n\n\r\ntf.enum_message_property as \"Term Frequency Period\",\r\nl.annual_nominal_interest_rate as \" Annual Nominal Interest Rate\",\r\ndate(l.approvedon_date) \"Approved\",\r\ndatediff(l.expected_disbursedon_date, curdate()) as \"Days to Disbursal\",\r\ndate(l.expected_disbursedon_date) \"Expected Disbursal\",\r\npurp.code_value as \"Loan Purpose\",\r\n lo.display_name as \"Loan Officer\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nleft join r_enum_value tf on tf.enum_name = \"term_period_frequency_enum\" and tf.enum_id = l.term_period_frequency_enum\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 200\r\norder by ounder.hierarchy, datediff(l.expected_disbursedon_date, curdate()), c.account_no','Individual Client Report',1,1),(6,'Loans Awaiting Disbursal Summary','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\npl.`name` as \"Product\", \r\nifnull(cur.display_symbol, l.currency_code) as Currency, f.`name` as Fund,\r\nsum(l.principal_amount) as Principal\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 200\r\ngroup by ounder.hierarchy, pl.`name`, l.currency_code, f.`name`\r\norder by ounder.hierarchy, pl.`name`, l.currency_code, f.`name`','Individual Client Report',1,1),(7,'Loans Awaiting Disbursal Summary by Month','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\npl.`name` as \"Product\", \r\nifnull(cur.display_symbol, l.currency_code) as Currency, \r\nyear(l.expected_disbursedon_date) as \"Year\", \r\nmonthname(l.expected_disbursedon_date) as \"Month\",\r\nsum(l.principal_amount) as Principal\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 200\r\ngroup by ounder.hierarchy, pl.`name`, l.currency_code, year(l.expected_disbursedon_date), month(l.expected_disbursedon_date)\r\norder by ounder.hierarchy, pl.`name`, l.currency_code, year(l.expected_disbursedon_date), month(l.expected_disbursedon_date)','Individual Client Report',1,1),(8,'Loans Pending Approval','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nc.account_no as \"Client Account No.\", c.display_name as \"Client Name\", \r\nifnull(cur.display_symbol, l.currency_code) as Currency, pl.`name` as \"Product\", \r\nl.account_no as \"Loan Account No.\", \r\nl.principal_amount as \"Loan Amount\", \r\nl.term_frequency as \"Term Frequency\",\n\n\r\ntf.enum_message_property as \"Term Frequency Period\",\r\nl.annual_nominal_interest_rate as \" Annual \n\nNominal Interest Rate\", \r\ndatediff(curdate(), l.submittedon_date) \"Days Pending Approval\", \r\npurp.code_value as \"Loan Purpose\",\r\nlo.display_name as \"Loan Officer\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nleft join r_enum_value tf on tf.enum_name = \"term_period_frequency_enum\" and tf.enum_id = l.term_period_frequency_enum\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 100 /*Submitted and awaiting approval */\r\norder by ounder.hierarchy, l.submittedon_date, l.account_no','Individual Client Report',1,1),(11,'Active Loans Summary','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(mo.`hierarchy`) - LENGTH(REPLACE(mo.`hierarchy`, \'.\', \'\')) - 1))), mo.`name`) as \"Office/Branch\", x.currency as Currency,\r\n x.client_count as \"No. of Clients\", x.active_loan_count as \"No. Active Loans\", x. principal_arrears_loan_count as \"No. of Loans in Arrears\",\r\nx.principal as \"Total Loans Disbursed\", x.principal_repaid as \"Principal Repaid\", x.principal_outstanding as \"Principal Outstanding\", x.principal_overdue as \"Principal Overdue\",\r\nx.interest as \"Total Interest\", x.interest_repaid as \"Interest Repaid\", x.interest_outstanding as \"Interest Outstanding\", x.interest_overdue as \"Interest Overdue\",\r\nx.fees as \"Total Fees\", x.fees_repaid as \"Fees Repaid\", x.fees_outstanding as \"Fees Outstanding\", x.fees_overdue as \"Fees Overdue\",\r\nx.penalties as \"Total Penalties\", x.penalties_repaid as \"Penalties Repaid\", x.penalties_outstanding as \"Penalties Outstanding\", x.penalties_overdue as \"Penalties Overdue\",\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.principal_overdue * 100) / x.principal_outstanding, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.principal_overdue + x.interest_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue + x.penalties_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding + x.penalties_overdue), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from m_office mo\r\njoin \r\n(select ounder.id as branch,\r\nifnull(cur.display_symbol, l.currency_code) as currency,\r\ncount(distinct(c.id)) as client_count, \r\ncount(distinct(l.id)) as active_loan_count,\r\ncount(distinct(if(r.duedate <= curdate(), \r\n if(ifnull(r.principal_amount,0) - ifnull(r.principal_completed_derived, 0), l.id, null), null) )) as principal_arrears_loan_count,\r\n\r\nsum(ifnull(r.principal_amount,0)) as principal,\r\nsum(ifnull(r.principal_completed_derived, 0)) as principal_repaid,\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as principal_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as principal_overdue,\r\n\r\nsum(ifnull(r.interest_amount,0)) as interest,\r\nsum(ifnull(r.interest_completed_derived, 0)) as interest_repaid,\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as interest_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as interest_overdue,\r\n\r\nsum(ifnull(r.fee_charges_amount,0)) as fees,\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as fees_repaid,\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as fees_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as fees_overdue,\r\n\r\nsum(ifnull(r.penalty_charges_amount,0)) as penalties,\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as penalties_repaid,\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as penalties_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as penalties_overdue\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\ngroup by ounder.id, l.currency_code) x on x.branch = mo.id\r\norder by mo.hierarchy, x.Currency',NULL,1,1),(12,'Active Loans Details','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nlo.display_name as \"Loan Officer\", \r\nc.display_name as \"Client\", l.account_no as \"Loan Account No.\", pl.`name` as \"Product\", \r\nf.`name` as Fund, \r\nl.principal_amount as \"Loan Amount\", \r\nl.annual_nominal_interest_rate as \" Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed Date\", \r\ndate(l.expected_maturedon_date) as \"Expected Matured On\",\r\n\r\nsum(ifnull(r.principal_completed_derived, 0)) as \"Principal Repaid\",\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_completed_derived, 0)) as \"Interest Repaid\",\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as \"Fees Repaid\",\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as \"Penalties Repaid\",\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\ngroup by l.id\r\norder by ounder.hierarchy, l.currency_code, c.account_no, l.account_no','Individual Client \n\nReport',1,1),(13,'Obligation Met Loans Details','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nc.account_no as \"Client Account No.\", c.display_name as \"Client\",\r\nl.account_no as \"Loan Account No.\", pl.`name` as \"Product\", \r\nf.`name` as Fund, \r\nl.principal_amount as \"Loan Amount\", \r\n(sum(ifnull(r.principal_completed_derived, 0)) + \r\n sum(ifnull(r.interest_completed_derived, 0)) + \r\n sum(ifnull(r.fee_charges_completed_derived, 0)) + \r\n sum(ifnull(r.penalty_charges_completed_derived, 0))) as \"Total Repaid\", \r\nl.annual_nominal_interest_rate as \" Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed\", \r\ndate(l.closedon_date) as \"Closed\",\r\n\r\nsum(ifnull(r.principal_completed_derived, 0)) as \"Principal Repaid\",\r\nsum(ifnull(r.interest_completed_derived, 0)) as \"Interest Repaid\",\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as \"Fees Repaid\",\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as \"Penalties Repaid\",\r\nlo.display_name as \"Loan Officer\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand (case\r\n when ${obligDateType} = 1 then\r\n l.closedon_date between \'${startDate}\' and \'${endDate}\'\r\n when ${obligDateType} = 2 then\r\n l.disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\n else 1 = 1\r\n end)\r\nand l.loan_status_id = 600\r\ngroup by l.id\r\norder by ounder.hierarchy, l.currency_code, c.account_no, l.account_no','Individual Client \n\nReport',1,1),(14,'Obligation Met Loans Summary','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\ncount(distinct(c.id)) as \"No. of Clients\",\r\ncount(distinct(l.id)) as \"No. of Loans\",\r\nsum(l.principal_amount) as \"Total Loan Amount\", \r\n\"wait for derived field\" as \"Total Principal Repaid\",\r\n\"wait for derived field\" as \"Total Interest Repaid\",\r\n\"wait for derived field\" as \"Total Fees Repaid\",\r\n\"wait for derived field\" as \"Total Interest Waived\",\r\n\"wait for derived field\" as \"Total Fees Waived\",\r\n\"wait for derived field\" as \"Total Penalties Waived\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand (case\r\n when ${obligDateType} = 1 then\r\n l.closedon_date between \'${startDate}\' and \'${endDate}\'\r\n when ${obligDateType} = 2 then\r\n l.disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\n else 1 = 1\r\n end)\r\nand l.loan_status_id = 600\r\ngroup by o.hierarchy, l.currency_code\r\norder by ounder.hierarchy, l.currency_code','Individual Client \n\nReport',1,1),(15,'Portfolio at Risk','Table',NULL,'Loan','select x.Currency, x.`Principal Outstanding`, x.`Principal Overdue`, x.`Interest Outstanding`, x.`Interest Overdue`, \r\nx.`Fees Outstanding`, x.`Fees Overdue`, x.`Penalties Outstanding`, x.`Penalties Overdue`,\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.`Principal Overdue` * 100) / x.`Principal Outstanding`, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding`), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue` + x.`Fees Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding` + x.`Fees Outstanding`), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue` + x.`Fees Overdue` + x.`Penalties Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding` + x.`Fees Outstanding` + x.`Penalties Overdue`), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from \r\n(select ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nleft join m_product_loan p on p.id = l.product_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n and r.completed_derived is false\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\ngroup by l.currency_code\r\norder by l.currency_code) x','Covers all loans.\r\n\r\nFor larger MFIs … we should add some derived fields on loan (or a 1:1 loan related table like mifos 2.x does)\r\nPrinciple, Interest, Fees, Penalties Outstanding and Overdue (possibly waived and written off too)',1,1),(16,'Portfolio at Risk by Branch','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(mo.`hierarchy`) - LENGTH(REPLACE(mo.`hierarchy`, \'.\', \'\')) - 1))), mo.`name`) as \"Office/Branch\",\r\nx.Currency, x.`Principal Outstanding`, x.`Principal Overdue`, x.`Interest Outstanding`, x.`Interest Overdue`, \r\nx.`Fees Outstanding`, x.`Fees Overdue`, x.`Penalties Outstanding`, x.`Penalties Overdue`,\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.`Principal Overdue` * 100) / x.`Principal Outstanding`, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding`), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue` + x.`Fees Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding` + x.`Fees Outstanding`), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.`Principal Overdue` + x.`Interest Overdue` + x.`Fees Overdue` + x.`Penalties Overdue`) * 100) / (x.`Principal Outstanding` + x.`Interest Outstanding` + x.`Fees Outstanding` + x.`Penalties Overdue`), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from m_office mo\r\njoin \r\n(select ounder.id as \"branch\", ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_code_value purp on purp.id = l.loanpurpose_cv_id\r\nleft join m_product_loan p on p.id = l.product_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n and r.completed_derived is false\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\ngroup by ounder.id, l.currency_code) x on x.branch = mo.id\r\norder by mo.hierarchy, x.Currency','Covers all loans.\r\n\r\nFor larger MFIs … we should add some derived fields on loan (or a 1:1 loan related table like mifos 2.x does)\r\nPrinciple, Interest, Fees, Penalties Outstanding and Overdue (possibly waived and written off too)',1,1),(20,'Funds Disbursed Between Dates Summary','Table',NULL,'Fund','select ifnull(f.`name`, \'-\') as Fund, ifnull(cur.display_symbol, l.currency_code) as Currency, \r\nround(sum(l.principal_amount), 4) as disbursed_amount\r\nfrom m_office ounder \r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_currency cur on cur.`code` = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nwhere disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (l.currency_code = \'${currencyId}\' or \'-1\' = \'${currencyId}\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\ngroup by ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, l.currency_code)\r\norder by ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, l.currency_code)',NULL,1,1),(21,'Funds Disbursed Between Dates Summary by Office','Table',NULL,'Fund','select \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\n \n\nifnull(f.`name`, \'-\') as Fund, ifnull(cur.display_symbol, l.currency_code) as Currency, round(sum(l.principal_amount), 4) as disbursed_amount\r\nfrom m_office o\r\n\n\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c \n\non c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_currency cur on cur.`code` = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\n\n\nwhere disbursedon_date between \'${startDate}\' and \'${endDate}\'\r\nand o.id = ${officeId}\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand \n\n(l.currency_code = \'${currencyId}\' or \'-1\' = \'${currencyId}\')\r\ngroup by ounder.`name`, ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, \n\nl.currency_code)\r\norder by ounder.`name`, ifnull(f.`name`, \'-\') , ifnull(cur.display_symbol, l.currency_code)',NULL,1,1),(48,'Balance Sheet','Pentaho',NULL,'Accounting',NULL,'Balance Sheet',1,0),(49,'Income Statement','Pentaho',NULL,'Accounting',NULL,'Profit and Loss Statement',1,0),(50,'Trial Balance','Pentaho',NULL,'Accounting',NULL,'Trial Balance Report',1,0),(51,'Written-Off Loans','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, ml.currency_code) as Currency, \r\nc.account_no as \"Client Account No.\",\r\nc.display_name AS \'Client Name\',\r\nml.account_no AS \'Loan Account No.\',\r\nmpl.name AS \'Product Name\',\r\nml.disbursedon_date AS \'Disbursed Date\',\r\nlt.transaction_date AS \'Written Off date\',\r\nml.principal_amount as \"Loan Amount\",\r\nifnull(lt.principal_portion_derived, 0) AS \'Written-Off Principal\',\r\nifnull(lt.interest_portion_derived, 0) AS \'Written-Off Interest\',\r\nifnull(lt.fee_charges_portion_derived,0) AS \'Written-Off Fees\',\r\nifnull(lt.penalty_charges_portion_derived,0) AS \'Written-Off Penalties\',\r\nn.note AS \'Reason For Write-Off\',\r\nIFNULL(ms.display_name,\'-\') AS \'Loan Officer Name\'\r\nFROM m_office o\r\nJOIN m_office ounder ON ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nAND ounder.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\nJOIN m_client c ON c.office_id = ounder.id\r\nJOIN m_loan ml ON ml.client_id = c.id\r\nJOIN m_product_loan mpl ON mpl.id=ml.product_id\r\nLEFT JOIN m_staff ms ON ms.id=ml.loan_officer_id\r\nJOIN m_loan_transaction lt ON lt.loan_id = ml.id\r\nLEFT JOIN m_note n ON n.loan_transaction_id = lt.id\r\nLEFT JOIN m_currency cur on cur.code = ml.currency_code\r\nWHERE lt.transaction_type_enum = 6 /*write-off */\r\nAND lt.is_reversed is false \r\nAND ml.loan_status_id=601\r\nAND o.id=${officeId}\r\nAND (mpl.id=${loanProductId} OR ${loanProductId}=-1)\r\nAND lt.transaction_date BETWEEN \'${startDate}\' AND \'${endDate}\'\r\nAND (ml.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nORDER BY ounder.hierarchy, ifnull(cur.display_symbol, ml.currency_code), ml.account_no','Individual Lending Report. Written Off Loans',1,1),(52,'Aging Summary (Arrears in weeks) - review','Table',NULL,'Loan','SELECT \r\n arr.clientName AS \"Client Name\",\r\n arr.accNo AS \"Account Number\",\r\n arr.loanAmt AS \"Loan Amount\",\r\n loan.principal AS \"Original Principal\",\r\n loan.interest AS \"Original Interest\",\r\n loan.prinPaid AS \"Principal Paid\",\r\n loan.intPaid AS \"Interest Paid\",\r\n arr.prinOverdue AS \"Principal Overdue\",\r\n arr.intOverdue AS \"Interest Overdue\",\r\n arr.arrWeek AS \"Weeks In Arrears\"\r\nFROM \r\n (SELECT mr.loan_id loanId,\r\n mc.display_name AS clientName,\r\n ml.account_no AS accNo,\r\n ml.principal_amount AS loanAmt,\r\n SUM(mr.principal_amount - IFNULL(mr.principal_completed_derived, 0)) AS prinOverdue, \r\n SUM(mr.interest_amount - IFNULL(mr.interest_completed_derived, 0)) AS intOverdue,\r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<7, \'Less than a week in arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<8, \'1 Week in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<15, \'2 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<22, \'3 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<29, \'4 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<36, \'5 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<43, \'6 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<50, \'7 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<57, \'8 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<64, \'9 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<71, \'10 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<78, \'11 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), MIN(mr.duedate))<85, \'12 Weeks in Arrears\', \'12+ Weeks in Arrears\'))))))))))))) AS arrWeek\r\n FROM m_office mo \r\n JOIN m_office ounder ON ounder.hierarchy like concat(mo.hierarchy, \'%\')\r\n AND ounder.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\n INNER JOIN m_client mc ON mc.office_id=ounder.id\r\n INNER JOIN m_loan ml ON ml.client_id = mc.id\r\n INNER JOIN r_enum_value rev ON rev.enum_id=ml.loan_status_id\r\n INNER JOIN m_loan_repayment_schedule mr ON mr.loan_id=ml.id\r\n WHERE mr.completed_derived=0 \r\n AND mr.duedate <= CURDATE() \r\n AND ml.loan_status_id=300\r\n AND mo.id=${officeId}\r\n GROUP BY mr.loan_id\r\n ORDER BY mc.display_name) arr\r\nINNER JOIN \r\n (SELECT mr.loan_id loanId, \r\n SUM(mr.principal_amount) principal,\r\n SUM(mr.interest_amount) interest,\r\n SUM(IFNULL(mr.principal_completed_derived,0)) prinPaid,\r\n SUM(IFNULL(mr.interest_completed_derived,0)) intPaid\r\n FROM m_loan_repayment_schedule mr\r\n GROUP BY mr.loan_id) loan ON arr.loanId=loan.loanId','Loan arrears aging (Weeks)',1,1),(53,'Amount In Arrears','Table',NULL,'Loan','SELECT \r\n IFNULL(weeks.currencyName, weeks.currency) as currency, \r\n weeks.week_no \'Weeks In Arrears\', \r\n IFNULL(ars.loanId, 0) \'No Of Loans\', \r\n IFNULL(ars.principal,0.0) \'Original Principal\', \r\n IFNULL(ars.interest,0.0) \'Original Interest\', \r\n IFNULL(ars.prinPaid,0.0) \'Principal Paid\', \r\n IFNULL(ars.intPaid,0.0) \'Interest Paid\', \r\n IFNULL(ars.prinOverdue,0.0) \'Principal Overdue\', \r\n IFNULL(ars.intOverdue,0.0)\'Interest Overdue\'\r\nFROM \r\n /* full table of aging weeks/currencies used combo to ensure each line represented */\r\n (SELECT curs.code as currency, curs.name as currencyName, wks.* from\r\n (SELECT \'On Schedule\' week_no,0 wid UNION\r\n SELECT \'0 to 6 days arrears\',1 UNION\r\n SELECT \'1 Week in Arrears\',2 UNION\r\n SELECT \'2 Weeks in Arrears\',3 UNION\r\n SELECT \'3 Weeks in Arrears\',4 UNION\r\n SELECT \'4 Weeks in Arrears\',5 UNION\r\n SELECT \'5 Weeks in Arrears\',6 UNION\r\n SELECT \'6 Weeks in Arrears\',7 UNION\r\n SELECT \'7 Weeks in Arrears\',8 UNION\r\n SELECT \'8 Weeks in Arrears\',9 UNION\r\n SELECT \'9 Weeks in Arrears\',10 UNION\r\n SELECT \'10 Weeks in Arrears\',11 UNION\r\n SELECT \'11 Weeks in Arrears\',12 UNION\r\n SELECT \'12 Weeks in Arrears\',13 UNION\r\n SELECT \'12+ Weeks in Arrears\',14) wks,\r\n (SELECT distinctrow moc.code, moc.name\r\n FROM m_office mo2\r\n INNER JOIN m_office ounder2 ON ounder2.hierarchy \r\n LIKE CONCAT(mo2.hierarchy, \'%\')\r\nAND ounder2.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\n INNER JOIN m_client mc2 ON mc2.office_id=ounder2.id\r\n INNER JOIN m_loan ml2 ON ml2.client_id = mc2.id\r\n INNER JOIN m_organisation_currency moc ON moc.code = ml2.currency_code\r\n WHERE ml2.loan_status_id=300 /* active */\r\n AND mo2.id=${officeId}) curs) weeks\r\n\r\n\r\nLEFT JOIN /* table of aging weeks per currency with gaps if no applicable loans */\r\n(SELECT \r\n z.currency, z.arrWeek, \r\n COUNT(z.loanId) as loanId, SUM(z.principal) as principal, SUM(z.interest) as interest, \r\n SUM(z.prinPaid) as prinPaid, SUM(z.intPaid) as intPaid, \r\n SUM(z.prinOverdue) as prinOverdue, SUM(z.intOverdue) as intOverdue\r\nFROM\r\n /*derived table just used to get arrWeek value (was much slower to\r\n duplicate calc of minOverdueDate in inner query) */\r\n (SELECT x.loanId, x.currency, x.principal, x.interest, x.prinPaid, x.intPaid, x.prinOverdue, x.intOverdue,\r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<0, \'On Schedule\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<7, \'0 to 6 days arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<8, \'1 Week in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<15, \'2 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<22, \'3 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<29, \'4 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<36, \'5 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<43, \'6 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<50, \'7 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<57, \'8 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<64, \'9 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<71, \'10 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<78, \'11 Weeks in Arrears\', \r\n IF(DATEDIFF(CURDATE(), minOverdueDate)<85, \'12 Weeks in Arrears\',\r\n \'12+ Weeks in Arrears\')))))))))))))) AS arrWeek\r\n\r\n FROM /* get the individual loan details */\r\n (SELECT ml.id AS loanId, ml.currency_code as currency,\r\n SUM(mr.principal_amount) principal, \r\n SUM(mr.interest_amount) interest, \r\n SUM(IFNULL(mr.principal_completed_derived,0)) prinPaid, \r\n SUM(IFNULL(mr.interest_completed_derived,0)) intPaid,\r\n\r\n SUM(IF((mr.completed_derived is false) AND (mr.duedate <= CURDATE()), \r\n (mr.principal_amount - ifnull(mr.principal_completed_derived, 0))\r\n , 0)) as prinOverdue,\r\n SUM(IF((mr.completed_derived is false) AND (mr.duedate <= CURDATE()), \r\n (mr.interest_amount - ifnull(mr.interest_completed_derived, 0))\r\n , 0)) as intOverdue,\r\n\r\n CAST(MIN(\r\n IF((mr.completed_derived is false) AND (mr.duedate <= CURDATE()), mr.duedate, CURDATE() + 1)) as date) as minOverdueDate\r\n \r\n FROM m_office mo\r\n INNER JOIN m_office ounder ON ounder.hierarchy \r\n LIKE CONCAT(mo.hierarchy, \'%\')\r\nAND ounder.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\n INNER JOIN m_client mc ON mc.office_id=ounder.id\r\n INNER JOIN m_loan ml ON ml.client_id = mc.id\r\n INNER JOIN m_loan_repayment_schedule mr on mr.loan_id = ml.id\r\n WHERE ml.loan_status_id=300 /* active */\r\n AND mo.id=${officeId}\r\n GROUP BY ml.id) x\r\n ) z \r\nGROUP BY z.currency, z.arrWeek ) ars ON ars.arrWeek=weeks.week_no and ars.currency = weeks.currency\r\nORDER BY weeks.currency, weeks.wid','Loan amount in arrears by branch',1,1),(54,'Rescheduled Loans','Table',NULL,'Loan','SELECT \r\nconcat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, ml.currency_code) as Currency, \r\nc.account_no as \"Client Account No.\",\r\nc.display_name AS \'Client Name\',\r\nml.account_no AS \'Loan Account No.\',\r\nmpl.name AS \'Product Name\',\r\nml.disbursedon_date AS \'Disbursed Date\',\r\nlt.transaction_date AS \'Written Off date\',\r\nml.principal_amount as \"Loan Amount\",\r\nifnull(lt.principal_portion_derived, 0) AS \'Rescheduled Principal\',\r\nifnull(lt.interest_portion_derived, 0) AS \'Rescheduled Interest\',\r\nifnull(lt.fee_charges_portion_derived,0) AS \'Rescheduled Fees\',\r\nifnull(lt.penalty_charges_portion_derived,0) AS \'Rescheduled Penalties\',\r\nn.note AS \'Reason For Rescheduling\',\r\nIFNULL(ms.display_name,\'-\') AS \'Loan Officer Name\'\r\nFROM m_office o\r\nJOIN m_office ounder ON ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nAND ounder.hierarchy like CONCAT(\'${currentUserHierarchy}\', \'%\')\r\nJOIN m_client c ON c.office_id = ounder.id\r\nJOIN m_loan ml ON ml.client_id = c.id\r\nJOIN m_product_loan mpl ON mpl.id=ml.product_id\r\nLEFT JOIN m_staff ms ON ms.id=ml.loan_officer_id\r\nJOIN m_loan_transaction lt ON lt.loan_id = ml.id\r\nLEFT JOIN m_note n ON n.loan_transaction_id = lt.id\r\nLEFT JOIN m_currency cur on cur.code = ml.currency_code\r\nWHERE lt.transaction_type_enum = 7 /*marked for rescheduling */\r\nAND lt.is_reversed is false \r\nAND ml.loan_status_id=602\r\nAND o.id=${officeId}\r\nAND (mpl.id=${loanProductId} OR ${loanProductId}=-1)\r\nAND lt.transaction_date BETWEEN \'${startDate}\' AND \'${endDate}\'\r\nAND (ml.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nORDER BY ounder.hierarchy, ifnull(cur.display_symbol, ml.currency_code), ml.account_no','Individual Lending Report. Rescheduled Loans. The ability to reschedule (or mark that you have rescheduled the loan elsewhere) is a legacy of the older Mifos product. Needed for migration.',1,1),(55,'Active Loans Passed Final Maturity','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(ounder.`hierarchy`) - LENGTH(REPLACE(ounder.`hierarchy`, \'.\', \'\')) - 1))), ounder.`name`) as \"Office/Branch\",\r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nlo.display_name as \"Loan Officer\", \r\nc.display_name as \"Client\", l.account_no as \"Loan Account No.\", pl.`name` as \"Product\", \r\nf.`name` as Fund, \r\nl.principal_amount as \"Loan Amount\", \r\nl.annual_nominal_interest_rate as \" Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed Date\", \r\ndate(l.expected_maturedon_date) as \"Expected Matured On\",\r\n\r\nsum(ifnull(r.principal_completed_derived, 0)) as \"Principal Repaid\",\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_completed_derived, 0)) as \"Interest Repaid\",\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as \"Fees Repaid\",\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as \"Penalties Repaid\",\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\nand l.expected_maturedon_date < curdate()\r\ngroup by l.id\r\norder by ounder.hierarchy, l.currency_code, c.account_no, l.account_no','Individual Client \n\nReport',1,1),(56,'Active Loans Passed Final Maturity Summary','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(mo.`hierarchy`) - LENGTH(REPLACE(mo.`hierarchy`, \'.\', \'\')) - 1))), mo.`name`) as \"Office/Branch\", x.currency as Currency,\r\n x.client_count as \"No. of Clients\", x.active_loan_count as \"No. Active Loans\", x. principal_arrears_loan_count as \"No. of Loans in Arrears\",\r\nx.principal as \"Total Loans Disbursed\", x.principal_repaid as \"Principal Repaid\", x.principal_outstanding as \"Principal Outstanding\", x.principal_overdue as \"Principal Overdue\",\r\nx.interest as \"Total Interest\", x.interest_repaid as \"Interest Repaid\", x.interest_outstanding as \"Interest Outstanding\", x.interest_overdue as \"Interest Overdue\",\r\nx.fees as \"Total Fees\", x.fees_repaid as \"Fees Repaid\", x.fees_outstanding as \"Fees Outstanding\", x.fees_overdue as \"Fees Overdue\",\r\nx.penalties as \"Total Penalties\", x.penalties_repaid as \"Penalties Repaid\", x.penalties_outstanding as \"Penalties Outstanding\", x.penalties_overdue as \"Penalties Overdue\",\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.principal_overdue * 100) / x.principal_outstanding, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.principal_overdue + x.interest_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue + x.penalties_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding + x.penalties_overdue), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from m_office mo\r\njoin \r\n(select ounder.id as branch,\r\nifnull(cur.display_symbol, l.currency_code) as currency,\r\ncount(distinct(c.id)) as client_count, \r\ncount(distinct(l.id)) as active_loan_count,\r\ncount(distinct(if(r.duedate <= curdate(), \r\n if(ifnull(r.principal_amount,0) - ifnull(r.principal_completed_derived, 0), l.id, null), null) )) as principal_arrears_loan_count,\r\n\r\nsum(ifnull(r.principal_amount,0)) as principal,\r\nsum(ifnull(r.principal_completed_derived, 0)) as principal_repaid,\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as principal_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as principal_overdue,\r\n\r\nsum(ifnull(r.interest_amount,0)) as interest,\r\nsum(ifnull(r.interest_completed_derived, 0)) as interest_repaid,\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as interest_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as interest_overdue,\r\n\r\nsum(ifnull(r.fee_charges_amount,0)) as fees,\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as fees_repaid,\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as fees_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as fees_overdue,\r\n\r\nsum(ifnull(r.penalty_charges_amount,0)) as penalties,\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as penalties_repaid,\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as penalties_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as penalties_overdue\r\n\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\n\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\nand l.expected_maturedon_date < curdate()\r\ngroup by ounder.id, l.currency_code) x on x.branch = mo.id\r\norder by mo.hierarchy, x.Currency',NULL,1,1),(57,'Active Loans in last installment','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(lastInstallment.`hierarchy`) - LENGTH(REPLACE(lastInstallment.`hierarchy`, \'.\', \'\')) - 1))), lastInstallment.branch) as \"Office/Branch\",\r\nlastInstallment.Currency,\r\nlastInstallment.`Loan Officer`, \r\nlastInstallment.`Client Account No`, lastInstallment.`Client`, \r\nlastInstallment.`Loan Account No`, lastInstallment.`Product`, \r\nlastInstallment.`Fund`, lastInstallment.`Loan Amount`, \r\nlastInstallment.`Annual Nominal Interest Rate`, \r\nlastInstallment.`Disbursed`, lastInstallment.`Expected Matured On` ,\r\n\r\nsum(ifnull(r.principal_completed_derived, 0)) as \"Principal Repaid\",\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as \"Principal Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as \"Principal Overdue\",\r\n\r\nsum(ifnull(r.interest_completed_derived, 0)) as \"Interest Repaid\",\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as \"Interest Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as \"Interest Overdue\",\r\n\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as \"Fees Repaid\",\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as \"Fees Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Fees Overdue\",\r\n\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as \"Penalties Repaid\",\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as \"Penalties Outstanding\",\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as \"Penalties Overdue\"\r\n\r\nfrom \r\n(select l.id as loanId, l.number_of_repayments, min(r.installment), \r\nounder.id, ounder.hierarchy, ounder.`name` as branch, \r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nlo.display_name as \"Loan Officer\", c.account_no as \"Client Account No\",\r\nc.display_name as \"Client\", l.account_no as \"Loan Account No\", pl.`name` as \"Product\", \r\nf.`name` as Fund, l.principal_amount as \"Loan Amount\", \r\nl.annual_nominal_interest_rate as \"Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed\", date(l.expected_maturedon_date) as \"Expected Matured On\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\nand r.completed_derived is false\r\nand r.duedate >= curdate()\r\ngroup by l.id\r\nhaving l.number_of_repayments = min(r.installment)) lastInstallment\r\njoin m_loan_repayment_schedule r on r.loan_id = lastInstallment.loanId\r\ngroup by lastInstallment.loanId\r\norder by lastInstallment.hierarchy, lastInstallment.Currency, lastInstallment.`Client Account No`, lastInstallment.`Loan Account No`','Individual Client \n\nReport',1,1),(58,'Active Loans in last installment Summary','Table',NULL,'Loan','select concat(repeat(\"..\", \r\n ((LENGTH(mo.`hierarchy`) - LENGTH(REPLACE(mo.`hierarchy`, \'.\', \'\')) - 1))), mo.`name`) as \"Office/Branch\", x.currency as Currency,\r\n x.client_count as \"No. of Clients\", x.active_loan_count as \"No. Active Loans\", x. principal_arrears_loan_count as \"No. of Loans in Arrears\",\r\nx.principal as \"Total Loans Disbursed\", x.principal_repaid as \"Principal Repaid\", x.principal_outstanding as \"Principal Outstanding\", x.principal_overdue as \"Principal Overdue\",\r\nx.interest as \"Total Interest\", x.interest_repaid as \"Interest Repaid\", x.interest_outstanding as \"Interest Outstanding\", x.interest_overdue as \"Interest Overdue\",\r\nx.fees as \"Total Fees\", x.fees_repaid as \"Fees Repaid\", x.fees_outstanding as \"Fees Outstanding\", x.fees_overdue as \"Fees Overdue\",\r\nx.penalties as \"Total Penalties\", x.penalties_repaid as \"Penalties Repaid\", x.penalties_outstanding as \"Penalties Outstanding\", x.penalties_overdue as \"Penalties Overdue\",\r\n\r\n (case\r\n when ${parType} = 1 then\r\n cast(round((x.principal_overdue * 100) / x.principal_outstanding, 2) as char)\r\n when ${parType} = 2 then\r\n cast(round(((x.principal_overdue + x.interest_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding), 2) as char)\r\n when ${parType} = 3 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding), 2) as char)\r\n when ${parType} = 4 then\r\n cast(round(((x.principal_overdue + x.interest_overdue + x.fees_overdue + x.penalties_overdue) * 100) / (x.principal_outstanding + x.interest_outstanding + x.fees_outstanding + x.penalties_overdue), 2) as char)\r\n else \"invalid PAR Type\"\r\n end) as \"Portfolio at Risk %\"\r\n from m_office mo\r\njoin \r\n(select lastInstallment.branchId as branchId,\r\nlastInstallment.Currency,\r\ncount(distinct(lastInstallment.clientId)) as client_count, \r\ncount(distinct(lastInstallment.loanId)) as active_loan_count,\r\ncount(distinct(if(r.duedate <= curdate(), \r\n if(ifnull(r.principal_amount,0) - ifnull(r.principal_completed_derived, 0),lastInstallment.loanId, null), null) )) as principal_arrears_loan_count,\r\n\r\nsum(ifnull(r.principal_amount,0)) as principal,\r\nsum(ifnull(r.principal_completed_derived, 0)) as principal_repaid,\r\nsum(ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0))) as principal_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.principal_amount,0) - (ifnull(r.principal_completed_derived, 0) + ifnull(r.principal_writtenoff_derived, 0)))\r\n , 0)) as principal_overdue,\r\n\r\nsum(ifnull(r.interest_amount,0)) as interest,\r\nsum(ifnull(r.interest_completed_derived, 0)) as interest_repaid,\r\nsum(ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0))) as interest_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.interest_amount,0) - (ifnull(r.interest_completed_derived, 0) + ifnull(r.interest_waived_derived, 0) + ifnull(r.interest_writtenoff_derived, 0)))\r\n , 0)) as interest_overdue,\r\n\r\nsum(ifnull(r.fee_charges_amount,0)) as fees,\r\nsum(ifnull(r.fee_charges_completed_derived, 0)) as fees_repaid,\r\nsum(ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0))) as fees_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.fee_charges_amount,0) - (ifnull(r.fee_charges_completed_derived, 0) + ifnull(r.fee_charges_waived_derived, 0) + ifnull(r.fee_charges_writtenoff_derived, 0)))\r\n , 0)) as fees_overdue,\r\n\r\nsum(ifnull(r.penalty_charges_amount,0)) as penalties,\r\nsum(ifnull(r.penalty_charges_completed_derived, 0)) as penalties_repaid,\r\nsum(ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0))) as penalties_outstanding,\r\nsum(if(r.duedate <= curdate(), \r\n (ifnull(r.penalty_charges_amount,0) - (ifnull(r.penalty_charges_completed_derived, 0) + ifnull(r.penalty_charges_waived_derived, 0) + ifnull(r.penalty_charges_writtenoff_derived, 0)))\r\n , 0)) as penalties_overdue\r\n\r\nfrom \r\n(select l.id as loanId, l.number_of_repayments, min(r.installment), \r\nounder.id as branchId, ounder.hierarchy, ounder.`name` as branch, \r\nifnull(cur.display_symbol, l.currency_code) as Currency,\r\nlo.display_name as \"Loan Officer\", c.id as clientId, c.account_no as \"Client Account No\",\r\nc.display_name as \"Client\", l.account_no as \"Loan Account No\", pl.`name` as \"Product\", \r\nf.`name` as Fund, l.principal_amount as \"Loan Amount\", \r\nl.annual_nominal_interest_rate as \"Annual Nominal Interest Rate\", \r\ndate(l.disbursedon_date) as \"Disbursed\", date(l.expected_maturedon_date) as \"Expected Matured On\"\r\nfrom m_office o \r\njoin m_office ounder on ounder.hierarchy like concat(o.hierarchy, \'%\')\r\nand ounder.hierarchy like concat(\'${currentUserHierarchy}\', \'%\')\r\njoin m_client c on c.office_id = ounder.id\r\njoin m_loan l on l.client_id = c.id\r\njoin m_product_loan pl on pl.id = l.product_id\r\nleft join m_staff lo on lo.id = l.loan_officer_id\r\nleft join m_currency cur on cur.code = l.currency_code\r\nleft join m_fund f on f.id = l.fund_id\r\nleft join m_loan_repayment_schedule r on r.loan_id = l.id\r\nwhere o.id = ${officeId}\r\nand (l.currency_code = \"${currencyId}\" or \"-1\" = \"${currencyId}\")\r\nand (l.product_id = \"${loanProductId}\" or \"-1\" = \"${loanProductId}\")\r\nand (ifnull(l.loan_officer_id, -10) = \"${loanOfficerId}\" or \"-1\" = \"${loanOfficerId}\")\r\nand (ifnull(l.fund_id, -10) = ${fundId} or -1 = ${fundId})\r\nand (ifnull(l.loanpurpose_cv_id, -10) = ${loanPurposeId} or -1 = ${loanPurposeId})\r\nand l.loan_status_id = 300\r\nand r.completed_derived is false\r\nand r.duedate >= curdate()\r\ngroup by l.id\r\nhaving l.number_of_repayments = min(r.installment)) lastInstallment\r\njoin m_loan_repayment_schedule r on r.loan_id = lastInstallment.loanId\r\ngroup by lastInstallment.branchId) x on x.branchId = mo.id\r\norder by mo.hierarchy, x.Currency','Individual Client \n\nReport',1,1); -INSERT INTO `stretchy_parameter` VALUES (1,'startDateSelect','startDate','startDate','date','date','today',NULL,NULL,NULL,NULL),(2,'endDateSelect','endDate','endDate','date','date','today',NULL,NULL,NULL,NULL),(3,'obligDateTypeSelect','obligDateType','obligDateType','select','number','0',NULL,'Y',NULL,'select * from\r\n(select 1 as id, \"Closed\" as `name` union all\r\nselect 2, \"Disbursal\" ) x\r\norder by x.`id`'),(5,'OfficeIdSelectOne','officeId','Office','select','number','0',NULL,'Y',NULL,'select id, \r\nconcat(substring(\"........................................\", 1, \r\n \n\n((LENGTH(`hierarchy`) - LENGTH(REPLACE(`hierarchy`, \'.\', \'\')) - 1) * 4)), \r\n `name`) as tc\r\nfrom m_office\r\nwhere hierarchy like concat\n\n(\'${currentUserHierarchy}\', \'%\')\r\norder by hierarchy'),(6,'loanOfficerIdSelectAll','loanOfficerId','Loan Officer','select','number','0',NULL,'Y','Y','(select id, \n\ndisplay_name as `Name` from m_staff\nwhere is_loan_officer = true)\r\nunion all\r\n(select -10, \'-\')\r\norder by 2'),(10,'currencyIdSelectAll','currencyId','Currency','select','number','0',NULL,'Y','Y','select `code`, `name`\r\nfrom m_organisation_currency\r\norder by `code`'),(11,'currencyIdSelectOne','currencyId','Currency','select','number','0',NULL,'Y',NULL,'select `code`, `name`\r\nfrom m_organisation_currency\r\norder by `code`'),(20,'fundIdSelectAll','fundId','Fund','select','number','0',NULL,'Y','Y','(select id, `name`\r\nfrom m_fund)\r\nunion all\r\n(select -10, \'-\')\r\norder by 2'),(25,'loanProductIdSelectAll','loanProductId','Product','select','number','0',NULL,'Y','Y','select id, `name`\r\nfrom m_product_loan\r\norder by 2'),(26,'loanPurposeIdSelectAll','loanPurposeId','Loan Purpose','select','number','0',NULL,'Y','Y','select -10 as id, \'-\' as code_value\r\nunion all\r\nselect * from (select v.id, v.code_value\r\nfrom m_code c\r\njoin m_code_value v on v.code_id = c.id\r\nwhere c.code_name = \"loanPurpose\"\r\norder by v.order_position) x'),(100,'parTypeSelect','parType','parType','select','number','0',NULL,'Y',NULL,'select * from\r\n(select 1 as id, \"Principal Only\" as `name` union all\r\nselect 2, \"Principal + Interest\" union all\r\nselect 3, \"Principal + Interest + Fees\" union all\r\nselect 4, \"Principal + Interest + Fees + Penalties\") x\r\norder by x.`id`'),(1001,'FullReportList',NULL,'n/a','n/a','n/a','n/a','Y',NULL,NULL,'select r.report_id, r.report_name, r.report_type, r.report_subtype, r.report_category,\r\n \n\nrp.parameter_id, rp.report_parameter_name, p.parameter_name\r\n from stretchy_report r\r\n left join stretchy_report_parameter rp on rp.report_id = r.report_id\r\n \n\nleft join stretchy_parameter p on p.parameter_id = rp.parameter_id\r\n where r.use_report is true\r\n and exists\r\n (\r\n select \'f\'\r\n from m_appuser_role ur \n\n\r\n join m_role r on r.id = ur.role_id\r\n join m_role_permission rp on rp.role_id = r.id\r\n join m_permission p on p.id = rp.permission_id\r\n where \n\nur.appuser_id = ${currentUserId}\r\n and (p.code in (\'ALL_FUNCTIONS_READ\', \'ALL_FUNCTIONS\') or p.code = concat(\"READ_\", r.report_name))\r\n )\r\n order by \n\nr.report_category, r.report_name, rp.parameter_id'),(1002,'FullParameterList',NULL,'n/a','n/a','n/a','n/a','Y',NULL,NULL,'select parameter_name, parameter_variable, \n\nparameter_label, parameter_displayType, \r\nparameter_FormatType, parameter_default, selectOne, selectAll\r\nfrom stretchy_parameter p\r\nwhere special is null\r\n\n\norder by parameter_id'),(1003,'reportCategoryList',NULL,'n/a','n/a','n/a','n/a','Y',NULL,NULL,'select r.report_id, r.report_name, r.report_type, r.report_subtype, \n\nr.report_category,\r\n rp.parameter_id, rp.report_parameter_name, p.parameter_name\r\n from stretchy_report r\r\n left join stretchy_report_parameter rp on \n\nrp.report_id = r.report_id\r\n left join stretchy_parameter p on p.parameter_id = rp.parameter_id\r\n where r.report_category = \'${reportCategory}\'\r\n and \n\nr.use_report is true\r\n and exists\r\n (\r\n select \'f\'\r\n from m_appuser_role ur \r\n join m_role r on r.id = ur.role_id\r\n join m_role_permission rp on \n\nrp.role_id = r.id\r\n join m_permission p on p.id = rp.permission_id\r\n where ur.appuser_id = ${currentUserId}\r\n and (p.code in (\'ALL_FUNCTIONS_READ\', \n\n\'ALL_FUNCTIONS\') or p.code = concat(\"READ_\", r.report_name))\r\n )\r\n order by r.report_category, r.report_name, rp.parameter_id'); -INSERT INTO `stretchy_report_parameter` VALUES (1,5,NULL),(2,5,NULL),(2,6,NULL),(2,10,NULL),(2,20,NULL),(2,25,NULL),(2,26,NULL),(5,5,NULL),(5,6,NULL),(5,10,NULL),(5,20,NULL),(5,25,NULL),(5,26,NULL),(6,5,NULL),(6,6,NULL),(6,10,NULL),(6,20,NULL),(6,25,NULL),(6,26,NULL),(7,5,NULL),(7,6,NULL),(7,10,NULL),(7,20,NULL),(7,25,NULL),(7,26,NULL),(8,5,NULL),(8,6,NULL),(8,10,NULL),(8,25,NULL),(8,26,NULL),(11,5,NULL),(11,6,NULL),(11,10,NULL),(11,20,NULL),(11,25,NULL),(11,26,NULL),(11,100,NULL),(12,5,NULL),(12,6,NULL),(12,10,NULL),(12,20,NULL),(12,25,NULL),(12,26,NULL),(13,1,NULL),(13,2,NULL),(13,3,NULL),(13,5,NULL),(13,6,NULL),(13,10,NULL),(13,20,NULL),(13,25,NULL),(13,26,NULL),(14,1,NULL),(14,2,NULL),(14,3,NULL),(14,5,NULL),(14,6,NULL),(14,10,NULL),(14,20,NULL),(14,25,NULL),(14,26,NULL),(15,5,NULL),(15,6,NULL),(15,10,NULL),(15,20,NULL),(15,25,NULL),(15,26,NULL),(15,100,NULL),(16,5,NULL),(16,6,NULL),(16,10,NULL),(16,20,NULL),(16,25,NULL),(16,26,NULL),(16,100,NULL),(20,1,NULL),(20,2,NULL),(20,10,NULL),(20,20,NULL),(21,1,NULL),(21,2,NULL),(21,5,NULL),(21,10,NULL),(21,20,NULL),(48,5,'branch'),(48,2,'date'),(49,5,'branch'),(49,1,'fromDate'),(49,2,'toDate'),(50,5,'branch'),(50,1,'fromDate'),(50,2,'toDate'),(51,1,NULL),(51,2,NULL),(51,5,NULL),(51,10,NULL),(51,25,NULL),(52,5,NULL),(53,5,NULL),(54,1,NULL),(54,2,NULL),(54,5,NULL),(54,10,NULL),(54,25,NULL),(55,5,NULL),(55,6,NULL),(55,10,NULL),(55,20,NULL),(55,25,NULL),(55,26,NULL),(56,5,NULL),(56,6,NULL),(56,10,NULL),(56,20,NULL),(56,25,NULL),(56,26,NULL),(56,100,NULL),(57,5,NULL),(57,6,NULL),(57,10,NULL),(57,20,NULL),(57,25,NULL),(57,26,NULL),(58,5,NULL),(58,6,NULL),(58,10,NULL),(58,20,NULL),(58,25,NULL),(58,26,NULL),(58,100,NULL); -