Rapport-Laravel/app/Http/Controllers/GennovadcReportController.php

463 lines
18 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Exports\InvoicesExport;
use App\Http\Requests\LambdaRequest;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
class GennovadcReportController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->_service = 'reports';
$this->_module = 'gennovadc_reports';
$this->_viewname = 'gennovadcreport';
$this->_title = 'My Dc Reports';
$this->_base_url = '/gennovadcreports';
}
public function export(Request $request)
{
$type = $request->input('type', 0);
$month = $request->input('month', date('F Y', strtotime('now')));
if ($type == 'monthsummary') {
$monthlyResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/monthly_summary', ['month' => $month]);
$rows = [];
$totals = [];
$headers = [];
$rawHeaders = [];
if (!empty($monthlyResponse->data->item) && is_array($monthlyResponse->data->item)) {
foreach ($monthlyResponse->data->item as $item) {
$rawHeaders = array_unique(array_merge($rawHeaders, array_keys((array) $item)));
}
}
usort($rawHeaders, function ($a, $b) {
return $a === 'product_name' ? -1 : ($b === 'product_name' ? 1 : 0);
});
$headers = array_map(function ($key) {
return ucwords(str_replace('_', ' ', $key));
}, $rawHeaders);
foreach ($monthlyResponse->data->item as $item) {
$row = [];
foreach ($rawHeaders as $header) {
$value = $item->$header ?? '';
$row[] = $value;
if (is_numeric($item->$header ?? null) && $header !== 'product_name' && $header !== 'percent_ach') {
$totals[$header] = ($totals[$header] ?? 0) + $item->$header;
}
}
$rows[] = $row;
}
$totalRow = [];
foreach ($rawHeaders as $header) {
if ($header === 'product_name') {
$totalRow[] = 'Total';
} elseif ($header === 'percent_ach' && isset($totals['units_plan']) && $totals['units_plan'] > 0 && isset($totals['units_ach'])) {
$totalRow[] = round(($totals['units_ach'] / $totals['units_plan']) * 100) . '%';
} else {
$totalRow[] = $totals[$header] ?? '';
}
}
$rows[] = $totalRow;
$excelData = [];
$excelData[] = $headers;
$excelData = array_merge($excelData, $rows);
return Excel::download(new InvoicesExport($excelData), $type . '-report.xlsx');
} elseif ($type == 'weeksummary') {
$weeklyResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/weekly_summary', ['month' => $month]);
$weeklyData = $weeklyResponse->data->item ?? [];
$weeklySummary = [];
$weeklyTotals = [];
if (!empty($weeklyData)) {
$firstItem = (array) $weeklyData[0];
foreach ($firstItem as $key => $value) {
$label = ucwords(str_replace(['_', '-'], ' ', $key));
$weeklySummary[] = [
'label' => $label,
'key' => $key,
];
if (is_numeric($value)) {
$weeklyTotals[$key] = 0;
}
}
foreach ($weeklyData as $item) {
foreach ($weeklyTotals as $key => $_) {
$weeklyTotals[$key] += $item->$key ?? 0;
}
}
}
$excelData = [];
$excelData[] = array_column($weeklySummary, 'label');
foreach ($weeklyData as $row) {
$rowData = [];
foreach ($weeklySummary as $col) {
$val = $row->{$col['key']} ?? '';
$rowData[] = is_numeric($val) ? (float) $val : $val;
}
$excelData[] = $rowData;
}
if (!empty($weeklyData)) {
$totalRow = [];
foreach ($weeklySummary as $col) {
if ($col['key'] === 'product_name') {
$totalRow[] = 'Total';
} elseif ($col['key'] === 'percent_ach') {
$totalRow[] = $weeklyTotals['month_plan'] > 0 ? round(($weeklyTotals['units_ach'] / $weeklyTotals['month_plan']) * 100) . '%' : '0.00%';
} else {
$totalRow[] = $weeklyTotals[$col['key']] ?? 0;
}
}
$excelData[] = $totalRow;
}
return Excel::download(new InvoicesExport($excelData), 'weekly-summary.xlsx');
} elseif ($type == 'centersummary') {
$centerResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/center_summary', ['month' => $month]);
$centerSummarySubmission = $centerResponse->data->item ?? [];
$centerTotals = $centerResponse->data->total ?? null;
$centerSummary = [];
$excelData = [];
if (!empty($centerSummarySubmission)) {
$firstItem = (array) $centerSummarySubmission[0];
foreach ($firstItem as $key => $val) {
$label = ucwords(str_replace(['_', '-'], ' ', $key));
$centerSummary[] = [
'label' => $label,
'key' => $key,
];
}
$excelData[] = array_column($centerSummary, 'label');
foreach ($centerSummarySubmission as $row) {
$rowData = [];
foreach ($centerSummary as $col) {
$val = $row->{$col['key']} ?? '';
$rowData[] = is_numeric($val) ? (float) $val : $val;
}
$excelData[] = $rowData;
}
if ($centerTotals) {
$totalRow = [];
foreach ($centerSummary as $index => $col) {
if ($index === 0) {
$totalRow[] = 'Total';
continue;
}
$key = $col['key'];
if ($key === 'percent_ach') {
$plan = $centerTotals->centers_plan ?? 0;
$ach = $centerTotals->centers_ach ?? 0;
$percent = $plan > 0 ? round(($ach / $plan) * 100) : 0;
$totalRow[] = $percent . '%';
} else {
$val = $centerTotals->{$key} ?? 0;
$totalRow[] = (float) $val;
}
}
$excelData[] = $totalRow;
}
}
return Excel::download(new InvoicesExport($excelData), 'center-summary.xlsx');
} elseif ($type == 'centertrendsummary') {
$centerTrendResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/center_trend_summary');
$centerTrendSummarySubmission = $centerTrendResponse->data->item ?? [];
$centerTrendTotals = [];
$monthKeys = [];
$centerTrendTotals = (array) ($centerTrendResponse->data->total ?? []);
$rawMonthKeys = array_keys($centerTrendTotals);
$year = 25;
$currentYear = date('y');
$centerTrendSummary = [['name' => 'Product Name', 'key' => 'product_name']];
foreach ($rawMonthKeys as $month) {
$monthUpper = strtoupper($month);
if ($year == $currentYear && in_array($monthUpper, ['JAN','FEB','MAR','APR','MAY','JUN'])) {
continue;
}
if ($month === 'JAN') {
$year++;
}
$displayKey = $month . '-' . $year;
$centerTrendSummary[] = ['name' => $displayKey, 'key' => $month];
}
$excelData = [];
$excelData[] = array_column($centerTrendSummary, 'name'); // header row
// Data rows
foreach ($centerTrendSummarySubmission as $row) {
$rowData = [];
foreach ($centerTrendSummary as $col) {
$val = isset($row->{$col['key']}) ? $row->{$col['key']} : '';
$rowData[] = $val;
}
$excelData[] = $rowData;
}
// Total row
if (!empty($centerTrendSummarySubmission)) {
$totalRow = ['Total'];
foreach (array_slice($centerTrendSummary, 1) as $col) {
$val = $centerTrendTotals[$col['key']] ?? 0;
$totalRow[] = $val;
}
$excelData[] = $totalRow;
}
return Excel::download(new InvoicesExport($excelData), 'center-trend-summary.xlsx');
} elseif ($type == 'unittrendsummary') {
$unitTrendResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/unit_trend_summary');
$unitTrendSummarySubmission = $unitTrendResponse->data->item ?? [];
$unitTrendTotals = [];
$monthKeys = [];
if (!empty($unitTrendSummarySubmission)) {
$firstItem = (array) $unitTrendSummarySubmission[0];
$monthKeys = array_filter(array_keys($firstItem), fn($key) => $key !== 'product_name');
foreach ($monthKeys as $month) {
$unitTrendTotals[$month] = 0;
}
foreach ($unitTrendSummarySubmission as $item) {
foreach ($monthKeys as $month) {
$unitTrendTotals[$month] += $item->{$month} ?? 0;
}
}
}
$unitTrendSummary = [['name' => 'Product Name', 'key' => 'product_name']];
$year = 25;
$currentYear = date('y');
foreach ($monthKeys as $month) {
$monthUpper = strtoupper($month);
if ($year == $currentYear && in_array($monthUpper, ['JAN','FEB','MAR','APR','MAY','JUN'])) {
continue;
}
if ($month === 'JAN') {
$year++;
}
$displayKey = $month . '-' . $year; // visible in Excel
$unitTrendSummary[] = ['name' => $displayKey, 'key' => $month]; // key stays raw
}
$excelData = [];
$excelData[] = array_column($unitTrendSummary, 'name');
foreach ($unitTrendSummarySubmission as $row) {
$rowData = [];
foreach ($unitTrendSummary as $col) {
$val = $row->{$col['key']} ?? '';
$rowData[] = is_numeric($val) ? (float) $val : $val;
}
$excelData[] = $rowData;
}
if (!empty($unitTrendSummarySubmission)) {
$totalRow = ['Total'];
foreach (array_slice($unitTrendSummary, 1) as $col) {
$val = $unitTrendTotals[$col['key']] ?? 0;
$totalRow[] = (float) $val;
}
$excelData[] = $totalRow;
}
return Excel::download(new InvoicesExport($excelData), 'unit-trend-summary.xlsx');
} else {
}
}
public function dashboard(Request $request)
{
// month
$month = $request->input('month', date('F Y', strtotime('now')));
$monthlyResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/monthly_summary', ['month' => $month]);
$monthSummary = [];
$numericKeys = [];
if (!empty($monthlyResponse->data->item)) {
$firstItem = (array) $monthlyResponse->data->item[0];
foreach ($firstItem as $key => $val) {
$monthSummary[] = ['name' => ucwords(str_replace('_', ' ', $key)), 'key' => $key];
if ($key !== 'product_name' && is_numeric($val)) {
$numericKeys[] = $key;
}
}
}
$totals = [];
foreach ($numericKeys as $key) {
$totals[$key] = 0;
}
foreach ($monthlyResponse->data->item as $item) {
foreach ($numericKeys as $key) {
$totals[$key] += $item->{$key} ?? 0;
}
}
// week
$weeklyResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/weekly_summary', ['month' => $month]);
$weeklyData = $weeklyResponse->data->item ?? [];
$weeklySummary = [];
$weeklyTotals = [];
if (!empty($weeklyData)) {
$firstItem = (array) $weeklyData[0];
foreach ($firstItem as $key => $value) {
$label = ucwords(str_replace(['_', '-'], ' ', $key));
$weeklySummary[] = [
'label' => $label,
'key' => $key,
];
if (is_numeric($value)) {
$weeklyTotals[$key] = 0;
}
}
foreach ($weeklyData as $item) {
foreach ($weeklyTotals as $key => $_) {
$weeklyTotals[$key] += $item->$key ?? 0;
}
}
}
// center
$centerResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/center_summary', ['month' => $month]);
$centerSummarySubmission = $centerResponse->data->item ?? [];
$centerTotals = $centerResponse->data->total ?? null;
$centerSummary = [];
if (!empty($centerSummarySubmission)) {
$firstItem = (array) $centerSummarySubmission[0];
foreach ($firstItem as $key => $val) {
$label = ucwords(str_replace(['_', '-'], ' ', $key));
$centerSummary[] = ['name' => $label, 'key' => $key];
}
}
// centertrend
$centerTrendResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/center_trend_summary');
$centerTrendSummarySubmission = $centerTrendResponse->data->item ?? [];
$centerTrendTotals = [];
$centerTrendTotals = (array) ($centerTrendResponse->data->total ?? []);
$monthKeys = array_keys($centerTrendTotals);
$year = 25;
$currentYear = date('y');
$centerTrendSummary = [['name' => 'Product Name', 'key' => 'product_name']];
foreach ($monthKeys as $month) {
$monthUpper = strtoupper($month);
if ($year == $currentYear && in_array($monthUpper, ['JAN','FEB','MAR','APR','MAY','JUN'])) {
continue;
}
if ($month === 'JAN') {
$year++;
}
$centerTrendSummary[] = ['name' => $month . '-' . $year, 'key' => $month];
}
$unitTrendResponse = LambdaRequest::signedrequest('get', 'reports', 'gennovadc_reports/unit_trend_summary');
$unitTrendSummarySubmission = $unitTrendResponse->data->item ?? [];
$unitTrendTotals = [];
if (!empty($unitTrendSummarySubmission)) {
$firstItem = (array) $unitTrendSummarySubmission[0];
$rawMonthKeys = array_filter(array_keys($firstItem), fn($key) => $key !== 'product_name');
// keep totals indexed by raw month
foreach ($rawMonthKeys as $month) {
$unitTrendTotals[$month] = 0;
}
foreach ($unitTrendSummarySubmission as $item) {
foreach ($rawMonthKeys as $month) {
$unitTrendTotals[$month] += $item->{$month} ?? 0;
}
}
// build month headers with year suffix
$year = 25;
$currentYear = date('y');
$unitTrendSummary = [['name' => 'Product Name', 'key' => 'product_name']];
foreach ($rawMonthKeys as $month) {
$monthUpper = strtoupper($month);
if ($year == $currentYear && in_array($monthUpper, ['JAN','FEB','MAR','APR','MAY','JUN'])) {
continue;
}
if ($month === 'JAN') {
$year++;
}
$displayKey = $month . '-' . $year;
$unitTrendSummary[] = ['name' => $displayKey, 'key' => $month];
}
} else {
$rawMonthKeys = [];
$unitTrendSummary = [['name' => 'Product Name', 'key' => 'product_name']];
}
return view($this->_viewname . '.dashboard', [
'title' => 'My Dc Dashboard',
'month' => $month,
'monthsummary' => $monthSummary,
'monthsummarysubmission' => $monthlyResponse->data->item ?? [],
'monthTotals' => $totals,
'weeklysummarysubmission' => $weeklyResponse->data->item ?? [],
'weeklysummary' => $weeklySummary,
'weeklyTotals' => $weeklyTotals,
'centersummarysubmission' => $centerSummarySubmission,
'centersummary' => $centerSummary,
'centerTotals' => $centerTotals,
'centertrendsummarysubmission' => $centerTrendSummarySubmission,
'centertrendsummary' => $centerTrendSummary,
'centerTrendTotals' => $centerTrendTotals,
'unittrendsummarysubmission' => $unitTrendSummarySubmission,
'unittrendsummary' => $unitTrendSummary,
'unitTrendTotals' => $unitTrendTotals,
]);
}
}