PARAMETERS cBdd, _nTipoScan, cIdView

SET TALK off
SET STATUS BAR OFF
SET SYSMENU off
SET DELETED on
SET CONFIRM OFF
SET NOTIFY off
SET SAFETY off
SET CENTURY ON
SET DATE TO DMY
SET FIXED OFF
SET DECIMALS TO 6
_SCREEN.Visible=.F.


IF EMPTY(cBdd)
	cBdd='fss1'
ENDIF


*** Clases para el manejo de Web
SET PATH TO ".\classes" ADDITIVE
SET PROCEDURE TO wwHTTP ADDITIVE   
SET PROCEDURE TO wwAPI ADDITIVE
SET PROCEDURE TO wwFTP ADDITIVE
SET PROCEDURE TO wwUtils ADDITIVE

DO app.prg WITH cBdd
oApp.loadGhost()

DO load_def.prg

oWeb = CREATEOBJECT ('VfacClass', cIdView,_nTipoScan)

oWeb.run()


DEFINE CLASS VfacClass AS CUSTOM 

cMsgResult = ''

cIdView = ""
nTipoScan = '0'


FUNCTION init (cIdView,_nTipoScan) 
	
	IF !EMPTY(cIdView)
		this.cIdView = cIdView
	ENDIF
	
	IF !EMPTY(_nTipoScan)
		this.nTipoScan = TRANSFORM(_nTipoScan)
	ENDIF
	

ENDFUNC	

FUNCTION run()

	
	this.actualizar ()
		
	IF This.nTipoScan='0'
		this.cmdProc ()
	ENDIF

	oApp.setView (oWeb.cIdView,2)
	oApp.setSetup('Ghost_lwebBuzy',.F.,'L')
	
	oApp.closeDB()

ENDFUNC



FUNCTION subir
PARAMETERS nToken, nRecs, ltest

	lRunSQL = .T.
	IF !EMPTY(nRecs)
		lRunSQL = .F.
	ENDIF
	

	nSeconds = aDef[nToken,1]
	cTabla   = aDef[nToken,2]
	cCampos  = aDef[nToken,3]
	cMsg 	 = aDef[nToken,4]
	cWhere0	 = aDef[nToken,5]
	
	
	IF aDef[nToken,1] = 0
		aDef[nToken,1] = SECONDS()

		cWhere = ' WHERE !ya '+IIF(EMPTY(cWhere0),'',' and '+cWhere0)
		
		IF tk_stock=nToken
			cWhere = ' WHERE ya=3 '+IIF(EMPTY(cWhere0),'',' and '+cWhere0)
		ENDIF

		IF VAL(this.nTipoScan)=1
			cWhere = IIF(EMPTY(cWhere0),'',' WHERE '+cWhere0)
		ENDIF


		IF lRunSQL && 0 y 1 generan consulta generica, 2 la consulta ya viene generada
			nRecs = oApp.SQLExec  ('SELECT '+cCampos+' FROM '+cTabla+cWhere,cTabla)
			oApp.SQLExec ('UPDATE '+cTabla+' SET ya=1 WHERE ya=0')
		ENDIF 
		
		IF nRecs>0	

			DIMENSION aCampos[1]
			nLen = oApp.stringToArray (cCampos,@aCampos,',')
				
			cCmd = ''
			

			iCount = 0

			SELECT &cTabla
			GO top

			SCAN all
				IF iCount>0
					cCmd = cCmd + '~'
				ENDIF		
				FOR ii = 1 TO nLen
					iCampo = aCampos[ii]
					cCmd = cCmd + this.fixstr(&iCampo)+IIF(ii<nLen,'|','')
				ENDFOR
				SELECT &cTabla
				iCount = iCount + 1
			ENDSCAN
			

			IF iCount>0 
				this.ftpsend('up_'+cTabla,cCmd, ltest)
				this.msg_add(aDef[nToken,4]+CHR(13)+' No. Reg. ' + allt(STR(iCount)) + '  en  ' + allt(STR(SECONDS()-aDef[nToken,1],10,2)) + ' seg')
			ENDIF
		ENDIF
		
		aDef[nToken,1] = 0 
	ENDIF

