๐Ÿ“š ERP Documentation Viewer

Beautiful, colorful documentation for your ERP system

Project Material Summary Report Controller Documentation

File: /controllers/projectreportControllerdetail2.php

Purpose: Generate consolidated material usage reports by product for projects

Last Updated: 2024-12-20

---

๐Ÿ“‹ Overview

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables

Table NamePurposeKey Columns
projectexchmaterialMaterial issuesid, projectid, totalbuyprice
projectexchmaterialdetailIssue line itemsprojectexchid, productid, pronumber, probuyprice
projectexchmaterialreturnMaterial returnsid, projectid, totalbuyprice
projectexchmaterialdetailreturnReturn line itemsprojectexchid, productid, pronumber, probuyprice
productProduct masterproductid, productName
---

๐Ÿ”ง Key Functions

1. Material Consolidation Engine

Purpose: Calculate net material usage by product across all project transactions

Business Logic:

// Get all unique products used in project
$allproductids = R::getAll("SELECT productid FROM ( 
    SELECT productid FROM projectexchmaterial left join projectexchmaterialdetail on 
    projectexchmaterial.id = projectexchmaterialdetail.projectexchid WHERE projectexchmaterial.projectid = $projectId
    UNION ALL
    SELECT productid FROM projectexchmaterialreturn left join projectexchmaterialdetailreturn on 
    projectexchmaterialreturn.id = projectexchmaterialdetailreturn.projectexchid WHERE projectexchmaterialreturn.projectid = $projectId 
) s GROUP BY productid");

$alldata = array();
$alltotal = 0;
$alltotaln = 0;

foreach($allproductids as $product) {
    // Calculate total issued
    $projectexchmaterialnum = R::getrow("SELECT sum(pronumber) as pronumbers, sum(probuyprice) as probuyprices FROM projectexchmaterial left join projectexchmaterialdetail on 
    projectexchmaterial.id = projectexchmaterialdetail.projectexchid WHERE projectexchmaterial.projectid = $projectId and productid = " . $product['productid']);
    
    // Get product details
    $productdata = R::getrow("SELECT productName FROM product WHERE productid = " . $product['productid']);
    
    // Calculate total returned
    $projectexchmaterialnnum = R::getrow("SELECT sum(pronumber) as pronumbers, sum(probuyprice) as probuyprices FROM projectexchmaterialreturn left join projectexchmaterialdetailreturn on 
    projectexchmaterialreturn.id = projectexchmaterialdetailreturn.projectexchid WHERE projectexchmaterialreturn.projectid = $projectId and productid = " . $product['productid']);
    
    // Calculate totals
    $alltotal += intval($projectexchmaterialnum['pronumbers']) * intval($projectexchmaterialnum['probuyprices']);
    $alltotaln += intval($projectexchmaterialnnum['pronumbers']) * intval($projectexchmaterialnnum['probuyprices']);
    
    $inside_item = array(
        "productid" => $product['productid'],
        "productName" => $productdata['productName'],
        "pronumber" => intval($projectexchmaterialnum['pronumbers']),
        "probuyprice" => intval($projectexchmaterialnum['probuyprices']),
        "pronumbern" => intval($projectexchmaterialnnum['pronumbers']),
        "probuypricen" => intval($projectexchmaterialnnum['probuyprices'])
    );
    
    array_push($alldata, $inside_item);
}

2. Summary Calculations

Net Usage Calculation:

$smarty->assign("alltotal", $alltotal);        // Total issued value
$smarty->assign("alltotaln", $alltotaln);      // Total returned value  
$smarty->assign("alltotals", $alltotal - $alltotaln); // Net consumption value

---

๐Ÿ“Š Report Structure

Product Summary Format

Product ID | Product Name | Issued Qty | Issued Value | Returned Qty | Returned Value | Net Qty | Net Value
-----------|--------------|------------|--------------|--------------|----------------|---------|----------
001        | Steel Bars   | 100        | 5,000        | 10           | 500            | 90      | 4,500
002        | Cement       | 50         | 2,500        | 5            | 250            | 45      | 2,250

Summary Totals

---

๐ŸŽฏ URL Routes & Actions

Action (`?do=`)MethodDescriptionView Template
(empty)GET/POSTGenerate material summary reportreportdetail2.html
---

๐Ÿ“ˆ Business Intelligence Features

Material Utilization Analysis

Cost Control Benefits

---

โš ๏ธ Known Issues & Fixes

Issue 1: Integer Casting for Calculations (Lines 38-39)

Problem: Using intval() may lose decimal precision

Cause: Cost calculations need decimal precision

Fix: Use floatval() or (float) casting

File: Lines 38-39, 44-46

Issue 2: UNION ALL Query Performance

Problem: Complex UNION query may be slow for large datasets

Cause: Full table scans on material tables

Fix: Add indexes on projectid and productid columns

File: Lines 22-28

---

๐Ÿ“ Notes

Important Features

Use Cases

Performance Considerations

---

๐Ÿ“š Related Documentation

โ†‘