LOCAL lcFileNamePd

oJson.setbdd_remote()

lcFileNamePd = oJson._cFileName+'.7z'
lcFileNameBddPd = 'p'+oJson._cFileName+'.sqlite'

WAIT WINDOW 'Descargando... '+lcFileNamePd nowait

cResp = 'Ok'

IF oApp.ftpdownload(lcFileNamePd)
	WAIT WINDOW 'Descomprimiendo... '+lcFileNamePd nowait
	
	DELETE FILE &lcFileNameBddPd

	cmdZip = '7z e -y '+lcFileNamePd
	RUN &cmdZip
	
	DELETE FILE &lcFileNamePd
ELSE
	cResp = 'Fallo'
ENDIF
oApp.runLink('json/set_respuesta.php?id='+oJs.cIdCmd+'&resp='+cResp)


pdLocal = CreateObject("newObjects.sqlite3.dbutf8")
pdLocal.NoErrorMode = .T.


LOCAL o as "adodb.recordset" 

IF pdLocal.Open(lcFileNameBddPd) THEN
	o = oJson.sqlite (pdLocal,'select * from pedido where estado=0')
	
	FOR iCount = 1 TO o.Count
		oApp.t_clear()
		cId = oApp.newid('Basket', 14, .t.)
		
		tt = CTOT(o.item(iCount).item("t_ini"))
		
		cIdBasketPDa = DTOC(DATETIME(),1)+allt(TRANSFORM(o.item(iCount).item("id")))+ALLTRIM(TRANSFORM(o.item(iCount).item("idsalesman")))
		
		IF oApp.sqlexec('SELECT id from basket where pocustomer='+cIdBasketpda)=0
		
			oApp.t_clear()
			oApp.t_set('id', cId, 'C')	
			oApp.t_set('IdCustomer', PADL(o.item(iCount).item("idcustomer"),12,'0'), 'C')	
			oApp.t_set('IdQuote', '', 'C')
			oApp.t_set('Date', TTOD(tt), 'D')
			oApp.t_set('Hora', PADL(hour(tt),2,'0')+':'+PADL(minute(tt),2,'0')+':'+PADL(sec(tt),2,'0'), 'C')
			oApp.t_set('IdSalesman', PADL(o.item(iCount).item("idsalesman"),8,'0'), 'C')
			oApp.t_set('CodePrinter', 1, 'N')
			oApp.t_set('PVP', o.item(iCount).item("pvp"), 'N')
			oApp.t_set('SubTotal', o.item(iCount).item("subtotal"), 'N')
			oApp.t_set('Subt0', o.item(iCount).item("subt0"), 'N')
			oApp.t_set('SubtX', o.item(iCount).item("subtx"), 'N')
			oApp.t_set('Tax', o.item(iCount).item("iva"), 'N')
			oApp.t_set('Total', o.item(iCount).item("total"), 'N')
			oApp.t_set('IdUser', PADL(o.item(iCount).item("iduser"),8,'0'), 'C')
			oApp.t_set('TaxPercent', oApp.niva, 'N')
			oApp.t_set('FormaPago', 12, 'N')
			oApp.t_set('DateOk', TTOD(tt), 'D')
			oApp.t_set('Moneda', 1, 'N')
			oApp.t_set('TipoCambio', 1, 'N')
			oApp.t_set('IdCC', '0000000000', 'C')
			oApp.t_set('IdCP', '0000000000', 'C')
			oApp.t_set('noteprint', allt(TRANSFORM(o.item(iCount).item("id")))+".- "+SUBSTR(ALLTRIM(o.item(iCount).item("nota")),1,250), 'C')
			
			oApp.t_set('pocustomer', cIdBasketPDa , 'C')
			
			oApp.t_set('note', ALLTRIM("Pedido Nro. "+allt(TRANSFORM(o.item(iCount).item("id")))+" "+TTOC(DATETIME(),3)), 'C')
			
			
			oApp.t_insert('Basket')
			
			p = oJson.sqlite (pdLocal,'select * from pedidodet where idpedido='+STR(o.item(iCount).item("id")))
			
			FOR jCount = 1 TO p.Count

				nPvp = IIF(p.item(jCount).item("pvp")=1,4,3)
	
				oApp.t_clear()
				oApp.t_set('id', cId, 'C')	
				oApp.t_set('IdItem', PADL(p.item(jCount).item("iditem"),12,'0'), 'C')
				oApp.t_set('Line', PADL(p.item(jCount).item("linea"),4,'0'), 'C')
				oApp.t_set('Price', p.item(jCount).item("precio")/IIF(p.item(jCount).item("iva")=1,1.12,1), 'N')
				oApp.t_set('IVA', p.item(jCount).item("precio")*IIF(p.item(jCount).item("iva")=1,1.12/.12,0), 'N')
				oApp.t_set('PVP',nPvp , 'N')
				oApp.t_set('LIVA', p.item(jCount).item("iva"))
				oApp.t_set('Quantity', p.item(jCount).item("cantidad"), 'N')
				
				oApp.t_set('bultos', p.item(jCount).item("bultos"), 'N')
				oApp.t_set('uc', p.item(jCount).item("uc"), 'N')
				oApp.t_set('unidades', p.item(jCount).item("unidades"), 'N')
				
				oApp.t_insert('BasketDet')
			

			ENDFOR

		ENDIF


	ENDFOR
	
	
	pdLocal.Close()
	
	WAIT WINDOW 'listo... ' TIMEOUT .3
ENDIF
