ProductStock.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. $model->residue_stock=bcsub($model->residue_stock,$num,8);
  59. $model->pending_stock=bcsub($model->pending_stock,$num,8);
  60. $save=$model->save();
  61. if(!$save) throw new \Exception('库存修改失败');
  62. return ['product_id'=>$model->product_id,'type'=>2,'num'=>$num];
  63. }
  64. // 库存增加
  65. public static function AddSingleStock($model,$num,$product_id){
  66. $model->product_id=$product_id;
  67. $model->residue_stock=bcadd($model->residue_stock??"0",$num,8);
  68. $model->total_stock=bcadd($model->total_stock??"0",$num,8);
  69. $save=$model->save();
  70. if(!$save) throw new \Exception('库存修改失败');
  71. return ['product_id'=>$product_id,'type'=>1,'num'=>$num];
  72. }
  73. public static function SubCombindStock($product_id,$num,$type=1){
  74. $list = ProductsCombind::where('parent_id',$product_id)->select();
  75. if($list->isEmpty()) throw new \Exception('组合商品不存在');
  76. $productID = [];
  77. foreach ($list as $k=>$v){
  78. $where = $type==1? ['skuCode'=>$v['skuCode'],"basic_status"=>1] : ['skuCode'=>$v['skuCode']];
  79. $stock=FinancialProducts::with(['ProductStock'])->where($where)->select();
  80. if($stock->isEmpty()) throw new \Exception('组合商品子商品不存在');
  81. $child_num = bcmul( $v['child_num'],$num,8);
  82. $total= count($stock);
  83. $stock->each(function ($item,$key) use (&$productID,&$child_num,$total,$type){
  84. $residue_stock = $item->ProductStock->residue_stock;
  85. if($residue_stock > $child_num || ($type==2 && $total==$key+1)) {
  86. $sub_stock = $child_num;
  87. $child_num = "0";
  88. }else{
  89. $child_num = bcsub($child_num,$residue_stock,8);
  90. $sub_stock = $residue_stock;
  91. }
  92. $productID[] = ['product_id' => $item->id, 'type'=>2,'num'=> $sub_stock];
  93. if($child_num == 0) return false;
  94. });
  95. }
  96. return $productID;
  97. }
  98. public static function AddCombindStock($product_id,$num){
  99. $list = ProductsCombind::where('parent_id',$product_id)->select();
  100. if($list->isEmpty()) throw new \Exception('组合商品不存在');
  101. $productID = [];
  102. foreach ($list as $k=>$v) {
  103. $child=FinancialProducts::findOrEmpty($v['child_id']);
  104. $productID[]=self::AddSingleStock($child,$num,$v['child_id']);
  105. }
  106. return $productID;
  107. }
  108. /**
  109. * @param $product_id 商品id
  110. * @param $num 待出库数量
  111. * @param $type 1 正常出库 2 强制出库
  112. * @return true
  113. * @throws \Exception
  114. */
  115. public static function OutStock($product_id,$num,$type=1){
  116. $product=self::where("product_id",$product_id)->findOrEmpty();
  117. if(!$product->isEmpty()){
  118. if($product->residue_stock<$num && $type==1) throw new \Exception('库存不足');
  119. $product->residue_stock=bcsub($product->residue_stock,$num,8);
  120. $product->pending_stock=bcadd($product->pending_stock,$num,8);
  121. $save=$product->save();
  122. if(!$save) throw new \Exception('库存修改失败');
  123. }else{
  124. throw new \Exception('商品库存未找到'.$product_id);
  125. }
  126. return true;
  127. }
  128. }