cqp 3 weeks ago
parent
commit
e68568af8b

+ 55 - 16
app/Exports/TableHeadExport.php

@@ -2,50 +2,89 @@
 
 
 namespace App\Exports;
 namespace App\Exports;
 
 
-
 use Illuminate\Support\Collection;
 use Illuminate\Support\Collection;
 use Maatwebsite\Excel\Concerns\FromCollection;
 use Maatwebsite\Excel\Concerns\FromCollection;
 use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
 use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
 use Maatwebsite\Excel\Concerns\WithHeadings;
 use Maatwebsite\Excel\Concerns\WithHeadings;
-use Maatwebsite\Excel\Concerns\WithStrictNullComparison;    // 导出 0 原样显示,不为 null
+use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
+use Maatwebsite\Excel\Concerns\WithEvents;
+use Maatwebsite\Excel\Events\AfterSheet;
 use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
 use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
+use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
 
 
-
-class TableHeadExport extends DefaultValueBinder implements WithCustomValueBinder , FromCollection, WithStrictNullComparison,withHeadings
+class TableHeadExport extends DefaultValueBinder implements
+    WithCustomValueBinder,
+    FromCollection,
+    WithStrictNullComparison,
+    WithHeadings,
+    WithEvents
 {
 {
-    /**
-     * @return \Illuminate\Support\Collection
-     */
-    public function __construct($data,$headers)
+    protected $data;
+    protected $headers;
+    protected $headerComments; // 表头批注:键=表头名,值=批注内容
+
+    public function __construct($data, $headers = [], $headerComments = [])
     {
     {
         $this->data = $data;
         $this->data = $data;
         $this->headers = $headers;
         $this->headers = $headers;
+        $this->headerComments = $headerComments;
     }
     }
 
 
-    //数组转集合
     public function collection()
     public function collection()
     {
     {
         return new Collection($this->createData());
         return new Collection($this->createData());
     }
     }
-    //业务代码
+
     public function createData()
     public function createData()
     {
     {
-        $data = $this->export();
-        return $data;
+        return $this->export();
     }
     }
 
 
-    // 自定义表头,需实现withHeadings接口
     public function headings(): array
     public function headings(): array
     {
     {
         return $this->headers;
         return $this->headers;
     }
     }
 
 
-    private function export(){
+    private function export()
+    {
         $list = [];
         $list = [];
-//        dump($this->data);die;
-        foreach ($this->data as $v){
+        foreach ($this->data as $v) {
             $list[] = $v;
             $list[] = $v;
         }
         }
         return $list;
         return $list;
     }
     }
+
+    public function registerEvents(): array
+    {
+        return [
+            AfterSheet::class => function(AfterSheet $event) {
+                if (empty($this->headerComments)) {
+                    return; // 不传就不加批注
+                }
+
+                $sheet = $event->sheet->getDelegate();
+
+                foreach ($this->headers as $index => $headerName) {
+                    if (!isset($this->headerComments[$headerName])) {
+                        continue; // 该表头没设置批注
+                    }
+
+                    $columnLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($index + 1);
+                    $cell = $columnLetter . '1';
+
+                    $commentText = $this->headerComments[$headerName];
+
+                    // 创建批注
+                    $comment = $sheet->getComment($cell);
+                    $comment->getText()->createTextRun($commentText);
+                    $comment->setAuthor('系统提示');
+
+                    // 设置批注框大小(可调整数值)
+                    $comment->setWidth('200pt');   // 宽度
+                    $comment->setHeight('100pt');  // 高度
+                }
+            }
+        ];
+    }
+
 }
 }

+ 2 - 2
app/Service/GiveOutService.php

@@ -121,7 +121,7 @@ class GiveOutService extends Service
     public function giveOutList($data,$user){
     public function giveOutList($data,$user){
         $model = $this->giveOutCommon($data, $user);
         $model = $this->giveOutCommon($data, $user);
         $list = $this->limit($model,'',$data);
         $list = $this->limit($model,'',$data);
-        $list = $this->fillData($list,$user,$data);
+        $list = $this->fillData($list);
 
 
         return [true, $list];
         return [true, $list];
     }
     }
@@ -160,7 +160,7 @@ class GiveOutService extends Service
         return [true, $data];
         return [true, $data];
     }
     }
 
 
