/* 
 * 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: 23 ago. 2021
 */
DROP  PROCEDURE `ReportCustomerPay`;

DELIMITER //
CREATE PROCEDURE `ReportCustomerPay`(
    in in_start_date VARCHAR(10),
    in in_end_date VARCHAR(10),
    in id_taxi_web VARCHAR(50)
    )
    READS SQL DATA
begin
SET @FromDate      = in_start_date;
SET @ToDate        = in_end_date;
SET @DiaSemana     = 7; -- 7 domingo, 1 lunes
SET @Minimo        = CASE WHEN DAYOFWEEK(@FromDate)-1 = 0 THEN 7 ELSE DAYOFWEEK(@FromDate)-1 END;
  SELECT 
            t.economic_number,
            concat(cstmr.first_name, ' ', cstmr.second_name, ' ', cstmr.last_name, ' ', cstmr.middle_name) as customer_name,
            IF(ISNULL(drvr.id), '', concat(drvr.first_name, ' ', drvr.second_name, ' ', drvr.last_name, ' ', drvr.middle_name)) as driver_name,
            (select COUNT(DISTINCT(pay_date)) from SALE where taxi_id = t.id and DATE_ADD(DATE(pay_date), interval 1 day) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) as worked_days,
            (select SUM(pay_day) from SALE where taxi_id = t.id and DATE_ADD(DATE(pay_date), interval 1 day) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d') and LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) not in ('sunday')) as total_worked_to_pay_days,
            (select sum(amount_menudeo) from EXPENSE where taxi_id = t.id and expense_type IN ('BILL','NOTE','ELECTRICO','OTRAS_SALIDAS') and DATE(payment_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) total_electric_expense,
            (select sum(amount_menudeo) from EXPENSE where taxi_id = t.id and expense_type = 'TORNO' and DATE(payment_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) total_torno_expense,
            (select sum(amount_menudeo) from EXPENSE where taxi_id = t.id and expense_type = 'SOLDADURA' and DATE(payment_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) total_soldadura_expense,
            (select sum(amount_menudeo) from EXPENSE where taxi_id = t.id and expense_type = 'ESTETICO' and DATE(payment_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) total_estetico_expense,
            (select sum(amount_menudeo) from EXPENSE where taxi_id = t.id and expense_type = 'GRUA' and DATE(payment_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) total_grua_expense,
            (select sum(subtotal) from SERVICE_ORDER where taxi_id = t.id and service_order_type = 'FINISHED' and DATE(finish_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) as total_service_order,
            (select GROUP_CONCAT(concat(' ',name)) from SERVICE_ORDER where taxi_id = t.id and service_order_type = 'FINISHED' and DATE(finish_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) as service_order_folios,
            (select GROUP_CONCAT(concat(' ',description)) from SERVICE_ORDER where taxi_id = t.id and service_order_type = 'FINISHED' and DATE(finish_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) as service_order_desc,
            (select GROUP_CONCAT(concat(' ',srvc.name)) from CONTRACT cntrct inner join CONTRACT_HAS_SERVICE chs on cntrct.id = chs.contract_id inner join SERVICE srvc on chs.service_id = srvc.id where cntrct.taxi_id = t.id and cntrct.active = 1) as contract_service,
            (select sum(srvc.charge) from CONTRACT cntrct inner join CONTRACT_HAS_SERVICE chs on cntrct.id = chs.contract_id inner join SERVICE srvc on chs.service_id = srvc.id where cntrct.taxi_id = t.id and cntrct.active = 1) total_contract_service,
            (select 
            CASE 
            WHEN sum(TIMESTAMPDIFF(MINUTE, progress_date, finish_date)) > 1080 THEN sum(TIMESTAMPDIFF(MINUTE, progress_date, finish_date))/1080
            WHEN sum(TIMESTAMPDIFF(MINUTE, progress_date, finish_date)) > 540 and sum(TIMESTAMPDIFF(MINUTE, progress_date, finish_date)) < 1080 THEN 0.5
            ELSE 0
            END
            from SERVICE_ORDER 
            where taxi_id = t.id and service_order_type = 'FINISHED' 
            and DATE(finish_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')
            ) as total_pays_to_discount,
            (SELECT datediff(@ToDate, @FromDate) DIV 7 + (CASE WHEN @Minimo = @DiaSemana THEN 1 ELSE 0 END)) AS 'to_collect_days',
            t.payment_car AS payment_car,
(select SUM(pay_day) from SALE where taxi_id = t.id and DATE_ADD(DATE(pay_date), interval 1 day) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d') and LOWER(DAYNAME(DATE_ADD(DATE(pay_date), interval 1 day))) in ('sunday')) as total_worked_to_pay_days_sunday
  FROM TAXI t inner join
    PERSON cstmr on t.customer_id = cstmr.id left join
    PERSON drvr on t.driver_id = drvr.id
  WHERE
    t.active = 1 and t.id = "'" +  id_taxi_web  + "'" and
    cstmr.active = 1 
    
  order by t.economic_number;
end; //
DELIMITER ;

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