GoodStockInfo.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. $result=self::insert($data);
  32. }else{
  33. $total_num = $data["balance_num"]+$num;
  34. if($total_num> $data["total_num"]) throw new Exception("批次入库数量超过总数",1006);
  35. $temp=[];
  36. $temp["balance_num"]=$total_num;
  37. $temp["used_num"]=$data["used_num"]-$num;
  38. $temp["updatetime"]=date("Y-m-d H:i:s");
  39. $result=self::update($temp,$data)->getNumRows();
  40. }
  41. return $result;
  42. }
  43. /**
  44. * @param int $stockid
  45. * @param int $num
  46. * @return array 库存品下单消耗bn库存数
  47. * @throws \think\Exception
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. static function SaleBn($stockid,int $num,int $isstock=1){
  53. $arr = self::where([["balance_num",">",0]])->where(["stockid"=>$stockid])->select()->toArray();
  54. if(empty($arr)) throw new Exception("库存批次数量不足",1006);
  55. $list=[];
  56. foreach ($arr as $item) {
  57. $stock = self::name("good_stock")->where(["id"=>$item['stockid']])->field("id,usable_stock,wait_out_stock")->find()->toArray();
  58. if($num<=0) break;
  59. $update=[];
  60. if($item['balance_num']<=$num){
  61. $update['balance_num'] =0;
  62. $update['used_num']= $item['used_num']+$item['balance_num'];
  63. $num-=$item['balance_num'];
  64. $update['updatetime'] =date("Y-m-d H:i:s");
  65. $temp=["stockid"=>$item['stockid'],"bnCode"=>$item['bnCode'],"num"=>$item['balance_num'],"origin_price"=>$item["origin_price"]];
  66. $stockup=["usable_stock"=>$stock['usable_stock']-$item['balance_num'],"wait_out_stock"=>$stock['wait_out_stock']+$item['balance_num'],"updatetime"=>date("Y-m-d H:i:s")];
  67. }else{
  68. $update['balance_num']=$item['balance_num']-$num;
  69. $update['used_num']=$item['used_num']+$num;
  70. $update['updatetime'] =date("Y-m-d H:i:s");
  71. $temp=["stockid"=>$item['stockid'],"bnCode"=>$item['bnCode'],"num"=>$num,"origin_price"=>$item["origin_price"]];
  72. $stockup=["usable_stock"=>$stock['usable_stock']-$num,"wait_out_stock"=>$stock['wait_out_stock']+$num,"updatetime"=>date("Y-m-d H:i:s")];
  73. $num=0;
  74. }
  75. $up=self::update($update,$item);
  76. if($up==false) throw new Exception("库存批次库存更新失败",1006);
  77. if($isstock==1) {
  78. $stup=self::name("good_stock")->where($stock)->update($stockup);
  79. if($stup==false) throw new Exception("库存更新失败",1006);
  80. }
  81. $list[]=$temp;
  82. }
  83. return $list;
  84. }
  85. /**
  86. * @param int $stockid
  87. * @param string $bn
  88. * @param int $num
  89. * @return bool 盘点修改bn仓库库存数
  90. * @throws \think\Exception
  91. */
  92. static function CheckBn(int $stockid,string $bn,int $num){
  93. $data=self::where(["stockid"=>$stockid,"bnCode"=>$bn])->findOrEmpty()->toArray();
  94. if(empty($data)) throw new Exception("未找到Bn库存数据",1006);
  95. $update=[];
  96. $update["balance_num"]=$num;
  97. $update["total_num"]=$data["used_num"]+$num;
  98. $update["updatetime"]=date("Y-m-d H:i:s");
  99. return self::update($update,$data)->getNumRows();
  100. }
  101. /**
  102. * @param string $orderCode
  103. * @param int|array $stockid
  104. * @param int $num
  105. * @return int
  106. * @throws \think\Exception
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. static function OrderBn(string $orderCode, $stockid,int $num,int $isstock=1){
  112. $bnArr=self::SaleBn($stockid,$num,$isstock);
  113. if(empty($bnArr)) throw new Exception("未找到Bn库存数据",1006);
  114. $ordBn=[];
  115. foreach ($bnArr as $value){
  116. $saleBn=[
  117. "orderCode"=>$orderCode,
  118. "bnCode"=>$value['bnCode'],
  119. "stockid"=>$value['stockid'],
  120. "num"=>$value['num'],
  121. "send_num"=>$value['num'],
  122. "origin_price"=>$value['origin_price'],
  123. "addtime"=>date("Y-m-d H:i:s"),
  124. "updatetime"=>date("Y-m-d H:i:s"),
  125. ];
  126. $ordBn[]=$saleBn;
  127. }
  128. return self::name("sale_info")->insertAll($ordBn);
  129. }
  130. /**
  131. * @param string $returnCode
  132. * @param int $salebnid
  133. * @param int $num
  134. * @param int $type
  135. * @return bool
  136. * @throws \think\Exception
  137. */
  138. static function ReturnBn(string $returnCode,int $salebnid,int $num,int $type=1){
  139. $sabn=self::name("sale_info")->where(['id'=>$salebnid])->findOrEmpty()->toArray();
  140. if(empty($sabn))throw new Exception("未找到Bn订单数据",1006);
  141. $return=[
  142. "returnCode"=>$returnCode,
  143. "type"=>$type,
  144. "orderCode"=>$sabn['orderCode'],
  145. "bnCode"=>$sabn['bnCode'],
  146. "origin_price"=>$sabn['origin_price'],
  147. "num"=>$num,
  148. "addtime"=>date("Y-m-d H:i:s")
  149. ];
  150. return self::name("return_info")->insert($return);
  151. }
  152. /** 调拨专用 需要注意bn总数变化
  153. * @param int $stockid 库存id
  154. * @param string $bnCode bn编号
  155. * @param int $num 库存数量
  156. * @param int $flag 操作类型 0 添加库存 1 减少库存
  157. * @param float|int $origin_price 成本价
  158. * @return bool
  159. * @throws \think\Exception
  160. */
  161. static function bnStock(int $stockid,string $bnCode,int $num,int $flag=0,float $origin_price=0){
  162. $stock =self::where(["stockid"=>$stockid,"bnCode"=>$bnCode])->lock(true)->findOrEmpty()->toArray();
  163. $update=[];
  164. if($flag==1) {
  165. if(empty($stock)|| $stock['balance_num']< $num)
  166. throw new Exception("bn库存数不足",1006);
  167. else{
  168. $update['balance_num']=$stock['balance_num']-$num;
  169. $update['total_num']=$stock['total_num']-$num;
  170. $update['updatetime']=date("Y-m-d H:i:s");
  171. }
  172. }else{
  173. if (empty($stock)){
  174. $update=[
  175. "bnCode"=>$bnCode,
  176. "stockid"=>$stockid,
  177. "total_num"=>$num,
  178. "used_num"=>0,
  179. "balance_num"=>$num,
  180. "origin_price"=>$origin_price,
  181. "addtime"=>date("Y-m-d H:i:s"),
  182. "updatetime"=>date("Y-m-d H:i:s")
  183. ];
  184. }else{
  185. $update['balance_num']=$stock['balance_num']+$num;
  186. $update['total_num']=$stock['total_num']+$num;
  187. $update['updatetime']=date("Y-m-d H:i:s");
  188. }
  189. }
  190. return empty($stock)? self::insert($update): self::update($update,$stock)->getNumRows();
  191. }
  192. /**
  193. 退货退到其他仓库的话 原仓库批次总数 - 退货数量
  194. * @param $stockid 退货仓库
  195. * @param $bnCode bn批次编号
  196. * @param $num 退货数量
  197. * @param $origin_stockid 原仓库
  198. * @return int
  199. * @throws \think\Exception
  200. */
  201. static function ReturnAdd(int $stockid,string $bnCode,int $num,int $origin_stockid){
  202. $origin = self::where(["stockid"=>$origin_stockid,"bnCode"=>$bnCode])->findOrEmpty()->toArray();
  203. if($stockid!=$origin_stockid){
  204. if(empty($origin))throw new Exception("未找到Bn订单数据",1006);
  205. $temp['total_num'] =$origin['total_num'] -$num;
  206. $temp['used_num'] =$origin['used_num'] -$num;
  207. $temp['updatetime'] =date("Y-m-d H:i:s");
  208. self::update($temp,$origin);
  209. }
  210. $stock = self::where(["stockid"=>$stockid,"bnCode"=>$bnCode])->findOrEmpty()->toArray();
  211. if(empty($stock)){
  212. $update=[
  213. "bnCode"=>$bnCode,
  214. "stockid"=>$stockid,
  215. "total_num"=>$num,
  216. "used_num"=>0,
  217. "balance_num"=>$num,
  218. "origin_price"=>$origin['origin_price'],
  219. "addtime"=>date("Y-m-d H:i:s"),
  220. "updatetime"=>date("Y-m-d H:i:s")
  221. ];
  222. $result= self::insert($update);
  223. }else{
  224. $update['balance_num']=$stock['balance_num']+$num;
  225. $stockid!=$origin_stockid ? $update['total_num']=$stock['total_num']+$num:$update['used_num']=$stock['used_num']-$num;
  226. $update['updatetime']=date("Y-m-d H:i:s");
  227. $result=self::update($update,$stock)->getNumRows();
  228. }
  229. return $result;
  230. }
  231. }