ENDFUNC



FUNCTION subir2
PARAMETERS nToken, nRecs, ltest

	lRunSQL = .T.
	IF !EMPTY(nRecs)
		lRunSQL = .F.
	ENDIF
	

	nSeconds = aDef[nToken,1]
	cTabla   = aDef[nToken,2]
	cCampos  = aDef[nToken,3]
	cMsg 	 = aDef[nToken,4]
	cWhere0	 = aDef[nToken,5]
	
	
	IF aDef[nToken,1] = 0
		aDef[nToken,1] = SECONDS()


		IF VAL(this.nTipoScan)=1
			cWhere = IIF(EMPTY(cWhere0),'',' WHERE '+cWhere0)
		ENDIF


		IF lRunSQL && 0 y 1 generan consulta generica, 2 la consulta ya viene generada
			nRecs = oApp.SQLExec  ('SELECT '+cCampos+' FROM '+cTabla+cWhere,cTabla)
		ENDIF 
		
		IF nRecs>0	

			DIMENSION aCampos[1]
			nLen = oApp.stringToArray (cCampos,@aCampos,',')
				
			cCmd = ''
			

			iCount = 0

			SELECT &cTabla
			GO top

			SCAN all
				IF iCount>0
					cCmd = cCmd + '~'
				ENDIF		
				FOR ii = 1 TO nLen
					iCampo = aCampos[ii]
					cCmd = cCmd + this.fixstr(&iCampo)+IIF(ii<nLen,'|','')
				ENDFOR
				SELECT &cTabla
				iCount = iCount + 1
			ENDSCAN
			

			IF iCount>0 
				this.ftpsend('up_'+cTabla,cCmd, ltest)
				this.msg_add(aDef[nToken,4]+CHR(13)+' No. Reg. ' + allt(STR(iCount)) + '  en  ' + allt(STR(SECONDS()-aDef[nToken,1],10,2)) + ' seg')
			ENDIF
		ENDIF
		
		aDef[nToken,1] = 0 
	ENDIF

ENDFUNC



FUNCTION cmdProc ()

	cCmd = oApp.dolink ("./web/cmd_estado_0.php")
	
	DIMENSION aCmd[1]
	
	nCmd = oApp.explode(cCmd ,@aCmd,'~')
	
	FOR iCmd = 1 TO nCmd
		DIMENSION aCmdi[1]
		cCmdi = oApp.str2array(aCmd[iCmd], @aCmdi, '|')

		_cmd 		= aCmdi[1]
		cIdSalesman = aCmdi[2]
		cIdCust = aCmdi[3]
		cIdCmd 		= aCmdi[4]
		cAmbito		= aCmdi[5]
		cCmdTxt		= aCmdi[6]

		IF _cmd=='1'
			snota=''
			cResp = this.CXC(cIdSalesman,cIdCust,@snota)
			this.cmdResp (cIdCmd,cResp,snota)
		ENDIF
		
		
		IF _cmd=='7'
			snota=''
			DIMENSION aCmdTxt[1]

			oApp.explode(cCmdTxt, @aCmdTxt, '/')
			cResp = this.Setup(aCmdTxt[1],aCmdTxt[2],aCmdTxt[3],aCmdTxt[4])
			this.cmdResp (cIdCmd,cResp,cResp)
		ENDIF
		
		IF _cmd=='100'
			cResp = 'Actualizado centro de costo y producto.'
			this.Clone_CC_CP ()
			this.cmdResp (cIdCmd,cResp,cResp)
		ENDIF
		
		IF _cmd=='200'
			this.msg_add('Generando información financiera...')
			this.ContaGrupo(cCmdTxt,cIdCmd)
		ENDIF
		
		IF _cmd=='710'
			this.msg_add('Actualizando itinerario...')
			snota=''
			DIMENSION aCmdTxt[1]

			oApp.explode(cCmdTxt, @aCmdTxt, '/')
			cResp = this.up_itinerario(aCmdTxt[1],aCmdTxt[2],aCmdTxt[3],aCmdTxt[4])
			this.cmdResp (cIdCmd,cResp,cResp)
		ENDIF
		
	ENDFOR
ENDFUNC
	


