Browse Source

合并,解决冲突

wufeng 2 years ago
parent
commit
920016cdaa

+ 3 - 3
app/admin/controller/Sale.php

@@ -1166,7 +1166,7 @@ class Sale extends Base
                                 Db::rollback();
                                 return error_show(1002, "发货地址添加创建失败");
                             } else {
-                            	OrderOutChild::makeChild($outCode);
+//                            	OrderOutChild::makeChild($outCode); //非库存品下单无法立即分单
                                 //修改状态,添加待办
                                 ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
                                     "order_code" => $outCode,//出库单号
@@ -1679,7 +1679,7 @@ class Sale extends Base
                         if ($ou == false) {
                              throw new Exception("发货单生成失败");
                         }
-                        OrderOutChild::makeChild($outCode);
+//                        OrderOutChild::makeChild($outCode);
                         //修改状态,添加待办
                         ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
                             "order_code" => $outCode,//项目编码
@@ -1972,7 +1972,7 @@ class Sale extends Base
                         if ($ou == false) {
                             throw new Exception("发货单生成失败");
                         } else {
-                        	OrderOutChild::makeChild($outCode);
+                        	if($good['is_stock'] == 1)OrderOutChild::makeChild($outCode);//非库存品无法立即分单
                             $standing_book_data['outCode'][] = $outCode;
                             //修改状态,添加待办
                             if($good['is_stock'] == 1){

+ 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;
+    }
+
 }

+ 1 - 1
app/command/ImportOrderFromCHandleData.php

@@ -388,7 +388,7 @@ class ImportOrderFromCHandleData extends Command
                             $ou = Db::name("order_out")->insertGetId($out);
                             if ($ou == false) throw new Exception('发货地址添加创建失败');
                             else {
-                            	OrderOutChild::makeChild($outCode);
+                            if($is_stock == 1)OrderOutChild::makeChild($outCode);//非库存品无法立即分单
 
                                 //修改状态,添加待办
                                 if ($is_stock == 1) {

+ 2 - 2
app/command/handleYzOrderData.php

@@ -276,7 +276,7 @@ class handleYzOrderData extends Command
 //                    ->field('id,person,personid')
 //                    ->where('code', $ct['supplierNo'])
 //                    ->findOrEmpty();
-                $userCommon = new User();
+                $userCommon = User::getIns();
                 $tmp = $userCommon->handle('sInfo',['code'=>$ct['supplierNo']]);
                 $supplier_temp_info = $tmp['data']??[];
 
@@ -528,7 +528,7 @@ class handleYzOrderData extends Command
                             $ou = Db::name("order_out")->insertGetId($out);
                             if ($ou == false) throw new Exception('发货地址添加创建失败');
                             else {
-                            	OrderOutChild::makeChild($outCode);
+                            if ($is_stock == 1) OrderOutChild::makeChild($outCode);
                                 //修改状态,添加待办
                                 ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
                                     "order_code" => $outCode,//出库单号