# ER図

## 概要

本ドキュメントは、InvenTree Appのエンティティ関連図（ER図）をMermaid形式で記載したものです。

## ローカルストレージ ER図

ローカルデバイスに保存されるデータのER図です。

```mermaid
erDiagram
    profiles {
        int key PK
        string name
        string server
        string token
        bool selected
    }

    settings {
        string key PK
        dynamic value
    }
```

## サーバー側エンティティ ER図

InvenTreeサーバーのデータベースエンティティのER図です。APIを通じてアクセスします。

### コア部品・在庫管理

```mermaid
erDiagram
    Part {
        int pk PK
        string name
        string description
        string IPN
        string revision
        string keywords
        string link
        int category FK
        int default_location FK
        string units
        bool active
        bool assembly
        bool component
        bool purchaseable
        bool salable
        bool trackable
        bool is_template
        bool virtual
        bool testable
        int variant_of FK
        string image
        string thumbnail
        bool starred
        double in_stock
        double ordering
        double building
    }

    PartCategory {
        int pk PK
        string name
        string description
        int parent FK
        string pathstring
        int part_count
        bool structural
    }

    PartTestTemplate {
        int pk PK
        int part FK
        string test_name
        string key
        bool required
        bool requires_value
        bool requires_attachment
    }

    StockItem {
        int pk PK
        int part FK
        int location FK
        double quantity
        string serial
        string batch
        int status
        string packaging
        double purchase_price
        string purchase_price_currency
        int purchase_order FK
        int supplier_part FK
        string link
        bool in_stock
        bool is_building
        double allocated
        int sales_order FK
        int customer FK
        datetime expiry_date
        datetime updated
        datetime stocktake_date
    }

    StockLocation {
        int pk PK
        string name
        string description
        int parent FK
        string pathstring
        int items
        bool structural
    }

    StockItemTestResult {
        int pk PK
        int stock_item FK
        int template FK
        string test
        bool result
        string value
        string notes
        string attachment
        string date
        int user FK
    }

    StockItemHistory {
        int pk PK
        int stock_item FK
        string label
        datetime date
        int user FK
    }

    BomItem {
        int pk PK
        int part FK
        int sub_part FK
        string reference
        double quantity
    }

    Part ||--o{ PartCategory : "belongs to"
    Part ||--o{ PartTestTemplate : "has"
    Part ||--o{ StockItem : "has"
    Part ||--o{ BomItem : "parent of"
    Part ||--o{ BomItem : "sub part of"
    Part }o--|| Part : "variant of"
    StockLocation ||--o{ StockItem : "stores"
    StockLocation }o--|| StockLocation : "parent of"
    PartCategory }o--|| PartCategory : "parent of"
    StockItem ||--o{ StockItemTestResult : "has"
    StockItem ||--o{ StockItemHistory : "has"
    PartTestTemplate ||--o{ StockItemTestResult : "template for"
```

### 会社・サプライヤー管理

```mermaid
erDiagram
    Company {
        int pk PK
        string name
        string description
        string website
        string phone
        string email
        bool is_supplier
        bool is_manufacturer
        bool is_customer
        bool active
        string currency
        string image
        string thumbnail
    }

    SupplierPart {
        int pk PK
        int part FK
        int supplier FK
        string SKU
        int manufacturer_part FK
        string link
        string note
        string packaging
        string pack_quantity
        bool active
        double in_stock
        double on_order
    }

    ManufacturerPart {
        int pk PK
        int part FK
        int manufacturer FK
        string MPN
        string link
    }

    Part ||--o{ SupplierPart : "supplied by"
    Part ||--o{ ManufacturerPart : "manufactured by"
    Company ||--o{ SupplierPart : "supplies"
    Company ||--o{ ManufacturerPart : "manufactures"
    ManufacturerPart ||--o{ SupplierPart : "referenced by"
```

### 発注管理

```mermaid
erDiagram
    PurchaseOrder {
        int pk PK
        string reference
        string description
        int supplier FK
        string supplier_reference
        int status
        int project_code FK
        int destination FK
        string start_date
        string target_date
        string creation_date
        string issue_date
        string complete_date
        string link
        int responsible FK
        int contact FK
        double total_price
        string order_currency
        int line_items
        bool overdue
    }

    POLineItem {
        int pk PK
        int order FK
        int part FK
        string reference
        double quantity
        double received
        double purchase_price
        string purchase_price_currency
        int destination FK
        string notes
        string link
        string target_date
        bool overdue
    }

    POExtraLineItem {
        int pk PK
        int order FK
        string reference
        string description
        double quantity
        double price
        string price_currency
        string link
        string notes
    }

    Company ||--o{ PurchaseOrder : "supplier of"
    PurchaseOrder ||--o{ POLineItem : "contains"
    PurchaseOrder ||--o{ POExtraLineItem : "contains"
    SupplierPart ||--o{ POLineItem : "ordered as"
    StockLocation ||--o{ PurchaseOrder : "destination of"
    ProjectCode ||--o{ PurchaseOrder : "assigned to"
```

