create procedure proc_insert_hexiao(IN ln_contract_id varchar(200), IN ln_plan_date varchar(200), IN in_rent varchar(200), IN in_hire_list varchar(200), IN in_type varchar(2))
BEGIN 
 -- 如果in_type=1就是手工核销
 IF in_type = 1 THEN
   INSERT INTO LC_RENT_INCOME (
  ID,
  QUOT_ID,
  CUST_ID,
  PROJECT_ID,
  PROJECT_PLAN_NUMBER,
  CONTRACT_ID,
  CONTRACT_PLAN_NUMBER,
  PAYMENT_NUMBER,
  EBANK_NUMBER,
  PLAN_ID,
  PLAN_LIST,
 INTEREST_ADJUST,
  SETTLE_METHOD,
  HIRE_LIST,
  HIRE_DATE,
  RENT,
  CORPUS,
  INTEREST,
  PENALTY,
  CORPUS_ADJUST,
  PENALTY_ADJUST,
  ROLL_BACK,
  COIN,
  hire_number,
  hire_bank,
  hire_account,
  own_bank,
  own_account,
  own_number
) 
  (SELECT 
    REPLACE (UUID(), '-', '') AS id,
    O.QUOT_ID AS QUOT_ID,
    O.CUST_ID AS CUST_ID,
    O.PROJECT_ID AS PROJECT_ID,
    O.PROJECT_PLAN_NUMBER AS PROJECT_PLAN_NUMBER,
    O.CONTRACT_ID AS CONTRACT_ID,
    O.CONTRACT_PLAN_NUMBER AS CONTRACT_PLAN_NUMBER,
    O.PAYMENT_NUMBER AS PAYMENT_NUMBER,
    '' AS EBANK_NUMBER,
    O.id AS PLAN_ID,
    O.PLAN_LIST AS PLAN_LIST,
   '0' AS INTEREST_ADJUST,
    'settlemethod6' AS SETTLE_METHOD,
    in_hire_list AS HIRE_LIST,
  DATE_FORMAT (CURDATE(),'%Y/%m/%d')  AS HIRE_DATE,
    vl.rent_over AS RENT,
    vl.corpus_over AS CORPUS,
    vl.interest_over AS INTEREST,
    (
     in_rent - vl.corpus_over - vl.interest_over
    ) AS PENALTY,
    '0' AS CORPUS_ADJUST,
    '0' AS PENALTY_ADJUST,
    '0' AS ROLL_BACK,
    O.COIN AS COIN ,
    ca.acc_number,
    ca.bank_name,
    ca.account,
    '光大银行',
    '安鹏国际融资租赁（深圳）有限公司',
    '35230188000138484'
  FROM
    LC_RENT_PLAN O,
   VI_LC_RENT_PLAN vl 
   LEFT JOIN customer_account ca ON ca.contract_id=vl.contract_id
  WHERE O.id = vl.id 					
    AND vl.contract_id =ln_contract_id
    AND vl.plan_date = ln_plan_date ) ; 
    -- in_type = 2为银联卡口手动刷新核销数据
