/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Author:  Oscar
 * Created: 26/10/2021
 */

ALTER TABLE `apo_pro_com_april_ten`.`apc_loan_employee` 
ADD COLUMN `payroll` ENUM('ENEBLED', 'DISABLED') NOT NULL AFTER `total_amount_to_pay`,
ADD COLUMN `active_status` ENUM('ENEBLED', 'DISABLED') NULL AFTER `payroll`;


/*Updates de donadony*/
ALTER TABLE `apo_pro_com_april_ten`.`apc_loan_employee_detail` 
ADD COLUMN `payroll` ENUM('ENEBLED', 'DISABLED') NOT NULL DEFAULT 'ENEBLED' AFTER `created_on`;


/*Update Base de datos saving view*/
CREATE 
    OR REPLACE
VIEW `APC_STATS_EMPLOYEE_SAVING_VIEW` AS
   SELECT 
        `es`.`id` AS `id`,
        `es`.`id_user` AS `id_user`,
        CONCAT(`hr`.`first_name`, ' ', `hr`.`last_name`) AS `name`,
        `hrr`.`route_name` AS `route_name`,
        `es`.`employee_saving` AS `employee_saving`,
        `es`.`created_on` AS `created_on`,
        `es`.`Type` AS `Type`
    FROM
        ((`APC_EMPLOYEE_SAVING` `es`
        JOIN `APC_HUMAN_RESOURCE` `hr` ON ((`es`.`id_user` = `hr`.`id`)))
        LEFT JOIN (SELECT 
            `hrr`.`id_human_resource` AS `id_human_resource`,
                GROUP_CONCAT(DISTINCT `r`.`route_name`
                    SEPARATOR ',') AS `route_name`
        FROM
            (`APC_HUMAN_RESOURCE_HAS_ROUTE` `hrr`
        LEFT JOIN `APC_ROUTE` `r` ON ((`hrr`.`id_route` = `r`.`id`)))
        GROUP BY `hrr`.`id_human_resource`) `hrr` ON ((`hr`.`id` = `hrr`.`id_human_resource`)))

