Skip to main content

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 key
  • serie (String) - Unique invoice series (auto-generated)
  • numero (String) - Unique invoice number (auto-generated)
  • descripcion (String) - Sale description
  • clienteId (Long) - Customer ID reference
  • cliente (Cliente) - Customer data (transient, from Cliente service)
  • detalle (List<VentaDetalle>) - Sale line items
  • fechaVenta (LocalDateTime) - Sale 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: venta Lifecycle Hooks:
  • @PrePersist - Calculates totals and generates serie/numero
  • @PreUpdate - Recalculates totals
Auto-generation:
  • Serie: 3 random uppercase letters (e.g., “ABC”)
  • Numero: 6-digit random number (e.g., “000123”)
Relationships:
  • One-to-many with VentaDetalle via venta_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 key
  • cantidad (Integer) - Quantity
  • precioUnitario (Double) - Unit price
  • 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: venta_detalle Relationships:
  • Many-to-one with Venta via venta_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 sale
  • GET /venta - List all sales
  • GET /venta/{id} - Get sale by ID
  • PUT /venta/{id} - Update sale
  • DELETE /venta/{id} - Delete sale
  • GET /venta/buscar-por-fechas - Search by date range
  • GET /venta/buscar/serie - Search by serie
  • GET /venta/buscar/numero - Search by number
  • GET /venta/buscar/serie-numero - Find by serie and number
  • GET /venta/productos-mas-vendidos - Top 10 best-selling products
  • GET /venta/ventas-por-mes - Sales by month
  • GET /venta/total-ventas - Total sales amount
Analytics Endpoints:
  • 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 updates
  • getAllVentas() - List all sales with customer/payment data
  • getVentaById() - Get sale with full details
  • updateVenta() - Update sale
  • deleteVenta() - Delete sale
  • buscarPorRangoFechas() - Date range search
  • buscarPorSerie() - Serie search
  • buscarPorNumero() - Number search
  • buscarPorSerieYNumero() - Exact match search
  • obtenerTop10ProductosVendidos() - Sales analytics
  • obtenerVentasPorMes() - Monthly aggregation
  • obtenerTotalVentas() - 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 service
  • Cliente - Customer data from Customer service
  • FormaPago - Payment method from Payment service
  • Categoria - Category data from Catalog service

Analytics DTOs

  • ProductoMasVendidoDTO - Best-selling product statistics
  • VentaPorMesDTO - Monthly sales aggregation

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-venta-service.yml Key Settings:
server:
  port: ${PORT:${SERVER_PORT:0}}

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

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

Database Schema

Main Tables:
  • venta - Sales transaction header
    • Primary key: id
    • Unique constraints: serie, numero
    • Foreign keys: cliente_id, formapago_id (references external services)
  • venta_detalle - Sales line items
    • Primary key: id
    • Foreign keys: venta_idventa.id, producto_id (external)

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:
  1. Validates product availability via Catalog service
  2. Processes sale transaction
  3. 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

Build docs developers (and LLMs) love