Skip to main content
POST
/
compra
curl -X POST https://api.example.com/compra \
  -H "Content-Type: application/json" \
  -d '{
    "descripcion": "Monthly office supplies purchase",
    "proveedorId": 1,
    "formapagoId": 2,
    "detalle": [
      {
        "productoId": 10,
        "cantidad": 100,
        "precio": 11.80
      },
      {
        "productoId": 15,
        "cantidad": 50,
        "precio": 23.60
      }
    ]
  }'
{
  "id": 1,
  "serie": "XYZ",
  "numero": "000123",
  "descripcion": "Monthly office supplies purchase",
  "proveedorId": 1,
  "proveedor": {
    "id": 1,
    "ruc": "20123456789",
    "nombre": "Acme Supplies Inc",
    "telefono": "+51-123-4567",
    "direccion": "Av. Principal 123",
    "correo": "[email protected]",
    "estado": true
  },
  "detalle": [
    {
      "id": 1,
      "productoId": 10,
      "cantidad": 100.0,
      "precio": 11.80,
      "baseImponible": 1000.0,
      "igv": 180.0,
      "total": 1180.0
    },
    {
      "id": 2,
      "productoId": 15,
      "cantidad": 50.0,
      "precio": 23.60,
      "baseImponible": 1000.0,
      "igv": 180.0,
      "total": 1180.0
    }
  ],
  "fechaCompra": "2024-01-15T10:30:00",
  "baseImponible": 2000.0,
  "igv": 360.0,
  "total": 2360.0,
  "formapagoId": 2,
  "formaPago": {
    "id": 2,
    "nombre": "Credit"
  }
}
Creates a new purchase order with automatic calculation of totals, taxes, and series/number generation.

Request Body

descripcion
string
Description of the purchase
proveedorId
long
required
ID of the supplier
formapagoId
long
ID of the payment method
fechaCompra
string
Purchase date in ISO 8601 format. Defaults to current timestamp if not provided.
detalle
array
required
Array of purchase detail items

Response

id
long
Unique identifier for the purchase
serie
string
Auto-generated 3-letter series code (e.g., “ABC”)
numero
string
Auto-generated 6-digit number (e.g., “000123”)
descripcion
string
Description of the purchase
proveedorId
long
ID of the supplier
proveedor
object
Supplier details (populated from supplier service)
detalle
array
Array of purchase detail items
fechaCompra
string
Purchase date in ISO 8601 format
baseImponible
double
Total taxable base (auto-calculated from all detail lines)
igv
double
Total IGV tax (auto-calculated from all detail lines)
total
double
Total purchase amount (auto-calculated from all detail lines)
formapagoId
long
Payment method ID
formaPago
object
Payment method details
curl -X POST https://api.example.com/compra \
  -H "Content-Type: application/json" \
  -d '{
    "descripcion": "Monthly office supplies purchase",
    "proveedorId": 1,
    "formapagoId": 2,
    "detalle": [
      {
        "productoId": 10,
        "cantidad": 100,
        "precio": 11.80
      },
      {
        "productoId": 15,
        "cantidad": 50,
        "precio": 23.60
      }
    ]
  }'
{
  "id": 1,
  "serie": "XYZ",
  "numero": "000123",
  "descripcion": "Monthly office supplies purchase",
  "proveedorId": 1,
  "proveedor": {
    "id": 1,
    "ruc": "20123456789",
    "nombre": "Acme Supplies Inc",
    "telefono": "+51-123-4567",
    "direccion": "Av. Principal 123",
    "correo": "[email protected]",
    "estado": true
  },
  "detalle": [
    {
      "id": 1,
      "productoId": 10,
      "cantidad": 100.0,
      "precio": 11.80,
      "baseImponible": 1000.0,
      "igv": 180.0,
      "total": 1180.0
    },
    {
      "id": 2,
      "productoId": 15,
      "cantidad": 50.0,
      "precio": 23.60,
      "baseImponible": 1000.0,
      "igv": 180.0,
      "total": 1180.0
    }
  ],
  "fechaCompra": "2024-01-15T10:30:00",
  "baseImponible": 2000.0,
  "igv": 360.0,
  "total": 2360.0,
  "formapagoId": 2,
  "formaPago": {
    "id": 2,
    "nombre": "Credit"
  }
}

Business Logic

Automatic Calculations

The system automatically performs the following calculations:
  1. Tax Calculation: Each detail line calculates IGV (18%) from the price
    • Base price = precio / 1.18
    • IGV = (precio - base price) * cantidad
    • Total = precio * cantidad
  2. Purchase Totals: Aggregates all detail lines
    • baseImponible = sum of all detail baseImponible
    • igv = sum of all detail igv
    • total = sum of all detail total
  3. Series and Number Generation: Automatically generates unique identifiers
    • Serie: 3 random uppercase letters
    • Numero: 6-digit random number (zero-padded)

Workflow

  1. Submit purchase with supplier ID and detail items
  2. System validates and calculates all amounts
  3. Serie and numero are auto-generated if not provided
  4. Purchase is persisted with calculated totals
  5. Response includes full purchase details with related entities

Build docs developers (and LLMs) love