# DATABASE RELATION DIAGRAM – ERPdash

This document describes the main table relationships in ERPdash.
It is a textual ERD guide for developers and AI agents.

---

# Core Relationships

## branches
- hasMany settings
- hasMany number_sequences
- hasMany cost_centers
- hasMany journals
- hasMany journal_entries

## settings
- belongsTo branch (nullable)

## number_sequences
- belongsTo branch (nullable)

## fiscal_years
- hasMany fiscal_periods
- hasMany journal_entries
- hasMany account_period_balances

## fiscal_periods
- belongsTo fiscal_year
- hasMany journal_entries
- hasMany account_period_balances

---

# Finance Relationships

## cost_centers
- belongsTo parent cost_center (nullable)
- hasMany children cost_centers
- belongsTo branch (nullable)
- hasMany journal_entry_lines
- hasMany account_period_balances (optional by design)

## accounts
- belongsTo parent account (nullable)
- hasMany children accounts
- hasMany journal_entry_lines
- hasMany account_period_balances

## journals
- belongsTo branch (nullable)
- hasMany journal_entries

## journal_entries
- belongsTo journal
- belongsTo branch
- belongsTo fiscal_year
- belongsTo fiscal_period
- hasMany journal_entry_lines

## journal_entry_lines
- belongsTo journal_entry
- belongsTo account
- belongsTo cost_center (nullable)

## account_period_balances
- belongsTo branch
- belongsTo fiscal_year
- belongsTo fiscal_period
- belongsTo account
- belongsTo cost_center (nullable)

Unique combination should support:
- branch_id
- fiscal_year_id
- fiscal_period_id
- account_id
- cost_center_id (nullable handling required for MySQL uniqueness)

---

# Sales Relationships (future)

## customers
- hasMany sales_orders
- hasMany invoices
- hasMany collections

## sales_orders
- belongsTo customer
- hasMany sales_order_lines

## invoices
- belongsTo customer
- hasMany invoice_lines
- may create journal_entries later

---

# Inventory Relationships (future)

## warehouses
- hasMany stock_movements

## items
- hasMany stock_movements
- may appear in sales/invoice lines later

## stock_movements
- belongsTo warehouse
- belongsTo item
- may create journal_entries later

---

# HR Relationships (future)

## employees
- belongsTo branch
- hasMany attendance records
- hasMany payroll records

## payroll
- belongsTo employee
- may create journal_entries later

---

# Notes

- All translated fields should be stored as JSON.
- Foreign keys must be indexed.
- Use clear belongsTo / hasMany relations in models.
- Avoid adding hidden relationships not documented here unless explicitly needed.

