Skip to main content

Introduction

This guide provides a full implementation of an accounting system’s backend in Laravel, leveraging MongoDB’s document model and aggregation pipeline. We will build:
  1. Database Schemas & Models: Using camelCase for collections and fields.
  2. Journal Posting Logic: A conceptual guide to ensure data integrity.
  3. Three Artisan Commands: For generating a Trial Balance, Profit & Loss Statement, and Balance Sheet directly from the database with a single, powerful query each.

Part 1: Setup and Configuration

First, ensure you have the mongodb/laravel-mongodb package installed and configured.
  1. Install the package:
  1. Configure config/database.php: Add a new MongoDB connection.
  1. Update your .env file:

Part 2: MongoDB Schema and Laravel Models (camelCase)

We will define three collections: dimChartOfAccounts, journalEntries, and generalLedger.

A. Chart of Accounts Model

This collection acts as the blueprint for all financial reporting.
  • Collection Name: dimChartOfAccounts
  • Purpose: Stores the list of all accounts, their properties, and their mapping to financial statements.
File: app/Models/ChartOfAccount.php

B. Journal Entry Model

This collection stores the journal entries with their lines embedded, a natural fit for a document database.
  • Collection Name: journalEntries
  • Purpose: To record all financial transactions before they are finalized in the General Ledger.
File: app/Models/JournalEntry.php

C. General Ledger Model

This collection is the final, immutable record of all transactions, optimized for fast reporting.
  • Collection Name: generalLedger
  • Purpose: Provides a flat, indexed list of every debit and credit for high-performance aggregation.
File: app/Models/GeneralLedger.php

Part 3: The Business Logic - Journal Posting

The posting process moves data from an approved JournalEntry to the generalLedger. This action must be atomic. If any part fails, the entire operation should roll back. MongoDB transactions are perfect for this. Conceptual Implementation (e.g., in a Job or Service class):

Part 4: Financial Statement Generators (Artisan Commands)

Here are the complete, fully documented Artisan commands.

A. Trial Balance Generator

This command verifies the fundamental accounting equation (Debits = Credits) across all accounts. File: app/Console/Commands/GenerateTrialBalance.php

B. Profit & Loss (Income Statement) Generator

This command calculates the company’s financial performance over a period. File: app/Console/Commands/GenerateProfitAndLoss.php

C. Balance Sheet Generator

This command presents a snapshot of the company’s financial position (Assets = Liabilities + Equity). File: app/Console/Commands/GenerateBalanceSheet.php

Part 5: How to Use

  1. Seed your dimChartOfAccounts collection: You must populate this collection with your company’s Chart of Accounts data first.
  2. Record and post journal entries: Use your application logic to create journalEntries and run the posting service to populate the generalLedger.
  3. Run the commands from your terminal:

UI Wireframe

Of course. Text-based wireframes are an excellent tool for focusing on structure, information hierarchy, and functionality without getting distracted by visual design. They clearly communicate the layout and user flow. Here are the text-based wireframes for the Journal Entry Form and the General Ledger View.

1. Text-Based Wireframe: Journal Entry Form

This wireframe illustrates the layout and key interactions of the form used to create a new journal entry.

2. Text-Based Wireframe: General Ledger View

This wireframe illustrates the layout for viewing and filtering General Ledger entries for a specific account.

UI

Of course. Building user-friendly interfaces for data entry and viewing is a critical next step. Here are complete, well-documented Blade component layouts for the Journal Entry Form and the General Ledger View. These layouts are designed using Tailwind CSS for styling and Alpine.js for client-side interactivity on the journal entry form, which are standard and highly effective tools in the Laravel ecosystem.

Part 1: Journal Entry Form Layout

This layout provides an interactive form for creating a new journal entry. It allows users to dynamically add and remove transaction lines, and it calculates debit/credit totals in real-time.

Conceptual Controller Method

First, you need a controller method to pass the necessary data (like the list of accounts) to the view.

Blade View: resources/views/journals/create.blade.php

This file contains the complete layout for the form.

Part 2: General Ledger View Layout

This layout provides a classic ledger view for a specific account. It includes filters, a summary, and a paginated list of transactions with a running balance.

Conceptual Controller Method

This controller method will handle filtering, fetching data, calculating the opening and running balances, and passing it all to the view.

Blade View: resources/views/ledger/index.blade.php

This file displays the filtered GL entries in a clean, readable table.