CombindStock.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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")],'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")->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. if($goodbasic['usable_stock']<$param['stock_num'])return error_show(1004,"商品库存数不足");
  49. $stock = Db::name("good_stock")->where(["spuCode"=>$param['spuCode'],"wsm_code"=>$param["wsm_code"]])->value("usable_stock",0);
  50. if($stock< $param['stock_num'])return error_show(1004,'商品库存数不足');
  51. }
  52. $combindList = $this->combind->where([["spuCode"=>$param["spuCode"],"is_del"=>0]])->select()->toArray();
  53. if(empty($combindList)) return error_show(1004,"未找到组合商品的商品组合信息");
  54. $childarr= Db::name('good')->whereIn('spuCode',array_unique(array_column($combindList,'childCode')))->column("spuCode,usable_stock,is_combind","spuCode");
  55. $stockCode=makeNo("CB");
  56. $this->combindStock->startTrans();
  57. try{
  58. $data=[
  59. "stockCode"=>$stockCode,
  60. "spuCode"=>$param["spuCode"],
  61. "wsm_code"=>$param["wsm_code"],
  62. "good_name"=>$goodbasic['good_name'],
  63. "flag"=>$param["flag"],
  64. "stock_num"=>$param['stock_num'],
  65. "apply_name"=>$this->uname,
  66. "apply_id"=>$this->uid,
  67. "status"=>1
  68. ];
  69. $up =$this->combindStock->save($data);
  70. if($up==false) throw new \Exception("创建失败");
  71. if($param["flag"]==0){
  72. $stockup = Db::name('good')->where('spuCode',$param['spuCode'])->dec('usable_stock',$param["stock_num"])
  73. ->update();
  74. if($stockup==false) throw new \Exception('组合商品库存占用失败');
  75. }else{
  76. foreach ($combindList as $item){
  77. if(!isset($childarr[$item['childCode']])) throw new \Exception('未找到组合商品的子商品信息');
  78. $num = bcmul($item['child_num'],$param['stock_num'],2);
  79. if($childarr[$item['childCode']]['usable_stock']<$num)throw new \Exception($item['childCode'].'组合商品的子商品库存不足');
  80. $stockup = Db::name("good")->where("spuCode",$item["childCode"])->dec("usable_stock",intval($num))
  81. ->update();
  82. if($stockup==false) throw new \Exception($item["childCode"].'组合商品的子商品信息库存占用失败');
  83. }
  84. }
  85. $this->combindStock->commit();
  86. }catch (\Exception $exception){
  87. $this->combindStock->rollback();
  88. return error_show(1004,$exception->getMessage());
  89. }
  90. return app_show(0,"数据创建成功",$stockCode);
  91. }
  92. public function info(){
  93. $param=$this->request->only(["id"=>""],"post","trim");
  94. $info =$this->combindStock->withJoin(["good","wsminfo"],"left")->with(['combindgood'])->findOrEmpty($param['id']);
  95. if($info->isEmpty())return error_show(1004,"未找到数据");
  96. if(!empty($info->combindgood)){
  97. foreach ($info->combindgood as &$item){
  98. $item['stock'] = bcmul($info->stock_num,$item['child_num']);
  99. }
  100. }
  101. return app_show(0,"获取成功",$info);
  102. }
  103. //status 1 待审核 2审核通过 3 审核驳回
  104. public function status(){
  105. $param=$this->request->only(['id'=>'',"status"=>"","remark"=>""],'post','trim');
  106. $valid=Validate::rule(["id|主键ID"=>"require","status|状态"=>"require|number|in:2,3"]);
  107. if($valid->check($param)==false)return error_show(1004,$valid->getError());
  108. $row = $this->combindStock->findOrEmpty($param['id']);
  109. if($row->status!=1)return error_show(1004,"数据状态有误");
  110. $goodinfo = (new \app\admin\model\GoodBasic())->where(['spuCode'=>$row->spuCode])->findOrEmpty();
  111. if($goodinfo->isEmpty())return error_show(1004,'未找到对应得组合商品信息');
  112. $comblist = (new GoodCombind())->where(["spuCode"=>$row->spuCode,"is_del"=>0])->select()->toArray();
  113. if(empty($comblist)) return error_show(1004,'组合商品子商品信息未找到');
  114. $row->status = $param['status'];
  115. $row->remark = $param['remark'];
  116. $row->startTrans();
  117. try{
  118. $up =$row->save();
  119. if($up==false) throw new \Exception("数据状态更新失败");
  120. if($param['status']==2){
  121. $GoodStock =new GoodStock();
  122. $stock =$GoodStock->where(["spuCode"=>$row->spuCode,"wsm_code"=>$row->wsm_code])->findOrEmpty();
  123. if($row->flag==1){
  124. $bn = makeNo('BN');
  125. if($stock->isEmpty()){
  126. $wsm_stock = [
  127. "spuCode"=>$row->spuCode,
  128. "wsm_code"=>$row->wsm_code,
  129. "usable_stock"=>$row->stock_num,
  130. "total_stock"=>$row->stock_num,
  131. ];
  132. $id =$stock->insertGetId($wsm_stock);
  133. if($id<=0) throw new \Exception('仓库库存数更新失败');
  134. }else{
  135. $wsm_stock=[
  136. 'usable_stock'=>bcadd($stock->usable_stock,$row->stock_num),
  137. 'total_stock'=>bcadd($stock->total_stock,$row->stock_num)
  138. ];
  139. $up=$stock->save($wsm_stock);
  140. if($up==false) throw new \Exception("仓库库存数更新失败");
  141. $id = $stock->id;
  142. }
  143. $good= (new \app\admin\model\Good())->where(["spuCode"=>$row->spuCode])->findOrEmpty();
  144. if($good->isEmpty()==false){
  145. $good->usable_stock=bcadd($good->usable_stock,$row->stock_num);
  146. $upe=$good->save();
  147. if($upe==false)throw new \Exception('商品可用库存数更新失败');
  148. }
  149. $origin = Db::name('good_nake')->where([['spuCode', '=', $row->spuCode], ['min_num', '<=',
  150. $row->stock_num], ['is_del', '=', 0]])->order('min_num desc')->find();
  151. if ($origin == false) {
  152. throw new \Exception( '未找到相关阶梯成本价格');
  153. }
  154. $origin_price = $origin['nake_total'];
  155. $stcoc=GoodStockInfo::AddBn($id,$bn,$row->stock_num,$origin_price);
  156. if($stcoc==false) throw new \Exception( '组合商品bn库存生成失败');
  157. $stockCodeLog= (new CombindStockInfo())->save(["stockCode"=>$row->stockCode,"flag"=>1,
  158. "stock_num"=>$row->stock_num,"bnCode"=>$bn,"origin_price"=>$origin_price]);
  159. if($stockCodeLog==false)throw new \Exception( '组合商品bn库存生成失败');
  160. $bnlist =$this->combind->AddStock($row->spuCode,$row->stock_num);
  161. (new CombindBninfo())->AaddBNLog($row->spuCode,$bn,$bnlist);
  162. }else{
  163. if($stock->usable_stock < $row->stock_num) throw new \Exception('仓库可用库数不足');
  164. $up =GoodStockInfo::SaleBn($stock->id,intval($row->stock_num));
  165. if(empty($up)) throw new \Exception('未找到对应子商品BN库存信息');
  166. foreach ($up as $item){
  167. $bnlist =$this->combind->subStock($row->spuCode,$item['bnCode'],$item['num']);
  168. if($bnlist==false)throw new \Exception('子商品BN库存信息更新失败');
  169. }
  170. }
  171. }
  172. if($param['status']==3){
  173. $good= (new \app\admin\model\Good())->where(['spuCode'=>$row->spuCode])->findOrEmpty();
  174. if($good->isEmpty()==false){
  175. if($row->flag==0){
  176. $good->usable_stock = bcadd($good->usable_stock ,$row->stock_num);
  177. $stockup = $good->save();
  178. if($stockup==false) throw new \Exception('组合商品库存解除驳回失败');
  179. }else{
  180. foreach ($comblist as $item){
  181. $num = bcmul($item['child_num'],$row->stock_num);
  182. $stockup = Db::name('good')->where('spuCode',$item['childCode'])->inc('usable_stock',intval
  183. ($num))->update();
  184. if($stockup==false) throw new \Exception('组合商品的子商品信息库存占用驳回失败');
  185. }
  186. }
  187. }
  188. }
  189. $row->commit();
  190. }catch (\Exception $exception){
  191. $row->rollback();
  192. return error_show(1004,$exception->getMessage());
  193. }
  194. return app_show(0,"数据更新成功");
  195. }
  196. }