Gracias de antemano por sus consejos.
Nesito crear una consulta que genere un estado de cuenta para clientes,
una linea seran los datos de la factura y la/las siguientes con los pagos aplicados a esas mismas facturas.
Estoy tratando de filtrar por el campo entity y no hace el filtro.
obvio que esoy generando mal la consulta.
Es posible utilizar subconsultas en lugar de esta UNION
Código SQL:
Ver originalSELECT
f.rowid AS rowid,
f.datef AS datef,
CASE f.TYPE
WHEN 0 THEN
'Factura'
WHEN 2 THEN
'Credito'
ELSE
'Desconocido'
END
AS Tipo_Doc,
f.rowid AS facid,
f.facnumber AS facnumber,
f.fk_soc AS fk_soc,
s.nom AS nom,
IF (( f.total_ttc >= 1 ),f.total_ttc,'') AS Cargo,
IF (( f.total_ttc <= 1 ),f.total_ttc,'') AS Abono,
s.entity AS entity
FROM llx_facture AS f JOIN llx_societe AS s ON f.fk_soc = s.rowid
WHERE f.fk_statut BETWEEN 1 AND 2
UNION ALL
SELECT
p.rowid AS rowid,
p.datep AS datep,
'PAGO' AS Tipo_Doc,
pf.fk_facture AS facid,
f.facnumber AS facnumber,
f.fk_soc AS fk_soc,
s.nom AS nom,
IF ((pf.amount <= 1),pf.amount,'') AS Cargo,
IF ((pf.amount >= 1),pf.amount,'') AS Abono,
s.entity AS entity
FROM llx_paiement AS p JOIN llx_paiement_facture AS pf ON p.rowid = pf.fk_paiement
JOIN llx_facture AS f ON pf.fk_facture = f.rowid
JOIN llx_societe AS s ON f.fk_soc = s.rowid
WHERE s.entity = 21
ORDER BY facid
Anexo fragmento del resultado separado por comas:
rowid,datef,Tipo_Doc,facid,facnumber,fk_soc,nom,Ca rgo,Cargo,entity
75,2/2/2015 00:00:00,Factura,75,FAC-00001,44,ORGANIZACIÓN AURORA S.A DE C.V,1900.00000000,,18
76,2/2/2015 00:00:00,Factura,76,FAC-00002,44,ORGANIZACIÓN AURORA S.A DE C.V,750.00000000,,18
80,2/2/2015 00:00:00,Credito,80,NCR-00001,44,ORGANIZACIÓN AURORA S.A DE C.V,,-1900.00000000,18
81,2/2/2015 00:00:00,Credito,81,NCR-00002,44,ORGANIZACIÓN AURORA S.A DE C.V,,-750.00000000,18
90,2/2/2015 00:00:00,Factura,90,FAC-00004,43,VERONICA SANTOS JOACHIN,500001.25000000,,18
92,2/2/2015 00:00:00,Factura,92,FAC-00005,43,VERONICA SANTOS JOACHIN,590000.18000000,,18
39,20/2/2015 12:00:00,PAGO,192,FAC-00035,106,PROMOTORA HOTELERA ORIGINAL SA DE CV,,638.12000000,21
192,16/2/2015 00:00:00,Factura,192,FAC-00035,106,PROMOTORA HOTELERA ORIGINAL SA DE CV,638.12000000,,21
28,19/2/2015 12:00:00,PAGO,193,FAC-00036,100,GRUPO MAJOSA SA DE CV,,939.60000000,21
193,17/2/2015 00:00:00,Factura,193,FAC-00036,100,GRUPO MAJOSA SA DE CV,939.60000000,,21
194,11/2/2015 00:00:00,Factura,194,FAC-01961,19,GRUPO INDUSTRIAL CREYSI, S.A. DE C.V.,31237.18000000,,17
196,11/2/2015 00:00:00,Factura,196,FAC-01963,19,GRUPO INDUSTRIAL CREYSI, S.A. DE C.V.,35253.79000000,,17
198,12/2/2015 00:00:00,Factura,198,FAC-01964,19,GRUPO INDUSTRIAL CREYSI, S.A. DE C.V.,63336.00000000,,17
199,12/2/2015 00:00:00,Factura,199,FAC-01965,19,GRUPO INDUSTRIAL CREYSI, S.A. DE C.V.,8184.96000000,,17