CombindBninfo.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }else{
  47. $tempnum = $num;
  48. $item['childBnNum']=$num;
  49. $num =0;
  50. }
  51. unset($item['id']);
  52. $item['stockCode']=$stockCode;
  53. $item['flag']=0;
  54. $this->save($item);
  55. $up=GoodStockInfo::AddBn(intval($item['stock_id']),$item['child_bnCode'],intval($tempnum),floatval($item['origin_price']));
  56. if($up==false) throw new \Exception('bn库存数据更新失败');
  57. $save= (new GoodStock())->where(["id"=>$item['stock_id']])->inc("usable_stock",intval($tempnum))->inc("total_stock",intval($tempnum))->update();
  58. if($save==false) throw new \Exception('bn库存数据更新失败');
  59. }
  60. return true;
  61. }
  62. /**解除库存
  63. * @param string $spuCode
  64. * @param string $bnCode
  65. * @param string $num
  66. */
  67. public function subBNAdd($spuCode='',$bnCode='',$childCode='',$num='',$stockCode=''){
  68. $stockinfo = $this->where(['spuCode'=>$spuCode,'bnCode'=>$bnCode,'childCode'=>$childCode,'flag'=>1])->where('childBnNum','>',0)->order('id desc')
  69. ->select()->toArray();
  70. if(empty($stockinfo)) throw new \Exception('未找到关联得bn库存数据');
  71. foreach ($stockinfo as $item){
  72. $tempnum=0;
  73. if($num<=0) break;
  74. if($num > $item['childBnNum']){
  75. $tempnum = $item['childBnNum'];
  76. $num= $num -$item['childBnNum'];
  77. $item['childBnNum']=0;
  78. }else{
  79. $tempnum = $num;
  80. $item['childBnNum']=$item['childBnNum']-$num;
  81. $num =0;
  82. }
  83. self::update($item);
  84. $up=GoodStockInfo::AddBn(intval($item['stock_id']),$item['child_bnCode'],intval($tempnum),floatval($item['origin_price']));
  85. if($up==false) throw new \Exception('bn库存数据更新失败');
  86. $save= (new GoodStock())->where(['id'=>$item['stock_id']])->inc('usable_stock',intval($tempnum))->inc('total_stock',intval($tempnum))->update();
  87. if($save==false) throw new \Exception('bn库存数据更新失败');
  88. }
  89. return true;
  90. }
  91. }