cqp 2 maanden geleden
bovenliggende
commit
316ca77b6a

+ 2 - 0
app/Model/PurchaseOrder.php

@@ -52,4 +52,6 @@ class PurchaseOrder extends UseScopeBaseModel
         self::STATE_ZERO,
         self::STATE_ONE,
     ];
+
+    const usage_return = "returnExchange";
 }

+ 17 - 0
app/Model/PurchaseOrderInfoForOutBound.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class PurchaseOrderInfoForOutBound extends Model
+{
+    protected $table = "purchase_order_info_for_outbound"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+    const is_not_use = 0;
+    const is_use = 1;
+    const type_one = 1;
+    const prefix = 'CGCk';
+}

+ 27 - 2
app/Service/CheckService.php

@@ -22,6 +22,7 @@ use App\Model\ProductInventory;
 use App\Model\ProductInventorySet;
 use App\Model\PurchaseOrder;
 use App\Model\PurchaseOrderInfo;
+use App\Model\PurchaseOrderInfoForOutBound;
 use App\Model\PurchaseOrderSpecial;
 use App\Model\PurchaseOrderSpecialInfo;
 use App\Model\ReturnExchangeOrder;
@@ -1140,8 +1141,10 @@ class CheckService extends Service
     }
 
     public function recordOut($data, $order){
-        $result = OutBoundOrderInfo::where('del_time',0)
-            ->where('out_bound_id',$order['id'])
+        $result = PurchaseOrderInfoForOutBound::where('del_time',0)
+            ->where('data_id',$order['id'])
+            ->where('type',PurchaseOrderInfoForOutBound::type_one)
+            ->where('is_use',PurchaseOrderInfoForOutBound::is_not_use)
             ->get()->toArray();
         if(empty($result)) return [false,'出库单明细信息不存在或已被删除'];
 
@@ -1167,11 +1170,33 @@ class CheckService extends Service
                     'price' => $value['price'],
                 ];
             }
+
+            if(empty($value['purchase_order_id'])){
+                //没采购信息 则加入一条记录平流水
+                $insert[$key] = [
+                    'product_id' => $value['product_id'],
+                    'number' => $value['number'],
+                    'order_type' => PurchaseOrderInfoForOutBound::prefix,
+                    'order_number' => $order['order_number'],
+                    'crt_time' => $time,
+                    'storehouse_id' => $value['storehouse_id'],
+                    'depart_id' => $order['depart_id'],
+                    'top_depart_id' => $order['top_depart_id'],
+                    'price' => $value['price'],
+                ];
+            }
         }
 
         $bool = InOutRecord::insert($insert);
         if(! $bool) return [false, '流水写入失败'];
 
+        //更新采购出库占用
+        PurchaseOrderInfoForOutBound::where('del_time',0)
+            ->where('data_id',$order['id'])
+            ->where('type',PurchaseOrderInfoForOutBound::type_one)
+            ->where('is_use',PurchaseOrderInfoForOutBound::is_not_use)
+            ->update(['is_use' => PurchaseOrderInfoForOutBound::is_use]);
+
         return [true, ''];
     }
 

+ 229 - 19
app/Service/OutBoundOrderService.php

@@ -2,15 +2,18 @@
 
 namespace App\Service;
 
-use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\OrderInventoryStock;
 use App\Model\OutBoundOrder;
 use App\Model\OutBoundOrderInfo;
+use App\Model\Product;
+use App\Model\ProductInventorySet;
+use App\Model\PurchaseOrderInfoForOutBound;
 use App\Model\ReturnExchangeOrder;
 use App\Model\ReturnExchangeOrderProductInfo;
 use App\Model\SalesOrder;
 use App\Model\SalesOrderProductInfo;
+use App\Model\Setting;
 use App\Model\Storehouse;
 use Illuminate\Support\Facades\DB;
 
@@ -21,7 +24,9 @@ class OutBoundOrderService extends Service
         if(! $status) return [$status,$msg];
 
         try{
+            $time = time();
             DB::beginTransaction();
+
             $material_model = OutBoundOrder::where('order_number',$data['order_number'])->first();
             $material_model->data_id = $data['data_id'];
             $material_model->type = $data['type'];
@@ -58,6 +63,20 @@ class OutBoundOrderService extends Service
                 ProductInventoryService::changeLockNumber($user, $msg[0],$msg[1],$is_check_stock);
             }
 
+            PurchaseOrderInfoForOutBound::where('del_time',0)
+                ->where('data_id', $material_model->id)
+                ->where('type', PurchaseOrderInfoForOutBound::type_one)
+                ->update(['del_time' => $time]);
+
+            //增加采购占用
+            $purchase_return = $msg[2] ?? [];
+            if(! empty($purchase_return)){
+                foreach ($purchase_return as $key => $value){
+                    $purchase_return[$key]['data_id'] = $material_model->id;
+                }
+                PurchaseOrderInfoForOutBound::insert($purchase_return);
+            }
+
             DB::commit();
         }catch (\Throwable $e){
             DB::rollBack();
@@ -116,6 +135,15 @@ class OutBoundOrderService extends Service
                 ProductInventoryService::changeLockNumber($user,$msg[0],[]);
             }
 
