create procedure proc_contract_rent_month()
BEGIN
       DECLARE p_tax_rate DECIMAL(18,2) DEFAULT 1.06;  
       DECLARE p_interest DECIMAL(20,16);
       DECLARE p_leas_form VARCHAR(32);
       DECLARE n_interest DECIMAL(20,16); 
       DECLARE n_formula VARCHAR(32); 
       DECLARE l_formula VARCHAR(32);
       DECLARE p_contract_id VARCHAR(32);
       DECLARE p_payment_number VARCHAR(32);
       DECLARE p_all_interest VARCHAR(32);
       DECLARE p_handling_charge_money VARCHAR(32);
       DECLARE p_remain_corpus VARCHAR(32);
       DECLARE p_prev_plan_date VARCHAR(32);
       DECLARE p_year_rate VARCHAR(32);
       DECLARE p_day_rate VARCHAR(32);
       DECLARE p_month_interest DECIMAL(20,16);
       DECLARE income_number VARCHAR(32);
       DECLARE done BOOLEAN DEFAULT FALSE;
       DECLARE contract_info CURSOR FOR
	SELECT lci.leas_form,lcc.contract_id p_contract_id,lcc.payment_number p_payment_number,IFNULL(lr.interest,0) p_all_interest,lcc.handling_charge_money/1.06 p_handling_charge_money,lcc.clean_lease_money p_remain_corpus,lcc.start_date p_prev_plan_date,lcc.year_rate p_year_rate,lcc.year_rate/100/360 p_day_rate,
	
	CASE WHEN  SIGN((ROUND(lcc.clean_lease_money*(lcc.year_rate/100/360),2)*(TIMESTAMPDIFF(DAY,DATE_FORMAT(lcc.start_date,'%Y/%m/%d'),LAST_DAY(DATE_FORMAT(lcc.start_date,'%Y/%m/%d')))))-0)=1 THEN  lcc.clean_lease_money*(lcc.year_rate/100/360)*(TIMESTAMPDIFF(DAY,DATE_FORMAT(lcc.start_date,'%Y/%m/%d'),LAST_DAY(DATE_FORMAT(lcc.start_date,'%Y/%m/%d')))) ELSE 0 END AS p_month_interest,lcc.INCOME_NUMBER income_number FROM lc_calc_condition lcc LEFT JOIN lb_contract_info lci ON lci.id = lcc.contract_id LEFT JOIN (SELECT contract_id,SUM(interest/1.06) interest FROM lc_rent_plan GROUP BY contract_id) lr ON lr.contract_id = lcc.contract_id WHERE NOT EXISTS (SELECT 1 FROM contract_rent_month WHERE contract_id = lcc.contract_id) AND nvl(lr.interest,0) > 0 AND lci.contract_status BETWEEN 31 AND 100 AND lci.id NOT IN  (SELECT contract_id FROM lc_fund_rent_adjust)  and lci.businesstype='1';
	
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
	
	
	OPEN contract_info;
	clyLoop:LOOP
		FETCH contract_info INTO p_leas_form,p_contract_id,p_payment_number, p_all_interest, p_handling_charge_money,p_remain_corpus,p_prev_plan_date,p_year_rate,p_day_rate,p_month_interest,income_number;
		
       IF done THEN LEAVE clyLoop;
	END IF;	 
       BEGIN
          DECLARE al_apport_handling_money DECIMAL(22,16) DEFAULT 0.00;
          DECLARE rent_time_limit INT DEFAULT 0;
          DECLARE hc_money DECIMAL(22,16) DEFAULT 0.00;
		IF (p_leas_form ='01') THEN SET p_tax_rate=1.16 ;
		END IF; 
           SET  rent_time_limit=income_number;
           SET  n_interest =p_month_interest;
           SET  n_formula = '';
             INSERT INTO contract_rent_month(id,rent_list,interest,fee,accrued_date,start_date,end_date,
                                             formula,hand_formula,create_date,contract_id,payment_number)
             VALUES(REPLACE(UUID(),'-',''),'0',ROUND(n_interest/p_tax_rate,2),
             
             ROUND(CASE WHEN SIGN(p_handling_charge_money*ROUND(n_interest/p_tax_rate,2)-0)=1 THEN p_handling_charge_money*(n_interest/p_tax_rate)/p_all_interest ELSE 0 END,2),
             DATE_FORMAT(p_prev_plan_date,'%Y/%m'),
             DATE_ADD(p_prev_plan_date,INTERVAL -DAY(p_prev_plan_date)+1 DAY),
             LAST_DAY(p_prev_plan_date),
             n_formula,'',
             DATE_FORMAT(NOW(),'%Y/%m/%d'),p_contract_id,p_payment_number);
             SET al_apport_handling_money=al_apport_handling_money+ROUND((CASE WHEN SIGN(p_handling_charge_money*ROUND(n_interest/p_tax_rate,2)-0)=1 THEN  p_handling_charge_money*(n_interest/p_tax_rate)/p_all_interest ELSE 0 END),2);
             SET n_formula = '';
             BEGIN
             DECLARE r_interest DECIMAL(20,16);
             DECLARE r_all_remain_corpus DECIMAL(20,2);
             DECLARE r_plan_date VARCHAR(32);
             DECLARE r_plan_list VARCHAR(32);
	     DECLARE subdone BOOLEAN DEFAULT FALSE;
             DECLARE rent_info CURSOR FOR
               SELECT interest,all_remain_corpus,plan_date,plan_list FROM lc_rent_plan WHERE contract_id = p_contract_id ORDER BY contract_id,plan_list;
              DECLARE CONTINUE HANDLER FOR NOT FOUND SET subdone = TRUE;
              OPEN rent_info;
	      subLoop:LOOP
	      FETCH rent_info INTO r_interest,r_all_remain_corpus,r_plan_date,r_plan_list;
	      
		IF subdone THEN LEAVE subLoop;
		END IF;
               BEGIN
                 SET p_interest = ((CASE WHEN r_interest = 0 THEN 0 ELSE r_interest END)-n_interest)/p_tax_rate;
                 SET n_interest = CASE WHEN ROUND(r_all_remain_corpus*p_day_rate,2)*(
           
                 TIMESTAMPDIFF(DAY,DATE_FORMAT(r_plan_date,'%Y/%m/%d'),LAST_DAY(DATE_FORMAT(r_plan_date,'%Y/%m/%d')))
                 ) = 0 THEN 0 ELSE (r_all_remain_corpus*p_day_rate)*(TIMESTAMPDIFF(DAY,DATE_FORMAT(r_plan_date,'%Y/%m/%d'),LAST_DAY(DATE_FORMAT(r_plan_date,'%Y/%m/%d')))) END;
                 SET l_formula = '';
                 IF  r_plan_list=rent_time_limit THEN
                  SET hc_money=p_handling_charge_money-al_apport_handling_money;
                   ELSE SET hc_money=ROUND((CASE WHEN SIGN(p_handling_charge_money*ROUND(p_interest+n_interest,2)-0)=1 THEN  p_handling_charge_money*(p_interest+n_interest/p_tax_rate)/p_all_interest ELSE 0 END),2);
                   END IF;
                 INSERT INTO contract_rent_month(id,rent_list,interest,fee,accrued_date,start_date,end_date,formula,hand_formula,create_date,contract_id,payment_number)
                 VALUES(REPLACE(UUID(),'-',''),r_plan_list,ROUND(p_interest+n_interest/p_tax_rate,2),hc_money,
                        DATE_FORMAT(r_plan_date,'%Y/%m'),
                        DATE_ADD(r_plan_date,INTERVAL -DAY(r_plan_date)+1 DAY),
                        LAST_DAY(r_plan_date),
                        '','',DATE_FORMAT(NOW(),'%Y/%m/%d'),p_contract_id,p_payment_number);
                 SET n_formula = l_formula;
                 SET al_apport_handling_money=al_apport_handling_money+ROUND((CASE WHEN SIGN(p_handling_charge_money*ROUND(p_interest+n_interest,2)-0)=1 THEN p_handling_charge_money*(p_interest+n_interest/p_tax_rate)/p_all_interest ELSE 0 END),2);
               END;
             END LOOP;
             CLOSE rent_info;
           END;
          END;
         END LOOP;
       CLOSE contract_info;
END;

