Kaynağa Gözat

Merge branch 'dev_wf' of wugg/phpstock into version1.5

wufeng 2 yıl önce
ebeveyn
işleme
68ccc46c6d

+ 1 - 78
app/admin/controller/Report.php

@@ -42,7 +42,7 @@ class Report extends Base
         'delivery_day' => '物流时间',
         'lead_time' => '供货周期',
         'sample_day' => '调样周期',
-        'sample_fee' => '调样费',
+        'sample_fee' => '调样费',
         'good_img' => '商品详情主图 ',
         'good_thumb_img' => '商品缩略图',
         'good_info_img' => '商品详情图',
@@ -1474,7 +1474,6 @@ class Report extends Base
 
             $before_info = json_decode($value['修改前的内容'], true);
             $after_info = json_decode($value['修改后的内容'], true);
-
             if (isset($after_info['field_change'])) {
                 $field_change = json_decode($after_info['field_change'], true);
 
@@ -1686,80 +1685,4 @@ class Report extends Base
          **/
     }
 
-    //【十、退货台账-业务口径】
-    public function ReturnExport()
-    {
-        $param = $this->request->only([
-            'token',
-            'start_date' => '',
-            'end_date' => ''
-        ], 'post', 'trim');
-
-        $val_params = Validate::rule([
-            'start_date' => 'date|elt:end_date',
-            'end_date' => 'date'
-        ]);
-
-        if (!$val_params->check($param)) return error_show(1004, $val_params->getError());
-
-        $where = [['sr.is_del', '=', 0]];
-        if ($param['start_date'] != '' && $param['end_date'] != '') $where[] = ['sr.addtime', 'between', [$param['start_date'] . ' 00:00:00', $param['end_date'] . ' 23:59:59']];
-
-        $data = Db::name('sale_return')
-            ->alias('sr')
-            ->field('sr.returnCode 退货单号,sr.status 流程进度,sr.addtime 退货发起日期,s.supplierNo 公司名称,sr.apply_name 退货人,sr.num 退货数量,sr.total_fee 退货销售货款,sr.remark 退货备注,c.companyName 客户名称,or.customer_code 客户编码,"" 一级组织,"" 二级组织,"" 三级组织,p.platform_name 平台类型,s.poNo PO编号,sr.orderCode 确认单编号,s.order_type 确认单类型,s.addtime 确认单下单时间,s.apply_name 业务人员,sr.orderCode 确认单产品编号,sr.good_name 产品名称,sr.good_code 产品编码,s.cat_id,c.itemid')
-            ->leftJoin('platform p', 'p.id = sr.platform_id')
-            ->leftJoin('order_return or', 'or.orderCode = sr.orderCode')
-            ->leftJoin('customer_info c', 'c.companyNo = or.customer_code')
-            ->leftJoin('sale s', 's.orderCode = sr.orderCode')
-            ->where($where)
-            ->cursor();
-
-        $all_sale_return_status = [1 => '待业务审批', 2 => '待专员审批', 3 => '待主管审批', 4 => '退货完成', 5 => '业务驳回', 6 => '采购驳回', 7 => '专员审批不通过'];
-        $all_sale_order_type = [1 => '备库', 2 => '非库存', 3 => '咨询采反', 4 => '项目采反', 5 => '平台部订单销售',];
-        $list = [];
-        foreach ($data as $value) {
-
-            $value['流程进度'] = isset($all_sale_return_status[$value['流程进度']]) ? $all_sale_return_status[$value['流程进度']] : '';
-            $value['确认单类型'] = isset($all_sale_order_type[$value['确认单类型']]) ? $all_sale_order_type[$value['确认单类型']] : '';
-
-            $top = made($value['cat_id']);
-            $value['一级分类'] = isset($top[0]['name']) ? $top[0]['name'] : '';
-
-            if (!empty($value['itemid'])) {
-                $customer_org1 = get_top_customer_org($value['itemid']);
-
-                foreach ($customer_org1 as $vv) {
-                    switch ($vv['level']) {
-                        case 1:
-                            $value['一级组织'] = $vv['name'];
-                            break;
-                        case 2:
-                            $value['二级组织'] = $vv['name'];
-                            break;
-                        case 3:
-                            $value['三级组织'] = $vv['name'];
-                            break;
-                    }
-                }
-            }
-
-            unset($value['cat_id']);
-            unset($value['itemid']);
-
-            $list[] = $value;
-        }
-
-        if (empty($list)) $list[] = '没有可供导出的数据';
-
-        $headerArr = array_keys($list[0]);
-        excelSave('退货台账-业务口径' . date('YmdHis'), $headerArr, $list);
-    }
-
-    //库存预警汇总
-    public function StockExport()
-    {
-
-    }
-
 }

