saveAll($list); } /**解除库存 * @param string $spuCode * @param string $bnCode * @param string $num */ public function subBN($spuCode='',$bnCode='',$childCode='',$num='',$stockCode=''){ $stockinfo = $this->where(["spuCode"=>$spuCode,"bnCode"=>$bnCode,"childCode"=>$childCode,"flag"=>1])->where("childBnNum",">",0)->order("id desc") ->select()->toArray(); if(empty($stockinfo)) throw new \Exception("未找到关联得bn库存数据"); foreach ($stockinfo as $item){ $tempnum=0; if($num<=0) break; if($num > $item['childBnNum']){ $tempnum = $item['childBnNum']; $num= $num -$item['childBnNum']; $item['childBnNum']=0; }else{ $tempnum = $num; $item['childBnNum']=$item['childBnNum']-$num; $num =0; } self::update($item); unset($item['id']); $item['stockCode']=$stockCode; $item['flag']=0; $item['childBnNum']=$tempnum; $this->save($item); $up=GoodStockInfo::AddBn(intval($item['stock_id']),$item['child_bnCode'],intval($tempnum),floatval($item['origin_price'])); if($up==false) throw new \Exception('bn库存数据更新失败'); $save= (new GoodStock())->where(["id"=>$item['stock_id']])->inc("usable_stock",intval($tempnum))->inc("total_stock",intval($tempnum))->update(); if($save==false) throw new \Exception('bn库存数据更新失败'); } return true; } /**解除库存 * @param string $stockCode */ public function stockSub($stockCode=''){ $stockinfo = $this->where(["stockCode"=>$stockCode,'flag'=>1])->where('childBnNum','>',0)->order('id desc') ->select()->toArray(); if(empty($stockinfo)) throw new \Exception('未找到关联得bn库存数据'); $goodinfo =new Good(); foreach ($stockinfo as $item){ $up=GoodStockInfo::AddBn(intval($item['stock_id']),$item['child_bnCode'],intval($item['childBnNum']),floatval($item['origin_price'])); if($up==false) throw new \Exception('bn库存数据更新失败'); $save= (new GoodStock())->where(['id'=>$item['stock_id']])->inc('usable_stock',intval($item['childBnNum']))->inc('total_stock',intval($item['childBnNum']))->update(); if($save==false) throw new \Exception('bn库存数据更新失败'); $good = $goodinfo->where(['spuCode'=>$item['childCode']])->findOrEmpty(); if($good->isEmpty()==false){ $good->usable_stock = bcadd($good->usable_stock,$item['childBnNum']); $gdup=$good->save(); if($gdup==false)throw new \Exception('子商品库存更新失败'); } } return true; } }