create function getRentPenalty2(r_payment_number varchar(40), r_plan_list varchar(40), calc_date varchar(40)) returns varchar(200)
RETURN (
select t.plan_penalty from (
 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` > calc_date),
                calc_date,
                `ll`.`hire_date`
              )
            ) 
            THEN (
              (
                (`lcc`.`PENA_RATE` / 10000) * (
                  TO_DAYS(
                    IF(
                      (`ll`.`hire_date` > calc_date),
                      calc_date,
                      `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` > calc_date),
                  calc_date,
                  `ll`.`hire_date`
                )
              ) 
              THEN (
                (
                  (`lcc`.`PENA_RATE` / 10000) * (
                    TO_DAYS(
                      IF(
                        (`ll`.`hire_date` > calc_date),
                        calc_date,
                        `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 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`,
          calc_date 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` =r_payment_number)
      and l.PLAN_LIST=r_plan_list
    ) 
  GROUP BY `l`.`PAYMENT_NUMBER`,
    `l`.`PLAN_LIST`,
    `l`.`PLAN_DATE` 
  ORDER BY `l`.`PAYMENT_NUMBER`,
    `l`.`PLAN_LIST` ) t
);

