MateCheck.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\MateGoodsLog;
  4. use app\common\util\ExcelUtil;
  5. use think\App;
  6. use think\Db;
  7. use think\Exception;
  8. class MateCheck extends Auth
  9. {
  10. public function __construct(App $app = null) {
  11. parent::__construct($app);
  12. $this->model = new \app\common\model\MateCheck();
  13. $this->table = $this->model->table;
  14. }
  15. public function index(){
  16. if(request()->isAjax()){
  17. //分页参数
  18. $length = input('rows',10,'intval'); //每页条数
  19. $page = input('page',1,'intval'); //第几页
  20. $start = ($page - 1) * $length; //分页开始位置
  21. //排序
  22. $sortRow = input('sidx','id','trim'); //排序列
  23. $sort = input('sord','desc','trim'); //排序方式
  24. $order = $sortRow.' '.$sort;
  25. $enable = input('enable','','trim');
  26. if($enable != ''){
  27. $map[] = ['status','=',$enable];
  28. }
  29. $map[] = ['del','=',0];
  30. $map[] = ['org_id','=',$this->orgId];
  31. $map= empty($map) ? true: $map;
  32. //数据查询
  33. $lists =db($this->table)
  34. ->where($map)->limit($start,$length)
  35. ->order($order)->select();
  36. $lists = $lists?$lists:[];
  37. foreach ($lists as $k=>$v){
  38. $lists[$k]['userName'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  39. }
  40. //数据返回
  41. $totalCount = db($this->table)->where($map)->count();
  42. $totalPage = ceil($totalCount/$length);
  43. $result['page'] = $page;
  44. $result['total'] = $totalPage;
  45. $result['records'] = $totalCount;
  46. $result['rows'] = $lists;
  47. return json($result);
  48. }else{
  49. $this->assign('meta_title','盘库列表');
  50. return $this->fetch();
  51. }
  52. }
  53. /**
  54. * 新增/编辑
  55. */
  56. public function add(){
  57. if(request()->isPost()){
  58. $res = $this->model->updates($this->userId);
  59. if($res){
  60. $this->success('操作成功',url('index'));
  61. }else{
  62. $this->error($this->model->getError());
  63. }
  64. }else{
  65. $meta_title = '新增盘库';
  66. $this->assign('meta_title',$meta_title);
  67. return $this->fetch();
  68. }
  69. }
  70. //详情
  71. public function info($id){
  72. $info = $this->model->getInfo($id);
  73. if(!$info) $this->error('记录不存在');
  74. $this->assign('info',$info);
  75. return $this->fetch();
  76. }
  77. //excel导出
  78. public function export($id=0) {
  79. set_time_limit(0);
  80. ini_set("memory_limit","1024M");
  81. $info = $this->model->getInfo($id);
  82. $lists = $info['goods']?$info['goods']:[];
  83. foreach ($lists as $k=>$v){
  84. $lists[$k]['check_nums'] = "";
  85. $lists[$k]['cy_nums'] = "";
  86. $lists[$k]['check_time'] = "";
  87. $lists[$k]['remark'] = "";
  88. }
  89. $header = [
  90. ['title' => '名称', 'name' => 'title','width'=>'20'],
  91. ['title' => '规格', 'name' => 'spec','width'=>'20'],
  92. ['title' => '品牌', 'name' => 'brand','width'=>'20'],
  93. ['title' => '单价', 'name' => 'price','width'=>'20'],
  94. ['title' => '应盘数量', 'name' => 'nums','width'=>'20'],
  95. ['title' => '实盘数量', 'name' => 'check_nums','width'=>'20'],
  96. ['title' => '数量差异', 'name' => 'cy_nums','width'=>'20'],
  97. ['title' => '盘点时间', 'name' => 'check_time','width'=>'20'],
  98. ['title' => '盘点说明', 'name' => 'remark','width'=>'20'],
  99. ];
  100. $filename = '盘库';
  101. ExcelUtil::export($filename,$header,$lists);
  102. }
  103. public function check($id=0){
  104. if(request()->isPost()){
  105. $res = $this->model->checkGoods($this->userId);
  106. if($res){
  107. $this->success('操作成功',url('index'));
  108. }else{
  109. $this->error($this->model->getError());
  110. }
  111. }else{
  112. $this->assign('id',$id);
  113. return $this->fetch();
  114. }
  115. }
  116. public function delGoods($id=0){
  117. $info = Db::name('mate_check_goods')->where('id',$id)->where('del',0)->find();
  118. if(!$info){
  119. $this->error('记录不存在');
  120. }
  121. $ret = Db::name('mate_check_goods')->where('id',$id)->update(['user_id'=>$this->userId,'del'=>1]);
  122. if($ret){
  123. $this->success('操作成功');
  124. }else{
  125. $this->error('操作失败');
  126. }
  127. }
  128. public function del($id=0){
  129. $info = Db::name('mate_check')->where('id',$id)->where('del',0)->find();
  130. if(!$info){
  131. $this->error('记录不存在');
  132. }
  133. Db::startTrans();
  134. try{
  135. $ret = Db::name('mate_check')->where('id',$id)->update(['del_user'=>$this->userId,'del'=>1,'del_time'=>date('Y-m-d H:i:s')]);
  136. if(!$ret){
  137. \exception('操作成功');
  138. }
  139. Db::name('mate_check_goods')->where('check_id',$id)->update(['user_id'=>$this->userId,'del'=>1]);
  140. Db::commit();
  141. }catch (Exception $e){
  142. Db::rollback();
  143. $this->error('操作失败');
  144. }
  145. $this->success('操作成功');
  146. }
  147. public function finish($id=0){
  148. $info = Db::name('mate_check')->where('id',$id)->where('del',0)->find();
  149. if(!$info){
  150. $this->error('记录不存在');
  151. }
  152. if($info['status'] != 0){
  153. $this->error('盘库已完成');
  154. }
  155. $ginfo = Db::name('mate_check_goods')->where('check_id',$id)->where('status',0)->where('del',0)->find();
  156. if($ginfo){
  157. $this->error('还有物品未盘库');
  158. }
  159. Db::startTrans();
  160. try{
  161. $ret = Db::name('mate_check')->where('id',$id)->update(['status'=>1,'finish_time'=>date('Y-m-d H:i:s')]);
  162. if(!$ret){
  163. \exception("操作失败");
  164. }
  165. $goodsList = Db::name('mate_check_goods')
  166. ->where('check_id',$id)
  167. ->where('del',0)
  168. ->where('status','>',0)
  169. ->select();
  170. $goodsList = $goodsList?$goodsList:[];
  171. foreach ($goodsList as $k=>$v){
  172. $save = Db::name('mate_goods')
  173. ->where('id',$v['goods_id'])
  174. ->update(['nums'=>$v['check_nums'],'update_time'=>date("Y-m-d H:i:s")]);
  175. if(!$save){
  176. \exception("操作失败");
  177. }
  178. }
  179. Db::commit();
  180. }catch (\Exception $e){
  181. Db::rollback();
  182. $this->error('操作失败');
  183. }
  184. $this->success('操作成功');
  185. }
  186. }