cqp 2 månader sedan
förälder
incheckning
18e16184c3

+ 135 - 0
app/Service/ImportService.php

@@ -15,6 +15,8 @@ use App\Model\Inventory;
 use App\Model\InventorySub;
 use App\Model\LastJc;
 use App\Model\Product;
+use App\Model\ProductAdjustment;
+use App\Model\ProductAdjustmentSub;
 use App\Model\ProductCategory;
 use App\Model\ProductInventorySet;
 use App\Model\ProductPriceDetail;
@@ -38,6 +40,7 @@ class ImportService extends Service
         'btOnline', //补贴订单
         'lastJc', //上月结存
         'inventory', // 盘点
+        'productAdjustment', // 产品金额调整单
     ];
 
     //写活的导入------------------- 暂时不好用
@@ -235,6 +238,17 @@ class ImportService extends Service
         return [true, $config_array,$filename];
     }
 
+    private function productAdjustment($data,$user){
+        //生成下载文件
+        $filename =  "产品金额调整单导入模板_" . time() . '.' . 'xlsx';
+
+        //获取配置文件
+        $config = "excel.productAdjustment";
+        $config_array = config($config) ?? [];
+        if(empty($config_array)) return [false, '配置文件不存在',''];
+        return [true, $config_array,$filename];
+    }
+
     //导入入口
     public function importAll($data,$user){
 //        //不超时
@@ -1706,5 +1720,126 @@ class ImportService extends Service
 
         return [true, ''];
     }
+
+    public function productAdjustmentImport($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]) || empty($value[2])) {
+                if($key != 1){
+                    if(empty($value[1])) return [false, '带*号的字段项必填'];
+                } else{
+                    return [false, '带*号的字段项必填'];
+                }
+            }else{
+                foreach ($value as $k => $v){
+                    $value[$k] = trim($v);
+                }
+                if($value[0] && ! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
+                if(! in_array($value[1],$product_code)) {
+                    $product_code[] = $value[1];
+                }else{
+                    return [false, '产品编号:' . $value[1] .'已存在'];
+                }
+                if(! is_numeric($value[2])) return [false, '调整金额请填写正确的数值'];
+            }
+        }
+        if(count($shop_name) != 1) return [false, '只允许单门店创建调整金额'];
+
+        //门店信息
+        $depart = Depart::whereIn('title',$shop_name)
+            ->where('del_time',0)
+            ->where('parent_id',0)
+            ->select('id','title')
+            ->first();
+        if(empty($depart)) return [false, "门店:" .$shop_name . "不存在或已被删除"];
+        $depart = $depart->toArray();
+        $top_depart_id = $depart['id'];
+
+        //门店的仓库
+        $storehouse = Storehouse::where('top_depart_id', $top_depart_id)
+            ->where('del_time',0)
+            ->select('id')
+            ->first();
+        if(empty($storehouse)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
+        $storehouse = $storehouse->toArray();
+        $storehouse_id = $storehouse['id'];
+        if(empty($storehouse_id)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
+
+        //产品
+        $model = Product::ProductClear($user,[]);
+        $product = $model->whereIn('code',$product_code)
+            ->where('del_time',0)
+            ->select('id','code')
+            ->get()->toArray();
+        $product_map = array_column($product,'id','code');
+
+        $product_submit = [];
+        foreach ($product as $value){
+            $key = $value['id'] . ',' . $storehouse_id;
+            if(! isset($product_submit[$key])) $product_submit[$key] = 0;
+        }
+        //校验是否存在产品调整
+        list($status, $msg) = (new ProductAdjustService())->issetProduct(['top_depart_id' => $top_depart_id], $product_submit);
+        if(! $status) return [false, $msg];
+
+        $time = time();
+        $sub = [];
+        foreach ($array as $value){
+            $product_tmp = $product_map[$value[1]] ?? 0;
+            if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
+
+            $sub[] = [
+                'product_id' => $product_tmp,
+                'storehouse_id' => $storehouse_id,
+                'order_number' => "",
+                'product_adjustment_id' => 0,
+                'final_amount' => $value[2],
+            ];
+        }
+        if(empty($sub)) return [false, '暂无需要写入的产品调整单数据'];
+
+        try{
+            $order_number = (new OrderNoService())->createOrderNumber(ProductAdjustment::prefix);
+
+            DB::beginTransaction();
+
+            $inventory_model = new ProductAdjustment();
+            $inventory_model->order_number = $order_number;
+            $inventory_model->storehouse_id = $storehouse_id;
+            $inventory_model->counted_id = $user['id'];
+            $inventory_model->counted_time = $time;
+            $inventory_model->depart_id = $top_depart_id;
+            $inventory_model->top_depart_id = $top_depart_id;
+            $inventory_model->crt_id = $user['id'];
+            $inventory_model->save();
+
+            foreach ($sub as $key => $value){
+                $sub[$key]['product_adjustment_id'] = $inventory_model->id;
+                $sub[$key]['order_number'] = $order_number;
+            }
+
+            //写入数据
+            ProductAdjustmentSub::insert($sub);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
+                return [false, '网络波动,请重新操作!'];
+            }
+            return [false, $exception->getMessage() . $exception->getLine() . $exception->getCode()];
+        }
+
+        return [true, ''];
+    }
 }
 

