HouseContract.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class HouseContract extends Model
  8. {
  9. public function add(){
  10. $dateRange = input('dateRange','','trim');
  11. $dateRange = explode(',',$dateRange);
  12. if(count($dateRange) != 2){
  13. HelpHander::error('未选择合同期限');
  14. }
  15. $data = [
  16. 'id' => input('id/d',0),
  17. 'intro' => input('intro','','trim'),
  18. 'sn' => input('sn','','trim'),
  19. 'remark' => input('remark','','trim'),
  20. 'file' => input('file','','trim'),
  21. 'imgs' => input('imgs','','trim'),
  22. 'org_id' => input('orgId/d',0),
  23. 'sign_date' => input('signDate',null,'trim'),
  24. 'remind' => input('remind/d',0),
  25. 'remind_date' => input('remindDate',null,'trim'),
  26. 'start_time' => $dateRange[0],
  27. 'end_time' => $dateRange[1],
  28. 'contract_id1' => input('contractId1/d',0),
  29. 'contract_id2' => input('contractId2/d',0),
  30. 'tenant_id' => input('tenantId/d',0),
  31. 'pay_id' => input('payId/d',0),
  32. 'level_id' => input('levelId/d',0),
  33. 'price_id' => input('priceId/d',0),
  34. 'price' => input('price/f',0),
  35. 'year_money' => input('yearMoney/f',0),
  36. 'amount' => input('amount/f',0),
  37. 'margin' => input('margin/f',0),
  38. 'day_money1' => input('dayMoney1/f',0),
  39. 'day_money2' => input('dayMoney2/f',0),
  40. 'change_price' => input('changePrice','','trim'),
  41. 'lessee_id' => input('lesseeId/d',0),
  42. 'houses' => input('houses','','trim'),
  43. 'release' => input('release/d',0)
  44. ];
  45. $logdata = json_encode($data);
  46. $scene = 'add';
  47. if($data['id'] > 0){
  48. $scene = 'edit';
  49. }
  50. $result = validate('HouseContract')->check($data,[],$scene);
  51. if(true !== $result){
  52. HelpHander::error(validate('HouseContract')->getError());
  53. }
  54. if(!$data['remind_date']){
  55. unset($data['remind_date']);
  56. }
  57. Db::startTrans();
  58. try{
  59. $id = $data['id'];
  60. $houses = array_unique(explode(',',$data['houses']));
  61. unset($data['id']);
  62. unset($data['houses']);
  63. if($id > 0){
  64. $info = Db::name('house_contract')->where('id',$id)->find();
  65. if($info['lessee_id'] > 0){
  66. unset($data['lessee_id']);
  67. }
  68. $data['update_time'] = date('Y-m-d H:i:s');
  69. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  70. if(!$ret){
  71. \exception('操作失败');
  72. }
  73. if($info['lessee_id'] == 0 && $houses){
  74. $sdata = [];
  75. foreach ($houses as $v){
  76. $sdata[] = [
  77. 'contract_id' => $id,
  78. 'house_id' => $v
  79. ];
  80. }
  81. $res = Db::name('house_contract_relation')->insertAll($sdata);
  82. if($res != count($houses)){
  83. \exception('操作失败');
  84. }
  85. // 房屋当前合同设置
  86. $dd = [
  87. 'cur_lessee_id' => $data['lessee_id'],
  88. 'cur_contract_id' => $id
  89. ];
  90. $retc = Db::name('house')->where('id','in',$houses)->update($dd);
  91. if($retc != count($houses)){
  92. \exception('操作失败');
  93. }
  94. }
  95. }else{
  96. $data['user_id'] = input('userId/d',0);
  97. $data['create_time'] = date('Y-m-d H:i:s');
  98. $ret = $this->allowField(true)->save($data);
  99. if(!$ret){
  100. \exception('操作失败');
  101. }
  102. $id = $this->id;
  103. if($houses){
  104. $sdata = [];
  105. foreach ($houses as $v){
  106. $sdata[] = [
  107. 'contract_id' => $id,
  108. 'house_id' => $v
  109. ];
  110. }
  111. $res = Db::name('house_contract_relation')->insertAll($sdata);
  112. if($res != count($houses)){
  113. \exception('操作失败');
  114. }
  115. // 房屋当前合同设置
  116. $dd = [
  117. 'cur_lessee_id' => $data['lessee_id'],
  118. 'cur_contract_id' => $id
  119. ];
  120. $retc = Db::name('house')->where('id','in',$houses)->update($dd);
  121. if($retc != count($houses)){
  122. \exception('操作失败');
  123. }
  124. }
  125. }
  126. Db::commit();
  127. }catch (Exception $e){
  128. Db::rollback();
  129. HelpHander::error('操作失败'.$e->getMessage());
  130. }
  131. if($id > 0){
  132. $content = '修改经营性房产-合同';
  133. }else{
  134. $content = '添加经营性房产-合同';
  135. }
  136. model('ActionLog')->add(15,$content,0,$logdata);
  137. return true;
  138. }
  139. public function del($id){
  140. $info = Db::name('house_contract')->where('id',$id)->where('del',0)->find();
  141. if(!$info){
  142. HelpHander::error('合同已删除');
  143. }
  144. Db::startTrans();
  145. try{
  146. $ret = Db::name('house_contract')->where('id',$id)->setField('del',1);
  147. if(!$ret){
  148. \exception('操作失败');
  149. }
  150. $houses = Db::name('house_contract_relation')->where('contract_id',$id)->column('house_id');
  151. $dd = [
  152. 'cur_lessee_id' => 0,
  153. 'cur_contract_id' => 0
  154. ];
  155. Db::name('house')->where('id','in',$houses)->update($dd);
  156. Db::commit();
  157. }catch (Exception $e){
  158. Db::rollback();
  159. HelpHander::error($e->getMessage());
  160. }
  161. $logdata = json_encode(['id' => $id]);
  162. model('ActionLog')->add(15,'删除经营性房产-合同',0,$logdata);
  163. return true;
  164. }
  165. // 终止合同
  166. public function stop($id){
  167. $info = Db::name('house_contract')->where('id',$id)->where('del',0)->find();
  168. if(!$info){
  169. HelpHander::error('记录不存在');
  170. }
  171. if($info['status'] == 2){
  172. HelpHander::error('合同已终止');
  173. }
  174. $curTime = date('Y-m-d');
  175. if($info['end_time'] < $curTime){
  176. HelpHander::error('合同已过期无需终止');
  177. }
  178. Db::startTrans();
  179. try{
  180. $ret = Db::name('house_contract')->where('id',$id)->update(['status' => 2,'update_time'=>date('Y-m-d H:i:s')]);
  181. if(!$ret){
  182. \exception('操作失败');
  183. }
  184. $houses = Db::name('house_contract_relation')->where('contract_id',$id)->column('house_id');
  185. $dd = [
  186. 'cur_lessee_id' => 0,
  187. 'cur_contract_id' => 0
  188. ];
  189. Db::name('house')->where('id','in',$houses)->update($dd);
  190. Db::commit();
  191. }catch (Exception $e){
  192. Db::rollback();
  193. HelpHander::error($e->getMessage());
  194. }
  195. return true;
  196. }
  197. public function info($id){
  198. $info = Db::name('house_contract')->where('id',$id)->where('del',0)->find();
  199. if(!$info){
  200. HelpHander::error('记录不存在');
  201. }
  202. $curTime = date('Y-m-d');
  203. if($info['end_time'] < $curTime){
  204. $info['status'] = 3; //已到期
  205. }
  206. if($info['lessee_id'] > 0){
  207. $info['priceName'] = Db::name('house_select')->where('id',$info['price_id'])->value('title');
  208. $info['levelName'] = Db::name('house_level')->where('id',$info['level_id'])->value('title');
  209. $info['payName'] = Db::name('house_select')->where('id',$info['pay_id'])->value('title');
  210. $info['tenantName'] = Db::name('house_select')->where('id',$info['tenant_id'])->value('title');
  211. $info['contractName1'] = Db::name('house_select')->where('id',$info['contract_id1'])->value('title');
  212. $info['contractName2'] = Db::name('house_select')->where('id',$info['contract_id2'])->value('title');
  213. $info['lessee'] = model('HouseLessee')->info($info['lessee_id']);
  214. }else{
  215. $info['priceName'] = '';
  216. $info['levelName'] = '';
  217. $info['payName'] = '';
  218. $info['tenantName'] = '';
  219. $info['contractName1'] = '';
  220. $info['contractName2'] = '';
  221. $info['lessee'] = [];
  222. }
  223. // 获取已收费金额
  224. $sqmoney = Db::name('house_contract_pay')
  225. ->where('contract_id',$id)
  226. ->where('del',0)
  227. ->where('status',1)
  228. ->sum('money');
  229. $sqmoney = $sqmoney?$sqmoney:0;
  230. $progress = $info['amount'] > 0?round($sqmoney/$info['amount'],3)*100:0;
  231. $info['progress'] = $progress;
  232. $info['sqmoney'] = $sqmoney;
  233. return $info;
  234. }
  235. public function lists($page,$size,$intro,$sn,$lessee,$status,$userId,$orgId){
  236. $curTime = date('Y-m-d');
  237. $map[] = ['hc.org_id','=',$orgId];
  238. $map[] = ['hc.del','=',0];
  239. if($intro != ''){
  240. $map[] = ['hc.intro','like','%'.$intro.'%'];
  241. }
  242. if($sn != ''){
  243. $map[] = ['hc.sn','like','%'.$sn.'%'];
  244. }
  245. if($lessee != ''){
  246. $map[] = ['hl.title','like','%'.$lessee.'%'];
  247. }
  248. if($status != ''){
  249. if($status == 3){
  250. $map[] = ['hc.status','=',1];
  251. $map[] = ['hc.end_time','<',$curTime];
  252. }else{
  253. $map[] = ['hc.status','=',$status];
  254. }
  255. }
  256. $auths = Db::name('house_auth')->where('user_id',$userId)->where('type',1)->value('ids');
  257. if(!$auths){ // 无查看权限
  258. return [
  259. 'total' => 0,
  260. 'list' => []
  261. ];
  262. }
  263. $contractIds = Db::name('house_contract_relation')
  264. ->alias('hcr')
  265. ->join('house h','h.id = hcr.house_id')
  266. ->where('h.level_id','in',$auths)
  267. ->where('h.del',0)
  268. ->column('hcr.contract_id');
  269. $contractIds = $contractIds?$contractIds:[];
  270. // $contractIds2 = Db::name('house_contract')
  271. // ->where('del',0)
  272. // ->where('level_id',0)
  273. // ->where('contract_id','>',0)
  274. // ->column('id');
  275. // $contractIds2 = $contractIds2?$contractIds2:[];
  276. // $contractIds = array_merge($contractIds,$contractIds2);
  277. if($contractIds){
  278. $map[] = ['hc.id','in',$contractIds];
  279. }else{
  280. $map[] = ['hc.id','=',-1];
  281. }
  282. $lists = Db::name('house_contract')
  283. ->alias('hc')
  284. ->join('house_lessee hl','hl.id = hc.lessee_id')
  285. ->where($map)
  286. ->page($page,$size)
  287. ->field('hc.*,hl.title as lesseeName')
  288. ->order('hc.id desc')
  289. ->select();
  290. foreach ($lists as $k=>$v){
  291. if($v['end_time'] < $curTime){
  292. $lists[$k]['status'] = 3; //已到期
  293. }
  294. // $lists[$k]['lesseeName'] = Db::name('house_lessee')->where('id',$v['lessee_id'])->value('title');
  295. $house = Db::name('house_contract_relation')
  296. ->alias('hcr')
  297. ->join('house h','h.id = hcr.house_id')
  298. ->where('hcr.contract_id',$v['id'])
  299. ->field('h.floor_id,h.number')
  300. ->select();
  301. $titles = [];
  302. foreach ($house as $kk=>$vv){
  303. $floor = Db::name('house_floor')->where('id',$vv['floor_id'])->find();
  304. $building = Db::name('house_building')->where('id',$floor['building_id'])->find();
  305. $community = Db::name('house_community')->where('id',$building['community_id'])->find();
  306. $district = Db::name('house_district')->where('id',$community['district_id'])->find();
  307. $titles[] = $district['title'].$community['title'].$building['title'].$floor['title'].$vv['number'];
  308. }
  309. $lists[$k]['houses'] = implode(',',$titles);
  310. }
  311. $total = Db::name('house_contract')
  312. ->alias('hc')
  313. ->join('house_lessee hl','hl.id = hc.lessee_id')
  314. ->where($map)->count();
  315. $data = [
  316. 'total' => $total,
  317. 'list' => $lists?$lists:[]
  318. ];
  319. return $data;
  320. }
  321. public function lists2($page,$size,$intro,$sn,$status,$userId,$orgId){
  322. $map[] = ['hc.org_id','=',$orgId];
  323. $map[] = ['hc.del','=',0];
  324. $map[] = ['hc.contract_id','>',0];
  325. $map[] = ['hc.lessee_id','=',0];
  326. if($intro != ''){
  327. $map[] = ['hc.intro','like','%'.$intro.'%'];
  328. }
  329. if($sn != ''){
  330. $map[] = ['hc.sn','like','%'.$sn.'%'];
  331. }
  332. if($status != ''){
  333. $map[] = ['hc.status','=',$status];
  334. }
  335. $lists = Db::name('house_contract')
  336. ->alias('hc')
  337. ->where($map)
  338. ->page($page,$size)
  339. ->field('hc.*')
  340. ->order('hc.id desc')
  341. ->select();
  342. $total = Db::name('house_contract')
  343. ->alias('hc')
  344. ->where($map)->count();
  345. $data = [
  346. 'total' => $total,
  347. 'list' => $lists?$lists:[]
  348. ];
  349. return $data;
  350. }
  351. public function house($contractId){
  352. $map[] = ['hcr.contract_id','=',$contractId];
  353. $lists = Db::name('house_contract_relation')
  354. ->alias('hcr')
  355. ->join('house h','h.id = hcr.house_id')
  356. ->where($map)
  357. ->field('h.*')
  358. ->order('hcr.id asc')
  359. ->select();
  360. foreach ($lists as $k=>$v){
  361. $floor = Db::name('house_floor')->where('id',$v['floor_id'])->find();
  362. $building = Db::name('house_building')->where('id',$floor['building_id'])->find();
  363. $community = Db::name('house_community')->where('id',$building['community_id'])->find();
  364. $district = Db::name('house_district')->where('id',$community['district_id'])->find();
  365. $lists[$k]['title'] = $district['title'].$community['title'].$building['title'].$floor['title'].$v['number'];
  366. }
  367. return $lists?$lists:[];
  368. }
  369. public function listByHouse($page,$size,$houseId,$userId){
  370. $map[] = ['hcr.house_id','=',$houseId];
  371. $lists = Db::name('house_contract_relation')
  372. ->alias('hcr')
  373. ->join('house_contract hc','hc.id = hcr.contract_id')
  374. ->where($map)
  375. ->page($page,$size)
  376. ->field('hc.*')
  377. ->order('hc.id desc')
  378. ->select();
  379. foreach ($lists as $k=>$v){
  380. $lists[$k]['lesseeName'] = Db::name('house_lessee')->where('id',$v['lessee_id'])->value('title');
  381. }
  382. $total = Db::name('house_contract_relation')
  383. ->alias('hcr')
  384. ->join('house_contract hc','hc.id = hcr.contract_id')
  385. ->where($map)->count();
  386. $data = [
  387. 'total' => $total,
  388. 'list' => $lists?$lists:[]
  389. ];
  390. return $data;
  391. }
  392. }