# Patrol Management System Flow Diagram

## System Architecture Flow

```mermaid
graph TB
    %% Client Setup Layer
    Client[Client Setup] --> Contract[Client Contract]
    Contract --> Service[Client Services]
    
    %% Service Configuration Layer
    Service --> Checkpoint[Patrol Checkpoints]
    Checkpoint --> QR[QR Code Generation]
    
    %% Employee Assignment Layer
    Employee[Employee Management] --> Assignment[Employee Assignment]
    Assignment --> Schedule[Patrol Scheduling]
    Schedule --> TimeSlot[Time Slots]
    
    %% Patrol Execution Layer
    QR --> Scan[QR Scanning]
    TimeSlot --> Scan
    Assignment --> Scan
    
    %% Validation Layer
    Scan --> Validation[Multi-Level Validation]
    Validation --> ContractCheck[Contract Validation]
    Validation --> ServiceCheck[Service Validation]
    Validation --> ScheduleCheck[Schedule Validation]
    Validation --> GeofenceCheck[Geofence Validation]
    
    %% Recording Layer
    Validation --> Log[Patrol Log]
    Validation --> Violation[Patrol Violation]
    Validation --> QRScan[QR Scan Record]
    
    %% Reporting Layer
    Log --> Report[Patrol Reports]
    Violation --> Report
    Report --> Workflow[Approval Workflow]
    Workflow --> Export[Export Options]
    
    %% Styling
    classDef clientLayer fill:#e1f5fe
    classDef serviceLayer fill:#f3e5f5
    classDef employeeLayer fill:#e8f5e8
    classDef executionLayer fill:#fff3e0
    classDef validationLayer fill:#ffebee
    classDef recordingLayer fill:#f1f8e9
    classDef reportingLayer fill:#fce4ec
    
    class Client,Contract clientLayer
    class Service,Checkpoint,QR serviceLayer
    class Employee,Assignment,Schedule,TimeSlot employeeLayer
    class Scan executionLayer
    class Validation,ContractCheck,ServiceCheck,ScheduleCheck,GeofenceCheck validationLayer
    class Log,Violation,QRScan recordingLayer
    class Report,Workflow,Export reportingLayer
```

## Detailed Process Flow

### 1. Client Setup Flow
```mermaid
flowchart TD
    Start([Start]) --> CreateClient[Create Client]
    CreateClient --> CreateContract[Create Client Contract]
    CreateContract --> DefineScope[Define Service Scope]
    DefineScope --> ActivateContract[Activate Contract]
    ActivateContract --> End([Client Ready])
    
    classDef setup fill:#e3f2fd
    class Start,End,CreateClient,CreateContract,DefineScope,ActivateContract setup
```

### 2. Service Configuration Flow
```mermaid
flowchart TD
    Start([Start]) --> SelectClient[Select Client]
    SelectClient --> CreateService[Create Service]
    CreateService --> DefineLocation[Define Location/Zone]
    DefineLocation --> SetServiceType[Set Service Type]
    SetServiceType --> AddCheckpoints[Add Checkpoints]
    AddCheckpoints --> GenerateQR[Generate QR Codes]
    GenerateQR --> End([Service Ready])
    
    classDef service fill:#f3e5f5
    class Start,End,SelectClient,CreateService,DefineLocation,SetServiceType,AddCheckpoints,GenerateQR service
```

### 3. Employee Assignment Flow
```mermaid
flowchart TD
    Start([Start]) --> SelectService[Select Service]
    SelectService --> ChooseEmployee[Choose Employee]
    ChooseEmployee --> SetAssignment[Set Assignment Type]
    SetAssignment --> DefinePeriod[Define Assignment Period]
    DefinePeriod --> ValidateAccess[Validate Access Rights]
    ValidateAccess --> ConfirmAssignment[Confirm Assignment]
    ConfirmAssignment --> End([Employee Assigned])
    
    classDef employee fill:#e8f5e8
    class Start,End,SelectService,ChooseEmployee,SetAssignment,DefinePeriod,ValidateAccess,ConfirmAssignment employee
```

