DELIMITER $$

create ALGORITHM=UNDEFINED DEFINER=`apzl`@`%` SQL SECURITY DEFINER VIEW `vi_li_invoice_tj_base` AS 
SELECT `t`.`id` AS `contract_id`,`t`.`contract_no` AS `contract_no`,`t`.`invoice_money` AS `invoice_money`,`t`.`tax_no` AS `tax_no`,`t`.`plan_list` AS `plan_list` FROM (SELECT `lci`.`ID` AS `id`,`lci`.`CONTRACT_NO` AS `contract_no`,`lri`.`INTEREST` AS `invoice_money`,'interest' AS `tax_no`,`lri`.`plan_list` AS `plan_list` FROM ((`apzl`.`lc_rent_plan` `lrp` LEFT JOIN `apzl`.`lb_contract_info` `lci` ON((`lrp`.`CONTRACT_ID` = `lci`.`ID`))) LEFT JOIN (SELECT `apzl`.`lc_rent_income`.`PLAN_ID` AS `plan_id`,SUM(IFNULL(`apzl`.`lc_rent_income`.`INTEREST`,0)) AS `INTEREST`,SUM(`apzl`.`lc_rent_income`.`RENT`) AS `rent`,`apzl`.`lc_rent_income`.`PLAN_LIST` AS `plan_list` FROM `apzl`.`lc_rent_income` WHERE (`apzl`.`lc_rent_income`.`INTEREST` <> 0.00) GROUP BY `apzl`.`lc_rent_income`.`PLAN_ID`) `lri` ON((`lrp`.`ID` = `lri`.`plan_id`))) WHERE ((`lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6') AND (`lci`.`CONTRACT_STATUS` IN ('31','100','105')) AND (`lri`.`INTEREST` <> 0.00) AND (`lri`.`rent` >= `lrp`.`RENT`)) UNION ALL SELECT `lci`.`ID` AS `id`,`lci`.`CONTRACT_NO` AS `contract_no`,`lri`.`penalty` AS `invoice_money`,'penalty' AS `tax_no`,`lri`.`plan_list` AS `plan_list` FROM ((`apzl`.`lc_rent_plan` `lrp` LEFT JOIN `apzl`.`lb_contract_info` `lci` ON((`lrp`.`CONTRACT_ID` = `lci`.`ID`))) LEFT JOIN (SELECT `apzl`.`lc_rent_income`.`PLAN_ID` AS `plan_id`,SUM(IFNULL(`apzl`.`lc_rent_income`.`PENALTY`,0)) AS `penalty`,SUM(`apzl`.`lc_rent_income`.`RENT`) AS `rent`,`apzl`.`lc_rent_income`.`PLAN_LIST` AS `plan_list` FROM `apzl`.`lc_rent_income` WHERE (`apzl`.`lc_rent_income`.`PENALTY` <> 0.00) GROUP BY `apzl`.`lc_rent_income`.`PLAN_ID`) `lri` ON((`lrp`.`ID` = `lri`.`plan_id`))) WHERE ((`lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6') AND (`lci`.`CONTRACT_STATUS` IN ('31','100','105')) AND (`lri`.`penalty` <> 0.00) AND (`lri`.`rent` >= `lrp`.`RENT`)) UNION ALL SELECT `lci`.`ID` AS `id`,`lci`.`CONTRACT_NO` AS `contract_no`,`lri`.`rent` AS `invoice_money`,'rent' AS `tax_no`,`lri`.`plan_list` AS `plan_list` FROM ((`apzl`.`lc_rent_plan` `lrp` LEFT JOIN `apzl`.`lb_contract_info` `lci` ON((`lrp`.`CONTRACT_ID` = `lci`.`ID`))) LEFT JOIN (SELECT `apzl`.`lc_rent_income`.`PLAN_ID` AS `plan_id`,SUM(`apzl`.`lc_rent_income`.`RENT`) AS `rent`,`apzl`.`lc_rent_income`.`PLAN_LIST` AS `plan_list` FROM `apzl`.`lc_rent_income` WHERE (`apzl`.`lc_rent_income`.`RENT` <> 0.00) GROUP BY `apzl`.`lc_rent_income`.`PLAN_ID`) `lri` ON((`lrp`.`ID` = `lri`.`plan_id`))) WHERE ((`lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6') AND (`lci`.`CONTRACT_STATUS` IN ('31','100','105')) AND (`lri`.`rent` <> 0.00) AND (`lri`.`rent` >= `lrp`.`RENT`)) UNION ALL SELECT `lci`.`ID` AS `id`,`lci`.`CONTRACT_NO` AS `contract_no`,`lfi`.`FACT_MONEY` AS `invoice_money`,`lfi`.`FEE_TYPE` AS `tax_no`,CONVERT('1' USING gbk) AS `plan_list` FROM (`apzl`.`lc_fund_income` `lfi` LEFT JOIN `apzl`.`lb_contract_info` `lci` ON((`lfi`.`CONTRACT_ID` = `lci`.`ID`))) WHERE ((`lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6') AND (`lci`.`CONTRACT_STATUS` IN ('31','100','105')) AND (`lfi`.`FEE_TYPE` IN ('feetype30','feetype1','feetype4','feetype24')) AND (`lfi`.`PAY_TYPE` = 'pay_type_in') AND (((`lfi`.`SETTLE_METHOD` IN ('settlemethod11','settlemethod6')) AND (`lfi`.`PAY_STATUS` = 'apply_pass')) OR (`lfi`.`PAY_STATUS` = 'have_paid'))) GROUP BY `lfi`.`PLAN_ID`) `t`$$

DELIMITER ;