GoodStockInfo.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use think\Exception;
  5. use think\Model;
  6. /**
  7. * @mixin \think\Model
  8. */
  9. class GoodStockInfo extends Model
  10. {
  11. /**
  12. * @param int $stockid 仓库库存id
  13. * @param string $bn bn编号
  14. * @param int $num bn数量
  15. * @param float $origin_price 采购成本价
  16. * @return bool 入库bn库存数新建 或退回bn库存数
  17. */
  18. static function AddBn(int $stockid,string $bn,int $num,float $origin_price=0){
  19. $data=self::where(["stockid"=>$stockid,"bnCode"=>$bn])->findOrEmpty()->toArray();
  20. if(empty($data)){
  21. $data=[
  22. "stockid"=>$stockid,
  23. "bnCode"=>$bn,
  24. "total_num"=>$num,
  25. "used_num"=>0,
  26. "balance_num"=>$num,
  27. "origin_price"=>$origin_price,
  28. "addtime"=>date("Y-m-d H:i:s"),
  29. "updatetime"=>date("Y-m-d H:i:s")
  30. ];
  31. }else{
  32. $total_num = $data["balance_num"]+$num;
  33. if($total_num> $data["total_num"]) throw new Exception("批次入库数量超过总数",1006);
  34. $data["balance_num"]=$num;
  35. $data["updatetime"]=date("Y-m-d H:i:s");
  36. }
  37. return (new self())->save($data);
  38. }
  39. /**
  40. * @param int $stockid
  41. * @param int $num
  42. * @return array 库存品下单消耗bn库存数
  43. * @throws \think\Exception
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. static function SaleBn(int $stockid,int $num){
  49. $arr = self::where([["stockid","=",$stockid],["balance_num",">",0]])->select()->toArray();
  50. if(empty($arr)) throw new Exception("库存批次数量不足","1006");
  51. $list=[];
  52. foreach ($arr as $item) {
  53. if($num<=0) break;
  54. $temp=[];
  55. if($item['balance_num']<=$num){
  56. $item['balance_num'] =0;
  57. $item['used_num'] +=$item['balance_num'];
  58. $num-=$item['balance_num'];
  59. $item['updatetime'] =date("Y-m-d H:i:s");
  60. $temp=["bnCode"=>$item['bnCode'],"num"=>$item['balance_num'],"origin_price"=>$item["origin_price"]];
  61. }else{
  62. $item['balance_num']-=$num;
  63. $item['used_num'] +=$num;
  64. $num=0;
  65. $item['updatetime'] =date("Y-m-d H:i:s");
  66. $temp=["bnCode"=>$item['bnCode'],"num"=>$num,"origin_price"=>$item["origin_price"]];
  67. }
  68. (new self())->save($item);
  69. $list[]=$temp;
  70. }
  71. return $list;
  72. }
  73. /**
  74. * @param int $stockid
  75. * @param string $bn
  76. * @param int $num
  77. * @return bool 盘点修改bn仓库库存数
  78. * @throws \think\Exception
  79. */
  80. static function CheckBn(int $stockid,string $bn,int $num){
  81. $data=self::where(["stockid"=>$stockid,"bnCode"=>$bn])->findOrEmpty()->toArray();
  82. if(empty($data)) throw new Exception("未找到Bn库存数据",1006);
  83. $data["balance_num"]=$num;
  84. $data["total_num"]=$data["used_num"]+$num;
  85. $data["updatetime"]=date("Y-m-d H:i:s");
  86. return (new self())->save($data);
  87. }
  88. /**
  89. * @param string $orderCode
  90. * @param int $stockid
  91. * @param int $num
  92. * @return int
  93. * @throws \think\Exception
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. static function OrderBn(string $orderCode,int $stockid,int $num){
  99. $bnArr=self::SaleBn($stockid,$num);
  100. if(empty($bnArr)) throw new Exception("未找到Bn库存数据",1006);
  101. $ordBn=[];
  102. foreach ($bnArr as $value){
  103. $saleBn=[
  104. "orderCode"=>$orderCode,
  105. "bnCode"=>$value['bnCode'],
  106. "stockid"=>$stockid,
  107. "num"=>$value['num'],
  108. "origin_price"=>$value['origin_price'],
  109. "addtime"=>date("Y-m-d H:i:s"),
  110. "updatetime"=>date("Y-m-d H:i:s"),
  111. ];
  112. $ordBn[]=$saleBn;
  113. }
  114. return self::name("sale_info")->insertAll($ordBn);
  115. }
  116. /**
  117. * @param string $returnCode
  118. * @param int $salebnid
  119. * @param int $num
  120. * @param int $type
  121. * @return bool
  122. * @throws \think\Exception
  123. */
  124. static function ReturnBn(string $returnCode,int $salebnid,int $num,int $type=1){
  125. $sabn=self::name("sale_info")->where(['id'=>$salebnid])->findOrEmpty()->toArray();
  126. if(empty($sabn))throw new Exception("未找到Bn订单数据",1006);
  127. $return=[
  128. "returnCode"=>$returnCode,
  129. "type"=>$type,
  130. "orderCode"=>$sabn['orderCode'],
  131. "bnCode"=>$sabn['bnCode'],
  132. "origin_price"=>$sabn['origin_price'],
  133. "num"=>$num,
  134. "addtime"=>date("Y-m-d H:i:s")
  135. ];
  136. return self::name("return_info")->save($return);
  137. }
  138. /** 调拨专用 需要注意bn总数变化
  139. * @param int $stockid 库存id
  140. * @param string $bnCode bn编号
  141. * @param int $num 库存数量
  142. * @param int $flag 操作类型 0 添加库存 1 减少库存
  143. * @param float|int $origin_price 成本价
  144. * @return bool
  145. * @throws \think\Exception
  146. */
  147. static function bnStock(int $stockid,string $bnCode,int $num,int $flag=0,float $origin_price=0){
  148. $stock =self::where(["stockid"=>$stockid,"bnCode"=>$bnCode])->lock(true)->findOrEmpty()->toArray();
  149. if($flag==1) {
  150. if(empty($stock)|| $stock['balance_num']< $num)
  151. throw new Exception("bn库存数不足",1006);
  152. else{
  153. $stock['balance_num']-=$num;
  154. $stock['total_num']-=$num;
  155. $stock['updatetime']=date("Y-m-d H:i:s");
  156. }
  157. }else{
  158. if (empty($stock)){
  159. $stock=[
  160. "bnCode"=>$bnCode,
  161. "stockid"=>$stockid,
  162. "total_num"=>$num,
  163. "used_num"=>0,
  164. "balance_num"=>$num,
  165. "origin_price"=>$origin_price,
  166. "addtime"=>date("Y-m-d H:i:s"),
  167. "updatetime"=>date("Y-m-d H:i:s")
  168. ];
  169. }else{
  170. $stock['balance_num']+=$num;
  171. $stock['total_num']+=$num;
  172. $stock['updatetime']=date("Y-m-d H:i:s");
  173. }
  174. }
  175. return (new self())->save($stock);
  176. }
  177. }