### 受注管理

```mermaid
erDiagram
    SalesOrder {
        int pk PK
        string reference
        string description
        int customer FK
        string customer_reference
        int status
        int project_code FK
        string start_date
        string target_date
        string creation_date
        string shipment_date
        string link
        int responsible FK
        int contact FK
        double total_price
        string order_currency
        int line_items
        int shipments_count
        bool overdue
    }

    SOLineItem {
        int pk PK
        int order FK
        int part FK
        string reference
        double quantity
        double allocated
        double shipped
        double sale_price
        string sale_price_currency
        string notes
        string link
        string target_date
        double available_stock
        bool overdue
    }

    SOExtraLineItem {
        int pk PK
        int order FK
        string reference
        string description
        double quantity
        double price
        string price_currency
        string link
        string notes
    }

    SalesOrderShipment {
        int pk PK
        int order FK
        string reference
        string tracking_number
        string invoice_number
        string link
        string shipment_date
        string delivery_date
        int checked_by FK
    }

    SalesOrderAllocation {
        int pk PK
        int order FK
        int line FK
        int item FK
        int shipment FK
        double quantity
    }

    Company ||--o{ SalesOrder : "customer of"
    SalesOrder ||--o{ SOLineItem : "contains"
    SalesOrder ||--o{ SOExtraLineItem : "contains"
    SalesOrder ||--o{ SalesOrderShipment : "shipped via"
    Part ||--o{ SOLineItem : "ordered"
    SOLineItem ||--o{ SalesOrderAllocation : "allocated from"
    StockItem ||--o{ SalesOrderAllocation : "allocated to"
    SalesOrderShipment ||--o{ SalesOrderAllocation : "shipped in"
    ProjectCode ||--o{ SalesOrder : "assigned to"
```

### 共通エンティティ

```mermaid
erDiagram
    Attachment {
        int pk PK
        string model_type
        int model_id
        string attachment
        string link
        string comment
        datetime upload_date
    }

    Parameter {
        int pk PK
        string model_type
        int model_id
        int template FK
        string data
    }

    ProjectCode {
        int pk PK
        string code
        string description
    }

    Notification {
        int pk PK
        string name
        string message
        datetime creation
        bool read
    }

    Plugin {
        int pk PK
        string key
        string name
        bool active
    }

    GlobalSetting {
        int pk PK
        string key
        string value
        string type
    }

    UserSetting {
        int pk PK
        string key
        string value
        string type
    }
```

## 全体関連図（簡略版）

```mermaid
erDiagram
    PartCategory ||--o{ Part : contains
    Part ||--o{ StockItem : "has stock"
    Part ||--o{ SupplierPart : "supplied by"
    Part ||--o{ ManufacturerPart : "manufactured by"
    Part ||--o{ BomItem : "BOM parent"
    Part ||--o{ BomItem : "BOM child"
    Part ||--o{ PartTestTemplate : "has tests"
    Part ||--o{ SOLineItem : "sold as"

    StockLocation ||--o{ StockItem : stores
    StockItem ||--o{ StockItemTestResult : "test results"
    StockItem ||--o{ SalesOrderAllocation : "allocated to"

    Company ||--o{ SupplierPart : "as supplier"
    Company ||--o{ ManufacturerPart : "as manufacturer"
    Company ||--o{ PurchaseOrder : "supplier for"
    Company ||--o{ SalesOrder : "customer for"

    SupplierPart ||--o{ POLineItem : "ordered as"

    PurchaseOrder ||--o{ POLineItem : contains
    SalesOrder ||--o{ SOLineItem : contains
    SalesOrder ||--o{ SalesOrderShipment : "shipped via"

    SOLineItem ||--o{ SalesOrderAllocation : "allocated from"
    SalesOrderShipment ||--o{ SalesOrderAllocation : "shipped in"

    ProjectCode ||--o{ PurchaseOrder : "assigned to"
    ProjectCode ||--o{ SalesOrder : "assigned to"
```
