# Project Progress

## Overview

**Project Progress** is a consolidated view of project reports, field visits, final visits, and the overall project completion percentage.

The Project Progress is divided into the following sections:

1. **Project Updates**
2. **Final Report**
3. **Visit Documentation Log**
4. **Field Visits**
5. **Final Report Details**

---

# 1. Project Updates

The **Project Updates** tab provides a consolidated view of the project's reports and overall completion.

## Project Progress

The overall project progress is retrieved directly from:

```php
$project->overall_completion_percentage
```

---

## Design Review Stage

The Design Review stage percentage is retrieved from:

```php
config('report.rd0.weight_percentage_text')
```

### RD0

RD0 is considered **Completed** when a final RD0 report exists.
***if not RD0,RD1,RD2,DR,Balady are Not Complete***
```text
Completed = Final RD0 exists
```

### RD1

RD1 is considered **Completed** when a final RD1 report exists.

```text
Completed = Final RD1 exists
```

### RD2

RD2 is considered **Completed** when a final RD2 report exists.

```text
Completed = Final RD2 exists
```

### DR

DR is considered **Completed** when a final DR report exists.

```text
Completed = Final DR exists
```

### Balady Report

Balady Report is considered **Completed** when a Balady Report exists.

```text
Completed = Balady Report exists
```

---

# 2. Final Report

The Final Report section handles the project's Final Visit and the RD6 / Unified Balady / RD3 completion.

> The Final Visit does not have a skip percentage.
>
> When RD6 is completed, the Final Visit percentage is automatically raised to `100%`.
> 
***get weight From***
```php
config('report.TECHNICAL_CERTIFICATE.all')
```
## Final Visit

### Total

```php
calculateProjectProgressForPhase($phaseId)['total']
```

### Completed

```php
calculateProjectProgressForPhase($phaseId)['done']
```

### Remaining

```php
calculateProjectProgressForPhase($phaseId)['remainingVisits']
```
### Progress

```php
calculateProjectProgressForPhase($phaseId)['donePercentage']
```

---

## RD6 / Unified Balady (UBR) / RD3

RD6, Unified Balady (UBR), and RD3 are considered completed when an RD6 report exists and its inspection review status is approved.

```text
Completed =
    RD6 exists
    AND
    rd6.ins_rev_status == APPROVE
```

The same completion state is used for:

* RD6
* Unified Balady (UBR)
* RD3

---

# 3. Visit Documentation Log

The **Visit Documentation Log** records the project's progress at every recorded visit.

Each recorded visit in any of the six stages creates a documentation row containing:

* Visit number
* Visit stage
* Project progress at the time of the visit

The stored progress value is a **snapshot**.

Once recorded, the value remains fixed even if the project's current progress changes later.

## Source Reports

The Visit Documentation Log retrieves:

* All Inspection Reports
* Final Reports
* Excludes reports where:

```text
r_result = RD5_MS
```

Conceptually:

```text
Reports =
    All Inspection Reports
    + Final Reports
    where r_result != RD5_MS
```

---

## Visit

The visit stage is retrieved from the report's stage.

```text
Visit = Stage Name
```

---

## Project Progress at Visit

The project progress recorded for the visit is retrieved from:

```php
$report->project_progress_percentage
```

This value represents the project's completion percentage **at the moment the visit was recorded**.

```text
Project Progress at Visit =
    report.project_progress_percentage
```

---

## Undo Latest Visit

When the latest visit is undone, its corresponding latest Visit Documentation Log entry is also removed.

This ensures that the documentation log remains synchronized with the actual recorded visits.

---

# 4. Field Visits

Field Visits represent the progress of the project's individual phases.

RD5 MS reports are used to record the **skip percentage** for each phase.

Multiple RD5 MS reports can be added for the same phase.

The combined skip percentage and completed visit progress for a phase cannot exceed `100%`.

---

## Available Phases

The system retrieves all phases where:

```text
View = true
```

except:

```text
Technical Certificate of Approval and Missing Stage
```

The Technical Certificate of Approval phase is handled separately under the Final Report section.

---

# Phase Information

For every available phase, the following information is displayed:

* Total Weight
* Phase Weight
* Percentage
* Total Visits
* Completed Visits
* Skip Percentage
* Remaining Percentage

---

## Total Weight

The weight of each phase is retrieved from:

```php
config("report.phases.$phaseId.weight_percentage")
```

The total weight is calculated using the phase weight multiplied by the total number of available phases.

Currently, the Field Visits section contains **5 phases**.

```text
Total Weight =
    config("report.phases.$phaseId.weight_percentage")
    ×
    count(all phases)
```

---

## Phase Weight

The weight of each individual phase is retrieved from:

```php
config("report.phases.$phaseId.weight_percentage")
```

Example:

```php
$weight = config("report.phases.$phaseId.weight_percentage");
```

---

# Phase Percentage

The phase percentage is calculated using the completed percentage after removing the skip percentage.

```text
Percentage =
    Done Percentage After Skip
    /
    Phase Weight
```

The calculation uses the result of:

```php
calculateProjectProgressForPhase($phaseId)['donePercentage']
```

---

# Total Visits

The total number of visits for a phase is calculated using:

```text
project.projectColumnPhase
+
additionalVisitQuotationCountForPhase
```

The calculation is handled by:

```php
getTotalVisitsForPhase()
```

Conceptually:

```text
Total Visits =
    Project Column Phase
    +
    Additional Visit Quotation Count
```

---

# Completed Visits

Completed visits are retrieved from Inspection Reports where:


```php
calculateProjectProgressForPhase($phaseId)['done']
```


RD5 MS reports are not counted as completed visits.

---

# Skip Percentage

The skip percentage represents the percentage skipped for the current phase through RD5 MS reports.

The value is calculated using:

```php
calculateProjectProgressForPhase($phaseId)['skippingPercentage']
```

Conceptually:

```text
Skip Percentage =
    Sum of RD5 MS skip percentages for the phase
```

Multiple RD5 MS reports for the same phase are included in this calculation.

---

# Remaining Percentage

The remaining percentage is calculated after applying the completed progress and removing the skip percentage.

The value is retrieved from:

```php
calculateProjectProgressForPhase($phaseId)['reminingPercentage']
```

Conceptually:

```text
Remaining =
    100
    -
    (
        Done Percentage After Calculate New Total
        -
        Skip Percentage
    )
```

Where:

```php
$skippingPercentage =
    calculateProjectProgressForPhase($phaseId)['skippingPercentage'];
```

and:

```php
$remainingPercentage =
    calculateProjectProgressForPhase($phaseId)['reminingPercentage'];
```

---

# Total After Skip Calculation

The calculated total after applying the skip percentage is retrieved from:

```php
calculateProjectProgressForPhase($phaseId)['totalAfterCalculateSkippingPercentage']
```

This value represents the phase's calculated progress after considering the configured visits and skip percentages.

---

# Phase Progress Calculation

The main calculation method for Field Visits is:

```php
calculateProjectProgressForPhase($phaseId)
```

The method provides the calculated values required by the Field Visits section, including:

```php
[
    'skippingPercentage',
    'reminingPercentage',
    'totalAfterCalculateSkippingPercentage',
]
```

---

# 5. Final Report Details

The Final Report section handles the Technical Certificate of Approval and RD6-related completion.

---

## Technical Certificate Weight

The Technical Certificate of Approval weight is retrieved from:

```php
config("report.phases.TECHNICAL_CERTIFICATE.all")
```

---

# Final Visit

The Final Visit contributes the Technical Certificate weight when:

1. The report's `stage_id` represents the Final Visit.
2. A Final report exists.

When both conditions are satisfied:

```text
Final Visit =
    config("report.phases.TECHNICAL_CERTIFICATE.weight_percentage")
```

Otherwise:

```text
Final Visit = 0
```

Conceptually:

```text
IF
    report.stage_id = Final Visit
    AND
    Final report exists

THEN
    Final Visit =
        config("report.phases.TECHNICAL_CERTIFICATE.weight_percentage")

ELSE
    Final Visit = 0
```

---

# RD6

RD6 contributes to the Technical Certificate progress when an RD6 report exists and:

```text
rd6.ins_rev_status == APPROVED
```

The RD6 percentage is retrieved from:

```php
config("report.phases.TECHNICAL_CERTIFICATE.rd6_unified_balady")
```

Therefore:

```text
IF
    RD6 exists
    AND
    rd6.ins_rev_status == APPROVED

THEN
    RD6 =
        config("report.phases.TECHNICAL_CERTIFICATE.rd6_unified_balady")
```

Otherwise:

```text
RD6 = 0
```

---

# RD6 / UBR / RD3

The `rd6_ubr__rd3` field represents whether the RD6 / Unified Balady / RD3 requirement has been completed.

It is a boolean value.

```text
rd6_ubr__rd3 = true
```

when:

```text
RD6 exists
AND
rd6.ins_rev_status == APPROVED
```

Otherwise:

```text
rd6_ubr__rd3 = false
```

---

# Configuration Reference

The Project Progress implementation depends on the following configuration values.

## Design Review

```php
config('report.rd0.weight_percentage_text')
```

## Phase Weight

```php
config("report.phases.$phaseId.weight_percentage")
```

## Technical Certificate Weight

```php
config("report.phases.TECHNICAL_CERTIFICATE.weight_percentage")
```

## RD6 Unified Balady Weight

```php
config("report.phases.TECHNICAL_CERTIFICATE.rd6_unified_balady")
```

---

# Progress Data Sources

