销售报表-数组展示和数据组装
前端Vue 使用element-UI table
后端PHP
后端PHP数据处理:
<?php
// 获取数据后,需要在每个大区的结尾插入,各大区的数据统计行
// 循环获取每个大区的结尾,用于插入每个大区每个type的统计值记录
foreach ($data as $key => $value) {
// 检查当前大区是否已经存在于结果数组中
if (!array_key_exists($value['department'], $lastItemsByRegion)) {
// 如果不存在,将当前元素添加到结果数组中
$lastItemsByRegion[$value['department']] = $key;
} else {
// 如果存在,更新结果数组中该大区的元素为当前元素
$lastItemsByRegion[$value['department']] = $key;
}
}
// 【其次】每个大区的数据统计,将各大区数组统计的每个type类型放在每个地区的尾部
$number = 1;
foreach ($lastItemsByRegion as $key => $val) {
foreach ($type as $v) {
$newArray[] = [
'sales_id' => 0,
'sales_true_name' => 'Region total',
'department' => $key,
'type' => $v,
'at_year' => $key,
'jan' => 0,
'feb' => 0,
'mar' => 0,
'apr' => 0,
'may' => 0,
'jun' => 0,
'jul' => 0,
'aug' => 0,
'sep' => 0,
'oct' => 0,
'nov' => 0,
'dec' => 0,
'receivable' => 0,
];
}
// 使用 array_splice 插入新数组
array_splice($setList, $val + $number, 0, $newArray);
$number += 6;
$newArray = [];
}
$total = [];
// 初始化每种类型的累计数据
foreach ($type as $v) {
$total[$v] = [
'sales_id' => '',
'sales_true_name' => 'Region total',
'department' => '100',
'type' => $v,
'at_year' => $queryWhere['at_year'], // 你可能需要从 `$setList` 中获取年份
'jan' => '0.00',
'feb' => '0.00',
'mar' => '0.00',
'apr' => '0.00',
'may' => '0.00',
'jun' => '0.00',
'jul' => '0.00',
'aug' => '0.00',
'sep' => '0.00',
'oct' => '0.00',
'nov' => '0.00',
'dec' => '0.00',
'receivable' => '0.00'
];
}
// 【优先】计算报表最底部的统计
foreach ($setList as $key => $value) {
$typeValue = $value['type'];
if (in_array($typeValue, $type)) {
if (isset($total[$typeValue])) {
$total[$typeValue]['jan'] = number_format($total[$typeValue]['jan'] + floatval($value['jan']), 2, '.', '');
$total[$typeValue]['feb'] = number_format($total[$typeValue]['feb'] + floatval($value['feb']), 2, '.', '');
$total[$typeValue]['mar'] = number_format($total[$typeValue]['mar'] + floatval($value['mar']), 2, '.', '');
$total[$typeValue]['apr'] = number_format($total[$typeValue]['apr'] + floatval($value['apr']), 2, '.', '');
$total[$typeValue]['may'] = number_format($total[$typeValue]['may'] + floatval($value['may']), 2, '.', '');
$total[$typeValue]['jun'] = number_format($total[$typeValue]['jun'] + floatval($value['jun']), 2, '.', '');
$total[$typeValue]['jul'] = number_format($total[$typeValue]['jul'] + floatval($value['jul']), 2, '.', '');
$total[$typeValue]['aug'] = number_format($total[$typeValue]['aug'] + floatval($value['aug']), 2, '.', '');
$total[$typeValue]['sep'] = number_format($total[$typeValue]['sep'] + floatval($value['sep']), 2, '.', '');
$total[$typeValue]['oct'] = number_format($total[$typeValue]['oct'] + floatval($value['oct']), 2, '.', '');
$total[$typeValue]['nov'] = number_format($total[$typeValue]['nov'] + floatval($value['nov']), 2, '.', '');
$total[$typeValue]['dec'] = number_format($total[$typeValue]['dec'] + floatval($value['dec']), 2, '.', '');
$total[$typeValue]['receivable'] = number_format($total[$typeValue]['receivable'] + floatval($value['receivable']), 2, '.', '');
}
}
}
// 【其次】每个大区的数据统计,将各大区数组统计的每个type类型放在每个地区的尾部
$number = 1;
foreach ($lastItemsByRegion as $key => $val) {
foreach ($type as $v) {
$newArray[] = [
'sales_id' => 0,
'sales_true_name' => 'Region total',
'department' => $key,
'type' => $v,
'at_year' => $key,
'jan' => 0,
'feb' => 0,
'mar' => 0,
'apr' => 0,
'may' => 0,
'jun' => 0,
'jul' => 0,
'aug' => 0,
'sep' => 0,
'oct' => 0,
'nov' => 0,
'dec' => 0,
'receivable' => 0,
];
}
// 使用 array_splice 插入新数组
array_splice($setList, $val + $number, 0, $newArray);
$number += 6;
$newArray = [];
}
$data = [
['id' => '371', 'sales_id' => '66', 'sales_true_name' => '许三多', 'department' => '40', 'type' => '10', 'at_year' => '2024', 'jan' => '190000.00', 'feb' => '190000.00', 'mar' => '190000.00', 'apr' => '260000.00', 'may' => '260000.00', 'jun' => '260000.00', 'jul' => '390000.00', 'aug' => '390000.00', 'sep' => '390000.00', 'oct' => '450000.00', 'nov' => '450000.00', 'dec' => '450000.00'],
['sales_id' => '66', 'sales_true_name' => '许三多', 'department' => '40', 'type' => '11', 'at_year' => '2024', 'jan' => '438819.02', 'feb' => '462625.75', 'mar' => '791277.22', 'apr' => '790993.40', 'may' => '1035246.07', 'jun' => '459118.08', 'jul' => '387061.98', 'aug' => '59593.01', 'sep' => '0.00', 'oct' => '0.00', 'nov' => '0.00', 'dec' => '0.00'],
['id' => '378', 'sales_id' => '66', 'sales_true_name' => '许三多', 'department' => '40', 'type' => '20', 'at_year' => '2024', 'jan' => '253154.00', 'feb' => '595343.69', 'mar' => '617315.77', 'apr' => '849812.68', 'may' => '1050733.96', 'jun' => '449518.58', 'jul' => '300146.88', 'aug' => '78858.28', 'sep' => '0.00', 'oct' => '0.00', 'nov' => '0.00', 'dec' => '0.00'],
['id' => '385', 'sales_id' => '66', 'sales_true_name' => '许三多', 'department' => '40', 'type' => '30', 'at_year' => '2024', 'jan' => '624484.05', 'feb' => '329907.81', 'mar' => '965238.67', 'apr' => '732174.13', 'may' => '1019758.19', 'jun' => '468717.58', 'jul' => '473977.08', 'aug' => '40327.74', 'sep' => '0.00', 'oct' => '0.00', 'nov' => '0.00', 'dec' => '0.00'],
['sales_id' => '66', 'sales_true_name' => '许三多', 'department' => '40', 'type' => '31', 'at_year' => '2024', 'jan' => '2.3095', 'feb' => '2.4348', 'mar' => '4.1646', 'apr' => '3.0422', 'may' => '3.9817', 'jun' => '1.7658', 'jul' => '0.9924', 'aug' => '0.1528', 'sep' => '0.0000', 'oct' => '0.0000', 'nov' => '0.0000', 'dec' => '0.0000'],
['id' => '392', 'sales_id' => '66', 'sales_true_name' => '许三多', 'department' => '40', 'type' => '40', 'at_year' => '2024', 'jan' => '1293.20', 'feb' => '1813.60', 'mar' => '194544.27', 'apr' => '433455.88', 'may' => '7262568.96', 'jun' => '10907986.15', 'jul' => '4795951.69', 'aug' => '456540.47', 'sep' => '0.00', 'oct' => '0.00', 'nov' => '0.00', 'dec' => '0.00'],
['sales_id' => 0, 'sales_true_name' => 'Region total', 'department' => 40, 'type' => 10, 'at_year' => 40, 'jan' => 0, 'feb' => 0, 'mar' => 0, 'apr' => 0, 'may' => 0, 'jun' => 0, 'jul' => 0, 'aug' => 0, 'sep' => 0, 'oct' => 0, 'nov' => 0, 'dec' => 0],
['sales_id' => 0, 'sales_true_name' => 'Region total', 'department' => 40, 'type' => 11, 'at_year' => 40, 'jan' => 0, 'feb' => 0, 'mar' => 0, 'apr' => 0, 'may' => 0, 'jun' => 0, 'jul' => 0, 'aug' => 0, 'sep' => 0, 'oct' => 0, 'nov' => 0, 'dec' => 0],
['sales_id' => 0, 'sales_true_name' => 'Region total', 'department' => 40, 'type' => 20, 'at_year' => 40, 'jan' => 0, 'feb' => 0, 'mar' => 0, 'apr' => 0, 'may' => 0, 'jun' => 0, 'jul' => 0, 'aug' => 0, 'sep' => 0, 'oct' => 0, 'nov' => 0, 'dec' => 0],
['sales_id' => 0, 'sales_true_name' => 'Region total', 'department' => 40, 'type' => 30, 'at_year' => 40, 'jan' => 0, 'feb' => 0, 'mar' => 0, 'apr' => 0, 'may' => 0, 'jun' => 0, 'jul' => 0, 'aug' => 0, 'sep' => 0, 'oct' => 0, 'nov' => 0, 'dec' => 0],
['sales_id' => 0, 'sales_true_name' => 'Region total', 'department' => 40, 'type' => 31, 'at_year' => 40, 'jan' => '1507752.58', 'feb' => '1579693.28', 'mar' => '2758380.09', 'apr' => '3066439.13', 'may' => '10628311.16', 'jun' => '12545342.16', 'jul' => '6347138.62', 'aug' => '1025319.65', 'sep' => '390000.00', 'oct' => '450000.00', 'nov' => '450000.00', 'dec' => '450000.00', 'receivable' => '7158470.68'],
['sales_id' => 0, 'sales_true_name' => 'Region total', 'department' => 40, 'type' => 40, 'at_year' => 40, 'jan' => 0, 'feb' => 0, 'mar' => 0, 'apr' => 0, 'may' => 0, 'jun' => 0, 'jul' => 0, 'aug' => 0, 'sep' => 0, 'oct' => 0, 'nov' => 0, 'dec' => 0],
];
// Step 1: Initialize an array to hold the department and type totals
$totals = [];
foreach ($data as $row) {
// Skip rows where sales_true_name is "Region total"
if ($row['sales_true_name'] === 'Region total') {
continue;
}
$dept = $row['department'];
$type = $row['type'];
// Initialize the totals array for this department and type if not set
if (!isset($totals[$dept][$type])) {
$totals[$dept][$type] = [
'jan' => 0,
'feb' => 0,
'mar' => 0,
'apr' => 0,
'may' => 0,
'jun' => 0,
'jul' => 0,
'aug' => 0,
'sep' => 0,
'oct' => 0,
'nov' => 0,
'dec' => 0,
];
}
// Sum up the amounts for each month
$totals[$dept][$type]['jan'] += (float)$row['jan'];
$totals[$dept][$type]['feb'] += (float)$row['feb'];
$totals[$dept][$type]['mar'] += (float)$row['mar'];
$totals[$dept][$type]['apr'] += (float)$row['apr'];
$totals[$dept][$type]['may'] += (float)$row['may'];
$totals[$dept][$type]['jun'] += (float)$row['jun'];
$totals[$dept][$type]['jul'] += (float)$row['jul'];
$totals[$dept][$type]['aug'] += (float)$row['aug'];
$totals[$dept][$type]['sep'] += (float)$row['sep'];
$totals[$dept][$type]['oct'] += (float)$row['oct'];
$totals[$dept][$type]['nov'] += (float)$row['nov'];
$totals[$dept][$type]['dec'] += (float)$row['dec'];
}
// Step 2: Assign the totals back to the "Region total" rows
foreach ($data as &$row) {
if ($row['sales_true_name'] === 'Region total') {
$dept = $row['department'];
$type = $row['type'];
if (isset($totals[$dept][$type])) {
$row['jan'] = $totals[$dept][$type]['jan'];
$row['feb'] = $totals[$dept][$type]['feb'];
$row['mar'] = $totals[$dept][$type]['mar'];
$row['apr'] = $totals[$dept][$type]['apr'];
$row['may'] = $totals[$dept][$type]['may'];
$row['jun'] = $totals[$dept][$type]['jun'];
$row['jul'] = $totals[$dept][$type]['jul'];
$row['aug'] = $totals[$dept][$type]['aug'];
$row['sep'] = $totals[$dept][$type]['sep'];
$row['oct'] = $totals[$dept][$type]['oct'];
$row['nov'] = $totals[$dept][$type]['nov'];
$row['dec'] = $totals[$dept][$type]['dec'];
}
}
}
// Now $data contains the updated totals
print_r($data);
?>
标签:报表,typeValue,组装,sales,totals,数组,total,type,row
From: https://www.cnblogs.com/css2020/p/18362607