+ 114 - 0
app/admin/controller/ReportReserve.php

@@ -0,0 +1,114 @@
+<?php
+
+namespace app\admin\controller;
+
+use think\Exception;
+use think\facade\Cache;
+use think\facade\Db;
+use think\facade\Validate;
+
+//报表预约处理类
+class ReportReserve extends Base
+{
+    //redis队列的key,在ReportReserveHandle脚本中也有定义,要同步修改(轻易不要修改)
+    private $key = 'wanyuhengtong_report_reserve';
+
+    //预约记录编码及对应名称
+    private $code_reserve_name = [
+        'A' => '退货台账-业务口径',
+        'B' => '库存预警汇总',
+        'C' => '退货明细',
+    ];
+
+
+    //获取预约列表
+    public function getList()
+    {
+        $param = $this->request->only([
+            'code' => '',
+            'start_addtime' => '',
+            'end_addtime' => '',
+            'status' => 0,
+            'applyname' => '',
+            'page' => 1,
+            'size' => 15
+        ], 'post', 'trim');
+
+        $where = [];
+        if (!empty($param['code'])) $where[] = ['code', '=', $param['code']];
+        if (!empty($param['start_addtime']) && !empty($param['end_addtime'])) $where[] = ['addtime', 'between', [$param['start_addtime'], $param['end_addtime']]];
+        if (!empty($param['status'])) $where[] = ['status', '=', $param['status']];
+        if (!empty($param['applyname'])) $where[] = ['applyname', 'like', '%' . $param['applyname'] . '%'];
+
+        $count = Db::name('report_form_reserve')
+            ->where($where)
+            ->count('id');
+
+        $list = Db::name('report_form_reserve')
+            ->where($where)
+            ->order('id')
+            ->page($param['page'], $param['size'])
+            ->select()
+            ->toArray();
+
+        return app_show(0, '请求成功', ['list' => $list, 'count' => $count]);
+
+    }
+
+
+    //添加预约记录
+    public function add()
+    {
+
+        $param = $this->request->only(['code', 'start_time', 'end_time', 'token', 'remark' => ''], 'post', 'trim');
+
+        $val = Validate::rule([
+            'token' => 'require',
+            'code|报表编码' => 'require|in:A,B,C',
+            'start_time|开始时间' => 'require|date|elt:end_time',
+            'end_time|结束时间' => 'require|date',
+        ]);
+
+        if (!$val->check($param)) return error_show(1004, $val->getError());
+
+        $user = GetUserInfo($param['token']);
+
+        Db::startTrans();
+        try {
+
+            $insert_id = Db::name('report_form_reserve')
+                ->insertGetId([
+                    'code' => $param['code'],
+                    'name' => $this->code_reserve_name[$param['code']],
+                    'start_time' => $param['start_time'],
+                    'end_time' => $param['end_time'],
+                    'apply_name' => isset($user['data']['nickname']) ? $user['data']['nickname'] : '',
+                    'apply_id' => isset($user['data']['id']) ? $user['data']['id'] : '',
+                    'status' => 1,
+                    'remark' => $param['remark'],
+                    'addtime' => date('Y-m-d H:i:s'),
+                    'updatetime' => date('Y-m-d H:i:s'),
+                ]);
+
+            if (!$insert_id) throw new Exception('预约记录新增失败');
+
+            //lpush 往头部插入元素
+            $rs = Cache::store('redis')->handler()->lpush($this->key, $insert_id);
+
+            if (!$rs) throw new Exception('预约记录加入redis队列失败');
+
+            Db::commit();
+
+            return app_show(0, '添加成功');
+
+        } catch (Exception $exception) {
+
+            Db::rollback();
+
+            return error_show(1005, '添加失败,' . $exception->getMessage());
+        }
+
+    }
+
+
+}

+ 3 - 1
app/admin/route/app.php

@@ -512,7 +512,9 @@ Route::rule('reportchangeloglist','admin/Report/ChangeLogList');//报表-商品
 Route::rule('reportchangelogeexport','admin/Report/ChangeLogExport');//报表-商品报表-商品修改报表_导出
 Route::rule('reportgoodloglist','admin/Report/GoodLogList');//报表-出入库明细报表_列表
 Route::rule('reportgoodlogexport','admin/Report/GoodLogExport');//报表-出入库明细报表_导出
-Route::rule('reportreturnexport','admin/Report/ReturnExport');//报表-退货台账-业务口径
+
+Route::rule('reportreservegetlist','admin/ReportReserve/getList');//报表预约-列表
+Route::rule('reportreserveadd','admin/ReportReserve/add');//报表预约-添加
 
 //数据看板
 Route::rule('datatodaysalesvolume','admin/Data/todaySalesVolume');//1.今日销量

