cqp 23 小时之前
父节点
当前提交
54dfd0f263
共有 3 个文件被更改,包括 463 次插入12 次删除
  1. 184 0
      app/Exports/ExportOrder2.php
  2. 17 12
      app/Service/ExportFileService.php
  3. 262 0
      app/Service/StatisticsService.php

+ 184 - 0
app/Exports/ExportOrder2.php

@@ -508,4 +508,188 @@ class ExportOrder2 extends DefaultValueBinder implements WithCustomValueBinder ,
             },
         ];
     }
+
+    private function jc4_set()
+    {
+        return [
+            AfterSheet::class => function (AfterSheet $event) {
+                $sheet = $event->sheet->getDelegate();
+
+                // ==========================
+                // 1. 第一行:大标题 (A1:Z2)
+                // ==========================
+                $sheet->mergeCells('A1:AB2');
+                $sheet->setCellValue('A1', '各项目利润');
+
+                // ==========================
+                // 2. 第三、四行:固定表头与人员分组
+                // ==========================
+                $sheet->mergeCells('A3:A4');
+                $sheet->setCellValue('A3', '分类');
+                $sheet->mergeCells('B3:B4');
+                $sheet->setCellValue('B3', '项目');
+
+                //大卖场
+                $sheet->mergeCells('C3:D3');
+                $sheet->setCellValue('C3', '大卖场');
+                $sheet->setCellValue('C4', '当月');
+                $sheet->setCellValue('D4', '年累计(7月-明年的6月)');
+
+                //杭州大红鹰超市有限公司
+                $sheet->mergeCells('E3:F3');
+                $sheet->setCellValue('E3', '杭州大红鹰超市有限公司');
+                $sheet->setCellValue('E4', '当月');
+                $sheet->setCellValue('F4', '年累计(7月-明年的6月)');
+
+                // 杭州宏祐贸易有限公司
+                $sheet->mergeCells('G3:H3');
+                $sheet->setCellValue('G3', '杭州宏祐贸易有限公司');
+                $sheet->setCellValue('G4', '当月');
+                $sheet->setCellValue('H4', '年累计(7月-明年的6月)');
+
+                // 联华加盟
+                $sheet->mergeCells('I3:J3');
+                $sheet->setCellValue('I3', '联华加盟');
+                $sheet->setCellValue('I4', '当月');
+                $sheet->setCellValue('J4', '年累计(7月-明年的6月)');
+
+                // 社区
+                $sheet->mergeCells('K3:L3');
+                $sheet->setCellValue('K3', '社区');
+                $sheet->setCellValue('K4', '当月');
+                $sheet->setCellValue('L4', '年累计(7月-明年的6月)');
+
+                // 通路
+                $sheet->mergeCells('M3:N3');
+                $sheet->setCellValue('M3', '通路');
+                $sheet->setCellValue('M4', '当月');
+                $sheet->setCellValue('N4', '年累计(7月-明年的6月)');
+
+                // 浙江汇德隆食品有限公司
+                $sheet->mergeCells('O3:P3');
+                $sheet->setCellValue('O3', '浙江汇德隆食品有限公司');
+                $sheet->setCellValue('O4', '当月');
+                $sheet->setCellValue('P4', '年累计(7月-明年的6月)');
+
+                // 浙江灵峰教育后勤管理有限公司
+                $sheet->mergeCells('Q3:R3');
+                $sheet->setCellValue('Q3', '浙江灵峰教育后勤管理有限公司');
+                $sheet->setCellValue('Q4', '当月');
+                $sheet->setCellValue('R4', '年累计(7月-明年的6月)');
+
+                // 浙江物联电子商务有限公司
+                $sheet->mergeCells('S3:T3');
+                $sheet->setCellValue('S3', '浙江物联电子商务有限公司');
+                $sheet->setCellValue('S4', '当月');
+                $sheet->setCellValue('T4', '年累计(7月-明年的6月)');
+
+                // 浙江新宇贸易有限公司
+                $sheet->mergeCells('U3:V3');
+                $sheet->setCellValue('U3', '浙江新宇贸易有限公司');
+                $sheet->setCellValue('U4', '当月');
+                $sheet->setCellValue('V4', '年累计(7月-明年的6月)');
+
+                // 智鲸
+                $sheet->mergeCells('W3:X3');
+                $sheet->setCellValue('W3', '智鲸');
+                $sheet->setCellValue('W4', '当月');
+                $sheet->setCellValue('X4', '年累计(7月-明年的6月)');
+
+                // 行政
+                $sheet->mergeCells('Y3:Z3');
+                $sheet->setCellValue('Y3', '行政');
+                $sheet->setCellValue('Y4', '当月');
+                $sheet->setCellValue('Z4', '年累计(7月-明年的6月)');
+
+                // 合计
+                $sheet->mergeCells('AA3:AB3');
+                $sheet->setCellValue('AA3', '合计');
+                $sheet->setCellValue('AA4', '当月');
+                $sheet->setCellValue('AB4', '年累计(7月-明年的6月)');
+
+                $sheet->mergeCells('A5:A8');
+                $sheet->setCellValue('A5', '收入');
+
+                $sheet->mergeCells('A9:A33');
+                $sheet->setCellValue('A9', '销售支出');
+
+                $items = [
+                    5  => "回款销售收入",
+                    6  => "票扣费用(返利、调整等)",
+                    7  => "回款业务成本",
+                    8  => "回款销售毛利",
+                    9  => "结算费用(560116)",
+                    10  => "管理费(收入*5%)",
+                    11 => "物流配送(2.3%)",
+                    12 => "合同费(560115)",
+                    13 => "账扣费用(560114)",
+                    14 => "促销员工资(560113)",
+                    15 => "团购及其他返点(560105)",
+                    16 => "陈列费(560120)",
+                    17 => "客情费(560112)",
+                    18 => "赠品费用(560111)",
+                    19 => "广告宣传费(560110)",
+                    20 => "快递费(560109)",
+                    21 => "销售其他费用(560101)",
+                    22 => "员工工资支出(560119)",
+                    23 => "员工社保、福利费(560118)",
+                    24 => "退货亏损(不良仓)",
+                    25 => "采购费用(手工)(按人事计提)",
+                    26 => "税金(1%)",
+                    27 => "财务手续费(1%)",
+                    28 => "其他出库单",
+                    29 => "其他(1)为后续准备",
+                    30 => "其他(2)为后续准备",
+                    31 => "其他(3)为后续准备",
+                    32 => "其他(4)为后续准备",
+                    33 => "销售支出小计",
+                ];
+
+                foreach ($items as $rowNum => $text) {
+                    $sheet->setCellValue("B{$rowNum}", $text);
+                }
+
+                // ==========================
+                // 4. 样式设置
+                // ==========================
+                $lastCol = 'AB';
+                $sheet->getStyle("A1:{$lastCol}33")
+                    ->getAlignment()
+                    ->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)
+                    ->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER)
+                    ->setWrapText(true);
+
+                // 设置列宽
+                $sheet->getColumnDimension('A')->setWidth(10);
+                $sheet->getColumnDimension('B')->setWidth(35);
+
+                // 使用数字索引循环,从 C (第3列) 到 AB (第28列)
+                for ($i = 3; $i <= 28; $i++) {
+                    $colLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($i);
+                    $sheet->getColumnDimension($colLetter)->setWidth(16);
+                }
+
+                // ==========================
+                // 5. 数据填充逻辑 (从C5开始)
+                // ==========================
+
+                if (!empty($this->data)) {
+                    // 彻底废弃 fromArray
+                    foreach ($this->data as $rowNum => $rowValues) {
+                        // 这里的 $rowNum 直接就是 5, 6, ..., 28, 33
+                        // 即使数据里混进了 29-32,我们也再拦截一次
+                        if ($rowNum >= 29 && $rowNum <= 32) {
+                            continue;
+                        }
+
+                        foreach ($rowValues as $colKey => $val) {
+                            $colNum = $colKey + 3; // C列开始
+                            $colLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($colNum);
+                            $sheet->setCellValue($colLetter . $rowNum, $val);
+                        }
+                    }
+                }
+            },
+        ];
+    }
 }

