csrtechnew.ohctech.in/financial_variance_report_pdf.php

474 lines
15 KiB
PHP
Raw Normal View History

2025-04-14 13:28:09 +05:30
<?php
// Include necessary files
include('includes/config/config.php');
include('includes/functions.php');
// Retrieve POST data
$year = $_POST['year'];
$month = $_POST['month'];
$ohcid = $_POST['ohc_id3'];
error_log("Year: " . $year);
// $month = implode(',', $month);
error_log("check_month: " . $month);
$sql_month = '';
$type = isset($_GET['type']) ? $_GET['type'] : ''; // Get the type parameter
error_log("Check_type : ".$type);
if ($type === 'excel') {
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=financial_variance_report.xls");
header("Pragma: no-cache");
header("Expires: 0");
}
// Retrieve session data
$ohc = $ohcid;
$month_arry = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Financial Variance Report</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
text-align: center;
}
.header {
margin-bottom: 20px;
}
.header h1 {
text-align: center;
}
.header .details {
font-size: 15px;
}
.details strong {
margin-right: 5px;
}
</style>
</head>
<body>
<!-- Include header -->
<?php
if ($type === 'excel') {
}else{
include('pdf_current_ohc_header.php');
}
?>
<!-- Report Title and Details -->
<div class="header">
<h1>Financial Variance Report</h1>
<div class="details">
<strong>Yaer:</strong>
<?php echo $year . "-" . ($year + 1) ?>
</div>
</div>
<!-- Data Table -->
<table>
<thead>
<tr width='100%'>
<th></th>
<th colspan="3"><?php
$sql_month = '';
$sql_month2 = '';
$len = count($month);
$c = 0;
foreach ($month as $m) {
$c++;
echo $month_arry[$m - 1];
$sql_month = $sql_month . "month = " . $m;
$sql_month2 = $sql_month2 . "MONTH(issue_date) = " . $m;
if ($c < $len) {
echo ",";
$sql_month = $sql_month . " or ";
$sql_month2 = $sql_month2 . " or ";
}
error_log("check_month_sql : " . $sql_month);
error_log("check_month_sql2 : " . $sql_month2);
}
?></th>
<th colspan="3"><?php echo $year . "-" . ($year + 1); ?></th>
</tr>
<tr>
<th>Project</th>
<!-- <th>Jan - <?php echo $year; ?></th> -->
<!-- <th><?php echo $year . "-" . ($year + 1); ?></th> -->
<th>Planned</th>
<th>Actual</th>
<th>Variance</th>
<th>Planned</th>
<th>Actual</th>
<th>Variance</th>
</tr>
</thead>
<tbody>
<?php
// Fetch program data
$sql2 = "SELECT * FROM program_master
WHERE (
(YEAR(last_modified) = $year AND MONTH(last_modified) >= 4) OR
(YEAR(last_modified) = " . ($year + 1) . " AND MONTH(last_modified) <= 3)
)";
error_log("SQL Query: " . $sql2);
$result2 = mysqli_query($conn, $sql2);
$planned_total_m = 0;
$actual_total_m = 0;
$variance_total_m = 0;
$planned_total_y = 0;
$actual_total_y = 0;
$variance_total_y = 0;
while ($row2 = mysqli_fetch_assoc($result2)) {
extract($row2);
?>
<tr>
<td><?php echo htmlspecialchars($row2['program_name']); ?></td>
<td>
<?php
$mp = get_Planned($row2['program_id'], $year, $sql_month, 'Approved');
$planned_total_m += $mp;
echo '₹' . $mp;
?>
</td>
<td>
<?php $ma = get_actual($row2['program_id'], $year, $sql_month2, 'N');
$actual_total_m += $ma;
echo '₹' . $ma;
?>
</td>
<td>
<?php
$mv = get_Planned($row2['program_id'], $year, $sql_month, 'Approved') - get_actual($row2['program_id'], $year, $sql_month2, 'N');
$variance_total_m += $mv;
echo '₹' . $mv;
?>
</td>
<td>
<?php $yp = get_Planned($row2['program_id'], $year, '', 'Approved');
$planned_total_y = $yp;
echo '₹ ' . $yp;
?>
</td>
<td>
<?php $ya = get_actual($row2['program_id'], $year, '', 'N');
$actual_total_y += $ya;
echo '₹' . $ya;
?>
</td>
<td>
<?php $yv = get_Planned($row2['program_id'], $year, '', 'Approved') - get_actual($row2['program_id'], $year, '', 'N');
$variance_total_y += $yv;
echo '₹' . $yv; ?></td>
</tr>
<?php
}
?>
<t>
<td><strong>Total :- </strong></td>
<td><strong><?php echo '₹' . $planned_total_m; ?></strong></td>
<td><strong><?php echo '₹' . $actual_total_m; ?></strong></td>
<td><strong><?php echo '₹' . $variance_total_m; ?></strong></td>
<td><strong><?php echo '₹' . $planned_total_y; ?></strong></td>
<td><strong><?php echo '₹' . $actual_total_y; ?></strong></td>
<td><strong><?php echo '₹' . $variance_total_y; ?></strong></td>
</t
</tbody>
</table>
<br><br><br>
<table>
<tr>
<th>Location</th>
<th>Target</th>
<th>Actual</th>
<th>Variance(%)</th>
</tr>
<?php
$ohc_sql = "SELECT * FROM ohc_type";
error_log("SQL Query: " . $ohc_sql);
$ohc_result = mysqli_query($conn, $ohc_sql);
while ($ohc_row = mysqli_fetch_assoc($ohc_result)) {
?>
<tr>
<td width='20%'><?= $ohc_row['ohc_type_name'].'/'.$ohc_row['ohc_type_id'] ?></td>
<td><?php
$a = get_Planned2($ohc_row['ohc_type_id'], $year, 'Approved');
echo $a;
?></td>
<td><?php
$b = get_actual2($ohc_row['ohc_type_id'], $year, 'N');
echo $b;
?>
</td>
<td><?php
echo number_format(calculatePercentageDifference($a, $b), 2) . '%'; ?></td>
</tr>
<?php
}
?>
</table>
<?php
function calculatePercentageDifference($a, $b)
{
// Calculate the absolute difference
$difference = abs($a - $b);
// Calculate the average of $a and $b
$average = ($a + $b) / 2;
// Check for division by zero
if ($average == 0) {
return "Error: Average is zero";
}
// Calculate the percentage difference
$percentageDifference = ($difference / $average) * 100;
return $percentageDifference;
}
function get_Planned($activity, $year, $months, $status)
{
global $conn; // Access the global $conn variable
// Escape input parameters
$activity = mysqli_real_escape_string($conn, $activity);
$year = mysqli_real_escape_string($conn, $year);
$status = mysqli_real_escape_string($conn, $status);
$ohcTypeId = mysqli_real_escape_string($conn, $_REQUEST['ohc_id3']);
// Correct SQL query
if ($months == '') {
$sql = "SELECT * FROM participate_csr_program
WHERE activity_id = '$activity'
AND year = '$year'
AND status = '$status'
AND (
(month >= 4 AND month <= 12 AND year = '$year') OR
(month >= 1 AND month <= 3 AND year = '" . ($year + 1) . "')
)
AND location = '$ohcTypeId'";
error_log("check_sql_query1 : " . $sql);
} else {
$sql = "SELECT * FROM participate_csr_program
WHERE activity_id = '$activity'
AND year = '$year'
AND ( $months )
AND status = '$status'
AND location = '$ohcTypeId'";
error_log("check_sql_query2 : " . $sql);
}
// Execute the query
$result = mysqli_query($conn, $sql);
// Check for query execution errors
if (!$result) {
error_log("SQL Error: " . mysqli_error($conn));
return 0;
}
// Calculate the total cost
$total = 0;
while ($row = mysqli_fetch_assoc($result)) {
$total += $row['total_cost'];
}
// error_log("Check_function_query : " . $total);
return (int) $total;
}
function get_actual($activity, $year, $months, $status)
{
global $conn; // Access the global $conn variable
// Escape input parameters
$activity = mysqli_real_escape_string($conn, $activity);
$year = mysqli_real_escape_string($conn, $year);
$status = mysqli_real_escape_string($conn, $status);
// $ohcTypeId = mysqli_real_escape_string($conn, $_SESSION['current_ohcttype']);
// Correct SQL query
if ($months == '') {
$sql = "SELECT * FROM stock_issue
WHERE activity_name = '$activity'
AND (
(YEAR(issue_date) = '$year' AND MONTH(issue_date) >= 4) OR
(YEAR(issue_date) = '" . ($year + 1) . "' AND MONTH(issue_date) <= 3)
)
AND status = '$status'";
} else {
$sql = "SELECT * FROM stock_issue
WHERE activity_name = '$activity'
AND YEAR(issue_date) = '$year'
AND ($months)
AND status = '$status'";
}
// Execute the query
error_log("Check_function_query : " . $sql);
$result = mysqli_query($conn, $sql);
// Calculate the total cost
while ($row = mysqli_fetch_assoc($result)) {
$sql2 = "SELECT * FROM stock_issue_items where stock_issue_id = '" . $row['stock_issue_id'] . "' ";
$result2 = mysqli_query($conn, $sql2);
$actual_value = 0;
while ($row2 = mysqli_fetch_assoc($result2)) {
$per_unit_rate = getFieldFromTable('per_unit_rate', 'item_stock', 'item_id', $row2['item_id']);
$qty = $row2['issue_qty'];
$actual_value += ($per_unit_rate * $qty);
error_log("Check_stock_issue_qty : " . $qty);
}
}
return (int) $actual_value;
}
function get_Planned2($location, $year, $status)
{
global $conn; // Access the global $conn variable
// Escape input parameters
$location = mysqli_real_escape_string($conn, $location);
$year = mysqli_real_escape_string($conn, $year);
$status = mysqli_real_escape_string($conn, $status);
// $ohcTypeId = mysqli_real_escape_string($conn, $_SESSION['current_ohcttype']);
// Correct SQL query
$sql = "SELECT * FROM participate_csr_program
WHERE
(
(month >= 4 AND month <= 12 AND year = '$year') OR
(month >= 1 AND month <= 3 AND year = '" . ($year + 1) . "')
)
AND status = '$status'
AND location = '$location'";
error_log("check_sql_query22 : " . $sql);
error_log("check_sql_query2 : " . $sql);
// Execute the query
$result = mysqli_query($conn, $sql);
// Check for query execution errors
if (!$result) {
error_log("SQL Error: " . mysqli_error($conn));
return 0;
}
// Calculate the total cost
$total = 0;
while ($row = mysqli_fetch_assoc($result)) {
$total += $row['total_cost'];
}
// error_log("Check_function_query : " . $total);
return (int) $total;
}
function get_actual2($location, $year, $status)
{
global $conn; // Access the global $conn variable
// Escape input parameters
$location = mysqli_real_escape_string($conn, $location);
$year = mysqli_real_escape_string($conn, $year);
$status = mysqli_real_escape_string($conn, $status);
// $ohcTypeId = mysqli_real_escape_string($conn, $_SESSION['current_ohcttype']);
// Correct SQL query
$sql = "SELECT * FROM stock_issue
WHERE ohc_location_id = '$location'
AND (
(YEAR(issue_date) = '$year' AND MONTH(issue_date) >= 4) OR
(YEAR(issue_date) = '" . ($year + 1) . "' AND MONTH(issue_date) <= 3)
)
AND status = '$status'";
// Execute the query
error_log("Check_function_query : " . $sql);
$result = mysqli_query($conn, $sql);
// Calculate the total cost
while ($row = mysqli_fetch_assoc($result)) {
$sql2 = "SELECT * FROM stock_issue_items where stock_issue_id = '" . $row['stock_issue_id'] . "' ";
$result2 = mysqli_query($conn, $sql2);
$actual_value = 0;
while ($row2 = mysqli_fetch_assoc($result2)) {
$per_unit_rate = getFieldFromTable('per_unit_rate', 'item_stock', 'item_id', $row2['item_id']);
$qty = $row2['issue_qty'];
$actual_value += ($per_unit_rate * $qty);
error_log("Check_stock_issue_qty : " . $qty);
}
}
return (int) $actual_value;
}
?>
<!-- Print Script -->
<script>
// document.addEventListener('DOMContentLoaded', function() {
// window.print();
// });
</script>
</body>
</html>