DELIMITER $$
create VIEW `vi_li_invoice_tj_leaseback` AS 
  (SELECT 
    `t`.`id` AS `contract_id`,
    `t`.`contract_no` AS `contract_no`,
    `t`.`invoice_money` AS `invoice_money`,
    `t`.`tax_type` AS `tax_type`,
    `t`.`cert_id` AS `cert_id`,
    `t`.`account` AS `account`,
    `t`.`acc_number` AS `acc_number`,
    `t`.`bank_name` AS `bank_name`,
    `t`.`telephone` AS `telephone`,
    `t`.`customer_type` AS `customer_type`,
    `t`.`customername` AS `customer_name`,
    `t`.`tax_ratio` AS `tax_ratio`,
    `t`.`full_code` AS `full_code`,
    `t`.`FPDM` AS `fpdm`,
    `t`.`FPHM` AS `fphm`,
    `t`.`plan_list` AS `plan_list` 
  FROM
    (SELECT 
      `lci`.`ID` AS `id`,
      `lci`.`CONTRACT_NO` AS `contract_no`,
      `lri`.`INTEREST` AS `invoice_money`,
      (SELECT 
        `apzl`.`code_library`.`itemname` 
      FROM
        `apzl`.`code_library` 
      WHERE (
          (
            `apzl`.`code_library`.`codeno` = 'tax_type'
          ) 
          AND (
            `apzl`.`code_library`.`itemno` = 'interest'
          )
        )) AS `tax_type`,
      `ci`.`certid` AS `cert_id`,
      `cac`.`account` AS `account`,
      `cac`.`acc_number` AS `acc_number`,
      `cac`.`bank_name` AS `bank_name`,
      `cac`.`MOBILE` AS `telephone`,
      `ci`.`customertype` AS `customer_type`,
      `ci`.`customername` AS `customername`,
      `ti`.`INTEREST_RATE` AS `tax_ratio`,
      (SELECT 
        `tci`.`full_code` 
      FROM
        `apzl`.`tax_code_info` `tci` 
      WHERE (`tci`.`tax_type` = 'interest')) AS `full_code`,
      `lri`.`interest_fpdm` AS `FPDM`,
      `lri`.`interest_fphm` AS `FPHM`,
      `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`.`INTEREST_FPDM` AS `interest_fpdm`,
                    `apzl`.`lc_rent_income`.`INTEREST_FPHM` AS `interest_fphm`,
                    `apzl`.`lc_rent_income`.`PLAN_LIST` AS `plan_list` 
                  FROM
                    `apzl`.`lc_rent_income` 
                  WHERE (
                      (
                        `apzl`.`lc_rent_income`.`INTEREST` <> 0.00
                      ) 
                      AND (
                        ISNULL(
                          `apzl`.`lc_rent_income`.`INTEREST_FPDM`
                        ) 
                        OR (
                          `apzl`.`lc_rent_income`.`INTEREST_FPDM` = ''
                        )
                      )
                    ) 
                  GROUP BY `apzl`.`lc_rent_income`.`PLAN_ID`,
                    `apzl`.`lc_rent_income`.`INTEREST_FPDM`,
                    `apzl`.`lc_rent_income`.`INTEREST_FPHM`) `lri` 
                  ON ((`lrp`.`ID` = `lri`.`plan_id`))
              ) 
              LEFT JOIN `apzl`.`lb_union_lessee` `lul` 
                ON ((`lul`.`CONTRACT_ID` = `lci`.`ID`))
            ) 
            LEFT JOIN `apzl`.`customer_info` `ci` 
              ON (
                (
                  `ci`.`customerid` = `lul`.`CUSTOMER_ID`
                )
              )
          ) 
          LEFT JOIN `apzl`.`customer_account` `cac` 
            ON ((`cac`.`CONTRACT_ID` = `lci`.`ID`))
        ) 
        LEFT JOIN `apzl`.`tax_info` `ti` 
          ON (
            (
              (`ti`.`LEASE_FORM` = '02') 
              AND (
                `ti`.`ID` = '5ceefeccd3af4db8a98d41c203c9dbe9'
              )
            )
          )
      ) 
    WHERE (
        (
          `lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6'
        ) 
        AND (`lci`.`LEAS_FORM` = '02') 
        AND (
          `lci`.`CONTRACT_STATUS` IN ('31', '100', '105')
        ) 
        AND (`lri`.`INTEREST` <> 0.00) 
        AND (`lri`.`rent` >= `lrp`.`RENT`) 
        AND (`lul`.`IS_MAIN` = 'Y')
      ) 
      UNION
      ALL 
      SELECT 
        `lci`.`ID` AS `id`,
        `lci`.`CONTRACT_NO` AS `contract_no`,
        `lri`.`penalty` AS `invoice_money`,
        (SELECT 
          `apzl`.`code_library`.`itemname` 
        FROM
          `apzl`.`code_library` 
        WHERE (
            (
              `apzl`.`code_library`.`codeno` = 'tax_type'
            ) 
            AND (
              `apzl`.`code_library`.`itemno` = 'penalty'
            )
          )) AS `tax_type`,
        `ci`.`certid` AS `cert_id`,
        `cac`.`account` AS `account`,
        `cac`.`acc_number` AS `acc_number`,
        `cac`.`bank_name` AS `bank_name`,
        `cac`.`MOBILE` AS `telephone`,
        `ci`.`customertype` AS `customer_type`,
        `ci`.`customername` AS `customername`,
        `ti`.`break_money` AS `tax_ratio`,
        (SELECT 
          `tci`.`full_code` 
        FROM
          `apzl`.`tax_code_info` `tci` 
        WHERE (`tci`.`tax_type` = 'penalty')) AS `full_code`,
        `lri`.`penalty_fpdm` AS `penalty_fpdm`,
        `lri`.`penalty_fphm` AS `penalty_fphm`,
        `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`.`PENALTY_FPDM` AS `penalty_fpdm`,
                      `apzl`.`lc_rent_income`.`PENALTY_FPHM` AS `penalty_fphm`,
                      `apzl`.`lc_rent_income`.`PLAN_LIST` AS `plan_list` 
                    FROM
                      `apzl`.`lc_rent_income` 
                    WHERE (
                        (
                          `apzl`.`lc_rent_income`.`PENALTY` <> 0.00
                        ) 
                        AND (
                          ISNULL(
                            `apzl`.`lc_rent_income`.`PENALTY_FPDM`
                          ) 
                          OR (
                            `apzl`.`lc_rent_income`.`PENALTY_FPDM` = ''
                          )
                        )
                      ) 
                    GROUP BY `apzl`.`lc_rent_income`.`PLAN_ID`,
                      `apzl`.`lc_rent_income`.`PENALTY_FPDM`,
                      `apzl`.`lc_rent_income`.`PENALTY_FPHM`) `lri` 
                    ON ((`lrp`.`ID` = `lri`.`plan_id`))
                ) 
                LEFT JOIN `apzl`.`lb_union_lessee` `lul` 
                  ON ((`lul`.`CONTRACT_ID` = `lci`.`ID`))
              ) 
              LEFT JOIN `apzl`.`customer_info` `ci` 
                ON (
                  (
                    `ci`.`customerid` = `lul`.`CUSTOMER_ID`
                  )
                )
            ) 
            LEFT JOIN `apzl`.`customer_account` `cac` 
              ON ((`cac`.`CONTRACT_ID` = `lci`.`ID`))
          ) 
          LEFT JOIN `apzl`.`tax_info` `ti` 
            ON (
              (
                (`ti`.`LEASE_FORM` = '02') 
                AND (
                  `ti`.`ID` = '5ceefeccd3af4db8a98d41c203c9dbe9'
                )
              )
            )
        ) 
      WHERE (
          (
            `lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6'
          ) 
          AND (`lci`.`LEAS_FORM` = '02') 
          AND (
            `lci`.`CONTRACT_STATUS` IN ('31', '100', '105')
          ) 
          AND (`lri`.`penalty` <> 0.00) 
          AND (`lri`.`rent` >= `lrp`.`RENT`) 
          AND (`lul`.`IS_MAIN` = 'Y')
        ) 
        UNION
        ALL 
        SELECT 
          `lci`.`ID` AS `id`,
          `lci`.`CONTRACT_NO` AS `contract_no`,
          `lfi`.`FACT_MONEY` AS `invoice_money`,
          (SELECT 
            `apzl`.`code_library`.`itemname` 
          FROM
            `apzl`.`code_library` 
          WHERE (
              (
                `apzl`.`code_library`.`codeno` = 'tax_type'
              ) 
              AND (
                `apzl`.`code_library`.`itemno` = 'handle_price'
              )
            )) AS `tax_type`,
          `ci`.`certid` AS `cert_id`,
          `cac`.`account` AS `account`,
          `cac`.`acc_number` AS `acc_number`,
          `cac`.`bank_name` AS `bank_name`,
          `cac`.`MOBILE` AS `telephone`,
          `ci`.`customertype` AS `customer_type`,
          `ci`.`customername` AS `customername`,
          `ti`.`HAND_RATE` AS `tax_ratio`,
          (SELECT 
            `tci`.`full_code` 
          FROM
            `apzl`.`tax_code_info` `tci` 
          WHERE (
              `tci`.`tax_type` = 'handle_price'
            )) AS `full_code`,
          `lfi`.`FPDM` AS `FPDM`,
          `lfi`.`FPHM` AS `FPHM`,
          '1' AS `plan_list` 
        FROM
          (
            (
              (
                (
                  (
                    `apzl`.`lc_fund_income` `lfi` 
                    LEFT JOIN `apzl`.`lb_contract_info` `lci` 
                      ON ((`lfi`.`CONTRACT_ID` = `lci`.`ID`))
                  ) 
                  LEFT JOIN `apzl`.`lb_union_lessee` `lul` 
                    ON ((`lul`.`CONTRACT_ID` = `lci`.`ID`))
                ) 
                LEFT JOIN `apzl`.`customer_info` `ci` 
                  ON (
                    (
                      `ci`.`customerid` = `lul`.`CUSTOMER_ID`
                    )
                  )
              ) 
              LEFT JOIN `apzl`.`customer_account` `cac` 
                ON ((`cac`.`CONTRACT_ID` = `lci`.`ID`))
            ) 
            LEFT JOIN `apzl`.`tax_info` `ti` 
              ON (
                (
                  (`ti`.`LEASE_FORM` = '02') 
                  AND (
                    `ti`.`ID` = '5ceefeccd3af4db8a98d41c203c9dbe9'
                  )
                )
              )
          ) 
        WHERE (
            (
              ISNULL(`lfi`.`FPDM`) 
              OR (`lfi`.`FPDM` = '')
            ) 
            AND (
              `lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6'
            ) 
            AND (`lci`.`LEAS_FORM` = '02') 
            AND (`lci`.`CONTRACT_STATUS` >= '31') 
            AND (`lfi`.`FEE_TYPE` = 'feetype1') 
            AND (`lfi`.`PAY_TYPE` = 'pay_type_in') 
            AND (
              (
                (
                  `lfi`.`SETTLE_METHOD` = 'settlemethod11'
                ) 
                AND (
                  `lfi`.`PAY_STATUS` = 'apply_pass'
                )
              ) 
              OR (`lfi`.`PAY_STATUS` = 'have_paid')
            ) 
            AND (`lul`.`IS_MAIN` = 'Y')
          ) 
        GROUP BY `lfi`.`PLAN_ID`,
          `lfi`.`FPDM`,
          `lfi`.`FPHM` 
        UNION
        ALL 
        SELECT 
          `lci`.`ID` AS `id`,
          `lci`.`CONTRACT_NO` AS `contract_no`,
          `lfi`.`FACT_MONEY` AS `invoice_money`,
          (SELECT 
            `apzl`.`code_library`.`itemname` 
          FROM
            `apzl`.`code_library` 
          WHERE (
              (
                `apzl`.`code_library`.`codeno` = 'tax_type'
              ) 
              AND (
                `apzl`.`code_library`.`itemno` = 'nominal_price'
              )
            )) AS `tax_type`,
          `ci`.`certid` AS `cert_id`,
          `cac`.`account` AS `account`,
          `cac`.`acc_number` AS `acc_number`,
          `cac`.`bank_name` AS `bank_name`,
          `cac`.`MOBILE` AS `telephone`,
          `ci`.`customertype` AS `customer_type`,
          `ci`.`customername` AS `customername`,
          `ti`.`NOMINAL_RATE` AS `tax_ratio`,
          (SELECT 
            `tci`.`full_code` 
          FROM
            `apzl`.`tax_code_info` `tci` 
          WHERE (
              `tci`.`tax_type` = 'nominal_price'
            )) AS `full_code`,
          `lfi`.`FPDM` AS `FPDM`,
          `lfi`.`FPHM` AS `FPHM`,
          '1' AS `plan_list` 
        FROM
          (
            (
              (
                (
                  (
                    `apzl`.`lc_fund_income` `lfi` 
                    LEFT JOIN `apzl`.`lb_contract_info` `lci` 
                      ON ((`lfi`.`CONTRACT_ID` = `lci`.`ID`))
                  ) 
                  LEFT JOIN `apzl`.`lb_union_lessee` `lul` 
                    ON ((`lul`.`CONTRACT_ID` = `lci`.`ID`))
                ) 
                LEFT JOIN `apzl`.`customer_info` `ci` 
                  ON (
                    (
                      `ci`.`customerid` = `lul`.`CUSTOMER_ID`
                    )
                  )
              ) 
              LEFT JOIN `apzl`.`customer_account` `cac` 
                ON ((`cac`.`CONTRACT_ID` = `lci`.`ID`))
            ) 
            LEFT JOIN `apzl`.`tax_info` `ti` 
              ON (
                (
                  (`ti`.`LEASE_FORM` = '02') 
                  AND (
                    `ti`.`ID` = '5ceefeccd3af4db8a98d41c203c9dbe9'
                  )
                )
              )
          ) 
        WHERE (
            ISNULL(`lfi`.`FPDM`) 
            OR (
              (`lfi`.`FPDM` = '') 
              AND (
                `lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6'
              ) 
              AND (`lci`.`LEAS_FORM` = '02') 
              AND (
                `lci`.`CONTRACT_STATUS` IN ('31', '100', '105')
              ) 
              AND (`lfi`.`FEE_TYPE` = 'feetype4') 
              AND (`lfi`.`PAY_TYPE` = 'pay_type_in') 
              AND (
                (
                  (
                    `lfi`.`SETTLE_METHOD` = 'settlemethod11'
                  ) 
                  AND (
                    `lfi`.`PAY_STATUS` = 'apply_pass'
                  )
                ) 
                OR (`lfi`.`PAY_STATUS` = 'have_paid')
              )
            )
          ) 
        GROUP BY `lfi`.`PLAN_ID`,
          `lfi`.`FPDM`,
          `lfi`.`FPHM` 
        UNION
        ALL 
        SELECT 
          `lci`.`ID` AS `id`,
          `lci`.`CONTRACT_NO` AS `contract_no`,
          `lfi`.`FACT_MONEY` AS `invoice_money`,
          (SELECT 
            `apzl`.`code_library`.`itemname` 
          FROM
            `apzl`.`code_library` 
          WHERE (
              (
                `apzl`.`code_library`.`codeno` = 'tax_type'
              ) 
              AND (
                `apzl`.`code_library`.`itemno` = 'break_price'
              )
            )) AS `tax_type`,
          `ci`.`certid` AS `cert_id`,
          `cac`.`account` AS `account`,
          `cac`.`acc_number` AS `acc_number`,
          `cac`.`bank_name` AS `bank_name`,
          `cac`.`MOBILE` AS `telephone`,
          `ci`.`customertype` AS `customer_type`,
          `ci`.`customername` AS `customername`,
          `ti`.`penalty` AS `tax_ratio`,
          (SELECT 
            `tci`.`full_code` 
          FROM
            `apzl`.`tax_code_info` `tci` 
          WHERE (`tci`.`tax_type` = 'break_price')) AS `full_code`,
          `lfi`.`FPDM` AS `FPDM`,
          `lfi`.`FPHM` AS `FPHM`,
          '1' AS `plan_list` 
        FROM
          (
            (
              (
                (
                  (
                    `apzl`.`lc_fund_income` `lfi` 
                    LEFT JOIN `apzl`.`lb_contract_info` `lci` 
                      ON ((`lfi`.`CONTRACT_ID` = `lci`.`ID`))
                  ) 
                  LEFT JOIN `apzl`.`lb_union_lessee` `lul` 
                    ON ((`lul`.`CONTRACT_ID` = `lci`.`ID`))
                ) 
                LEFT JOIN `apzl`.`customer_info` `ci` 
                  ON (
                    (
                      `ci`.`customerid` = `lul`.`CUSTOMER_ID`
                    )
                  )
              ) 
              LEFT JOIN `apzl`.`customer_account` `cac` 
                ON ((`cac`.`CONTRACT_ID` = `lci`.`ID`))
            ) 
            LEFT JOIN `apzl`.`tax_info` `ti` 
              ON (
                (
                  (`ti`.`LEASE_FORM` = '02') 
                  AND (
                    `ti`.`ID` = '5ceefeccd3af4db8a98d41c203c9dbe9'
                  )
                )
              )
          ) 
        WHERE (
            ISNULL(`lfi`.`FPDM`) 
            OR (
              (`lfi`.`FPDM` = '') 
              AND (
                `lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6'
              ) 
              AND (`lci`.`LEAS_FORM` = '02') 
              AND (
                `lci`.`CONTRACT_STATUS` IN ('31', '100', '105')
              ) 
              AND (`lfi`.`FEE_TYPE` = 'feetype30') 
              AND (`lfi`.`PAY_TYPE` = 'pay_type_in') 
              AND (
                (
                  (
                    `lfi`.`SETTLE_METHOD` = 'settlemethod11'
                  ) 
                  AND (
                    `lfi`.`PAY_STATUS` = 'apply_pass'
                  )
                ) 
                OR (`lfi`.`PAY_STATUS` = 'have_paid')
              )
            )
          ) 
        GROUP BY `lfi`.`PLAN_ID`,
          `lfi`.`FPDM`,
          `lfi`.`FPHM`) `t`) $$

DELIMITER ;

