MateSubGoods.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\MateGoodsLog;
  4. use think\App;
  5. use think\Db;
  6. class MateSubGoods extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->model = new \app\common\model\MateSubGoods();
  11. $this->table = $this->model->table;
  12. }
  13. public function index(){
  14. if(request()->isAjax()){
  15. //分页参数
  16. $length = input('rows',10,'intval'); //每页条数
  17. $page = input('page',1,'intval'); //第几页
  18. $start = ($page - 1) * $length; //分页开始位置
  19. //排序
  20. $sortRow = input('sidx','sort','trim'); //排序列
  21. $sort = input('sord','asc','trim'); //排序方式
  22. $order = $sortRow.' '.$sort.' ,id desc';
  23. $title = input('title','','trim');
  24. if($title){
  25. $goods_id = db('mate_goods')
  26. ->where('title','like','%'.$title.'%')
  27. ->column('id');
  28. $map[] = ['pid','in',$goods_id];
  29. }
  30. $enable = input('enable','','trim');
  31. if($enable != ''){
  32. $map[] = ['enable','=',$enable];
  33. }
  34. $map[] = ['org_id','=',$this->orgId];
  35. $map= empty($map) ? true: $map;
  36. //数据查询
  37. $lists =$this->model->getList($start,$length,$map,$order);
  38. //数据返回
  39. $totalCount = db($this->table)->where($map)->count();
  40. $totalPage = ceil($totalCount/$length);
  41. $result['page'] = $page;
  42. $result['total'] = $totalPage;
  43. $result['records'] = $totalCount;
  44. $result['rows'] = $lists;
  45. return json($result);
  46. }else{
  47. $this->assign('meta_title','物品列表');
  48. return $this->fetch();
  49. }
  50. }
  51. /**
  52. * 新增/编辑
  53. */
  54. public function add($id=0){
  55. $model = $this->model;
  56. if(request()->isPost()){
  57. $res = $model->updates();
  58. if($res){
  59. $this->success('操作成功',url('index'));
  60. }else{
  61. $this->error($model->getError());
  62. }
  63. }else{
  64. $meta_title = '新增物品';
  65. if($id){
  66. $info =db($this->table)->where('id',$id)->find();
  67. $this->assign('info',$info);
  68. $meta_title = '编辑物品';
  69. }
  70. $this->assign('meta_title',$meta_title);
  71. return $this->fetch();
  72. }
  73. }
  74. /**
  75. * 删除记录
  76. * @param int $id
  77. */
  78. public function del($id=0){
  79. if(!$id){
  80. $this->error('参数错误');
  81. }
  82. if(db('company_dispatch')->where('goods_id',$id)->find()){
  83. $this->error('该商品有入库或调拨记录,暂不能删除');
  84. }
  85. $res = db($this->table)->where('id',$id)->update(['del'=>1]);
  86. if($res){
  87. $this->success('删除成功');
  88. }else{
  89. $this->error('删除失败');
  90. }
  91. }
  92. /**
  93. * 改变字段值
  94. * @param int $fv
  95. * @param string $fn
  96. * @param int $fv
  97. */
  98. public function changeField($id=0,$fn='',$fv=0){
  99. if(!$fn||!$id){
  100. $this->error('参数错误');
  101. }
  102. $res = db($this->table)->where('id',$id)->update([$fn => $fv]);
  103. if($res){
  104. $this->success('操作成功');
  105. }else{
  106. $this->error('操作失败');
  107. }
  108. }
  109. /**
  110. * 处置
  111. */
  112. public function option($id=0){
  113. $model = new MateGoodsLog();
  114. if(request()->isPost()){
  115. $data = request()->post();
  116. if(empty($data['nums'])) $this->error('请输入数量');
  117. if(empty($data['remark'])) $this->error('请输入备注');
  118. $info = db('mate_sub_goods')
  119. ->where('id',$id)
  120. ->find();
  121. if($info['nums'] <$data['nums']) $this->error('库存数量不足');
  122. $sData = [
  123. 'org_id'=>$this->orgId,
  124. 'goods_id'=>$info['pid'],
  125. 'nums'=>$data['nums'],
  126. 'remark'=>$data['remark'],
  127. 'price'=>$info['price'],
  128. ];
  129. $res = $model->saveData($sData);
  130. db('mate_sub_goods')->where('id',$id)
  131. ->setDec('nums',$data['nums']);
  132. if($res){
  133. $this->success('操作成功');
  134. }else{
  135. $this->error($model->getError());
  136. }
  137. }else{
  138. $this->assign('id',$id);
  139. return $this->fetch();
  140. }
  141. }
  142. }