CombindBninfo.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class CombindBninfo extends Model
  9. {
  10. protected $createTime="addtime";
  11. protected $updateTime='updatetime';
  12. public function AaddBNLog($spuCode='',$bnCode='',$bnArr=[],$stockCode=''){
  13. if(empty($bnArr)) return ;
  14. $list=[];
  15. foreach ($bnArr as $item){
  16. $tmp=[];
  17. $tmp["bnCode"]=$bnCode;
  18. $tmp["stockCode"]=$stockCode;
  19. $tmp["flag"]=1;
  20. $tmp["spuCode"]=$spuCode;
  21. $tmp["child_bnCode"]=$item['bnCode'];
  22. $tmp["stock_id"]=$item['stockid'];
  23. $tmp["childCode"]=$item['spuCode'];
  24. $tmp["childBnNum"]=$item['num'];
  25. $tmp["bnNum"]=$item['num'];
  26. $tmp["origin_price"]=$item['origin_price'];
  27. $list[]=$tmp;
  28. }
  29. $this->saveAll($list);
  30. }
  31. /**解除库存
  32. * @param string $spuCode
  33. * @param string $bnCode
  34. * @param string $num
  35. */
  36. public function subBN($spuCode='',$bnCode='',$childCode='',$num='',$stockCode=''){
  37. $stockinfo = $this->where(["spuCode"=>$spuCode,"bnCode"=>$bnCode,"childCode"=>$childCode,"flag"=>1])->where("childBnNum",">",0)->order("id desc")
  38. ->select()->toArray();
  39. if(empty($stockinfo)) throw new \Exception("未找到关联得bn库存数据");
  40. foreach ($stockinfo as $item){
  41. $tempnum=0;
  42. if($num<=0) break;
  43. if($num > $item['childBnNum']){
  44. $tempnum = $item['childBnNum'];
  45. $num= $num -$item['childBnNum'];
  46. $item['childBnNum']=0;
  47. }else{
  48. $tempnum = $num;
  49. $item['childBnNum']=$item['childBnNum']-$num;
  50. $num =0;
  51. }
  52. self::update($item);
  53. unset($item['id']);
  54. $item['stockCode']=$stockCode;
  55. $item['flag']=0;
  56. $item['childBnNum']=$tempnum;
  57. $this->save($item);
  58. $up=GoodStockInfo::AddBn(intval($item['stock_id']),$item['child_bnCode'],intval($tempnum),floatval($item['origin_price']));
  59. if($up==false) throw new \Exception('bn库存数据更新失败');
  60. $save= (new GoodStock())->where(["id"=>$item['stock_id']])->inc("usable_stock",intval($tempnum))->inc("total_stock",intval($tempnum))->update();
  61. if($save==false) throw new \Exception('bn库存数据更新失败');
  62. }
  63. return true;
  64. }
  65. /**解除库存
  66. * @param string $stockCode
  67. */
  68. public function stockSub($stockCode=''){
  69. $stockinfo = $this->where(["stockCode"=>$stockCode,'flag'=>1])->where('childBnNum','>',0)->order('id desc')
  70. ->select()->toArray();
  71. if(empty($stockinfo)) throw new \Exception('未找到关联得bn库存数据');
  72. $goodinfo =new Good();
  73. foreach ($stockinfo as $item){
  74. $up=GoodStockInfo::AddBn(intval($item['stock_id']),$item['child_bnCode'],intval($item['childBnNum']),floatval($item['origin_price']));
  75. if($up==false) throw new \Exception('bn库存数据更新失败');
  76. $save= (new GoodStock())->where(['id'=>$item['stock_id']])->inc('usable_stock',intval($item['childBnNum']))->inc('total_stock',intval($item['childBnNum']))->update();
  77. if($save==false) throw new \Exception('bn库存数据更新失败');
  78. $good = $goodinfo->where(['spuCode'=>$item['childCode']])->findOrEmpty();
  79. if($good->isEmpty()==false){
  80. $good->usable_stock = bcadd($good->usable_stock,$item['childBnNum']);
  81. $gdup=$good->save();
  82. if($gdup==false)throw new \Exception('子商品库存更新失败');
  83. }
  84. }
  85. return true;
  86. }
  87. }