CombindStock.php 13 KB

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