From 03b0b2c4c37ac4bab3a4cb85a9b397494517a573 Mon Sep 17 00:00:00 2001 From: rodley82 Date: Mon, 24 Nov 2025 00:12:59 -0300 Subject: [PATCH] working --- afip_ingresar_datos_emision.lua | 183 ++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 afip_ingresar_datos_emision.lua diff --git a/afip_ingresar_datos_emision.lua b/afip_ingresar_datos_emision.lua new file mode 100644 index 0000000..54c70e1 --- /dev/null +++ b/afip_ingresar_datos_emision.lua @@ -0,0 +1,183 @@ +--[[ +{ + "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_servicios(tipo_concepto) + if tipo_concepto == "servicios" or tipo_concepto == "servicio" then + return true + end + return false +end + +local function is_productos_y_servicios(tipo_concepto) + if tipo_concepto == "productos y servicios" or tipo_concepto == "productos servicios" then + return true + end + return false +end + +local function is_productos(tipo_concepto) + if tipo_concepto == "productos" or tipo_concepto == "producto" then + return true + end + return false +end + +-- Map "tipo_concepto" (as text) to the numeric value expected by AFIP in +-- the "idconcepto" select. +-- Mirrors TipoConceptoFromString in Go (internal/robot/tipos.go). +local function mapTipoConcepto(tipo) + if is_productos(tipo) then + return "1" -- TipoConceptoProductos + end + if is_servicios(tipo) then + return "2" -- TipoConceptoServicios + end + if is_productos_y_servicios(tipo) then + return "3" -- TipoConceptoProductosYServicios + end + + return "" +end + +local function conceptoRequierePeriodoServicio(tipo) + if is_servicios(tipo) or is_productos_y_servicios(tipo) then + return true + end + + return false +end + +---------------------------------------------------------------------- +-- Main logic +---------------------------------------------------------------------- + +local function main() + log("afip_ingresar_datos_emision: starting") + + local payload, err = json.decode(json_payload) + log("afip_ingresar_datos_emision: decoded payload: " .. json.encode(payload)) + if err then + logErrorAndExit("afip_ingresar_datos_emision: error decoding payload: " .. err) + end + + -------------------------------------------------------------------- + -- Extract fields from payload + -------------------------------------------------------------------- + local fechaComprobante = payload["fecha_comprobante"] or "" + local tipoConceptoTexto = payload["tipo_concepto"] or "" + local servicioFechaDesde = payload["servicio_fecha_desde"] or "" + local servicioFechaHasta = payload["servicio_fecha_hasta"] or "" + local servicioFechaVencimiento= payload["servicio_fecha_vencimiento_pago"] or "" + local referencia = payload["referencia"] or "" + + if fechaComprobante == "" then + logErrorAndExit("afip_ingresar_datos_emision: fecha_comprobante is empty") + end + if tipoConceptoTexto == "" then + logErrorAndExit("afip_ingresar_datos_emision: tipo_concepto is empty") + end + + local conceptoValue = mapTipoConcepto(tipoConceptoTexto) + if conceptoValue == "" then + logErrorAndExit("afip_ingresar_datos_emision: tipo_concepto value not recognized: " .. tostring(tipoConceptoTexto)) + end + + -------------------------------------------------------------------- + -- 1) Fecha de comprobante y concepto (ids: fc, idconcepto) + -------------------------------------------------------------------- + local fechaComprobanteId = "fc" + local conceptoId = "idconcepto" + + err = setTextFieldById(fechaComprobanteId, tostring(fechaComprobante)) + if err then + logErrorAndExit("afip_ingresar_datos_emision: setTextFieldById for fechaComprobante failed: " .. err) + end + + err = setTextFieldById(conceptoId, tostring(conceptoValue)) + if err then + logErrorAndExit("afip_ingresar_datos_emision: setTextFieldById for concepto failed: " .. err) + end + + -------------------------------------------------------------------- + -- 2) Periodo de servicio y vencimiento de pago (solo servicios o + -- productos y servicios) + -------------------------------------------------------------------- + if conceptoRequierePeriodoServicio(tipoConceptoTexto) then + log("afip_ingresar_datos_emision: setting service period and due date") + + if servicioFechaDesde == "" or servicioFechaHasta == "" or servicioFechaVencimiento == "" then + logErrorAndExit("afip_ingresar_datos_emision: service period dates are required when tipo_concepto is 'servicios' or 'productos y servicios'") + end + + local fechaDesdeId = "fsd" + local fechaHastaId = "fsh" + local fechaVencimientoPagoId = "vencimientopago" + + err = setTextFieldById(fechaDesdeId, tostring(servicioFechaDesde)) + if err then + logErrorAndExit("afip_ingresar_datos_emision: setTextFieldById for fechaDesde failed: " .. err) + end + + err = setTextFieldById(fechaHastaId, tostring(servicioFechaHasta)) + if err then + logErrorAndExit("afip_ingresar_datos_emision: setTextFieldById for fechaHasta failed: " .. err) + end + + err = setTextFieldById(fechaVencimientoPagoId, tostring(servicioFechaVencimiento)) + if err then + logErrorAndExit("afip_ingresar_datos_emision: setTextFieldById for fechaVencimientoPago failed: " .. err) + end + else + log("afip_ingresar_datos_emision: tipo_concepto does not require service period: " .. tostring(tipoConceptoTexto)) + end + + -------------------------------------------------------------------- + -- 3) Referencia (id: refComEmisor) + -------------------------------------------------------------------- + local referenciaId = "refComEmisor" + + -- Even if referencia is empty, setting the field should be harmless and + -- mirrors the Go behavior that always calls SetValue. + err = setTextFieldById(referenciaId, tostring(referencia)) + if err then + logErrorAndExit("afip_ingresar_datos_emision: setTextFieldById for referencia failed: " .. err) + end + + -------------------------------------------------------------------- + -- Done + -------------------------------------------------------------------- + log("afip_ingresar_datos_emision: 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 datosDeEmision + logErrorAndExit("afip_ingresar_datos_emision failed: " .. tostring(err)) +end