# frappe/agriculture architecture

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

- Interactive diagram: https://gitdiagram.com/frappe/agriculture
- Repository: https://github.com/frappe/agriculture

## Overview

##### Project Type and Purpose
- It is an ERP module for ERPNext built on the Frappe framework.
- The module adds functionalities to track various agricultural entities (crop, crop cycle, fertilizer, soil analysis, etc.) and integrates within the larger ERPNext ecosystem.
- The architecture must capture both the backend components (Python logic, business rules) and the frontend components (JS scripts, interactive dashboards/templates).

##### Analyzing the File Structure
- Notice the top-level “agriculture” folder, which contains the app’s initialization files, configuration files (hooks.py, modules.txt, patches.txt), and setup.py.
- Inside agriculture/agriculture/, there is a nested structure that holds multiple “doctype” directories. Each directory represents a distinct data entity (for example, “crop”, “disease”, “fertilizer”, “soil_analysis”, etc.), and each contains:
– An __init__.py for module initialization.
– A .py file for server-side logic.
– A .js file for client-side logic or dynamic behavior.
– A .json file that likely defines schema/metadata for the doctype.
– Test files to ensure quality.
- Additional directories and folders (e.g., config, templates, workspace) suggest there are configurations for the ERP desk and possibly customized user-interface elements.

##### Important Components
a. Core ERPNext Integration:
– Represent the ERPNext “bench” environment as a container that interfaces with multiple apps.
b. Agriculture Module as an Add-on:
– The agriculture module should be depicted as a separate component that plugs into ERPNext.
c. Domain Entities ("Doctypes"):
– Create a component (or a group of components) for each significant doctype such as Crop, Crop Cycle, Disease, Fertilizer, Plant Analysis, Soil Analysis, Soil Texture, Water Analysis, Weather, etc.
– Indicate that each doctype consists of multiple parts: a Python backend (business logic), a JavaScript frontend (client behavior), JSON definitions (metadata), and associated tests.
d. Configuration and Customization:
– Highlight configuration files (hooks.py, config files) that manage app behavior and integration.
– Include the workspace and template directories that influence the user-interface.
e. Data Flow and Interaction:
– Show dependencies like configuration settings feeding into doctype controllers, and client-side validations based on JavaScript.
f. Testing and Validation:
– Optionally include a note (or a component) that represents the testing suite, illustrating a quality assurance layer.

