# アーキテクチャ構成図

## システム全体アーキテクチャ

```mermaid
flowchart TB
    subgraph Client["InvenTree Mobile App"]
        subgraph Presentation["Presentation Layer"]
            HomeWidget["Home Widget"]
            PartWidgets["Part Widgets"]
            StockWidgets["Stock Widgets"]
            OrderWidgets["Order Widgets"]
            CompanyWidgets["Company Widgets"]
            SettingsWidgets["Settings Widgets"]
            BarcodeScanner["Barcode Scanner"]
        end

        subgraph Application["Application Layer"]
            API["InvenTreeAPI\n(Singleton)"]
            BarcodeHandler["BarcodeHandler"]
            APIForm["APIFormField\nDynamic Forms"]
        end

        subgraph Domain["Domain Layer"]
            BaseModel["InvenTreeModel\n(Base Class)"]
            Part["InvenTreePart"]
            Stock["InvenTreeStockItem"]
            StockLocation["InvenTreeStockLocation"]
            PurchaseOrder["InvenTreePurchaseOrder"]
            SalesOrder["InvenTreeSalesOrder"]
            Company["InvenTreeCompany"]
        end

        subgraph Infrastructure["Infrastructure Layer"]
            HTTP["HTTP Client"]
            Sembast["Sembast NoSQL"]
            CacheManager["Cache Manager"]
            SentryClient["Sentry Client"]
        end
    end

    subgraph External["External Systems"]
        InvenTreeServer["InvenTree Server\n(REST API)"]
        SentryService["Sentry\n(Error Monitoring)"]
        DeviceCamera["Device Camera"]
        FileSystem["Local File System"]
    end

    %% Presentation to Application
    HomeWidget --> API
    PartWidgets --> API
    StockWidgets --> API
    OrderWidgets --> API
    CompanyWidgets --> API
    SettingsWidgets --> API
    BarcodeScanner --> BarcodeHandler
    BarcodeHandler --> API

    %% Application to Domain
    API --> BaseModel
    APIForm --> BaseModel
    BaseModel --> Part
    BaseModel --> Stock
    BaseModel --> StockLocation
    BaseModel --> PurchaseOrder
    BaseModel --> SalesOrder
    BaseModel --> Company

    %% Application to Infrastructure
    API --> HTTP
    API --> CacheManager
    API --> SentryClient
    SettingsWidgets --> Sembast

    %% Infrastructure to External
    HTTP --> InvenTreeServer
    SentryClient --> SentryService
    BarcodeScanner --> DeviceCamera
    Sembast --> FileSystem
    CacheManager --> FileSystem
```

## レイヤー構成図

```mermaid
flowchart LR
    subgraph PL["Presentation Layer\n(lib/widget/, lib/settings/)"]
        direction TB
        Widgets["StatefulWidgets\nRefreshableState"]
        UI["UI Components\nDialogs, Snacks, Fields"]
    end

    subgraph AL["Application Layer\n(lib/api.dart, lib/barcode/)"]
        direction TB
        APIClient["InvenTreeAPI"]
        Handlers["BarcodeHandlers"]
        Forms["APIFormField"]
    end

    subgraph DL["Domain Layer\n(lib/inventree/)"]
        direction TB
        Models["InvenTreeModel\nand Subclasses"]
        StatusCodes["Status Codes"]
    end

    subgraph IL["Infrastructure Layer"]
        direction TB
        Network["HTTP/HTTPS"]
        Storage["Sembast DB"]
        External["Sentry, Camera"]
    end

    PL --> AL
    AL --> DL
    AL --> IL
    DL -.-> IL
```

## データフロー図

```mermaid
sequenceDiagram
    participant User as User
    participant Widget as Widget<br/>(Presentation)
    participant API as InvenTreeAPI<br/>(Application)
    participant Model as InvenTreeModel<br/>(Domain)
    participant HTTP as HTTP Client<br/>(Infrastructure)
    participant Server as InvenTree Server<br/>(External)

    User->>Widget: User Action (tap, input)
    Widget->>API: Request data
    API->>HTTP: HTTP Request
    HTTP->>Server: HTTPS Request
    Server-->>HTTP: JSON Response
    HTTP-->>API: APIResponse
    API->>Model: Parse JSON to Model
    Model-->>API: Domain Object
    API-->>Widget: Return data
    Widget->>Widget: setState()
    Widget-->>User: Update UI
```

## バーコードスキャン処理フロー

```mermaid
sequenceDiagram
    participant User as User
    participant Scanner as BarcodeScanner
    participant Handler as BarcodeHandler
    participant API as InvenTreeAPI
    participant Server as InvenTree Server
    participant Navigator as Navigator

    User->>Scanner: Open scanner
    Scanner->>Scanner: Camera preview
    User->>Scanner: Scan barcode
    Scanner->>Handler: processBarcode(data)
    Handler->>API: POST /api/barcode/
    API->>Server: HTTP Request
    Server-->>API: Match result
    API-->>Handler: Response data

    alt Barcode matched
        Handler->>Handler: onBarcodeMatched()
        Handler->>Handler: barcodeSuccessTone()
        Handler->>Navigator: Push detail page
    else Barcode unknown
        Handler->>Handler: onBarcodeUnknown()
        Handler->>Handler: barcodeFailureTone()
        Handler->>User: Show error snackbar
    end
```

## 認証フロー

