create view vi_contract_penalty as SELECT L.PAYMENT_NUMBER                                                                                AS PAYMENT_NUMBER,
       L.PLAN_LIST                                                                                     AS PLAN_LIST,
       L.PLAN_DATE                                                                                     AS PLAN_DATE,
       round(sum((CASE
                      WHEN ((str_to_date(L.PLAN_DATE, '%Y/%m/%d') + C.ATTR_VALUE) <= if((LL.HIRE_DATE > curdate()), curdate(), LL.HIRE_DATE)) THEN (
                              ((LCC.PENA_RATE / 10000) *
                               (to_days(if((LL.HIRE_DATE > curdate()), curdate(), LL.HIRE_DATE)) - to_days(str_to_date(L.PLAN_DATE, '%Y/%m/%d')))) *
                              ifnull(LL.RENT, 0))
                      ELSE 0 END)), 2)                                                                 AS PLAN_PENALTY,
       round(sum((ifnull(LL.PENALTY, 0) + ifnull(LL.PENALTY_ADJUST, 0))), 2)                           AS INCOME_PENALTY,
       round((sum((CASE
                       WHEN ((str_to_date(L.PLAN_DATE, '%Y/%m/%d') + C.ATTR_VALUE) <= if((LL.HIRE_DATE > curdate()), curdate(), LL.HIRE_DATE)) THEN (
                               ((LCC.PENA_RATE / 10000) *
                                (to_days(if((LL.HIRE_DATE > curdate()), curdate(), LL.HIRE_DATE)) - to_days(str_to_date(L.PLAN_DATE, '%Y/%m/%d')))) *
                               ifnull(LL.RENT, 0))
                       ELSE 0 END)) - sum((ifnull(LL.PENALTY, 0) + ifnull(LL.PENALTY_ADJUST, 0)))), 2) AS PENALTY
FROM ((((APZL.LC_CALC_CONDITION LCC LEFT JOIN APZL.LC_CALC_CONDITION_STATUS LCCS ON ((LCC.PAYMENT_NUMBER = LCCS.PAYMENT_NUMBER))) LEFT JOIN APZL.CONFIGURATE C ON (((1 = 1) AND (C.ATTR_ID = '免罚息天数配置') AND (C.FIELD1 = '汽车')))) LEFT JOIN APZL.LC_RENT_PLAN L ON ((LCCS.PAYMENT_NUMBER = L.PAYMENT_NUMBER)))
LEFT JOIN (
    SELECT LRI.PAYMENT_NUMBER                     AS PAYMENT_NUMBER,
           LRI.PLAN_LIST                          AS PLAN_LIST,
           LRI.RENT                               AS RENT,
           LRI.PENALTY                            AS PENALTY,
           LRI.PENALTY_ADJUST                     AS PENALTY_ADJUST,
           str_to_date(LRI.HIRE_DATE, '%Y/%m/%d') AS HIRE_DATE
    FROM APZL.LC_RENT_INCOME LRI
    WHERE (LRI.ROLL_BACK = '0')
    UNION ALL
    SELECT LRIT.PAYMENT_NUMBER                     AS PAYMENT_NUMBER,
           LRIT.PLAN_LIST                          AS PLAN_LIST,
           LRIT.RENT                               AS RENT,
           LRIT.PENALTY                            AS PENALTY,
           LRIT.PENALTY_ADJUST                     AS PENALTY_ADJUST,
           str_to_date(LRIT.HIRE_DATE, '%Y/%m/%d') AS HIRE_DATE
    FROM APZL.LC_RENT_INCOME_TEMP LRIT
    WHERE ((LRIT.ROLL_BACK = '0') AND (LRIT.IS_FLOWING = '0'))
    UNION ALL
    SELECT LRP.PAYMENT_NUMBER                                        AS PAYMENT_NUMBER,
           LRP.PLAN_LIST                                             AS PLAN_LIST,
           ((LRP.RENT - ifnull(LR.SRENT, 0)) - ifnull(LRT.SRENT, 0)) AS RENT,
           0                                                         AS `0`,
           0                                                         AS `0`,
           curdate()                                                 AS HIRE_DATE
    FROM ((APZL.LC_RENT_PLAN LRP LEFT JOIN (
        SELECT APZL.LC_RENT_INCOME.PAYMENT_NUMBER AS PAYMENT_NUMBER,
               APZL.LC_RENT_INCOME.PLAN_LIST      AS PLAN_LIST,
               sum(APZL.LC_RENT_INCOME.RENT)      AS SRENT
        FROM APZL.LC_RENT_INCOME
        WHERE (APZL.LC_RENT_INCOME.ROLL_BACK = '0')
        GROUP BY APZL.LC_RENT_INCOME.PAYMENT_NUMBER, APZL.LC_RENT_INCOME.PLAN_LIST
    ) LR ON (((LR.PAYMENT_NUMBER = LRP.PAYMENT_NUMBER) AND (LR.PLAN_LIST = LRP.PLAN_LIST))))
    LEFT JOIN (
        SELECT APZL.LC_RENT_INCOME_TEMP.PAYMENT_NUMBER AS PAYMENT_NUMBER,
               APZL.LC_RENT_INCOME_TEMP.PLAN_LIST      AS PLAN_LIST,
               sum(APZL.LC_RENT_INCOME_TEMP.RENT)      AS SRENT
        FROM APZL.LC_RENT_INCOME_TEMP
        WHERE ((APZL.LC_RENT_INCOME_TEMP.ROLL_BACK = '0') AND (APZL.LC_RENT_INCOME_TEMP.IS_FLOWING = '0'))
        GROUP BY APZL.LC_RENT_INCOME_TEMP.PAYMENT_NUMBER, APZL.LC_RENT_INCOME_TEMP.PLAN_LIST
    ) LRT ON (((LRT.PAYMENT_NUMBER = LRP.PAYMENT_NUMBER) AND (LRT.PLAN_LIST = LRP.PLAN_LIST))))
    WHERE (((LRP.RENT - ifnull(LR.SRENT, 0)) - ifnull(LRT.SRENT, 0)) > 0)
) LL ON (((LL.PAYMENT_NUMBER = L.PAYMENT_NUMBER) AND (LL.PLAN_LIST = L.PLAN_LIST))))
WHERE ((LCCS.PLAN_STATUS = '31') AND (L.PAYMENT_NUMBER IS NOT NULL))
GROUP BY L.PAYMENT_NUMBER, L.PLAN_LIST, L.PLAN_DATE
ORDER BY L.PAYMENT_NUMBER, L.PLAN_LIST;

-- comment on column vi_contract_penalty.PAYMENT_NUMBER not supported: 投放编号

-- comment on column vi_contract_penalty.PLAN_LIST not supported: 期次

-- comment on column vi_contract_penalty.PLAN_DATE not supported: 计划日期

