Overview
The SUNAT Electronic Invoicing API is built on Laravel 11 and follows a service-oriented architecture with clear separation of concerns. The system handles electronic document generation, SUNAT communication, and PDF generation.Architectural Layers
1. Models Layer
Eloquent models represent core business entities and handle data persistence.Company Model
Location:app/Models/Company.php
The Company model is the central entity that holds SUNAT credentials and configurations.
app/Models/Company.php
hasMany(Branch::class)- Multiple branches per companyhasMany(Invoice::class)- All invoiceshasMany(Boleta::class)- All sales receipts (boletas)hasMany(CompanyConfiguration::class)- Configuration settings
Branch Model
Location:app/Models/Branch.php
Branches (sucursales) represent physical locations within a company.
app/Models/Branch.php
2. Services Layer
Services contain business logic and external integrations.DocumentService
Location:app/Services/DocumentService.php
Handles document creation, tax calculation, and SUNAT communication orchestration.
app/Services/DocumentService.php
createInvoice() - Create Invoice with Automatic Tax Calculation
createInvoice() - Create Invoice with Automatic Tax Calculation
calculateTotals() - Automatic Tax Calculation Engine
calculateTotals() - Automatic Tax Calculation Engine
This method handles all SUNAT tax types including IGV, ISC, ICBPER, IVAP, and gratuitas (free items).
sendToSunat() - Send Documents to SUNAT
sendToSunat() - Send Documents to SUNAT
GreenterService
Location:app/Services/GreenterService.php
Wrpper for the Greenter library (thegreenter/greenter) which communicates with SUNAT web services.
app/Services/GreenterService.php
initializeSee() - Configure SUNAT Connection
initializeSee() - Configure SUNAT Connection
createInvoice() - Build Greenter Invoice Object
createInvoice() - Build Greenter Invoice Object
sendDocument() - Send to SUNAT and Get CDR Response
sendDocument() - Send to SUNAT and Get CDR Response
PdfService
Location:app/Services/PdfService.php
Generates PDF representations of electronic documents using DomPDF.
app/Services/PdfService.php
- Multiple format support (A4, A5, 80mm ticket, 50mm ticket)
- QR code generation following SUNAT standard
- Number to words conversion for totals
- Dynamic template selection
3. Configuration Management
Trait:HasCompanyConfigurationsLocation:
app/Traits/HasCompanyConfigurations.php
Provides flexible configuration management with environment-specific settings.
Data Flow
Invoice Creation Flow
Design Patterns
Repository Pattern (Implicit)
Eloquent models act as repositories with scopes and query builders.Service Layer Pattern
Business logic is encapsulated in service classes (DocumentService, GreenterService, PdfService).Dependency Injection
Factory Pattern
GreenterService creates Greenter objects (invoices, notes, guides) based on document types.Transaction Script Pattern
DocumentService methods use database transactions to ensure data consistency:Key Features
Automatic Tax Calculation
Calculates IGV, ISC, ICBPER, IVAP automatically based on affectation types
Thread-Safe Correlatives
Uses database locking to prevent duplicate document numbers
Multi-Environment
Separate configurations for Beta and Production SUNAT environments
Flexible Configuration
Environment-specific settings with caching for performance
File Structure
Next Steps
Authentication
Learn about Laravel Sanctum authentication and token management
Certificate Setup
Configure SUNAT digital certificates (.pfx/.pem)