FUNCTION cmdResp (cIdCmd,cResp,snota)
	
	cmdHttp = "cmd_estado_1.php?idcmd="+cIdCmd+"&resp="+cResp
	
	
	cCmd = oApp.doLink (cmdHttp)
	
	IF cCmd='ok' 
		IF !EMPTY(snota)
			this.msg_add(snota)
		ENDIF
	ENDIF
	
		
ENDFUNC


FUNCTION CXC (cIdSalesman,cIdCust,snota)

	TEXT TO cCmd NOSHOW	
	
		SELECT	A.reference,
			  	CAST(A.Date as char) as date, 
				CAST(A.Due as char) as due,
				CAST(ROUND(A.Total,2) as char) as total, 
				CAST(ROUND(A.Balance,2) as char) as balance, 
				CAST(A.Cuota as char) as cuota,
				CAST(ROUND(A.interes,2) as char) as interes,
				M.IdInvoice, 
				CAST(M.Moneda as char) as moneda, 
				A.Id AS IdCuotaDet, 
				M.Id AS IdCuota,
				C.Id as tipo,
				A.Cuenta,
				M.idsalesman,
				IF(ISNULL(i.numero),a.reference,i.numero) as sri,
				M.idchequesposf,
				A.total as nTotal,
				A.balance as nBalance
		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 
		WHERE M.IdCustomer = "@idcustomer" AND A.Balance != 0 AND !M.Void AND !M.agrupado 
		ORDER BY A.date, A.reference, A.Cuota 
		
	ENDTEXT
	
	cCmd = STRTRAN(cCmd,"@idcustomer",cIdCust)

	IF oApp.openDB()

		oApp.SQLExec (cCmd, 'curCxC')
		
		cRet = this.cur2ET ('curCxC','reference,date,due,total,balance,cuota,sri')
		nSaldo = 0

		cCliente = ''
		IF oApp.sqlexec('SELECT name FROM Customer WHERE id='+cIdCust,'curCliTmp')>0
			cCliente = ALLTRIM(curCliTmp.name)
		ENDIF
		
		cVend = ''
		IF oApp.sqlexec('SELECT name FROM Salesman WHERE id='+cIdSalesman,'curSalTmp')>0
			cVend = ALLTRIM(curSalTmp.name)
		ENDIF
		

		
		IF !EMPTY(cRet)
			SELECT SUM(nTotal) as nTotal, SUM(nBalance) as nBalance FROM curCxC INTO CURSOR Suma
			nSaldo = Suma.nBalance
		ENDIF
		
		snota = 'Cliente '+ cCliente +' Saldo $ '+;
		ALLTRIM(TRANSFORM(nSaldo,'999999999.99'))+ '  Vendedor: '+cVend

		
		SELECT curCxC
		USE
	ENDIF
	
	RETURN cRet

ENDFUNC

FUNCTION getIdApertura(cBdd)	
LOCAL cIdResult

	cIdResult = ''
	IF oApp.sqlexec('SELECT idApertura FROM '+cBdd+'.cierrecontable','curApertura')>0
		SELECT curApertura
		
		SCAN all
			cIdResult = cIdResult + idApertura +' '
		ENDSCAN		
	ENDIF
	RETURN cIdResult 
ENDFUNC