+            //增加采购占用
+            $purchase_return = $msg[2] ?? [];
+            if(! empty($purchase_return)){
+                foreach ($purchase_return as $key => $value){
+                    $purchase_return[$key]['data_id'] = $material_model->id;
+                }
+                PurchaseOrderInfoForOutBound::insert($purchase_return);
+            }
+
             //单据创建时是否校验库存
             (new CheckService())->orderInventoryInsert(['order_number' => $data['order_number'], 'is_check_stock' => $data['is_check_stock']]);
 
@@ -135,7 +163,7 @@ class OutBoundOrderService extends Service
                 "opt_case" => CheckService::sixteen,
                 "menu_id" => $data['menu_id']
             ],$user);
-//            if(! $status) return [true, '保存成功,出库单确认失败,异常信息:' . $msg];
+            if(! $status) return [true, '保存成功,出库单确认失败,异常信息:' . $msg];
         }
 
         return [true, ''];
@@ -193,14 +221,19 @@ class OutBoundOrderService extends Service
         if($order['state'] > OutBoundOrder::STATE_ZERO) return [false,'请确认出库单状态,删除失败'];
 
         $product_save = $this->getSaveDetail($data['id']);
+        $time = time();
         try{
             DB::beginTransaction();
             OutBoundOrder::where('order_number',$data['order_number'])->update([
-                'del_time'=>time()
+                'del_time'=>$time
             ]);
             OutBoundOrderInfo::where('order_number',$data['order_number'])->update([
-                'del_time'=>time()
+                'del_time'=>$time
             ]);
+            PurchaseOrderInfoForOutBound::where('del_time',0)
+                ->where('data_id', $order['id'])
+                ->where('type', 1)
+                ->update(['del_time' => $time]);
 //            (new RangeService())->RangeDelete($order['id'],SeeRange::type_three);
 
             $is_check_stock = OrderInventoryStock::where('order_number', $data['order_number'])
@@ -290,6 +323,7 @@ class OutBoundOrderService extends Service
             if(empty($value['number'])) return [false, '出库产品数量不能为空'];
             $res = $this->checkNumber($value['number']);
             if(! $res) return [false,'请输入正确的产品数量'];
+
             if(! isset($value['price'])) return [false, '出库产品单价不能为空'];
             if(! isset($value['final_amount'])) return [false, '出库产品总价值不能为空'];
 
@@ -304,20 +338,27 @@ class OutBoundOrderService extends Service
 
         //剩余能发
         $id = $data['id'] ?? 0;
-        $s_product = [];
+        $purchase_return = [];
         if($data['type'] == OutBoundOrder::out_type_one)  {
-            $return = $this->getSalesProduct(['out_bound_id' => $id, 'data_id' => $data['data_id'], 'type' => $data['type']], $user);
+            list($status,$return) = $this->getSalesProduct(['out_bound_id' => $id, 'data_id' => $data['data_id'], 'type' => $data['type']], $user, $data['storehouse_id']);
+            if(! $status) return [false, $return];
             $s_product = array_column($return,'number','product_id');
-        }
 
-        //比较
-        foreach ($product_submit as $pro => $number){
-            $tmp = explode(',',$pro);
-            $p = $tmp[0];
-            if(! isset($s_product[$p])) return [false,'产品错误,合同中不存在该产品或产品出库已完成'];
-            $s_number = $s_product[$p];
+            //比较
+            foreach ($product_submit as $pro => $number){
+                $tmp = explode(',',$pro);
+                $p = $tmp[0];
+                if(! isset($s_product[$p])) return [false,'产品错误,合同中不存在该产品或产品出库已完成'];
+                $s_number = $s_product[$p];
+
+                if($number > $s_number) return [false,'出库产品数量不能超过合同产品数量'];
+            }
+
+            //校验采购单
+            list($status, $msg) = $this->checkPurchase($id,$data,$user);
+            if(! $status) return [false, $msg];
 
-            if($number > $s_number) return [false,'出库产品数量不能超过合同产品数量'];
+            $purchase_return = $msg;
         }
 
         //已保存
@@ -348,7 +389,7 @@ class OutBoundOrderService extends Service
             if($order['state'] > OutBoundOrder::STATE_ZERO) return [false, '请确认出库单状态,编辑失败'];
         }
 
