/* 
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Other/SQLTemplate.sql to edit this template
 */
/**
 * Author:  Cato32
 * Created: 19 may 2023
 */

ALTER TABLE `crov_inmobiliaria`.`person` 
ADD COLUMN `usuario_app` ENUM('ENABLED', 'DISABLED') NULL DEFAULT 'DISABLED' AFTER `regimen_fiscal`,
ADD COLUMN `password` VARCHAR(100) NULL AFTER `usuario_app`;

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`localhost` 
    SQL SECURITY DEFINER
VIEW `person_list_view` AS
    SELECT 
        `p`.`id` AS `id`,
        CONCAT((CASE
                    WHEN
                        ((`p`.`first_name` IS NOT NULL)
                            AND (`p`.`first_name` <> ''))
                    THEN
                        CONCAT(SUBSTR(UPPER(`p`.`first_name`), 1, 1),
                                SUBSTR(LOWER(`p`.`first_name`), 2),
                                ' ')
                    ELSE ''
                END),
                (CASE
                    WHEN
                        ((`p`.`second_name` IS NOT NULL)
                            AND (`p`.`second_name` <> ''))
                    THEN
                        CONCAT(SUBSTR(UPPER(`p`.`second_name`), 1, 1),
                                SUBSTR(LOWER(`p`.`second_name`), 2),
                                ' ')
                    ELSE ''
                END),
                (CASE
                    WHEN
                        ((`p`.`last_name` IS NOT NULL)
                            AND (`p`.`last_name` <> ''))
                    THEN
                        CONCAT(SUBSTR(UPPER(`p`.`last_name`), 1, 1),
                                SUBSTR(LOWER(`p`.`last_name`), 2),
                                ' ')
                    ELSE ''
                END),
                (CASE
                    WHEN
                        ((`p`.`middle_name` IS NOT NULL)
                            AND (`p`.`middle_name` <> ''))
                    THEN
                        CONCAT(SUBSTR(UPPER(`p`.`middle_name`), 1, 1),
                                SUBSTR(LOWER(`p`.`middle_name`), 2))
                    ELSE ''
                END)) AS `full_name`,
        (SELECT 
                `cn`.`contact_number`
            FROM
                `contact_number` `cn`
            WHERE
                ((`cn`.`number_type` = 'HOME')
                    AND (`cn`.`person_id` = `p`.`id`))) AS `contact_number_local`,
        (SELECT 
                `cn`.`contact_number`
            FROM
                `contact_number` `cn`
            WHERE
                ((`cn`.`number_type` = 'MOBILE')
                    AND (`cn`.`person_id` = `p`.`id`))) AS `contact_number_mobile`,
        (SELECT 
                `a`.`address_google_maps`
            FROM
                `address` `a`
            WHERE
                ((`a`.`address_type` = 'HOME')
                    AND (`a`.`person_id` = `p`.`id`))) AS `address`,
        (SELECT 
                GROUP_CONCAT(CONCAT(`taxi`.`economic_number`)
                        SEPARATOR ',')
            FROM
                `taxi`
            WHERE
                (`taxi`.`customer_id` = `p`.`id`)
            GROUP BY `taxi`.`customer_id`) AS `customer_vehicles`,
        `p`.`admission_date` AS `driver_admission_date`,
        `p`.`person_type` AS `person_type`,
        IF((`p`.`adeudo` IS NULL),
            0,
            `p`.`adeudo`) AS `customer_adeudo`,
        `p`.`id_location` AS `location`,
        IF((`p`.`saving` IS NULL),
            0,
            `p`.`saving`) AS `saving_adeudo`,
        `p`.`usuario_app` AS `usuario_app`,
        `p`.`mail` AS `mail`
    FROM
        `person` `p`
    WHERE
        (`p`.`active` = 1)
    ORDER BY CONCAT((CASE
                WHEN
                    ((`p`.`first_name` IS NOT NULL)
                        AND (`p`.`first_name` <> ''))
                THEN
                    CONCAT(SUBSTR(UPPER(`p`.`first_name`), 1, 1),
                            SUBSTR(LOWER(`p`.`first_name`), 2),
                            ' ')
                ELSE ''
            END),
            (CASE
                WHEN
                    ((`p`.`second_name` IS NOT NULL)
                        AND (`p`.`second_name` <> ''))
                THEN
                    CONCAT(SUBSTR(UPPER(`p`.`second_name`), 1, 1),
                            SUBSTR(LOWER(`p`.`second_name`), 2),
                            ' ')
                ELSE ''
            END),
            (CASE
                WHEN
                    ((`p`.`last_name` IS NOT NULL)
                        AND (`p`.`last_name` <> ''))
                THEN
                    CONCAT(SUBSTR(UPPER(`p`.`last_name`), 1, 1),
                            SUBSTR(LOWER(`p`.`last_name`), 2),
                            ' ')
                ELSE ''
            END),
            (CASE
                WHEN
                    ((`p`.`middle_name` IS NOT NULL)
                        AND (`p`.`middle_name` <> ''))
                THEN
                    CONCAT(SUBSTR(UPPER(`p`.`middle_name`), 1, 1),
                            SUBSTR(LOWER(`p`.`middle_name`), 2))
                ELSE ''
            END))

ALTER TABLE `crov_inmobiliaria`.`lote` 
ADD COLUMN `monto_pagado` DOUBLE NULL DEFAULT 0 AFTER `estatus_activo`;
