chenqp 7 ماه پیش
والد
کامیت
c8d9777c04
4فایلهای تغییر یافته به همراه254 افزوده شده و 4 حذف شده
  1. 14 0
      app/Model/LastJc.php
  2. 100 1
      app/Service/ImportService.php
  3. 111 3
      app/Service/StatisticsService.php
  4. 29 0
      config/excel/lastJc.php

+ 14 - 0
app/Model/LastJc.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class LastJc extends Model
+{
+    protected $guarded = [];
+    protected $table = "last_jc"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 100 - 1
app/Service/ImportService.php

@@ -10,12 +10,14 @@ use App\Model\Customer;
 use App\Model\CustomerInfo;
 use App\Model\Depart;
 use App\Model\Employee;
+use App\Model\LastJc;
 use App\Model\Product;
 use App\Model\ProductCategory;
 use App\Model\ProductPriceDetail;
 use App\Model\SalesOrder;
 use App\Model\SalesOrderInfo;
 use App\Model\SalesOrderProductInfo;
+use App\Model\Storehouse;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use Maatwebsite\Excel\Facades\Excel;
@@ -28,7 +30,8 @@ class ImportService extends Service
         'product', //产品
         'customer', //客户
         'salesOnline', //线上订单
-        'btOnline' //补贴订单
+        'btOnline', //补贴订单
+        'lastJc', //上月结存
     ];
 
     //写活的导入------------------- 暂时不好用
@@ -204,6 +207,17 @@ class ImportService extends Service
         return [true, $config_array,$filename];
     }
 
+    private function lastJc($data,$user){
+        //生成下载文件
+        $filename =  "上月结存数据更新模板_" . time() . '.' . 'xlsx';
+
+        //获取配置文件
+        $config = "excel.lastJc";
+        $config_array = config($config) ?? [];
+        if(empty($config_array)) return [false, '配置文件不存在',''];
+        return [true, $config_array,$filename];
+    }
+
     //导入入口
     public function importAll($data,$user){
 //        //不超时
@@ -1175,5 +1189,90 @@ class ImportService extends Service
 
         return [true, ''];
     }
+
+    public function lastJcImport($array, $user){
+        $head = $user['head']['id'] ?? 0;
+
+        // 去除表头
+        unset($array[0]);
+        if(empty($array)) return [false, '导入数据不能为空'];
+
+        $shop_name = $product_code = [];
+        foreach ($array as $key => $value){
+            $rowData = array_filter($value);
+            if (empty($rowData)) {
+                unset($array[$key]);
+            } elseif(empty($value[0]) || empty($value[1])) {
+                return [false, '带*号的字段项必填'];
+            }else{
+                foreach ($value as $k => $v){
+                    $value[$k] = trim($v);
+                }
+                if(! is_numeric($value[2])) return [false, '* 上月结存数量请填写正确的数值'];
+                if(! is_numeric($value[3])) return [false, '* 上月结存单价请填写正确的数值'];
+                if(! is_numeric($value[4])) return [false, '* 上月结存金额请填写正确的数值'];
+                if(! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
+                if(! in_array($value[1],$product_code)) $product_code[] = $value[1];
+            }
+        }
+        $model = Product::ProductClear($user,[]);
+        $product_map = $model->whereIn('code',$product_code)
+            ->where('del_time',0)
+            ->pluck('id','code')
+            ->toArray();
+
+        $depart_map = Depart::whereIn('title',$shop_name)
+            ->where('del_time',0)
+            ->pluck('id','title')
+            ->toArray();
+        $storehouse_map = Storehouse::whereIn('top_depart_id',array_values($depart_map))
+            ->where('del_time',0)
+            ->pluck('id','top_depart_id')
+            ->toArray();
+        $time = time();
+        $insert = [];
+
+        $last_month_stamp = strtotime(date('Y-m-t 23:59:59', strtotime('last month')));
+        foreach ($array as $value){
+            $depart_tmp = $depart_map[$value[0]] ?? 0;
+            if($depart_tmp <= 0) return [false, "门店:" . $value[0] . "不存在或已被删除"];
+            $product_tmp = $product_map[$value[1]] ?? 0;
+            if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
+            $storehouse_tmp = $storehouse_map[$depart_tmp] ?? 0;
+            $insert[] = [
+                'top_depart_id' => $depart_tmp,
+                'product_id' => $product_tmp,
+                'storehouse_id' => $storehouse_tmp,
+                'number' => $value[2],
+                'price' => $value[3],
+                'total' => $value[4],
+                'crt_time' => $time,
+                'time' => $last_month_stamp
+            ];
+        }
+        if(empty($insert)) return [false, '暂无更新的数据'];
+
+        try{
+            DB::beginTransaction();
+
+            //更新数据为删除
+            foreach ($insert as $value){
+                LastJc::where('del_time',0)
+                    ->where('time', $last_month_stamp)
+                    ->where('top_depart_id',$value['top_depart_id'])
+                    ->where('storehouse_id',$value['storehouse_id'])
+                    ->where('product_id',$value['product_id'])
+                    ->update(['del_time' => $time]);
+            }
+            //写入数据
+            LastJc::insert($insert);
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
+        }
+
+        return [true, ''];
+    }
 }
 

+ 111 - 3
app/Service/StatisticsService.php

@@ -7,6 +7,7 @@ use App\Model\DepartIndex;
 use App\Model\InOutRecord;
 use App\Model\InvoiceOrder;
 use App\Model\InvoiceOrderInfo;
+use App\Model\LastJc;
 use App\Model\Product;
 use App\Model\ProductCategory;
 use App\Model\PurchaseOrder;
