create view vi_normalend_contract as SELECT O.ID                  AS ID,
       O.PROJECT_ID          AS PROJECT_ID,
       O.CONTRACT_NUMBER     AS CONTRACT_NUMBER,
       O.INPUTUSERID         AS INPUTUSERID,
       O.UPDATEUSERID        AS UPDATEUSERID,
       O.INPUTTIME           AS INPUTTIME,
       O.UPDATETIME          AS UPDATETIME,
       O.FLOWUNID            AS FLOWUNID,
       CU.CUSTOMERID         AS CUSTOMERID,
       CU.CUSTOMERNAME       AS CUSTOMERNAME,
       CA.PROVINCE           AS PROVINCE,
       CA.DRESSDETAIL        AS DRESSDETAIL,
       CT.TELEPHONE          AS TELEPHONE,
       DI.DISTRIBUTOR_NAME   AS DISTRIBUTOR_NAME,
       O.CONTRACT_STATUS     AS CONTRACT_STATUS,
       CON.CLEAN_LEASE_MONEY AS CLEAN_LEASE_MONEY,
       CON.NOMINAL_PRICE     AS NOMINAL_PRICE,
       CON.START_DATE        AS START_DATE,
       CON.RENT_VALUE        AS RENT_VALUE,
       CU.CUSTOMERTYPE       AS CUSTOMERTYPE,
       O.PRODUCT_ID          AS PRODUCT_ID,
       CON.EACH_RENT         AS EACH_RENT
FROM (((((((((APZL.LB_CONTRACT_INFO O LEFT JOIN APZL.LB_PROJECT_INFO LPI ON ((LPI.ID = convert(O.PROJECT_ID USING UTF8)))) LEFT JOIN APZL.LB_UNION_LESSEE UL ON (((O.ID = UL.CONTRACT_ID) AND (UL.IS_MAIN = 'Y')))) LEFT JOIN APZL.CUSTOMER_INFO CU ON ((UL.CUSTOMER_ID = CU.CUSTOMERID))) LEFT JOIN APZL.CUSTOMER_ADDRESS CA ON ((convert(CU.CUSTOMERID USING UTF8) = CA.CUSTOMERID))) LEFT JOIN APZL.CUSTOMER_TEL CT ON ((convert(CU.CUSTOMERID USING UTF8) = CT.CUSTOMERID))) LEFT JOIN APZL.DISTRIBUTOR_RECEIVABLES DRE ON ((O.ID = DRE.CONTRACT_ID))) LEFT JOIN APZL.DISTRIBUTOR_INFO DI ON ((DRE.DISTRIBUTOR_ID = DI.ID))) LEFT JOIN APZL.LC_CONTRACT_CONDITION CON ON ((O.ID = CON.CONTRACT_ID)))
LEFT JOIN APZL.VI_RENT_PLAN_SUM VRPS ON ((VRPS.CONTRACT_ID = O.ID)))
WHERE exists(SELECT 1 FROM APZL.BUSINESS_STATUS BS WHERE ((O.CONTRACT_NUMBER = BS.CONTRACT_NUMBER) AND (BS.BUSINESS_STATUS IN ('51', '52'))))
UNION
SELECT O.ID                  AS ID,
       O.PROJECT_ID          AS PROJECT_ID,
       O.CONTRACT_NUMBER     AS CONTRACT_NUMBER,
       O.INPUTUSERID         AS INPUTUSERID,
       O.UPDATEUSERID        AS UPDATEUSERID,
       O.INPUTTIME           AS INPUTTIME,
       O.UPDATETIME          AS UPDATETIME,
       O.FLOWUNID            AS FLOWUNID,
       CU.CUSTOMERID         AS CUSTOMERID,
       CU.CUSTOMERNAME       AS CUSTOMERNAME,
       CA.PROVINCE           AS PROVINCE,
       CA.DRESSDETAIL        AS DRESSDETAIL,
       CT.TELEPHONE          AS TELEPHONE,
       DI.DISTRIBUTOR_NAME   AS DISTRIBUTOR_NAME,
       O.CONTRACT_STATUS     AS CONTRACT_STATUS,
       CON.CLEAN_LEASE_MONEY AS CLEAN_LEASE_MONEY,
       CON.NOMINAL_PRICE     AS NOMINAL_PRICE,
       CON.START_DATE        AS START_DATE,
       CON.RENT_VALUE        AS RENT_VALUE,
       CU.CUSTOMERTYPE       AS CUSTOMERTYPE,
       O.PRODUCT_ID          AS PRODUCT_ID,
       CON.EACH_RENT         AS EACH_RENT
FROM (((((((((APZL.LB_CONTRACT_INFO O LEFT JOIN APZL.LB_PROJECT_INFO LPI ON ((LPI.ID = convert(O.PROJECT_ID USING UTF8)))) LEFT JOIN APZL.LB_UNION_LESSEE UL ON (((O.ID = UL.CONTRACT_ID) AND (UL.IS_MAIN = 'Y')))) LEFT JOIN APZL.CUSTOMER_INFO CU ON ((UL.CUSTOMER_ID = CU.CUSTOMERID))) LEFT JOIN APZL.CUSTOMER_ADDRESS CA ON ((convert(CU.CUSTOMERID USING UTF8) = CA.CUSTOMERID))) LEFT JOIN APZL.CUSTOMER_TEL CT ON ((convert(CU.CUSTOMERID USING UTF8) = CT.CUSTOMERID))) LEFT JOIN APZL.DISTRIBUTOR_RECEIVABLES DRE ON ((O.ID = DRE.CONTRACT_ID))) LEFT JOIN APZL.DISTRIBUTOR_INFO DI ON ((DRE.DISTRIBUTOR_ID = DI.ID))) LEFT JOIN APZL.LC_CONTRACT_CONDITION CON ON ((O.ID = CON.CONTRACT_ID)))
LEFT JOIN APZL.VI_RENT_PLAN_SUM VRPS ON ((VRPS.CONTRACT_ID = O.ID)))
WHERE ((O.CONTRACT_STATUS BETWEEN 31 AND 99) AND (VRPS.RENT_OVER = 0) AND (VRPS.PENALTY_OVER = 0));