### 4. Patrol Scheduling Flow
```mermaid
flowchart TD
    Start([Start]) --> SelectService[Select Service]
    SelectService --> CreateSchedule[Create Schedule]
    CreateSchedule --> SetPattern[Set Repeat Pattern]
    SetPattern --> AddTimeSlots[Add Time Slots]
    AddTimeSlots --> AssignCheckpoints[Assign Checkpoints]
    AssignCheckpoints --> ValidateSchedule[Validate Schedule]
    ValidateSchedule --> ActivateSchedule[Activate Schedule]
    ActivateSchedule --> End([Schedule Active])
    
    classDef schedule fill:#fff3e0
    class Start,End,SelectService,CreateSchedule,SetPattern,AddTimeSlots,AssignCheckpoints,ValidateSchedule,ActivateSchedule schedule
```

### 5. QR Scanning & Validation Flow
```mermaid
flowchart TD
    Start([Employee Scans QR]) --> ReadQR[Read QR Code]
    ReadQR --> ValidateQR{QR Valid?}
    ValidateQR -->|No| QRInvalid[QR Invalid/Expired]
    ValidateQR -->|Yes| CheckContract{Contract Active?}
    CheckContract -->|No| ContractInactive[Contract Inactive]
    CheckContract -->|Yes| CheckAssignment{Employee Assigned?}
    CheckAssignment -->|No| NotAssigned[Employee Not Assigned]
    CheckAssignment -->|Yes| CheckSchedule{Within Schedule?}
    CheckSchedule -->|No| ScheduleViolation[Schedule Violation]
    CheckSchedule -->|Yes| CheckGeofence{Within Geofence?}
    CheckGeofence -->|No| GeofenceViolation[Geofence Violation]
    CheckGeofence -->|Yes| RecordScan[Record Successful Scan]
    
    RecordScan --> CreateLog[Create Patrol Log]
    QRInvalid --> CreateViolation[Create Violation Record]
    ContractInactive --> CreateViolation
    NotAssigned --> CreateViolation
    ScheduleViolation --> CreateViolation
    GeofenceViolation --> CreateViolation
    
    CreateLog --> Success([Patrol Recorded])
    CreateViolation --> Violation([Violation Recorded])
    
    classDef success fill:#c8e6c9
    classDef violation fill:#ffcdd2
    classDef validation fill:#e1f5fe
    
    class Start,ReadQR,CheckContract,CheckAssignment,CheckSchedule,CheckGeofence validation
    class RecordScan,CreateLog,Success success
    class QRInvalid,ContractInactive,NotAssigned,ScheduleViolation,GeofenceViolation,CreateViolation,Violation violation
```

### 6. Reporting & Approval Flow
```mermaid
flowchart TD
    Start([Generate Report]) --> SelectParams[Select Report Parameters]
    SelectParams --> GatherData[Gather Patrol Data]
    GatherData --> CalculateStats[Calculate Statistics]
    CalculateStats --> GenerateReport[Generate Report]
    GenerateReport --> SaveDraft[Save as Draft]
    SaveDraft --> MarkPrepared[Mark as Prepared]
    MarkPrepared --> NotifyChecker[Notify Checker]
    
    NotifyChecker --> Review{Review Report}
    Review -->|Reject| RejectReport[Reject Report]
    Review -->|Approve| MarkChecked[Mark as Checked]
    Review -->|Request Changes| RequestChanges[Request Changes]
    
    MarkChecked --> NotifyApprover[Notify Approver]
    RequestChanges --> EditReport[Edit Report]
    EditReport --> MarkPrepared
    
    NotifyApprover --> FinalReview{Final Review}
    FinalReview -->|Reject| RejectReport
    FinalReview -->|Approve| MarkApproved[Mark as Approved]
    
    MarkApproved --> LockReport[Lock Report]
    LockReport --> ExportOptions[Export Options]
    ExportOptions --> End([Report Complete])
    
    RejectReport --> End
    
    classDef process fill:#e8eaf6
    classDef approval fill:#f3e5f5
    classDef export fill:#e0f2f1
    
    class Start,SelectParams,GatherData,CalculateStats,GenerateReport,SaveDraft,MarkPrepared,NotifyChecker process
    class Review,MarkChecked,NotifyApprover,FinalReview,MarkApproved approval
    class LockReport,ExportOptions,End export
    class RejectReport,RequestChanges,EditReport fill:#ffebee
```

## Data Flow Architecture