```mermaid
sequenceDiagram
    participant App as App
    participant Profile as UserProfileDBManager
    participant API as InvenTreeAPI
    participant Server as InvenTree Server

    App->>Profile: getSelectedProfile()
    Profile-->>App: UserProfile

    alt Has Token
        App->>API: connectToServer(profile)
        API->>Server: GET /api/
        Server-->>API: Server info
        API->>Server: GET /api/user/me/
        Server-->>API: User info
        API->>Server: GET /api/user/roles/
        Server-->>API: Roles & Permissions
        API-->>App: Connected
    else No Token
        App->>App: Show login screen
        App->>API: fetchToken(username, password)
        API->>Server: GET /api/user/token/
        Server-->>API: Token
        API->>Profile: updateProfile(token)
        API-->>App: Token saved
    end
```

## ドメインモデル関連図

```mermaid
classDiagram
    class InvenTreeModel {
        +int pk
        +String name
        +String description
        +Map jsondata
        +String URL
        +reload()
        +update()
        +delete()
        +list()
        +get()
        +create()
    }

    class InvenTreePart {
        +String IPN
        +int categoryId
        +String image
        +double inStock
        +getStockItems()
    }

    class InvenTreePartCategory {
        +String pathstring
        +int partcount
    }

    class InvenTreeStockItem {
        +int partId
        +int locationId
        +double quantity
        +String serialNumber
        +String batch
        +adjustStock()
        +transferStock()
    }

    class InvenTreeStockLocation {
        +String pathstring
        +int itemcount
    }

    class InvenTreePurchaseOrder {
        +int supplierId
        +String reference
        +int status
        +receiveLine()
    }

    class InvenTreeSalesOrder {
        +int customerId
        +String reference
        +int status
    }

    class InvenTreeCompany {
        +bool isSupplier
        +bool isCustomer
        +bool isManufacturer
    }

    InvenTreeModel <|-- InvenTreePart
    InvenTreeModel <|-- InvenTreePartCategory
    InvenTreeModel <|-- InvenTreeStockItem
    InvenTreeModel <|-- InvenTreeStockLocation
    InvenTreeModel <|-- InvenTreePurchaseOrder
    InvenTreeModel <|-- InvenTreeSalesOrder
    InvenTreeModel <|-- InvenTreeCompany

    InvenTreePart --> InvenTreePartCategory : belongs to
    InvenTreeStockItem --> InvenTreePart : references
    InvenTreeStockItem --> InvenTreeStockLocation : located at
    InvenTreePurchaseOrder --> InvenTreeCompany : from supplier
    InvenTreeSalesOrder --> InvenTreeCompany : to customer
```

## Widget 継承構造

```mermaid
classDiagram
    class StatefulWidget {
        +createState()
    }

    class State {
        +build()
        +setState()
        +initState()
    }

    class BaseWidgetProperties {
        <<mixin>>
        +appBarActions()
        +getAppBarTitle()
        +getDrawer()
        +getTabs()
        +getTiles()
        +getBody()
        +buildAppBar()
        +buildBottomAppBar()
        +actionButtons()
        +barcodeButtons()
    }

    class RefreshableState {
        +scaffoldKey
        +refreshKey
        +loading
        +api
        +onBuild()
        +request()
        +refresh()
    }

    State <|-- RefreshableState
    BaseWidgetProperties <|.. RefreshableState

    class PartDetailWidget
    class StockDetailWidget
    class PurchaseOrderDetailWidget
    class SalesOrderDetailWidget

    RefreshableState <|-- PartDetailWidget
    RefreshableState <|-- StockDetailWidget
    RefreshableState <|-- PurchaseOrderDetailWidget
    RefreshableState <|-- SalesOrderDetailWidget
```

## 設定データ構造

```mermaid
erDiagram
    SETTINGS_STORE {
        string key PK
        dynamic value
    }

    PROFILES_STORE {
        int key PK
        string name
        string server
        string token
    }

    PROFILES_STORE ||--o| SELECTED : "selected record"

    SETTINGS_STORE ||--|| APP_SETTINGS : contains
    APP_SETTINGS {
        bool homeShowPo
        bool homeShowSo
        bool homeShowShipments
        int screenOrientation
        bool barcodeSounds
        bool serverSounds
        int barcodeScanDelay
        int barcodeScanType
        bool strictHttps
        bool reportErrors
    }
```

## パッケージ依存関係

```mermaid
flowchart TB
    subgraph Core["Core Dependencies"]
        Flutter["flutter"]
        Dart["dart:async, dart:io, dart:convert"]
    end

    subgraph UI["UI Dependencies"]
        AdaptiveTheme["adaptive_theme"]
        FlutterSpeedDial["flutter_speed_dial"]
        FlutterTablerIcons["flutter_tabler_icons"]
        DropdownSearch["dropdown_search"]
        DatetimePicker["datetime_picker_formfield"]
    end

    subgraph Network["Network Dependencies"]
        HTTP["http"]
        CachedNetworkImage["cached_network_image"]
        FlutterCacheManager["flutter_cache_manager"]
    end

    subgraph Storage["Storage Dependencies"]
        Sembast["sembast"]
        PathProvider["path_provider"]
    end

    subgraph Hardware["Hardware Dependencies"]
        MobileScanner["mobile_scanner"]
        Camera["camera"]
        ImagePicker["image_picker"]
        AudioPlayers["audioplayers"]
    end

    subgraph Monitoring["Monitoring Dependencies"]
        SentryFlutter["sentry_flutter"]
        PackageInfoPlus["package_info_plus"]
        DeviceInfoPlus["device_info_plus"]
    end

    subgraph Localization["Localization Dependencies"]
        FlutterLocalizations["flutter_localizations"]
        FlutterLocalizedLocales["flutter_localized_locales"]
        Intl["intl"]
    end

    Core --> UI
    Core --> Network
    Core --> Storage
    Core --> Hardware
    Core --> Monitoring
    Core --> Localization
```
