CombindStock.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\model\CombindBninfo;use app\admin\model\CombindStockInfo;use app\admin\model\GoodCombind;use app\admin\model\GoodStock;use app\admin\model\GoodStockInfo;use think\App;use think\facade\Db;use think\facade\Validate;
  5. class CombindStock extends Base
  6. {
  7. private $combind ;
  8. private $combindStock ;
  9. private $combindStockInfo ;
  10. private $combindBninfo;
  11. public function __construct(App $app) {
  12. parent::__construct($app);
  13. $this->combindStock =new \app\admin\model\CombindStock();
  14. $this->combindStockInfo =new \app\admin\model\CombindStockInfo();
  15. $this->combindBninfo =new \app\admin\model\CombindBninfo();
  16. $this->combind =new \app\admin\model\GoodCombind();
  17. }
  18. public function list(){
  19. $param=$this->request->only(['spuCode'=>'','wsm_code'=>'','good_name'=>'','cat_id'=>'','status'=>'','flag'=>'',
  20. "create_start"=>"2023-01-01 00:00:00","create_end"=>date("Y-m-d H:i:s"),"page"=>1,"size"=>15],'post','trim');
  21. $where=[["createtime","between",[$param['create_start'],$param['create_end']]]];
  22. $param['spuCode']?? $where[]=["spuCode","like","%{$param['spuCode']}%"];
  23. $param['wsm_code']?? $where[]=["wsm_code","like","%{$param['wsm_code']}%"];
  24. $param['good_name']?? $where[]=["good_name","like","%{$param['good_name']}%"];
  25. $param['cat_id']?? $where[]=["good_basic.cat_id","=",$param['cat_id']];
  26. $list = $this->combindStock->withJoin(["wsminfo","good"],"left")->order("id desc")->paginate
  27. (["page"=>$param['page'],"list_rows"=>$param['size']]);
  28. return app_show(0,"获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  29. }
  30. /**@param spuCode 商品编号
  31. **@param wsm_code 仓库编号
  32. **@param stock_num 库存数量
  33. **@param flag 0 减库存 1 加库存
  34. * @return \think\response\Json|void
  35. */
  36. public function create(){
  37. $param =$this->request->only(["spuCode"=>"","wsm_code"=>"","stock_num"=>"",'flag'=>''],"post","trim");
  38. $valid =Validate::rule([
  39. "spuCode|商品编号"=>"require",
  40. "wsm_code|仓库编号"=>"require",
  41. "stock_num|库存数量"=>"require|number|gt:0",
  42. "flag|库存变化标识"=>"require|number|in:0,1"]);
  43. if($valid->check($param)==false)return error_show(1004,$valid->getError());
  44. $goodbasic =Db::name("good_basic")->where("spuCode",$param["spuCode"])->find();
  45. if($goodbasic==false) return error_show(1004,"未找到组合商品信息");
  46. if($goodbasic['is_combind']==0) return error_show(1004,'商品不是组合商品');
  47. if($param['flag']==0){//减库存 先判断库存是否足够
  48. $stock = (new GoodStock())->where(["spuCode"=>$param['spuCode'],"wsm_code"=>$param["wsm_code"]])
  49. ->findOrEmpty();
  50. if($stock->isEmpty())return error_show(1004,'商品库存信息为找到');
  51. if($stock->usable_stock< $param['stock_num'])return error_show(1004,'商品库存数不足');
  52. $isT = (new \app\admin\model\Good())->where(['spuCode'=>$param['spuCode']])->findOrEmpty();
  53. if($isT->isEmpty()==false){
  54. if($isT->usable_stock< $param['stock_num'])return error_show(1004,'商品库存数不足');
  55. }
  56. }
  57. $combindList = $this->combind->where(["spuCode"=>$param["spuCode"],"is_del"=>0])->select()->toArray();
  58. if(empty($combindList)) return error_show(1004,"未找到组合商品的商品组合信息");
  59. $childarr= Db::name('good')->whereIn('spuCode',array_unique(array_column($combindList,'childCode')))->column("spuCode,usable_stock,is_combind","spuCode");
  60. $stockCode=makeNo("CB");
  61. $this->combindStock->startTrans();
  62. try{
  63. $data=[
  64. "stockCode"=>$stockCode,
  65. "spuCode"=>$param["spuCode"],
  66. "wsm_code"=>$param["wsm_code"],
  67. "good_name"=>$goodbasic['good_name'],
  68. "flag"=>$param["flag"],
  69. "bnCode"=>$param['flag']==1?makeNo("BN"):"",
  70. "stock_num"=>$param['stock_num'],
  71. "apply_name"=>$this->uname,
  72. "apply_id"=>$this->uid,
  73. "status"=>1
  74. ];
  75. $up =$this->combindStock->save($data);
  76. if($up==false) throw new \Exception("创建失败");
  77. if($param["flag"]==0){
  78. if($isT->isEmpty()==false){
  79. $stockup = Db::name('good')->where('spuCode',$param['spuCode'])->dec('usable_stock',$param["stock_num"])->update();
  80. if($stockup==false) throw new \Exception('组合商品库存占用失败');
  81. }
  82. $stock->usable_stock = bcsub($stock->usable_stock,$param['stock_num']);
  83. $stock->total_stock = bcsub($stock->total_stock ,$param['stock_num']);
  84. $reld=$stock->save();
  85. if($reld==false) throw new \Exception('组合商品库存占用失败');
  86. }else{
  87. $bnArr=[];
  88. foreach ($combindList as $item){
  89. $num = bcmul($item['child_num'],$param['stock_num'],2);
  90. if(isset($childarr[$item['childCode']])) {
  91. if($childarr[$item['childCode']]['usable_stock']<$num)throw new \Exception($item['childCode'].'组合商品的子商品库存不足');
  92. $stockup = Db::name('good')->where('spuCode',$item['childCode'])->dec('usable_stock',intval($num))
  93. ->update();
  94. if($stockup==false) throw new \Exception($item['childCode'].'组合商品的子商品信息库存占用失败');
  95. }
  96. $bnlist= (new GoodStock())->stockSub($item['childCode'],$num);
  97. $bnArr=array_merge($bnArr,$bnlist);
  98. }
  99. if(empty($bnArr)) throw new \Exception('组合商品的子商品信息库存占用失败');
  100. (new CombindBninfo())->AaddBNLog($param['spuCode'],$data['bnCode'],$bnArr);
  101. }
  102. $this->combindStock->commit();
  103. }catch (\Exception $exception){
  104. $this->combindStock->rollback();
  105. return error_show(1004,$exception->getMessage());
  106. }
  107. return app_show(0,"数据创建成功",$stockCode);
  108. }
  109. public function info(){
  110. $param=$this->request->only(["id"=>""],"post","trim");
  111. $info =$this->combindStock->withJoin(["good","wsminfo"],"left")->with(['combindgood'])->findOrEmpty($param['id']);
  112. if($info->isEmpty())return error_show(1004,"未找到数据");
  113. if(!empty($info->combindgood)){
  114. foreach ($info->combindgood as &$item){
  115. $item['stock'] = bcmul($info->stock_num,$item['child_num']);
  116. }
  117. }
  118. return app_show(0,"获取成功",$info);
  119. }
  120. //status 1 待审核 2审核通过 3 审核驳回
  121. public function status(){
  122. $param=$this->request->only(['id'=>'',"status"=>"","remark"=>""],'post','trim');
  123. $valid=Validate::rule(["id|主键ID"=>"require","status|状态"=>"require|number|in:2,3"]);
  124. if($valid->check($param)==false)return error_show(1004,$valid->getError());
  125. $row = $this->combindStock->findOrEmpty($param['id']);
  126. if($row->status!=1)return error_show(1004,"数据状态有误");
  127. $goodinfo = (new \app\admin\model\GoodBasic())->where(['spuCode'=>$row->spuCode])->findOrEmpty();
  128. if($goodinfo->isEmpty())return error_show(1004,'未找到对应得组合商品信息');
  129. $comblist = (new GoodCombind())->where(["spuCode"=>$row->spuCode,"is_del"=>0])->select()->toArray();
  130. if(empty($comblist)) return error_show(1004,'组合商品子商品信息未找到');
  131. $good= (new \app\admin\model\Good())->where(['spuCode'=>$row->spuCode])->findOrEmpty();
  132. $GoodStock =new GoodStock();
  133. $stock =$GoodStock->where(['spuCode'=>$row->spuCode,'wsm_code'=>$row->wsm_code])->findOrEmpty();
  134. $row->status = $param['status'];
  135. $row->remark = $param['remark'];
  136. $row->startTrans();
  137. try{
  138. $up =$row->save();
  139. if($up==false) throw new \Exception("数据状态更新失败");
  140. if($param['status']==2){
  141. if($row->flag==1){
  142. $bn = $row->bnCode;
  143. if($stock->isEmpty()){
  144. $wsm_stock = [
  145. "spuCode"=>$row->spuCode,
  146. "wsm_code"=>$row->wsm_code,
  147. "usable_stock"=>$row->stock_num,
  148. "total_stock"=>$row->stock_num,
  149. ];
  150. $id =$stock->insertGetId($wsm_stock);
  151. if($id<=0) throw new \Exception('仓库库存数更新失败');
  152. }else{
  153. $wsm_stock=[
  154. 'usable_stock'=>bcadd($stock->usable_stock,$row->stock_num),
  155. 'total_stock'=>bcadd($stock->total_stock,$row->stock_num)
  156. ];
  157. $up=$stock->save($wsm_stock);
  158. if($up==false) throw new \Exception("仓库库存数更新失败");
  159. $id = $stock->id;
  160. }
  161. if($good->isEmpty()==false){
  162. $good->usable_stock=bcadd($good->usable_stock,$row->stock_num);
  163. $upe=$good->save();
  164. if($upe==false)throw new \Exception('商品可用库存数更新失败');
  165. }
  166. $origin = Db::name('good_nake')->where([['spuCode', '=', $row->spuCode], ['min_num', '<=',
  167. $row->stock_num], ['is_del', '=', 0]])->order('min_num desc')->find();
  168. if ($origin == false) {
  169. throw new \Exception( '未找到相关阶梯成本价格');
  170. }
  171. $origin_price = $origin['nake_total'];
  172. $stcoc=GoodStockInfo::AddBn(intval($id),$bn,intval($row->stock_num),floatval($origin_price) );
  173. if($stcoc==false) throw new \Exception( '组合商品bn库存生成失败');
  174. $stockCodeLog= (new CombindStockInfo())->save(["stockCode"=>$row->stockCode,"flag"=>1,
  175. "stock_num"=>$row->stock_num,"bnCode"=>$bn,"origin_price"=>$origin_price]);
  176. if($stockCodeLog==false)throw new \Exception( '组合商品bn库存生成失败');
  177. }
  178. else{
  179. $up =GoodStockInfo::StockBnSub($stock->id,intval($row->stock_num));
  180. if(empty($up)) throw new \Exception('未找到对应子商品BN库存信息');
  181. foreach ($up as $item){
  182. $bnlist =$this->combind->subStock($row->spuCode,$row->bnCode,$item['num']);
  183. if($bnlist==false)throw new \Exception('子商品BN库存信息更新失败');
  184. }
  185. }
  186. }
  187. if($param['status']==3){
  188. if($row->flag==0){
  189. if($good->isEmpty()==false){
  190. $good->usable_stock = bcadd($good->usable_stock ,$row->stock_num);
  191. $stockup = $good->save();
  192. if($stockup==false) throw new \Exception('组合商品库存解除驳回失败');
  193. }
  194. $stock->usable_stock = bcadd($stock->usable_stock,$row->stock_num);
  195. $stock->total_stock = bcadd($stock->total_stock ,$row->stock_num);
  196. $reld=$stock->save();
  197. if($reld==false) throw new \Exception('组合商品库存解除驳回失败');
  198. }else{
  199. $bnlist =$this->combind->subStock($row->spuCode,$row->bnCode,$row->stock_num);
  200. if($bnlist==false)throw new \Exception('子商品BN库存信息更新失败');
  201. }
  202. }
  203. $row->commit();
  204. }catch (\Exception $exception){
  205. $row->rollback();
  206. return error_show(1004,$exception->getMessage());
  207. }
  208. return app_show(0,"数据更新成功");
  209. }
  210. }