123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\model;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class CombindBninfo extends Model
- {
- protected $createTime="addtime";
- protected $updateTime='updatetime';
-
- public function AaddBNLog($spuCode='',$bnCode='',$bnArr=[],$stockCode=''){
- if(empty($bnArr)) return ;
- $list=[];
- foreach ($bnArr as $item){
- $tmp=[];
- $tmp["bnCode"]=$bnCode;
- $tmp["stockCode"]=$stockCode;
- $tmp["flag"]=1;
- $tmp["spuCode"]=$spuCode;
- $tmp["child_bnCode"]=$item['bnCode'];
- $tmp["stock_id"]=$item['stockid'];
- $tmp["childCode"]=$item['spuCode'];
- $tmp["childBnNum"]=$item['num'];
- $tmp["bnNum"]=$item['num'];
- $tmp["origin_price"]=$item['origin_price'];
- $list[]=$tmp;
- }
-
- $this->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;
- }
- }
|