{
  "openapi": "3.0.3",
  "info": {
    "title": "API comercial de VenVenta",
    "version": "1.0.0",
    "description": "Consultas de productos, existencias y ventas. Requiere habilitar la API del negocio y usar una clave con los permisos correspondientes. No permite crear ni modificar operaciones."
  },
  "servers": [
    {
      "url": "https://app.venventa.com/api/integrations/commercial/v1",
      "description": "API comercial de VenVenta"
    }
  ],
  "security": [
    {
      "ApiKey": []
    }
  ],
  "paths": {
    "/products": {
      "get": {
        "operationId": "listProducts",
        "summary": "Consultar productos",
        "description": "Requiere el permiso catalog:read.",
        "parameters": [
          {
            "name": "search",
            "in": "query",
            "required": false,
            "description": "Busca una coincidencia parcial en nombre o código interno; sin distinguir mayúsculas.",
            "schema": {
              "type": "string",
              "maxLength": 240
            }
          },
          {
            "name": "page",
            "in": "query",
            "required": false,
            "description": "Página, empezando en cero.",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 100000,
              "default": 0
            }
          },
          {
            "name": "size",
            "in": "query",
            "required": false,
            "description": "Elementos por página.",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Consulta completada.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProductPage"
                }
              }
            }
          },
          "400": {
            "description": "Parámetros inválidos.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Clave inválida, vencida o revocada.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Permiso o acceso insuficiente.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Recurso no disponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Límite alcanzado.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "Segundos antes de reintentar.",
                "schema": {
                  "type": "integer",
                  "minimum": 1
                }
              }
            }
          },
          "500": {
            "description": "Error interno.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Servicio no disponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/products/{id}/stock": {
      "get": {
        "operationId": "getProductStock",
        "summary": "Consultar existencias",
        "description": "Requiere el permiso stock:read.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID del producto.",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Consulta completada.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Stock"
                }
              }
            }
          },
          "400": {
            "description": "Parámetros inválidos.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Clave inválida, vencida o revocada.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Permiso o acceso insuficiente.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Recurso no disponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Límite alcanzado.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "Segundos antes de reintentar.",
                "schema": {
                  "type": "integer",
                  "minimum": 1
                }
              }
            }
          },
          "500": {
            "description": "Error interno.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Servicio no disponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/sales": {
      "get": {
        "operationId": "listSales",
        "summary": "Consultar ventas",
        "description": "Requiere el permiso sales:read.",
        "parameters": [
          {
            "name": "dateFrom",
            "in": "query",
            "required": false,
            "description": "Fecha comercial inicial, inclusive.",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "dateTo",
            "in": "query",
            "required": false,
            "description": "Fecha comercial final, inclusive.",
            "schema": {
              "type": "string",
              "format": "date"
            }
          },
          {
            "name": "customerId",
            "in": "query",
            "required": false,
            "description": "ID de cliente.",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "status",
            "in": "query",
            "required": false,
            "description": "Sin filtro incluye registradas y canceladas; nunca borradores.",
            "schema": {
              "type": "string",
              "enum": [
                "REGISTERED",
                "CANCELLED"
              ]
            }
          },
          {
            "name": "collectionStatus",
            "in": "query",
            "required": false,
            "description": "Estado de cobro actual.",
            "schema": {
              "type": "string",
              "enum": [
                "PENDING",
                "PARTIAL",
                "PAID"
              ]
            }
          },
          {
            "name": "page",
            "in": "query",
            "required": false,
            "description": "Página, empezando en cero.",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 100000,
              "default": 0
            }
          },
          {
            "name": "size",
            "in": "query",
            "required": false,
            "description": "Elementos por página.",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Consulta completada.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SalePage"
                }
              }
            }
          },
          "400": {
            "description": "Parámetros inválidos.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Clave inválida, vencida o revocada.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Permiso o acceso insuficiente.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Recurso no disponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Límite alcanzado.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "Segundos antes de reintentar.",
                "schema": {
                  "type": "integer",
                  "minimum": 1
                }
              }
            }
          },
          "500": {
            "description": "Error interno.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Servicio no disponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/sales/{id}": {
      "get": {
        "operationId": "getSale",
        "summary": "Consultar detalle de una venta",
        "description": "Requiere el permiso sales:read.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "ID de venta.",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Consulta completada.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Sale"
                }
              }
            }
          },
          "400": {
            "description": "Parámetros inválidos.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Clave inválida, vencida o revocada.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Permiso o acceso insuficiente.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Recurso no disponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Límite alcanzado.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "Segundos antes de reintentar.",
                "schema": {
                  "type": "integer",
                  "minimum": 1
                }
              }
            }
          },
          "500": {
            "description": "Error interno.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Servicio no disponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Api-Key"
      }
    },
    "schemas": {
      "Product": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "description": "Identificador estable del producto o servicio.",
            "format": "uuid"
          },
          "internalCode": {
            "type": "string",
            "description": "Código interno del catálogo."
          },
          "barcode": {
            "type": "string",
            "description": "Código de barras, si está registrado.",
            "nullable": true
          },
          "description": {
            "type": "string",
            "description": "Nombre o descripción del producto."
          },
          "type": {
            "type": "string",
            "description": "Tipo de artículo.",
            "enum": [
              "PRODUCT",
              "SERVICE"
            ]
          },
          "unit": {
            "type": "string",
            "description": "Nombre de la unidad de medida.",
            "nullable": true
          },
          "quantityDecimalPlaces": {
            "type": "integer",
            "description": "Precisión configurada para cantidades en esa unidad.",
            "nullable": true
          },
          "price": {
            "type": "number",
            "description": "Precio base del catálogo; interpretar junto con pricesIncludeTaxes."
          },
          "currency": {
            "type": "string",
            "description": "Moneda del negocio, por ejemplo ARS."
          },
          "pricesIncludeTaxes": {
            "type": "boolean",
            "description": "Indica si el precio configurado incluye impuestos."
          },
          "variant": {
            "type": "object",
            "description": "Sólo presente en variantes. Cada combinación es un producto con ID, precio y stock propios.",
            "properties": {
              "groupId": {
                "type": "string",
                "format": "uuid"
              },
              "name": {
                "type": "string"
              },
              "size": {
                "type": "string",
                "nullable": true
              },
              "color": {
                "type": "string",
                "nullable": true
              }
            },
            "required": [
              "groupId",
              "name",
              "size",
              "color"
            ]
          }
        },
        "required": [
          "id",
          "internalCode",
          "description",
          "type",
          "price",
          "currency",
          "pricesIncludeTaxes"
        ]
      },
      "Stock": {
        "type": "object",
        "properties": {
          "productId": {
            "type": "string",
            "description": "Producto consultado.",
            "format": "uuid"
          },
          "tracked": {
            "type": "boolean",
            "description": "Indica si el negocio controla existencias del producto."
          },
          "onHand": {
            "type": "number",
            "description": "Existencia registrada en la ubicación principal. Nulo si no se controla stock.",
            "nullable": true
          },
          "queriedAt": {
            "type": "string",
            "description": "Instante de consulta en formato ISO 8601.",
            "format": "date-time"
          }
        },
        "required": [
          "productId",
          "tracked",
          "queriedAt"
        ]
      },
      "SaleItem": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "description": "Identificador del ítem de venta.",
            "format": "uuid"
          },
          "productServiceId": {
            "type": "string",
            "description": "Producto vinculado, si existe.",
            "nullable": true,
            "format": "uuid"
          },
          "unitOfMeasureId": {
            "type": "string",
            "description": "Unidad del producto vinculado, si existe.",
            "nullable": true,
            "format": "uuid"
          },
          "unitOfMeasureName": {
            "type": "string",
            "description": "Nombre de esa unidad.",
            "nullable": true
          },
          "description": {
            "type": "string",
            "description": "Descripción guardada en la venta."
          },
          "quantity": {
            "type": "number",
            "description": "Cantidad vendida."
          },
          "unitPrice": {
            "type": "number",
            "description": "Precio unitario guardado en la venta."
          },
          "discountRate": {
            "type": "number",
            "description": "Porcentaje de descuento del ítem."
          },
          "discountAmount": {
            "type": "number",
            "description": "Importe del descuento del ítem."
          },
          "taxRateId": {
            "type": "string",
            "description": "Impuesto vinculado, si existe.",
            "nullable": true,
            "format": "uuid"
          },
          "taxLabelSnapshot": {
            "type": "string",
            "description": "Nombre del impuesto guardado en la venta.",
            "nullable": true
          },
          "taxRateSnapshot": {
            "type": "number",
            "description": "Tasa de impuesto guardada en la venta.",
            "nullable": true
          },
          "lineSubtotal": {
            "type": "number",
            "description": "Subtotal de la línea calculado por VenVenta."
          },
          "lineTax": {
            "type": "number",
            "description": "Importe de impuestos calculado para la línea."
          },
          "lineTotal": {
            "type": "number",
            "description": "Total de la línea calculado por VenVenta."
          },
          "sortOrder": {
            "type": "integer",
            "description": "Posición del ítem.",
            "nullable": true
          }
        },
        "required": [
          "id",
          "description",
          "quantity",
          "unitPrice",
          "discountRate",
          "discountAmount",
          "lineSubtotal",
          "lineTax",
          "lineTotal"
        ]
      },
      "Sale": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "description": "Identificador estable de la venta.",
            "format": "uuid"
          },
          "internalNumber": {
            "type": "string",
            "description": "Número interno de la venta; no es un número de factura fiscal."
          },
          "customerId": {
            "type": "string",
            "description": "Cliente vinculado, si corresponde.",
            "nullable": true,
            "format": "uuid"
          },
          "customerDisplayName": {
            "type": "string",
            "description": "Nombre visible del cliente guardado en la venta.",
            "nullable": true
          },
          "date": {
            "type": "string",
            "description": "Fecha comercial de la venta.",
            "format": "date"
          },
          "dueDate": {
            "type": "string",
            "description": "Vencimiento comercial, si existe.",
            "nullable": true,
            "format": "date"
          },
          "status": {
            "type": "string",
            "description": "Estado comercial.",
            "enum": [
              "REGISTERED",
              "CANCELLED"
            ]
          },
          "currency": {
            "type": "string",
            "description": "Moneda de los importes de esta venta."
          },
          "subtotal": {
            "type": "number",
            "description": "Subtotal de la venta calculado por VenVenta."
          },
          "discountTotal": {
            "type": "number",
            "description": "Descuentos totales."
          },
          "taxTotal": {
            "type": "number",
            "description": "Impuestos totales."
          },
          "total": {
            "type": "number",
            "description": "Total comercial de la venta."
          },
          "collectionStatus": {
            "type": "string",
            "description": "Estado de cobro.",
            "enum": [
              "PENDING",
              "PARTIAL",
              "PAID"
            ]
          },
          "collectedTotal": {
            "type": "number",
            "description": "Cobrado acumulado actual de la venta."
          },
          "balance": {
            "type": "number",
            "description": "Saldo pendiente actual."
          },
          "pricesIncludeTaxes": {
            "type": "boolean",
            "description": "Indica cómo interpretar los precios unitarios de la venta."
          },
          "items": {
            "type": "array",
            "description": "Vacío en el listado. El detalle de la venta incluye sus ítems.",
            "items": {
              "$ref": "#/components/schemas/SaleItem"
            }
          }
        },
        "required": [
          "id",
          "internalNumber",
          "date",
          "status",
          "currency",
          "subtotal",
          "discountTotal",
          "taxTotal",
          "total",
          "collectionStatus",
          "collectedTotal",
          "balance",
          "pricesIncludeTaxes",
          "items"
        ]
      },
      "ProductPage": {
        "type": "object",
        "required": [
          "content",
          "page",
          "size",
          "totalElements",
          "totalPages"
        ],
        "properties": {
          "content": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Product"
            }
          },
          "page": {
            "type": "integer",
            "description": "Página actual; comienza en cero."
          },
          "size": {
            "type": "integer",
            "description": "Tamaño solicitado de página."
          },
          "totalElements": {
            "type": "integer",
            "description": "Cantidad total que coincide con los filtros.",
            "format": "int64"
          },
          "totalPages": {
            "type": "integer",
            "description": "Número total de páginas; cero para un resultado vacío."
          }
        }
      },
      "SalePage": {
        "type": "object",
        "required": [
          "content",
          "page",
          "size",
          "totalElements",
          "totalPages"
        ],
        "properties": {
          "content": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Sale"
            }
          },
          "page": {
            "type": "integer",
            "description": "Página actual; comienza en cero."
          },
          "size": {
            "type": "integer",
            "description": "Tamaño solicitado de página."
          },
          "totalElements": {
            "type": "integer",
            "description": "Cantidad total que coincide con los filtros.",
            "format": "int64"
          },
          "totalPages": {
            "type": "integer",
            "description": "Número total de páginas; cero para un resultado vacío."
          }
        }
      },
      "Error": {
        "type": "object",
        "description": "Los errores siempre se identifican por code. Según el origen pueden incluir también timestamp, status, path y fieldErrors.",
        "properties": {
          "code": {
            "type": "string",
            "description": "Código de error estable."
          },
          "message": {
            "type": "string",
            "description": "Explicación legible."
          },
          "correlationId": {
            "type": "string",
            "description": "Referencia para soporte.",
            "nullable": true
          },
          "status": {
            "type": "integer",
            "description": "Estado HTTP, cuando se incluye."
          },
          "timestamp": {
            "type": "string",
            "description": "Instante del error, cuando se incluye.",
            "format": "date-time"
          },
          "path": {
            "type": "string",
            "description": "Ruta solicitada, cuando se incluye."
          },
          "fieldErrors": {
            "type": "array",
            "nullable": true,
            "items": {
              "type": "object",
              "properties": {
                "field": {
                  "type": "string"
                },
                "message": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
}
