Files
MBS/WEBSERVICES/woocommerce/dcategorias.4gl
T

135 lines
4.0 KiB
Plaintext

IMPORT util
IMPORT com
IMPORT os
IMPORT security
TYPE t_sync_cmd RECORD
oper STRING,
data STRING
END RECORD
DEFINE s, cmd,usuario,clave STRING
MAIN
CONNECT TO 'smarmotech' USER 'jsoto' USING 'lmmjvsd2014'
CALL categorias()
END MAIN
FUNCTION categorias()
DEFINE metodo,url,selec,ret,res STRING,
categoria DYNAMIC ARRAY OF RECORD
id INT,
NAME,
slug ,
parent,
description,
DISPLAY STRING,
IMAGE STRING,
menu_order INT,
COUNT INT,
links STRING,
self STRING,
collection STRING,
UP STRING
END RECORD
,
http_req com.HTTPRequest,
http_resp com.HTTPResponse,
data t_sync_cmd,
idx,idfamilia INT
LET url = 'https://shop.marmotech.com.do/wp-json/wc/v3/products/categories'
LET selec = "SELECT id_familia FROM familia WHERE descripcion like ? "
PREPARE buscaf FROM selec
LET selec = "UPDATE familia set parentcode = ?,code=? WHERE id_familia = ? "
PREPARE familia FROM selec
LET selec = "UPDATE categorias set code = ? where descripcion_esp = ? "
PREPARE dcategoria FROM selec
# LET data.oper = 'products/categories'
# LET data.data = util.JSON.stringify(categoria)
# LET cmd= util.JSON.stringify(data)
LET cmd = url CLIPPED,"?per_page=45"
# DISPLAY "cmd ",cmd
CALL sync_send_command(url,cmd) RETURNING ret,res
# DISPLAY "RESPUESTA ",ret," ",util.JSON.stringify(res)
CALL util.JSON.parse(res,categoria)
# DISPLAY "PARSEADA ",categoria.getLength()
FOR idx = 1 TO categoria.getLength()
LET categoria[idx].name = categoria[idx].name CLIPPED,'%'
DISPLAY "BUSCANDO LA CAT ",categoria[idx].NAME
EXECUTE buscaf INTO idfamilia USING categoria[idx].NAME
IF STATUS !=NOTFOUND THEN
EXECUTE familia USING categoria[idx].parent,categoria[idx].id, idfamilia
ELSE
DISPLAY "NO ENCONTRE LA CAT ",categoria[idx].NAME
END IF
# EXECUTE dcategoria USING categoria[idx].id,categoria[idx].name
# DISPLAY "familia ",categoria[idx].name," ID ",categoria[idx].ID, " parent ",categoria[idx].parent
END FOR
END FUNCTION
FUNCTION sync_send_command(url, cmd)
DEFINE url, cmd STRING
DEFINE result, tmp STRING
DEFINE http_req com.HTTPRequest
DEFINE http_resp com.HTTPResponse
LET usuario = "ck_f41ca9ba9ec888d82243a7c24717af625c26f834"
LET clave = "cs_dbd762cb45184e3badc5a114b8f4a30c19f114fc"
TRY LET http_req = com.HTTPRequest.Create(cmd)
CALL http_req.setAuthentication(usuario,clave,"Basic",NULL)
CALL http_req.setKeepConnection(TRUE)
# CALL http_req.setConnectionTimeout(40)
# CALL http_req.setTimeout(40)
CALL http_req.setMethod("GET")
CALL http_req.setCharset("UTF-8")
# CALL http_req.doTextRequest(cmd)
CALL http_req.doRequest()
LET http_resp=http_req.getResponse()
IF http_resp.getStatusCode() != 200 THEN
RETURN http_resp.getStatusCode(), http_resp.getStatusDescription()
END IF
LET tmp = http_resp.getStatusDescription()
IF (tmp == "OK" || tmp == "no error" ) THEN
RETURN -1, SFMT("HTTP request status description: %1 ",tmp)
END IF
LET result = http_resp.getTextResponse()
CATCH
RETURN -2, SFMT("HTTP request error: STATUS=%1 (%2)",STATUS,SQLCA.SQLERRM)
END TRY
RETURN 0, result
END FUNCTION
FUNCTION show_sync_err(msg)
DEFINE msg STRING
CALL fgl_winmessage(%"ERROR", msg, "exclamation")
END FUNCTION
FUNCTION b64_to_file(str_b64,f_sal)
DEFINE f_sal,str_b64,str_aux STRING
TRY
CALL security.Base64.SaveBinary(f_sal,str_b64)
CATCH
DISPLAY " data ",str_b64
LET str_aux="Unable to convert Base64 String to file :",STATUS
CALL fgl_winmessage("Error",str_aux,"stop")
END TRY
END FUNCTION