",0]])->select()->toArray(); if(empty($arr))throw new \Exception("未找到可用的批次库存数据",1006); $stockid = array_unique(array_column($arr,"stockid")); $stock=self::name("good_stock")->where(["id"=>$stockid])->column("id,usable_stock,wait_out_stock",'id'); // Log::write("库存更新{$orderCode}:".json_encode($stock,JSON_UNESCAPED_UNICODE),"info",true); foreach ($arr as $value){ $temp=["send_num"=>0,"updatetime"=>date("Y-m-d H:i:s")]; if($sendNum==0) break; if($stock[$value['stockid']]['wait_out_stock']<$sendNum) throw new \Exception("待出库存数量不足"); if($sendNum >$value['send_num']){ $sendNum-=$value['send_num']; $stockup=["total_stock"=>$stock[$value['stockid']]['wait_out_stock']+$stock[$value['stockid']]['usable_stock']-$value['send_num'],"wait_out_stock"=>$stock[$value['stockid']]['wait_out_stock']-$value['send_num'],"updatetime"=>date("Y-m-d H:i:s")]; $stock[$value['stockid']]['wait_out_stock'] =$stock[$value['stockid']]['wait_out_stock']-$value['send_num']; }else{ $temp['send_num'] =$value['send_num']-$sendNum; $stockup=["total_stock"=>$stock[$value['stockid']]['wait_out_stock']+$stock[$value['stockid']]['usable_stock']-$sendNum,"wait_out_stock"=>$stock[$value['stockid']]['wait_out_stock']-$sendNum,"updatetime"=>date("Y-m-d H:i:s")]; $stock[$value['stockid']]['wait_out_stock'] =$stock[$value['stockid']]['wait_out_stock']-$sendNum; $sendNum=0; } $ip = self::where($value)->update($temp); if($ip==false) throw new \Exception("库存更新失败"); $sp=self::name("good_stock")->where(["id"=>$stock[$value['stockid']]['id']])->update($stockup); if($sp==false) throw new \Exception("库存更新失败"); } return true; } }