HouseLock.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class HouseLock extends Base
  6. {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. public $table = 'house_lock';
  10. protected $validateName = 'HouseLock';
  11. public function getStartTime($userId){
  12. $info = $this
  13. ->where('user_id',$userId)
  14. ->order('end','desc')
  15. ->find();
  16. if(!$info){
  17. $info = Db::name('house_pay')
  18. ->where('create_user_id',$userId)
  19. ->where('status','in',[1,2])
  20. ->order('pay_time','asc')
  21. ->find();
  22. if($info){
  23. return $info['pay_time'];
  24. }
  25. }else{
  26. return $info['end'];
  27. }
  28. return '';
  29. }
  30. public function getDataInfo($start,$end,$userId,$orgId){
  31. // 检查是否有返销的数据
  32. $ret = Db::name('house_pay')
  33. ->where('create_user_id',$userId)
  34. ->where('org_id',$orgId)
  35. ->where('status',2)
  36. ->where('del',0)
  37. ->where('pay_time','between',[$start,$end])
  38. ->find();
  39. if($ret){
  40. $this->error = '存在返销中的数据';
  41. return false;
  42. }
  43. $lists = Db::name('house_pay')
  44. ->alias('hp')
  45. ->join('owner o','o.id = hp.owner_id')
  46. ->join('user u','u.id = hp.create_user_id')
  47. ->where('hp.create_user_id',$userId)
  48. ->where('hp.status',1)
  49. ->where('hp.del',0)
  50. ->where('hp.pay_time','between',[$start,$end])
  51. ->field('hp.*,o.name,u.real_name')
  52. ->select();
  53. if(!$lists){
  54. $this->error = '该时间段内没有收费记录';
  55. return false;
  56. }
  57. $mData = [
  58. 'money' => 0,
  59. 'pre_money' => 0,
  60. 'true_money' => 0,
  61. 'pay_money1' => 0,
  62. 'pay_money3' => 0,
  63. 'pay_money4' => 0,
  64. 'pay_money5' => 0,
  65. ];
  66. $payIds = [];
  67. foreach ($lists as $k=>$v){
  68. $payIds[] = $v['id'];
  69. $mData['money'] += $v['money'];
  70. $mData['pre_money'] += $v['pre_money'];
  71. $mData['true_money'] += $v['true_money'];
  72. if($v['pay_type'] == 1){ //1=现金 3=支付宝 4=银行托收 5=微信 6=组合付款
  73. $mData['pay_money1'] += $v['true_money'];
  74. }else if($v['pay_type'] == 3){
  75. $mData['pay_money3'] += $v['true_money'];
  76. }else if($v['pay_type'] == 4){
  77. $mData['pay_money4'] += $v['true_money'];
  78. }else if($v['pay_type'] == 5){
  79. $mData['pay_money5'] += $v['true_money'];
  80. }else{
  81. $payData = json_decode($v['pay_data'],true);
  82. foreach ($payData as $kk=>$vv){
  83. if($kk == 1){
  84. $mData['pay_money1'] += $vv;
  85. }else if($kk == 3){
  86. $mData['pay_money3'] += $vv;
  87. }else if($kk == 4){
  88. $mData['pay_money4'] += $vv;
  89. }else if($kk == 5){
  90. $mData['pay_money5'] += $vv;
  91. }
  92. }
  93. }
  94. }
  95. $logs = Db::name('house_pay_log_view')
  96. ->where('pay_id','in',$payIds)
  97. ->select();
  98. $feeIds = [];
  99. $fees = [];
  100. foreach ($logs as $k=>$v){
  101. if(!in_array($v['fee_id'],$feeIds)){
  102. $feeIds[] = $v['fee_id'];
  103. $fees[] = [
  104. 'fee_id' => $v['fee_id'],
  105. 'fee_title' => $v['fee_title'],
  106. 'money' => 0
  107. ];
  108. }
  109. }
  110. foreach ($fees as $k=>$v){
  111. foreach ($logs as $kk=>$vv){
  112. if($v['fee_id'] == $vv['fee_id']){
  113. $fees[$k]['money'] += $vv['money'];
  114. }
  115. }
  116. }
  117. $data = [
  118. 'list' => $lists,
  119. 'data' => $mData,
  120. 'logs' => $logs,
  121. 'fees' => $fees
  122. ];
  123. return $data;
  124. }
  125. public function addSave($start,$end,$remark,$userId,$orgId){
  126. $gStart = $this->getstarttime($userId);
  127. if($start != $gStart){
  128. $this->error = '开始时间不正确';
  129. return false;
  130. }
  131. $data = $this->getDataInfo($start,$end,$userId,$orgId);
  132. if(!$data){
  133. return false;
  134. }
  135. $post = $data['data'];
  136. $this->startTrans();
  137. try{
  138. $sData = [
  139. 'org_id' => $orgId,
  140. 'user_id' => $userId,
  141. 'status' => 0,
  142. 'start' => $start?$start:null,
  143. 'end' => $end?$end:null,
  144. 'money' => $post['money'],
  145. 'pre_money' => $post['pre_money'],
  146. 'true_money' => $post['true_money'],
  147. 'pay_money1' => $post['pay_money1'],
  148. 'pay_money3' => $post['pay_money3'],
  149. 'pay_money4' => $post['pay_money4'],
  150. 'pay_money5' => $post['pay_money5'],
  151. 'create_time' => getTime(),
  152. 'remark' => $remark
  153. ];
  154. $lid = $this->insertGetId($sData);
  155. if (!$lid) {
  156. exception('操作失败');
  157. }
  158. foreach ($data['list'] as $k=>$v){
  159. $res = Db::name('house_lock_pay')
  160. ->insert([
  161. 'house_lock_id' => $lid,
  162. 'pay_id' => $v['id']
  163. ]);
  164. if (!$res) {
  165. exception('操作失败');
  166. }
  167. }
  168. foreach ($data['fees'] as $k=>$v){
  169. $res = Db::name('house_lock_fee')
  170. ->insert([
  171. 'house_lock_id' => $lid,
  172. 'fee_id' => $v['fee_id'],
  173. 'money' => $v['money']
  174. ]);
  175. if (!$res) {
  176. exception('操作失败');
  177. }
  178. }
  179. $this->commit();
  180. return true;
  181. }catch (Exception $e){
  182. $this->error = $e->getmessage();
  183. $this->rollback();
  184. return false;
  185. }
  186. }
  187. public function deal($id,$status,$userId){
  188. $info = $this->where('id',$id)->find();
  189. if(!$info){
  190. $this->error = '记录不存在';
  191. return false;
  192. }
  193. $info = json_decode(json_encode($info),true);
  194. if($info['status'] != 0){
  195. $this->error = '无权限操作';
  196. return false;
  197. }
  198. $res = $this->where('id',$id)->update([
  199. 'status' => $status,
  200. 'deal_user_id' => $userId,
  201. 'update_time' => getTime()
  202. ]);
  203. if (!$res) {
  204. $this->error = '操作失败';
  205. return false;
  206. }
  207. return true;
  208. }
  209. public function getDataById($id){
  210. $info = $this->where('id',$id)->find();
  211. if(!$info){
  212. $this->error = '记录不存在';
  213. return false;
  214. }
  215. $lists = Db::name('house_lock_pay')
  216. ->alias('hlp')
  217. ->join('house_pay hp','hp.id = hlp.pay_id')
  218. ->join('owner o','o.id = hp.owner_id')
  219. ->join('user u','u.id = hp.create_user_id')
  220. ->where('hlp.house_lock_id',$id)
  221. ->field('hp.*,o.name,u.real_name')
  222. ->select();
  223. $lists = $lists?$lists:[];
  224. $payIds = [];
  225. foreach ($lists as $k=>$v){
  226. $payIds[] = $v['id'];
  227. }
  228. $logs = Db::name('house_pay_log_view')
  229. ->where('pay_id','in',$payIds)
  230. ->select();
  231. $fees =Db::name('house_lock_fee')
  232. ->alias('hlf')
  233. ->join('house_fee hf','hf.id = hlf.fee_id')
  234. ->where('hlf.house_lock_id',$id)
  235. ->field('hlf.*,hf.title as fee_title')
  236. ->select();
  237. $data = [
  238. 'list' => $lists,
  239. 'data' => json_decode(json_encode($info),true),
  240. 'logs' => $logs?$logs:[],
  241. 'fees' => $fees?$fees:[]
  242. ];
  243. return $data;
  244. }
  245. }