-    public function fillData($data, $user, $search){
+    public function fillData($data){
         if(empty($data['data'])) return $data;
         if(empty($data['data'])) return $data;
 
 
         foreach ($data['data'] as $key => $value){
         foreach ($data['data'] as $key => $value){

+ 139 - 2
app/Service/ImportService.php

@@ -4,7 +4,9 @@ namespace App\Service;
 
 
 use App\Exports\TableHeadExport;
 use App\Exports\TableHeadExport;
 use App\Import\ImportAll;
 use App\Import\ImportAll;
+use App\Model\Employee;
 use App\Model\Freight;
 use App\Model\Freight;
+use App\Model\GiveOut;
 use App\Model\Product;
 use App\Model\Product;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Log;
@@ -17,6 +19,7 @@ class ImportService extends Service
     public static $type = [
     public static $type = [
         'product', //存货
         'product', //存货
         'freight', //运费设置
         'freight', //运费设置
+        'giveOut', //发放统计
     ];
     ];
 
 
     public function getTableTitleXls($data,$user){
     public function getTableTitleXls($data,$user){
@@ -30,7 +33,11 @@ class ImportService extends Service
         if(! $status) return [false, $msg];
         if(! $status) return [false, $msg];
 
 
         $headers = array_column($msg,'value');
         $headers = array_column($msg,'value');
-        Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []);
+        $comments = [];
+        foreach ($msg as $value){
+            if(! empty($value['comments'])) $comments[$value['value']] = $value['comments'];
+        }
+        Excel::store(new TableHeadExport([], $headers, $comments),"/public/export/{$filename}", null, 'Xlsx', []);
         return [true, ['file' => $filename]];
         return [true, ['file' => $filename]];
     }
     }
 
 
@@ -62,6 +69,16 @@ class ImportService extends Service
         return [true, [$config_array, $filename]];
         return [true, [$config_array, $filename]];
     }
     }
 
 
+    private function giveOut($data,$user){
+        $config_array = $this->getTableConfig($data['type']);
+        if(empty($config_array)) return [false, ['导入配置表头文件不存在','']];
+
+        //生成下载文件
+        $filename =  "发放统计导入模板_" . time() . '.' . 'xlsx';
+
+        return [true, [$config_array, $filename]];
+    }
+
     //导入入口
     //导入入口
     public function importAll($data,$user){
     public function importAll($data,$user){
 //        //不超时
 //        //不超时
@@ -205,7 +222,6 @@ class ImportService extends Service
             $tmp['crt_id'] = $user['id'];
             $tmp['crt_id'] = $user['id'];
             $tmp['crt_time'] = $time;
             $tmp['crt_time'] = $time;
 
 
-            //产品主表
             $insert[] = $tmp;
             $insert[] = $tmp;
         }
         }
 
 
@@ -224,6 +240,95 @@ class ImportService extends Service
         return [true, ''];
         return [true, ''];
     }
     }
 
 
