ALTER TABLE LoanDemandUnpaid 
	DROP FOREIGN KEY LoanDemandUnpaid_ibfk_1,
	DROP FOREIGN KEY LoanDemandUnpaid_ibfk_2,
	DROP FOREIGN KEY LoanDemandUnpaid_ibfk_3,
	DROP INDEX loandemandunpaid_acct_type_date_fee_idx,
	DROP INDEX loandemandunpaid_date_idx;
RENAME TABLE LoanDemandUnpaid TO AccountDemandUnpaid;
ALTER TABLE AccountDemandUnpaid 
	ADD FOREIGN KEY (account_id) REFERENCES Account (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (type_id) REFERENCES TransactionTypeMaster (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (fee_id) REFERENCES Fee (id) ON DELETE NO ACTION ON UPDATE CASCADE;
CREATE UNIQUE INDEX accountdemandunpaid_acct_type_date_fee_idx ON AccountDemandUnpaid(account_id, date, type_id, fee_id);
CREATE INDEX accountdemandunpaid_date_idx ON AccountDemandUnpaid(date);

ALTER TABLE LoanPayment
	DROP FOREIGN KEY LoanPayment_ibfk_1,
	DROP FOREIGN KEY LoanPayment_ibfk_2,
	DROP FOREIGN KEY LoanPayment_ibfk_3,
	DROP FOREIGN KEY LoanPayment_ibfk_4,
	DROP FOREIGN KEY LoanPayment_ibfk_5,
	DROP FOREIGN KEY LoanPayment_ibfk_6,
	DROP FOREIGN KEY LoanPayment_ibfk_7,
	DROP INDEX loanpayment_acct_date_idx,
	DROP INDEX loanpayment_related_payment_id_idx;

ALTER TABLE LoanPaymentCheque
	DROP FOREIGN KEY LoanPaymentCheque_ibfk_1,
	DROP FOREIGN KEY LoanPaymentCheque_ibfk_2;

ALTER TABLE LoanPaymentReceipt
	DROP FOREIGN KEY LoanPaymentReceipt_ibfk_1,
	DROP FOREIGN KEY LoanPaymentReceipt_ibfk_2,
	DROP INDEX loanpaymentreceipt_book_no_idx;

ALTER TABLE LoanPaymentDemand
	DROP FOREIGN KEY LoanPaymentDemand_ibfk_1,
	DROP FOREIGN KEY LoanPaymentDemand_ibfk_2;

ALTER TABLE LoanWriteoff
	DROP FOREIGN KEY LoanWriteoff_ibfk_1;

ALTER TABLE LoanTransaction
	DROP FOREIGN KEY LoanTransaction_ibfk_1,
	DROP FOREIGN KEY LoanTransaction_ibfk_2,
	DROP FOREIGN KEY LoanTransaction_ibfk_3,
	DROP FOREIGN KEY LoanTransaction_ibfk_4,
	DROP INDEX loantransaction_acct_date_tx_idx,
	DROP INDEX loantransaction_acct_date_dem_idx,
	DROP INDEX loantransaction_acct_date_pay_idx;

ALTER TABLE LoanSystemTransaction
	DROP FOREIGN KEY LoanSystemTransaction_ibfk_1,
	DROP FOREIGN KEY LoanSystemTransaction_ibfk_2,
	DROP FOREIGN KEY LoanSystemTransaction_ibfk_3,
	DROP INDEX loansystemtransaction_acct_date_idx;

ALTER TABLE LoanDemand
	DROP FOREIGN KEY LoanDemand_ibfk_1,
	DROP FOREIGN KEY LoanDemand_ibfk_2,
	DROP FOREIGN KEY LoanDemand_ibfk_3,
	DROP INDEX loandemand_acct_type_date_fee_idx,
	DROP INDEX loandemand_date_idx;

RENAME TABLE LoanDemand TO AccountDemand;
ALTER TABLE AccountDemand
	ADD FOREIGN KEY (account_id) REFERENCES Account (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (type_id) REFERENCES TransactionTypeMaster (id) ON DELETE NO ACTION ON UPDATE CASCADE,
	ADD FOREIGN KEY (fee_id) REFERENCES Fee (id) ON DELETE NO ACTION ON UPDATE CASCADE;
CREATE UNIQUE INDEX accountdemand_acct_type_date_fee_idx ON AccountDemand(account_id, date, type_id, fee_id);
CREATE INDEX accountdemand_date_idx ON AccountDemand(date);

RENAME TABLE LoanPayment TO AccountPayment;
ALTER TABLE AccountPayment
	ADD FOREIGN KEY (account_id) REFERENCES Account (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (type_id) REFERENCES PaymentTypeMaster (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (mode_id) REFERENCES PaymentModeMaster (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (paid_by_id) REFERENCES PaymentByMaster (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (status) REFERENCES PaymentStatusMaster (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (related_payment_id) REFERENCES AccountPayment (id) ON DELETE NO ACTION ON UPDATE CASCADE;
CREATE INDEX accountpayment_acct_date_idx ON AccountPayment(account_id, date);
CREATE INDEX accountpayment_related_payment_id_idx ON AccountPayment(related_payment_id ASC);

RENAME TABLE LoanPaymentCheque TO AccountPaymentCheque;
ALTER TABLE AccountPaymentCheque
	ADD FOREIGN KEY (payment_id) REFERENCES AccountPayment (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (cheque_id) REFERENCES Cheque (id) ON DELETE NO ACTION ON UPDATE CASCADE;

RENAME TABLE LoanPaymentReceipt TO AccountPaymentReceipt;
ALTER TABLE AccountPaymentReceipt
	ADD FOREIGN KEY (payment_id) REFERENCES AccountPayment (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (receipt_book_id) REFERENCES ReceiptBook (id) ON DELETE NO ACTION ON UPDATE CASCADE;
CREATE UNIQUE INDEX accountpaymentreceipt_book_no_idx ON AccountPaymentReceipt(receipt_book_id, receipt_no);

RENAME TABLE LoanPaymentDemand TO AccountPaymentDemand;
ALTER TABLE AccountPaymentDemand
	ADD FOREIGN KEY (payment_id) REFERENCES AccountPayment (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (demand_id) REFERENCES AccountDemand (id) ON DELETE NO ACTION ON UPDATE CASCADE;

RENAME TABLE LoanTransaction TO AccountTransaction;
ALTER TABLE AccountTransaction
	ADD FOREIGN KEY (account_id) REFERENCES Account (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (tx_gl_type_id) REFERENCES TransactionGLTypeMap (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (demand_id) REFERENCES AccountDemand (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (payment_id) REFERENCES AccountPayment (id) ON DELETE NO ACTION ON UPDATE CASCADE;
CREATE INDEX accounttransaction_acct_date_tx_idx ON AccountTransaction(account_id, date, tx_gl_type_id);
CREATE INDEX accounttransaction_acct_date_dem_idx ON AccountTransaction(account_id, date, demand_id);
CREATE INDEX accounttransaction_acct_date_pay_idx ON AccountTransaction(account_id, date, payment_id);

RENAME TABLE LoanSystemTransaction TO AccountSystemTransaction;
ALTER TABLE AccountSystemTransaction
	ADD FOREIGN KEY (account_id) REFERENCES Account (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (tx_gl_type_id) REFERENCES TransactionGLTypeMap (id) ON DELETE NO ACTION ON UPDATE CASCADE,
  	ADD FOREIGN KEY (demand_id) REFERENCES AccountDemand (id) ON DELETE NO ACTION ON UPDATE CASCADE;
CREATE INDEX accountsystemtransaction_acct_date_idx ON AccountSystemTransaction(account_id, date);

ALTER TABLE LoanWriteoff
	 ADD FOREIGN KEY (payment_id) REFERENCES AccountPayment (id) ON DELETE NO ACTION ON UPDATE CASCADE;