-        return [true, [$product_submit, $product_save]];
+        return [true, [$product_submit, $product_save, $purchase_return]];
     }
 
     public function outBoundGetProduct($data, $user){
@@ -369,11 +410,24 @@ class OutBoundOrderService extends Service
         $return = [];
         $data_id = $data['data_id'];
         $out_bound_id = $data['out_bound_id'] ?? 0;
+        $sale = SalesOrder::where('del_time',0)
+            ->where('id', $data_id)
+            ->first();
+        if(empty($sale)) return [false, '合同不存在或已被删除'];
+        $sale = $sale->toArray();
+        //仓库ID
+        $storehouse = Storehouse::where('top_depart_id', $sale['top_depart_id'])->where('del_time',0)->value('id');
+
         $product = SalesOrderProductInfo::where('del_time',0)
             ->where('sales_order_id', $data_id)
             ->select('product_id', 'number', 'final_amount','price')
             ->get()->toArray();
-        $map = (new ProductService())->getProductDetail(array_column($product,'product_id'));
+        $product_id = array_column($product,'product_id');
+        $map = (new ProductService())->getProductDetail($product_id);
+
+        //采购单产品
+        $service = new PurchaseOrderService();
+        $purchase = $service->getPurchaseProduct($product_id, $storehouse);
 
         //合同出库产品
         $product_map = [];
@@ -421,7 +475,7 @@ class OutBoundOrderService extends Service
             $p1 = $product_map[$value['product_id']] ?? 0;
             //合同退货产品
             $p2 = $product_map2[$value['product_id']] ?? 0;
-            $number = bcsub(bcsub($value['number'], $p1), $p2);
+            $number = bcsub(bcsub($value['number'], $p1,2), $p2,2);
 
             if($p1 > 0) {
                 $state = 2;
@@ -450,16 +504,31 @@ class OutBoundOrderService extends Service
                 'code' => $tmp['code'] ?? "",
                 'size' => $tmp['size'] ?? "",
                 'unit' => $tmp['unit'] ?? "",
+                'purchase_product' => $purchase[$value['product_id']] ?? [],
             ];
         }
 
         return $return;
     }
 
-    public function getSalesProduct($data, $user){
+    public function getSalesProduct($data, $user, $storehouse_id = 0){
         $return = [];
         $data_id = $data['data_id'];
         $out_bound_id = $data['out_bound_id'] ?? 0;
+        $sale = SalesOrder::where('del_time',0)
+            ->where('id', $data_id)
+            ->first();
+        if(empty($sale)) return [false, '合同不存在或已被删除'];
+        $sale = $sale->toArray();
+
+        $top_depart_id = $user['depart_top'][0] ?? [];
+        $top_depart_id = $top_depart_id['depart_id'] ?? 0;
+        list($status, $msg) = $this->returnOrderEditErrorCommon($top_depart_id,$sale['top_depart_id']);
+        if(! $status) return [false, $msg];
+
+        $storehouse = Storehouse::where('top_depart_id', $sale['top_depart_id'])->where('del_time',0)->value('id');
+        if($storehouse_id && $storehouse_id != $storehouse) return [false, '出库仓库错误'];
+
         $product = SalesOrderProductInfo::where('del_time',0)
             ->where('sales_order_id', $data_id)
             ->select('product_id', 'number', 'final_amount','price')
@@ -526,7 +595,7 @@ class OutBoundOrderService extends Service
             ];
         }
 
-        return $return;
+        return [true, $return];
     }
 
     public function getSaveDetail($id){
@@ -547,4 +616,145 @@ class OutBoundOrderService extends Service
 
         return $product_save;
     }