### Client Isolation Flow
```mermaid
graph LR
    subgraph "Tenant 1"
        C1[Client A] --> S1[Service A1]
        C1 --> S2[Service A2]
        S1 --> CP1[Checkpoint A1-1]
        S1 --> CP2[Checkpoint A1-2]
        S2 --> CP3[Checkpoint A2-1]
    end
    
    subgraph "Tenant 2"
        C2[Client B] --> S3[Service B1]
        C2 --> S4[Service B2]
        S3 --> CP4[Checkpoint B1-1]
        S3 --> CP5[Checkpoint B1-2]
        S4 --> CP6[Checkpoint B2-1]
    end
    
    subgraph "Data Isolation"
        T1[Database: Tenant 1]
        T2[Database: Tenant 2]
    end
    
    C1 -.-> T1
    C2 -.-> T2
    
    classDef tenant1 fill:#e3f2fd
    classDef tenant2 fill:#f3e5f5
    classDef isolation fill:#fff3e0
    
    class C1,S1,S2,CP1,CP2,CP3,T1 tenant1
    class C2,S3,S4,CP4,CP5,CP6,T2 tenant2
    class T1,T2 isolation
```

### Security Validation Flow
```mermaid
flowchart TD
    Scan([QR Scan Request]) --> Validate1[1. QR Code Signature]
    Validate1 --> Validate2[2. Contract Status]
    Validate2 --> Validate3[3. Employee Assignment]
    Validate3 --> Validate4[4. Schedule Time Window]
    Validate4 --> Validate5[5. Geofence Location]
    
    Validate5 --> Decision{All Validations Pass?}
    Decision -->|Yes| Success[✓ Record Patrol Log]
    Decision -->|No| Failure[✗ Create Violation]
    
    Success --> LogSuccess[Log: Successful Patrol]
    Failure --> LogFailure[Log: Violation Record]
    
    classDef validation fill:#e1f5fe
    classDef success fill:#c8e6c9
    classDef failure fill:#ffcdd2
    
    class Validate1,Validate2,Validate3,Validate4,Validate5,Decision validation
    class Success,LogSuccess success
    class Failure,LogFailure failure
```

## Integration Points

### Mobile App Integration
```mermaid
graph TB
    subgraph "Mobile App"
        Camera[Camera Access]
        GPS[GPS Location]
        Storage[Local Storage]
    end
    
    subgraph "Backend API"
        QRValidation[QR Validation API]
        LocationValidation[Location Validation]
        ScheduleCheck[Schedule Check API]
        PatrolLog[Patrol Log API]
    end
    
    subgraph "Database"
        QRTable[QR Codes Table]
        LogTable[Patrol Logs Table]
        ViolationTable[Violations Table]
    end
    
    Camera --> QRValidation
    GPS --> LocationValidation
    Storage --> ScheduleCheck
    QRValidation --> QRTable
    LocationValidation --> LogTable
    ScheduleCheck --> ViolationTable
    PatrolLog --> LogTable
    
    classDef mobile fill:#e8f5e8
    classDef api fill:#e3f2fd
    classDef db fill:#fff3e0
    
    class Camera,GPS,Storage mobile
    class QRValidation,LocationValidation,ScheduleCheck,PatrolLog api
    class QRTable,LogTable,ViolationTable db
```

### Report Generation Flow
```mermaid
flowchart TD
    Trigger([Report Trigger]) --> CollectData[Collect Data from Multiple Tables]
    CollectData --> FilterClient[Filter by Client/Service]
    FilterClient --> CalculateMetrics[Calculate Metrics]
    CalculateMetrics --> GenerateCharts[Generate Charts]
    GenerateCharts --> CompileReport[Compile Report]
    CompileReport --> SaveReport[Save to Database]
    SaveReport --> Workflow[Start Approval Workflow]
    
    Workflow --> Check[Check Status]
    Check --> Approve[Approve Status]
    Approve --> Export[Export Options]
    
    Export --> PDF[PDF Export]
    Export --> Excel[Excel Export]
    Export --> Email[Email Report]
    
    classDef process fill:#e1f5fe
    classDef data fill:#f3e5f5
    classDef output fill:#e8f5e8
    
    class Trigger,CollectData,FilterClient,CalculateMetrics,GenerateCharts,CompileReport,SaveReport process
    class Workflow,Check,Approve data
    class Export,PDF,Excel,Email output
```

This comprehensive flow diagram illustrates the complete patrol management system architecture, from client setup through patrol execution to reporting, with emphasis on security validation and client isolation.
