# thegreenter/greenter architecture

> Architecture diagram of the GitHub repository thegreenter/greenter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thegreenter/greenter
- Repository: https://github.com/thegreenter/greenter

## Overview

Based on your file tree and the README, this project is a PHP monorepo library called “Greenter” that implements electronic invoicing (Facturación Electrónica) for the Peruvian market. It is not a full-stack web application per se, but rather a collection of interrelated packages that together form the core of an electronic invoicing system. Its purpose is to generate invoices (and other electronic documents) in XML form (following the UBL standard), sign them with a digital certificate, send them to SUNAT web services, and handle the responses (CDR), as well as provide supporting components such as document validation, PDF report generation, and XML parsing/building.

##### Main Components
- Core Module – Found under packages/core. This package defines the main models (for Clients, Companies, Documents, Sales, Despatch, etc.) as well as interfaces for building, factory, parsing, validation, and service interactions.
- Validator Packages – For example, packages/cpe-validator and packages/validator. These handle document validations, error code resolution, custom rules, and loader implementations that ensure document correctness.
- Data Module – Found under packages/data. Provides “store” generators to create various types of documents (invoices, notes, retention, etc.) for testing or demo purposes.
- XML Processing Packages – Two prominent modules:
▪ packages/xml – Contains XML builders (using Twig templates) to generate XML documents conforming to the standard.
▪ packages/xml-parser – Contains parsers for different document types such as invoices, despatches, and notes.
- Web Service (WS) Module – Under packages/ws. This package is responsible for the communication with SUNAT (web service calls), including building SOAP requests, reading responses (CDR), and managing endpoints.
- Report and HTML/PDF Modules – For example, packages/report and packages/htmltopdf. These generate human-readable document representations such as reports or PDFs.
- Lite and Xcodes – Smaller utility or helper packages that facilitate API simplifications (packages/lite) or handle error codes (packages/xcodes).

##### Relationships and Interactions
- The “core” package is the heart of the system that defines basic models and common interfaces. All other packages either extend or interact with these core models.
- When a document (invoice, note, despatch, etc.) is to be generated, one of the Data Generators (from packages/data) will create an instance based on the core model.
- The XML Builder (from packages/xml) takes these models and produces the corresponding XML output using Twig templates.
- Prior to sending, validator modules (packages/validator and packages/cpe-validator) operate to ensure document structure and compliance with required rules.
- The WS module then uses the formatted XML to communicate with external SUNAT web service endpoints. It both sends requests (using classes such as those in packages/ws/Services) and processes responses, often with the aid of XML parsers.
- Optionally, reporting modules (packages/report and packages/htmltopdf) convert electronic documents into PDF or formatted HTML reports.
- Utility packages like lite and xcodes provide lighter weight API wrappers and error code mappings that streamline interactions in the above steps.

##### Architectural Patterns or Principles
- Modularity and Separation of Concerns – The project is structured as a monorepo containing several well-defined packages each handling one aspect of the overall process (core models, XML generation, validation, web service communications, etc.).
- Interface-driven Design – Many packages define and rely on interfaces (e.g., BuilderInterface, FactoryInterface, ValidatorInterface) to allow components to be replaced or extended.
- Integration via Dependency Management – Composer and monorepo-builder assist in managing dependencies between the packages.
- Test-Driven Design – The presence of phpunit.xml.dist and extensive tests across packages indicate a design oriented towards maintainability and reliability.

##### Relevant Technologies and Frameworks
- PHP 7.4 or higher.
- Extensions such as SOAP, cURL, OpenSSL, and Zlib – which indicate cryptographic signing and network communication.
- Twig (implied by the XML builder using Twig templates).
- Tools: GitHub workflows for building and testing, Composer for dependency management.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Core Layer"
        Core["Core Models (packages/core)"]:::core
    end

    subgraph "Document Generation Layer"
        Data["Data Generator (packages/data)"]:::data
    end

    subgraph "XML Processing Layer"
        XMLBuilder["XML Builder (packages/xml)"]:::xml
        XMLParser["XML Parser (packages/xml-parser)"]:::xml
    end

    subgraph "Validation Layer"
        Validator["Validator (packages/validator)"]:::validator
        CpeValidator["CPE Validator (packages/cpe-validator)"]:::validator
    end

    subgraph "Web Service Layer"
        WS["Web Services (packages/ws)"]:::ws
    end

    subgraph "Reporting Layer"
        Report["Report Module (packages/report)"]:::report
        HTMLPDF["HTML/PDF Module (packages/htmltopdf)"]:::report
    end

    subgraph "Utilities"
        Lite["Lite Utilities (packages/lite)"]:::utility
        Xcodes["Xcodes (packages/xcodes)"]:::utility
    end

    Data -->|"build_document"| Core
    Core -->|"pass_model"| XMLBuilder
    XMLBuilder -->|"generate_XML"| Validator
    XMLBuilder -->|"generate_XML"| CpeValidator
    Validator -->|"send_XML"| WS
    CpeValidator -->|"send_XML"| WS
    WS -->|"receive_response"| XMLParser
    XMLParser -->|"parsed_output"| Report
    XMLParser -->|"parsed_output"| HTMLPDF

    %% Utilities support
    Lite ---|"support"| Core
    Xcodes ---|"support"| Validator

    %% Click Events
    click Core "https://github.com/thegreenter/greenter/tree/master/packages/core"
    click Data "https://github.com/thegreenter/greenter/tree/master/packages/data"
    click XMLBuilder "https://github.com/thegreenter/greenter/tree/master/packages/xml"
    click XMLParser "https://github.com/thegreenter/greenter/tree/master/packages/xml-parser"
    click Validator "https://github.com/thegreenter/greenter/tree/master/packages/validator"
    click CpeValidator "https://github.com/thegreenter/greenter/tree/master/packages/cpe-validator"
    click WS "https://github.com/thegreenter/greenter/tree/master/packages/ws"
    click Report "https://github.com/thegreenter/greenter/tree/master/packages/report"
    click HTMLPDF "https://github.com/thegreenter/greenter/tree/master/packages/htmltopdf"
    click Lite "https://github.com/thegreenter/greenter/tree/master/packages/lite"
    click Xcodes "https://github.com/thegreenter/greenter/tree/master/packages/xcodes"

    %% Styles
    classDef core fill:#f4d03f,stroke:#333,stroke-width:2px;
    classDef data fill:#aed6f1,stroke:#333,stroke-width:2px;
    classDef xml fill:#abebc6,stroke:#333,stroke-width:2px;
    classDef validator fill:#f1948a,stroke:#333,stroke-width:2px;
    classDef ws fill:#f7dc6f,stroke:#333,stroke-width:2px;
    classDef report fill:#bb8fce,stroke:#333,stroke-width:2px;
    classDef utility fill:#85c1e9,stroke:#333,stroke-width:2px;
```