| Progress Item            | Source                                                                 |
| ------------------------ | ---------------------------------------------------------------------- |
| Overall Project Progress | `$project->overall_completion_percentage`                              |
| Design Review            | `config('report.rd0.weight_percentage_text')`                          |
| RD0                      | Final RD0 exists                                                       |
| RD1                      | Final RD1 exists                                                       |
| RD2                      | Final RD2 exists                                                       |
| DR                       | Final DR exists                                                        |
| Balady Report            | Balady Report exists                                                   |
| Final Visit Total        | `calculateProjectProgressForPhase($phaseId)['total']`                  |
| Final Visit Completed    | `calculateProjectProgressForPhase($phaseId)['done']`                   |
| Final Visit Remaining    | `calculateProjectProgressForPhase($phaseId)['remainingVisits']`        |
| Final Visit Progress     | `calculateProjectProgressForPhase($phaseId)['donePercentage']`         |
| RD6                      | Approved RD6                                                            |
| UBR                      | Approved RD6                                                            |
| RD3                      | Approved RD6                                                            |
| Visit Documentation      | Inspection + Final Reports excluding RD5 MS                            |
| Visit Progress           | `$report->project_progress_percentage`                                 |
| Available Field Phases   | `View = true`, excluding Technical Certificate and Missing Stage      |
| Field Visit Total        | `getTotalVisitsForPhase()`                                             |
| Field Visit Completed    | `calculateProjectProgressForPhase($phaseId)['done']`                   |
| Field Visit Skip         | `calculateProjectProgressForPhase($phaseId)['skippingPercentage']`     |
| Field Visit Remaining    | `calculateProjectProgressForPhase($phaseId)['reminingPercentage']`     |
| Field Visit Total After Skip | `calculateProjectProgressForPhase($phaseId)['totalAfterCalculateSkippingPercentage']` |
| Technical Certificate    | `config("report.phases.TECHNICAL_CERTIFICATE.all")` + Final Visit + RD6 |
---

# Business Rules

## 1. Final Reports

A stage is considered completed when its corresponding Final Report exists.

```text
Final Report exists => Stage Completed
```

---

## 2. RD5 MS

RD5 MS reports are used to calculate skip percentages.

They are **not counted as completed visits**.

```text
r_result = RD5_MS
```

is excluded from completed visit calculations.

---

## 3. Multiple RD5 MS Reports

More than one RD5 MS report can be created for the same phase.

All applicable RD5 MS reports are included when calculating the phase skip percentage.

```text
Total Skip =
    Sum of all applicable RD5 MS skip percentages
```

---

## 4. Maximum Phase Progress

The combined progress of completed visits and skipped percentage for a phase cannot exceed:

```text
100%
```

---

## 5. Final Visit

The Final Visit does not have a skip percentage.

When RD6 is completed, the Final Visit percentage is automatically raised to:

```text
100%
```

---

## 6. Visit Progress Snapshot

The progress recorded in the Visit Documentation Log is immutable after the visit is recorded.

```text
report.project_progress_percentage
```

represents the project's progress at the time of the visit.

---

## 7. Undo Visit

Undoing the latest visit removes the latest corresponding Visit Documentation Log entry.

---
## 8. Report Summary

### Completed RD5 MS Reports

Completed RD5 MS reports are divided into:

* **Active RD5 MS reports**
* **Inactive RD5 MS reports**

```text
Completed RD5 MS Reports =
    Active RD5 MS Reports
    /
    Inactive RD5 MS Reports
```

### Reports Marking Finishing Complete

Finishing progress is calculated from the approved phases in project.project_phase.
A phase is considered approved when its status is `Approve`.

```php
public function approvedPhasesCount(): int
{
    return collect([
        $this->foundation,
        $this->ground_beam,
        $this->column,
        $this->slab,
        $this->waterproofing,
    ])->filter(fn ($status) => $status === 'Approve')->count();
}
```

The method returns the number of approved phases out of the five project phases.

```text
Approved Phases =
    foundation
    + ground_beam
    + column
    + slab
    + waterproofing
```

For example:

```text
Approve      → foundation
Approve      → ground_beam
NotStarted   → column
Approve      → slab
NotStarted   → waterproofing

Approved Phases = 3 / 5
```

---
# Calculation Flow

The overall Project Progress calculation can be summarized as:

```text
Project
 │
 ├── Project Updates
 │    ├── Overall Project Progress
 │    ├── Design Review
 │    ├── RD0
 │    ├── RD1
 │    ├── RD2
 │    ├── DR
 │    └── Balady Report
 │
 ├── Final Report
 │    ├── Final Visit
 │    ├── RD6
 │    ├── UBR
 │    └── RD3
 │
 ├── Visit Documentation Log
 │    ├── Visit Number
 │    ├── Stage
 │    └── Project Progress at Visit
 │
 └── Field Visits
      ├── Phase Weight
      ├── Total Visits
      ├── Completed Visits
      ├── Skip Percentage
      ├── Remaining Percentage
      └── Calculated Phase Progress
```

---

# Main Methods and Properties

The Project Progress implementation depends on the following properties and methods:

```php
$project->overall_completion_percentage

getTotalVisitsForPhase()

calculateProjectProgressForPhase($phaseId)

$report->project_progress_percentage
```

The implementation also uses the following configuration keys:

```php
config('report.rd0.weight_percentage_text')

config("report.phases.$phaseId.weight_percentage")

config("report.phases.TECHNICAL_CERTIFICATE.weight_percentage")

config("report.phases.TECHNICAL_CERTIFICATE.rd6_unified_balady")
```
