-- --------------------------------------------------------
-- Arrebol Consuntancy
-- version 1.0.1
-- http://www.arrebolconsultancy.com
-- --------------------------------------------------------
-- --------------------------------------------------------
--
-- Estructura para la vista `DASHBOARD_TO_COLLECT_VIEW`
--
CREATE OR REPLACE VIEW `DASHBOARD_TO_COLLECT_VIEW` AS 
SELECT 
    tx.id AS id_vehicle,
    tx.economic_number AS economic_number,
    vh.name AS vehicle_type,
    pr.id AS id_driver,
    CONCAT( CASE WHEN pr.first_name IS NOT NULL AND pr.first_name != '' THEN CONCAT(SUBSTR(UPPER(pr.first_name), 1, 1),SUBSTR(LOWER(pr.first_name), 2)) ELSE '' END, CASE  WHEN pr.second_name IS NOT NULL AND pr.second_name != '' THEN CONCAT(' ',SUBSTR(UPPER(pr.second_name), 1, 1),SUBSTR(LOWER(pr.second_name), 2))  ELSE '' END, CASE  WHEN pr.last_name IS NOT NULL AND pr.last_name != '' THEN CONCAT(' ',SUBSTR(UPPER(pr.last_name), 1, 1),SUBSTR(LOWER(pr.last_name), 2)) ELSE '' END, CASE  WHEN pr.middle_name IS NOT NULL AND pr.middle_name != '' THEN CONCAT(' ',SUBSTR(UPPER(pr.middle_name), 1, 1),SUBSTR(LOWER(pr.middle_name), 2)) ELSE '' END) AS driver_name,
    tx.app_platform AS app_platform,
    tx.pay_day AS pay_day,
    tx.pay_saving AS pay_saving,
    tx.pay_accident AS pay_accident,
    tx.pay_day+tx.pay_saving+tx.pay_accident AS total_to_pay,
    (
        SELECT 
            IFNULL(SUM(pay_day + pay_saving + accident),0) 
        FROM SALE 
        WHERE 
            person_id = pr.id AND 
            taxi_id = tx.id AND 
            DATE(created_on) = CURDATE()
    ) AS amount_paid,
    (SELECT IF(ISNULL(SUM(toc.total)) , 0 , SUM(toc.total)) FROM TO_COLLECT toc WHERE toc.driver_id = tx.driver_id) as total_adeudo,
    (CASE
        -- WHEN (SELECT COUNT(id) FROM SALE WHERE person_id = pr.id AND taxi_id = tx.id AND DATE(created_on) = CURDATE() AND (garage = 1 OR banca = 1)) > 0
        WHEN (SELECT COUNT(id) FROM SALE WHERE person_id = pr.id AND taxi_id = tx.id AND DATE(created_on) = CURDATE()) > 0
        THEN ''
        ELSE			
			CASE 
                -- list.amountPaid eq 0.0 and (list.payDay + list.paySaving + list.payAccident) lt list.totalAdeudo? 'datatableRowEmpty' = red
				WHEN (
                (SELECT IFNULL(SUM(pay_day + pay_saving + accident),0) FROM SALE WHERE person_id = pr.id AND taxi_id = tx.id AND DATE(created_on) = CURDATE()) = 0
                AND
                (tx.pay_day + tx.pay_saving + tx.pay_accident) < (SELECT IF(ISNULL(SUM(toc.total)) , 0 , SUM(toc.total)) FROM TO_COLLECT toc WHERE toc.driver_id = tx.driver_id)
                ) THEN 'datatableRowEmpty'
                -- : list.amountPaid ge (list.payDay + list.paySaving + list.payAccident) ? 'datatableRowGreen' = GREEN
                WHEN 
                (SELECT IFNULL(SUM(pay_day + pay_saving + accident),0) FROM SALE WHERE person_id = pr.id AND taxi_id = tx.id AND DATE(created_on) = CURDATE()) >= (tx.pay_day + tx.pay_saving + tx.pay_accident )
                THEN 'datatableRowGreen'
                -- : list.amountPaid eq 0.0 ? '' : 'datatableRowLittle'
                WHEN (SELECT IFNULL(SUM(pay_day + pay_saving + accident),0) FROM SALE WHERE person_id = pr.id AND taxi_id = tx.id AND DATE(created_on) = CURDATE()) = 0
                THEN 'datatableRowLittle'
                ELSE ''
            END
    END) AS style_class,
    tx.id_location AS id_location
