Overview
The Sales Service (jea-venta) manages sales transactions, invoice generation, and sales analytics. It integrates with Catalog, Customer, and Payment services to process complete sales operations. Database: MySQL (venta-ms)
Port: Dynamic (configured via Eureka)
Core Entities
Venta
Sales transaction header. Location:jea-venta/src/main/java/com/example/jeaventa/entity/Venta.java:13
Fields:
id(Integer) - Primary keyserie(String) - Unique invoice series (auto-generated)numero(String) - Unique invoice number (auto-generated)descripcion(String) - Sale descriptionclienteId(Long) - Customer ID referencecliente(Cliente) - Customer data (transient, from Cliente service)detalle(List<VentaDetalle>) - Sale line itemsfechaVenta(LocalDateTime) - Sale timestampbaseImponible(Double) - Taxable base amountigv(Double) - Tax amount (IGV)total(Double) - Total amountformapagoId(Long) - Payment method ID referenceformaPago(FormaPago) - Payment method data (transient)
venta
Lifecycle Hooks:
@PrePersist- Calculates totals and generates serie/numero@PreUpdate- Recalculates totals
- Serie: 3 random uppercase letters (e.g., “ABC”)
- Numero: 6-digit random number (e.g., “000123”)
- One-to-many with
VentaDetalleviaventa_id - References
Cliente(external service) - References
FormaPago(external service)
VentaDetalle
Sales transaction line item. Location:jea-venta/src/main/java/com/example/jeaventa/entity/VentaDetalle.java
Fields:
id(Long) - Primary keycantidad(Integer) - QuantityprecioUnitario(Double) - Unit pricebaseImponible(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)
venta_detalle
Relationships:
- Many-to-one with
Ventaviaventa_id - References
Producto(from Catalog service)
Key Components
Controllers
VentaController
Location:jea-venta/src/main/java/com/example/jeaventa/controller/VentaController.java:17
Endpoints:
POST /venta- Create new saleGET /venta- List all salesGET /venta/{id}- Get sale by IDPUT /venta/{id}- Update saleDELETE /venta/{id}- Delete saleGET /venta/buscar-por-fechas- Search by date rangeGET /venta/buscar/serie- Search by serieGET /venta/buscar/numero- Search by numberGET /venta/buscar/serie-numero- Find by serie and numberGET /venta/productos-mas-vendidos- Top 10 best-selling productsGET /venta/ventas-por-mes- Sales by monthGET /venta/total-ventas- Total sales amount
- Sales statistics and reporting
- Product performance analysis
- Monthly sales aggregation
Services
VentaService
Location:jea-venta/src/main/java/com/example/jeaventa/service/VentaService.java
Implementation: VentaServiceImpl.java
Key Methods:
createVenta()- Create sale with inventory updatesgetAllVentas()- List all sales with customer/payment datagetVentaById()- Get sale with full detailsupdateVenta()- Update saledeleteVenta()- Delete salebuscarPorRangoFechas()- Date range searchbuscarPorSerie()- Serie searchbuscarPorNumero()- Number searchbuscarPorSerieYNumero()- Exact match searchobtenerTop10ProductosVendidos()- Sales analyticsobtenerVentasPorMes()- Monthly aggregationobtenerTotalVentas()- Total revenue
Repositories
VentaRepository
Location:jea-venta/src/main/java/com/example/jeaventa/repository/VentaRepository.java
Extends JpaRepository<Venta, Integer>
Provides custom query methods for sales data.
VentaDetalleRepository
Location:jea-venta/src/main/java/com/example/jeaventa/repository/VentaDetalleRepository.java
Manages sale line items.
Feign Clients
ProductoFeign
Location:jea-venta/src/main/java/com/example/jeaventa/feign/ProductoFeign.java
Integrates with Catalog service for product data.
ClienteFeign
Location:jea-venta/src/main/java/com/example/jeaventa/feign/ClienteFeign.java
Integrates with Customer service for customer data.
FormaPagoFeign
Location:jea-venta/src/main/java/com/example/jeaventa/feign/FormaPagoFeign.java
Integrates with Payment service for payment method data.
CategoriaFeign
Location:jea-venta/src/main/java/com/example/jeaventa/feign/CategoriaFeign.java
Integrates with Catalog service for category data.
Configuration
OpenAPIConfig
Location:jea-venta/src/main/java/com/example/jeaventa/config/OpenAPIConfig.java
Swagger/OpenAPI documentation configuration.
DTOs
Transient DTOs (from other services)
Producto- Product data from Catalog serviceCliente- Customer data from Customer serviceFormaPago- Payment method from Payment serviceCategoria- Category data from Catalog service
Analytics DTOs
ProductoMasVendidoDTO- Best-selling product statisticsVentaPorMesDTO- Monthly sales aggregation
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-venta-service.yml
Key Settings:
Database Schema
Main Tables:venta- Sales transaction header- Primary key:
id - Unique constraints:
serie,numero - Foreign keys:
cliente_id,formapago_id(references external services)
- Primary key:
venta_detalle- Sales line items- Primary key:
id - Foreign keys:
venta_id→venta.id,producto_id(external)
- Primary key:
Business Logic
Tax Calculation
The system automatically calculates:- Base Imponible: Sum of line item bases (quantity × unit price)
- IGV (Tax): 18% of base imponible
- Total: Base imponible + IGV
Invoice Number Generation
Each sale 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 sale:- Validates product availability via Catalog service
- Processes sale transaction
- Updates product quantities in Catalog service
Key Features
- Complete sales transaction management
- Automatic invoice numbering (serie/numero)
- Tax calculation (IGV at 18%)
- Multi-line item support
- Integration with Customer, Product, and Payment services
- Sales analytics and reporting
- Best-selling products analysis
- Monthly sales aggregation
- Date range filtering
- Circuit breaker for fault tolerance
- OpenAPI/Swagger documentation
- Eureka service discovery integration
Integration Points
Consumes:
- Catalog Service (
ProductoFeign,CategoriaFeign) - Product data and inventory updates - Customer Service (
ClienteFeign) - Customer information - Payment Service (
FormaPagoFeign) - Payment method details
APIs Exposed:
- Sales CRUD operations
- Sales search and filtering
- Sales analytics and reports
- Product performance metrics