Projectsaccountreport Documentation

Project Accounting Summary Report Controller Documentation

File: /controllers/projectsaccountreport.php

Purpose: Generate comprehensive accounting analysis by expense type with date filtering for project financial management

Last Updated: 2024-12-20

---

๐Ÿ“‹ Overview

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables

Table NamePurposeKey Columns
projectProject masterid, clientid, costcenterid, supervision_amount
clientdebtchangeClient paymentsclientid, clientdebtchangedate, clientdebtchangeamount
incomeProject incomecostcenterid, incomeDate, incomeValue
projectexchmaterialdetailMaterial issues by expense typeexpensestypeid, totalbuyprice, finalsupervision
projectexchmaterialdetailreturnMaterial returns by expense typeexpensestypeid, totalbuyprice, finalsupervision
expenseexchangeAdditional project expensesprojectid, expensetype, thevalue, finalsupervision
expensestypeExpense categoriesexpensestypeid, expensestypename
---

๐Ÿ”ง Key Functions

1. Date-Filtered Financial Analysis

Purpose: Generate accounting report with flexible date filtering

Date Filter Construction:

if ($start_date != '') {
    $clientdebtchangeQuery .=' and clientdebtchange.clientdebtchangedate >= "' . $start_date . ' 00-00-00" ';
    $expenseexchangeQuery .=' and expenseexchange.expensedate >= "' . $start_date . '" ';
    $projectexchmaterialchangeQuery .=' and projectexchmaterial.projdate >= "' . $start_date . '" ';
    $projectexchmaterialreturnQuery .=' and projectexchmaterialreturn.projdate >= "' . $start_date . '" ';
    $incomeQuery .=' and income.incomeDate >= "' . $start_date . '" ';
}

if ($end_date != '') {
    $clientdebtchangeQuery .=' and clientdebtchange.clientdebtchangedate <= "' . $end_date . ' 23-59-55" ';
    $expenseexchangeQuery .=' and expenseexchange.expensedate <= "' . $end_date . '" ';
    $projectexchmaterialchangeQuery .=' and projectexchmaterial.projdate <= "' . $end_date . '" ';
    $projectexchmaterialreturnQuery .=' and projectexchmaterialreturn.projdate <= "' . $end_date . '" ';
    $incomeQuery .=' and income.incomeDate <= "' . $end_date . '" ';
}

2. Expense Type Consolidation Engine

Purpose: Aggregate expenses from multiple sources by expense type

Material Issues by Expense Type:

