Skip to main content

Overview

The Purchase Service (jea-compra) manages purchase transactions from suppliers, including purchase orders, receipts, and inventory replenishment. It integrates with Catalog, Supplier, and Payment services. Database: MySQL (compra-ms) Port: Dynamic (configured via Eureka)

Core Entities

Compra

Purchase transaction header. Location: jea-compra/src/main/java/com/example/jeacompra/entity/Compra.java:13 Fields:
  • id (Long) - Primary key
  • serie (String) - Unique purchase series (auto-generated)
  • numero (String) - Unique purchase number (auto-generated)
  • descripcion (String) - Purchase description
  • proveedorId (Long) - Supplier ID reference
  • proveedor (Proveedor) - Supplier data (transient, from Supplier service)
  • detalle (List<CompraDetalle>) - Purchase line items
  • fechaCompra (LocalDateTime) - Purchase timestamp
  • baseImponible (Double) - Taxable base amount
  • igv (Double) - Tax amount (IGV)
  • total (Double) - Total amount
  • formapagoId (Long) - Payment method ID reference
  • formaPago (FormaPago) - Payment method data (transient)
Table: compra Lifecycle Hooks:
  • @PrePersist - Calculates totals and generates serie/numero
  • @PreUpdate - Recalculates totals
Auto-generation:
  • Serie: 3 random uppercase letters (e.g., “XYZ”)
  • Numero: 6-digit random number (e.g., “001234”)
Relationships:
  • One-to-many with CompraDetalle via compra_id
  • References Proveedor (external service)
  • References FormaPago (external service)

CompraDetalle

Purchase transaction line item. Location: jea-compra/src/main/java/com/example/jeacompra/entity/CompraDetalle.java Fields:
  • id (Long) - Primary key
  • cantidad (Integer) - Quantity purchased
  • precioUnitario (Double) - Unit cost
  • baseImponible (Double) - Line taxable base (auto-calculated)
  • igv (Double) - Line tax amount (auto-calculated, 18%)
  • total (Double) - Line total (auto-calculated)
  • productoId (Long) - Product ID reference
  • producto (Producto) - Product data (transient)
Table: compra_detalle Relationships:
  • Many-to-one with Compra via compra_id
  • References Producto (from Catalog service)

Key Components

Controllers

CompraController

Location: jea-compra/src/main/java/com/example/jeacompra/controller/CompraController.java:15 Endpoints:
  • POST /compra - Create new purchase
  • GET /compra - List all purchases
  • GET /compra/{id} - Get purchase by ID
  • PUT /compra/{id} - Update purchase
  • DELETE /compra/{id} - Delete purchase
  • GET /compra/buscar-por-fechas - Search by date range
  • GET /compra/buscar/serie - Search by serie
  • GET /compra/buscar/numero - Search by number
  • GET /compra/buscar/serie-numero - Find by serie and number

Services

CompraService

Location: jea-compra/src/main/java/com/example/jeacompra/service/CompraService.java Implementation: CompraServiceImpl.java Key Methods:
  • createCompra() - Create purchase with inventory updates
  • getAllCompra() - List all purchases with supplier/payment data
  • getCompraById() - Get purchase with full details
  • updateCompra() - Update purchase
  • deleteCompra() - Delete purchase
  • buscarPorRangoFechas() - Date range search
  • buscarPorSerie() - Serie search
  • buscarPorNumero() - Number search
  • buscarPorSerieYNumero() - Exact match search

Repositories

CompraRepository

Location: jea-compra/src/main/java/com/example/jeacompra/repository/CompraRepository.java Extends JpaRepository<Compra, Long> Provides custom query methods for purchase data.

Feign Clients

ProductoFeign

Location: jea-compra/src/main/java/com/example/jeacompra/feign/ProductoFeign.java Integrates with Catalog service for product data and inventory updates.

ProveedorFeign

Location: jea-compra/src/main/java/com/example/jeacompra/feign/ProveedorFeign.java Integrates with Supplier service for supplier data.

FormaPagoFeign

Location: jea-compra/src/main/java/com/example/jeacompra/feign/FormaPagoFeign.java Integrates with Payment service for payment method data.

CategoriaFeign

Location: jea-compra/src/main/java/com/example/jeacompra/feign/CategoriaFeign.java Integrates with Catalog service for category data.

DTOs

Transient DTOs (from other services)

  • Producto - Product data from Catalog service
  • Proveedor - Supplier data from Supplier service
  • FormaPago - Payment method from Payment service
  • Categoria - Category data from Catalog service

Dependencies

Spring Boot Version: 3.1.3 Spring Cloud Version: 2022.0.2 Key Dependencies:
  • spring-boot-starter-data-jpa - JPA/Hibernate
  • spring-boot-starter-web - REST API
  • spring-boot-starter-validation - Bean validation
  • spring-cloud-starter-netflix-eureka-client:4.0.3 - Service discovery
  • spring-cloud-starter-config:4.0.4 - Centralized configuration
  • spring-cloud-starter-openfeign - Feign client for microservice communication
  • spring-cloud-starter-circuitbreaker-resilience4j - Circuit breaker pattern
  • springdoc-openapi-starter-webmvc-ui:2.0.2 - OpenAPI/Swagger UI
  • mysql-connector-j - MySQL driver
  • lombok - Code generation

Configuration

Config File: config-data/jea-compra-service.yml Key Settings:
server:
  port: ${PORT:${SERVER_PORT:0}}

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/compra-ms
    username: root
    password: ""
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8090/eureka

Database Schema

Main Tables:
  • compra - Purchase transaction header
    • Primary key: id
    • Unique constraints: serie, numero
    • Foreign keys: proveedor_id, formapago_id (references external services)
  • compra_detalle - Purchase line items
    • Primary key: id
    • Foreign keys: compra_idcompra.id, producto_id (external)

Business Logic

Tax Calculation

The system automatically calculates:
  • Base Imponible: Sum of line item bases (quantity × unit cost)
  • IGV (Tax): 18% of base imponible
  • Total: Base imponible + IGV

Purchase Number Generation

Each purchase receives:
  • Serie: 3 random uppercase letters
  • Numero: 6-digit zero-padded number
  • Both are unique and auto-generated on creation

Inventory Integration

When creating a purchase:
  1. Validates product existence via Catalog service
  2. Processes purchase transaction
  3. Increases product quantities in Catalog service (inventory replenishment)

Key Features

  • Complete purchase transaction management
  • Automatic purchase numbering (serie/numero)
  • Tax calculation (IGV at 18%)
  • Multi-line item support
  • Integration with Supplier, Product, and Payment services
  • Purchase search and filtering by date range
  • Serie/number-based lookup
  • Circuit breaker for fault tolerance
  • Inventory replenishment automation
  • OpenAPI/Swagger documentation
  • Eureka service discovery integration

Integration Points

Consumes:

  • Catalog Service (ProductoFeign, CategoriaFeign) - Product data and inventory updates
  • Supplier Service (ProveedorFeign) - Supplier information
  • Payment Service (FormaPagoFeign) - Payment method details

APIs Exposed:

  • Purchase CRUD operations
  • Purchase search and filtering
  • Date range queries
  • Serie/number lookup

Comparison with Sales Service

The Purchase Service mirrors the Sales Service architecture but focuses on:
  • Inbound transactions (purchases from suppliers) vs outbound (sales to customers)
  • Inventory increases vs decreases
  • Supplier management vs customer management
  • Similar document numbering (serie/numero)
  • Identical tax calculation logic (18% IGV)

Build docs developers (and LLMs) love