1 changed files with 250 additions and 0 deletions
@ -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 |
||||
|
--[[ |
||||
|
// <option value="" selected="selected" style="color:#888;">seleccionar...</option> |
||||
|
// <option value="1"> IVA Responsable Inscripto</option> |
||||
|
// <option value="4"> IVA Sujeto Exento</option> |
||||
|
// <option value="5"> Consumidor Final</option> |
||||
|
// <option value="6"> Responsable Monotributo</option> |
||||
|
// <option value="7"> Sujeto No Categorizado</option> |
||||
|
// <option value="8"> Proveedor del Exterior</option> |
||||
|
// <option value="9"> Cliente del Exterior</option> |
||||
|
// <option value="10"> IVA Liberado - Ley Nº 19.640</option> |
||||
|
// <option value="13"> Monotributista Social</option> |
||||
|
// <option value="15"> IVA No Alcanzado</option> |
||||
|
// <option value="16"> 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_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) |
||||
|
--[[ |
||||
|
// <option value="" selected="selected" style="color:#888;">seleccionar...</option> |
||||
|
// <option value="1"> Contado</option> |
||||
|
// <option value="2"> Tarjeta de Debito</option> |
||||
|
// <option value="3"> Tarjeta de Credito</option> |
||||
|
// <option value="4"> Cuenta Corriente</option> |
||||
|
// <option value="5"> Cheque</option> |
||||
|
// <option value="6"> Ticket</option> |
||||
|
// <option value="7"> 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 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 |
||||
Loading…
Reference in new issue