cqp преди 3 месеца
родител
ревизия
24c31d718c

+ 1 - 1
app/Http/Controllers/Api/StatisticsController.php

@@ -70,7 +70,7 @@ class StatisticsController extends BaseController
     public function statisticsJc(Request $request){
         $service = new StatisticsService();
         $userData = $request->userData->toArray();
-        list($status,$data) = $service->statisticsJc($request->all(),$userData);
+        list($status,$data) = $service->statisticsJcNew($request->all(),$userData);
 
         if($status){
             return $this->json_return(200,'',$data);

+ 6 - 1
app/Http/Controllers/Api/SysMenuController.php

@@ -84,6 +84,11 @@ class SysMenuController extends BaseController
     //获取文件的位置
     public function getExport($file_name){
         $path = storage_path() . "/app/public/export/".$file_name;
-        return response()->file($path)->deleteFileAfterSend(true);
+
+        return response()->download($path, $file_name, [
+            'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+            'Content-Disposition' => 'attachment; filename="' . $file_name . '"',
+        ])->deleteFileAfterSend(true);
+//        return response()->file($path)->deleteFileAfterSend(true);
     }
 }

+ 1 - 0
app/Model/RoleMenuButton.php

@@ -19,4 +19,5 @@ class RoleMenuButton extends Model
     const special_six = -6;
     const special_seven = -7;
     const special_eight = -8;
+    const special_nine = -9;
 }

+ 2 - 1
app/Service/CheckService.php

@@ -1241,6 +1241,7 @@ class CheckService extends Service
             ->where('del_time',0)
             ->first();
         if(empty($model)) return [false, '合同不存在或已被删除'];
+
         //安装件
         if($model->state != SalesOrder::State_four) return [false, '请确认合同状态,操作失败'];
         $model->is_confirm = 1;
