CombindStock.php 12 KB

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