ELSEIF in_type = 2 THEN 
INSERT INTO LC_RENT_INCOME (
  ID,
  QUOT_ID,
  CUST_ID,
  PROJECT_ID,
  PROJECT_PLAN_NUMBER,
  CONTRACT_ID,
  CONTRACT_PLAN_NUMBER,
  PAYMENT_NUMBER,
  EBANK_NUMBER,
  PLAN_ID,
  PLAN_LIST,
 INTEREST_ADJUST,
  SETTLE_METHOD,
  HIRE_LIST,
  HIRE_DATE,
  RENT,
  CORPUS,
  INTEREST,
  PENALTY,
  CORPUS_ADJUST,
  PENALTY_ADJUST,
  ROLL_BACK,
  COIN,
    hire_number,
  hire_bank,
  hire_account,
  own_bank,
  own_account,
  own_number 
)
(SELECT 
  REPLACE (UUID (), '-', '') AS id,
  O.QUOT_ID AS QUOT_ID,
  O.CUST_ID AS CUST_ID,
  O.PROJECT_ID AS PROJECT_ID,
  O.PROJECT_PLAN_NUMBER AS PROJECT_PLAN_NUMBER,
  O.CONTRACT_ID AS CONTRACT_ID,
  O.CONTRACT_PLAN_NUMBER AS CONTRACT_PLAN_NUMBER,
  O.PAYMENT_NUMBER AS PAYMENT_NUMBER,
  '' AS EBANK_NUMBER,
  O.id AS PLAN_ID,
  O.PLAN_LIST AS PLAN_LIST,
 '0' AS INTEREST_ADJUST,
  'settlemethod6' AS SETTLE_METHOD,
  in_hire_list AS HIRE_LIST,
   DATE_FORMAT (CURDATE(),'%Y/%m/%d') AS HIRE_DATE,
CASE WHEN   in_rent>=vl.corpus_over+vl.interest_over+vl.penalty_over THEN vl.rent_over
     WHEN   in_rent<vl.corpus_over+vl.interest_over+vl.penalty_over AND in_rent>=vl.interest_over+vl.penalty_over THEN in_rent-vl.penalty_over
     WHEN   in_rent<vl.interest_over+vl.penalty_over AND in_rent>vl.penalty_over THEN in_rent-vl.penalty_over
     ELSE '0'
     END RENT,
CASE WHEN   in_rent>=vl.corpus_over+vl.interest_over+vl.penalty_over THEN vl.corpus_over
     WHEN   in_rent<vl.corpus_over+vl.interest_over+vl.penalty_over AND in_rent>=vl.interest_over+vl.penalty_over THEN in_rent-(vl.penalty_over+vl.interest_over)
     WHEN   in_rent<vl.interest_over+vl.penalty_over AND in_rent>=vl.penalty_over THEN '0'
     ELSE '0'
     END CORPUS,
CASE WHEN   in_rent>=vl.corpus_over+vl.interest_over+vl.penalty_over THEN vl.interest_over
     WHEN   in_rent<vl.corpus_over+vl.interest_over+vl.penalty_over AND in_rent>=vl.interest_over+vl.penalty_over THEN vl.interest_over
     WHEN   in_rent<vl.interest_over+vl.penalty_over AND in_rent>=vl.penalty_over THEN in_rent-vl.penalty_over
     ELSE '0'
     END INTEREST,
  CASE WHEN   in_rent>=vl.corpus_over+vl.interest_over+vl.penalty_over THEN vl.penalty_over
     WHEN   in_rent<vl.corpus_over+vl.interest_over+vl.penalty_over AND in_rent>=vl.interest_over+vl.penalty_over THEN vl.penalty_over
     WHEN   in_rent<vl.interest_over+vl.penalty_over AND in_rent>=vl.penalty_over THEN vl.penalty_over
     ELSE in_rent
     END    AS PENALTY,
'0' AS CORPUS_ADJUST,
  '0' AS PENALTY_ADJUST,
  '0' AS ROLL_BACK,
  O.COIN AS COIN ,
  ca.acc_number,
    ca.bank_name,
    ca.account,
    '光大银行',
    '安鹏国际融资租赁（深圳）有限公司',
    '35230188000138484'
FROM
  LC_RENT_PLAN O,
  VI_LC_RENT_PLAN vl 
   LEFT JOIN customer_account ca ON ca.contract_id=vl.contract_id
WHERE O.id = vl.id 
  AND vl.contract_id = ln_contract_id
  AND vl.plan_date = ln_plan_date )   ;
 
 -- 如果in_type=3就是经销商保证金代偿，in_hire_list为核销罚息金额
 ELSEIF in_type = 3 THEN
 INSERT INTO LC_RENT_INCOME (
  ID,
  QUOT_ID,
  CUST_ID,
  PROJECT_ID,
  PROJECT_PLAN_NUMBER,
  CONTRACT_ID,
  CONTRACT_PLAN_NUMBER,
  PAYMENT_NUMBER,
  EBANK_NUMBER,
  PLAN_ID,
  PLAN_LIST,
 INTEREST_ADJUST,
  SETTLE_METHOD,
  HIRE_LIST,
  HIRE_DATE,
  RENT,
  CORPUS,
  INTEREST,
  PENALTY,
  CORPUS_ADJUST,
  PENALTY_ADJUST,
  ROLL_BACK,
  COIN,
  hire_number,
  hire_bank,
  hire_account,
  own_bank,
  own_account,
  own_number
) 
  (SELECT 
    REPLACE (UUID(), '-', '') AS id,
    O.QUOT_ID AS QUOT_ID,
    O.CUST_ID AS CUST_ID,
    O.PROJECT_ID AS PROJECT_ID,
    O.PROJECT_PLAN_NUMBER AS PROJECT_PLAN_NUMBER,
    O.CONTRACT_ID AS CONTRACT_ID,
    O.CONTRACT_PLAN_NUMBER AS CONTRACT_PLAN_NUMBER,
    O.PAYMENT_NUMBER AS PAYMENT_NUMBER,
    '' AS EBANK_NUMBER,
    O.id AS PLAN_ID,
    O.PLAN_LIST AS PLAN_LIST,
   '0' AS INTEREST_ADJUST,
    'settlemethod6' AS SETTLE_METHOD,
    '1' AS HIRE_LIST,
  DATE_FORMAT (CURDATE(),'%Y/%m/%d')  AS HIRE_DATE,
    vl.rent_over AS RENT,
    vl.corpus_over AS CORPUS,
    vl.interest_over AS INTEREST,
   in_hire_list AS PENALTY,
    '0' AS CORPUS_ADJUST,
    penalty_over-in_hire_list AS PENALTY_ADJUST,
    '0' AS ROLL_BACK,
    O.COIN AS COIN ,
    ca.acc_number,
    ca.bank_name,
    ca.account,
    '光大银行',
    '安鹏国际融资租赁（深圳）有限公司',
    '35230188000138484'
  FROM
    LC_RENT_PLAN O,
   VI_LC_RENT_PLAN vl 
   LEFT JOIN customer_account ca ON ca.contract_id=vl.contract_id
  WHERE O.id = vl.id 					
    AND vl.contract_id =ln_contract_id
    AND vl.plan_date = ln_plan_date ) ; 
  ELSE 
        SELECT 1; 
END IF;
END;

