123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AssetDisposal extends Model
- {
- public function updates(){
- $data = request()->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;
- }
- }
|