+ 17 - 12
app/Service/ExportFileService.php

@@ -375,18 +375,7 @@ class ExportFileService extends Service
         return [true, $this->saveExportData2($return)];
     }
 
-    //        $data = [];
-//        for ($i = 0; $i < 22; $i++) {
-//            $row = [];
-//            foreach ($title as $value) {
-//                $row[] = rand(1000, 5000);   // 当月
-//                $row[] = rand(10000, 20000); // 累计
-//            }
-//            $data[] = $row;
-//        }dd($data);
-//
-//        dd($data, $title);
-    public function eve($ergs,$user){
+    public function eveold($ergs,$user){
         $service = new StatisticsService();
         $result = $service->getChannelReport($ergs);
         if(empty($result)) return [false, '暂无数据'];
@@ -546,6 +535,22 @@ class ExportFileService extends Service
         return [true, $filename];
     }
 
+    public function eve($ergs,$user){
+        $service = new StatisticsService();
+        $result = $service->getChannelReportItem($ergs); // 得到 5 - 33 行的二维数组
+
+        if (!$result || (isset($result[0]) && $result[0] === false)) {
+            return [false, $result[1] ?? '暂无数据'];
+        }
+        $export = new ExportOrder2($result, 'jc4', [], $ergs['order_time']);
+        if(empty($file_name)) $file_name = self::$filename . "_". date("Y-m-d") . "_". rand(1000,9999);
+        $filename =  $file_name . '.' . 'xlsx';
+
+        Excel::store($export, "public/export/{$filename}");
+
+        return [true, $filename];
+    }
+
     public function twl($ergs,$user){
         $service = new StatisticsService();
         $result = $service->getChannelReportMan($ergs); // 得到 5-32 行的二维数组

+ 262 - 0
app/Service/StatisticsService.php

@@ -1538,4 +1538,266 @@ class StatisticsService extends Service
 
         return $finalData;
     }
+
+    public function getChannelReportItem1($data)
+    {
+        if (empty($data['order_time'])) return [false, '年月不能为空'];
+        $yearMonth = $data['order_time'];
+
+        [$year, $month] = explode('-', $yearMonth);
+        $year = (int)$year;
+        $month = (int)$month;
+
+        // 1. 计算时间范围:7月财年规则
+        $currentMonthTs = mktime(0, 0, 0, $month, 1, $year);
+        $startYear = ($month >= 7) ? $year : $year - 1;
+        $startTs = mktime(0, 0, 0, 7, 1, $startYear);
+
+        // 2. 一次性获取数据
+        $records = DB::table('item_report')
+            ->whereBetween('time', [$startTs, $currentMonthTs])
+            ->get();
+
+        // 3. 字段映射 (注意 24 行,若数据库无字段则设为 null 会自动补 0)
+        $rowFieldMapping = [
+            5  => 'receipt_amount',   6  => 'pk_amount',       7  => 'cost',    8  => 'profit',
+            9  => 'settle_amount',    10  => 'gl_amount',      11 => 'wl_amount',
+            12 => 'ht_amount',        13 => 'zk_amount',       14 => 'cx_amount',
+            15 => 'tg_amount',        16 => 'cl_amount',       17 => 'kq_amount',
+            18 => 'zp_amount',        19 => 'gg_amount',       20 => 'kd_amount',
+            21 => 'xsqt_amount',      22 => 'ry_amount',       23 => 'sb_amount',
+            24 => null,               // 退货亏损 (不良仓),暂无字段映射,填充0
+            25 => 'cg_amount',        26 => 'sj_amount',
+            27 => 'sx_amount',        28 => 'other_ck_amount'
+        ];
+
+        // 4. 初始化列容器 (C 到 Z)
+        $columns = [];
+        $columnKeys = range('C', 'AB');
+        foreach ($columnKeys as $col) {
+            $columns[$col] = array_fill(5, 33, 0); // 覆盖 5-33 行
+        }
+
+        // 5. 数据分拣累加
+        foreach ($records as $record) {
+            $channel_details = $record->channel_details;
+            $isCurrent = ((int)$record->time === $currentMonthTs);
+
+            foreach ($rowFieldMapping as $rowIdx => $field) {
+                $val = $field ? (float)($record->$field ?? 0) : 0;
+
+                if ($channel_details === '大卖场') {
+                    if ($isCurrent) $columns['C'][$rowIdx] += $val;
+                    $columns['F'][$rowIdx] += $val;
+                }
+                if (($empId === 2 || $empId === 5) && $finance === '通路') {
+                    if ($isCurrent) $columns['D'][$rowIdx] += $val;
+                    $columns['F'][$rowIdx] += $val;
+                }
+                // 金小勇
+                if ($empId === 3 && $finance === '通路') {
+                    if ($isCurrent) $columns['G'][$rowIdx] += $val;
+                    $columns['J'][$rowIdx] += $val;
+                }
+                if ($empId === 3 && $finance === '联华加盟') {
+                    if ($isCurrent) $columns['H'][$rowIdx] += $val;
+                    $columns['J'][$rowIdx] += $val;
+                }
+                // 沈强
+                if ($empId === 23 && $finance === '通路') {
+                    if ($isCurrent) $columns['K'][$rowIdx] += $val;
+                    $columns['N'][$rowIdx] += $val;
+                }
+                if ($empId === 23 && $finance === '智鲸') {
+                    if ($isCurrent) $columns['L'][$rowIdx] += $val;
+                    $columns['N'][$rowIdx] += $val;
+                }
+                // 王利英等组合 (O/P列)
+                if (in_array($empId, [7, 8, 54]) && in_array($finance, ['通路', '大卖场'])) {
+                    if ($isCurrent) $columns['O'][$rowIdx] += $val;
+                    $columns['P'][$rowIdx] += $val;
+                }
+                // 叶南汝
+                if ($empId === 48 && $finance === '通路') {
+                    if ($isCurrent) $columns['Q'][$rowIdx] += $val;
+                    $columns['T'][$rowIdx] += $val;
+                }
+                if ($empId === 48 && $finance === '大卖场') {
+                    if ($isCurrent) $columns['R'][$rowIdx] += $val;
+                    $columns['T'][$rowIdx] += $val;
+                }
+                // 行政
+                if ($finance === '行政') {
+                    if ($isCurrent) $columns['U'][$rowIdx] += $val;
+                    $columns['V'][$rowIdx] += $val;
+                }
+                // 鲍总
+                if ($finance === '鲍总') {
+                    if ($isCurrent) $columns['W'][$rowIdx] += $val;
+                    $columns['X'][$rowIdx] += $val;
+                }
+            }
+        }
+
+        // 6. 计算合计列与纵向小计
+        for ($i = 5; $i <= 27; $i++) {
+            // 合计列
+            $columns['E'][$i] = $columns['C'][$i] + $columns['D'][$i];
+            $columns['I'][$i] = $columns['G'][$i] + $columns['H'][$i];
+            $columns['M'][$i] = $columns['K'][$i] + $columns['L'][$i];
+            $columns['S'][$i] = $columns['Q'][$i] + $columns['R'][$i];
+
+            // Y/Z 总计
+            $columns['Y'][$i] = $columns['C'][$i] + $columns['D'][$i] + $columns['G'][$i] + $columns['H'][$i] +
+                $columns['K'][$i] + $columns['L'][$i] + $columns['O'][$i] + $columns['Q'][$i] +
+                $columns['R'][$i] + $columns['U'][$i] + $columns['W'][$i];
+
+            $columns['Z'][$i] = $columns['F'][$i] + $columns['J'][$i] + $columns['N'][$i] + $columns['P'][$i] +
+                $columns['T'][$i] + $columns['V'][$i] + $columns['X'][$i];
+        }
+
+        // 纵向小计 (第 32 行 = 8 到 31 行累加)
+        foreach ($columnKeys as $col) {
+            $subTotal = 0;
+            for ($r = 8; $r <= 31; $r++) {
+                $subTotal += (float)($columns[$col][$r] ?? 0);
+            }
+            $columns[$col][32] = $subTotal;
+        }
+
+        // 7. 最终格式化
+        $finalData = [];
+        for ($row = 5; $row <= 32; $row++) {
+            $rowData = [];
+            foreach ($columnKeys as $colLetter) {
+                $val = $columns[$colLetter][$row] ?? 0;
+
+                if ($row >= 28 && $row <= 31) {
+                    $rowData[] = ''; // 预留行填空
+                } else {
+                    $rowData[] = ($val == 0) ? '0.00' : number_format($val, 2, '.', '');
+                }
+            }
+            $finalData[] = $rowData;
+        }
+
+        return $finalData;
+    }
+
+    public function getChannelReportItem($data)
+    {
+        if (empty($data['order_time'])) return [false, '年月不能为空'];
+        $yearMonth = $data['order_time'];
+
+        [$year, $month] = explode('-', $yearMonth);
+        $year = (int)$year;
+        $month = (int)$month;
+
+        // 1. 财年计算 (7月起算)
+        $currentMonthTs = mktime(0, 0, 0, $month, 1, $year);
+        $startYear = ($month >= 7) ? $year : $year - 1;
+        $startTs = mktime(0, 0, 0, 7, 1, $startYear);
+
+        // 2. 获取数据
+        $records = DB::table('item_report')
+            ->whereBetween('time', [$startTs, $currentMonthTs])
+            ->get();
+
+        // 3. 字段映射 (严格对应 B 列 5-28 行项目)
+        $rowFieldMapping = [
+            5  => 'receipt_amount',   6  => 'pk_amount',       7  => 'cost',           8  => 'profit',
+            9  => 'settle_amount',    10 => 'gl_amount',       11 => 'wl_amount',      12 => 'ht_amount',
+            13 => 'zk_amount',        14 => 'cx_amount',       15 => 'tg_amount',      16 => 'cl_amount',
+            17 => 'kq_amount',        18 => 'zp_amount',       19 => 'gg_amount',      20 => 'kd_amount',
+            21 => 'xsqt_amount',      22 => 'ry_amount',       23 => 'sb_amount',
+            24 => null,               // 退货亏损 (不良仓) 预留映射位
+            25 => 'cg_amount',        26 => 'sj_amount',       27 => 'sx_amount',      28 => 'other_ck_amount'
+        ];
+
+        // 4. 初始化列容器 (C 到 AB, 即第 3 列到第 28 列)
+        $columnKeys = [];
+        for ($i = 3; $i <= 28; $i++) {
+            $columnKeys[] = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($i);
+        }
+
+        $columns = [];
+        foreach ($columnKeys as $col) {
+            $columns[$col] = array_fill(5, 29, 0); // 初始化 5-33 行
+        }
+
+        // 渠道名称与其起始列(当月列)的映射
+        $channelMap = [
+            '大卖场' => 'C',
+            '杭州大红鹰超市有限公司' => 'E',
+            '杭州宏祐贸易有限公司' => 'G',
+            '联华加盟' => 'I',
+            '社区' => 'K',
+            '通路' => 'M',
+            '浙江汇德隆食品有限公司' => 'O',
+            '浙江灵峰教育后勤管理有限公司' => 'Q',
+            '浙江物联电子商务有限公司' => 'S',
+            '浙江新宇贸易有限公司' => 'U',
+            '智鲸' => 'W',
+            '行政' => 'Y',
+        ];
+
+        // 5. 分拣累加
+        foreach ($records as $record) {
+            $channel = $record->channel_details;
+            $isCurrent = ((int)$record->time === $currentMonthTs);
+
+            if (isset($channelMap[$channel])) {
+                $curCol = $channelMap[$channel]; // 当月列
+                // 安全获取下一列(累计列)字母,避免 chr() 处理双字母失败
+                $accCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(
+                    \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($curCol) + 1
+                );
+
+                foreach ($rowFieldMapping as $rowIdx => $field) {
+                    $val = $field ? (float)($record->$field ?? 0) : 0;
+                    if ($isCurrent) $columns[$curCol][$rowIdx] += $val;
+                    $columns[$accCol][$rowIdx] += $val;
+                }
+            }
+        }
+
+        // 6. 计算横向合计 (AA, AB) 与 纵向小计 (第 33 行)
+        $channelStartCols = array_values($channelMap);
+        for ($row = 5; $row <= 28; $row++) {
+            foreach ($channelStartCols as $sCol) {
+                $aCol = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(
+                    \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($sCol) + 1
+                );
+                $columns['AA'][$row] += $columns[$sCol][$row];
+                $columns['AB'][$row] += $columns[$aCol][$row];
+            }
+        }
+
+        // 计算纵向小计 (第 33 行 = 9 到 28 行的累加,跳过预留行)
+        foreach ($columnKeys as $col) {
+            $subTotal = 0;
+            for ($r = 9; $r <= 28; $r++) {
+                $subTotal += (float)($columns[$col][$r] ?? 0);
+            }
+            $columns[$col][33] = $subTotal;
+        }
+
+        // 7. 最终格式化输出
+
+        $finalData = [];
+        for ($row = 5; $row <= 33; $row++) {
+            // 关键:29-32行我们直接不生成数据,或者生成空
+            if ($row >= 29 && $row <= 32) continue;
+
+            $rowData = [];
+            foreach ($columnKeys as $colLetter) {
+                $val = $columns[$colLetter][$row] ?? 0;
+                $rowData[] = ($val == 0) ? '0.00' : number_format($val, 2, '.', '');
+            }
+            // 使用行号作为 Key
+            $finalData[$row] = $rowData;
+        }
+
+        return $finalData;
+    }
 }