create view vi_overdue_rent_plan as (
SELECT LRP.ID                                                                                           AS ID,
       LRP.QUOT_ID                                                                                      AS QUOT_ID,
       LRP.CUST_ID                                                                                      AS CUST_ID,
       LRP.PROJECT_ID                                                                                   AS PROJECT_ID,
       LRP.PROJECT_PLAN_NUMBER                                                                          AS PROJECT_PLAN_NUMBER,
       LRP.CONTRACT_ID                                                                                  AS CONTRACT_ID,
       LRP.CONTRACT_PLAN_NUMBER                                                                         AS CONTRACT_PLAN_NUMBER,
       LRP.PAYMENT_NUMBER                                                                               AS PAYMENT_NUMBER,
       LRP.PLAN_LIST                                                                                    AS PLAN_LIST,
       LRP.PLAN_DATE                                                                                    AS PLAN_DATE,
       LRP.RENT                                                                                         AS RENT,
       LRP.CORPUS                                                                                       AS CORPUS,
       INCOME.HIRE_DATE                                                                                 AS HIRE_DATE,
       ifnull(INCOME.FACT_RENT, 0)                                                                      AS FACT_RENT,
       ifnull(INCOME.FACT_CORPUS, 0)                                                                    AS FACT_CORPUS,
       ifnull(INCOME.FACT_PENALTY, 0)                                                                   AS FACT_PENALTY,
       (ifnull(LRP.RENT, 0) - ifnull(INCOME.FACT_RENT, 0))                                              AS OVERDUERENT,
       (ifnull(LRP.INTEREST, 0) - ifnull(INCOME.FACT_INTEREST, 0))                                      AS OVERINTEREST,
       `getRentPenalty`(LRP.PAYMENT_NUMBER, LRP.PLAN_LIST, date_format(sysdate(), '%Y/%m/%d'))          AS PENALTY,
       (CASE
            WHEN ((ifnull(INCOME.FACT_RENT, 0) - ifnull(LRP.RENT, 0)) >= 0) THEN (CASE
                                                                                      WHEN ((to_days(INCOME.HIRE_DATE) - to_days(LRP.PLAN_DATE)) < 0)
                                                                                          THEN 0
                                                                                      ELSE (to_days(INCOME.HIRE_DATE) - to_days(LRP.PLAN_DATE)) END)
            ELSE (to_days(date_format(sysdate(), '%Y/%m/%d')) - to_days(LRP.PLAN_DATE)) END)            AS OVERDUE_DAYS,
       (CASE WHEN ((ifnull(INCOME.FACT_RENT, 0) - ifnull(LRP.RENT, 0)) >= 0) THEN '已结清' ELSE '未结清' END) AS PLAN_STATUS
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,
           max(APZL.LC_RENT_INCOME.HIRE_DATE)           AS HIRE_DATE,
           ifnull(sum(APZL.LC_RENT_INCOME.RENT), 0)     AS FACT_RENT,
           ifnull(sum(APZL.LC_RENT_INCOME.CORPUS), 0)   AS FACT_CORPUS,
           ifnull(sum(APZL.LC_RENT_INCOME.INTEREST), 0) AS FACT_INTEREST,
           ifnull(sum(APZL.LC_RENT_INCOME.PENALTY), 0)  AS FACT_PENALTY
    FROM APZL.LC_RENT_INCOME
    WHERE (APZL.LC_RENT_INCOME.PAYMENT_NUMBER IS NOT NULL)
    GROUP BY APZL.LC_RENT_INCOME.PAYMENT_NUMBER, APZL.LC_RENT_INCOME.PLAN_LIST
) INCOME ON (((LRP.PAYMENT_NUMBER = INCOME.PAYMENT_NUMBER) AND (LRP.PLAN_LIST = INCOME.PLAN_LIST))))
WHERE ((LRP.PAYMENT_NUMBER IS NOT NULL) AND (LRP.PLAN_DATE <= convert(date_format(sysdate(), '%Y/%m/%d') USING GBK)))
ORDER BY LRP.PAYMENT_NUMBER, LRP.PLAN_LIST);

-- comment on column vi_overdue_rent_plan.ID not supported: 标识

-- comment on column vi_overdue_rent_plan.QUOT_ID not supported: 报价编号

-- comment on column vi_overdue_rent_plan.CUST_ID not supported: 客户编号

-- comment on column vi_overdue_rent_plan.PROJECT_ID not supported: 项目编号

-- comment on column vi_overdue_rent_plan.PROJECT_PLAN_NUMBER not supported: 项目方案编号

-- comment on column vi_overdue_rent_plan.CONTRACT_ID not supported: 合同编号

-- comment on column vi_overdue_rent_plan.CONTRACT_PLAN_NUMBER not supported: 合同方案编号

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

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

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

-- comment on column vi_overdue_rent_plan.RENT not supported: 租金

-- comment on column vi_overdue_rent_plan.corpus not supported: 财务本金

-- comment on column vi_overdue_rent_plan.HIRE_DATE not supported: 核销日期

