ProductStock.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\cxinv\model;
  3. class ProductStock extends Base
  4. {
  5. //设置字段信息
  6. protected $schema = [
  7. 'id' =>'bigint',//
  8. 'product_id' =>'bigint',//商品id
  9. 'residue_stock' =>'bigint',//剩余库存
  10. 'pending_stock' =>'bigint',//待出库存
  11. 'total_stock' =>'bigint',//总库存
  12. 'create_time' =>'datetime',//
  13. 'update_time' =>'datetime',//
  14. ];
  15. protected $createTime = 'create_time';
  16. protected $updateTime = 'update_time';
  17. protected $append= ['combind_stock'];
  18. public function Product(){
  19. return $this->belongsTo(FinancialProducts::class,'product_id','id')->bind(['goodName','skuCode']);
  20. }
  21. public function getCombindStockAttr($value,$data){
  22. $isCombind=FinancialProducts::where('id',$data['product_id'])->value('is_combind');
  23. if($isCombind==1){
  24. $combind_stock=ProductsCombind::with(['ProductStock'=>['ProductStock']])->where('parent_id',$data['product_id'])->select();
  25. $num=0;
  26. $combind_stock->each(function ($item,$key) use (&$num){
  27. $child_num = $item->child_num;
  28. $Total_stock = array_sum(array_column($item->ProductStock->toArray(),'residue_stock'));
  29. $Parent_stock = bcdiv($Total_stock,$child_num,8);
  30. if($key==0) $num=$Parent_stock;
  31. elseif($Parent_stock<$num) $num=$Parent_stock;
  32. });
  33. return $num;
  34. }else{
  35. return 0;
  36. }
  37. }
  38. public static function AddStock($product_id,$num){
  39. $product=self::where("product_id",$product_id)->findOrEmpty();
  40. if(!$product->isEmpty()){
  41. $product->residue_stock=bcadd($product->residue_stock,$num,8);
  42. $product->total_stock=bcadd($product->total_stock,$num,8);
  43. $save=$product->save();
  44. if(!$save) throw new \Exception('库存添加失败');
  45. }else{
  46. $product_data=[
  47. 'product_id'=>$product_id,
  48. 'residue_stock'=>$num,
  49. 'total_stock'=>$num,
  50. ];
  51. $creste= self::create($product_data);
  52. if($creste->isEmpty()) throw new \Exception('库存添加失败');
  53. }
  54. return true;
  55. }
  56. // 库存减少
  57. public static function SubSingleStock($model,$num){
  58. $info = self::where('product_id',$model->id)->findOrEmpty();
  59. $info->residue_stock=bcsub($model->residue_stock,$num,8);
  60. $info->pending_stock=bcsub($model->pending_stock,$num,8);
  61. $save=$info->save();
  62. if(!$save) throw new \Exception('库存修改失败');
  63. return ['product_id'=>$model->id,'type'=>2,'num'=>$num,'unit_price'=>$model['unit_price'],'rate'=>$model['cat_tax'],'subunit_price'=>$model['subunit_price']];
  64. }
  65. // 库存增加
  66. public static function AddSingleStock($model,$num){
  67. $info = self::where('product_id',$model->id)->findOrEmpty();
  68. $info->product_id=$model->id;
  69. $info->residue_stock=bcadd($info->residue_stock??"0",$num,8);
  70. $info->total_stock=bcadd($info->total_stock??"0",$num,8);
  71. $save=$info->save();
  72. if(!$save) throw new \Exception('库存修改失败');
  73. return ['product_id'=>$model->id,'type'=>1,'num'=>$num,'unit_price'=>$model['unit_price'],'rate'=>$model['cat_tax'],'subunit_price'=>$model['subunit_price']];
  74. }
  75. public static function SubCombindStock($product_id,$num,$type=1){
  76. $list = ProductsCombind::where('parent_id',$product_id)->select();
  77. if($list->isEmpty()) throw new \Exception('组合商品不存在');
  78. $productID = [];
  79. foreach ($list as $k=>$v){
  80. $where = $type==1? ['skuCode'=>$v['skuCode'],"basic_status"=>1] : ['skuCode'=>$v['skuCode']];
  81. $stock=FinancialProducts::with(['ProductStock'])->where($where)->select();
  82. if($stock->isEmpty()) throw new \Exception('组合商品子商品不存在');
  83. $child_num = bcmul( $v['child_num'],$num,8);
  84. $total= count($stock);
  85. $stock->each(function ($item,$key) use (&$productID,&$child_num,$total,$type){
  86. $residue_stock = $item->ProductStock->residue_stock;
  87. if($residue_stock > $child_num || ($type==2 && $total==$key+1)) {
  88. $sub_stock = $child_num;
  89. $child_num = "0";
  90. }else{
  91. $child_num = bcsub($child_num,$residue_stock,8);
  92. $sub_stock = $residue_stock;
  93. }
  94. $productID[] = ['product_id' => $item->id, 'type'=>2,'num'=> $sub_stock,'unit_price'=>$item['unit_price'],'rate'=>$item['cat_tax'],'subunit_price'=>$item['subunit_price']];
  95. if($child_num == 0) return false;
  96. });
  97. if($child_num != 0 && $type==1 ) throw new \Exception('组合商品子'.$v['skuCode'].'商品库存不足') ;
  98. }
  99. return $productID;
  100. }
  101. public static function AddCombindStock($product_id,$num){
  102. $list = ProductsCombind::where('parent_id',$product_id)->select();
  103. if($list->isEmpty()) throw new \Exception('组合商品不存在');
  104. $productID = [];
  105. foreach ($list as $k=>$v) {
  106. $child=FinancialProducts::findOrEmpty($v['child_id']);
  107. if($child->isEmpty()) throw new \Exception('组合商品子'.$v['skuCode'].'商品不存在');
  108. $child_num = bcmul( $v['child_num'],$num,8);
  109. $productID[]=self::AddSingleStock($child,$child_num);
  110. }
  111. return $productID;
  112. }
  113. /**
  114. * @param $product_id 商品id
  115. * @param $num 待出库数量
  116. * @param $type 1 正常出库 2 强制出库
  117. * @return true
  118. * @throws \Exception
  119. */
  120. public static function OutStock($product_id,$num,$type=1){
  121. $product=self::where("product_id",$product_id)->findOrEmpty();
  122. if(!$product->isEmpty()){
  123. if($product->residue_stock<$num && $type==1) throw new \Exception('库存不足');
  124. $product->residue_stock=bcsub($product->residue_stock,$num,8);
  125. $product->pending_stock=bcadd($product->pending_stock,$num,8);
  126. $save=$product->save();
  127. if(!$save) throw new \Exception('库存修改失败');
  128. }else{
  129. throw new \Exception('商品库存未找到'.$product_id);
  130. }
  131. return true;
  132. }
  133. }