FUNCTION ContaGrupo (cYear,cIdCmd)


	IF EMPTY(cYear)
		cYear = PADL(YEAR(DATE()),4,'0')
	ENDIF

	oApp.setBDDSetup('fss1')
	cCurBDD = oApp.setup ("GHOST_BDD",'C')
	
	cRet = 0
	

	IF !EMPTY(cCurBDD)
	
		DIMENSION aBDD[1]
		
		nLen = oApp.stringToArray (cCurBDD, @aBDD)
		FOR iCount=1 TO nLen
			cBdd = UPPER(aBDD[iCount])

			TEXT TO cCmd NOSHOW	
			
				SELECT  concat("@bdd",SPACE(30)) as bdd, 
						concat(year(B.date),lpad(month(b.date),2,'0')) as periodo, 
						concat(P.code,SPACE(20)) as code, 
						concat(ucase(TRIM(P.name)),SPACE(200)) as name,
						concat(e.idcc+0,SPACE(10)) as idcc, 
						concat(e.idcp+0,SPACE(10)) as idcp, 
						concat(' ',round(sum(E.Debit-E.Credit),2)) as valor 
			      FROM @bdd.Entry AS E 
			      		INNER JOIN @bdd.BookEntry AS B ON E.id=B.Id 
			         	LEFT JOIN @bdd.Plan AS P ON E.IdPLan=P.Id 
			      WHERE B.state=1 AND B.void=0 
			         AND (B.date between "@dini" and "@dfin") 
		             AND instr("@cIdApertura",E.id)=0 
				  GROUP BY 1,2,3,4,5

			ENDTEXT

			cCmd = STRTRAN(cCmd,"@bdd", cBdd)
			cCmd = STRTRAN(cCmd,"@cIdApertura", this.getIdApertura(cBdd))
			cCmd = STRTRAN(cCmd,"@dini", cYear+"-01-01")
			cCmd = STRTRAN(cCmd,"@dfin", cYear+"-12-31")
			
			oApp.SQLExec (cCmd, cBdd)
	
		ENDFOR
		
		IF USED('curConta')
			SELECT curconta 
			use
		ENDIF
		
		
		cBdd = UPPER(aBDD[1])
		SELECT ALL * FROM &cBdd INTO CURSOR curConta READWRITE
		FOR iCount=2 TO nLen
			cBdd = UPPER(aBDD[iCount])
			SELECT curConta 
			INSERT INTO curConta SELECT ALL * FROM &cBdd
		ENDFOR 
	
		cRet = this.cur2ET ('curConta','bdd,periodo,code,name,idcc,idcp,valor')
		
	ENDIF

	SELECT curConta
	USE
	
	&&STRTOFILE(cRet,'xxx.txt')

	this.ftpsend ('up_fin',cRet)
	
	this.cmdResp (cIdCmd,'Ok','Información financiera generada correctamente')
	

ENDFUNC





FUNCTION cur2ET (curName, cvars)
	
	
	DIMENSION avars[1]
	nvars = oApp.str2array(cvars,@avars,',')
	
	sRet = ''
	SELECT &curName
	SCAN all
		IF !EMPTY(sRet)
			sRet = sRet + '~'
		ENDIF
		
		sLine = ''
		FOR i= 1 TO nvars
			IF !EMPTY(sLine)
				sLine = sLine + '|'
			ENDIF
			cCampo = avars[i]
			sLine = sLine + IIF(ISNULL(&cCampo),'',ALLTRIM(&cCampo))
		ENDFOR
		
		sRet = sRet + sLine 
	ENDSCAN
	
	RETURN sRet
ENDFUNC

FUNCTION update_table (cData)
	IF EMPTY(cData)
		RETURN .f.
	ENDIF
	
	strCabeza  = SUBSTR(cData,1,AT('~',cData)-1)
	
	strDetalle = SUBSTR(cData,  AT('~',cData)+1)
	
	
	DIMENSION aCabeza[1]
	oApp.str2array(strCabeza,@aCabeza,'|')
	
	&& centercost|id|C|name|C ...

	IF EMPTY(strCabeza)
		RETURN .f.
	ENDIF

	DIMENSION aDetalles[1],aDetalle[1]
	
	oApp.openDB()

	oApp.str2array(strDetalle,@aDetalles,'º')
	FOR iCount = 1 TO ALEN(aDetalles) 
	
	
		nCountArray = oApp.str2array(aDetalles[iCount],@aDetalle,'|')
		
		oApp.t_clear()
		FOR jCount = 1 TO ALEN(aDetalles) STEP 2
			oApp.t_set(aDetalle[jCount], aDetalle[jCount+1], aCabeza[jCount*2+1])
		ENDFOR
		
		IF oApp.SQLExec('SELECT '+aDetalle[1]+' FROM '+aCabeza[1])>0
			oApp.t_update(aCabeza[1],aDetalle[1])
		ELSE
			oApp.t_insert(aCabeza[1])
		ENDIF

	ENDFOR
	

ENDFUNC


