input('id/d',0), 'intro' => input('intro','','trim'), 'sn' => input('sn','','trim'), 'remark' => input('remark','','trim'), 'file' => input('file','','trim'), 'imgs' => input('imgs','','trim'), 'org_id' => input('orgId/d',0), 'sign_date' => input('signDate',null,'trim'), 'remind' => input('remind/d',0), 'remind_date' => input('remindDate',null,'trim'), 'start_time' => $dateRange[0], 'end_time' => $dateRange[1], 'contract_id1' => input('contractId1/d',0), 'contract_id2' => input('contractId2/d',0), 'tenant_id' => input('tenantId/d',0), 'pay_id' => input('payId/d',0), 'level_id' => input('levelId/d',0), 'price_id' => input('priceId/d',0), 'price' => input('price/f',0), 'year_money' => input('yearMoney/f',0), 'amount' => input('amount/f',0), 'margin' => input('margin/f',0), 'day_money1' => input('dayMoney1/f',0), 'day_money2' => input('dayMoney2/f',0), 'change_price' => input('changePrice','','trim'), 'lessee_id' => input('lesseeId/d',0), 'houses' => input('houses','','trim'), 'release' => input('release/d',0) ]; $logdata = json_encode($data); $scene = 'add'; if($data['id'] > 0){ $scene = 'edit'; } $result = validate('HouseContract')->check($data,[],$scene); if(true !== $result){ HelpHander::error(validate('HouseContract')->getError()); } if(!$data['remind_date']){ unset($data['remind_date']); } Db::startTrans(); try{ $id = $data['id']; $houses = array_unique(explode(',',$data['houses'])); unset($data['id']); unset($data['houses']); if($id > 0){ $info = Db::name('house_contract')->where('id',$id)->find(); if($info['lessee_id'] > 0){ unset($data['lessee_id']); } $data['update_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data,['id'=>$id]); if(!$ret){ \exception('操作失败'); } if($info['lessee_id'] == 0 && $houses){ $sdata = []; foreach ($houses as $v){ $sdata[] = [ 'contract_id' => $id, 'house_id' => $v ]; } $res = Db::name('house_contract_relation')->insertAll($sdata); if($res != count($houses)){ \exception('操作失败'); } // 房屋当前合同设置 $dd = [ 'cur_lessee_id' => $data['lessee_id'], 'cur_contract_id' => $id ]; $retc = Db::name('house')->where('id','in',$houses)->update($dd); if($retc != count($houses)){ \exception('操作失败'); } } }else{ $data['user_id'] = input('userId/d',0); $data['create_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data); if(!$ret){ \exception('操作失败'); } $id = $this->id; if($houses){ $sdata = []; foreach ($houses as $v){ $sdata[] = [ 'contract_id' => $id, 'house_id' => $v ]; } $res = Db::name('house_contract_relation')->insertAll($sdata); if($res != count($houses)){ \exception('操作失败'); } // 房屋当前合同设置 $dd = [ 'cur_lessee_id' => $data['lessee_id'], 'cur_contract_id' => $id ]; $retc = Db::name('house')->where('id','in',$houses)->update($dd); if($retc != count($houses)){ \exception('操作失败'); } } } Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error('操作失败'.$e->getMessage()); } if($id > 0){ $content = '修改经营性房产-合同'; }else{ $content = '添加经营性房产-合同'; } model('ActionLog')->add(15,$content,0,$logdata); return true; } public function del($id){ $info = Db::name('house_contract')->where('id',$id)->where('del',0)->find(); if(!$info){ HelpHander::error('合同已删除'); } Db::startTrans(); try{ $ret = Db::name('house_contract')->where('id',$id)->setField('del',1); if(!$ret){ \exception('操作失败'); } $houses = Db::name('house_contract_relation')->where('contract_id',$id)->column('house_id'); $dd = [ 'cur_lessee_id' => 0, 'cur_contract_id' => 0 ]; Db::name('house')->where('id','in',$houses)->update($dd); Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error($e->getMessage()); } $logdata = json_encode(['id' => $id]); model('ActionLog')->add(15,'删除经营性房产-合同',0,$logdata); return true; } // 终止合同 public function stop($id){ $info = Db::name('house_contract')->where('id',$id)->where('del',0)->find(); if(!$info){ HelpHander::error('记录不存在'); } if($info['status'] == 2){ HelpHander::error('合同已终止'); } $curTime = date('Y-m-d'); if($info['end_time'] < $curTime){ HelpHander::error('合同已过期无需终止'); } Db::startTrans(); try{ $ret = Db::name('house_contract')->where('id',$id)->update(['status' => 2,'update_time'=>date('Y-m-d H:i:s')]); if(!$ret){ \exception('操作失败'); } $houses = Db::name('house_contract_relation')->where('contract_id',$id)->column('house_id'); $dd = [ 'cur_lessee_id' => 0, 'cur_contract_id' => 0 ]; Db::name('house')->where('id','in',$houses)->update($dd); Db::commit(); }catch (Exception $e){ Db::rollback(); HelpHander::error($e->getMessage()); } return true; } public function info($id){ $info = Db::name('house_contract')->where('id',$id)->where('del',0)->find(); if(!$info){ HelpHander::error('记录不存在'); } $curTime = date('Y-m-d'); if($info['end_time'] < $curTime){ $info['status'] = 3; //已到期 } if($info['lessee_id'] > 0){ $info['priceName'] = Db::name('house_select')->where('id',$info['price_id'])->value('title'); $info['levelName'] = Db::name('house_level')->where('id',$info['level_id'])->value('title'); $info['payName'] = Db::name('house_select')->where('id',$info['pay_id'])->value('title'); $info['tenantName'] = Db::name('house_select')->where('id',$info['tenant_id'])->value('title'); $info['contractName1'] = Db::name('house_select')->where('id',$info['contract_id1'])->value('title'); $info['contractName2'] = Db::name('house_select')->where('id',$info['contract_id2'])->value('title'); $info['lessee'] = model('HouseLessee')->info($info['lessee_id']); }else{ $info['priceName'] = ''; $info['levelName'] = ''; $info['payName'] = ''; $info['tenantName'] = ''; $info['contractName1'] = ''; $info['contractName2'] = ''; $info['lessee'] = []; } // 获取已收费金额 $sqmoney = Db::name('house_contract_pay') ->where('contract_id',$id) ->where('del',0) ->where('status',1) ->sum('money'); $sqmoney = $sqmoney?$sqmoney:0; $progress = $info['amount'] > 0?round($sqmoney/$info['amount'],3)*100:0; $info['progress'] = $progress; $info['sqmoney'] = $sqmoney; return $info; } public function lists($page,$size,$intro,$sn,$lessee,$status,$userId,$orgId){ $curTime = date('Y-m-d'); $map[] = ['hc.org_id','=',$orgId]; $map[] = ['hc.del','=',0]; if($intro != ''){ $map[] = ['hc.intro','like','%'.$intro.'%']; } if($sn != ''){ $map[] = ['hc.sn','like','%'.$sn.'%']; } if($lessee != ''){ $map[] = ['hl.title','like','%'.$lessee.'%']; } if($status != ''){ if($status == 3){ $map[] = ['hc.status','=',1]; $map[] = ['hc.end_time','<',$curTime]; }else{ $map[] = ['hc.status','=',$status]; } } $auths = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids'); if(!$auths){ // 无查看权限 return [ 'total' => 0, 'list' => [] ]; } $contractIds = Db::name('house_contract_relation') ->alias('hcr') ->join('house h','h.id = hcr.house_id') ->where('h.level_id','in',$auths) ->where('h.del',0) ->column('hcr.contract_id'); $contractIds = $contractIds?$contractIds:[]; // $contractIds2 = Db::name('house_contract') // ->where('del',0) // ->where('level_id',0) // ->where('contract_id','>',0) // ->column('id'); // $contractIds2 = $contractIds2?$contractIds2:[]; // $contractIds = array_merge($contractIds,$contractIds2); if($contractIds){ $map[] = ['hc.id','in',$contractIds]; }else{ $map[] = ['hc.id','=',-1]; } $lists = Db::name('house_contract') ->alias('hc') ->join('house_lessee hl','hl.id = hc.lessee_id') ->where($map) ->page($page,$size) ->field('hc.*,hl.title as lesseeName') ->order('hc.id desc') ->select(); foreach ($lists as $k=>$v){ if($v['end_time'] < $curTime){ $lists[$k]['status'] = 3; //已到期 } // $lists[$k]['lesseeName'] = Db::name('house_lessee')->where('id',$v['lessee_id'])->value('title'); $house = Db::name('house_contract_relation') ->alias('hcr') ->join('house h','h.id = hcr.house_id') ->where('hcr.contract_id',$v['id']) ->field('h.floor_id,h.number') ->select(); $titles = []; foreach ($house as $kk=>$vv){ $floor = Db::name('house_floor')->where('id',$vv['floor_id'])->find(); $building = Db::name('house_building')->where('id',$floor['building_id'])->find(); $community = Db::name('house_community')->where('id',$building['community_id'])->find(); $district = Db::name('house_district')->where('id',$community['district_id'])->find(); $titles[] = $district['title'].$community['title'].$building['title'].$floor['title'].$vv['number']; } $lists[$k]['houses'] = implode(',',$titles); } $total = Db::name('house_contract') ->alias('hc') ->join('house_lessee hl','hl.id = hc.lessee_id') ->where($map)->count(); $data = [ 'total' => $total, 'list' => $lists?$lists:[] ]; return $data; } public function lists2($page,$size,$intro,$sn,$status,$userId,$orgId){ $map[] = ['hc.org_id','=',$orgId]; $map[] = ['hc.del','=',0]; $map[] = ['hc.contract_id','>',0]; $map[] = ['hc.lessee_id','=',0]; if($intro != ''){ $map[] = ['hc.intro','like','%'.$intro.'%']; } if($sn != ''){ $map[] = ['hc.sn','like','%'.$sn.'%']; } if($status != ''){ $map[] = ['hc.status','=',$status]; } $lists = Db::name('house_contract') ->alias('hc') ->where($map) ->page($page,$size) ->field('hc.*') ->order('hc.id desc') ->select(); $total = Db::name('house_contract') ->alias('hc') ->where($map)->count(); $data = [ 'total' => $total, 'list' => $lists?$lists:[] ]; return $data; } public function house($contractId){ $map[] = ['hcr.contract_id','=',$contractId]; $lists = Db::name('house_contract_relation') ->alias('hcr') ->join('house h','h.id = hcr.house_id') ->where($map) ->field('h.*') ->order('hcr.id asc') ->select(); foreach ($lists as $k=>$v){ $floor = Db::name('house_floor')->where('id',$v['floor_id'])->find(); $building = Db::name('house_building')->where('id',$floor['building_id'])->find(); $community = Db::name('house_community')->where('id',$building['community_id'])->find(); $district = Db::name('house_district')->where('id',$community['district_id'])->find(); $lists[$k]['title'] = $district['title'].$community['title'].$building['title'].$floor['title'].$v['number']; } return $lists?$lists:[]; } public function listByHouse($page,$size,$houseId,$userId){ $map[] = ['hcr.house_id','=',$houseId]; $lists = Db::name('house_contract_relation') ->alias('hcr') ->join('house_contract hc','hc.id = hcr.contract_id') ->where($map) ->page($page,$size) ->field('hc.*') ->order('hc.id desc') ->select(); foreach ($lists as $k=>$v){ $lists[$k]['lesseeName'] = Db::name('house_lessee')->where('id',$v['lessee_id'])->value('title'); } $total = Db::name('house_contract_relation') ->alias('hcr') ->join('house_contract hc','hc.id = hcr.contract_id') ->where($map)->count(); $data = [ 'total' => $total, 'list' => $lists?$lists:[] ]; return $data; } }