Overview
Gestión de Ventas follows a decoupled, service-based architecture designed to achieve:- Skinny Controllers: Controllers act as orchestrators, not business logic containers
- High Reusability: Business logic can be shared across controllers, jobs, and commands
- Testability: Each layer can be unit tested independently
- Maintainability: Clear separation of concerns makes code easier to understand and modify
Architectural Layers
The system is organized into 6 distinct layers, each with specific responsibilities:Data Layer
Models manage persistence and relationships
UI Configuration
Tables centralize column definitions and visibility
Filtering
Pipeline pattern for clean, reusable filters
Validation
Form Requests validate data and permissions
Business Logic
Services handle all business operations
Orchestration
Controllers coordinate requests and responses
1. Data Layer (Model)
Location:app/Models/[Module].php
Responsibility: Manage database persistence, relationships, and query scopes.
Key Requirements
Every model must define a
scopeWithIndexRelations($query) method to centralize eager loading used by both web tables and Excel exports.Example: Sale Model
app/Models/Sales/Sale.php
2. UI Configuration Layer (Tables)
Location:app/Tables/[Module]Table.php
Responsibility: Centralize column names, labels, and visibility logic for different devices.
Methods Required
allColumns(): Returns all available columns with human-readable labelsdefaultDesktop(): Columns visible by default on desktop devicesdefaultMobile(): Critical columns for mobile devices
Example: SaleTable
app/Tables/SalesTables/SaleTable.php
3. Filtering Layer (Pipeline Pattern)
Location:app/Filters/[Module]/
Responsibility: Keep controllers clean by moving all where clauses into dedicated filter classes.
See Filters & Pipeline Pattern for detailed documentation.
4. Validation & Security Layer (Form Requests)
Location:app/Http/Requests/[Module]/
Responsibility: Validate incoming data and verify permissions before the controller executes any logic.
Files
Store[Module]Request.php- For creating new recordsUpdate[Module]Request.php- For updating existing recordsBulk[Module]Request.php- For bulk operations
Key Features
See Permissions & Authorization for more details.
5. Business Logic Layer (Services)
Location:app/Services/[Module]/
Responsibility: Execute all business operations, database transactions, and complex calculations.
Service Types
- Business Service
- Catalog Service
File:
[Module]Service.phpHandles write operations, complex calculations, and bulk actions.6. Orchestration Layer (Controllers)
Location:app/Http/Controllers/[Module]/
Responsibility: Receive requests, call services, and return responses (views or JSON). Contains zero business logic.
Example: Standard Store Method
app/Http/Controllers/Sales/SaleController.php
Notice how the controller is clean and focused solely on orchestration - no business logic!
Implementation Checklist
When creating a new module, use this checklist to ensure consistency:Database & Security
Database & Security
- Create and run migration with
SoftDeletes - Create and run permissions seeder (
[Module]PermissionsSeeder) - Configure model with necessary scopes and relationships
Backend & Logic
Backend & Logic
- Define
[Module]Tableclass with column methods - Create filter pipeline in
app/Filters/[Module] - Implement
CatalogService(filter by country if applicable) - Implement business
Servicewith create, update, and bulk methods
HTTP & Routes
HTTP & Routes
- Create Form Requests (Store, Update, Bulk) with permission validation
- Register routes in
web.php(index, create, store, edit, update, destroy, bulk, export) - Configure controller with service injection
Frontend
Frontend
- Create index view with AJAX table and filter components
- Configure
window.filterSourcesfor filter chips - Create Create/Edit forms populated by
CatalogService
Benefits of This Architecture
Testability
Each layer can be tested independently with mocks and stubs
Reusability
Services can be used from controllers, commands, jobs, and tests
Maintainability
Clear separation makes it easy to find and modify code
Scalability
New features can be added without affecting existing code
Related Documentation
Service Layer
Deep dive into service architecture
Filters Pipeline
Learn about the filtering system
Permissions
Understand authorization flow