123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?php
- namespace app\admin\controller;
- use app\common\model\MateGoodsLog;
- use app\hander\HelpHander;
- use think\App;
- use think\Db;
- use think\Exception;
- class MateApplyUse extends Auth
- {
- public function __construct(App $app = null) {
- parent::__construct($app);
- $this->table = 'mate_apply_use';
- }
- //领取记录
- public function get(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','sort','trim'); //排序列
- $sort = input('sord','asc','trim'); //排序方式
- $order = $sortRow.' '.$sort.' ,id desc';
- $title = input('title','','trim');
- if($title){
- $map[] = ['sn','like','%'.$title.'%'];
- }
- // $orgs = mate_user($this->userId);
- // if(!empty($orgs)){
- // $map[] = ['org_id','in',$orgs];
- // }else{
- // $map[] = ['org_id','=',$this->orgId];
- // }
- $map[] = ['type','=',1];
- $map[] = ['org_id','=',$this->orgId];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = Db::name('mate_apply_use')
- ->where($map)
- ->limit($start,$length)
- ->order($order)
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['userName'] = Db::name('user')
- ->where('id',$v['user_id'])
- ->value('real_name');
- $lists[$k]['approverName'] = Db::name('user')
- ->where('id',$v['user_id'])
- ->value('real_name');
- }
- //数据返回
- $totalCount = Db::name('mate_apply_use')->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- // $isMateUser = is_mate_user($this->userId);
- // $this->assign('isMateUser',$isMateUser);
- $this->assign('meta_title','物品领取记录');
- return $this->fetch();
- }
- }
- //审核
- public function check($id=0){
- if(request()->isAjax()){
- $data = request()->post();
- $info = Db::name('mate_apply_use')->where('id',$id)->find();
- if(!$info){
- $this->error('记录不存在');
- }
- if($info['status'] != 0){
- $this->error('记录已处理');
- }
- $goods = Db::name('mate_apply_use_goods')->where('apply_id',$id)->select();
- if($data['status'] == 1){
- Db::startTrans();
- try{
- $data = [
- 'status'=>1,
- 'approver_user_id'=>$this->userId,
- 'approver_time'=>date('Y-m-d H:i:s')
- ];
- // 更改记录状态
- $cc = Db::name('mate_apply_use')->where('id',$info['id'])->update($data);
- if(!$cc){
- \exception('操作失败');
- }
- // 更新物品信息
- if($info['type'] == 1){ // 领取
- foreach ($goods as $k=>$v){
- $ginfo = Db::name('mate_apply_use')
- ->where('id',$v['goods_id'])
- ->where('org_id',cur_org_id())
- ->find();
- if($ginfo['consume'] == 2 && $ginfo['type'] == 2){ // 出库消耗
- $gs = [[
- 'goodsId' => $v['goods_id'],
- 'nums' => $v['nums'],
- 'price' => $v['price']
- ]];
- $ret = model('MateGoodsUse')->save1(3,$v['id'],$info['user_id'],cur_org_id(),$gs);
- if(!$ret){
- \exception('操作失败');
- }
- }else{ //出库到小包
- $uginfo = Db::name('mate_user_goods')
- ->where('user_id',$info['user_id'])
- ->where('org_id',$info['org_id'])
- ->where('goods_id',$v['goods_id'])
- ->find();
- if($uginfo){
- Db::name('mate_user_goods')
- ->where('id',$uginfo['id'])
- ->update([
- 'nums'=>$uginfo['nums'] + $v['nums'],
- 'price' => round(($v['nums']*$v['price'] + $uginfo['nums']*$uginfo['price'])/($uginfo['nums'] + $v['nums']),2)
- ]);
- }else{
- $dd = Db::name('mate_user_goods')
- ->insert([
- 'org_id'=>cur_org_id(),
- 'user_id'=>$info['user_id'],
- 'goods_id'=>$v['goods_id'],
- 'nums'=>$v['nums'],
- 'price'=>$v['price'],
- ]);
- if(!$dd){
- \exception('操作失败');
- }
- }
- }
- }
- }else{ //归还
- foreach ($goods as $k=>$v){
- $ginfo = Db::name('mate_goods')
- ->where('id',$v['goods_id'])
- ->where('org_id',cur_org_id())
- ->find();
- $cc = Db::name('mate_goods')
- ->where('id',$ginfo['id'])
- ->update([
- 'nums'=>$ginfo['nums'] + $v['nums'],
- 'price' => round(($v['nums']*$v['price'] + $ginfo['nums']*$ginfo['price'])/($ginfo['nums'] + $v['nums']),2)
- ]);
- if(!$cc){
- \exception('操作失败');
- }
- }
- }
- Db::commit();
- }catch (\Exception $e){
- $this->error($e->getMessage());
- Db::rollback();
- }
- $this->success('操作成功');
- }elseif ($data['status'] == 2){
- Db::startTrans();
- try{
- $aa = Db::name('mate_apply_use')
- ->where('id',$info['id'])
- ->update([
- 'status' =>2,
- 'approver_user_id'=>$this->userId,
- 'approver_time'=>date('Y-m-d H:i:s')
- ]);
- // 更改记录状态
- if(!$aa){
- \exception('操作失败');
- }
- // 更新物品信息
- if($info['type'] == 1){ // 领取反库
- foreach ($goods as $k=>$v){
- $ginfo = Db::name('mate_goods')
- ->where('org_id',cur_org_id())
- ->where('id',$v['goods_id'])
- ->find();
- $b = Db::name('mate_goods')
- ->where('id',$ginfo['id'])
- ->update([
- 'nums'=>$ginfo['nums'] + $v['nums'],
- 'price'=>round(($v['nums'] * $v['price'] + $ginfo['nums'] * $ginfo['price'])/($ginfo['nums'] + $v['nums']),2),
- ]);
- if(!$b){
- \exception('操作失败');
- }
- }
- }else{ //归还小包
- foreach ($goods as $k=>$v){
- $uginfo = Db::name('mate_user_goods')
- ->where('goods_id',$v['goods_id'])
- ->where('org_id',cur_org_id())
- ->where('user_id',$info['user_id'])
- ->find();
- $c = Db::name('mate_user_goods')
- ->where('id',$uginfo['id'])
- ->update([
- 'nums'=>$uginfo['nums'] + $v['nums'],
- 'price' => round(($v['nums']*$v['price'] + $uginfo['nums']*$uginfo['price'])/($uginfo['nums'] + $v['nums']),2)
- ]);
- if(!$c){
- \exception('操作失败');
- }
- }
- }
- Db::commit();
- }catch (Exception $e){
- $this->error($e->getMessage());
- Db::rollback();
- }
- $this->success('操作成功');
- }
- }else{
- $this->assign('id',$id);
- return $this->fetch();
- }
- }
- //归还
- public function back(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','sort','trim'); //排序列
- $sort = input('sord','asc','trim'); //排序方式
- $order = $sortRow.' '.$sort.' ,id desc';
- $title = input('title','','trim');
- if($title){
- $map[] = ['sn','like','%'.$title.'%'];
- }
- $map[] = ['type','=',2];
- // $orgs = mate_user($this->userId);
- // if(!empty($orgs)){
- // $map[] = ['org_id','in',$orgs];
- // }else{
- // $map[] = ['org_id','=',$this->orgId];
- // }
- $map[] = ['org_id','=',$this->orgId];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = Db::name('mate_apply_use')
- ->where($map)
- ->limit($start,$length)
- ->order($order)
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['userName'] = Db::name('user')
- ->where('id',$v['user_id'])
- ->value('real_name');
- $lists[$k]['approverName'] = Db::name('user')
- ->where('id',$v['user_id'])
- ->value('real_name');
- }
- //数据返回
- $totalCount = Db::name('mate_apply_use')->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- // $isMateUser = is_mate_user($this->userId);
- // $this->assign('isMateUser',$isMateUser);
- $this->assign('meta_title','物品归还记录');
- return $this->fetch();
- }
- }
- public function detail($id){
- $info = (new \app\common\model\MateApplyUse())->info($id);
- if(!$info) $this->error('记录不存在');
- $this->assign('info',$info);
- return $this->fetch();
- }
- }
|