zeusbart
11/1/2018 - 3:05 PM

reporte de cuentas

sql para reporte de cuentas

#### detallado  #####


SELECT
a.appointment_id,a.created_dt,a.status,a.amount,a.payment_type, w.unique_id,wt.type_name
FROM appointment a
left join workplace w on a.car_id= w.workplace_id
left join workplace_types wt on w.type_id = wt.type_id
WHERE a.status in (9,4) AND
a.payment_type in (1,2,4) AND a.amount>0 AND
(
CASE
WHEN a.status = 9 and a.payment_type = 4 then a.payment_type != 4 and a.created_dt > '2018-10-29 00:00:00' AND a.created_dt < '2018-11-04 23:59:59'
WHEN a.status = 4 and a.payment_type = 2 THEN a.created_dt > '2018-10-29 00:00:00' AND a.created_dt < '2018-11-04 23:59:59'
WHEN a.status = 4 THEN  (
         (a.appointment_id) IN
         (
            select pp.appointment_id from pending_payment as pp
            inner join slave_card_charge as scc
            on pp.appointment_id=scc.appointment_id
            where (scc.captured='true' or scc.captured=1) and pp.status=1  and
            scc.created_date BETWEEN '2018-10-29 00:00:00' AND '2018-11-04 23:59:59'
         )
         or
         (a.appointment_id) IN
         (
            SELECT swc.appointment_id  FROM slave_wallet_charge as swc
            inner JOIN pending_payment as pp on swc.appointment_id=pp.appointment_id
            where pp.status=1 and
            swc.created_date BETWEEN '2018-10-29 00:00:00' AND '2018-11-04 23:59:59'
         )
         or
         (a.appointment_id) IN
         (
            select a.appointment_id from slave_card_charge as scc
            where scc.appointment_id != 0  and a.appointment_id=scc.appointment_id  and  (scc.captured='true' or scc.captured=1)
            and scc.created_date BETWEEN '2018-10-29 00:00:00' AND '2018-11-04 23:59:59'
         )
         )
else a.created_dt > '2018-10-29 00:00:00' AND a.created_dt < '2018-11-04 23:59:59'
END
)




###suma  ####

SELECT SUM(amount) as suma, w.unique_id,wt.type_name FROM appointment a
left join workplace w on a.car_id = w.workplace_id
left join workplace_types wt on w.type_id = wt.type_id
WHERE a.status in (9,4) AND a.payment_type in (1,2,4) and a.amount>0 and
(
CASE
WHEN a.status = 9 and a.payment_type = 4 then a.payment_type != 4 and a.created_dt > '2018-10-29 00:00:00' AND a.created_dt < '2018-11-04 23:59:59'
WHEN a.status = 4 and a.payment_type = 2 THEN a.created_dt > '2018-10-29 00:00:00' AND a.created_dt < '2018-11-04 23:59:59'
WHEN a.status = 4 THEN  (
         (a.appointment_id) IN
         (
            select pp.appointment_id from pending_payment as pp
            left join slave_card_charge as scc
            on pp.appointment_id=scc.appointment_id
            where (scc.captured='true' or scc.captured=1) and pp.status=1  and
            scc.created_date BETWEEN '2018-10-29 00:00:00' AND '2018-11-04 23:59:59'
         )
         or
         (a.appointment_id) IN
         (
            SELECT swc.appointment_id  FROM slave_wallet_charge as swc
            LEFT JOIN pending_payment as pp on swc.appointment_id=pp.appointment_id
            where pp.status=1 and
            swc.created_date BETWEEN '2018-10-29 00:00:00' AND '2018-11-04 23:59:59'
         )
         or
         (a.appointment_id) IN
         (
            select a.appointment_id from slave_card_charge as scc
            where scc.appointment_id != 0  and a.appointment_id=scc.appointment_id  and  (scc.captured='true' or scc.captured=1)
            and scc.created_date BETWEEN '2018-10-29 00:00:00' AND '2018-11-04 23:59:59'
         )
         )
else a.created_dt > '2018-10-29 00:00:00' AND a.created_dt < '2018-11-04 23:59:59'
END
)
GROUP BY  a.car_id