DELIMITER //
create procedure `ReportCustomerPay`(
    in in_start_date VARCHAR(10),
    in in_end_date VARCHAR(10)
    )
    READS SQL DATA
begin
  SELECT 
            t.economic_number,
            concat(cstmr.first_name, ' ', cstmr.second_name, ' ', cstmr.last_name, ' ', cstmr.middle_name) as customer_name,
            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(pay_date) 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(pay_date) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) as total_worked_to_pay_days,
            (select sum(payment) from EXPENSE where taxi_id = t.id and expense_type = 'ELECTRICO' 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(payment) 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(payment) 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(payment) 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(payment) 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 COUNT(DISTINCT(to_collect)) from TO_COLLECT where driver_id = t.driver_id and DATE(to_collect) between STR_TO_DATE(in_start_date, '%Y-%m-%d') AND STR_TO_DATE(in_end_date,'%Y-%m-%d')) as to_collect_days,
            t.payment_car AS payment_car
  FROM TAXI t inner join
    PERSON cstmr on t.customer_id = cstmr.id inner join
    PERSON drvr on t.driver_id = drvr.id
  WHERE
    t.active = 1 and 
    cstmr.active = 1 
    and drvr.active = 1
  order by t.economic_number;
end ;//
DELIMITER ;
-- this is for production grant
-- GRANT EXECUTE ON PROCEDURE car_driver_tepic.ReportCustomerPay TO 'cardriver'@'%';
-- local host
-- GRANT EXECUTE ON PROCEDURE car_driver_tepic.ReportCustomerPay TO 'cardriverlocalhost'@'localhost';
