| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663 | <?phpnamespace app\common\model;use think\Db;use think\Exception;class HousePay extends Base{    public function createOrder($houseId,$lastDate,$order,$userId,$orgId){        $info = Db::name('house')->where('id',$houseId)            ->where('del',0)->find();        if(!$info||!$info['owner_id']||$info['enable']==0){            $this->error = '业主不存在或未绑定业主';            return false;        }        $owner = Db::name('owner')->              where('id',$info['owner_id'])            ->where('del',0)->find();        if(!$owner){            $this->error = '业主不存在';            return false;        }        $this->startTrans();        try{            $data = [                'org_id' => $orgId,                'last_time' => $lastDate,                'sn' => get_unique_id(),                'status' => 0,                'create_time' => getTime(),                'create_user_id' => $userId,                'money' => 0,                'owner_id' => $info['owner_id'],                'area_id' => $info['area_id'],                'pre_money' => 0,            ];            $money = 0;            foreach ($order as $k=>$v){                $money += $v['total_price'];            }            $lastMoney = 0;            if($owner['money'] > $money){                $lastMoney = $owner['money'] - $money;                $data['money'] = 0;                $data['pre_money'] = $money;            }else{                $data['money'] = round($money - $owner['money'],2);                $data['pre_money'] = $owner['money'];            }            // 修改业主剩余金额            $res = Db::name('owner')                ->where('id',$info['owner_id'])                ->update( [                'money' => $lastMoney,                'update_time'=>getTime()            ]);            if (!$res) {                 exception('业主余额修改失败');            }            $payid = Db::name('house_pay')->insertGetId($data);            if (!$res) {                exception('保存记录失败');            }            foreach ($order as $k=>$v){                $log = [                    'org_id' => $orgId,                    'type' => $v['type'],                    'bus_id' => $v['id'],                    'fee_id' => $v['fee_id'],                    'pay_id' => $payid,                    'money' => $v['total_price'],                    'start' => $v['start']?$v['start']:null,                    'end' => $v['end']?$v['end']:null,                    'remark' => $v['remark']                ];                $logid = Db::name('house_pay_log')->insertGetId($log);                if (!$logid) {                     exception('操作失败');                }                $months = $v['months'];                if($months){                    $lms = [];                    foreach ($months as $kk=>$vv){                        $lms[] = [                            'log_id' => $logid,                            'month' => $vv['year'].'-'.(strlen($vv['month']) == 1?'0'.$vv['month']:$vv['month']),                            'money' => $vv['money']                        ];                    }                    $res = Db::name('house_pay_log_money')                        ->insertAll($lms);                    if (!$res) {                        exception('操作失败');                    }                }            }            $this->commit();            return $payid;        }catch (Exception $e){            $this->error = $e->getmessage();            $this->rollback();            return false;        }    }    public function show_info($id){        $info = $this->where('id',$id)            ->where('del',0)->find();        if($info){            $info['pay_data'] = $info['pay_data']?json_decode($info['pay_data'],true):[];            $info['xq_title'] = $this->getTableField('house_areas',['id'=>$info['area_id']],'title');            $info['full_name'] = $this->getTableField('owner',['id'=>$info['owner_id']],'name');            $info['full_money'] = $this->getTableField('owner',['id'=>$info['owner_id']],'money');            $logs = Db::name('house_pay_log')                ->where('pay_id',$id)->select();            $titles = [];            $ids = [];            foreach ($logs as $k=>$v){                $logs[$k]['full_title'] = $this->getTableField('house_view',['id'=>$v['bus_id']],'full_title');                if(!in_array($v['bus_id'],$ids)){                    $ids[] = $v['bus_id'];                    $house = Db::name('house')->where('id',$v['bus_id'])                        ->find();                    if($house && $house['cate'] == 1){                        $titles[] = '[住房]'.$house['title'];                    }else if($house && $house['cate'] == 2){                        $titles[] = '[商铺]'.$house['title'];                    }else if($house && $house['cate'] == 3){                        $titles[] = '[营业房]'.$house['title'];                    }else if($house && $house['cate'] == 4){                        $titles[] = '[储藏室]'.$house['title'];                    }else if($house && $house['cate'] == 5){                        $titles[] = '[停车位]'.$house['title'];                    }                }                $logs[$k]['cate'] = $this->getTableField('house_view',['id'=>$v['bus_id']],'cate');                $logs[$k]['fee_title'] = $this->getTableField('house_fee',['id'=>$v['fee_id']],'title');            }            $info['logs'] = $logs?$logs:[];            $info['titles'] = implode(',',$titles);            $info['dx_money'] = $this->convertamounttocn($info['true_money']);        }        return $info;    }    public function paysave($post,$userId){        $info = $this            ->where('id',$post['id'])            ->where('del',0)            ->find();        if(!$info){            $this->error = '记录不存在';            return false;        }        if($info['status'] == 1){            $this->error = '该记录已缴费';            return false;        }        if($info['create_user_id'] != $userId){            $this->error = '无权限操作';            return false;        }        if($post['true_money'] < $info['money']){            $this->error = '实收金额必须大于等于总金额';            return false;        }        $dymoney = $post['true_money'] - $info['money']; // 多余的存入预付款        $premoney = $this->getTableField('owner',['id'=>$info['owner_id']],'money'); // 剩余预付款        $premoney = $premoney?$premoney:0;        $lastmoney = round($dymoney + $premoney,2); //当前剩余预付款        $this->startTrans();        try{            $data = [                'status' => 1,                'remark' => $post['remark'],                'pay_time' => $post['pay_time'],                'pay_type' => $post['pay_type'],                'true_money' => $post['true_money'],                'last_pre_money' => $lastmoney,                'payee' => $post['payee'],                'pay_data' => $post['pay_data']            ];            $ret = $this->where('id',$post['id'])                ->update($data);            if (!$ret) {                 exception('操作失败');            }            $ret = Db::name('owner')->where('id',$info['owner_id'])                ->update( [                'money' => $lastmoney,                'update_time'=>getTime()            ]);            if (!$ret) {                exception('操作失败');            }            $this->commit();            return true;        }catch (Exception $e){            $this->error = $e->getmessage();            $this->rollback();            return false;        }    }    // 返销    public function cancel($post,$id,$userId){        $info = $this->where('id',$id)->find();        if(!$info||$info['del'] == 1){            $this->error = '记录不存在';            return false;        }        if($info['status'] == 2||$info['status'] == 3){            $this->error = '缴费单已返销';            return false;        }        $ret = $this->checkfinish($id);        if($ret){            $this->error = '已扎帐缴费单不能返销';            return false;        }        $this->startTrans();        try{            $data = [                'cancel_reason' => isset($post['cancel_reason'])?$post['cancel_reason']:'',            ];            if($info['status'] == 0){ //未缴费可直接返销                $data['cancel_user_id'] = $userId;                $data['cancel_time'] = getTime();                $data['status'] = 3;                $data['is_cancel'] = 2;            }else{ // 已缴费需要财务审核返销                $data['status'] = 2;                $data['cancel_user_id'] = 0;                $data['cancel_time'] = null;                $data['is_cancel'] = 1;            }            $ret = $this->where('id',$id)                ->update($data);            if (!$ret) {                 exception('操作失败');            }            $this->commit();            return true;        }catch (Exception $e){            $this->rollback();            $this->error = $e->getmessage();            return false;        }    }    // 检查是否已扎帐    public function checkFinish($id){        $ret = Db::name('house_lock_pay')            ->alias('hlp')            ->join('house_lock hl','hl.id = hlp.house_lock_id')            ->where('hl.status','in',[0,1])            ->where('hlp.pay_id',$id)            ->field('hlp.*')            ->find();        return $ret?true:false; // true=已扎帐    }    public function show_all_fee($orgId,$searchtext){        if(isset($searchtext['name']) && $searchtext['name'] !=''){            $map1[] = ['name','like','%'.$searchtext['name'].'%'];            $user = Db::name('owner')                ->where($map1)                ->where('del',0)                ->where('enable',1)                ->column('id');            if(!empty($user)){                $map[] = ['hp.house_id','in',$user];            }else{                $map[] = ['hp.house_id','=',-1];            }        }        if(isset($searchtext['status']) && $searchtext['status'] !=''){            $map[] = ['hp.status','=',$searchtext['status']];        }        $b = isset($searchtext['start'])?$searchtext['start']:'';        $e = isset($searchtext['end'])?$searchtext['end']:'';        if($b && $e){            if($b <= $e){                $b = date('Y-m-d 00:00:00',strtotime($b));                $e = date('Y-m-d 23:59:59',strtotime($e));                $map[] = ['hp.create_time','>=',$b];                $map[] = ['hp.create_time','<=',$e];            }        }        if($b && $e==''){            $b = date('Y-m-d 00:00:00',strtotime($b));            $map[] = ['hp.create_time','>=',$b];        }        if($b=='' && $e){            $e = date('Y-m-d 23:59:59',strtotime($e));            $map[] = ['hp.create_time','<=',$e];        }        $map2 = [];        $c = isset($searchtext['wstart'])?$searchtext['wstart']:'';        $d = isset($searchtext['wend'])?$searchtext['wend']:'';        if($c && $d){            if($c <= $d){                $c = date('Y-m-d',strtotime($c));                $d = date('Y-m-d',strtotime($d));                $map2[] = ['hp.end','>=',$c];                $map2[] = ['hp.start','<=',$d];            }        }        if($c && $d==''){            $c = date('Y-m-d',strtotime($c));            $map2[] = ['hp.end','>=',$c];        }        if($c=='' && $d){            $d = date('Y-m-d',strtotime($d));            $map2[] = ['hp.start','<=',$d];        }        if(!empty($map2)){            $logs = Db::name('house_pay_log')->where($map2)                ->distinct(true)->column('pay_id');            if($logs){                $map[] = ['hp.id','in',$logs];            }else{                $map[] = ['hp.id','=',-1];            }        }        $map[] = ['hp.del','=',0];        $map[] = ['hp.org_id','=',$orgId];        $fees = Db::name('house_pay_log')            ->alias('hpl')            ->join('house_pay hp','hp.id = hpl.pay_id')            ->join('house_fee hf','hf.id = hpl.fee_id')            ->field('hpl.fee_id,hf.title')            ->group('hpl.fee_id')            ->distinct(true)            ->select();        return $fees;    }    public function getFeeSearch(){        $sn = input('sn','','trim');        if($sn){            $map[] = ['sn','like','%'.$sn.'%'];        }        $area_title = input('area_title','','trim');        if($area_title){            $map[] = ['area_title','like','%'.$area_title.'%'];        }        $title = input('title','','trim');        if($title){            $map[] = ['title','like','%'.$title.'%'];        }        $name = input('name','','trim');//用户        if($name){            $map[] = ['name','like','%'.$name.'%'];        }        $cate = input('cate','','trim');        if($cate != ''){            $map[] = ['cate','=',$cate];        }        $fee = input('fee','','trim');        if($fee != ''){            $map[] = ['fee_id','=',$fee];        }        $status = input('status','','trim');        if($status != ''){            $map[] = ['status','=',$status];        }        $c_type = input('c_type','','trim');        if($c_type != ''){            $map[] = ['c_type','=',$c_type];        }        $b = input('start','','trim');        $e = input('end','','trim');        if($b){            $b = date('Y-m-d 00:00:00',strtotime($b));            $map[] = ['create_time','>=',$b];        }        if($e){            $e = date('Y-m-d 23:59:59',strtotime($e));            $map[] = ['create_time','<=',$e];        }        $c = input('wstart','','trim');        $d = input('wend','','trim');        if($c){            $c = date('Y-m-d',strtotime($c));            $map[] = ['end','>=',$c];        }        if($d){            $d = date('Y-m-d',strtotime($d));            $map[] = ['start','<=',$d];        }        $map[] = ['org_id','=',cur_org_id()];        $map= empty($map) ? true: $map;        return $map;    }    /**     * 将数值金额转换为中文大写金额     * @param $amount float 金额(支持到分)     * @param $type   int   补整类型,0:到角补整;1:到元补整     * @return mixed 中文大写金额     */    public function convertAmountToCn($num, $type = 0) {        $c1 = "零壹贰叁肆伍陆柒捌玖";        $c2 = "分角元拾佰仟万拾佰仟亿";        //精确到分后面就不要了,所以只留两个小数位        $num = round($num, 2);        //将数字转化为整数        $num = $num * 100;        if (strlen($num) > 10) {            return "金额太大";        }        $i = 0;        $c = "";        while (1) {            if ($i == 0) {                //获取最后一位数字                $n = substr($num, strlen($num)-1, 1);            } else {                $n = $num % 10;            }            //每次将最后一位数字转化为中文            $p1 = substr($c1, 3 * $n, 3);            $p2 = substr($c2, 3 * $i, 3);            if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {                $c = $p1 . $p2 . $c;            } else {                $c = $p1 . $c;            }            $i = $i + 1;            //去掉数字最后一位了            $num = $num / 10;            $num = (int)$num;            //结束循环            if ($num == 0) {                break;            }        }        $j = 0;        $slen = strlen($c);        while ($j < $slen) {            //utf8一个汉字相当3个字符            $m = substr($c, $j, 6);            //处理数字中很多0的情况,每次循环去掉一个汉字“零”            if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {                $left = substr($c, 0, $j);                $right = substr($c, $j + 3);                $c = $left . $right;                $j = $j-3;                $slen = $slen-3;            }            $j = $j + 3;        }        //这个是为了去掉类似23.0中最后一个“零”字        if (substr($c, strlen($c)-3, 3) == '零') {            $c = substr($c, 0, strlen($c)-3);        }        //将处理的汉字加上“整”        if (empty($c)) {            return "零元整";        }else{            return $c;        }    }    // 退款    public function refund($post,$id,$userId,$orgId=0){        $info = Db::name('house_pay_log')->            where('id',$id)->find();        if(!$info){            $this->error = '记录不存在';            return false;        }        if($info['status'] != 0){            $this->error = '无权限操作';            return false;        }        $pInfo =$this            ->where('id',$info['pay_id'])            ->where('del',0)            ->find();        if(!$pInfo){            $this->error = '记录不存在';            return false;        }        if($pInfo['create_user_id'] != $userId){            $this->error = '无权限操作';            return false;        }        $refundMoney =Db::name('house_refund_log')            ->where('pay_id',$id)            ->where('status','in',[1,2])            ->sum('money');        if($post['money'] > $info['money']){            $this->error = '退款金额不能大于实收金额';            return false;        }        if($post['money']+$refundMoney > $info['money']){            $this->error = '累计退款金额不能大于实收金额';            return false;        }        $this->startTrans();        try{            $data = [                'reason' => isset($post['reason'])?$post['reason']:'',                'refund_user_id' => $userId,                'org_id' => $orgId,                'fee_id' => $info['fee_id'],                'bus_id' => $info['bus_id'],                'pay_id' => $id,                'refund_time' => getTime(),                'status' => 1,                'money' => $post['money'],                'refund_account' => $post['refund_account'],                'refund_name' => $post['refund_name'],                'refund_bank' => $post['refund_bank'],            ];            $ret = Db::name('house_refund_log')                ->insertGetId($data);            if (!$ret) {                 exception('事务失败');            }            $this->commit();            return true;        }catch (Exception $e){            $this->rollback();            $this->error = $e->getmessage();            return false;        }    }    // 处理待审核返销    public function dealpay($status,$id,$userId){        $info = $this->where('id',$id)->find();        if(!$info||$info['del'] == 1){            $this->error = '记录不存在';            return false;        }        if($info['status'] != 2){            $this->error = '缴费单不处于审核状态';            return false;        }        $this->startTrans();        try{            $data = [                'status' => $status,                'cancel_user_id' => $userId,                'cancel_time' => getTime()            ];            $res = $this->where('id',$id)->update($data);            if (!$res) {                 exception('操作失败');            }            if($status == 3){ // 同意                $preMoney = $this->getTableField('owner',['id'=>$info['owner_id']],'money'); // 剩余预付款                $dyMoney = $info['true_money'] - $info['money']; // 多余预付款                if($dyMoney > $preMoney){                    exception('剩余预付款不能为负值');                }                $lastMoney = round($preMoney - $dyMoney,2);                $res = Db::name('owner')                    ->where('id',$info['owner_id'])                    ->update([                    'money' => $lastMoney,                    'update_time' => date('y-m-d h:i:s')                ]);                if (!$res) {                    exception('操作失败');                }            }            $this->commit();            return true;        }catch (Exception $e){            $this->rollback();            $this->error = $e->getmessage();            return false;        }    }    public function disagreeRefund($id,$userId){        $info = Db::name('house_refund_log')              ->where('id',$id)             ->find();        if(!$info){            $this->error = '记录不存在';            return false;        }        if($info['status'] != 1){            $this->error = '无权限操作';            return false;        }        $data = [            'deal_user_id' => $userId,            'deal_time' => getTime(),            'status' => 0        ];        $res = Db::name('house_refund_log')            ->where('id',$id)            ->update($data);        if (!$res) {            $this->error = '操作失败';            return false;        }        return true;    }    public function agreeRefund($id,$userId,$post){        $info = Db::name('house_refund_log')            ->where('id',$id)            ->find();        if(!$info){            $this->error = '记录不存在';            return false;        }        if($info['status'] != 1){            $this->error = '无权限操作';            return false;        }        $data = [            'deal_user_id' => $userId,            'deal_time' => getTime(),            'status' => 2,            'voucher' => $post['voucher'],        ];        $res = Db::name('house_refund_log')            ->where('id',$id)            ->update($data);        if (!$res) {            $this->error = '操作失败';            return false;        }        return true;    }}
 |