> For the complete documentation index, see [llms.txt](https://senselab.gitbook.io/senselab-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://senselab.gitbook.io/senselab-docs/docs/diagrams/05-erd-modulo-contable.md).

# ERD — Módulo Contable

> Relaciones entre Empresa, Cuentas Contables (jerárquicas), Asientos y sus Detalles.

## Diagrama Entidad-Relación

```mermaid
erDiagram
    Empresa ||--o{ CuentaContable : "posee"
    Empresa ||--o{ AsientoContable : "registra"
    Empresa ||--o{ Sucursal : "tiene"
    
    CuentaContable ||--o{ CuentaContable : "cuenta_padre_id"
    CuentaContable }o--|| TipoCuenta : "tipo"
    CuentaContable ||--o{ DetalleAsiento : "movimientos"
    
    AsientoContable ||--o{ DetalleAsiento : "líneas"
    AsientoContable }o--|| Empresa : "empresa_id"
    
    DetalleAsiento }o--|| AsientoContable : "asiento_contable_id"
    DetalleAsiento }o--|| CuentaContable : "cuenta_contable_id"
    
    Venta ||--o| AsientoContable : "genera"
    
    Empresa {
        bigint id PK
        string nombre
        string cedula_juridica
        string tipo_identificacion
        boolean activo
        timestamp creado_en
        timestamp actualizado_en
    }
    
    CuentaContable {
        bigint id PK
        bigint empresa_id FK
        bigint tipo_cuenta_id FK
        bigint cuenta_padre_id FK
        string codigo
        string nombre
        string descripcion
        boolean acepta_movimientos
        boolean activo
    }
    
    TipoCuenta {
        bigint id PK
        string nombre
        string naturaleza
        int orden
    }
    
    AsientoContable {
        bigint id PK
        bigint empresa_id FK
        int numero_asiento
        date fecha_asiento
        string descripcion
        decimal total_debe
        decimal total_haber
        string estado
        boolean activo
    }
    
    DetalleAsiento {
        bigint id PK
        bigint asiento_contable_id FK
        bigint cuenta_contable_id FK
        decimal monto_debe
        decimal monto_haber
        string descripcion
    }
    
    Venta {
        bigint id PK
        bigint empresa_id FK
        bigint cliente_id FK
        string estado_venta
        decimal total
    }
```

## Notas de Diseño

* **CuentaContable** tiene relación **auto-referencial** via `cuenta_padre_id` para soportar el árbol jerárquico (ej: Activo → Circulante → Bancos → Banco X)
* **AsientoContable** valida cuadre: `total_debe == total_haber` mediante `estaCuadrado()`
* **DetalleAsiento** es la tabla pivote que conecta cada línea del asiento con su cuenta contable
* **Venta** genera automáticamente un AsientoContable al completarse
* Todos los modelos usan `BelongsToTenant` → filtro automático por `empresa_id`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://senselab.gitbook.io/senselab-docs/docs/diagrams/05-erd-modulo-contable.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