FUNCTION Clone_CC_CP 

	oApp.setBDDSetup('fss1')
	cBDD_CC_Host = oApp.setup('GHOST_BDD_CC_HOST','C')
	
	oApp.setBDDSetup('fss1')
	cBDD_CP_Host = oApp.setup('GHOST_BDD_CP_HOST','C')
	
	this.cloneTableGrp(cBDD_CC_Host ,'centercost')
	this.cloneTableGrp(cBDD_CP_Host ,'contprod')

ENDFUNC



FUNCTION cloneTableGrp
PARAMETERS cBDDHost, cTabla

IF !EMPTY(cBDDHost) AND !EMPTY(cTabla)
	oApp.setBDDSetup('fss1')
	cCurBDD = oApp.setup ("GHOST_BDD",'C')

	IF !EMPTY(cCurBDD)
		DIMENSION aBDD[1]
		nLen = oApp.stringToArray (cCurBDD, @aBDD)
		FOR iCount=1 TO nLen
			iBDD = UPPER(aBDD[iCount])
			IF cBDDHost!=iBDD 
				oApp.sqlexec('DELETE FROM '+iBDD+'.'+cTabla)
				oApp.sqlexec('INSERT INTO '+iBDD+'.'+cTabla+' SELECT * FROM '+cBDDHost+'.'+cTabla)
			ENDIF		
		ENDFOR
	ENDIF
ENDIF


ENDFUNC


FUNCTION ftpsend(cUpCmd, cCmd, lTest)
	

	cZipFileName = SYS(2015)+'.zip'
	cTxtFileName = SYS(2015)+'.txt'
	
	IF lTest 
		cZipFileName = 'eddy1.zip'
		cTxtFileName = 'eddy1.txt'
	ENDIF
	

	STRTOFILE(cCmd,cTxtFileName)

	cmdZip =  '7z a -tzip -mx=9 '+cZipFileName+' '+cTxtFileName
	RUN &cmdZip
	*oShell.Run(cmdZip)
	
	lRetVal = .T.

	IF oIp.FTPSendFile (oApp.FtpHost,;
	            cZipFileName,;
	            cZipFileName,;
	            oApp.FtpUser,;
	            oApp.FtpPswd)=0
	            
	    cmdHttp = "./web/"+cUpCmd+".php?path="+cZipFileName
	    
	    IF lTest 
	    	cmdHttp = "./web/"+cUpCmd+"2.php?path="+cZipFileName
	    	MESSAGEBOX(cmdHttp )
	    ELSE
	    	oApp.dolink (cmdHttp)
	    ENDIF
	ELSE
		lRetVal = .f.
		this.msg_add('Error ftp: '+oIp.cErrorMsg)
	ENDIF

	IF EMPTY(lTest) 
		DELETE FILE (cZipFileName)
		DELETE FILE(cTxtFileName)
	ENDIF

	RETURN lRetVal 

ENDFUNC

FUNCTION msg_add (cMsg)
	cMsg = 'Ghost Web... ' + CHR(13) + cMsg
	IF EMPTY(this.cIdView)
		WAIT WINDOW cMsg nowait
	ELSE
		oApp.msg_add(this.cIdView, cMsg)
	ENDIF
	
ENDFUNC


FUNCTION actualizar ()

	IF this.nTipoScan='1'
		this.msg_add('Limpiando todo...')
		oApp.dolink('./web/drop_all.php')
		oApp.dolink('./web/make_all.php')
	ENDIF

*	this.subir(tk_item,,.t.)
	
	this.subir(tk_item)
	IF oApp.lbodegas
		this.control_stock_bod()
	ELSE
		this.subir(tk_stock)
	ENDIF
	this.subir(tk_grupo)
	this.subir(tk_brand)
	this.subir(tk_customer)	
	this.subir(tk_supplier)
	this.subir(tk_salesman)
	this.subir(tk_zonas)
	this.subir(tk_bodega)
	this.subir(tk_centercost)

	this.subir(tk_contprod)
	this.subir(tk_usuario)
	this.subir(tk_plan)
	this.subir(tk_bank)
	this.subir(tk_tipoventa)
	
	IF oApp.lBoletos 
		this.subir(tk_itinerario)
		
		IF this.nTipoScan='1'
			this.subir2(tk_usergrupo)
		ENDIF
	
	ENDIF