@@ -1253,7 +1254,7 @@ class CheckService extends Service
             ->first();
         if(! empty($depart)){
             $depart = $depart->toArray();
-            $channel_id = Depart::where('id',$depart->param_id)->value('channel_id');
+            $channel_id = Depart::where('id',$depart['param_id'])->value('channel_id');
             if(! empty($channel_id)){
                 $emp_name = Employee::where('id',$channel_id)->value('emp_name');
                 $send_data[] = [

+ 26 - 3
app/Service/CustomerService.php

@@ -9,6 +9,7 @@ use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\FollowUpRecord;
 use App\Model\Product;
+use App\Model\RoleMenuButton;
 use App\Model\SeeRange;
 use App\Model\WxEmployeeOfficial;
 use Carbon\Carbon;
@@ -434,6 +435,7 @@ class CustomerService extends Service
             $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
             $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
             $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
+            $this->isTopDispatch($user, $customer[$key]);
             $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
             $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
             $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
@@ -665,7 +667,7 @@ class CustomerService extends Service
     public function customerList($data,$user){
         $model = $this->customerCommonSearch($data,$user);
         $list = $this->limit($model,'',$data);
-        $list = $this->fillData($list,$data);
+        $list = $this->fillData($list,$data, $user);
 
         //微信数据
         $count = $this->countData("crt_time", $data, $user);
@@ -755,7 +757,7 @@ class CustomerService extends Service
     public function customerList2($data,$user){
         $model = $this->customer2CommonSearch($data, $user);
         $list = $this->limit($model,'',$data);
-        $list = $this->fillData($list,$data);
+        $list = $this->fillData($list,$data,$user);
 
         //微信数据
         $count = $this->countData2("fp_time", $data, $user);
@@ -883,7 +885,7 @@ class CustomerService extends Service
      * @param $data
      * @return array
      */
-    public function fillData($data,$ergs){
+    public function fillData($data,$ergs,$user){
         if(empty($data['data'])) return $data;
 
         $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_intention'),array_column($data['data'],'customer_from'),array_column($data['data'],'customer_type'),array_column($data['data'],'car_type'),array_column($data['data'],'progress_stage'),array_column($data['data'],'state_type'),array_column($data['data'],'customer_state')));
@@ -948,6 +950,7 @@ class CustomerService extends Service
 
         $depart2_map = $this->getCustomerDepart($customer_id);
 
+        //地址
         $address_map = config('address');
         foreach ($data['data'] as $key => $value){
             $address_str = [];
@@ -962,7 +965,9 @@ class CustomerService extends Service
             }
             $data['data'][$key]['address'] = $address;
             $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
+            //客户来源
             $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
+            $this->isTopDispatch($user, $data['data'][$key]);
             $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
             $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
             $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
@@ -1001,6 +1006,24 @@ class CustomerService extends Service
         return $data;
     }
 
+    private function isTopDispatch($user, &$value){
+        //特殊功能按钮
+        $special_button = $user['special_button'] ?? [];
+        //总社
+        $head = $user['head']['id'] ?? 0;
+        //当前门店
+        $top_depart_id = $user['depart_top'][0] ?? [];
+        $top_depart_id = $top_depart_id['depart_id'] ?? 0;
+
+        //分社账号
+        if($top_depart_id != $head){
+            //总社创建分配给分社的客户
+            if($value['top_depart_id'] == $head && $value['fp_time'] > 0 && $value['fp_top_depart_id'] != $head){
+                if(! in_array(RoleMenuButton::special_nine,$special_button)) $value['customer_from_title'] = "";
+            }
+        }
+    }
+
     public function countData($column = "", $data, $user){
         if(empty($column) || empty($data['from_wx'])) return [0, 0];
 

+ 5 - 1
app/Service/EmployeeService.php

@@ -692,7 +692,11 @@ class EmployeeService extends Service
         $model = new Depart();
         $model->where('id',$data['id'])->update($update);
 
-        return [true,'保存成功!'];
+        Storehouse::where('del_time',0)
+            ->where('top_depart_id',$data['id'])
+            ->update(['title' => $update['title']]);
+
+        return [true, ''];
     }
 
     /**

+ 199 - 5
app/Service/ExportFileService.php

@@ -46,6 +46,8 @@ class ExportFileService extends Service
     const type_eight = 8;
     const type_nine = 9;
     const type_ten = 10;
+    const type_eve = 11;
+    const type_twl = 12;
 
     //导出文件方法
     protected static $fuc = [
@@ -59,6 +61,8 @@ class ExportFileService extends Service
         self::type_eight => 'eight',
         self::type_nine => 'nine',
         self::type_ten => 'ten',
+        self::type_eve => 'eve',
+        self::type_twl => 'twl',
     ];
 
     protected static $fuc_name = [
@@ -72,6 +76,8 @@ class ExportFileService extends Service
         self::type_eight => '进销存报表',
         self::type_nine => '虚拟采购单',
         self::type_ten => '客户',
+        self::type_eve => '现存量',
+        self::type_twl => '库存台账',
     ];
 
     public static $filename = "";
@@ -98,7 +104,7 @@ class ExportFileService extends Service
             if($search['page_size'] > 1000) return [false,'请选择导出数据的条数每次最多1000条'];
             $id = $this->getListForSearch($data, $user);
             $data['id'] = $id;
-            unset($data['order_search']);
+            if($data['type'] != self::type_twl) unset($data['order_search']);
         }
 
         //不超时
@@ -677,7 +683,7 @@ class ExportFileService extends Service
                 foreach ($data as $value){
                     $tmp_sales = $sales_map[$value['sales_order_id']] ?? [];
                     $handover_time = $value['handover_time'] ? date("Y-m-d",$value['handover_time']) : "";
-                    $sales_order_number = $tmp_sales['order_number'];
+                    $sales_order_number = $tmp_sales['order_number'] ?? "";
                     $crt_time = empty($value['crt_time']) ? '' : date('Y-m-d',$value['crt_time']);
                     $crt_name = $emp_map[$value['crt_id']] ?? "";
                     $value['crt_name'] = $crt_name;
@@ -1159,6 +1165,74 @@ class ExportFileService extends Service
             'this_jc_money' => 0,
         ];
 
+        $service = new StatisticsService();
+        $model = $service->statisticsJcNewCommon($ergs, $user);
+        $model->whereIn('product_id', $id)
+            ->orderBy('product_id','desc')
+            ->chunk(500,function ($data) use(&$return,&$total,$user,$ergs,$service){
+                $data = $data->toArray();
+                $list['data'] = $data;
+                $list = $service->fillDataNew($list);
+                foreach ($list['data'] as $value){
+                    $return[] = [
+                        'code' => $value['code'],
+                        'title' => $value['title'],
+                        'last_jc_number' => $value['last_jc_number'],
+                        'last_jc_price' => $value['last_jc_price'],
+                        'last_jc_money' => $value['last_jc_money'],
+                        'this_in_number' => $value['this_in_number'],
+                        'this_in_price' => $value['this_in_price'],
+                        'this_in_money' => $value['this_in_money'],
+                        'this_out_number' => $value['this_out_number'],
+                        'this_out_price' => $value['this_out_price'],
+                        'this_out_money' => $value['this_out_money'],
+                        'this_jc_number' => $value['this_jc_number'],
+                        'this_jc_price' => $value['this_jc_price'],
+                        'this_jc_money' => $value['this_jc_money'],
+                    ];
+
+                    $total['last_jc_number'] += $value['last_jc_number'];
+                    $total['last_jc_money'] += $value['last_jc_money'];
+                    $total['this_in_number'] += $value['this_in_number'];
+                    $total['this_in_money'] += $value['this_in_money'];
+                    $total['this_out_number'] += $value['this_out_number'];
+                    $total['this_out_money'] += $value['this_out_money'];
+                    $total['this_jc_number'] += $value['this_jc_number'];
+                    $total['this_jc_money'] += $value['this_jc_money'];
+                }
+            });
+
+        $header = [
+            ['产品编码','产品名称','上月结存','','','本月入库','','','本月出库','','','本月结存','',''],
+            ['产品编码','产品名称','数量','单价','金额','数量','单价','金额','数量','单价','金额','数量','单价','金额'],
+        ];
+
+        $return[] = $total;
+        return $this->saveExportData($return,$header,'jc');
+    }
+
+    public function eightOld($ergs,$user){
+        $id = $ergs['id'];
+
+        // 导出数据
+        $return = [];
+        $total = [
+            'code' => "",
+            'title' => "",
+            'last_jc_number' => 0,
+            'last_jc_price' => "",
+            'last_jc_money' => 0,
+            'this_in_number' => 0,
+            'this_in_price' => "",
+            'this_in_money' => 0,
+            'this_out_number' => 0,
+            'this_out_price' => "",
+            'this_out_money' => 0,
+            'this_jc_number' => 0,
+            'this_jc_price' => "",
+            'this_jc_money' => 0,
+        ];
+
         $model = Product::ProductClear2($user,$ergs);
         $model->whereIn('id', $id)
             ->select('title','id','code','depart_id','top_depart_id','product_attribute')
@@ -1307,7 +1381,7 @@ class ExportFileService extends Service
             ->whereIn('id', $id)
             ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','pond_state','top_depart_id','fp_time','fp_top_depart_id','enter_time')
             ->orderBy('id','desc')
-            ->chunk(200,function ($data) use(&$return,$user){
+            ->chunk(200,function ($data) use(&$return,$user,$ergs){
                 $data = Collect($data)->map(function ($object) {
                     return (array)$object;
                 })->toArray();
@@ -1315,7 +1389,7 @@ class ExportFileService extends Service
 
                 //订单数据
                 $service = new CustomerService();
-                $list = $service->fillData($list,$user);
+                $list = $service->fillData($list,$ergs,$user);
 
                 foreach ($list['data'] as $value){
                     $fz = $value['fz'];
@@ -1334,7 +1408,7 @@ class ExportFileService extends Service
                         'progress_stage_title' => $value['progress_stage_title'] ?? "",
                         'address' => $value['address'] ?? "",
                         'mark' => $value['mark'] ?? "",
-                        'crt_time' => $value['crt_time'] ?? "",
+                        'enter_time' => $value['enter_time'] ?? "",
                         'crt_name' => $value['crt_name'] ?? "",
                     ];
                 }
@@ -1349,6 +1423,111 @@ class ExportFileService extends Service
         return $this->saveExportData($return,$header);
     }
 
+    public function eve($ergs,$user){
+        $id = $ergs['id'];
+
+        // 导出数据
+        $return = [];
+        $total = [
+            'code' => "",
+            'title' => "",
+            'product_category_title' => "",
+            'bar_code' => "",
+            'top_depart_title' => "",
+            'unit_title' => "",
+            'number' => 0,
+            'crt_time' => "",
+        ];
+
+        $service = new ProductInventoryService();
+        $model = $service->productInventoryListCommon($ergs, $user);
+        $model->whereIn('a.id', $id)
+            ->chunk(500,function ($data) use(&$return,&$total,$user,$ergs,$service){
+                $data = $data->toArray();
+                $list['data'] = $data;
+                $list = $service->fillListData($list);
+                foreach ($list['data'] as $value){
+                    $return[] = [
+                        'code' => $value['code'],
+                        'title' => $value['title'],
+                        'product_category_title' => $value['product_category_title'],
+                        'bar_code' => $value['bar_code'],
+                        'top_depart_title' => $value['top_depart_title'],
+                        'unit_title' => $value['unit_title'],
+                        'number' => $value['number'],
+                        'crt_time' => $value['crt_time'],
+                    ];
+
+                    $total['number'] += $value['number'];
+                }
+            });
+
+        $header = [
+            ['产品编码','产品名称','产品分类','产品条码','仓库','计量单位','现存数量','入库时间'],
+        ];
+
+        $return[] = $total;
+        return $this->saveExportData($return,$header);
+    }
+
+    public function twl($ergs,$user){
+        $id = $ergs['id'];
+        $ergs['crt_time'] = $ergs['order_search']['crt_time'] ?? [];
+        $return_time = $this->changeDateToTimeStampAboutRange($ergs['crt_time']);
+
+        // 导出数据
+        $return = [];
+        $total = [
+            'code' => "",
+            'title' => "",
+            'product_category_title' => "",
+            'unit_title' => "",
+            'crt_time' => "",
+            'order_number' => "",
+            'order_name' => "",
+            'start_number' => 0,
+            'in_number' => 0,
+            'out_number' => 0,
+            'end_number' => 0,
+        ];
+
+        $service = new ProductInventoryService();
+        $model = $service->productInventoryStockListCommon($ergs, $user);
+        $model->whereIn('a.id', $id)
+            ->chunk(500,function ($data) use(&$return,&$total,$user,$ergs,$service,$return_time){
+                $data = $data->toArray();
+                $list = $data;
+                $list = $service->fillData($list,$return_time);
+                foreach ($list as $value){
+                    $return[] = [
+                        'code' => $value['code'],
+                        'title' => $value['title'],
+                        'product_category_title' => $value['product_category_title'],
+                        'unit_title' => $value['unit_title'],
+                        'crt_time' => $value['crt_time'],
+                        'order_number' => $value['order_number'],
+                        'order_name' => $value['order_name'],
+                        'start_number' => $value['start_number'],
+                        'in_number' => $value['in_number'],
+                        'out_number' => $value['out_number'],
+                        'end_number' => $value['end_number'],
+                    ];
+
+                    $total['start_number'] += $value['start_number'];
+                    $total['in_number'] += $value['in_number'];
+                    $total['out_number'] += $value['out_number'];
+                    $total['end_number'] += $value['end_number'];
+                }
+            });
+
+        $header = [
+            ['产品编码','产品名称','产品分类','计量单位','流水日期','单据编码','单据类型','期初数量','本期收入','本期发出','期末结存'],
+        ];
+
+        $return[] = $total;
+        return $this->saveExportData($return,$header);
+    }
+
     public function getListForSearch($ergs, $user){
         $data = $ergs['order_search'];
         $id = [];
@@ -1382,6 +1561,11 @@ class ExportFileService extends Service
             $model = $service->invoiceCommon($data, $user, ['id']);
             $return = $this->limitData($model,'',$data);
             $id = array_column($return,'id');
+        }elseif ($ergs['type'] == self::type_eight){
+            $service = new StatisticsService();
+            $model = $service->statisticsJcNewCommon($data, $user, ['product_id as id']);
+            $return = $this->limitData($model,'',$data);
+            $id = array_column($return,'id');
         }elseif ($ergs['type'] == self::type_nine){
             $service = new PurchaseOrderSpecialService();
             $model = $service->purchaseCommon($data, $user, ['id']);
@@ -1392,6 +1576,16 @@ class ExportFileService extends Service
             $model = $service->customerCommonSearch($data, $user, ['id']);
             $return = $this->limitData($model,'',$data);
             $id = array_column($return,'id');
+        }elseif ($ergs['type'] == self::type_eve){
+            $service = new ProductInventoryService();
+            $model = $service->productInventoryListCommon($data, $user, ['a.id']);
+            $return = $this->limitData($model,'',$data);
+            $id = array_column($return,'id');
+        }elseif ($ergs['type'] == self::type_twl){
+            $service = new ProductInventoryService();
+            $model = $service->productInventoryStockListCommon($data, $user, ['a.id']);
+            $return = $this->limitData($model,'',$data);
+            $id = array_column($return,'id');
         }
 
         return $id;

+ 6 - 0
app/Service/ImportService.php

@@ -511,6 +511,12 @@ class ImportService extends Service
             // 如果成功转换并且值大于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 H:i:s');
                 if(! strtotime($formattedDate)) return [false, '录入日期请填写正确的日期'];

+ 29 - 7
app/Service/ProductInventoryService.php

@@ -156,11 +156,15 @@ class ProductInventoryService extends Service
     }
 
     //现存量
-    public function productInventoryList($data,$user){
+    public function productInventoryListCommon($data,$user, $field = []){
+        if(empty($field)){
+            $field = ['a.product_id','a.id','a.number','b.title','b.code','b.product_category_id','b.unit','b.bar_code','a.crt_time','a.storehouse_id','a.top_depart_id'];
+        }
+
         $model = ProductInventory::ATopClear($user,$data);
         $model = $model->from('product_inventory as a')
             ->join('product as b','b.id','a.product_id')
-            ->select('a.product_id','a.id','a.number','b.title','b.code','b.product_category_id','b.unit','b.bar_code','a.crt_time','a.storehouse_id','a.top_depart_id')
+            ->select($field)
             ->orderby('a.crt_time', 'desc');
         if(! empty($data['product_name'])){
             $product_name = $data['product_name'];
@@ -186,6 +190,12 @@ class ProductInventoryService extends Service
             $model->whereBetween('a.crt_time',[$return[0],$return[1]]);
         }
 
+        return $model;
+    }
+
+    //现存量
+    public function productInventoryList($data,$user){
+        $model = $this->productInventoryListCommon($data, $user);
         $list = $this->limit($model,'',$data);
         $list = $this->fillListData($list);
 
@@ -217,20 +227,21 @@ class ProductInventoryService extends Service
     }
 
     //库存台账
-    public function productInventoryStockList($data,$user){
-        if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false,'时间必须选择'];
+    public function productInventoryStockListCommon($data,$user, $field = []){
+        if(empty($field)){
+            $field = ['a.id','a.product_id','a.number','a.order_type','a.crt_time','b.title','b.code','b.product_category_id','b.unit',DB::raw('SUM(CASE WHEN a.number < 0 THEN a.number ELSE 0 END) as out_number'),DB::raw('SUM(CASE WHEN a.number >= 0 THEN a.number ELSE 0 END) as in_number'),'a.order_number','a.crt_time','a.storehouse_id','a.top_depart_id'];
+        }
+        $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
 
         $model = InOutRecord::ATopClear($user,$data);
         $model = $model->from('in_out_record as a')
             ->where('a.del_time',0)
             ->join('product as b','b.id','a.product_id')
-            ->select('a.id','a.product_id','a.number','a.order_type','a.crt_time','b.title','b.code','b.product_category_id','b.unit',DB::raw('SUM(CASE WHEN a.number < 0 THEN a.number ELSE 0 END) as out_number'),DB::raw('SUM(CASE WHEN a.number >= 0 THEN a.number ELSE 0 END) as in_number'),'a.order_number','a.crt_time','a.storehouse_id','a.top_depart_id')
+            ->select($field)
             ->groupby('a.product_id','a.top_depart_id','a.order_number')
             ->orderBy('a.crt_time','asc');
 
-        $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
         $model->whereBetween('a.crt_time',[$return[0],$return[1]]);
-
         if(! empty($data['product_name'])){
             $product_name = $data['product_name'];
             $id = Product::where('del_time',0)
@@ -252,6 +263,17 @@ class ProductInventoryService extends Service
         if(! empty($data['code'])) $model->where('b.code', 'LIKE', '%'.$data['code'].'%');
         if(! empty($data['order_number'])) $model->where('a.order_number', 'LIKE', '%'.$data['order_number'].'%');
 
+       return $model;
+    }
+
+    //库存台账
+    public function productInventoryStockList($data,$user){
+        if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false,'时间必须选择'];
+        $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
+        $day = $this->returnDays($return);
+        if($day > 31) return [false, '库存台账查询时间仅支持范围区间在一月(31天)内'];
+
+        $model = $this->productInventoryStockListCommon($data, $user);
         $list = $model->get()->toArray();
         $list = $this->fillData($list, $return);
 

+ 15 - 6
app/Service/ProductService.php

@@ -594,10 +594,14 @@ class ProductService extends Service
         return [true, $customer];
     }
 
-    public function productListIndex($data,$user){
+    public function productCommon($data,$user, $field = []){
+        if(empty($field)){
+            $field = ['title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','state','crt_id','crt_time','mark','depart_id','top_depart_id','install_time','product_attribute','is_use','build_fee','item_code'];
+        }
+
         $model = Product::ProductClear($user,$data);
         $model = $model->where('del_time',0)
-            ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','state','crt_id','crt_time','mark','depart_id','top_depart_id','install_time','product_attribute','is_use','build_fee','item_code')
+            ->select($field)
             ->orderby('product_attribute', 'desc')
             ->orderby('id', 'desc');
 
@@ -643,10 +647,7 @@ class ProductService extends Service
         }
         if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
 
-        $list = $this->limit($model,'',$data);
-        $list = $this->fillData($list,$user,$data);
-
-        return [true, $list];
+        return $model;
     }
 
     /**
@@ -655,6 +656,14 @@ class ProductService extends Service
      * @param $user
      * @return array
      */
+    public function productListIndex($data,$user){
+        $model = $this->productCommon($data, $user);
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillData($list,$user,$data);
+
+        return [true, $list];
+    }
+
     public function productList($data,$user){
         $model = Product::ProductClear($user,$data);
         $model = $model->where('del_time',0)

+ 16 - 0
app/Service/Service.php

@@ -570,4 +570,20 @@ class Service
         // 如果没有找到给定ID的节点,返回null或抛出异常
         return 0;
     }
+
+    function returnDays($time = [], $is_mirco_time = false){
+
+        // 示例时间戳
+        $timestamp1 = $time[0];
+        $timestamp2 = $time[1];
+
+        // 计算时间差
+        $difference = abs($timestamp2 - $timestamp1);
+
+        // 将时间差转换为天数
+        $days = floor($difference / (60 * 60 * 24));
+        if($is_mirco_time) $days = $days / 1000;
+
+        return $days;
+    }
 }

+ 118 - 1
app/Service/StatisticsService.php

@@ -20,6 +20,7 @@ use App\Model\SalesOrder;
 use App\Model\SalesOrderProductInfo;
 use App\Model\SeeRange;
 use App\Model\Setting;
+use Carbon\Carbon;
 use Illuminate\Support\Facades\DB;
 
 class StatisticsService extends Service
@@ -942,7 +943,7 @@ class StatisticsService extends Service
     public function getLastMonthJc($product = [], $user = [], $search = []){
         // 如果存在上月结存数据则使用这个
         $top_depart_id = 0;
-        if(empty($search['top_depart_id'])) $top_depart_id = $search['top_depart_id'];
+        if(! empty($search['top_depart_id'])) $top_depart_id = $search['top_depart_id'];
 
         $startStamp = strtotime(date("Y-m-01 00:00:00"));
         $result = LastJc::where('del_time',0)
@@ -1054,6 +1055,122 @@ class StatisticsService extends Service
         return $purchase_product_array;
     }
 
+    //新的进销存统计通用搜索底层抽象
+    public function statisticsJcNewCommonOrigin($data,$user,$field = []){
+        $startStamp = strtotime(date("Y-m-01 00:00:00"));
+
+        //-------- 结存数量汇总 ---------- //
+        //上月结存
+        $last_month_stock = "SUM(CASE WHEN crt_time < $startStamp THEN number ELSE 0 END)";
+        //本月入库
+        $this_month_in = "SUM(CASE WHEN crt_time >= $startStamp AND number > 0 THEN number ELSE 0 END)";
+        //本月出库
+        $this_month_out = "SUM(CASE WHEN crt_time >= $startStamp AND number < 0 THEN number ELSE 0 END)";
+        //本月结存
+        $this_month_stock = "($last_month_stock + $this_month_in + $this_month_out)";
+        //-------- 结存数量汇总 ---------- //
+
+        //-------- 结存金额汇总 ---------- //
+        //上月结存
+        $last_month_stock_m = "ROUND(SUM(CASE WHEN crt_time < $startStamp THEN (number * price) ELSE 0 END), 2)";
+        //本月入库
+        $this_month_in_m = "ROUND(SUM(CASE WHEN crt_time >= $startStamp AND number > 0 THEN (number * price) ELSE 0 END), 2)";
+        //本月出库
+        $this_month_out_m = "ROUND(SUM(CASE WHEN crt_time >= $startStamp AND number < 0 THEN (number * price) ELSE 0 END), 2)";
+        //-------- 结存金额汇总 ---------- //
+
+        if(empty($field)){
+            $field = ['product_id as id',
+                DB::raw("$last_month_stock as last_jc_number"),
+                DB::raw("$last_month_stock_m as last_jc_money"),
+                DB::raw("$this_month_in as this_in_number"),
+                DB::raw("$this_month_in_m as this_in_money"),
+                DB::raw("$this_month_out as this_out_number"),
+                DB::raw("$this_month_out_m as this_out_money"),
+//                DB::raw("$this_month_stock as this_jc_number")];
+            ];
+        }
+
+        $model = InOutRecord::TopClear($user,$data);
+        $model = $model->where('del_time',0)
+            ->select($field)
+            ->groupBy('product_id')
+            ->havingRaw("$last_month_stock != 0 OR $this_month_in != 0 OR $this_month_out != 0 OR $this_month_stock != 0");
+
+        return $model;
+    }
+
+    //新的进销存统计通用搜索
+    public function statisticsJcNewCommon($data,$user, $field = []){
+        $model = $this->statisticsJcNewCommonOrigin($data, $user, $field);
+
+        if(! empty($data['product_name']) || ! empty($data['product_category_name']) || ! empty($data['code'])) {
+            $id = $this->searchForProduct($data, $user);
+            $model->whereIn('product_id', $id);
+        }
+
+        return $model;
+    }
+
+    //新的进销存统计
+    public function statisticsJcNew($data,$user){
+        $model = $this->statisticsJcNewCommon($data, $user);
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillDataNew($list);
+
+        return [true, $list];
+    }
+
+    public function fillDataNew($data){
+        if(empty($data['data'])) return $data;
+
+        //产品
+        $map = (new ProductService())->getProductDetail(array_column($data['data'],'id'));
+
+        foreach ($data['data'] as $key => $value){
+            //上月结存
+            $last_jc_price = floatval($value['last_jc_number']) ? bcdiv($value['last_jc_money'],$value['last_jc_number'],2) : 0;//结存单价
+            $data['data'][$key]['last_jc_price'] = $last_jc_price;
+
+            //本月入库
+            $this_in_price = floatval($value['this_in_number']) ? bcdiv($value['this_in_money'],$value['this_in_number'],2) : 0;//本月入单价
+            $data['data'][$key]['this_in_price'] = $this_in_price;
+
+            //本月出库
+            //单价= (上月结存金额+本月入库金额) /上月结存数量+本月入库数量
+            $amount = bcadd($value['last_jc_money'], $value['this_in_money'],2);
+            $number = bcadd($value['last_jc_number'], $value['this_in_number'],2);
+            $this_out_price = floatval($number) ? bcdiv($amount,$number,2) : 0;//本月出单价
+            $data['data'][$key]['this_out_price'] = $this_out_price;
+
+            //本月结存
+            //本月结存 数量/金额=本月入库+上月结存-本月出库
+            $this_jc_money = bcsub(bcadd($value['this_in_money'],$value['last_jc_money'],2),$value['this_out_money']);
+            $this_jc_number = bcsub(bcadd($value['this_in_number'],$value['last_jc_number'],2),$value['this_out_number']);
+            $this_jc_price = floatval($this_jc_number) ? bcdiv($this_jc_money,$this_jc_number,2) : 0;//本月结存单价
+            $data['data'][$key]['this_jc_money'] = $this_jc_money;
+            $data['data'][$key]['this_jc_number'] = $this_jc_number;
+            $data['data'][$key]['this_jc_price'] = $this_jc_price;
+
+            //产品
+            $tmp = $map[$value['id']] ?? [];
+            $data['data'][$key]['title'] = $tmp['title'] ?? "";
+            $data['data'][$key]['code'] = $tmp['code'] ?? "";
+        }
+
+        return $data;
+    }
+
+    public function searchForProduct($data, $user){
+        $search = [];
+        if(! empty($data['product_name'])) $search['title'] = $data['product_name'];
+        if(! empty($data['product_category_name'])) $search['product_category'] = $data['product_category_name'];
+        if(! empty($data['code'])) $search['code'] = $data['code'];
+        $model = (new ProductService())->productCommon($search, $user, ['id']);
+        $product = $model->get()->toArray();
+
+        return array_column($product,'id');
+    }
 
     //--------------------------脚本
     public function inoutrecord(){return;

+ 4 - 0
config/header/45.php

@@ -19,6 +19,10 @@ return [
         'value' => '采购人',
     ],
     [
+        'key' => 'storehouse_title',
+        'value' => '采购分社',
+    ],
+    [
         'key' => 'supplier_title',
         'value' => '供应商',
     ],

+ 7 - 0
config/specialButton.php

@@ -65,5 +65,12 @@ return [
         "func" => "special_operation_out",
         "menu_id" => 43,
     ],
+    [
+        "id" => \App\Model\RoleMenuButton::special_nine,
+        "title" => "客户来源可见设置",
+        "sort" => -9,
+        "func" => "special_show_customer_from",
+        "menu_id" => 16,
+    ],
 ];