首页 > 其他分享 >销售报表-数组展示和数据组装

销售报表-数组展示和数据组装

时间:2024-08-16 17:29:59浏览次数:6  
标签:报表 typeValue 组装 sales totals 数组 total type row

销售报表-数组展示和数据组装

前端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

相关文章

  • C:一个字符数组里面解析出多个字符串
    一个字符数组里面存放了多个字符串,每个字符串以‘\0’。要求把这些有效字符串筛选出来并输出。 扩展:'\0\0'表示字符串结束。V2方法就是实现的这个扩展功能。 #include<stdio.h>#include<string.h>#include<malloc.h>voidprintSzNameList(charszNameList[],in......
  • 解锁企业数据分析瓶颈:数据可视化与报表自动化的实战策略
    Hi~这里是ProXiao文章参考:晓观点 《数据可视化与报表自动化:如何破解企业数据分析的痛点?》在数字化时代背景下,企业面临着日益增长的数据量和复杂性,这对数据处理、分析和应用提出了更高要求。企业决策过程中如何高效利用数据资源,已成为一个关键议题。本文旨在分析企业在数据......
  • 数组
    数组是一个能存储多个同种类型数据的容器//数组作为参数传递:传递时传递的仅仅是存放首地址的变量,传递时注意将数组长度一同传递//索引越界:索引从0~length-1#include<stdio.h>voidgetArr(intarr[],intlen);intmain(){intarr1[]={1,2,3,4,5};intlen=sizeof(arr1)/siz......
  • 遍历数组对象
    前提条件:数组对象中的id唯一1consttree=[2{id:1},3{id:2,4children:[5{id:3,6children:[7{id:4,8children:[9{i......
  • C语言-使用数组法,指针法实现将一个5X5的矩阵中最大的元素放在中心,四个角分别放四个最
    1.题目要求:将一个5X5的矩阵中最大的元素放在中心·,四个角分别放四个最小的元素(顺序为从左到右,从上到下,从小到大存放),写一函数实现之。2.数组法实现#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>//一、数组法实现intmain(){ intarr[5][5]={ {1,2,3,4,5},......
  • 【代码随想录】一、数组:6.前缀和
    二刷的时候发现更新了一些新的题目,尝试写了写后,发现我完全不会ACM输入输出模式。这两天在补前几天没背的八股,写得不够满意(几乎是完全誊代码了),先放着,后面再补充补充吧。1.题目:44.开发商购买土地#include<iostream>#include<vector>#include<climits>usingnamespacestd......
  • 【代码随想录】一、数组:4.滑动窗口
    1.题目1:209.长度最小的子数组1.1.解法1:暴力解法(已超时)使用两层循环,外层循环确定最小子数组开始的位置(i),内层循环确定最小子数组结束的位置(j)。在每次跳出内层循环时,sum应重置为0。当找到的子数组相加的和等于或大于目标值(target)时,算出此刻子数组的长度(j-i+1),并更新result的值......
  • 【代码随想录】一、数组:5.螺旋矩阵
    本题并不涉及到什么算法,就是模拟过程,但却十分考察对代码的掌控能力。1.题目1:59.螺旋矩阵II1.1.解法1:模拟本题的重点还是像之前的“704.二分查找”,坚持循环不变量原则,即在本题中遍历每条边时,坚持相同的原则。如下是一个示例,即n=5,我们考虑根据圈数和边数来进行遍历:由外圈到内......
  • 你真的了解财务报表吗?快来看看财务报表的真正用法
    在现代商业的复杂网络中,每一家公司都像是一个精密运行的钟表,其运转的顺畅程度取决于各个齿轮的完美契合与精准配合。而财务报表,就像是是这钟表的指针,实时展现着公司运转的状态和效率,帮助管理者把握全局,做出明智的调整。然而,单一的数据或图表就像是钟表内部的一个独立齿轮,它们各自......
  • KMP算法——理解 next 数组
    !注意!本文与《王道》,《严书》有所不同,字符串均从第0位开始,next数组没有添加常数1。博客为梳理思路所用,难免纰漏,希望不吝赐教。在字符串匹配中,设m为待匹配的主串S长度,n为找寻的模式串T长度。如:在主串S='ababc'中寻找模式串T='abc'则字符串匹配算法返回S中第......