ENDFUNC

FUNCTION control_stock_bod
		
	&&this.msg_add('Cargando stock general de bodegas...')
	
	cCondItem = ' WHERE stock.ya=3 '

	IF This.nTipoScan='1'
		cCondItem = '  '
	ENDIF


	nToken = tk_ctrl_stock_bod

	cTabla   = aDef[nToken,2]

	TEXT TO cCmd NOSHOW	
		select i.id as iditem	
			 ,if(isnull(stock.stock), 00000000.00, stock.stock) as stock
			 ,if(isnull(b00.cantidad), 00000000.00, b00.cantidad) as stk00
			 ,if(isnull(b01.cantidad), 00000000.00, b01.cantidad) as stk01
			 ,if(isnull(b02.cantidad), 00000000.00, b02.cantidad) as stk02
			 ,if(isnull(b03.cantidad), 00000000.00, b03.cantidad) as stk03
			 ,if(isnull(b04.cantidad), 00000000.00, b04.cantidad) as stk04
			 ,if(isnull(b05.cantidad), 00000000.00, b05.cantidad) as stk05
			 ,if(isnull(b06.cantidad), 00000000.00, b06.cantidad) as stk06
			 ,if(isnull(b07.cantidad), 00000000.00, b07.cantidad) as stk07
			 ,if(isnull(b08.cantidad), 00000000.00, b08.cantidad) as stk08
			 ,if(isnull(b09.cantidad), 00000000.00, b09.cantidad) as stk09
			 ,if(isnull(b10.cantidad), 00000000.00, b10.cantidad) as stk10
			 ,if(isnull(b11.cantidad), 00000000.00, b11.cantidad) as stk11
			 ,if(isnull(b12.cantidad), 00000000.00, b12.cantidad) as stk12
			 ,if(isnull(b13.cantidad), 00000000.00, b13.cantidad) as stk13
			 ,if(isnull(b14.cantidad), 00000000.00, b14.cantidad) as stk14
			 ,if(isnull(b15.cantidad), 00000000.00, b15.cantidad) as stk15
			 ,if(isnull(b16.cantidad), 00000000.00, b16.cantidad) as stk16
			 ,if(isnull(b17.cantidad), 00000000.00, b17.cantidad) as stk17
			 ,if(isnull(b18.cantidad), 00000000.00, b18.cantidad) as stk18
			 ,if(isnull(b19.cantidad), 00000000.00, b19.cantidad) as stk19
			 ,if(isnull(b20.cantidad), 00000000.00, b20.cantidad) as stk20
			 ,if(isnull(b21.cantidad), 00000000.00, b21.cantidad) as stk21
			 ,if(isnull(b22.cantidad), 00000000.00, b22.cantidad) as stk22
			 ,if(isnull(b23.cantidad), 00000000.00, b23.cantidad) as stk23
			 ,if(isnull(b24.cantidad), 00000000.00, b24.cantidad) as stk24
			 ,if(isnull(b25.cantidad), 00000000.00, b25.cantidad) as stk25
			 ,if(isnull(b26.cantidad), 00000000.00, b26.cantidad) as stk26
			 ,if(isnull(b27.cantidad), 00000000.00, b27.cantidad) as stk27
			 ,if(isnull(b28.cantidad), 00000000.00, b28.cantidad) as stk28
			 ,if(isnull(b29.cantidad), 00000000.00, b29.cantidad) as stk29
			 ,if(isnull(b30.cantidad), 00000000.00, b30.cantidad) as stk30
			 ,if(isnull(b31.cantidad), 00000000.00, b31.cantidad) as stk31
			 ,if(isnull(b32.cantidad), 00000000.00, b32.cantidad) as stk32
			 ,if(isnull(b33.cantidad), 00000000.00, b33.cantidad) as stk33
			 ,if(isnull(b34.cantidad), 00000000.00, b34.cantidad) as stk34
			 ,if(isnull(b35.cantidad), 00000000.00, b35.cantidad) as stk35
			 ,if(isnull(b36.cantidad), 00000000.00, b36.cantidad) as stk36
			 ,if(isnull(b37.cantidad), 00000000.00, b37.cantidad) as stk37
			 ,if(isnull(b38.cantidad), 00000000.00, b38.cantidad) as stk38
			 ,if(isnull(b39.cantidad), 00000000.00, b39.cantidad) as stk39
			 ,if(isnull(b40.cantidad), 00000000.00, b40.cantidad) as stk40
			 ,if(isnull(b41.cantidad), 00000000.00, b41.cantidad) as stk41
			 ,if(isnull(b42.cantidad), 00000000.00, b42.cantidad) as stk42
			 ,if(isnull(b43.cantidad), 00000000.00, b43.cantidad) as stk43
			 ,if(isnull(b44.cantidad), 00000000.00, b44.cantidad) as stk44
			 ,if(isnull(b45.cantidad), 00000000.00, b45.cantidad) as stk45
			 ,if(isnull(b46.cantidad), 00000000.00, b46.cantidad) as stk46
			 ,if(isnull(b47.cantidad), 00000000.00, b47.cantidad) as stk47
			 ,if(isnull(b48.cantidad), 00000000.00, b48.cantidad) as stk48
			 ,if(isnull(b49.cantidad), 00000000.00, b49.cantidad) as stk49
			 ,if(isnull(b50.cantidad), 00000000.00, b50.cantidad) as stk50	 
			 ,if(isnull(b51.cantidad), 00000000.00, b51.cantidad) as stk51
			 ,if(isnull(b52.cantidad), 00000000.00, b52.cantidad) as stk52
			 ,if(isnull(b53.cantidad), 00000000.00, b53.cantidad) as stk53
			 ,if(isnull(b54.cantidad), 00000000.00, b54.cantidad) as stk54
			 ,if(isnull(b55.cantidad), 00000000.00, b55.cantidad) as stk55
			 ,if(isnull(b56.cantidad), 00000000.00, b56.cantidad) as stk56
			 ,if(isnull(b57.cantidad), 00000000.00, b57.cantidad) as stk57
			 ,if(isnull(b58.cantidad), 00000000.00, b58.cantidad) as stk58
		from item as i
			left join stock           on i.id=stock.iditem
			left join stockbod as b00 on b00.idbodega=00 and i.id=b00.iditem
			left join stockbod as b01 on b01.idbodega=01 and i.id=b01.iditem
			left join stockbod as b02 on b02.idbodega=02 and i.id=b02.iditem
			left join stockbod as b03 on b03.idbodega=03 and i.id=b03.iditem
			left join stockbod as b04 on b04.idbodega=04 and i.id=b04.iditem
			left join stockbod as b05 on b05.idbodega=05 and i.id=b05.iditem
			left join stockbod as b06 on b06.idbodega=06 and i.id=b06.iditem
			left join stockbod as b07 on b07.idbodega=07 and i.id=b07.iditem
			left join stockbod as b08 on b08.idbodega=08 and i.id=b08.iditem
			left join stockbod as b09 on b09.idbodega=09 and i.id=b09.iditem
			left join stockbod as b10 on b10.idbodega=10 and i.id=b10.iditem
			left join stockbod as b11 on b11.idbodega=11 and i.id=b11.iditem
			left join stockbod as b12 on b12.idbodega=12 and i.id=b12.iditem
			left join stockbod as b13 on b13.idbodega=13 and i.id=b13.iditem
			left join stockbod as b14 on b14.idbodega=14 and i.id=b14.iditem
			left join stockbod as b15 on b15.idbodega=15 and i.id=b15.iditem
			left join stockbod as b16 on b16.idbodega=16 and i.id=b16.iditem
			left join stockbod as b17 on b17.idbodega=17 and i.id=b17.iditem
			left join stockbod as b18 on b18.idbodega=18 and i.id=b18.iditem
			left join stockbod as b19 on b19.idbodega=19 and i.id=b19.iditem
			left join stockbod as b20 on b20.idbodega=20 and i.id=b20.iditem
			left join stockbod as b21 on b21.idbodega=21 and i.id=b21.iditem
			left join stockbod as b22 on b22.idbodega=22 and i.id=b22.iditem
			left join stockbod as b23 on b23.idbodega=23 and i.id=b23.iditem
			left join stockbod as b24 on b24.idbodega=24 and i.id=b24.iditem
			left join stockbod as b25 on b25.idbodega=25 and i.id=b25.iditem		
			left join stockbod as b26 on b26.idbodega=26 and i.id=b26.iditem
			left join stockbod as b27 on b27.idbodega=27 and i.id=b27.iditem
			left join stockbod as b28 on b28.idbodega=28 and i.id=b28.iditem
			left join stockbod as b29 on b29.idbodega=29 and i.id=b29.iditem
			left join stockbod as b30 on b30.idbodega=30 and i.id=b30.iditem
			left join stockbod as b31 on b31.idbodega=31 and i.id=b31.iditem
			left join stockbod as b32 on b32.idbodega=32 and i.id=b32.iditem
			left join stockbod as b33 on b33.idbodega=33 and i.id=b33.iditem
			left join stockbod as b34 on b34.idbodega=34 and i.id=b34.iditem
			left join stockbod as b35 on b35.idbodega=35 and i.id=b35.iditem
			left join stockbod as b36 on b36.idbodega=36 and i.id=b36.iditem
			left join stockbod as b37 on b37.idbodega=37 and i.id=b37.iditem
			left join stockbod as b38 on b38.idbodega=38 and i.id=b38.iditem
			left join stockbod as b39 on b39.idbodega=39 and i.id=b39.iditem
			left join stockbod as b40 on b40.idbodega=40 and i.id=b40.iditem
			left join stockbod as b41 on b41.idbodega=41 and i.id=b41.iditem
			left join stockbod as b42 on b42.idbodega=42 and i.id=b42.iditem
			left join stockbod as b43 on b43.idbodega=43 and i.id=b43.iditem
			left join stockbod as b44 on b44.idbodega=44 and i.id=b44.iditem
			left join stockbod as b45 on b45.idbodega=45 and i.id=b45.iditem
			left join stockbod as b46 on b46.idbodega=46 and i.id=b46.iditem
			left join stockbod as b47 on b47.idbodega=47 and i.id=b47.iditem
			left join stockbod as b48 on b48.idbodega=48 and i.id=b48.iditem
			left join stockbod as b49 on b49.idbodega=49 and i.id=b49.iditem
			left join stockbod as b50 on b50.idbodega=50 and i.id=b50.iditem
			left join stockbod as b51 on b51.idbodega=51 and i.id=b51.iditem
			left join stockbod as b52 on b52.idbodega=52 and i.id=b52.iditem
			left join stockbod as b53 on b53.idbodega=53 and i.id=b53.iditem
			left join stockbod as b54 on b54.idbodega=54 and i.id=b54.iditem
			left join stockbod as b55 on b55.idbodega=55 and i.id=b55.iditem 
			left join stockbod as b56 on b56.idbodega=56 and i.id=b56.iditem
			left join stockbod as b57 on b57.idbodega=57 and i.id=b57.iditem
			left join stockbod as b58 on b58.idbodega=58 and i.id=b58.iditem
		
	ENDTEXT


	nRecs = oApp.SQLExec(cCmd+cCondItem,cTabla)

	IF nRecs>0
		
		
		IF This.nTipoScan='1'
			oApp.SQLExec('UPDATE stock set ya=1 ')
		ELSE 
		
			SELECT &cTabla
			GO top
			SCAN all
				cCmd = 'UPDATE stock set ya=1 where iditem="'+&cTabla..iditem+'"'
				oApp.SQLExec(cCmd)
				SELECT &cTabla
			ENDSCAN
		ENDIF
		
		this.subir(nToken, nRecs) && 2 = mantiene la consulta generada
	ENDIF


ENDFUNC


FUNCTION fixStr(sVal)
	IF ISNULL(sval)
		RETURN ""
	ENDIF

	IF vartype(sval)='N'
		sval = transform(sVal)
	ENDIF
	
	IF vartype(sval)='D'
		sval = DTOC(sVal,1)
	ENDIF

	RETURN UPPER(ALLTRIM(sVal))
ENDFUNC




ENDDEFINE
