DELIMITER $$

CREATE VIEW `vi_li_invoice_tj_gps` AS 
  (SELECT 
    `lci`.`ID` AS `contract_id`,
    `lci`.`CONTRACT_NO` AS `contract_no`,
    '1' AS plan_list,
    `lfi`.`FACT_MONEY` AS `invoice_money`,
    (SELECT 
      `code_library`.`itemname` 
    FROM
      `code_library` 
    WHERE (
        (
          `code_library`.`codeno` = 'tax_type'
        ) 
        AND (`code_library`.`itemno` = 'feetype24')
      )) AS `tax_type`,
      'feetype24' AS tax_no,
    `cac`.`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 `customer_name`,
    `ti`.`gps_install_fee` AS `tax_ratio`,
    `liti`.`status` AS `invoice_status`,
    `liti`.`FPDM` AS `FPDM`,
    `liti`.`FPHM` AS `FPHM`,
    liti.hztzdh AS hztzdh,
    liti.mxxh AS mxxh
  FROM
    (
      (
        (
          (
            (
              `lc_fund_income` `lfi` 
              LEFT JOIN `lb_contract_info` `lci` 
                ON ((`lfi`.`CONTRACT_ID` = `lci`.`ID`))
            ) 
            LEFT JOIN (SELECT li.id,li.contract_no,li.status,li.FPDM,li.FPHM,li.hztzdh,li.mxxh FROM li_invoice_tj_info li WHERE li.tax_type='feetype24' AND li.status='Y') liti ON liti.contract_no = lci.contract_no
            LEFT JOIN `lb_union_lessee` `lul` 
              ON ((`lul`.`CONTRACT_ID` = `lci`.`ID`))
          ) 
          LEFT JOIN `customer_info` `ci` 
            ON (
              (
                `ci`.`customerid` = `lul`.`CUSTOMER_ID`
              )
            )
        ) 
        LEFT JOIN `customer_account` `cac` 
          ON ((`cac`.`CONTRACT_ID` = `lci`.`ID`))
      ) 
      LEFT JOIN `tax_info` `ti` 
        ON (
          (
            (
              `ti`.`LEASE_FORM` = `lci`.`LEAS_FORM`
            ) 
            AND (
              `ti`.`ID` IN (
                '5ceefeccd3af4db8a98d41c203c9dbe9',
                '7672ff2fa48046ebbaf905c02e993221'
              )
            )
          )
        )
    ) 
  WHERE (
      (
        `lci`.`SUBJECTID` = 'd989246c11c111eaaa0000163e0e11e6'
      ) 
      AND (
        `lci`.`CONTRACT_STATUS` IN ('31', '100', '105')
      )  
      AND (`lfi`.`FEE_TYPE` = 'feetype24') 
      AND (`lfi`.`PAY_TYPE` = 'pay_type_in') 
      AND (
        (
          (
            `lfi`.`SETTLE_METHOD` = 'settlemethod11'
          ) 
          AND (
            `lfi`.`PAY_STATUS` = 'apply_pass'
          )
        ) 
        OR (`lfi`.`PAY_STATUS` = 'have_paid')
      )
    )) $$

DELIMITER ;