$projectexchmaterialdetails = R::getAll("select *, sum(totalbuyprice) as totals, sum(finalsupervision) as finalsupervisions from projectexchmaterialdetail JOIN projectexchmaterial
     ON projectexchmaterialdetail.projectexchid = projectexchmaterial.id WHERE projectexchmaterial.projectid = ? $projectexchmaterialchangeQuery group by projectexchmaterialdetail.expensestypeid ",[$project->id]);

foreach ($projectexchmaterialdetails as $projectexchmaterialdetail) {
    $expensestypeids .= ',' . $projectexchmaterialdetail['expensestypeid'];
    $expensestypedata = R::getRow('SELECT expensestype.* FROM `expensestype` WHERE expensestypeid = ? ',[$projectexchmaterialdetail['expensestypeid']]);
    
    // Get additional expenses for same type
    $expenseexchange = R::getROW("select sum(thevalue) as totals, sum(finalsupervision) as finalsupervisions from expenseexchange where del = 0 and projectid = ? and expensetype = ? $expenseexchangeQuery ",[$project->id, $projectexchmaterialdetail['expensestypeid']]);
    
    // Get returns for same type
    $projectexchmaterialdetailreturn = R::getROW("select *, sum(totalbuyprice) as totals, sum(finalsupervision) as finalsupervisions from projectexchmaterialdetailreturn JOIN projectexchmaterialreturn
     ON projectexchmaterialdetailreturn.projectexchid = projectexchmaterialreturn.id WHERE projectexchmaterial.projectid = ? and projectexchmaterialdetailreturn.expensestypeid = ? $projectexchmaterialreturnQuery ",[$project->id, $projectexchmaterialdetail['expensestypeid']]);
    
    // Calculate net totals
    $totals = $expenseexchange['totals'] + $projectexchmaterialdetail['totals'] - $projectexchmaterialdetailreturn['totals'];
    $finalsupervisions = $expenseexchange['finalsupervisions'] + $projectexchmaterialdetail['finalsupervisions'] - $projectexchmaterialdetailreturn['finalsupervisions'];
    
    $expensestype = [
        'expensestypeid' => $expensestypedata['expensestypeid'],
        'expensestypename' => $expensestypedata['expensestypename'],
        'totals' => $totals,
        'finalsupervisions' => $finalsupervisions,
    ];
    
    $allexpensestype[] = $expensestype;
    $alltotals += $totals;
    $alltotalvalues += $finalsupervisions;
}

3. Revenue Stream Analysis

Client Payments:

$clientdebtchanges = R::getAll("SELECT clientdebtchange.* FROM `clientdebtchange` WHERE clientid = ? and paySerialNo > 0 and del = 0 $clientdebtchangeQuery ",[$project->clientid]);

Project Income:

$incomes = R::getAll("SELECT income.* FROM `income` WHERE costcenterid = ? and conditions = 0 $incomeQuery ",[$project->costcenterid]);

4. Expense Source Integration

Three-Way Expense Consolidation:

1. Material Issues: From projectexchmaterialdetail

2. Material Returns: From projectexchmaterialdetailreturn (subtracted)

3. Additional Expenses: From expenseexchange

Handle Return-Only Expense Types:

$projectexchmaterialdetailreturns = R::getAll("select *, sum(totalbuyprice) as totals, sum(finalsupervision) as finalsupervisions from projectexchmaterialdetailreturn JOIN projectexchmaterialreturn
     ON projectexchmaterialdetailreturn.projectexchid = projectexchmaterialreturn.id WHERE projectexchmaterialreturn.projectid = ? and projectexchmaterialdetailreturn.expensestypeid not in ($expensestypeids) $projectexchmaterialreturnQuery group by projectexchmaterialdetailreturn.expensestypeid ",[$project->id]);

Handle Expense-Exchange-Only Types:

$expenseexchanges = R::getAll("select *, sum(thevalue) as totals, sum(finalsupervision) as finalsupervisions from expenseexchange where del = 0 and projectid = ? and expensetype not in ($expensestypeids) $expenseexchangeQuery group by expenseexchange.expensetype ",[$project->id]);

---

๐Ÿ“Š Report Structure

Expense Type Summary

Expense Type | Material Costs | Additional Expenses | Material Returns | Net Cost | Supervision Fees | Total
-------------|----------------|---------------------|------------------|----------|------------------|-------
Labor        | 50,000         | 10,000              | 2,000            | 58,000   | 5,800           | 63,800
Materials    | 75,000         | 5,000               | 3,000            | 77,000   | 7,700           | 84,700
Equipment    | 25,000         | 15,000              | 1,000            | 39,000   | 3,900           | 42,900

Financial Summaries

---

๐ŸŽฏ URL Routes & Actions

Action (`?do=`)MethodDescriptionView Template
(empty)GET/POSTGenerate accounting report with filtersshow.html
---

โš ๏ธ Known Issues & Fixes

Issue 1: Date Filter Logic Error (Line 26, 36)

Problem: Income date filter uses wrong variable name

Cause: Copy-paste error in query construction

Fix: Correct variable name in income queries

File: Lines 26, 36

Issue 2: Complex Query Performance

Problem: Multiple aggregation queries with JOINs may be slow

Cause: Non-optimized query structure

Fix: Consider materialized views or query optimization

File: Throughout aggregation logic

Issue 3: Missing NULL Handling

Problem: SUM() results may be NULL causing calculation errors

Cause: No NULL value handling in aggregations

Fix: Use COALESCE() or IFNULL() in SQL queries

File: All SUM() operations

---

๐Ÿ” Business Rules

Date Range Logic

Expense Type Consolidation Rules

1. Material Issues: Add to expense type total

2. Material Returns: Subtract from expense type total

3. Additional Expenses: Add to expense type total

4. Supervision Fees: Calculated separately per type

Supervision Fee Handling

---

๐Ÿ“ Notes

Important Features

Use Cases

Performance Considerations

---

๐Ÿ“š Related Documentation