# mhdniyas/green-leaf-erp architecture

> Architecture diagram of the GitHub repository mhdniyas/green-leaf-erp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mhdniyas/green-leaf-erp
- Repository: https://github.com/mhdniyas/green-leaf-erp

## Overview

#### Purpose and entry points
A Laravel ERP/operations system for procurement, inventory, warehouses, shop ordering/delivery, sales, finance/cashbook, and HR/payroll. HTTP entry points are server-rendered web routes and a versioned API: `routes/web.php`, `routes/api.php`. Laravel boots through `public/index.php` and `bootstrap/app.php`; scheduled/operational work enters through `artisan` and `routes/console.php`.

#### Delivery surfaces and access boundary
Web controllers provide role-specific workspaces for administrators, purchasers, shop owners, warehouse staff, inventory staff, and finance users; Blade views and Vite-built JavaScript support these workflows. APIs expose inventory, purchasing, purchaser, and warehouse operations with request validation and resource serialization.
- `app/Http/Controllers/Web/Admin/FinanceV2Controller.php`
- `app/Http/Controllers/Api/Inventory/StockBatchController.php`
- `app/Http/Middleware/ApiVersionMiddleware.php`

Authorization is policy-based, with authentication/Sanctum configuration and role/permission schema. Secure headers and last-seen updates are cross-cutting middleware.
- `app/Policies/PurchaseOrderPolicy.php`
- `app/Http/Middleware/SecureHeaders.php`
- `config/sanctum.php`

#### Procurement and price lifecycle
Suppliers, purchase orders, goods receipts (GRNs), purchase invoices, purchaser carts, vendor advances, and daily/grade prices form the inbound purchasing lifecycle. Controllers validate requests, then services/actions apply transitions such as recording/approving GRNs and cancelling invoices. Persistent records are Eloquent models, while DTOs and repositories provide application/data boundaries.
- `app/Actions/Purchasing/RecordGoodsReceiptAction.php`
- `app/Actions/Purchasing/ApproveGoodsReceiptAction.php`
- `app/Services/Purchasing/PurchaseOrderService.php`

Daily purchaser pricing is synchronized by a console command; read caching and observers support high-traffic purchaser views.
- `app/Console/Commands/SyncDailyPurchaserPricesCommand.php`
- `app/Services/Purchasing/PurchaserReadCacheService.php`
- `app/Observers/PurchaserCacheInvalidationObserver.php`

#### Inventory, receiving, sorting, and warehouse fulfillment
Inventory centers on product/category master data, stock batches, movements, adjustments, wastage, daily closes, and sorting. Stock is ledger-oriented: operational mutations flow through inventory services/actions and generate stock movements. Batch sorting has explicit quantity and already-sorted failure boundaries.
- `app/Services/Inventory/StockLedgerService.php`
- `app/Actions/Inventory/ProcessBatchSortingAction.php`
- `app/Models/StockMovement.php`

Warehouse flows receive purchased/direct stock, scope loadout work by warehouse, and fulfill shop orders. Loadout state and completion are persisted separately, supporting idempotent client-side saves and delivery handoff.
- `app/Services/Warehouse/WarehouseScopedLoadoutService.php`
- `app/Services/Warehouse/WarehouseLoadoutCompletionService.php`
- `app/Models/ShopOrderLoadoutState.php`

#### Shop requisitions, delivery, and invoicing
Shops submit and revise orders; requisition services synchronize item quantities and record change history. Warehouse loadouts produce deliveries, which shop owners can receive/review; discrepancies and delivery-review resolution are explicit workflow stages. Shop invoices and payment requests convert fulfilled demand into receivable/payment processes.
- `app/Services/Requisition/ShopOrderRevisionService.php`
- `app/Domains/ShopOrder/Actions/ResolveDeliveryReviewAction.php`
- `app/Services/ShopInvoices/ShopInvoiceService.php`

#### Finance, cashbook, and HR
Finance is a separate but integrated accounting layer: journals, company accounts/payables, shop accounting, vendor settlements, direct company sales, and cashbook ledgers/reconciliation. Operational events such as expenses, sales, invoice activity, and payroll are posted/projection-managed by dedicated services rather than controllers.
- `app/Services/Finance/JournalService.php`
- `app/Services/Cashbook/TransactionGenerator.php`
- `app/Services/Cashbook/CompanyPaymentReconciliationService.php`

