DELIMITER $$

ALTER ALGORITHM=UNDEFINED DEFINER=`apzl`@`%` SQL SECURITY DEFINER VIEW `vi_splitting_pay` AS (
select
  `o`.`distributor_id`    AS `distributor_id`,
  `o`.`CONTRACT_NO`       AS `CONTRACT_NO`,
  `o`.`contract_id`       AS `contract_id`,
  `o`.`lccc_charge`       AS `lccc_charge`,
  `o`.`lccc_ratio`        AS `lccc_ratio`,
  `o`.`lccc_list`         AS `lccc_list`,
  `o`.`plan_list`         AS `plan_list`,
  `lul`.`CUSTOMER_NAME`   AS `customer_name`,
  `lrp`.`interest_sp`     AS `sum_sp`,
  ifnull(`lsp`.`splitting_money`,0) AS `splitting_money`,
  cast(((`lrp`.`interest_sp` * `o`.`lccc_ratio`) / 100) as decimal(22,2)) AS `should_sp`,
  (cast(((`lrp`.`interest_sp` * `o`.`lccc_ratio`) / 100) as decimal(22,2)) - ifnull(`lsp`.`splitting_money`,0)) AS `cur_sp`,
  `di`.`DISTRIBUTOR_NAME` AS `distributor_name`
from ((((`apzl`.`vi_split_config` `o`
      left join (select
                   sum(`apzl`.`lc_rent_plan`.`INTEREST_sp`) AS `interest_sp`,
                   `apzl`.`lc_rent_plan`.`CONTRACT_ID` AS `contract_id`
                 from `apzl`.`lc_rent_plan`
                 where (`apzl`.`lc_rent_plan`.`INTEREST_sp` is not null)
                 group by `apzl`.`lc_rent_plan`.`CONTRACT_ID`) `lrp`
        on ((`lrp`.`contract_id` = `o`.`contract_id`)))
     left join (select
                  sum(ifnull(`apzl`.`lc_splitting_pay`.`splitting_money`,0)) AS `splitting_money`,
                  `apzl`.`lc_splitting_pay`.`contract_id` AS `contract_id`,
                  max(`apzl`.`lc_splitting_pay`.`pay_list`) AS `pay_list`,
                  `apzl`.`lc_splitting_pay`.`id`          AS `id`
                from `apzl`.`lc_splitting_pay`
                group by `apzl`.`lc_splitting_pay`.`contract_id`) `lsp`
       on ((`o`.`contract_id` = `lsp`.`contract_id`)))
    left join `apzl`.`lb_union_lessee` `lul`
      on (((`lul`.`CONTRACT_ID` = `o`.`contract_id`)
           and (`lul`.`IS_MAIN` = 'Y'))))
   left join `apzl`.`distributor_info` `di`
     on ((`di`.`distributor_no` = `o`.`distributor_id`)))
where ((ifnull(`lsp`.`pay_list`,0) <> `o`.`lccc_charge`)
       and (`o`.`lccc_charge` > 0)))$$

DELIMITER ;