ProjectReport Documentation

Project Financial Reports Controller Documentation

File: /controllers/projectReportController.php

Purpose: Generate comprehensive financial analysis and profitability reports for projects

Last Updated: 2024-12-20

---

๐Ÿ“‹ Overview

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables

Table NamePurposeKey Columns
projectProject master dataid, name, clientid, costcenterid
clientdebtchangeClient payment historyclientid, clientdebtchangeamount, clientdebtchangetype
incomeProject incomecostcenterid, incomeValue
projectexchmaterialMaterial issuedprojectid, totalbuyprice
projectexchmaterialreturnMaterial returnedprojectid, totalbuyprice
expensesProject expensescostcenterid, expensesValue
### Secondary Tables (Referenced)

Table NamePurposeRelationship
clientCustomer dataForeign Key: project.clientid
costcenterCost centersForeign Key: project.costcenterid
supplierSupplier dataReferenced in payments
expenseexchangeAdditional expensesprojectid reference
---

๐Ÿ”ง Key Functions

1. search() - Financial Analysis Engine

Purpose: Calculate complete project financial position

Called By: POST action to ?do=search

Parameters:

Returns: Displays financial analysis report

Database Operations:

Business Logic:

1. Income Calculation:

   foreach ($allincomeData as $income) {
       $incomeData += $income->incomeValue;
   }
   ```

2. **Client Payments Calculation**:
   ```php
   foreach ($allclientDeptChangeDAO as $clientDeptChangeDAO) {
       if($clientDeptChangeDAO->clientdebtchangetype == 0){
           $clientPaid += $clientDeptChangeDAO->clientdebtchangeamount;
       }else{
           $clientPaid -= $clientDeptChangeDAO->clientdebtchangeamount;
       }
   }
   ```

3. **Material Cost Calculation**:
   ```php
   $supplies = $totalallMaterial - $totalallMaterialReturn;
   ```

4. **Total Expenses**:
   ```php
   $proMasrofat = $supplies + $masroofat + $supplierdebtchangeamountto;
   ```

5. **Profit/Loss Analysis**:
   ```php
   $clientPaids = $clientPaid + $incomeData;
   $operation = $clientPaids - abs($proMasrofat);
   
   if ($operation < 0) {
       $operation = abs($operation);
       $txt = 'ุตุงููŠ ุงู„ุฎุณุงุฑุฉ'; // Net Loss
   } else {
       $txt = 'ุตุงููŠ ุงู„ุฑุจุญ'; // Net Profit
   }
   ```

**Example Output**:

Project: Building Construction

Client Payments: 100,000

Income: 5,000

Material Costs: 60,000

Expenses: 20,000

Supplier Payments: 15,000

Total Revenue: 105,000

Total Costs: 95,000

Net Profit: 10,000

---

## ๐Ÿ“Š Main Workflows

### Workflow 1: Project Financial Analysis

1. User selects project

โ†“

2. Load project master data

โ†“

3. Calculate income streams

โ†“

4. Calculate client payments

โ†“

5. Calculate material costs

โ†“

6. Calculate all expenses

โ†“

7. Compute profit/loss

โ†“

8. Display financial report

**Files Involved**:
- View: `/views/default/projectview/report.html`
- Model: `/models/dto/Project.class.php`
- DAO: Multiple DAO classes for financial data

### Workflow 2: Financial Data Aggregation

1. Income Aggregation

โ”œโ”€โ”€ Direct Income (income table)

โ””โ”€โ”€ Client Payments (clientdebtchange)

2. Cost Aggregation

โ”œโ”€โ”€ Material Costs (projectexchmaterial)

โ”œโ”€โ”€ Direct Expenses (expenses)

โ”œโ”€โ”€ Supplier Payments (supplierdebtchange)

โ””โ”€โ”€ Additional Expenses (expenseexchange)

3. Net Position Calculation

โ””โ”€โ”€ Revenue - Costs = Profit/Loss

---

## ๐Ÿ”— File Dependencies

### Includes
php

include("../public/impOpreation.php");

include_once("../public/config.php");

include("../public/include_dao.php");

include_once("dailyentryfun.php");

### Required Models
- Project, Client, Clientdebtchange, Costcenter, Accountstree
- Expenseexchange, Projectexchmaterial, Projectexchmaterialreturn
- Income, Incometype, Supplier, Supplierdebtchange, YoutubeLink

### Related Views
- `/views/default/projectview/report.html` - Financial report display

---

## ๐ŸŽฏ URL Routes & Actions

| Action (`?do=`) | Method | Description | View Template |
|-----------------|--------|-------------|---------------|
| (empty) | GET | Display project selection | report.html |
| search | POST | Generate financial analysis | report.html |
| sucess | GET | Success message | succes.html |
| error | GET | Error message | error.html |

---

## โš ๏ธ Known Issues & Fixes

### Issue 1: Missing Project Validation (Line 205)
**Problem**: No validation if project ID is valid before processing  
**Cause**: Missing input validation  
**Fix**: Add project existence check  
**File**: Line 205-207

### Issue 2: Complex Financial Logic
**Problem**: Complex financial calculations in single function  
**Cause**: Monolithic function design  
**Fix**: Break into smaller calculation functions  
**File**: Lines 210-298

### Issue 3: Hardcoded Values
**Problem**: Hardcoded account IDs (e.g., accountstreeid = 16)  
**Cause**: Configuration not externalized  
**Fix**: Move to configuration file  
**File**: Line 750

---

## ๐Ÿ” Permissions & Security

### Required Permissions
- Project reporting access
- Financial data viewing

### Security Checks
php

include_once("../public/authentication.php");

### Input Validation
- Project ID filtered with `filter_input()`
- Redirect if invalid project selected

---

## ๐Ÿ“ˆ Financial Calculations

### Revenue Components
1. **Client Payments**: Direct payments from clients
2. **Project Income**: Additional income streams
3. **Total Revenue** = Client Payments + Project Income

### Cost Components  
1. **Material Costs**: Net material issued (issued - returned)
2. **Direct Expenses**: Operating expenses
3. **Supplier Payments**: Payments to suppliers
4. **Additional Expenses**: Miscellaneous project costs
5. **Total Costs** = Materials + Expenses + Supplier Payments + Additional

### Profitability Analysis

Net Position = Total Revenue - Total Costs

If Net Position > 0: Net Profit

If Net Position < 0: Net Loss

```

---

๐Ÿ“ Notes

Important Considerations

Future Improvements

Performance Considerations

---

๐Ÿ“š Related Documentation