

select c.fecha_trabajo ,pxc.id_comanda,pxc.num_cuenta, sum(( isnull(pxc.precio_venta,0) + isnull(ixc.monto_impuestosxcuenta,0) + isnull(mod.total_modificador,0) )) as total_real
,cu.total as total_sistema, 

round(( sum(( isnull(pxc.precio_venta,0) + isnull(ixc.monto_impuestosxcuenta,0) + isnull(mod.total_modificador,0) )) - cu.total),2) as diferencia_Real_entre_cuentas

,pagos.total_pagado

, (sum(( isnull(pxc.precio_venta,0) + isnull(ixc.monto_impuestosxcuenta,0) + isnull(mod.total_modificador,0) )) - pagos.total_pagado) as diferencia_real

from productosxcuenta pxc inner join impuestosxcuenta ixc on pxc.id_productosxcuenta = ixc.id_productosxcuenta

inner join cuenta cu on pxc.id_comanda = cu.id_comanda and pxc.num_cuenta=cu.num_cuenta

left join (
select id_comanda,num_cuenta, sum(isnull(monto_pago,0))  as total_pagado ,count(id_comanda)as num_pagos , sum(isnull(propina,0)) as propinas
from pago group by id_comanda,num_cuenta

) pagos on pxc.id_comanda=pagos.id_comanda and pxc.num_cuenta=pagos.num_cuenta


left join ( -- aqui salen todos los modificadores con su costo
select id_productosxcuenta,sum((isnull(precio_modificador,0) + isnull(impuesto_modificador,0))) as total_modificador
 from modificador
group by id_productosxcuenta
) mod on pxc.id_productosxcuenta = mod.id_productosxcuenta
inner join comanda c on pxc.id_comanda=c.id_comanda

where isnull(pxc.id_tipo_anulacion,0) = 0

group by c.fecha_trabajo,pxc.id_comanda,pxc.num_cuenta, cu.total,pagos.total_pagado
having abs( (sum(( isnull(pxc.precio_venta,0) + isnull(ixc.monto_impuestosxcuenta,0) + isnull(mod.total_modificador,0) )) - pagos.total_pagado) ) > 1
order by pxc.id_comanda,c.fecha_trabajo



