You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
9.2 KiB

--[[
{
"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
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
-- Based on the values from the json returns the expected option value to select on the the UI
--[[
// <option value="" selected="selected" style="color:#888;">seleccionar...</option>
// <option value="1">&nbsp;IVA Responsable Inscripto</option>
// <option value="4">&nbsp;IVA Sujeto Exento</option>
// <option value="5">&nbsp;Consumidor Final</option>
// <option value="6">&nbsp;Responsable Monotributo</option>
// <option value="7">&nbsp;Sujeto No Categorizado</option>
// <option value="8">&nbsp;Proveedor del Exterior</option>
// <option value="9">&nbsp;Cliente del Exterior</option>
// <option value="10">&nbsp;IVA Liberado - Ley Nº 19.640</option>
// <option value="13">&nbsp;Monotributista Social</option>
// <option value="15">&nbsp;IVA No Alcanzado</option>
// <option value="16">&nbsp;Monotributista Trabajador Independiente Promovido</option>
]]--
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_forma_pago_contado(forma)
return forma == "contado" or forma == "efectivo"
end
local function is_forma_pago_debito(forma)
return forma == "debito" or forma == "tarjeta de debito"
end
local function is_forma_pago_credito(forma)
return forma == "credito" or forma == "tarjeta de credito"
end
local function is_forma_pago_cuenta_corriente(forma)
return forma == "cuenta corriente" or forma == "cta corriente"
end
local function is_forma_pago_cheque(forma)
return forma == "cheque"
end
local function is_forma_pago_ticket(forma)
return forma == "ticket"
end
local function is_forma_pago_otra(forma)
return forma == "otra" or forma == "otro"
end
local function mapFormaPago(forma)
--[[
// <option value="" selected="selected" style="color:#888;">seleccionar...</option>
// <option value="1">&nbsp;Contado</option>
// <option value="2">&nbsp;Tarjeta de Debito</option>
// <option value="3">&nbsp;Tarjeta de Credito</option>
// <option value="4">&nbsp;Cuenta Corriente</option>
// <option value="5">&nbsp;Cheque</option>
// <option value="6">&nbsp;Ticket</option>
// <option value="7">&nbsp;Otra</option>
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 is_forma_pago_contado(forma) then
return "1"
end
if is_forma_pago_debito(forma) then
return "2"
end
if is_forma_pago_credito(forma) then
return "3"
end
if is_forma_pago_cuenta_corriente(forma) then
return "4"
end
if is_forma_pago_cheque(forma) then
return "5"
end
if is_forma_pago_ticket(forma) then
return "6"
end
if is_forma_pago_otra(forma) 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