FROM TAXI tx 
    INNER JOIN PERSON  pr ON tx.driver_id = pr.id
    INNER JOIN VEHICLE vh ON tx.vehicle_id = vh.id
WHERE 
    tx.active = 1 AND
    pr.active = 1 AND
    CASE
        WHEN (0 = (SELECT WEEKDAY(curdate())) AND tx.day_monday = 'ENEBLED')
        THEN 
            TRUE
        WHEN (1 = (SELECT WEEKDAY(curdate())) AND tx.day_tuesday = 'ENEBLED')
        THEN 
            TRUE
        WHEN (2 = (SELECT WEEKDAY(curdate())) AND tx.day_wednesday = 'ENEBLED')
        THEN 
            TRUE
        WHEN (3 = (SELECT WEEKDAY(curdate())) AND tx.day_thursday = 'ENEBLED')
        THEN 
            TRUE
        WHEN (4 = (SELECT WEEKDAY(curdate())) AND tx.day_friday = 'ENEBLED')
        THEN 
            TRUE
        WHEN (5 = (SELECT WEEKDAY(curdate())) AND tx.day_saturday = 'ENEBLED')
        THEN 
            TRUE
        WHEN (6 = (SELECT WEEKDAY(curdate())) AND tx.day_sunday = 'ENEBLED')
        THEN 
            TRUE
        ELSE 
            FALSE
    END;
-- --------------------------------------------------------
--
-- Estructura para la vista `SALE_DASHBOARD_VIEW` 
--
CREATE OR REPLACE VIEW `SALE_DASHBOARD_VIEW` AS 
SELECT 
    s.id AS id,
    s.person_id AS driver_id, s.taxi_id AS taxi_id, DATE_ADD(DATE(s.pay_date), INTERVAL 1 DAY) AS pay_date,
    buildFullPersonName(p.first_name, p.second_name, p.last_name, p.middle_name) AS full_name,
    SUM(s.pay_day) AS pay_day, SUM(s.pay_saving) AS pay_saving, SUM(s.accident) AS accident, 
    -- (SELECT IF(ISNULL(t.pay_day),0, t.pay_day) FROM TAXI t  WHERE t.id = s.taxi_id) - SUM(s.pay_day) AS to_complete_pay_day,
    s.pay_day_as_spected - SUM(s.pay_day) AS to_complete_pay_day,
    -- (SELECT IF(ISNULL(t.pay_saving),0, t.pay_saving) FROM TAXI t  WHERE t.id = s.taxi_id) - SUM(s.pay_saving) AS to_complete_pay_saving,
   s.pay_saving_as_spected - SUM(s.pay_saving) AS to_complete_pay_saving,
    -- (SELECT IF(ISNULL(t.pay_accident),0, t.pay_accident) FROM TAXI t  WHERE t.id = s.taxi_id) - SUM(s.accident) AS to_complete_pay_accident,
    s.accident_as_spected - SUM(s.accident) AS to_complete_pay_accident,
    -- (SELECT t.pay_day + t.pay_saving + t.pay_accident FROM TAXI t  WHERE t.id = s.taxi_id) AS total_to_pay,
    s.total_to_pay AS total_to_pay,
    (SUM(s.pay_day) + SUM(s.pay_saving) +  SUM(s.accident)) AS total_to_paid,
    GROUP_CONCAT(s.description SEPARATOR ',') AS description,
    IF(ISNULL(s.garage),0,s.garage) AS garage,
    IF(ISNULL(s.nueva_asignacion),0,s.nueva_asignacion) AS nueva_asignacion,
    IF(ISNULL(s.banca),0,s.banca) AS banca,
    (CASE
	WHEN (SUM(s.pay_day) + SUM(s.pay_saving)  + SUM(s.accident)) > s.total_to_pay
        THEN 'bigger'
        WHEN (SUM(s.pay_day) + SUM(s.pay_saving)  + SUM(s.accident)) = s.total_to_pay
        THEN 'complete'
        ELSE
            'incomplete'
    END) AS sale_status,
    s.id_bank_card AS id_bank_card,
    (SELECT numero FROM BANK_CARD WHERE id =  s.id_bank_card) AS account_number,
    s.transfer AS transfer
FROM SALE s
INNER JOIN PERSON p ON s.person_id = p.id
GROUP BY s.person_id, s.taxi_id, DATE(s.pay_date), s.total_to_pay
ORDER BY DATE(s.pay_date) DESC;