/* 
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
/**
 * Author:  Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 * Created: 17 ago. 2021
 */
DROP  PROCEDURE `ReportGoogleDrive`;

DELIMITER //
create procedure `ReportGoogleDrive`(
    in in_start_date VARCHAR(10),
    in in_end_date VARCHAR(10)
    )
    READS SQL DATA
begin
SELECT 
CONCAT(p.first_name, ' ', p.last_name) driver_name,
IF(p.active = 1, 'Activo', IF(p.black_list = 'ENABLED', 'Lista negra' , 'Banca')) estatus_driver,
IF(ISNULL(t.id), 'Sin vehiculo', CONCAT(t.economic_number)) vehicle_name,
CONCAT(p2.first_name, ' ', p2.last_name) customer_name,
(SELECT IF(ISNULL(SUM(total)) , 0 , SUM(total)) FROM TO_COLLECT WHERE driver_id = p.id) total_adeudo,
IF(ISNULL(t.pay_day), 0, t.pay_day) as payment_expected_week, 
IF(ISNULL(t.pay_saving), 0, t.pay_saving) as saving_expected_week, 
IF(ISNULL(t.pay_accident), 0, t.pay_accident) as accident_expected_week, 
(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'monday'  AND person_id = p.id ) monday_pay,
(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'tuesday'  AND person_id = p.id ) tuesday_pay,
(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'wednesday'  AND person_id = p.id ) wednesday_pay,
(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'thursday'  AND person_id = p.id ) thursday_pay,
(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'friday'  AND person_id = p.id ) friday_pay,
(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'saturday'  AND person_id = p.id ) saturday_pay,
(SELECT IF(ISNULL(SUM(pay_day + pay_saving + accident )), 0 , SUM(pay_day + pay_saving + accident )) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) = 'sunday'  AND person_id = p.id ) sunday_pay,
(SELECT IF(ISNULL(SUM(pay_day)), 0 , SUM(pay_day)) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND person_id = p.id ) total_pay_week,
(SELECT IF(ISNULL(SUM(pay_saving)), 0 , SUM(pay_saving)) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND person_id = p.id ) total_pay_saving_week,
(SELECT IF(ISNULL(SUM(accident)), 0 , SUM(accident)) FROM SALE s where WEEK(DATE_ADD(DATE(pay_date), interval 1 day), 1) = WEEK(STR_TO_DATE(in_start_date, '%Y-%m-%d'), 1) AND 
YEAR(DATE_ADD(DATE(pay_date), interval 1 day)) = YEAR(STR_TO_DATE(in_start_date, '%Y-%m-%d')) AND person_id = p.id ) total_accident_week,
IF(t.day_monday = 'ENEBLED', 1 , 0) as isMonday,
IF(t.day_tuesday = 'ENEBLED', 1 , 0) as isTuesday,
IF(t.day_wednesday = 'ENEBLED', 1 , 0) as isWednesday,
IF(t.day_thursday = 'ENEBLED', 1 , 0) as isThursday,
IF(t.day_friday = 'ENEBLED', 1 , 0) as isFriday,
IF(t.day_saturday = 'ENEBLED', 1 , 0) as isSaturday
FROM PERSON p
JOIN TAXI t ON t.driver_id = p.id
JOIN PERSON p2 ON p2.id = t.customer_id
WHERE p.person_type = 'DRIVER' AND p.active = 1;
end ;//
DELIMITER ;

GRANT EXECUTE ON PROCEDURE car_driver_tepic.ReportGoogleDrive TO 'cardriver'@'%';