LOCAL cResp, cCmd

cTabla = 'cxc'

cCond = " 0 "

cCond = "A.Balance != 0 AND !M.void AND !M.agrupado and cust.id>0 "

cIdS = ALLTRIM(oJs.cIdSalesman)

IF !oJs.all_cust
	cCond = cCond + ' and cust.id in (select idcustomer from visitascustomer where idsalesman="'+cIdS+'") '
	
	
	IF oJs.cxc_vendedor	
		cCond = cCond + ' and M.idsalesman="'+cIdS+'" '
	ENDIF
ENDIF


TEXT TO cCmd NOSHOW	TEXTMERGE 

	SELECT	A.reference as referencia,
		  	CAST(A.Date as char) as fecha, 
			CAST(A.Due as char) as vence,
			CAST(ROUND(A.Total,2) as char) as total, 
			CAST(ROUND(A.Balance,2) as char) as saldo, 
			CAST(A.Cuota as char) as cuota,
			M.idcustomer+0 as idcustomer,
			M.idsalesman+0 as idsalesman,
			A.id+0 as idCuota,
			M.idinvoice+0 as idinvoice,
			M.idcc+0 as idcc,
			M.idcp+0 as idcp,
			0.0 as color
	FROM AccountCus AS M INNER JOIN AccountCusCuota AS A on M.id = a.idaccountcus 
			LEFT JOIN Accountype      AS C on A.IdAccountype = C.Id 
			LEFT JOIN invoice         AS I on M.idinvoice = i.id 
			LEFT JOIN Customer        AS Cust on M.IdCustomer = Cust.Id 
			LEFT JOIN Salesman        AS Sales on Cust.IdSalesman = Sales.id
	WHERE  <<cCond>> 
	ORDER BY M.idcustomer, A.date, A.reference, A.Cuota 
ENDTEXT

*STRTOFILE(cCmd, '\temp\eddy_cxc.txt')

cIdSupplierCxc =  ALLTRIM(oApp.setup ('idsuppliercxc','C'))

oApp.sqlexec(cCmd,cTabla)

SELECT (cTabla)
GO TOP
SCAN ALL 
	cIdCuota = PADL(INT(idCuota),14,'0')
	cIdInvoice = PADL(INT(idinvoice),14,'0')
	cIdCustomer = PADL(INT(idcustomer),12,'0')
	
	IF !EMPTY(cIdSupplierCxc)
		TEXT TO cCmdCxC NOSHOW TEXTMERGE
			select id from invoicedet where id="<<cIdInvoice>>" and iditem in (select id from item where instr("<<cIdSupplierCxc>>",idsupplier+0))
		ENDTEXT
		
		IF idinvoice=44468
			*STRTOFILE(cCmdCxC, '\temp\eddy_cxc.txt')
		endif

		IF oApp.sqlexec(cCmdCxC)>0
			SELECT (cTabla)
			REPLACE color 	WITH 1.0
		ENDIF
	ENDIF
	
*select d.valororiginal,c.numero,c.idrecibos from detallechequesposf as d left join chequesposf as c on d.id = c.id where idaccountcuscuota = ?cIdCuota and anulado = 0 and cobrado = 0 and devuelto=0 
	
	TEXT TO cCmd NOSHOW TEXTMERGE 
		select ROUND(SUM(d.valororiginal)+0.00,2) as n
		from detallechequesposf as d 
		left join chequesposf as c on d.id = c.id 
		where d.idaccountcuscuota = "<<cIdCuota>>" and c.anulado = 0 and c.cobrado = 0 and c.devuelto=0
	ENDTEXT
	
	oApp.sqlexec(cCmd,'Cheq')

	SELECT (cTabla)
	replace saldo WITH ALLTRIM(TRANSFORM(VAL(saldo) - IIF(ISNULL(Cheq.n),0,Cheq.n)))
	IF VAL(saldo)<=0
		DELETE
	ENDIF
	
	SELECT (cTabla)
ENDSCAN
GO top
*BROWSE


oJs.android_up(cTabla)