HR maintains employee directories, attendance, leave ledgers, advances, assignments, payroll runs, and payments; payroll can feed finance/cashbook flows.
- `app/Services/HR/PayrollService.php`
- `app/Services/HR/LeaveLedgerService.php`
- `app/Models/PayrollRun.php`

#### Runtime and infrastructure
The application runs on PHP/Laravel with Eloquent migrations and a relational database, Laravel cache/session/queue facilities, filesystem-backed storage, mail, notifications, and Vite frontend assets. Background work includes inventory-empty processing, scheduled catalog/price/reconciliation tasks, and database-backup email delivery.
- `app/Jobs/ProcessInventoryEmptyBatch.php`
- `app/Services/Backup/DatabaseBackupService.php`
- `app/Mail/DatabaseBackupMail.php`

Main entry points: [`app/Services/Inventory/StockLedgerService.php`](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Inventory/StockLedgerService.php) (Stock ledger), [`app/Services/Purchasing/PurchaseOrderService.php`](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Purchasing/PurchaseOrderService.php) (Procurement lifecycle), [`app/Services/Warehouse/WarehouseScopedLoadoutService.php`](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Warehouse/WarehouseScopedLoadoutService.php) (Warehouse loadouts), [`routes/web.php`](https://github.com/mhdniyas/green-leaf-erp/blob/master/routes/web.php) (Web workspaces)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry &amp; Access"]
  node_web["Web workspaces<br/>Laravel web routes<br/>[web.php]"]
  node_api["Versioned operations API<br/>Laravel API routes<br/>[api.php]"]
  node_api_version["API version boundary<br/>middleware"]
  node_access["Policy authorization<br/>Laravel policies"]
end

subgraph group_operations["Operational Workflows"]
  node_procurement["Procurement lifecycle<br/>purchasing services"]
  node_receipt_approval["GRN approval<br/>purchasing action"]
  node_price_sync["Daily price sync<br/>console command"]
  node_purchaser_cache["Purchaser read cache<br/>cache service"]
  node_inventory["Stock ledger<br/>inventory service"]
  node_sorting["Batch sorting<br/>inventory action"]
  node_stock_movements[("Stock movements<br/>Eloquent model<br/>[StockMovement.php]")]
  node_warehouse["Warehouse loadouts<br/>warehouse services"]
  node_loadout_state[("Loadout state<br/>Eloquent model")]
  node_shop_orders["Shop requisitions<br/>requisition service"]
  node_delivery_review["Delivery review resolution<br/>shop-order action"]
  node_shop_invoices["Shop invoicing<br/>invoice service"]
end

subgraph group_accounting["Finance &amp; HR"]
  node_finance["Finance journals<br/>finance service<br/>[JournalService.php]"]
  node_cashbook["Cashbook &amp; reconciliation<br/>cashbook services"]
  node_hr_payroll["HR payroll<br/>HR service<br/>[PayrollService.php]"]
end

subgraph group_platform["Runtime Services"]
  node_database[("Operational database<br/>relational database")]
  node_async["Queue &amp; scheduled work<br/>Laravel async runtime"]
  node_backup["Database backup delivery<br/>backup service"]
end

node_web -->|"authorizes"| node_access
node_web -->|"drives"| node_procurement
node_web -->|"drives"| node_inventory
node_web -->|"drives"| node_warehouse
node_api -->|"passes through"| node_api_version
node_api_version -->|"inventory operations"| node_inventory
node_api_version -->|"purchasing operations"| node_procurement
node_procurement -->|"approves GRNs"| node_receipt_approval
node_receipt_approval -->|"receives stock"| node_inventory
node_price_sync -->|"synchronizes prices"| node_procurement
node_procurement -.->|"serves purchaser reads"| node_purchaser_cache
node_inventory -->|"processes batches"| node_sorting
node_inventory -->|"writes ledger"| node_stock_movements
node_shop_orders -->|"creates fulfillment demand"| node_warehouse
node_warehouse -->|"persists progress"| node_loadout_state
node_warehouse -->|"hands off deliveries"| node_delivery_review
node_delivery_review -->|"confirmed fulfillment"| node_shop_invoices
node_shop_invoices -->|"posts receivables"| node_finance
node_hr_payroll -->|"posts payroll"| node_finance
node_finance -->|"generates transactions"| node_cashbook
node_procurement -->|"persists records"| node_database
node_inventory -->|"persists stock state"| node_database
node_warehouse -->|"persists fulfillment"| node_database
node_finance -->|"persists journals"| node_database
node_async -->|"deferred processing"| node_inventory
node_async -->|"scheduled execution"| node_price_sync
node_backup -->|"backs up"| node_database

click node_web "https://github.com/mhdniyas/green-leaf-erp/blob/master/routes/web.php"
click node_api "https://github.com/mhdniyas/green-leaf-erp/blob/master/routes/api.php"
click node_api_version "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Http/Middleware/ApiVersionMiddleware.php"
click node_access "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Policies/PurchaseOrderPolicy.php"
click node_procurement "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Purchasing/PurchaseOrderService.php"
click node_receipt_approval "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Actions/Purchasing/ApproveGoodsReceiptAction.php"
click node_price_sync "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Console/Commands/SyncDailyPurchaserPricesCommand.php"
click node_purchaser_cache "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Purchasing/PurchaserReadCacheService.php"
click node_inventory "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Inventory/StockLedgerService.php"
click node_sorting "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Actions/Inventory/ProcessBatchSortingAction.php"
click node_stock_movements "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Models/StockMovement.php"
click node_warehouse "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Warehouse/WarehouseScopedLoadoutService.php"
click node_loadout_state "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Models/ShopOrderLoadoutState.php"
click node_shop_orders "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Requisition/ShopOrderRevisionService.php"
click node_delivery_review "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Domains/ShopOrder/Actions/ResolveDeliveryReviewAction.php"
click node_shop_invoices "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/ShopInvoices/ShopInvoiceService.php"
click node_finance "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Finance/JournalService.php"
click node_cashbook "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Cashbook/TransactionGenerator.php"
click node_hr_payroll "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/HR/PayrollService.php"
click node_async "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Jobs/ProcessInventoryEmptyBatch.php"
click node_backup "https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Backup/DatabaseBackupService.php"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web,node_api,node_api_version,node_access toneBlue
class node_procurement,node_receipt_approval,node_price_sync,node_purchaser_cache,node_inventory,node_sorting,node_stock_movements,node_warehouse,node_loadout_state,node_shop_orders,node_delivery_review,node_shop_invoices toneAmber
class node_finance,node_cashbook,node_hr_payroll toneMint
class node_database,node_async,node_backup toneRose
```

## Components

### Entry & Access

- [Web workspaces](https://github.com/mhdniyas/green-leaf-erp/blob/master/routes/web.php): Laravel web routes · Role-specific server-rendered operational workflows. · `routes/web.php`
- [Versioned operations API](https://github.com/mhdniyas/green-leaf-erp/blob/master/routes/api.php): Laravel API routes · `routes/api.php`
- [API version boundary](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Http/Middleware/ApiVersionMiddleware.php): middleware · `app/Http/Middleware/ApiVersionMiddleware.php`
- [Policy authorization](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Policies/PurchaseOrderPolicy.php): Laravel policies · `app/Policies/PurchaseOrderPolicy.php`

### Operational Workflows

- [Procurement lifecycle](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Purchasing/PurchaseOrderService.php): purchasing services · Orders, receipts, invoices, supplier advances, carts, and prices. · `app/Services/Purchasing/PurchaseOrderService.php`
- [GRN approval](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Actions/Purchasing/ApproveGoodsReceiptAction.php): purchasing action · Applies goods-receipt approval transitions. · `app/Actions/Purchasing/ApproveGoodsReceiptAction.php`
- [Daily price sync](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Console/Commands/SyncDailyPurchaserPricesCommand.php): console command · `app/Console/Commands/SyncDailyPurchaserPricesCommand.php`
- [Purchaser read cache](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Purchasing/PurchaserReadCacheService.php): cache service · Invalidated by purchasing observers. · `app/Services/Purchasing/PurchaserReadCacheService.php`
- [Stock ledger](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Inventory/StockLedgerService.php): inventory service · Records stock mutations as movements. · `app/Services/Inventory/StockLedgerService.php`
- [Batch sorting](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Actions/Inventory/ProcessBatchSortingAction.php): inventory action · Enforces available and already-sorted quantity boundaries. · `app/Actions/Inventory/ProcessBatchSortingAction.php`
- [Stock movements](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Models/StockMovement.php): Eloquent model · `app/Models/StockMovement.php`
- [Warehouse loadouts](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Warehouse/WarehouseScopedLoadoutService.php): warehouse services · Scopes fulfillment work by warehouse and completes loadouts. · `app/Services/Warehouse/WarehouseScopedLoadoutService.php`
- [Loadout state](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Models/ShopOrderLoadoutState.php): Eloquent model · Separate persisted state supports idempotent saves. · `app/Models/ShopOrderLoadoutState.php`
- [Shop requisitions](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Requisition/ShopOrderRevisionService.php): requisition service · Synchronizes revised order quantities and history. · `app/Services/Requisition/ShopOrderRevisionService.php`
- [Delivery review resolution](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Domains/ShopOrder/Actions/ResolveDeliveryReviewAction.php): shop-order action · Resolves delivery discrepancies after handoff. · `app/Domains/ShopOrder/Actions/ResolveDeliveryReviewAction.php`
- [Shop invoicing](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/ShopInvoices/ShopInvoiceService.php): invoice service · Converts fulfilled shop demand into receivables and payment requests. · `app/Services/ShopInvoices/ShopInvoiceService.php`

### Finance & HR

- [Finance journals](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Finance/JournalService.php): finance service · Posts and projects operational accounting events. · `app/Services/Finance/JournalService.php`
- [Cashbook & reconciliation](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Cashbook/TransactionGenerator.php): cashbook services · Generates cashbook transactions and reconciles company payments. · `app/Services/Cashbook/TransactionGenerator.php`
- [HR payroll](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/HR/PayrollService.php): HR service · Manages payroll runs and payments. · `app/Services/HR/PayrollService.php`

### Runtime Services

- **Operational database**: relational database · Eloquent records, migrations, and workflow state.
- [Queue & scheduled work](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Jobs/ProcessInventoryEmptyBatch.php): Laravel async runtime · Processes deferred inventory and operational tasks. · `app/Jobs/ProcessInventoryEmptyBatch.php`
- [Database backup delivery](https://github.com/mhdniyas/green-leaf-erp/blob/master/app/Services/Backup/DatabaseBackupService.php): backup service · Creates backups for email delivery. · `app/Services/Backup/DatabaseBackupService.php`

## Connections

- Web workspaces → Policy authorization: authorizes
- Web workspaces → Procurement lifecycle: drives
- Web workspaces → Stock ledger: drives
- Web workspaces → Warehouse loadouts: drives
- Versioned operations API → API version boundary: passes through
- API version boundary → Stock ledger: inventory operations
- API version boundary → Procurement lifecycle: purchasing operations
- Procurement lifecycle → GRN approval: approves GRNs
- GRN approval → Stock ledger: receives stock
- Daily price sync → Procurement lifecycle: synchronizes prices
- Procurement lifecycle → Purchaser read cache: serves purchaser reads
- Stock ledger → Batch sorting: processes batches
- Stock ledger → Stock movements: writes ledger
- Shop requisitions → Warehouse loadouts: creates fulfillment demand
- Warehouse loadouts → Loadout state: persists progress
- Warehouse loadouts → Delivery review resolution: hands off deliveries
- Delivery review resolution → Shop invoicing: confirmed fulfillment
- Shop invoicing → Finance journals: posts receivables
- HR payroll → Finance journals: posts payroll
- Finance journals → Cashbook & reconciliation: generates transactions
- Procurement lifecycle → Operational database: persists records
- Stock ledger → Operational database: persists stock state
- Warehouse loadouts → Operational database: persists fulfillment
- Finance journals → Operational database: persists journals
- Queue & scheduled work → Stock ledger: deferred processing
- Queue & scheduled work → Daily price sync: scheduled execution
- Database backup delivery → Operational database: backs up