+    public function giveOutImport($array, $user, $other_param){
+        $upload = $array[0];
+        list($status, $msg) = $this->compareTableAndReturn($upload, $other_param);
+        if(! $status) return [false, $msg];
+        $table_config = $msg;
+
+        // 去除表头
+        unset($array[0]);
+        if(empty($array)) return [false, '导入数据不能为空'];
+
+        list($array, $error) = $this->checkCommon($array, $table_config);
+        if(! empty($error)) return [0, $error];
+        if(empty($array)) return [false, '导入数据不能为空'];
+
+        $time = time();
+        $new_array = [];
+        foreach ($array as $value){
+            $tmp = [];
+            foreach ($value as $k => $val){
+                $field = $table_config[$k]['key'];
+                $tmp[$field] = $val;
+            }
+            $tmp['crt_id'] = $user['id'];
+            $tmp['crt_time'] = $time;
+
+            $new_array[] = $tmp;
+        }
+        $emp_map = Employee::where('del_time',0)
+            ->whereIn('emp_name', array_column($new_array,'employee_id_1_title'))
+            ->pluck('id','emp_name')
+            ->toArray();
+
+        $error_2 = [];
+        foreach ($new_array as $key => $value){
+            $line_number = $key + 2;
+            $line = '第' . $line_number . '行';
+            if(! isset($emp_map[$value['employee_id_1_title']])) {
+                $error_2[] = $line . "业务员不存在或已被删除";
+            }else{
+                $new_array[$key]['employee_id_1'] = $emp_map[$value['employee_id_1_title']];
+            }
+            list($status, $msg) = $this->convertExcelCellToDate($value['send_time']);
+            if(! $status){
+                $error_2[] = $line . "发放日期错误";
+            }else{
+                $new_array[$key]['send_time'] = $msg;
+            }
+            if (strpos($value['belong_time'], '|') !== false) {
+                list($start,$end) = explode('|', $value['belong_time']);
+                $startStamp = strtotime($start);
+                $endStamp = strtotime($end);
+                if(! $startStamp || ! $endStamp || $startStamp > $endStamp) {
+                    $error_2[] = $line . "归属日期错误";
+                }else{
+                    $new_array[$key]['start_time'] = $startStamp;
+                    $new_array[$key]['end_time'] = $endStamp;
+                    unset($new_array[$key]['belong_time']);
+                }
+            }else{
+                list($status, $msg) = $this->convertExcelCellToDate($value['belong_time']);
+                if(! $status){
+                    $error_2[] = $line . "归属日期错误";
+                }else{
+                    $new_array[$key]['start_time'] = $msg;
+                    $new_array[$key]['end_time'] = $msg;
+                    unset($new_array[$key]['belong_time']);
+                }
+            }
+            $res = $this->checkNumber($value['give_out_amount'],2,'positive');
+            if(! $res['valid']) $error_2[] = $line . "分红已发放金额:" . $res['error'];
+        }
+        if(! empty($error_2)) {
+            $error_string = implode('|', $error_2);
+            return [0, $error_string];
+        }
+
+        try{
+            //新增
+            if(! empty($new_array)) GiveOut::insert($new_array);
+
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
+        }
+
+        return [true, ''];
+    }
+
     //公共校验
     //公共校验
     private function checkCommon($array, $table_config){
     private function checkCommon($array, $table_config){
         $error = [];
         $error = [];
@@ -304,5 +409,37 @@ class ImportService extends Service
 
 
         return [true, $config_array];
         return [true, $config_array];
     }
     }
+
+    //转换日期
+    function convertExcelCellToDate($cellValue) {
+        // 尝试将单元格值转换为浮点数(Excel 日期序列号)
+        $excelTimestamp = filter_var($cellValue, FILTER_VALIDATE_FLOAT);
+
+        if ($excelTimestamp !== false && $excelTimestamp > 0) {
+            // 如果成功转换并且值大于0,则认为是Excel日期序列号
+            try {
+                $dateTimeObject = Date::excelToDateTimeObject($cellValue);
+
+//                if ($dateTimeObject->format('H:i:s') === '00:00:00') {
+//                    // 如果是,则将时间设置为 '23:59:00'
+//                    $dateTimeObject->setTime(23, 59);
+//                }
+
+                // 现在你可以格式化这个日期了
+                $formattedDate = $dateTimeObject->format('Y-m-d');
+                if(! strtotime($formattedDate)) return [false, ''];
+
+                return [true,  strtotime($formattedDate)];
+            } catch (\Exception $e) {
+                // 处理转换失败的情况
+                return [false, '单元格日期格式转换时间戳失败'];
+            }
+        }
+
+        // 如果不是有效的浮点数,则尝试按照多种日期格式解析
+        if(! strtotime($cellValue)) return [false, '单元格文本格式转换时间戳失败'];
+
+        return [true, strtotime($cellValue)];
+    }
 }
 }
 
 

+ 35 - 0
config/excel/giveOutTable.php

@@ -0,0 +1,35 @@
+<?php
+return [
+    [
+        'key' =>'employee_id_1_title',
+        'value' => '业务员名',
+        'required' => true,
+        'default' => "",
+        'comments' => '人员名称',
+    ],
+    [
+        'key' =>'send_time',
+        'value' => '发放日期',
+        'required' => true,
+        'default' => "",
+    ],
+    [
+        'key' =>'belong_time',
+        'value' => '归属日期',
+        'required' => true,
+        'default' => "",
+        'comments' => '时间段(2025-01-03|2025-01-05),时间(2025-01-03)',
+    ],
+    [
+        'key' =>'give_out_amount',
+        'value' => '分红已发放金额',
+        'required' => true,
+        'default' => 0,
+    ],
+    [
+        'key' =>'mark',
+        'value' => '备注',
+        'required' => false,
+        'default' => "",
+    ],
+];