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 keyserie(String) - Unique purchase series (auto-generated)numero(String) - Unique purchase number (auto-generated)descripcion(String) - Purchase descriptionproveedorId(Long) - Supplier ID referenceproveedor(Proveedor) - Supplier data (transient, from Supplier service)detalle(List<CompraDetalle>) - Purchase line itemsfechaCompra(LocalDateTime) - Purchase timestampbaseImponible(Double) - Taxable base amountigv(Double) - Tax amount (IGV)total(Double) - Total amountformapagoId(Long) - Payment method ID referenceformaPago(FormaPago) - Payment method data (transient)
compra
Lifecycle Hooks:
@PrePersist- Calculates totals and generates serie/numero@PreUpdate- Recalculates totals
- Serie: 3 random uppercase letters (e.g., “XYZ”)
- Numero: 6-digit random number (e.g., “001234”)
- One-to-many with
CompraDetalleviacompra_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 keycantidad(Integer) - Quantity purchasedprecioUnitario(Double) - Unit costbaseImponible(Double) - Line taxable base (auto-calculated)igv(Double) - Line tax amount (auto-calculated, 18%)total(Double) - Line total (auto-calculated)productoId(Long) - Product ID referenceproducto(Producto) - Product data (transient)
compra_detalle
Relationships:
- Many-to-one with
Compraviacompra_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 purchaseGET /compra- List all purchasesGET /compra/{id}- Get purchase by IDPUT /compra/{id}- Update purchaseDELETE /compra/{id}- Delete purchaseGET /compra/buscar-por-fechas- Search by date rangeGET /compra/buscar/serie- Search by serieGET /compra/buscar/numero- Search by numberGET /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 updatesgetAllCompra()- List all purchases with supplier/payment datagetCompraById()- Get purchase with full detailsupdateCompra()- Update purchasedeleteCompra()- Delete purchasebuscarPorRangoFechas()- Date range searchbuscarPorSerie()- Serie searchbuscarPorNumero()- Number searchbuscarPorSerieYNumero()- 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 serviceProveedor- Supplier data from Supplier serviceFormaPago- Payment method from Payment serviceCategoria- 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/Hibernatespring-boot-starter-web- REST APIspring-boot-starter-validation- Bean validationspring-cloud-starter-netflix-eureka-client:4.0.3- Service discoveryspring-cloud-starter-config:4.0.4- Centralized configurationspring-cloud-starter-openfeign- Feign client for microservice communicationspring-cloud-starter-circuitbreaker-resilience4j- Circuit breaker patternspringdoc-openapi-starter-webmvc-ui:2.0.2- OpenAPI/Swagger UImysql-connector-j- MySQL driverlombok- Code generation
Configuration
Config File:config-data/jea-compra-service.yml
Key Settings:
Database Schema
Main Tables:compra- Purchase transaction header- Primary key:
id - Unique constraints:
serie,numero - Foreign keys:
proveedor_id,formapago_id(references external services)
- Primary key:
compra_detalle- Purchase line items- Primary key:
id - Foreign keys:
compra_id→compra.id,producto_id(external)
- Primary key:
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:- Validates product existence via Catalog service
- Processes purchase transaction
- 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)