+ 1 - 1
app/Service/ProductAdjustService.php

@@ -329,7 +329,7 @@ class ProductAdjustService extends Service
             if(isset($product_submit[$key])){
                 $pro_tmp = $map[$value['product_id']] ?? [];
                 $pro_str = $pro_tmp['title'] . "( ". $pro_tmp['code'] .")";
-                return [false, "产品:" . $pro_str . "在调整单:" . $value['order_number'] . "已录入盘点"];
+                return [false, "产品:" . $pro_str . "在调整单:" . $value['order_number'] . "已录入"];
             }
         }
 

+ 8 - 4
app/Service/StatisticsService.php

@@ -1539,7 +1539,7 @@ class StatisticsService extends Service
                                     SUM(
                                         CASE 
                                             WHEN crt_time < $startStamp and number <> 0 THEN (number * price)
-                                            WHEN crt_time < $startStamp and number = 0 and order_type = ' . $order_type . ' THEN price
+                                            WHEN crt_time < $startStamp and number = 0 and order_type = '$order_type' THEN price
                                             ELSE 0 
                                         END
                                     ), 
@@ -1549,7 +1549,7 @@ class StatisticsService extends Service
                                   SUM(
                                       CASE 
                                             WHEN crt_time >= $startStamp and number > 0 THEN (number * price)
-                                            WHEN crt_time >= $startStamp and number = 0 and price >= 0 and order_type = ' . $order_type . ' THEN price
+                                            WHEN crt_time >= $startStamp and number = 0 and price >= 0 and order_type = '$order_type' THEN price
                                             ELSE 0 
                                       END
                                  ), 
@@ -1559,7 +1559,7 @@ class StatisticsService extends Service
                                   SUM(
                                       CASE 
                                             WHEN crt_time >= $startStamp and number < 0 THEN (number * price)
-                                            WHEN crt_time >= $startStamp and number = 0 and price < 0 and order_type = ' . $order_type . ' THEN price
+                                            WHEN crt_time >= $startStamp and number = 0 and price < 0 and order_type = '$order_type' THEN price
                                             ELSE 0 
                                       END
                                  ), 
@@ -1638,7 +1638,7 @@ class StatisticsService extends Service
 
             //本月结存
             //本月结存 数量/金额=本月入库+上月结存+(-本月出库)
-            $this_jc_money = bcsub(bcadd($value['this_in_money'],$value['last_jc_money'],2),$value['this_out_money'],2);
+            $this_jc_money = bcadd(bcadd($value['this_in_money'],$value['last_jc_money'],2),$value['this_out_money'],2);
             $this_jc_number = bcadd(bcadd($value['this_in_number'],$value['last_jc_number'],2),$value['this_out_number'],2);
             $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;
@@ -1649,6 +1649,10 @@ class StatisticsService extends Service
             $tmp = $map[$value['id']] ?? [];
             $data['data'][$key]['title'] = $tmp['title'] ?? "";
             $data['data'][$key]['code'] = $tmp['code'] ?? "";
+
+            //负数改为正数
+            $data['data'][$key]['this_out_number'] = abs($value['this_out_number']);
+            $data['data'][$key]['this_out_money'] = abs($value['this_out_money']);
         }
 
         return $data;

+ 21 - 0
config/excel/productAdjustment.php

@@ -0,0 +1,21 @@
+<?php
+/**
+ * '菜单ID' => [
+ *     '字段英文名' =》 '字段中文名'
+ * ]
+ */
+
+return [
+    [
+        'key' => 'depart',
+        'value' => '* 门店名称',
+    ],
+    [
+        'key' => 'code',
+        'value' => '* 产品编码',
+    ],
+    [
+        'key' => 'final_amount',
+        'value' => '* 调整金额',
+    ],
+];

+ 6 - 0
config/oa.php

@@ -551,5 +551,11 @@ return [
         'children' => [
         ]
     ],
+    [
+        'menu_id' => 58,
+        'menu_title' => '产品金额调整单',
+        'children' => [
+        ]
+    ],
 ];