From 7274540084314286276cdbf1d53a93f4fbb91c22 Mon Sep 17 00:00:00 2001 From: rodley82 Date: Sun, 23 Nov 2025 23:15:31 -0300 Subject: [PATCH] version funcionando --- afip_ingresar_datos_receptor.lua | 250 +++++++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 afip_ingresar_datos_receptor.lua diff --git a/afip_ingresar_datos_receptor.lua b/afip_ingresar_datos_receptor.lua new file mode 100644 index 0000000..986b614 --- /dev/null +++ b/afip_ingresar_datos_receptor.lua @@ -0,0 +1,250 @@ +--[[ +{ + "uid": "53935f27-5cf0-4281-9a29-a68f100ee981", + "execution_status": "Borrador", + "tipo_comprobante": "factura c", + "fecha_comprobante": "13/11/2025", + "tipo_concepto": "servicios", + "servicio_fecha_desde": "01/10/2025", + "servicio_fecha_hasta": "01/10/2025", + "servicio_fecha_vencimiento_pago": "13/11/2025", + "referencia": "", + "tipo_iva_receptor": "consumidor final", + "cuit_receptor": "", + "forma_pago": "contado", + "monto_unitario": "10000", + "cantidad": "1", + "descripcion": "Clase de apoyo" +} +--]] + +local json = require("json") + +---------------------------------------------------------------------- +-- Helpers +---------------------------------------------------------------------- +local function logErrorAndExit(message) + log(message) + error(message) +end + +-- Based on the values from the json returns the expected option value to select on the the UI +--[[ +// +// +// +// +// +// +// +// +// +// +// +// +]]-- +local function mapTipoIvaReceptor(tipo) + if is_consumidor_final(tipo) then + return "5" + end + if is_responsable_inscripto(tipo) then + return "1" + end + if is_responsable_monotributo(tipo) then + return "6" + end + return "" +end + +local function is_consumidor_final(tipo) + return tipo == "consumidor final" or tipo == "final" +end + +local function is_responsable_inscripto(tipo) + return tipo == "responsable inscripto" or tipo == "responsable" +end + +local function is_responsable_monotributo(tipo) + return tipo == "responsable monotributo" or tipo == "monotributo" or tipo == "monotributista" +end + +local function mapFormaPago(forma) + --[[ + // + // + // + // + // + // + // + // + + FormaPagoInvalido = 0 + FormaPagoContado = 1 + FormaPagoTarjetaDebito = 2 + FormaPagoTarjetaCredito = 3 + FormaPagoCuentaCorriente = 4 + FormaPagoCheque = 5 + FormaPagoTicket = 6 + FormaPagoOtra = 7 + + case "contado", "efectivo": + return FormaPagoContado, nil + case "debito", "tarjeta de debito": + return FormaPagoTarjetaDebito, nil + case "credito", "tarjeta de credito": + return FormaPagoTarjetaCredito, nil + case "cuenta corriente", "cta corriente": + return FormaPagoCuentaCorriente, nil + case "cheque": + return FormaPagoCheque, nil + case "ticket": + return FormaPagoTicket, nil + case "otra", "otro": + return FormaPagoOtra, nil + ]]-- + + if forma == "contado" or forma == "efectivo" then + return "1" + end + if forma == "debito" or forma == "tarjeta de debito" then + return "2" + end + if forma == "credito" or forma == "tarjeta de credito" then + return "3" + end + if forma == "cuenta corriente" or forma == "cta corriente" then + return "4" + end + if forma == "cheque" then + return "5" + end + if forma == "ticket" then + return "6" + end + if forma == "otra" or forma == "otro" then + return "7" + end + return "" +end + +---------------------------------------------------------------------- +-- Main logic +---------------------------------------------------------------------- + +local function main() + log("afip_ingresar_datos_receptor: starting") + + local payload, err = json.decode(json_payload) + log("afip_ingresar_datos_receptor: decoded payload: " .. json.encode(payload)) + if err then + logErrorAndExit("afip_ingresar_datos_receptor: error decoding payload: " .. err) + end + + -------------------------------------------------------------------- + -- Extract fields from payload + -------------------------------------------------------------------- + local tipoIvaReceptor = payload["tipo_iva_receptor"] or "" + local cuitReceptor = payload["cuit_receptor"] or "" + local formaPago = payload["forma_pago"] or "" + + if tipoIvaReceptor == "" then + logErrorAndExit("afip_ingresar_datos_receptor: tipo_iva_receptor is empty") + end + if formaPago == "" then + logErrorAndExit("afip_ingresar_datos_receptor: forma_pago is empty") + end + + local ivaValue = mapTipoIvaReceptor(tipoIvaReceptor) + local formaValue = mapFormaPago(formaPago) + + -------------------------------------------------------------------- + -- 1) Select IVA del receptor (select#idivareceptor) + -------------------------------------------------------------------- + local ivaId = "idivareceptor" + err = setTextFieldById(ivaId, tostring(ivaValue)) + + if err then + logErrorAndExit("afip_ingresar_datos_receptor: set IVA failed: " .. err) + end + + log("afip_ingresar_datos_receptor: IVA receptor set to " .. tostring(ivaValue)) + + -------------------------------------------------------------------- + -- 2) Ingresar CUIT receptor si corresponde + -- (Go logic: only for some IVA types and when CUIT is non-empty) + -- Here we only check that CUIT is non-empty; if you want to restrict + -- by tipoIva, put that logic in requiresCuit(type). + -------------------------------------------------------------------- + + local function requiresCuit(tipo) + -- Should we check that CUIT is non-empty? + return is_responsable_inscripto(tipo) or is_responsable_monotributo(tipo) + end + + if requiresCuit(tipoIvaReceptor) then + local cuitSelectorId = "nrodocreceptor" + + err = waitVisibleById(cuitSelectorId) + if err then + logErrorAndExit("afip_ingresar_datos_receptor: waitVisible CUIT failed: " .. err) + end + + --ok, setErr = pcall(function() + -- -- Clear first, then set and send Tab (same idea as Go code: SetValue + Tab) + -- clearTextFieldByQuerySelector(cuitSelector) + -- setTextFieldByQuerySelector(cuitSelector, tostring(cuitReceptor)) + -- sendReturnKeyByQuerySelector(cuitSelector) -- or Tab if you add a binding; Enter usually also triggers validation + --end) + --if not ok then + -- error("afip_ingresar_datos_receptor: set CUIT failed: " .. tostring(setErr)) + --end + err = setTextFieldById(cuitSelectorId, tostring(cuitReceptor)) + if err then + logErrorAndExit("afip_ingresar_datos_receptor: setTextFieldById failed: " .. err) + end + + err = sendTabKeyById(cuitSelectorId) + if err then + logErrorAndExit("afip_ingresar_datos_receptor: sendTabKeyById failed: " .. err) + end + + log("afip_ingresar_datos_receptor: CUIT receptor set to " .. tostring(cuitReceptor)) + else + log("afip_ingresar_datos_receptor: CUIT not required for tipo_iva_receptor=" .. tostring(tipoIvaReceptor)) + end + + -------------------------------------------------------------------- + -- 3) Seleccionar forma de pago (input[id=formadepagoX]) + -------------------------------------------------------------------- + if formaValue ~= nil and formaValue ~= "" then + log("afip_ingresar_datos_receptor: seleccionando forma de pago " .. tostring(formaValue)) + local formaSelector = "#formadepago" .. tostring(formaValue) + + err = waitVisibleByQuerySelector(formaSelector) + + if err then + logErrorAndExit("afip_ingresar_datos_receptor: waitVisible formaPago failed: " .. tostring(waitErr)) + end + + err = clickElementByQuerySelector(formaSelector) + if err then + logErrorAndExit("afip_ingresar_datos_receptor: click formaPago failed: " .. tostring(setErr)) + end + + log("afip_ingresar_datos_receptor: forma_pago selected: " .. tostring(formaValue)) + else + log("afip_ingresar_datos_receptor: forma_pago is empty, skipping selection") + end + + -------------------------------------------------------------------- + -- Done + -------------------------------------------------------------------- + log("afip_ingresar_datos_receptor: completed successfully") +end + +local ok, err = pcall(main) +if not ok then + -- Let Go see this as a Lua error so it can fall back to legacy datosReceptor + logErrorAndExit("afip_ingresar_datos_receptor failed: " .. tostring(err)) +end