HousePay.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class HousePay extends Base
  6. {
  7. public function createOrder($houseId,$lastDate,$order,$userId,$orgId){
  8. $info = Db::name('house')->where('id',$houseId)
  9. ->where('del',0)->find();
  10. if(!$info||!$info['owner_id']||$info['enable']==0){
  11. $this->error = '业主不存在或未绑定业主';
  12. return false;
  13. }
  14. $owner = Db::name('owner')->
  15. where('id',$info['owner_id'])
  16. ->where('del',0)->find();
  17. if(!$owner){
  18. $this->error = '业主不存在';
  19. return false;
  20. }
  21. $this->startTrans();
  22. try{
  23. $data = [
  24. 'org_id' => $orgId,
  25. 'last_time' => $lastDate,
  26. 'sn' => get_unique_id(),
  27. 'status' => 0,
  28. 'create_time' => getTime(),
  29. 'create_user_id' => $userId,
  30. 'money' => 0,
  31. 'owner_id' => $info['owner_id'],
  32. 'area_id' => $info['area_id'],
  33. 'pre_money' => 0,
  34. ];
  35. $money = 0;
  36. foreach ($order as $k=>$v){
  37. $money += $v['total_price'];
  38. }
  39. $lastMoney = 0;
  40. if($owner['money'] > $money){
  41. $lastMoney = $owner['money'] - $money;
  42. $data['money'] = 0;
  43. $data['pre_money'] = $money;
  44. }else{
  45. $data['money'] = round($money - $owner['money'],2);
  46. $data['pre_money'] = $owner['money'];
  47. }
  48. // 修改业主剩余金额
  49. $res = Db::name('owner')
  50. ->where('id',$info['owner_id'])
  51. ->update( [
  52. 'money' => $lastMoney,
  53. 'update_time'=>getTime()
  54. ]);
  55. if (!$res) {
  56. exception('业主余额修改失败');
  57. }
  58. $payid = Db::name('house_pay')->insertGetId($data);
  59. if (!$res) {
  60. exception('保存记录失败');
  61. }
  62. foreach ($order as $k=>$v){
  63. $log = [
  64. 'org_id' => $orgId,
  65. 'type' => $v['type'],
  66. 'bus_id' => $v['id'],
  67. 'fee_id' => $v['fee_id'],
  68. 'pay_id' => $payid,
  69. 'money' => $v['total_price'],
  70. 'start' => $v['start']?$v['start']:null,
  71. 'end' => $v['end']?$v['end']:null,
  72. 'remark' => $v['remark']
  73. ];
  74. $logid = Db::name('house_pay_log')->insertGetId($log);
  75. if (!$logid) {
  76. exception('操作失败');
  77. }
  78. $months = $v['months'];
  79. if($months){
  80. $lms = [];
  81. foreach ($months as $kk=>$vv){
  82. $lms[] = [
  83. 'log_id' => $logid,
  84. 'month' => $vv['year'].'-'.(strlen($vv['month']) == 1?'0'.$vv['month']:$vv['month']),
  85. 'money' => $vv['money']
  86. ];
  87. }
  88. $res = Db::name('house_pay_log_money')
  89. ->insertAll($lms);
  90. if (!$res) {
  91. exception('操作失败');
  92. }
  93. }
  94. }
  95. $this->commit();
  96. return $payid;
  97. }catch (Exception $e){
  98. $this->error = $e->getmessage();
  99. $this->rollback();
  100. return false;
  101. }
  102. }
  103. public function show_info($id){
  104. $info = $this->where('id',$id)
  105. ->where('del',0)->find();
  106. if($info){
  107. $info['pay_data'] = $info['pay_data']?json_decode($info['pay_data'],true):[];
  108. $info['xq_title'] = $this->getTableField('house_areas',['id'=>$info['area_id']],'title');
  109. $info['full_name'] = $this->getTableField('owner',['id'=>$info['owner_id']],'name');
  110. $info['full_money'] = $this->getTableField('owner',['id'=>$info['owner_id']],'money');
  111. $logs = Db::name('house_pay_log')
  112. ->where('pay_id',$id)->select();
  113. $titles = [];
  114. $ids = [];
  115. foreach ($logs as $k=>$v){
  116. $logs[$k]['full_title'] = $this->getTableField('house_view',['id'=>$v['bus_id']],'full_title');
  117. if(!in_array($v['bus_id'],$ids)){
  118. $ids[] = $v['bus_id'];
  119. $house = Db::name('house')->where('id',$v['bus_id'])
  120. ->find();
  121. if($house && $house['cate'] == 1){
  122. $titles[] = '[住房]'.$house['title'];
  123. }else if($house && $house['cate'] == 2){
  124. $titles[] = '[商铺]'.$house['title'];
  125. }else if($house && $house['cate'] == 3){
  126. $titles[] = '[营业房]'.$house['title'];
  127. }else if($house && $house['cate'] == 4){
  128. $titles[] = '[储藏室]'.$house['title'];
  129. }else if($house && $house['cate'] == 5){
  130. $titles[] = '[停车位]'.$house['title'];
  131. }
  132. }
  133. $logs[$k]['cate'] = $this->getTableField('house_view',['id'=>$v['bus_id']],'cate');
  134. $logs[$k]['fee_title'] = $this->getTableField('house_fee',['id'=>$v['fee_id']],'title');
  135. }
  136. $info['logs'] = $logs?$logs:[];
  137. $info['titles'] = implode(',',$titles);
  138. $info['dx_money'] = $this->convertamounttocn($info['true_money']);
  139. }
  140. return $info;
  141. }
  142. public function paysave($post,$userId){
  143. $info = $this
  144. ->where('id',$post['id'])
  145. ->where('del',0)
  146. ->find();
  147. if(!$info){
  148. $this->error = '记录不存在';
  149. return false;
  150. }
  151. if($info['status'] == 1){
  152. $this->error = '该记录已缴费';
  153. return false;
  154. }
  155. if($info['create_user_id'] != $userId){
  156. $this->error = '无权限操作';
  157. return false;
  158. }
  159. if($post['true_money'] < $info['money']){
  160. $this->error = '实收金额必须大于等于总金额';
  161. return false;
  162. }
  163. $dymoney = $post['true_money'] - $info['money']; // 多余的存入预付款
  164. $premoney = $this->getTableField('owner',['id'=>$info['owner_id']],'money'); // 剩余预付款
  165. $premoney = $premoney?$premoney:0;
  166. $lastmoney = round($dymoney + $premoney,2); //当前剩余预付款
  167. $this->startTrans();
  168. try{
  169. $data = [
  170. 'status' => 1,
  171. 'remark' => $post['remark'],
  172. 'pay_time' => $post['pay_time'],
  173. 'pay_type' => $post['pay_type'],
  174. 'true_money' => $post['true_money'],
  175. 'last_pre_money' => $lastmoney,
  176. 'payee' => $post['payee'],
  177. 'pay_data' => $post['pay_data']
  178. ];
  179. $ret = $this->where('id',$post['id'])
  180. ->update($data);
  181. if (!$ret) {
  182. exception('操作失败');
  183. }
  184. $ret = Db::name('owner')->where('id',$info['owner_id'])
  185. ->update( [
  186. 'money' => $lastmoney,
  187. 'update_time'=>getTime()
  188. ]);
  189. if (!$ret) {
  190. exception('操作失败');
  191. }
  192. $this->commit();
  193. return true;
  194. }catch (Exception $e){
  195. $this->error = $e->getmessage();
  196. $this->rollback();
  197. return false;
  198. }
  199. }
  200. // 返销
  201. public function cancel($post,$id,$userId){
  202. $info = $this->where('id',$id)->find();
  203. if(!$info||$info['del'] == 1){
  204. $this->error = '记录不存在';
  205. return false;
  206. }
  207. if($info['status'] == 2||$info['status'] == 3){
  208. $this->error = '缴费单已返销';
  209. return false;
  210. }
  211. $ret = $this->checkfinish($id);
  212. if($ret){
  213. $this->error = '已扎帐缴费单不能返销';
  214. return false;
  215. }
  216. $this->startTrans();
  217. try{
  218. $data = [
  219. 'cancel_reason' => isset($post['cancel_reason'])?$post['cancel_reason']:'',
  220. ];
  221. if($info['status'] == 0){ //未缴费可直接返销
  222. $data['cancel_user_id'] = $userId;
  223. $data['cancel_time'] = getTime();
  224. $data['status'] = 3;
  225. $data['is_cancel'] = 2;
  226. }else{ // 已缴费需要财务审核返销
  227. $data['status'] = 2;
  228. $data['cancel_user_id'] = 0;
  229. $data['cancel_time'] = null;
  230. $data['is_cancel'] = 1;
  231. }
  232. $ret = $this->where('id',$id)
  233. ->update($data);
  234. if (!$ret) {
  235. exception('操作失败');
  236. }
  237. $this->commit();
  238. return true;
  239. }catch (Exception $e){
  240. $this->rollback();
  241. $this->error = $e->getmessage();
  242. return false;
  243. }
  244. }
  245. // 检查是否已扎帐
  246. public function checkFinish($id){
  247. $ret = Db::name('house_lock_pay')
  248. ->alias('hlp')
  249. ->join('house_lock hl','hl.id = hlp.house_lock_id')
  250. ->where('hl.status','in',[0,1])
  251. ->where('hlp.pay_id',$id)
  252. ->field('hlp.*')
  253. ->find();
  254. return $ret?true:false; // true=已扎帐
  255. }
  256. public function show_all_fee($orgId,$searchtext){
  257. if(isset($searchtext['name']) && $searchtext['name'] !=''){
  258. $map1[] = ['name','like','%'.$searchtext['name'].'%'];
  259. $user = Db::name('owner')
  260. ->where($map1)
  261. ->where('del',0)
  262. ->where('enable',1)
  263. ->column('id');
  264. if(!empty($user)){
  265. $map[] = ['hp.house_id','in',$user];
  266. }else{
  267. $map[] = ['hp.house_id','=',-1];
  268. }
  269. }
  270. if(isset($searchtext['status']) && $searchtext['status'] !=''){
  271. $map[] = ['hp.status','=',$searchtext['status']];
  272. }
  273. $b = isset($searchtext['start'])?$searchtext['start']:'';
  274. $e = isset($searchtext['end'])?$searchtext['end']:'';
  275. if($b && $e){
  276. if($b <= $e){
  277. $b = date('Y-m-d 00:00:00',strtotime($b));
  278. $e = date('Y-m-d 23:59:59',strtotime($e));
  279. $map[] = ['hp.create_time','>=',$b];
  280. $map[] = ['hp.create_time','<=',$e];
  281. }
  282. }
  283. if($b && $e==''){
  284. $b = date('Y-m-d 00:00:00',strtotime($b));
  285. $map[] = ['hp.create_time','>=',$b];
  286. }
  287. if($b=='' && $e){
  288. $e = date('Y-m-d 23:59:59',strtotime($e));
  289. $map[] = ['hp.create_time','<=',$e];
  290. }
  291. $map2 = [];
  292. $c = isset($searchtext['wstart'])?$searchtext['wstart']:'';
  293. $d = isset($searchtext['wend'])?$searchtext['wend']:'';
  294. if($c && $d){
  295. if($c <= $d){
  296. $c = date('Y-m-d',strtotime($c));
  297. $d = date('Y-m-d',strtotime($d));
  298. $map2[] = ['hp.end','>=',$c];
  299. $map2[] = ['hp.start','<=',$d];
  300. }
  301. }
  302. if($c && $d==''){
  303. $c = date('Y-m-d',strtotime($c));
  304. $map2[] = ['hp.end','>=',$c];
  305. }
  306. if($c=='' && $d){
  307. $d = date('Y-m-d',strtotime($d));
  308. $map2[] = ['hp.start','<=',$d];
  309. }
  310. if(!empty($map2)){
  311. $logs = Db::name('house_pay_log')->where($map2)
  312. ->distinct(true)->column('pay_id');
  313. if($logs){
  314. $map[] = ['hp.id','in',$logs];
  315. }else{
  316. $map[] = ['hp.id','=',-1];
  317. }
  318. }
  319. $map[] = ['hp.del','=',0];
  320. $map[] = ['hp.org_id','=',$orgId];
  321. $fees = Db::name('house_pay_log')
  322. ->alias('hpl')
  323. ->join('house_pay hp','hp.id = hpl.pay_id')
  324. ->join('house_fee hf','hf.id = hpl.fee_id')
  325. ->field('hpl.fee_id,hf.title')
  326. ->group('hpl.fee_id')
  327. ->distinct(true)
  328. ->select();
  329. return $fees;
  330. }
  331. public function getFeeSearch(){
  332. $sn = input('sn','','trim');
  333. if($sn){
  334. $map[] = ['sn','like','%'.$sn.'%'];
  335. }
  336. $area_title = input('area_title','','trim');
  337. if($area_title){
  338. $map[] = ['area_title','like','%'.$area_title.'%'];
  339. }
  340. $title = input('title','','trim');
  341. if($title){
  342. $map[] = ['title','like','%'.$title.'%'];
  343. }
  344. $name = input('name','','trim');//用户
  345. if($name){
  346. $map[] = ['name','like','%'.$name.'%'];
  347. }
  348. $cate = input('cate','','trim');
  349. if($cate != ''){
  350. $map[] = ['cate','=',$cate];
  351. }
  352. $fee = input('fee','','trim');
  353. if($fee != ''){
  354. $map[] = ['fee_id','=',$fee];
  355. }
  356. $status = input('status','','trim');
  357. if($status != ''){
  358. $map[] = ['status','=',$status];
  359. }
  360. $c_type = input('c_type','','trim');
  361. if($c_type != ''){
  362. $map[] = ['c_type','=',$c_type];
  363. }
  364. $b = input('start','','trim');
  365. $e = input('end','','trim');
  366. if($b){
  367. $b = date('Y-m-d 00:00:00',strtotime($b));
  368. $map[] = ['create_time','>=',$b];
  369. }
  370. if($e){
  371. $e = date('Y-m-d 23:59:59',strtotime($e));
  372. $map[] = ['create_time','<=',$e];
  373. }
  374. $c = input('wstart','','trim');
  375. $d = input('wend','','trim');
  376. if($c){
  377. $c = date('Y-m-d',strtotime($c));
  378. $map[] = ['end','>=',$c];
  379. }
  380. if($d){
  381. $d = date('Y-m-d',strtotime($d));
  382. $map[] = ['start','<=',$d];
  383. }
  384. $map[] = ['org_id','=',cur_org_id()];
  385. $map= empty($map) ? true: $map;
  386. return $map;
  387. }
  388. /**
  389. * 将数值金额转换为中文大写金额
  390. * @param $amount float 金额(支持到分)
  391. * @param $type int 补整类型,0:到角补整;1:到元补整
  392. * @return mixed 中文大写金额
  393. */
  394. public function convertAmountToCn($num, $type = 0) {
  395. $c1 = "零壹贰叁肆伍陆柒捌玖";
  396. $c2 = "分角元拾佰仟万拾佰仟亿";
  397. //精确到分后面就不要了,所以只留两个小数位
  398. $num = round($num, 2);
  399. //将数字转化为整数
  400. $num = $num * 100;
  401. if (strlen($num) > 10) {
  402. return "金额太大";
  403. }
  404. $i = 0;
  405. $c = "";
  406. while (1) {
  407. if ($i == 0) {
  408. //获取最后一位数字
  409. $n = substr($num, strlen($num)-1, 1);
  410. } else {
  411. $n = $num % 10;
  412. }
  413. //每次将最后一位数字转化为中文
  414. $p1 = substr($c1, 3 * $n, 3);
  415. $p2 = substr($c2, 3 * $i, 3);
  416. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  417. $c = $p1 . $p2 . $c;
  418. } else {
  419. $c = $p1 . $c;
  420. }
  421. $i = $i + 1;
  422. //去掉数字最后一位了
  423. $num = $num / 10;
  424. $num = (int)$num;
  425. //结束循环
  426. if ($num == 0) {
  427. break;
  428. }
  429. }
  430. $j = 0;
  431. $slen = strlen($c);
  432. while ($j < $slen) {
  433. //utf8一个汉字相当3个字符
  434. $m = substr($c, $j, 6);
  435. //处理数字中很多0的情况,每次循环去掉一个汉字“零”
  436. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  437. $left = substr($c, 0, $j);
  438. $right = substr($c, $j + 3);
  439. $c = $left . $right;
  440. $j = $j-3;
  441. $slen = $slen-3;
  442. }
  443. $j = $j + 3;
  444. }
  445. //这个是为了去掉类似23.0中最后一个“零”字
  446. if (substr($c, strlen($c)-3, 3) == '零') {
  447. $c = substr($c, 0, strlen($c)-3);
  448. }
  449. //将处理的汉字加上“整”
  450. if (empty($c)) {
  451. return "零元整";
  452. }else{
  453. return $c;
  454. }
  455. }
  456. // 退款
  457. public function refund($post,$id,$userId,$orgId=0){
  458. $info = Db::name('house_pay_log')->
  459. where('id',$id)->find();
  460. if(!$info){
  461. $this->error = '记录不存在';
  462. return false;
  463. }
  464. if($info['status'] != 0){
  465. $this->error = '无权限操作';
  466. return false;
  467. }
  468. $pInfo =$this
  469. ->where('id',$info['pay_id'])
  470. ->where('del',0)
  471. ->find();
  472. if(!$pInfo){
  473. $this->error = '记录不存在';
  474. return false;
  475. }
  476. if($pInfo['create_user_id'] != $userId){
  477. $this->error = '无权限操作';
  478. return false;
  479. }
  480. $refundMoney =Db::name('house_refund_log')
  481. ->where('pay_id',$id)
  482. ->where('status','in',[1,2])
  483. ->sum('money');
  484. if($post['money'] > $info['money']){
  485. $this->error = '退款金额不能大于实收金额';
  486. return false;
  487. }
  488. if($post['money']+$refundMoney > $info['money']){
  489. $this->error = '累计退款金额不能大于实收金额';
  490. return false;
  491. }
  492. $this->startTrans();
  493. try{
  494. $data = [
  495. 'reason' => isset($post['reason'])?$post['reason']:'',
  496. 'refund_user_id' => $userId,
  497. 'org_id' => $orgId,
  498. 'fee_id' => $info['fee_id'],
  499. 'bus_id' => $info['bus_id'],
  500. 'pay_id' => $id,
  501. 'refund_time' => getTime(),
  502. 'status' => 1,
  503. 'money' => $post['money'],
  504. 'refund_account' => $post['refund_account'],
  505. 'refund_name' => $post['refund_name'],
  506. 'refund_bank' => $post['refund_bank'],
  507. ];
  508. $ret = Db::name('house_refund_log')
  509. ->insertGetId($data);
  510. if (!$ret) {
  511. exception('事务失败');
  512. }
  513. $this->commit();
  514. return true;
  515. }catch (Exception $e){
  516. $this->rollback();
  517. $this->error = $e->getmessage();
  518. return false;
  519. }
  520. }
  521. // 处理待审核返销
  522. public function dealpay($status,$id,$userId){
  523. $info = $this->where('id',$id)->find();
  524. if(!$info||$info['del'] == 1){
  525. $this->error = '记录不存在';
  526. return false;
  527. }
  528. if($info['status'] != 2){
  529. $this->error = '缴费单不处于审核状态';
  530. return false;
  531. }
  532. $this->startTrans();
  533. try{
  534. $data = [
  535. 'status' => $status,
  536. 'cancel_user_id' => $userId,
  537. 'cancel_time' => getTime()
  538. ];
  539. $res = $this->where('id',$id)->update($data);
  540. if (!$res) {
  541. exception('操作失败');
  542. }
  543. if($status == 3){ // 同意
  544. $preMoney = $this->getTableField('owner',['id'=>$info['owner_id']],'money'); // 剩余预付款
  545. $dyMoney = $info['true_money'] - $info['money']; // 多余预付款
  546. if($dyMoney > $preMoney){
  547. exception('剩余预付款不能为负值');
  548. }
  549. $lastMoney = round($preMoney - $dyMoney,2);
  550. $res = Db::name('owner')
  551. ->where('id',$info['owner_id'])
  552. ->update([
  553. 'money' => $lastMoney,
  554. 'update_time' => date('y-m-d h:i:s')
  555. ]);
  556. if (!$res) {
  557. exception('操作失败');
  558. }
  559. }
  560. $this->commit();
  561. return true;
  562. }catch (Exception $e){
  563. $this->rollback();
  564. $this->error = $e->getmessage();
  565. return false;
  566. }
  567. }
  568. public function disagreeRefund($id,$userId){
  569. $info = Db::name('house_refund_log')
  570. ->where('id',$id)
  571. ->find();
  572. if(!$info){
  573. $this->error = '记录不存在';
  574. return false;
  575. }
  576. if($info['status'] != 1){
  577. $this->error = '无权限操作';
  578. return false;
  579. }
  580. $data = [
  581. 'deal_user_id' => $userId,
  582. 'deal_time' => getTime(),
  583. 'status' => 0
  584. ];
  585. $res = Db::name('house_refund_log')
  586. ->where('id',$id)
  587. ->update($data);
  588. if (!$res) {
  589. $this->error = '操作失败';
  590. return false;
  591. }
  592. return true;
  593. }
  594. public function agreeRefund($id,$userId,$post){
  595. $info = Db::name('house_refund_log')
  596. ->where('id',$id)
  597. ->find();
  598. if(!$info){
  599. $this->error = '记录不存在';
  600. return false;
  601. }
  602. if($info['status'] != 1){
  603. $this->error = '无权限操作';
  604. return false;
  605. }
  606. $data = [
  607. 'deal_user_id' => $userId,
  608. 'deal_time' => getTime(),
  609. 'status' => 2,
  610. 'voucher' => $post['voucher'],
  611. ];
  612. $res = Db::name('house_refund_log')
  613. ->where('id',$id)
  614. ->update($data);
  615. if (!$res) {
  616. $this->error = '操作失败';
  617. return false;
  618. }
  619. return true;
  620. }
  621. }