Overview
TheJournalEntryService manages all accounting journal entries in the ERP system. It enforces double-entry bookkeeping rules, validates debits and credits balance, and provides lifecycle management for journal entries.
Namespace: App\Services\Accounting\JournalEntries\JournalEntryService
Location: app/Services/Accounting/JournalEntries/JournalEntryService.php
Core Principles
Double-Entry Validation
Every entry must have equal debits and credits
Transaction Safety
All operations wrapped in database transactions
State Management
Draft → Posted → (optionally) Cancelled
Audit Trail
Tracks creator and maintains reference documentation
Methods
create()
Creates a new journal entry with its detail lines.Journal entry data with the following structure:
Returns the created JournalEntry model with all items
- Double-Entry Balance - Total debits must equal total credits (within 0.001 tolerance)
- Positive Amount - Total debit/credit must be greater than zero
- Valid Accounts - All accounting account IDs must exist
- Sums all debit and credit amounts from items array
- Validates debits equal credits (allows 0.001 margin for decimal precision)
- Validates total is greater than zero
- Creates journal entry header with authenticated user as creator
- Creates all journal entry items (detail lines)
- Returns completed journal entry
Example Usage
update()
Updates an existing journal entry and synchronizes its items.The journal entry instance to update
Updated data with same structure as
create() methodReturns the updated JournalEntry model
- Can only update entries in
'draft'status - Posted or cancelled entries cannot be modified
- Throws exception if attempting to update non-draft entry
- Validates entry is in draft status
- Validates new items array (debits = credits)
- Updates journal entry header fields
- Deletes all existing items
- Creates new items from data array
- Returns updated entry
Example Usage
post()
Changes the status of a journal entry from draft to posted.The journal entry to post
Returns
true if posting was successful- Entry must be in
'draft'status - Throws exception if entry is already posted or cancelled
- Once posted, the entry cannot be edited via
update() - Posted entries represent committed accounting transactions
- In accounting practice, posted entries should not be modified
Example Usage
cancel()
Cancels a journal entry by changing its status to cancelled.The journal entry to cancel
Returns
true if cancellation was successfulExample Usage
JournalEntry Model Constants
Exception Handling
Unbalanced Entry
Unbalanced Entry
Exception:
"Error contable: El asiento no está cuadrado. Débito: {amount}, Crédito: {amount}"Cause: Total debits do not equal total creditsResolution: Ensure all items are correctly specified with balanced debits and creditsZero Amount Entry
Zero Amount Entry
Exception:
"El monto del asiento debe ser mayor a cero."Cause: All items have zero amountsResolution: Ensure at least one line has a non-zero debit or creditCannot Edit Posted Entry
Cannot Edit Posted Entry
Exception:
"No se puede editar un asiento que ya ha sido asentado o anulado."Cause: Attempting to update an entry that is posted or cancelledResolution:- Only draft entries can be edited
- To modify a posted entry, create a reversal entry and a new corrected entry
Cannot Post Non-Draft Entry
Cannot Post Non-Draft Entry
Exception:
"Solo se pueden asentar documentos en estado Borrador."Cause: Attempting to post an entry that is not in draft statusResolution: Verify entry status before calling post()Update Validation Error
Update Validation Error
Exception:
"Error: El asiento no está cuadrado. Diferencia: {amount}"Cause: Updated items array does not balanceResolution: Same as “Unbalanced Entry” - verify debit/credit totals matchDatabase Transaction Safety
All methods (create(), update(), post(), cancel()) wrap their operations in database transactions using DB::transaction(). This ensures:
- Entry header and items are created/updated atomically
- Partial failures trigger complete rollback
- Data consistency is maintained
Best Practices
Use Draft Status
Create entries as drafts initially, then post after review
Clear References
Always include reference numbers to link entries to source documents
Descriptive Notes
Add line-level notes to clarify the purpose of each debit/credit
Reversal Pattern
To correct posted entries, cancel and create reversal + correction entries
Integration Points
Used By
- SaleService - Creates entries for cash sales and cancellations
- InventoryMovementService - Creates entries for inventory movements
- ReceivableService - Creates entries for credit sales
- PaymentService - Creates entries for payment receipts
- PurchaseService - Creates entries for purchase transactions
Model Methods
TheJournalEntry model provides these helper methods:
Related Models
- JournalEntry Model - Entry header
- JournalItem Model - Entry detail lines
- AccountingAccount Model - Chart of accounts