patches and updates related to mifosx-175 Loan Derived Fields

This commit is contained in:
John Woodlock
2013-03-04 13:22:11 +00:00
parent d81316541d
commit b6c09cab38
6 changed files with 242 additions and 29 deletions
@@ -27,6 +27,7 @@ DROP TABLE IF EXISTS `m_group_client`;
DROP TABLE IF EXISTS `m_guarantor`;
DROP TABLE IF EXISTS `m_loan`;
DROP TABLE IF EXISTS `m_loan_charge`;
DROP TABLE IF EXISTS `m_loan_arrears_aging`;
DROP TABLE IF EXISTS `m_loan_collateral`;
DROP TABLE IF EXISTS `m_loan_officer_assignment_history`;
DROP TABLE IF EXISTS `m_loan_repayment_schedule`;
@@ -526,6 +527,7 @@ CREATE TABLE `m_product_loan_charge` (
-- ============ end of financial product related tables ===========
-- DDL for loan and loan related tables
CREATE TABLE `m_loan` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`account_no` varchar(20) NOT NULL,
@@ -536,7 +538,6 @@ CREATE TABLE `m_loan` (
`fund_id` bigint(20) DEFAULT NULL,
`loan_officer_id` bigint(20) DEFAULT NULL,
`loanpurpose_cv_id` int(11) DEFAULT NULL,
`loan_transaction_strategy_id` bigint(20) DEFAULT NULL,
`loan_status_id` smallint(5) NOT NULL,
`currency_code` varchar(3) NOT NULL,
`currency_digits` smallint(5) NOT NULL,
@@ -549,53 +550,43 @@ CREATE TABLE `m_loan` (
`interest_calculated_in_period_enum` smallint(5) NOT NULL DEFAULT '1',
`term_frequency` smallint(5) NOT NULL DEFAULT '0',
`term_period_frequency_enum` smallint(5) NOT NULL DEFAULT '2',
`number_of_repayments` smallint(5) NOT NULL,
`repay_every` smallint(5) NOT NULL,
`repayment_period_frequency_enum` smallint(5) NOT NULL,
`number_of_repayments` smallint(5) NOT NULL,
`amortization_method_enum` smallint(5) NOT NULL,
`interest_calculated_from_date` date DEFAULT NULL,
`expected_disbursedon_date` date DEFAULT NULL,
`expected_firstrepaymenton_date` date DEFAULT NULL,
`expected_maturedon_date` date DEFAULT NULL,
`maturedon_date` date DEFAULT NULL,
`submittedon_date` date DEFAULT NULL,
`submittedon_userid` bigint(20) DEFAULT NULL,
`approvedon_date` date DEFAULT NULL,
`approvedon_userid` bigint(20) DEFAULT NULL,
`rejectedon_date` date DEFAULT NULL,
`rejectedon_userid` bigint(20) DEFAULT NULL,
`withdrawnon_date` date DEFAULT NULL,
`withdrawnon_userid` bigint(20) DEFAULT NULL,
`expected_disbursedon_date` date DEFAULT NULL,
`expected_firstrepaymenton_date` date DEFAULT NULL,
`interest_calculated_from_date` date DEFAULT NULL,
`disbursedon_date` date DEFAULT NULL,
`disbursedon_userid` bigint(20) DEFAULT NULL,
`rescheduledon_date` date DEFAULT NULL,
`writtenoffon_date` date DEFAULT NULL,
`expected_maturedon_date` date DEFAULT NULL,
`maturedon_date` date DEFAULT NULL,
`closedon_date` date DEFAULT NULL,
`closedon_userid` bigint(20) DEFAULT NULL,
`total_charges_due_at_disbursement_derived` decimal(19,6) DEFAULT NULL,
`total_charges_due_at_disbursement_derived` decimal(19,6) DEFAULT NULL,
`principal_disbursed_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`principal_repaid_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`principal_writtenoff_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`principal_outstanding_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`principal_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`interest_charged_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`interest_repaid_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`interest_waived_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`interest_writtenoff_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`interest_outstanding_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`interest_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`fee_charges_charged_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`fee_charges_repaid_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`fee_charges_waived_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`fee_charges_writtenoff_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`fee_charges_outstanding_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`fee_charges_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`penalty_charges_charged_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`penalty_charges_repaid_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`penalty_charges_waived_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`penalty_charges_writtenoff_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`penalty_charges_outstanding_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`penalty_charges_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`total_expected_repayment_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`total_repayment_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`total_expected_costofloan_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
@@ -603,8 +594,13 @@ CREATE TABLE `m_loan` (
`total_waived_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`total_writtenoff_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`total_outstanding_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`total_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`overdue_since_date_derived` date DEFAULT NULL,
`rejectedon_date` date DEFAULT NULL,
`rejectedon_userid` bigint(20) DEFAULT NULL,
`rescheduledon_date` date DEFAULT NULL,
`withdrawnon_date` date DEFAULT NULL,
`withdrawnon_userid` bigint(20) DEFAULT NULL,
`writtenoffon_date` date DEFAULT NULL,
`loan_transaction_strategy_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `loan_account_no_UNIQUE` (`account_no`),
UNIQUE KEY `loan_externalid_UNIQUE` (`external_id`),
@@ -613,27 +609,39 @@ CREATE TABLE `m_loan` (
KEY `FK7C885877240145` (`fund_id`),
KEY `FK_loan_ltp_strategy` (`loan_transaction_strategy_id`),
KEY `FK_m_loan_m_staff` (`loan_officer_id`),
KEY `FK_m_loanpurpose_codevalue` (`loanpurpose_cv_id`),
KEY `group_id` (`group_id`),
KEY `FK_m_loanpurpose_codevalue` (`loanpurpose_cv_id`),
KEY `FK_submittedon_userid` (`submittedon_userid`),
KEY `FK_approvedon_userid` (`approvedon_userid`),
KEY `FK_rejectedon_userid` (`rejectedon_userid`),
KEY `FK_withdrawnon_userid` (`withdrawnon_userid`),
KEY `FK_closedon_userid` (`closedon_userid`),
KEY `FK_disbursedon_userid` (`disbursedon_userid`),
CONSTRAINT `FKB6F935D87179A0CB` FOREIGN KEY (`client_id`) REFERENCES `m_client` (`id`),
CONSTRAINT `m_loan_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `m_group` (`id`),
CONSTRAINT `FKB6F935D8C8D4B434` FOREIGN KEY (`product_id`) REFERENCES `m_product_loan` (`id`),
KEY `FK_closedon_userid` (`closedon_userid`),
CONSTRAINT `FK7C885877240145` FOREIGN KEY (`fund_id`) REFERENCES `m_fund` (`id`),
CONSTRAINT `FKB6F935D87179A0CB` FOREIGN KEY (`client_id`) REFERENCES `m_client` (`id`),
CONSTRAINT `FKB6F935D8C8D4B434` FOREIGN KEY (`product_id`) REFERENCES `m_product_loan` (`id`),
CONSTRAINT `FK_approvedon_userid` FOREIGN KEY (`approvedon_userid`) REFERENCES `m_appuser` (`id`),
CONSTRAINT `FK_closedon_userid` FOREIGN KEY (`closedon_userid`) REFERENCES `m_appuser` (`id`),
CONSTRAINT `FK_disbursedon_userid` FOREIGN KEY (`disbursedon_userid`) REFERENCES `m_appuser` (`id`),
CONSTRAINT `FK_loan_ltp_strategy` FOREIGN KEY (`loan_transaction_strategy_id`) REFERENCES `ref_loan_transaction_processing_strategy` (`id`),
CONSTRAINT `FK_m_loanpurpose_codevalue` FOREIGN KEY (`loanpurpose_cv_id`) REFERENCES `m_code_value` (`id`),
CONSTRAINT `FK_m_loan_m_staff` FOREIGN KEY (`loan_officer_id`) REFERENCES `m_staff` (`id`),
CONSTRAINT `FK_submittedon_userid` FOREIGN KEY (`submittedon_userid`) REFERENCES `m_appuser` (`id`),
CONSTRAINT `FK_approvedon_userid` FOREIGN KEY (`approvedon_userid`) REFERENCES `m_appuser` (`id`),
CONSTRAINT `FK_rejectedon_userid` FOREIGN KEY (`rejectedon_userid`) REFERENCES `m_appuser` (`id`),
CONSTRAINT `FK_submittedon_userid` FOREIGN KEY (`submittedon_userid`) REFERENCES `m_appuser` (`id`),
CONSTRAINT `FK_withdrawnon_userid` FOREIGN KEY (`withdrawnon_userid`) REFERENCES `m_appuser` (`id`),
CONSTRAINT `FK_disbursedon_userid` FOREIGN KEY (`disbursedon_userid`) REFERENCES `m_appuser` (`id`),
CONSTRAINT `FK_closedon_userid` FOREIGN KEY (`closedon_userid`) REFERENCES `m_appuser` (`id`)
CONSTRAINT `m_loan_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `m_group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `m_loan_arrears_aging` (
`loan_id` bigint(20) NOT NULL AUTO_INCREMENT,
`principal_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`interest_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`fee_charges_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`penalty_charges_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`total_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`overdue_since_date_derived` date DEFAULT NULL,
PRIMARY KEY (`loan_id`),
CONSTRAINT `m_loan_arrears_aging_ibfk_1` FOREIGN KEY (`loan_id`) REFERENCES `m_loan` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `m_guarantor` (
@@ -31,6 +31,31 @@ VALUES
('repayment_period_frequency_enum',2,'Months','Months'),('term_period_frequency_enum',0,'Days','Days'),
('term_period_frequency_enum',1,'Weeks','Weeks'),('term_period_frequency_enum',2,'Months','Months'),('term_period_frequency_enum',3,'Years','Years');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '1', 'Disbursement', 'Disbursement');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '2', 'Repayment', 'Repayment');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '3', 'Contra', 'Contra');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '4', 'Waive Interest', 'Waive Interest');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '5', 'Repayment At Disbursement', 'Repayment At Disbursement');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '6', 'Write-Off', 'Write-Off');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '8', 'Recovery Repayment', 'Recovery Repayment');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '9', 'Waive Charges', 'Waive Charges');
INSERT INTO `m_currency`
(`id`,`code`,`decimal_places`,`display_symbol`,`name`, `internationalized_name_code`)
VALUES
@@ -0,0 +1,25 @@
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '1', 'Disbursement', 'Disbursement');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '2', 'Repayment', 'Repayment');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '3', 'Contra', 'Contra');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '4', 'Waive Interest', 'Waive Interest');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '5', 'Repayment At Disbursement', 'Repayment At Disbursement');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '6', 'Write-Off', 'Write-Off');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '8', 'Recovery Repayment', 'Recovery Repayment');
INSERT INTO `r_enum_value` (`enum_name`, `enum_id`, `enum_message_property`, `enum_value`)
VALUES ('transaction_type_enum', '9', 'Waive Charges', 'Waive Charges');
@@ -0,0 +1,22 @@
DROP TABLE IF EXISTS `m_loan_arrears_aging`;
CREATE TABLE `m_loan_arrears_aging` (
`loan_id` bigint(20) NOT NULL AUTO_INCREMENT,
`principal_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`interest_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`fee_charges_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`penalty_charges_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`total_overdue_derived` decimal(19,6) NOT NULL DEFAULT '0.000000',
`overdue_since_date_derived` date DEFAULT NULL,
PRIMARY KEY (`loan_id`),
CONSTRAINT `m_loan_arrears_aging_ibfk_1` FOREIGN KEY (`loan_id`) REFERENCES `m_loan` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE m_loan
DROP COLUMN `total_overdue_derived` ,
DROP COLUMN `overdue_since_date_derived` ,
DROP COLUMN `penalty_charges_overdue_derived` ,
DROP COLUMN `fee_charges_overdue_derived` ,
DROP COLUMN `interest_overdue_derived` ,
DROP COLUMN `principal_overdue_derived` ;
@@ -0,0 +1,80 @@
update m_loan
join
(SELECT ml.id AS loanId,
SUM(mr.principal_amount) as principal_disbursed_derived,
SUM(IFNULL(mr.principal_completed_derived,0)) as principal_repaid_derived,
SUM(IFNULL(mr.principal_writtenoff_derived,0)) as principal_writtenoff_derived,
SUM(IFNULL(mr.interest_amount,0)) as interest_charged_derived,
SUM(IFNULL(mr.interest_completed_derived,0)) as interest_repaid_derived,
SUM(IFNULL(mr.interest_waived_derived,0)) as interest_waived_derived,
SUM(IFNULL(mr.interest_writtenoff_derived,0)) as interest_writtenoff_derived,
SUM(IFNULL(mr.fee_charges_amount,0)) as fee_charges_charged_derived,
SUM(IFNULL(mr.fee_charges_completed_derived,0)) as fee_charges_repaid_derived,
SUM(IFNULL(mr.fee_charges_waived_derived,0)) as fee_charges_waived_derived,
SUM(IFNULL(mr.fee_charges_writtenoff_derived,0)) as fee_charges_writtenoff_derived,
SUM(IFNULL(mr.penalty_charges_amount,0)) as penalty_charges_charged_derived,
SUM(IFNULL(mr.penalty_charges_completed_derived,0)) as penalty_charges_repaid_derived,
SUM(IFNULL(mr.penalty_charges_waived_derived,0)) as penalty_charges_waived_derived,
SUM(IFNULL(mr.penalty_charges_writtenoff_derived,0)) as penalty_charges_writtenoff_derived
FROM m_loan ml
INNER JOIN m_loan_repayment_schedule mr on mr.loan_id = ml.id
WHERE ml.disbursedon_date is not null
GROUP BY ml.id) x on x.loanId = m_loan.id
set m_loan.principal_disbursed_derived = x.principal_disbursed_derived,
m_loan.principal_repaid_derived = x.principal_repaid_derived,
m_loan.principal_writtenoff_derived = x.principal_writtenoff_derived,
m_loan.principal_outstanding_derived = (x.principal_disbursed_derived -
(x.principal_repaid_derived + x.principal_writtenoff_derived)),
m_loan.interest_charged_derived = x.interest_charged_derived,
m_loan.interest_repaid_derived = x.interest_repaid_derived,
m_loan.interest_waived_derived = x.interest_waived_derived,
m_loan.interest_writtenoff_derived = x.interest_writtenoff_derived,
m_loan.interest_outstanding_derived = (x.interest_charged_derived -
(x.interest_repaid_derived + x.interest_waived_derived + x.interest_writtenoff_derived)),
m_loan.fee_charges_charged_derived = x.fee_charges_charged_derived,
m_loan.fee_charges_repaid_derived = x.fee_charges_repaid_derived,
m_loan.fee_charges_waived_derived = x.fee_charges_waived_derived,
m_loan.fee_charges_writtenoff_derived = x.fee_charges_writtenoff_derived,
m_loan.fee_charges_outstanding_derived = (x.fee_charges_charged_derived -
(x.fee_charges_repaid_derived + x.fee_charges_waived_derived + x.fee_charges_writtenoff_derived)),
m_loan.penalty_charges_charged_derived = x.penalty_charges_charged_derived,
m_loan.penalty_charges_repaid_derived = x.penalty_charges_repaid_derived,
m_loan.penalty_charges_waived_derived = x.penalty_charges_waived_derived,
m_loan.penalty_charges_writtenoff_derived = x.penalty_charges_writtenoff_derived,
m_loan.fee_charges_outstanding_derived = (x.penalty_charges_charged_derived -
(x.penalty_charges_repaid_derived + x.penalty_charges_waived_derived + x.penalty_charges_writtenoff_derived)),
m_loan.total_expected_repayment_derived = (x.principal_disbursed_derived + x.interest_charged_derived +
x.fee_charges_charged_derived + x.penalty_charges_charged_derived),
m_loan.total_repayment_derived = (x.principal_repaid_derived + x.interest_repaid_derived +
x.fee_charges_repaid_derived + x.penalty_charges_repaid_derived),
m_loan.total_expected_costofloan_derived = (x.interest_charged_derived +
x.fee_charges_charged_derived + x.penalty_charges_charged_derived),
m_loan.total_costofloan_derived = (x.interest_repaid_derived +
x.fee_charges_repaid_derived + x.penalty_charges_repaid_derived),
m_loan.total_waived_derived = (x.interest_waived_derived +
x.fee_charges_waived_derived + x.penalty_charges_waived_derived),
m_loan.total_writtenoff_derived = (x.interest_writtenoff_derived +
x.fee_charges_writtenoff_derived + x.penalty_charges_writtenoff_derived),
m_loan.total_outstanding_derived =
(x.principal_disbursed_derived -
(x.principal_repaid_derived + x.principal_writtenoff_derived)) +
(x.interest_charged_derived -
(x.interest_repaid_derived + x.interest_waived_derived + x.interest_writtenoff_derived)) +
(x.fee_charges_charged_derived -
(x.fee_charges_repaid_derived + x.fee_charges_waived_derived + x.fee_charges_writtenoff_derived)) +
(x.penalty_charges_charged_derived -
(x.penalty_charges_repaid_derived + x.penalty_charges_waived_derived + x.penalty_charges_writtenoff_derived))
@@ -0,0 +1,53 @@
/*
This patch (or something like it) needs to run daily (after midnight and before next day opening)
against each tenant to keep the arrears values accurate
*/
truncate table m_loan_arrears_aging;
INSERT INTO m_loan_arrears_aging
(`loan_id`,
`principal_overdue_derived`,
`interest_overdue_derived`,
`fee_charges_overdue_derived`,
`penalty_charges_overdue_derived`,
`total_overdue_derived`,
`overdue_since_date_derived`)
select ml.id as loanId,
SUM(
(ifnull(mr.principal_amount,0) - ifnull(mr.principal_completed_derived, 0))
) as principal_overdue_derived,
SUM(
(ifnull(mr.interest_amount,0) - ifnull(mr.interest_completed_derived, 0))
) as interest_overdue_derived,
SUM(
(ifnull(mr.fee_charges_amount,0) - ifnull(mr.fee_charges_completed_derived, 0))
) as fee_charges_overdue_derived,
SUM(
(ifnull(mr.penalty_charges_amount,0) - ifnull(mr.penalty_charges_completed_derived, 0))
) as penalty_charges_overdue_derived,
SUM(
(ifnull(mr.principal_amount,0) - ifnull(mr.principal_completed_derived, 0))
) +
SUM(
(ifnull(mr.interest_amount,0) - ifnull(mr.interest_completed_derived, 0))
) +
SUM(
(ifnull(mr.fee_charges_amount,0) - ifnull(mr.fee_charges_completed_derived, 0))
) +
SUM(
(ifnull(mr.penalty_charges_amount,0) - ifnull(mr.penalty_charges_completed_derived, 0))
) as total_overdue_derived,
MIN(mr.duedate) as overdue_since_date_derived
FROM m_loan ml
INNER JOIN m_loan_repayment_schedule mr on mr.loan_id = ml.id
WHERE ml.loan_status_id = 300 /*active*/
and mr.completed_derived is false
and mr.duedate < CURDATE()
GROUP BY ml.id;