GoodStockInfo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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,spuCode")->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'],"spuCode"=>$stock['spuCode'],"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'],"spuCode"=>$stock['spuCode'],"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. $stup=self::name("good_stock")->where($stock)->update($stockup);
  78. if($stup==false) throw new Exception("库存更新失败",1006);
  79. $list[]=$temp;
  80. }
  81. return $list;
  82. }
  83. /**
  84. * @param int $stockid
  85. * @param string $bn
  86. * @param int $num
  87. * @return bool 盘点修改bn仓库库存数
  88. * @throws \think\Exception
  89. */
  90. static function CheckBn(int $stockid,string $bn,int $num){
  91. $data=self::where(["stockid"=>$stockid,"bnCode"=>$bn])->findOrEmpty()->toArray();
  92. if(empty($data)) throw new Exception("未找到Bn库存数据",1006);
  93. $update=[];
  94. $update["balance_num"]=$num;
  95. $update["total_num"]=$data["used_num"]+$num;
  96. $update["updatetime"]=date("Y-m-d H:i:s");
  97. return self::update($update,$data)->getNumRows();
  98. }
  99. /**
  100. * @param string $orderCode
  101. * @param int|array $stockid
  102. * @param int $num
  103. * @return int
  104. * @throws \think\Exception
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. static function OrderBn(string $orderCode, $stockid,int $num,int $isstock=1){
  110. $bnArr=self::SaleBn($stockid,$num,$isstock);
  111. if(empty($bnArr)) throw new Exception("未找到Bn库存数据",1006);
  112. $ordBn=[];
  113. foreach ($bnArr as $value){
  114. $saleBn=[
  115. "orderCode"=>$orderCode,
  116. "bnCode"=>$value['bnCode'],
  117. "stockid"=>$value['stockid'],
  118. "num"=>$value['num'],
  119. "send_num"=>$value['num'],
  120. "origin_price"=>$value['origin_price'],
  121. "addtime"=>date("Y-m-d H:i:s"),
  122. "updatetime"=>date("Y-m-d H:i:s"),
  123. ];
  124. $ordBn[]=$saleBn;
  125. }
  126. return self::name("sale_info")->insertAll($ordBn);
  127. }
  128. /**
  129. * @param string $returnCode
  130. * @param int $salebnid
  131. * @param int $num
  132. * @param int $type
  133. * @return bool
  134. * @throws \think\Exception
  135. */
  136. static function ReturnBn(string $returnCode,int $salebnid,int $num,int $type=1){
  137. $sabn=self::name("sale_info")->where(['id'=>$salebnid])->findOrEmpty()->toArray();
  138. if(empty($sabn))throw new Exception("未找到Bn订单数据",1006);
  139. $return=[
  140. "returnCode"=>$returnCode,
  141. "type"=>$type,
  142. "orderCode"=>$sabn['orderCode'],
  143. "bnCode"=>$sabn['bnCode'],
  144. "origin_price"=>$sabn['origin_price'],
  145. "num"=>$num,
  146. "addtime"=>date("Y-m-d H:i:s")
  147. ];
  148. return self::name("return_info")->insert($return);
  149. }
  150. /** 调拨专用 需要注意bn总数变化
  151. * @param int $stockid 库存id
  152. * @param string $bnCode bn编号
  153. * @param int $num 库存数量
  154. * @param int $flag 操作类型 0 添加库存 1 减少库存
  155. * @param float|int $origin_price 成本价
  156. * @return bool
  157. * @throws \think\Exception
  158. */
  159. static function bnStock(int $stockid,string $bnCode,int $num,int $flag=0,float $origin_price=0){
  160. $stock =self::where(["stockid"=>$stockid,"bnCode"=>$bnCode])->lock(true)->findOrEmpty()->toArray();
  161. $update=[];
  162. if($flag==1) {
  163. if(empty($stock)|| $stock['balance_num']< $num)
  164. throw new Exception("bn库存数不足",1006);
  165. else{
  166. $update['balance_num']=$stock['balance_num']-$num;
  167. $update['total_num']=$stock['total_num']-$num;
  168. $update['updatetime']=date("Y-m-d H:i:s");
  169. }
  170. }else{
  171. if (empty($stock)){
  172. $update=[
  173. "bnCode"=>$bnCode,
  174. "stockid"=>$stockid,
  175. "total_num"=>$num,
  176. "used_num"=>0,
  177. "balance_num"=>$num,
  178. "origin_price"=>$origin_price,
  179. "addtime"=>date("Y-m-d H:i:s"),
  180. "updatetime"=>date("Y-m-d H:i:s")
  181. ];
  182. }else{
  183. $update['balance_num']=$stock['balance_num']+$num;
  184. $update['total_num']=$stock['total_num']+$num;
  185. $update['updatetime']=date("Y-m-d H:i:s");
  186. }
  187. }
  188. return empty($stock)? self::insert($update): self::update($update,$stock)->getNumRows();
  189. }
  190. /**
  191. 退货退到其他仓库的话 原仓库批次总数 - 退货数量
  192. * @param $stockid 退货仓库
  193. * @param $bnCode bn批次编号
  194. * @param $num 退货数量
  195. * @param $origin_stockid 原仓库
  196. * @return int
  197. * @throws \think\Exception
  198. */
  199. static function ReturnAdd(int $stockid,string $bnCode,int $num,int $origin_stockid){
  200. $origin = self::where(["stockid"=>$origin_stockid,"bnCode"=>$bnCode])->findOrEmpty()->toArray();
  201. if($stockid!=$origin_stockid){
  202. if(empty($origin))throw new Exception("未找到Bn订单数据",1006);
  203. $temp['total_num'] =$origin['total_num'] -$num;
  204. $temp['used_num'] =$origin['used_num'] -$num;
  205. $temp['updatetime'] =date("Y-m-d H:i:s");
  206. self::update($temp,$origin);
  207. }
  208. $stock = self::where(["stockid"=>$stockid,"bnCode"=>$bnCode])->findOrEmpty()->toArray();
  209. if(empty($stock)){
  210. $update=[
  211. "bnCode"=>makeNo("BN"),
  212. "stockid"=>$stockid,
  213. "total_num"=>$num,
  214. "used_num"=>0,
  215. "balance_num"=>$num,
  216. "origin_price"=>$origin['origin_price'],
  217. "addtime"=>date("Y-m-d H:i:s"),
  218. "updatetime"=>date("Y-m-d H:i:s")
  219. ];
  220. $result= self::insert($update);
  221. }else{
  222. $update['balance_num']=$stock['balance_num']+$num;
  223. $stockid!=$origin_stockid ? $update['total_num']=$stock['total_num']+$num:$update['used_num']=$stock['used_num']-$num;
  224. $update['updatetime']=date("Y-m-d H:i:s");
  225. $result=self::update($update,$stock)->getNumRows();
  226. }
  227. return $result;
  228. }
  229. /**分单分配bn号
  230. * @param string $childCode 分单编号
  231. * @param int $stockid 库存id
  232. */
  233. static function ChildAddBn(string $childCode='',int $stockid=0){
  234. $child = OrderOutChild::where(["outChildCode" => $childCode, "is_del" => 0])->findOrEmpty()->toArray();
  235. if(empty($child))throw new Exception("未找到工单数据",1006);
  236. $bnArr=self::SaleBn($stockid,intval($child['num']));
  237. if(empty($bnArr)) throw new Exception("未找到Bn库存数据",1006);
  238. $ordBn=[];
  239. foreach ($bnArr as $value){
  240. $saleBn=[
  241. "orderCode"=>$child['orderCode'],
  242. "outCode"=>$child['outCode'],
  243. "childCode"=>$childCode,
  244. "bnCode"=>$value['bnCode'],
  245. "num"=>$value['num'],
  246. "total_bn_num"=>$value['num'],
  247. "origin_price"=>$value['origin_price'],
  248. "addtime"=>date("Y-m-d H:i:s"),
  249. "updatetime"=>date("Y-m-d H:i:s"),
  250. ];
  251. $ordBn[]=$saleBn;
  252. }
  253. return self::name("child_bn")->insertAll($ordBn);
  254. }
  255. /**
  256. * @param $childCode
  257. * @param $stockid
  258. * @param int $num
  259. * @return bool
  260. * @throws \think\Exception
  261. * @throws \think\db\exception\DataNotFoundException
  262. * @throws \think\db\exception\DbException
  263. * @throws \think\db\exception\ModelNotFoundException
  264. */
  265. static function ChildReturnBn($childCode,$stockid,$num=0){
  266. $childBN = self::name("child_bn")->where([["childCode","=",$childCode],["num",">=",0]])->select()->toArray();
  267. if(empty($childBN))throw new \Exception("未找到工单库存明细数据",1005);
  268. foreach ($childBN as $value){
  269. $bninfo =self::where(["bnCode"=>$value['bnCode'],"stockid"=>$stockid])->findOrEmpty()->toArray();
  270. if(empty($bninfo))throw new \Exception("未找到库存bn数据",1005);
  271. $temp=[];
  272. $bntmp=[];
  273. $temp['updatetime']=date("Y-m-d H:i:s");
  274. $bntmp['updatetime']=date("Y-m-d H:i:s");
  275. if($value["num"]<=$num){
  276. $temp['num']=0;
  277. $temp['is_del']=1;
  278. $bntmp['balance_num']= $bninfo['balance_num']+$value['num'];
  279. $bntmp['used_num']= $bninfo['used_num']-$value['num'];
  280. $num -=$value['num'];
  281. }else{
  282. $temp['num']=$value['num']-$num;
  283. $bntmp['balance_num']= $bninfo['balance_num']+$num;
  284. $bntmp['used_num']= $bninfo['used_num']-$num;
  285. $num=0;
  286. }
  287. $up = self::name("child_bn")->where('id',$value['id'])->update($temp);
  288. if($up==false)throw new \Exception("工单库存明细更新失败",1005);
  289. $bnup =self::update($bntmp,$bninfo)->getNumRows();
  290. if($bnup==false) throw new \Exception("库存bn更新失败",1005);
  291. if($num==0) break;
  292. }
  293. return true;
  294. }
  295. public static function StockBnSub($stockid,$num,$add=0){
  296. $arr = self::where([['balance_num','>',0]])->where(['stockid'=>$stockid])->select()->toArray();
  297. if(empty($arr)) throw new Exception('库存批次数量不足',1006);
  298. $list=[];
  299. foreach ($arr as $item) {
  300. $stock = self::name('good_stock')->where(['id'=>$item['stockid']])->field('id,usable_stock,wait_out_stock,spuCode')->find()->toArray();
  301. if($num<=0) break;
  302. $update=[];
  303. if($item['balance_num']<=$num){
  304. $update['balance_num'] =0;
  305. $update['used_num']= $item['used_num']+$item['balance_num'];
  306. $num-=$item['balance_num'];
  307. $update['updatetime'] =date('Y-m-d H:i:s');
  308. $temp=['stockid'=>$item['stockid'],'bnCode'=>$item['bnCode'],'spuCode'=>$stock['spuCode'],'num'=>$item['balance_num'],'origin_price'=>$item['origin_price']];
  309. $stockup=['usable_stock'=>$stock['usable_stock']-$item['balance_num'],'updatetime'=>date('Y-m-d H:i:s')];
  310. }else{
  311. $update['balance_num']=$item['balance_num']-$num;
  312. $update['used_num']=$item['used_num']+$num;
  313. $update['updatetime'] =date('Y-m-d H:i:s');
  314. $temp=['stockid'=>$item['stockid'],'bnCode'=>$item['bnCode'],'spuCode'=>$stock['spuCode'],'num'=>$num,'origin_price'=>$item['origin_price']];
  315. $stockup=['usable_stock'=>$stock['usable_stock']-$num,'updatetime'=>date('Y-m-d H:i:s')];
  316. $num=0;
  317. }
  318. $up=self::update($update,$item);
  319. if($up==false) throw new Exception('库存批次库存更新失败',1006);
  320. if($add==1){
  321. $stup=self::name('good_stock')->where($stock)->update($stockup);
  322. if($stup==false) throw new Exception('库存更新失败',1006);
  323. }
  324. $list[]=$temp;
  325. }
  326. return $list;
  327. }
  328. }