Просмотр исходного кода

Merge branch 'master-new-wgg' of wugg/phpstock into master-new_order_out_child

wugg 2 лет назад
Родитель
Сommit
27a0b5e906
2 измененных файлов с 89 добавлено и 19 удалено
  1. 1 1
      app/admin/model/GoodStockInfo.php
  2. 88 18
      app/admin/model/OrderOutChild.php

+ 1 - 1
app/admin/model/GoodStockInfo.php

@@ -240,7 +240,7 @@ class GoodStockInfo extends Model
 	 */
 	static function ChildAddBn(string $childCode='',int $stockid=0){
 		$child = OrderOutChild::where(["outChildCode" => $childCode, "is_del" => 0])->findOrEmpty()->toArray();
-		if(empty($child))throw new Exception("未找到单数据",1006);
+		if(empty($child))throw new Exception("未找到单数据",1006);
 		$bnArr=self::SaleBn($stockid,intval($child['num']));
         if(empty($bnArr)) throw new Exception("未找到Bn库存数据",1006);
         $ordBn=[];

+ 88 - 18
app/admin/model/OrderOutChild.php

@@ -3,20 +3,18 @@ declare (strict_types = 1);
 
 namespace app\admin\model;
 
-use app\admin\common\YouZan;use think\Model;
+use app\admin\common\YouZan;use think\Exception;use think\Model;
 
 /**
  * @mixin \think\Model
  */
 class OrderOutChild extends Model
 {   /**
-	* @param array $sale 销售单信息
 	* @param string $outCode 发货单信息
-	* @param int $num 发货单数量
 	* @throws \Exception
 	 */
     static function makeChild(string $outCode=''){
-    	if($outCode=='')throw new \Exception("单发货单单号不能为空");
+    	if($outCode=='')throw new \Exception("单发货单单号不能为空");
     	$out=self::name("order_out")->where(["outCode"=>$outCode,"is_del"=>0])->findOrEmpty();
 		if(empty($out)) throw new \Exception("发货单信息未找到");
 		if($out['send_status']!=0) throw new \Exception("发货单拆单状态有误");
@@ -25,14 +23,14 @@ class OrderOutChild extends Model
 		if($sale['wsend_num']<$out['send_num']) throw new \Exception("订单待发货数量不足");
 		$num= $out['send_num'];
 		$wsmlist = self::name("good_stock")->alias("a")
-			->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code")
+			->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code and b.wsm_type in (2,5)")
 			->field("a.id,a.usable_stock,a.wait_out_stock,b.wsm_code,b.supplierNo,b.supplierName")
-			->where(["spuCode"=>$sale['good_code'],"a.is_del"=>0])->findOrEmpty();
-		if($wsmlist['usable_stock']<$num) return false;
-		$outinfo = self::name("order_out")->where(["outCode"=>$outCode,"is_del"=>0])->findOrEmpty();
-		if(empty($outinfo))throw new \Exception("发货单信息未找到");
+			->where(["spuCode"=>$sale['good_code'],"a.is_del"=>0,"b.companyNo"=>$sale['companyNo']])
+			->where(["a.usable_stock",">=",$num])
+			->order("a.usable_stock asc")
+			->findOrEmpty();
 		$child=[
-			"outChildCode"=>makeNo("TCD"),
+			"outChildCode"=>'',
 			"outCode"=>$outCode,
 			"orderCode"=>$sale['orderCode'],
 			"companyNo"=>$sale['companyNo'],
@@ -46,19 +44,91 @@ class OrderOutChild extends Model
 			"order_type"=>$sale['order_type'],
 			"order_source"=>$sale['order_source'],
 			"num"=>$num,
-			"wsm_code"=>$wsmlist['wsm_code'],
-			"apply_id"=>$outinfo['apply_id'],
-			"apply_name"=>$outinfo['apply_name'],
-			"addrid"=>$outinfo['addrid'],
+			"wsm_code"=>$wsmlist['wsm_code']??'',
+			"apply_id"=>$out['apply_id'],
+			"apply_name"=>$out['apply_name'],
+			"addrid"=>$out['addrid'],
 			"addtime"=>date("Y-m-d H:i:s"),
 			"updatetime"=>date("Y-m-d H:i:s")
 		];
-		$childout=self::insert($child);
-		if($childout==false)throw new \Exception("分单失败");
+		if(empty($wsmlist)) self::BratchChild($child);
+		else self::SingleChild($child,$wsmlist['id']);
 		$outup=self::name("order_out")->where(["outCode"=>$outCode,"is_del"=>0])->update(["send_status"=>2,"status"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
 		if($outup==false)throw new \Exception("发货单信息更新失败");
-		$bnadd = GoodStockInfo::ChildAddBn($child['outChildCode'],$wsmlist['id']);
-		if($bnadd==false)throw new \Exception("库存更新失败");
 		return true;
     }
+	/**单工单创建
+	* @param array $child
+	* @param int $wsm_id
+	* @throws \think\Exception
+	 */
+    public static function SingleChild(array $child=[],int $wsm_id=0){
+		if(empty($child)) throw new Exception("发货工单信息不能为空");
+		$child['outChildCode'] = makeNo("TCD");
+		$childout=self::insert($child);
+		if($childout==false)throw new \Exception("发货工单生成失败");
+		$bnadd = GoodStockInfo::ChildAddBn($child['outChildCode'],$wsm_id);
+		if($bnadd==false)throw new \Exception("库存更新失败");
+
+    }
+	/**多工单创建
+	* @param array $child
+	* @throws \think\Exception
+	 */
+    public static function BratchChild(array $child=[]){
+		if(empty($child)) throw new Exception("工单信息不能为空");
+		$stock = self::name("good_stock")->alias("a")
+			->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code and b.wsm_type in (2,5)")
+			->where(["spuCode"=>$child['good_code'],"a.is_del"=>0,"b.companyNo"=>$child['companyNo']])
+			->order("a.usable_stock desc")
+			->column("a.id,a.usable_stock,a.wait_out_stock,b.wsm_code,b.supplierNo,b.supplierName","a.id");
+		if(empty($stock)) throw new Exception("商品库存信息未找到");
+		$wsmArr =self::GetKeyBySum($stock,$child['num']);
+		if(empty($wsmArr)) throw new Exception("库存数不足生成工单");
+		$i=0;
+		foreach ($wsmArr as $key=>$val){
+			$i++;
+			$child['outChildCode'] = substr(makeNo("TCD"), 0, -3) . str_pad($i, 3, '0', STR_PAD_LEFT);
+			$child['num'] = $val['desc_num']??0;
+			$child['wsm_code'] = $val['wsm_code'];
+			$childout=self::insert($child);
+			if($childout==false)throw new \Exception("工单生成失败");
+			$bnadd = GoodStockInfo::ChildAddBn($child['outChildCode'],$val['id']);
+			if($bnadd==false)throw new \Exception("库存更新失败");
+		}
+    }
+	/**仓库选择
+	* @param array $Arr
+	* @param int $num
+	* @return array
+	* @throws \Exception
+	*/
+    private static function GetKeyBySum( array $Arr=[],int $num=0){
+    	    $resp=[];
+    	    $keys = array_keys($Arr);
+    	    $stock =array_column($Arr,"usable_stock");
+    	    $newArr=array_combine($keys,$stock);
+    	    if(array_sum($stock)<$num) throw new \Exception("库存数不足生成工单");
+			foreach ($Arr as $key=>$value){
+				if($value['usable_stock']>=$num){
+						$value['desc_num'] = $num;
+						$num=0;
+				}else{
+					$value['desc_num'] = $value['usable_stock'];
+					$num-=$value['usable_stock'];
+				}
+				$resp[]=$value;
+				if($num==0)break;
+				unset($newArr[$key]);
+				$k=array_search($num,$newArr);
+				if($k===false) continue;
+				else{
+					$Arr[$k]['desc_num'] = $num;
+					$resp[]=$Arr[$k];
+					break;
+				}
+			}
+			return $resp;
+    }
+
 }