@@ -666,17 +667,59 @@ class StatisticsService extends Service
 
     //上月结存(汇总这个月之前的出入)(库存流水)
     public function getLastMonthBalance($product = [], $user = [], $search = []){
-        $return = [];
+        //用户提交的上月结存
+        $lastData = $this->getLastMonthJc($product,$user,$search);
+
         $startStamp = strtotime(date("Y-m-01 00:00:00"));
         $model = InOutRecord::TopClear($user,$search);
         $list = $model->where('del_time',0)
             ->where('crt_time','<',$startStamp)
             ->whereIn('product_id',$product)
-            ->select('product_id','number','price')
+            ->select('product_id','number','price','top_depart_id')
             ->get()->toArray();
+        $map = [];
+        foreach ($list as $val){
+            $map[$val['product_id'] . $val['top_depart_id']] = $val;
+        }
+
+        //先结存表
         $in = $out = [];
+        foreach ($lastData as $value){
+            $key = $value['product_id'] . '|' . $value['top_depart_id'];
+            if($value['number'] >= 0){
+                if(isset($in[$key])){
+                    $number = bcadd($in[$key]['number'], $value['number'],0);
+                    $total = bcadd($in[$key]['total'], $value['total'],2);
+                    $in[$key]['number'] = $number;
+                    $in[$key]['total'] = $total;
+                }else{
+                    $in[$key] = [
+                        'number' => $value['number'],
+                        'total' => $value['total'],
+                    ];
+                }
+            }else{
+                $number = abs($value['number']);
+                $total = abs($value['total']);
+                if(isset($out[$key])){
+                    $number = bcadd($out[$key]['number'], $number,0);
+                    $total = bcadd($out[$key]['total'], $total,2);
+                    $out[$key]['number'] = $number;
+                    $out[$key]['total'] = $total;
+                }else{
+                    $out[$key] = [
+                        'number' => $number,
+                        'total' => $total,
+                    ];
+                }
+            }
+        }
+        //后库存流水
         foreach ($list as $value){
+           $key = $value['product_id'] . '|' . $value['top_depart_id'];
+
             if($value['number'] >= 0){
+                if(isset($in[$key])) continue;
                 $tmp_total = bcmul($value['number'], $value['price'], 2);
                 if(isset($in[$value['product_id']])){
                     $number = bcadd($in[$value['product_id']]['number'], $value['number'],0);
@@ -690,6 +733,7 @@ class StatisticsService extends Service
                     ];
                 }
             }else{
+                if(isset($out[$key])) continue;
                 $number = abs($value['number']);
                 $tmp_total = bcmul($number, $value['price'], 2);
                 if(isset($out[$value['product_id']])){
@@ -705,8 +749,72 @@ class StatisticsService extends Service
                 }
             }
         }
+        //两个数组组合过滤
+        $new_in = $new_out = [];
+        foreach ($in as $key => $value){
+            $tmp = explode('|', $key);
+            $product_id = $tmp[0];
+            if(isset($new_in[$product_id])){
+                $number = bcadd($new_in[$product_id]['number'], $value['number'],0);
+                $total = bcadd($new_in[$product_id]['total'], $value['total'],2);
+                $new_in[$product_id] = [
+                    'number' => $number,
+                    'total' => $total,
+                ];
+            }else{
+                $new_in[$product_id] = [
+                    'number' => $value['number'],
+                    'total' => $value['total'],
+                ];
+            }
+        }
+        foreach ($out as $key => $value){
+            $tmp = explode('|', $key);
+            $product_id = $tmp[0];
+            if(isset($new_out[$product_id])){
+                $number = bcadd($new_out[$product_id]['number'], $value['number'],0);
+                $total = bcadd($new_out[$product_id]['total'], $value['total'],2);
+                $new_out[$product_id] = [
+                    'number' => $number,
+                    'total' => $total,
+                ];
+            }else{
+                $new_out[$product_id] = [
+                    'number' => $value['number'],
+                    'total' => $value['total'],
+                ];
+            }
+        }
 
-        return [$in, $out];
+        return [$new_in, $new_out];
+    }
+
+    public function getLastMonthJc($product = [], $user = [], $search = []){
+        // 如果存在上月结存数据则使用这个
+        $top_depart_id = 0;
+        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)
+            ->whereIn('product_id',$product)
+            ->where('time','<',$startStamp)
+            ->when(! empty($top_depart_id), function ($query) use ($top_depart_id) {
+                return $query->where('top_depart_id',$top_depart_id);
+            })
+            ->select('product_id','top_depart_id','total','number','price')
+            ->orderBy('time','desc')
+            ->get()->toArray();
+        $return = [];
+        $tmp = [];
+        foreach ($result as $value){
+            $key = $value['product_id'] . $value['top_depart_id'];
+            if(in_array($key, $tmp)) continue;
+
+            $return[] = $value;
+            $tmp[] = $key;
+        }
+
+        return $return;
     }
 
     //本月入库(单据)暂时没用

+ 29 - 0
config/excel/lastJc.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * '菜单ID' => [
+ *     '字段英文名' =》 '字段中文名'
+ * ]
+ */
+
+return [
+    [
+        'key' => 'depart',
+        'value' => '* 门店名称',
+    ],
+    [
+        'key' => 'code',
+        'value' => '* 产品编码',
+    ],
+    [
+        'key' => 'number',
+        'value' => '* 上月结存数量',
+    ],
+    [
+        'key' => 'price',
+        'value' => '* 上月结存单价',
+    ],
+    [
+        'key' => 'amount',
+        'value' => '* 上月结存金额',
+    ],
+];