+
+    public function checkPurchase($id, $data, $user){
+        //是否校验库存
+        if($id > 0){
+            $is_check_stock = OrderInventoryStock::where('order_number', $data['order_number'])
+                    ->where('del_time',0)
+                    ->value('is_check_stock') ?? 0;
+        }else{
+            $is_check_stock = $user['is_check_stock'];
+        }
+
+        $purchase_product = [];
+        foreach ($data['product'] as $value){
+            $purchase = $value['purchase_product'] ?? [];
+            if(empty($purchase)) return [false, '出库操作时,请选择出库的产品来源采购单'];
+
+            foreach ($purchase as $p_v){
+                //必须校验的内容
+                if(empty($p_v['number'])) return [false, '采购单产品数量不能为空'];
+                $res = $this->checkNumber($p_v['number']);
+                if(! $res) return [false,'请输入正确的采购单产品数量'];
+                if(empty($p_v['product_id'])) return [false, '采购单产品ID不能为空'];
+
+                if($is_check_stock != ProductInventorySet::type_two){
+                    //库存校验开启时 校验 需要采购单明细子表id
+                    if(! isset($p_v['id'])) return [false, 'ID不能为空'];
+                    if(! empty($p_v['id'])) {
+                        list($status, $msg) = $this->limitingSendRequestBackgExpire("purchaseOrderInfo" . $p_v['id']);
+                        if(! $status) return [false, '采购单产品处于出库操作中,请稍后'];
+                    }
+                    if(! isset($p_v['purchase_order_id'])) return [false, 'purchaseOrderId不能为空'];
+                    if(! isset($p_v['price'])) return [false, '采购单产品单价不能为空'];
+                    if(! isset($p_v['order_number'])) return [false, '采购单单号不能为空'];
+                }
+
+                $purchase_product[$value['product_id']] = [
+                    "product_id" => $value['product_id'],
+                    "total" => $value['number'],
+                    "from" => $purchase,
+                ];
+            }
+        }
+
+        if($is_check_stock != ProductInventorySet::type_two){
+            //库存校验开启时 校验 需要采购单产品数量
+            list($status, $msg) = (new PurchaseOrderService())->checkPurchaseProductByPurchaseInfoID($id, $purchase_product);
+            if(! $status) return [false, $msg];
+        }
+
+        //返回产品对应数据
+        $return = $this->returnProductData($data['product'], $user, $data['storehouse_id']);
+
+        return [true, $return];
+    }
+
+    public function returnProductData($product, $user, $storehouse_id){
+        $top_depart_id = $user['depart_top'][0] ?? [];
+        $my_top_depart_id = $top_depart_id['id'] ?? 0;
+        $basic_type_id = $top_depart_id['basic_type_id'] ?? 0;
+
+        //产品id
+        $product_id = array_column($product,'product_id');
+
+        //获取产品的采购成本 如果设置了分社价格
+        $service = new ProductService();
+        $detail_map = [];
+        if($basic_type_id) $detail_map = $service->getProductPrice($product_id,1, $basic_type_id);
+
+        //产品的成本 如果是 总社或者杭州旗舰店用cost 其它分社用retail_price
+        $product_array = Product::whereIn('id',$product_id)
+            ->where('del_time',0)
+            ->select('id','retail_price','cost')
+            ->get()->toArray();
+        $product_map = array_column($product_array,'retail_price','id');
+        $product_map_2 = array_column($product_array,'cost','id');
+
+        //需要所有数据的门店
+        $setting = Setting::where('setting_name','bt_top_depart_id')->first();
+        $bt_top_depart_id = $setting['setting_value'] ?? [];
+        $bt_top_depart_id = json_decode($bt_top_depart_id,true);
+
+        //产品使用价格
+        $product_use = [];
+        if(! empty($basic_type_id)){
+            foreach ($detail_map as $key => $value){
+                if(isset($value[$basic_type_id]) && ! empty($value[$basic_type_id]['price'])) {
+                    $product_use[$key] = $value[$basic_type_id]['price'];
+                }else{
+                    if(in_array($my_top_depart_id, $bt_top_depart_id)){
+                        $product_use[$key] = $product_map_2[$key];
+                    }else{
+                        $product_use[$key] = $product_map[$key];
+                    }
+                }
+            }
+        }else{
+            if(in_array($my_top_depart_id, $bt_top_depart_id)){
+                $product_use = $product_map_2;
+            }else{
+                $product_use = $product_map;
+            }
+        }
+
+        $return = [];
+        foreach ($product as $value){
+            $product_id = $value['product_id'];
+//            $total = $value['number'];
+            $purchase = $value['purchase_product'] ?? [];
+            $use_price = $product_use[$product_id] ?? 0;
+
+            foreach ($purchase as $p_v){
+                if(empty($p_v['id']) || empty($p_v['purchase_order_id'])){
+                    $return[] = [
+                        'purchase_order_id' => 0,
+                        'purchase_order_info_id' => 0,
+                        'order_number' => "",
+                        'product_id' => $p_v['product_id'],
+                        'number' => $p_v['number'],
+                        'price' => $use_price,
+                        'storehouse_id' => $storehouse_id,
+                        'type' => PurchaseOrderInfoForOutBound::type_one,
+                        'is_use' => PurchaseOrderInfoForOutBound::is_not_use,
+                    ];
+                }else{
+                    $return[] = [
+                        'purchase_order_id' => $p_v['purchase_order_id'],
+                        'purchase_order_info_id' => $p_v['id'],
+                        'order_number' => $p_v['order_number'],
+                        'product_id' => $p_v['product_id'],
+                        'number' => $p_v['number'],
+                        'price' => $p_v['price'],
+                        'storehouse_id' => $storehouse_id,
+                        'type' => PurchaseOrderInfoForOutBound::type_one,
+                        'is_use' => PurchaseOrderInfoForOutBound::is_not_use,
+                    ];
+                }
+            }
+        }
+
+        return $return;
+    }
 }

+ 10 - 0
app/Service/ProductService.php

@@ -710,6 +710,16 @@ class ProductService extends Service
             $model->where('crt_time','<=',$return[1]);
         }
         if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