+ 144 - 0
app/command/ReportReserveHandle.php

@@ -0,0 +1,144 @@
+<?php
+declare (strict_types=1);
+
+namespace app\command;
+
+use app\admin\model\Test1;
+use think\console\Command;
+use think\console\Input;
+use think\console\Output;
+use think\facade\Cache;
+use think\facade\Db;
+
+//处理报表预约记录,生成报表文件
+class ReportReserveHandle extends Command
+{
+    //redis队列的key,在ReportReserve控制器中也有定义,要同步修改(轻易不要修改)
+    private $key = 'wanyuhengtong_report_reserve';
+
+    //各个报表的导出字段(这里的字段要和下面的方法中查询字段个数对应)
+    private $field = [
+        'A' => ['退货单号', '流程进度', '退货发起日期', '公司名称', '退货人', '退货数量', '退货销售货款', '退货备注', '客户名称', '客户编码', '一级组织', '二级组织', '三级组织', '平台名称', 'PO编号', '确认单编号', '确认单类型', '确认单下单时间', '业务人员', '确认单产品编号', '产品名称', '产品编码', '一级分类'],
+        'B' => [],
+        'C' => [],
+    ];
+
+    protected function configure()
+    {
+        // 指令配置
+        $this->setName('handleData')
+            ->setDescription('处理报表预约记录,生成报表文件');
+    }
+
+    //处理报表预约记录,生成报表文件
+    protected function execute(Input $input, Output $output)
+    {
+
+        try {
+
+
+            $pk_id = Cache::store('redis')->handler()->rpop($this->key);
+            if ($pk_id) {
+                //有数据,开始处理
+
+                //查询预约记录
+                $rs = Db::name('report_form_reserve')
+                    ->field('id,name,code,start_time,end_time')
+                    ->where(['id' => $pk_id, 'status' => 1])
+                    ->find();
+
+                if ($rs) {
+                    //状态改为处理中
+                    Db::name('report_form_reserve')
+                        ->where(['id' => $pk_id, 'status' => 1])//status==1 待处理
+                        ->update(['status' => 2, 'updatetime' => date('Y-m-d H:i:s')]);//status==2 处理中
+
+                    //处理数据的方法
+                    switch ($rs['code']) {
+                        case 'A':
+                            $res = $this->A($rs['start_time'], $rs['end_time']);
+                            break;
+                    }
+
+                    $file = excelSaveFile($res, $this->field[$rs['code']], $rs['name'] . date('YmdHis'));
+
+                    Db::name('report_form_reserve')
+                        ->where(['id' => $pk_id, 'status' => 2])//status==2 处理中
+                        ->update([
+                            'status' => 3, //status==3 处理完成
+                            'down_url' => $file,
+                            'updatetime' => date('Y-m-d H:i:s'),
+                            'expiretime' => date('Y-m-d H:i:s', strtotime('+1 month'))
+                        ]);
+
+                    $output->writeln('【' . $pk_id . '】该预约记录处理成功');
+
+
+                } else $output->writeln('没有查询到该预约记录');
+
+            } else  $output->writeln('没有可供处理的报表预约记录');
+
+        } catch (\Exception $exception) {
+            $output->writeln($exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
+        }
+
+    }
+
+
+    //数据处理脚本,每个报表一个方法
+    private function A(string $start_date = '', string $end_date = '')
+    {
+
+        $data = Db::name('sale_return')
+            ->alias('sr')
+            ->field('sr.returnCode 退货单号,sr.status 流程进度,sr.addtime 退货发起日期,sup.name 公司名称,sr.apply_name 退货人,sr.num 退货数量,sr.total_fee 退货销售货款,sr.remark 退货备注,c.companyName 客户名称,or.customer_code 客户编码,"" 一级组织,"" 二级组织,"" 三级组织,p.platform_name 平台名称,s.poNo PO编号,sr.orderCode 确认单编号,s.order_type 确认单类型,s.addtime 确认单下单时间,s.apply_name 业务人员,sr.orderCode 确认单产品编号,sr.good_name 产品名称,sr.good_code 产品编码,"" 一级分类,s.cat_id,c.itemid')
+            ->leftJoin('platform p', 'p.id = sr.platform_id')
+            ->leftJoin('order_return or', 'or.orderCode = sr.orderCode')
+            ->leftJoin('customer_info c', 'c.companyNo = or.customer_code')
+            ->leftJoin('sale s', 's.orderCode = sr.orderCode')
+            ->leftJoin('supplier sup', 'sup.code = s.supplierNo')
+            ->where('sr.is_del', 0)
+            ->whereBetween('sr.addtime', [$start_date . ' 00:00:00', $end_date . ' 23:59:59'])
+            ->cursor();
+
+        $all_sale_return_status = [1 => '待业务审批', 2 => '待专员审批', 3 => '待主管审批', 4 => '退货完成', 5 => '业务驳回', 6 => '采购驳回', 7 => '专员审批不通过'];
+        $all_sale_order_type = [1 => '备库', 2 => '非库存', 3 => '咨询采反', 4 => '项目采反', 5 => '平台部订单销售',];
+        $list = [];
+        foreach ($data as $value) {
+
+            $value['流程进度'] = isset($all_sale_return_status[$value['流程进度']]) ? $all_sale_return_status[$value['流程进度']] : '';
+            $value['确认单类型'] = isset($all_sale_order_type[$value['确认单类型']]) ? $all_sale_order_type[$value['确认单类型']] : '';
+
+            $top = made($value['cat_id']);
+            $value['一级分类'] = isset($top[0]['name']) ? $top[0]['name'] : '';
+
+            if (!empty($value['itemid'])) {
+                $customer_org1 = get_top_customer_org($value['itemid']);
+
+                foreach ($customer_org1 as $vv) {
+                    switch ($vv['level']) {
+                        case 1:
+                            $value['一级组织'] = $vv['name'];
+                            break;
+                        case 2:
+                            $value['二级组织'] = $vv['name'];
+                            break;
+                        case 3:
+                            $value['三级组织'] = $vv['name'];
+                            break;
+                    }
+                }
+            }
+
+            unset($value['cat_id']);
+            unset($value['itemid']);
+
+            yield $list[] = $value;
+        }
+
+        return $list;
+
+    }
+
+
+}

+ 80 - 0
app/common.php

@@ -899,6 +899,86 @@ if (!function_exists('excelSaveBatch')) {
     }
 }
 