##### Architectural Patterns and Design Principles
- Follows a modular design where each business entity is isolated into its doctype with dedicated logic.
- Utilizes separation of concerns between backend (Python), frontend (JavaScript), and configuration (JSON, hooks).
- Integration with ERPNext ensures that the module adheres to Frappe’s MVC-like design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    ERP["ERPNext Bench"]:::external
    AGRMODULE["Agriculture Module"]:::module

    %% Agriculture Module Subsystems
    subgraph "Module Config Files"
        HOOKS["Hooks.py"]:::config
        MODULES["Modules.txt"]:::config
        PATCHES["Patches.txt"]:::config
    end

    subgraph "Agriculture Core"
        INIT["__init__.py"]:::core
        AGRI["Agriculture.py"]:::core
        SETUP["Setup.py"]:::core
    end

    subgraph "Doctypes"
        CROP["Crop Doctype (Python,JS,JSON,Tests)"]:::doctype
        CCYCLE["Crop Cycle Doctype (Python,JS,JSON,Tests)"]:::doctype
        DISEASE["Disease Doctype (Python,JS,JSON,Tests)"]:::doctype
        DETECTED["Detected Disease Doctype (Python,JSON)"]:::doctype
        FERTILIZER["Fertilizer Doctype (Python,JS,JSON,Tests)"]:::doctype
        FCONTENT["Fertilizer Content Doctype (Python,JSON)"]:::doctype
        PLANT["Plant Analysis Doctype (Python,JS,JSON,Tests)"]:::doctype
        PLANTCRIT["Plant Analysis Criteria Doctype (Python,JSON)"]:::doctype
        LINKEDPLANT["Linked Plant Analysis Doctype (Python,JSON)"]:::doctype
        SOIL["Soil Analysis Doctype (Python,JS,JSON,Tests)"]:::doctype
        SOILCRIT["Soil Analysis Criteria Doctype (Python,JSON)"]:::doctype
        LINKEDSOIL["Linked Soil Analysis Doctype (Python,JSON)"]:::doctype
        SOILTEX["Soil Texture Doctype (Python,JS,JSON,Tests)"]:::doctype
        SOILTEXCRIT["Soil Texture Criteria Doctype (Python,JSON)"]:::doctype
        LINKEDSOILTEX["Linked Soil Texture Doctype (Python,JSON)"]:::doctype
        WATER["Water Analysis Doctype (Python,JS,JSON,Tests)"]:::doctype
        WATERCRIT["Water Analysis Criteria Doctype (Python,JSON)"]:::doctype
        WEATHER["Weather Doctype (Python,JS,JSON,Tests)"]:::doctype
        WEATHPARAM["Weather Parameter Doctype (Python,JSON)"]:::doctype
        AGRIANALYSIS["Agriculture Analysis Criteria Doctype (Python,JS,JSON,Tests)"]:::doctype
        AGRITASK["Agriculture Task Doctype (Python,JS,JSON,Tests)"]:::doctype
    end

    subgraph "UI / Workspace"
        UI["UI Templates"]:::ui
        WORKSPACE["Workspace Config"]:::ui
    end

    subgraph "App Configurations"
        APPCONF["App & ERP Config (Config files)"]:::config
    end

    %% Connections from ERPNext Bench to Agriculture Module
    ERP -->|"installs"| AGRMODULE

    %% Agriculture Module internal connections
    AGRMODULE -->|"loads"| HOOKS
    AGRMODULE -->|"loads"| MODULES
    AGRMODULE -->|"loads"| PATCHES
    AGRMODULE -->|"initializes"| INIT
    AGRMODULE -->|"executes"| AGRI
    AGRMODULE -->|"sets up"| SETUP
    AGRMODULE -->|"defines"| APPCONF

    %% Agriculture Core drives Doctypes and UI
    AGRI -->|"manages"| CROP
    AGRI --> CCYCLE
    AGRI --> DISEASE
    AGRI --> DETECTED
    AGRI --> FERTILIZER
    AGRI --> FCONTENT
    AGRI --> PLANT
    AGRI --> PLANTCRIT
    AGRI --> LINKEDPLANT
    AGRI --> SOIL
    AGRI --> SOILCRIT
    AGRI --> LINKEDSOIL
    AGRI --> SOILTEX
    AGRI --> SOILTEXCRIT
    AGRI --> LINKEDSOILTEX
    AGRI --> WATER
    AGRI --> WATERCRIT
    AGRI --> WEATHER
    AGRI --> WEATHPARAM
    AGRI --> AGRIANALYSIS
    AGRI --> AGRITASK

    UI -->|"interfaces with"| CROP
    UI --> CCYCLE
    UI --> DISEASE
    UI --> FERTILIZER
    UI --> PLANT
    UI --> SOIL
    UI --> SOILTEX
    UI --> WATER
    UI --> WEATHER
    UI --> AGRITASK

    WORKSPACE -->|"customizes"| UI

    APPCONF -->|"configures"| AGRI

    classDef external fill:#f9e79f,stroke:#b7950b,stroke-width:2px;
    classDef module fill:#d4efdf,stroke:#27ae60,stroke-width:2px;
    classDef config fill:#fadbd8,stroke:#e74c3c,stroke-width:2px;
    classDef core fill:#d6eaf8,stroke:#3498db,stroke-width:2px;
    classDef doctype fill:#fcf3cf,stroke:#f1c40f,stroke-width:2px;
    classDef ui fill:#e8daef,stroke:#8e44ad,stroke-width:2px;

    %% Click Events for Module Config Files
    click HOOKS "https://github.com/frappe/agriculture/blob/develop/agriculture/hooks.py"
    click MODULES "https://github.com/frappe/agriculture/blob/develop/agriculture/modules.txt"
    click PATCHES "https://github.com/frappe/agriculture/blob/develop/agriculture/patches.txt"

    %% Click Events for Agriculture Module Core
    click INIT "https://github.com/frappe/agriculture/blob/develop/agriculture/agriculture/__init__.py"
    click AGRI "https://github.com/frappe/agriculture/blob/develop/agriculture/agriculture/agriculture.py"
    click SETUP "https://github.com/frappe/agriculture/blob/develop/agriculture/agriculture/setup.py"

    %% Click Events for Doctypes
    click CROP "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/crop/"
    click CCYCLE "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/crop_cycle/"
    click DISEASE "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/disease/"
    click DETECTED "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/detected_disease/"
    click FERTILIZER "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/fertilizer/"
    click FCONTENT "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/fertilizer_content/"
    click PLANT "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/plant_analysis/"
    click PLANTCRIT "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/plant_analysis_criteria/"
    click LINKEDPLANT "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/linked_plant_analysis/"
    click SOIL "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/soil_analysis/"
    click SOILCRIT "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/soil_analysis_criteria/"
    click LINKEDSOIL "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/linked_soil_analysis/"
    click SOILTEX "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/soil_texture/"
    click SOILTEXCRIT "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/soil_texture_criteria/"
    click LINKEDSOILTEX "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/linked_soil_texture/"
    click WATER "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/water_analysis/"
    click WATERCRIT "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/water_analysis_criteria/"
    click WEATHER "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/weather/"
    click WEATHPARAM "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/weather_parameter/"
    click AGRIANALYSIS "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/agriculture_analysis_criteria/"
    click AGRITASK "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/doctype/agriculture_task/"

    %% Click Events for UI / Workspace
    click UI "https://github.com/frappe/agriculture/tree/develop/agriculture/templates/pages/"
    click WORKSPACE "https://github.com/frappe/agriculture/tree/develop/agriculture/agriculture/workspace/agriculture/"

    %% Click Event for App Configurations
    click APPCONF "https://github.com/frappe/agriculture/tree/develop/agriculture/config/"
```