+        if(isset($data['is_head'])){
+            if($data['is_head'] == 1){
+                $head = $user['head']['id'] ?? 0;
+                $model->where('top_depart_id', $head);
+            }else{
+                $top_depart_id = $user['depart_top'][0] ?? [];
+                $top_depart_id = $top_depart_id['depart_id'] ?? 0;
+                $model->where('top_depart_id', $top_depart_id);
+            }
+        }
 
         $list = $this->limit($model,'',$data);
         $list = $this->fillData($list,$user,$data);

+ 321 - 16
app/Service/PurchaseOrderService.php

@@ -12,7 +12,9 @@ use App\Model\PaymentReceiptInfo;
 use App\Model\PurchaseOrder;
 use App\Model\PurchaseOrderFile;
 use App\Model\PurchaseOrderInfo;
+use App\Model\PurchaseOrderInfoForOutBound;
 use App\Model\ReturnExchangeOrder;
+use App\Model\ReturnExchangeOrderProductInfo;
 use App\Model\RoleMenuButton;
 use App\Model\SalesOrder;
 use App\Model\SeeRange;
@@ -573,22 +575,8 @@ class PurchaseOrderService extends Service
         $supplier_map = array_column($supplier,null,'id');
         $product = [];
         if(! empty($ergs['product'])){
-            $sales_p_info = PurchaseOrderInfo::where('del_time',0)
-                ->whereIn('purchase_order_id',array_column($data['data'],'id'))
-                ->get()->toArray();
-            $basic_price = BasicType::whereIn('id',array_unique(array_column($sales_p_info,'basic_type_id')))->pluck('title','id')->toArray();
-            $map = (new ProductService())->getProductDetail(array_column($sales_p_info,'product_id'));
-
-            foreach ($sales_p_info as $value){
-                $tmp = $map[$value['product_id']] ?? [];
-                $value['title'] = $tmp['title'] ?? "";
-                $value['code'] = $tmp['code'] ?? "";
-                $value['size'] = $tmp['size'] ?? "";
-                $value['unit'] = $tmp['unit'] ?? "";
-                $value['bar_code'] = $tmp['bar_code'] ?? "";
-                $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
-                $product[$value['purchase_order_id']][] = $value;
-            }
+            $purchase_order_id = array_column($data['data'],'id');
+            $product = $this->getPurchaseProductForList($purchase_order_id, $ergs);
         }
 
         //分社订货合同
@@ -821,4 +809,321 @@ class PurchaseOrderService extends Service
 
         return [true, ''];
     }