+
+/**
+ * 批量生成多个文件excel,生成压缩包,保存到本地,返回文件链接
+ * datas 生成器
+ * header array 头部字段
+ * filename string 文件名
+ */
+if (!function_exists('excelSaveFile')) {
+    function excelSaveFile($datas, array $header = [], string $filename = '')
+    {
+
+//        $urls = [];
+        $dir = root_path() . 'public/storage/report/' . date("YmdHis") . "/";
+        if (!is_dir($dir)) mkdir($dir, 0777, true);
+
+//        foreach ($datas as $item) {
+
+        $objPHPExcel = new PHPExcel();
+        $objPHPExcel->getProperties();
+        $keyA = 0; // 设置表头
+        foreach ($header as $v) {
+            $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
+            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
+            $keyA += 1;
+        }
+
+        $column = 2;
+        $objActSheet = $objPHPExcel->getActiveSheet();
+
+        foreach ($datas as $key => $rows) { // 行写入
+            $span = 0;
+            foreach ($rows as $keyName => $value) { // 列写入
+                //判断数据是否有数组,如果有数组,转换成字符串
+
+                if (is_array($value)) $value = implode("、", $value);
+
+                $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
+                $span++;
+            }
+            $column++;
+        }
+        $file = $filename . ".xls";
+
+        $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
+        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
+        $objWriter->save($dir . $file); // 文件通过浏览器下载
+        $url = $dir . $file;
+        if (!file_exists($url)) throw new Exception('文件生成失败');
+//            else $urls[] = $url;
+
+//        }
+
+        $saveDir = root_path() . "public/storage/zip/";
+        if (!is_dir($saveDir)) mkdir($saveDir, 0777, true);
+
+
+//        $datetime = date("YmdHis");
+        $file_dir = $saveDir . $filename . ".zip";
+        # 5.1 文件打包,提示:使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
+        $zip = new \ZipArchive ();
+        # 5.2 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
+        if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !== true) echo '无法打开文件或者文件创建失败';
+
+        # 5.3 批量写入压缩包
+//        $zip->addEmptyDir($fileName);//往zip压缩包写入空目录
+//        foreach ($urls as $fileName) {
+        @$zip->addFile($url, DIRECTORY_SEPARATOR . basename($url));
+//        }
+        // @$zip->addFile($v['file_path'], 'resume'.DIRECTORY_SEPARATOR.basename($headername));
+        # 5.4 关闭压缩包写入
+        $zip->close();
+
+        @deldir($dir);//删除已生成的文件及目录
+        //6. 检查文件是否存在,并输出文件
+        if (!file_exists($file_dir)) throw new Exception('压缩包文件不存在');
+
+        return $file_dir;
+    }
+}
+
 //通过快递网的api获取物流信息
 if (!function_exists('get_logistics_information')) {
     function get_logistics_information(string $post_code = '', string $phone = '')