post(); $data['org_id'] = cur_org_id(); $result = validate('AssetDisposal')->check($data,[],''); if(true !== $result){ $this->error = validate('AssetDisposal')->getError(); return false; } $data['agent_dep_id'] = Db::name('user_dep')->where('user_id',$data['agent_user_id'])->value('dep_id'); $id = $data['id']; unset($data['id']); if($id > 0){ $data['update_time'] = date('Y-m-d H:i:s'); unset($data['items']); $ret = $this->allowField(true)->save($data,['id'=>$id]); if(!$ret){ $this->error = '操作失败'; return false; } }else{ $data['create_time'] = date('Y-m-d H:i:s'); Db::startTrans(); try{ $data['sn'] = $this->getSn($data['org_id']); $items = $data['items']; unset($data['items']); $receiveId = Db::name('AssetDisposal')->insertGetId($data); if(!$receiveId){ \exception('操作失败'); } $d = []; $itemids = []; foreach ($items as $k=>$v){ $iteminfo = Db::name('asset_items') ->where('id',$v['id']) ->field('id,user_id,dep_id') ->find(); $d[] = [ 'disposal_id' => $receiveId, 'items_id' => $v['id'], 'user_id' => $iteminfo['user_id'], 'dep_id' => $iteminfo['dep_id'], ]; $itemids[] = $v['id']; } $res = Db::name('asset_disposal_items')->insertAll($d); if($res != count($d)){ \exception('操作失败'); } $sd = [ 'enable' => 5, 'update_time' => date('Y-m-d H:i:s') ]; $res = Db::name('asset_items')->where('id','in',$itemids)->update($sd); if($res != count($itemids)){ \exception('操作失败'); } Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } } return true; } public function getSn($orgId){ $max = Db::name('asset_disposal') ->where('org_id',$orgId) ->where('create_time','>=',date('Y-m-d').' 00:00:00') ->order('id desc') ->value('sn'); $maxinit = $max?$max:date('Ymd').'0000'; return $maxinit + 1; } public function info($id){ $info = $this->where('id',$id)->find(); if(!$info){ $this->error = '数据不存在'; return false; } $data = $info->toArray(); $data['agentDepName'] = Db::name('dep')->where('id',$info['agent_dep_id'])->value('title'); $data['agentUserName'] = Db::name('user')->where('id',$info['agent_user_id'])->value('real_name'); $data['way_name'] = Db::name('asset_way')->where('id',$info['way_id'])->value('title'); $items = Db::name('asset_disposal_items') ->alias('ari') ->join('asset_items ai','ai.id = ari.items_id') ->join('asset_unit au','au.id = ai.unit_id') ->where('ari.disposal_id',$id) ->field('ai.*,au.title as unitName') ->select(); $data['items'] = $items?$items:[]; $items = Db::name('asset_disposal_items') ->alias('ari') ->join('asset_items ai','ai.id = ari.items_id') ->join('asset_unit au','au.id = ai.unit_id') ->where('ari.disposal_id',$id) ->field('ai.*,au.title as unitName,ari.user_id as old_user_id,ari.dep_id as old_dep_id') ->select(); $total = 0; foreach ($items as $k=>$v){ $items[$k]['userName'] = Db::name('user')->where('id',$v['old_user_id'])->value('real_name'); $items[$k]['depName'] = Db::name('dep')->where('id',$v['old_dep_id'])->value('title'); $total +=$v['price']; } $data['items'] = $items?$items:[]; $data['total'] = $total; return $data; } }