+
+    //获取采购单产品数量 含退货 以及 出库 后最终产品数量 (通过产品以及仓库)
+    public function getPurchaseProduct($product = [], $storehouse = 0){
+        if(empty($product) || empty($storehouse)) return [];
+
+        //采购单
+        $purchase_id = PurchaseOrder::where('del_time',0)
+            ->where('state',PurchaseOrder::STATE_Four)
+            ->where('storehouse_id',$storehouse)
+            ->select('id')
+            ->get()->toArray();
+        $purchase_id = array_column($purchase_id,'id');
+
+        //采购单产品数量
+        $purchase_product = PurchaseOrderInfo::where('del_time',0)
+            ->whereIn('purchase_order_id',$purchase_id)
+            ->whereIn('product_id',$product)
+//            ->whereColumn('number','>','outbound_number')
+            ->select('id','number','price','purchase_order_id','order_number','product_id')
+            ->orderBy('id','asc')
+            ->get()->toArray();
+//        $map = (new ProductService())->getProductDetail(array_column($purchase_product,'product_id'));
+
+        //采购单已出库产品(包含占用)
+        $purchase_product_outbound = PurchaseOrderInfoForOutBound::where('del_time',0)
+            ->whereIn('purchase_order_id',array_column($purchase_product,'purchase_order_id'))
+            ->where('type',PurchaseOrderInfoForOutBound::type_one)
+            ->select('purchase_order_id','product_id','number')
+            ->get()->toArray();
+        $purchase_product_outbound_map = [];
+        foreach ($purchase_product_outbound as $value){
+            $key = $value['purchase_order_id'] . $value['product_id'];
+            if(isset($purchase_product_outbound_map[$key])){
+                $total_number = bcadd($purchase_product_outbound_map[$key], $value['number'],2);
+                $purchase_product_outbound_map[$key] = $total_number;
+            }else{
+                $purchase_product_outbound_map[$key] = $value['number'];
+            }
+        }
+
+        //退货
+        $return = ReturnExchangeOrder::where('del_time',0)
+            ->whereIn('data_id',$purchase_id)
+            ->where('type',ReturnExchangeOrder::Order_type2)
+            ->select('id','data_id')
+            ->get()->toArray();
+        $return_map = [];
+        foreach ($return as $value){
+            $return_map[$value['id']] = $value['data_id'];
+        }
+
+        $return_id = array_column($return,'id');
+        $product_save = [];
+        $sub = ReturnExchangeOrderProductInfo::where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
+            ->where('del_time',0)
+            ->whereIn('return_exchange_id',$return_id)
+            ->get()->toArray();
+        foreach ($sub as $value){
+            if(isset($return_map[$value['return_exchange_id']])){
+                $purchase_tmp_id = $return_map[$value['return_exchange_id']];
+
+                $key = $purchase_tmp_id . $value['product_id'];
+                if(isset($product_save[$key])){
+                    $tmp = bcadd($product_save[$key], $value['number'],2);
+                    $product_save[$key] = $tmp;
+                }else{
+                    $product_save[$key] = $value['number'];
+                }
+            }
+        }
+
+        $return = [];
+        foreach ($purchase_product as $value){
+            $key = $value['purchase_order_id'] . $value['product_id'];
+            $return_number_tmp = $product_save[$key] ?? 0; // 退货产品数量
+            $purchase_number_lock_tmp = $purchase_product_outbound_map[$key] ?? 0; //采购单产品锁定未释放的数量
+
+            //最终采购单下某个产品的实际数量
+//            $number = bcsub($value['number'], $value['outbound_number'],2);
+            $number = bcsub($value['number'], $purchase_number_lock_tmp,2);
+            $number = bcsub($number,$return_number_tmp,2);
+
+            if($number <= 0) continue;
+//            $tmp = $map[$value['product_id']] ?? [];
+
+            $return[$value['product_id']][] = [
+                'id' => $value['id'],
+                'purchase_order_id' => $value['purchase_order_id'],
+                'number' => $number,
+                'price' => $value['price'],
+                'order_number' => $value['order_number'],
+                'product_id' => $value['product_id'],
+//                'title' => $tmp['title'],
+//                'code' => $tmp['code'],
+//                'size' => $tmp['size'],
+            ];
+        }
+
+        return $return;
+    }
+
+    //校验采购单产品数量 含退货 以及 出库 后最终产品数量 (通过采购单明细id)
+    public function checkPurchaseProductByPurchaseInfoID($id, $product){
+        if(empty($product)) return [false, "出库产品校验产品不能为空"];
+
+        $purchase_id = [];
+        foreach ($product as $value){
+            foreach ($value['from'] as $f_v){
+                if(! in_array($f_v['purchase_order_id'], $purchase_id)) $purchase_id[] = $f_v['purchase_order_id'];
+            }
+        }
+        if(empty($purchase_id)) return [false, "出库产品来源采购单数据不能为空"];
+        $product_id = array_keys($product);
+
+        //采购单产品数量
+        $purchase_product = PurchaseOrderInfo::where('del_time',0)
+            ->whereIn('purchase_order_id',$purchase_id)
+            ->whereIn('product_id',$product_id)
+//            ->whereColumn('number','>','outbound_number')
+            ->select('id','number','price','purchase_order_id','order_number','product_id')
+            ->orderBy('id','asc')
+            ->get()->toArray();
+        $purchase_product_map = [];
+        foreach ($purchase_product as $value){
+            $key = $value['purchase_order_id'] . $value['product_id'];
+            if(isset($purchase_product_map[$key])){
+                $total_number = bcadd($purchase_product_map[$key], $value['number'],2);
+                $purchase_product_map[$key] = $total_number;
+            }else{
+                $purchase_product_map[$key] = $value['number'];
+            }
+        }
+
+        //采购单占用未释放数据
+        $purchase_product_outbound = PurchaseOrderInfoForOutBound::where('del_time',0)
+            ->whereIn('purchase_order_id',array_column($purchase_product,'purchase_order_id'))
+//            ->where('is_use',PurchaseOrderInfoForOutBound::is_not_use)
+            ->where('type',PurchaseOrderInfoForOutBound::type_one)
+            ->when(! empty($id), function ($query) use ($id) {
+                return $query->where('data_id', '<>', $id);
+            })
+            ->select('purchase_order_id','product_id','number')
+            ->get()->toArray();
+        $purchase_product_outbound_map = [];
+        foreach ($purchase_product_outbound as $value){
+            $key = $value['purchase_order_id'] . $value['product_id'];
+            if(isset($purchase_product_outbound_map[$key])){
+                $total_number = bcadd($purchase_product_outbound_map[$key], $value['number'],2);
+                $purchase_product_outbound_map[$key] = $total_number;
+            }else{
+                $purchase_product_outbound_map[$key] = $value['number'];
+            }
+        }
+
+        //退货
+        $return = ReturnExchangeOrder::where('del_time',0)
+            ->whereIn('data_id',$purchase_id)
+            ->where('type',ReturnExchangeOrder::Order_type2)
+            ->select('id','data_id')
+            ->get()->toArray();
+        $return_map = [];
+        foreach ($return as $value){
+            $return_map[$value['id']] = $value['data_id'];
+        }
+
+        $return_id = array_column($return,'id');
+        $product_save = [];
+        $sub = ReturnExchangeOrderProductInfo::where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
+            ->where('del_time',0)
+            ->whereIn('return_exchange_id',$return_id)
+            ->get()->toArray();
+        foreach ($sub as $value){
+            if(isset($return_map[$value['return_exchange_id']])){
+                $purchase_tmp_id = $return_map[$value['return_exchange_id']];
+
+                $key = $purchase_tmp_id . $value['product_id'];
+                if(isset($product_save[$key])){
+                    $tmp = bcadd($product_save[$key], $value['number'],2);
+                    $product_save[$key] = $tmp;
+                }else{
+                    $product_save[$key] = $value['number'];
+                }
+            }
+        }
+
+        //校验
+        foreach ($product as $value){
+            $product_id = $value['product_id'];
+            $total = $value['total'];
+            $num_tmp = 0;
+            foreach ($value['from'] as $f_v){
+                if(! $f_v['id'] || ! $f_v['purchase_order_id']) continue;
+
+                $key = $f_v['purchase_order_id'] . $product_id;
+                $total_tmp = $purchase_product_map[$key] ?? 0; // 采购单产品总数
+                $return_number_tmp = $product_save[$key] ?? 0; // 退货产品数量
+                $purchase_number_lock_tmp = $purchase_product_outbound_map[$key] ?? 0; // 采购单产品锁定未释放的数量
+
+                //最终采购单下某个产品的实际数量
+//                $number = bcsub($value['number'], $value['outbound_number'],2);
+                $number = bcsub($total_tmp, $purchase_number_lock_tmp,2);
+                $number = bcsub($number,$return_number_tmp,2);
+
+                //比较
+                if($number < $f_v['number']) return [false, "采购单:" . $f_v['order_number'] . "实际可以出库的数量" . $number];
+                $num_tmp = bcadd($num_tmp, $f_v['number'],2);
+            }
+
+            if(floatval($total) != floatval($num_tmp)) return [false, '出库数量需等于采购单选择产品数量'];
+        }
+
+        return [true, ''];
+    }
+
+    //获取采购单产品数量 含退货 以及 出库 后最终产品数量 (在采购单列表页上,通过采购单id)
+    public function getPurchaseProductForList($purchase_id, $search){
+        if(empty($purchase_id)) return [];
+
+        //采购单产品数量
+        $purchase_product = PurchaseOrderInfo::where('del_time',0)
+            ->whereIn('purchase_order_id',$purchase_id)
+            ->get()->toArray();
+        $basic_price = BasicType::whereIn('id',array_unique(array_column($purchase_product,'basic_type_id')))
+            ->pluck('title','id')
+            ->toArray();
+        $map = (new ProductService())->getProductDetail(array_column($purchase_product,'product_id'));
+        foreach ($purchase_product as $key => $value){
+            $tmp = $map[$value['product_id']] ?? [];
+            $purchase_product[$key]['title'] = $tmp['title'] ?? "";
+            $purchase_product[$key]['code'] = $tmp['code'] ?? "";
+            $purchase_product[$key]['size'] = $tmp['size'] ?? "";
+            $purchase_product[$key]['unit'] = $tmp['unit'] ?? "";
+            $purchase_product[$key]['bar_code'] = $tmp['bar_code'] ?? "";
+            $purchase_product[$key]['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
+        }
+
+        if(! isset($search['product_usage'])) {
+            return $this->clearProduct($purchase_product);
+        }else{
+            if($search['product_usage'] != PurchaseOrder::usage_return) return $this->clearProduct($purchase_product);
+        }
+
+        //采购单已出库产品(包含占用)
+        $purchase_product_outbound = PurchaseOrderInfoForOutBound::where('del_time',0)
+            ->whereIn('purchase_order_id',array_column($purchase_product,'purchase_order_id'))
+            ->where('type',PurchaseOrderInfoForOutBound::type_one)
+            ->select('purchase_order_id','product_id','number')
+            ->get()->toArray();
+        $purchase_product_outbound_map = [];
+        foreach ($purchase_product_outbound as $value){
+            $key = $value['purchase_order_id'] . $value['product_id'];
+            if(isset($purchase_product_outbound_map[$key])){
+                $total_number = bcadd($purchase_product_outbound_map[$key], $value['number'],2);
+                $purchase_product_outbound_map[$key] = $total_number;
+            }else{
+                $purchase_product_outbound_map[$key] = $value['number'];
+            }
+        }
+
+        //采购单退货
+        $return = ReturnExchangeOrder::where('del_time',0)
+            ->whereIn('data_id',$purchase_id)
+            ->where('type',ReturnExchangeOrder::Order_type2)
+            ->select('id','data_id')
+            ->get()->toArray();
+        $return_map = [];
+        foreach ($return as $value){
+            $return_map[$value['id']] = $value['data_id'];
+        }
+        $return_id = array_column($return,'id');
+        $product_save = [];
+        $sub = ReturnExchangeOrderProductInfo::where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
+            ->where('del_time',0)
+            ->whereIn('return_exchange_id',$return_id)
+            ->get()->toArray();
+        foreach ($sub as $value){
+            if(isset($return_map[$value['return_exchange_id']])){
+                $purchase_tmp_id = $return_map[$value['return_exchange_id']];
+
+                $key = $purchase_tmp_id . $value['product_id'];
+                if(isset($product_save[$key])){
+                    $tmp = bcadd($product_save[$key], $value['number'],2);
+                    $product_save[$key] = $tmp;
+                }else{
+                    $product_save[$key] = $value['number'];
+                }
+            }
+        }
+
+        foreach ($purchase_product as $k => $value){
+            $key = $value['purchase_order_id'] . $value['product_id'];
+            $return_number_tmp = $product_save[$key] ?? 0; // 退货产品数量
+            $purchase_number_lock_tmp = $purchase_product_outbound_map[$key] ?? 0; //采购单产品锁定未释放的数量
+
+            //最终采购单下某个产品的实际数量
+            $number = bcsub($value['number'], $purchase_number_lock_tmp,2);
+            $number = bcsub($number,$return_number_tmp,2);
+
+            if($number <= 0) {
+                unset($purchase_product[$k]);
+                continue;
+            }
+
+            $purchase_product[$k]['number'] = $number;
+        }
+
+        return $this->clearProduct($purchase_product);
+    }
+
+    public function clearProduct($purchase_product){
+        $product = [];
+        foreach ($purchase_product as $value){
+            $product[$value['purchase_order_id']][] = $value;
+        }
+
+        return $product;
+    }
 }

+ 19 - 1
app/Service/ReturnExchangeOrderService.php

@@ -7,6 +7,7 @@ use App\Model\Employee;
 use App\Model\OrderInventoryStock;
 use App\Model\PurchaseOrder;
 use App\Model\PurchaseOrderInfo;
+use App\Model\PurchaseOrderInfoForOutBound;
 use App\Model\ReturnExchangeOrder;
 use App\Model\ReturnExchangeOrderProductInfo;
 use App\Model\SalesOrder;
@@ -824,16 +825,33 @@ class ReturnExchangeOrderService extends Service
             }
         }
 
+        //出库
+        $out_bound = PurchaseOrderInfoForOutBound::where('del_time',0)
+            ->where('purchase_order_id',$purchase_id)
+            ->select('product_id','number')
+            ->get()->toArray();
+        $out_bound_map = [];
+        foreach ($out_bound as $value){
+            if(isset($out_bound_map[$value['product_id']])){
+                $out_bound_map[$value['product_id']] += $value['number'];
+            }else{
+                $out_bound_map[$value['product_id']] = $value['number'];
+            }
+        }
+
         $sales_order_product = [];
         $sales_product = PurchaseOrderInfo::where('del_time',0)
             ->where('purchase_order_id',$purchase_id)
             ->get()->toArray();
         foreach ($sales_product as $value){
             $product_save_tmp = $product_save[$value['product_id']] ?? 0;
+            $out_bound_tmp = $out_bound_map[$value['product_id']] ?? 0;
             if(isset($sales_order_product[$value['product_id']])){
                 $sales_order_product[$value['product_id']] += $value['number'];
             }else{
-                $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp;
+                $number = bcsub($value['number'], $product_save_tmp,2);
+                $number = bcsub($number, $out_bound_tmp,2);
+                $sales_order_product[$value['product_id']] = $number;
             }
         }
 

+ 7 - 0
app/Service/Service.php

@@ -252,6 +252,8 @@ class Service
     //后台端 某些需要限制请求频率的接口
     //需要主动删除  Redis::del($key)
     public function limitingSendRequestBackg($key,$value=0){
+        $prefix = config('app.name') . ':';
+        $key = $prefix . $key;
         if(! empty($value)) $value = 1;
         // 使用Redis Facade设置,当键名不存在时才设置成功
         if (Redis::setnx($key, $value)) return [true, ''];
@@ -260,11 +262,16 @@ class Service
     }
 
     public function dellimitingSendRequestBackg($key){
+        $prefix = config('app.name') . ':';
+        $key = $prefix . $key;
         Redis::del($key);
     }
 
     //后台端 某些需要限制请求频率的接口 有过期时间
     public function limitingSendRequestBackgExpire($key,$ttl = 5){
+        $prefix = config('app.name') . ':';
+        $key = $prefix . $key;
+
         if($ttl < 5) $ttl = 5;
 
         // 使用Redis Facade设置,当键名不存在时才设置成功