Beispiel #1
0
 /**
  * 取消还款
  *
  * @param int $order_id 订单号
  * @param mixed $issue 期数,字符串all表示取消全部还款,其它为数字,表示指定一期
  */
 public function repayCancel($order_id, $issue)
 {
     $user = get_user();
     if (!$user) {
         return $this->_error('无法读取当前操作人信息!');
     }
     // 获取订单并判断状态
     $orderModel = new \Loan\Model\OrderModel();
     $order = $orderModel->find($order_id);
     if (!$order) {
         return $this->_error('订单不存在!');
     }
     // 判断借款人
     $loan_user = get_user($order['uid']);
     if (!$loan_user) {
         return $this->_error('无法读取借款人信息!');
     }
     // 取消提前部分/全部还款
     if ($issue === 'part' || $issue == 'all') {
         $check_status = $issue == 'part' ? [$orderModel::STATUS_DELIVERY, $orderModel::STATUS_REPAY, $orderModel::STATUS_FINISH_SUCCESS] : [$orderModel::STATUS_FINISH_SUCCESS];
         if (!in_array($order['status'], $check_status)) {
             return $this->_error('当前订单状态不允许执行取消还款!');
         }
         $log = M('loan_order_log')->where(['order_id' => $order_id, 'action' => $issue == 'part' ? [['like', '提前部分%'], ['like', '提前全部%期还款'], 'or'] : '提前全部还款'])->order('id desc')->find();
         if (!$log) {
             return $this->_error('找不到还款操作日志!');
         }
         $total_money = explode(':', $log['remark'])[1] + 0;
         if ($total_money <= 0) {
             return $this->_error('无法读取操作的还款金额!');
         }
         // 根据操作时间读取当时操作的账单
         $bills = $this->where(['order_id' => $order_id, 'status' => 2, 'return_time' => $log['created']])->select();
         if (!$bills) {
             return $this->_error('无法读取当初操作的账单!');
         }
         // 判断订单涉及到的总金额
         $total_bill_money = 0;
         $total_money_principal = 0;
         $total_money_fee = 0;
         foreach ($bills as $vo) {
             $total_bill_money += $vo['return_money'];
             $total_money_principal += $vo['money_principal'];
             $total_money_fee += max(0, $vo['return_money'] - $vo['money_principal']);
         }
         if (bccomp($total_bill_money, $total_money, 2) != 0) {
             return $this->_error('账单还款金额[' . $total_bill_money . ']与操作日志金额[' . $total_money . ']不符,请联系技术改进!');
         }
         // 读取资金记录,判断金额
         $last_money_log = M('loan_money')->where(['type' => ['in', '2,3'], 'order_id' => $order_id, 'issue' => 0])->order('id desc')->field('id,money')->find();
         if (!$last_money_log) {
             return $this->_error('找不到资金记录编号!');
         }
         if (bccomp($last_money_log['money'], $total_money, 2) != 0) {
             return $this->_error('日志还款金额[' . $last_money_log['money'] . ']与资金记录金额[' . $total_money . ']不一致!');
         }
         // 开启事务,逐个账单进行处理
         $this->startTrans();
         $now = time();
         // 更新账单信息
         $this->where(['order_id' => $order_id, 'status' => 2, 'return_time' => $log['created']])->save(['status' => 0, 'return_time' => 0, 'return_money' => 0]);
         // 更新用户已归还金额(仅限本金)
         M('loan_user')->where('uid=' . $order['uid'])->setDec('total_money_return', $total_money_principal);
         M('loan_user')->where('uid=' . $order['uid'])->setDec('total_money_remain', $total_money_principal);
         // 更新 - 金额统计
         if ($total_money_fee > 0) {
             $statModel = new \Loan\Model\StatModel();
             $statModel->update($statModel::TYPE_ORDER_MONEY_FEE, $loan_user['university_id'], $total_money_fee * -1, $log['created']);
         }
         // 更新 - 订单最后还款期号和已还款金额
         if (!$orderModel->where(['id' => $order_id, 'status' => $order['status']])->save(['status' => $log['status'], 'last_bill_issue' => ['exp', '(select min(issue) from zj_loan_bill where order_id=id and status in (0,1))'], 'loan_money_return' => ['exp', 'loan_money_return-' . $total_money_principal]])) {
             return $this->_error('更新账单状态失败,请刷新后重试!', true);
         }
         // 新增订单日志
         M('loan_order_log')->add(['order_id' => $order_id, 'created' => $now, 'uid' => $user['uid'], 'uname' => $user['uname'], 'status' => $order['status'], 'action' => '取消' . $log['action'], 'remark' => '共' . count($bills) . '期,原提前还款金额' . $total_money . '元,涉及本金:' . $total_money_principal . '元']);
         // 删除资金记录
         M('loan_money')->where(['id' => $last_money_log['id']])->delete();
         // 删除账单支付记录
         M('loan_bill_pay')->where(['order_id' => $order_id, 'created' => $log['created']])->delete();
         // 订单金融数据
         M('loan_order_finance')->where(['order_id' => $order_id])->setDec('return_money', $total_money);
         // 提交事务
         $this->commit();
         return true;
     }
     // 指定某一期
     $issue = intval($issue);
     if ($issue < 1) {
         return $this->_error('还款期数必需大于0!');
     }
     if (!in_array($order['status'], [$orderModel::STATUS_DELIVERY, $orderModel::STATUS_LEND, $orderModel::STATUS_REPAY, $orderModel::STATUS_BREACH_CONTRACT, $orderModel::STATUS_FINISH_SUCCESS])) {
         return $this->_error('当前订单状态不允许执行取消还款!');
     }
     // 读取账单并判断状态
     $bill = $this->where(['order_id' => $order_id, 'issue' => $issue])->find();
     if (!$bill) {
         return $this->_error('找不到此账单信息!');
     }
     if (!in_array($bill['status'], [0, 2])) {
         return $this->_error('账单状态异常!');
     }
     // 所要取消的金额
     $total_money = $bill['return_money'];
     if ($total_money <= 0) {
         return $this->_error('此账单尚未还款!');
     }
     // 获取需要删除的支付日志
     $pay_logs = M('loan_bill_pay')->where(['order_id' => $order_id, 'issue' => $issue])->select();
     if (count($pay_logs) < 1) {
         return $this->_error('找不到账单支付记录!');
     }
     // 判断账单金额是否正确
     $pay_log_money = 0;
     foreach ($pay_logs as $pay_log) {
         $pay_log_money += $pay_log['money'];
     }
     if (bccomp($pay_log_money, $total_money, 2) != 0) {
         return $this->_error('账单支付日志总金额[' . $pay_log_money . ']与账单已返款金额[' . $total_money . ']不符!');
     }
     // 判断资金流金额是否正确
     $money_logs = M('loan_money')->where(['type' => ['in', '2,3'], 'order_id' => $order_id, 'issue' => $issue])->select();
     if (count($money_logs) < 1) {
         return $this->_error('找不到资金记录!');
     }
     $money_log_money = 0;
     foreach ($money_logs as $money_log) {
         $money_log_money += $money_log['money'];
     }
     if (bccomp($money_log_money, $total_money, 2) != 0) {
         return $this->_error('资金交易总金额[' . $money_log_money . ']与账单已返款金额[' . $total_money . ']不符!');
     }
     $now = time();
     // 更新 - 账单
     $this->startTrans();
     if (!$this->where(['order_id' => $order_id, 'issue' => $issue, 'status' => $bill['status']])->save(['status' => 0, 'return_time' => 0, 'return_money' => 0])) {
         $this->rollback();
         return $this->_error('操作失败,请稍后重试!');
     }
     // 删除支付日志
     M('loan_bill_pay')->where(['order_id' => $order_id, 'issue' => $issue])->delete();
     // 删除资金记录
     M('loan_money')->where(['type' => ['in', '2,3'], 'order_id' => $order_id, 'issue' => $issue])->delete();
     // 获取之前的状态,用于回退状态
     $before_status = M('loan_order_log')->where(['order_id' => $order_id])->order('id desc')->getField('status');
     // 新增订单日志
     M('loan_order_log')->add(['order_id' => $bill['order_id'], 'created' => $now, 'uid' => $user['uid'], 'uname' => $user['uname'], 'status' => $order['status'], 'action' => '取消还款', 'is_system' => 1, 'remark' => '第' . $issue . '期,金额:' . $total_money]);
     // 更新 - 订单状态
     if ($order['status'] == $orderModel::STATUS_FINISH_SUCCESS) {
         if (!$before_status || !$orderModel->where(['id' => $order_id, 'status' => $order['status']])->save(['status' => $before_status])) {
             $this->rollback();
             return $this->_error('更新账单状态失败,请联系技术检查!');
         }
     }
     // 账单从已还款变为未返款
     if ($bill['status'] == 2) {
         // 更新用户已归还金额(仅限本金)
         M('loan_user')->where('uid=' . $order['uid'])->setDec('total_money_return', $bill['money_principal']);
         M('loan_user')->where('uid=' . $order['uid'])->setDec('total_money_remain', $bill['money_principal']);
         // 更新 - 金额统计
         $money_fee = $bill['return_money'] - $bill['money_principal'];
         if ($money_fee > 0) {
             $statModel = new \Loan\Model\StatModel();
             $statModel->update($statModel::TYPE_ORDER_MONEY_FEE, $loan_user['university_id'], $money_fee * -1, $pay_log['created']);
         }
         // 更新 - 订单最后还款期号和已还款金额
         $orderModel->where(['id' => $order_id])->save(['loan_money_return' => ['exp', 'loan_money_return-' . $bill['money_principal']]]);
     }
     // 更新 - 订单最后还款期号
     $orderModel->where(['id' => $order_id])->save(['last_bill_issue' => ['exp', '(select min(issue) from zj_loan_bill where order_id=id and status in (0,1))']]);
     M('loan_order_finance')->where(['order_id' => $order_id])->setDec('return_money', $total_money);
     $this->commit();
     return true;
 }
Beispiel #2
0
 public function lendFailure($order_id, $reason)
 {
     // 读取订单
     $order = $this->_getOrder($order_id, [self::STATUS_CHECK_SUCCESS, self::STATUS_LEND, self::STATUS_DELIVERY, self::STATUS_REPAY]);
     if (!$order) {
         return false;
     }
     // 获取打款的金额
     $money = M('loan_order_finance')->where(['order_id' => $order_id, 'lend_status' => 2])->getField('lend_money');
     if ($money <= 0) {
         return $this->_error('无法获取已打款的金额!', true);
     }
     // 更新打款状态
     if (!M('loan_order_finance')->where(['order_id' => $order_id, 'lend_status' => 2])->save(['lend_status' => 3, 'lend_failure_reason' => $reason])) {
         return $this->_error('更新失败,请刷新后重试!', true);
     }
     $this->save(['id' => $order['id'], 'lend_status' => 3]);
     // 反向操作
     M('loan_money')->where(['type' => -1, 'order_id' => $order['id'], 'issue' => 0, 'money' => $money])->delete();
     M('loan_order_finance')->where(['order_id' => $order_id])->setDec('lend_money', $money);
     // 更新签约通过的各学校贷款金额
     $university_id = M('university_campus')->where('id=' . $order['campus_id'])->getField('university_id');
     if ($university_id < 1) {
         return $this->_error('无法读取学校信息!', true);
     }
     $statModel = new \Loan\Model\StatModel();
     $statModel->update($statModel::TYPE_ORDER_MONEY_LEND, $university_id, $money * -1);
     // 提交事务
     $this->commit();
     // 给专员发信息
     $salesman_uid = M('loan_order_salesman')->where(['order_id' => $order['id']])->getField('uid');
     if ($salesman_uid) {
         $salesman = M('loan_salesman')->find($salesman_uid);
         if ($salesman) {
             $msg = '订单打款失败,原因:' . $reason . ';订单信息:' . $order['title'] . ',' . $order['id'] . ',' . $order['realname'];
             $msg .= ',' . M('loan_user_contract')->where(['uid' => $order['uid']])->getField('id_card');
             sms($salesman['mobile'], $msg);
         }
     }
     $this->_push_salesman($order, '打款失败:' . $reason);
     return true;
 }
    /**
     * @title 修复数据
     *
     * @method get
     */
    public function fix_loan_stat_today($type)
    {
        $statModel = new \Loan\Model\StatModel();
        in_array($type, ['in', $statModel::TYPE_ORDER_MONEY_CHECK_SUCCESS, $statModel::TYPE_ORDER_MONEY_LEND]) or $this->error('不支持此类型!');
        if ($type == $statModel::TYPE_ORDER_MONEY_CHECK_SUCCESS) {
            $contractModel = M()->table('zj_loan_order o')->join('zj_loan_order_contract c on
c.order_id=o.id')->join('zj_university_campus campus on
campus.id=o.campus_id');
            $orders = $contractModel->where(['c.check_time' => [['egt', strtotime(date('Y-m-d') . ' 00:00:00')]], 'c.check_status' => 1])->field('o.loan_money,c.check_time,campus.university_id')->select();
            $orders or die('no data');
            $statModel->where(['type' => $type, 'target_date' => date('Y-m-d')])->delete();
            foreach ($orders as $order) {
                $statModel->update($type, $order['university_id'], $order['loan_money'], $order['check_time']);
            }
            die('done');
        }
        if ($type == $statModel::TYPE_ORDER_MONEY_LEND) {
            $financeModel = M()->table('zj_loan_order o')->join('zj_loan_order_finance f on f.order_id=o.id')->join('zj_university_campus c on c.id=o.campus_id');
            $orders = $financeModel->where(['f.lend_time' => ['egt', strtotime(date('Y-m-d') . ' 00:00:00')]])->field('f.lend_money,f.lend_time,c.university_id')->select();
            $orders or die('no data');
            $statModel->where(['type' => $type, 'target_date' => date('Y-m-d')])->delete();
            foreach ($orders as $order) {
                $statModel->update($type, $order['university_id'], $order['lend_money'], $order['lend_time']);
            }
            die('done');
        }
    }
 protected function _report_breach($where, $where_order)
 {
     $orderModel = new \Loan\Model\OrderModel();
     $data = [];
     $statModel = new \Loan\Model\StatModel();
     $statModel->updateBreach();
     $where = ['type' => ['in', [$statModel::TYPE_LOAN_MONEY_UNREPAY, $statModel::TYPE_LOAN_MONEY_BREACH, $statModel::TYPE_LOAN_MONEY_BREACH_REPAY, $statModel::TYPE_LOAN_MONEY_BREACH_UNREPAY]]];
     list($city, $is_city_id) = $this->_initRegion();
     if ($city > 0) {
         $where['university_id'] = ['exp', ' in (select university_id from zj_university_campus where ' . ($is_city_id ? 'city_id' : 'province_id') . '=' . $city . ')'];
     }
     for ($month = '2015-04-01'; $month < date('Y-m-01');) {
         $where['target_date'] = $month;
         $data[substr($month, 0, -3)] = $statModel->where($where)->group('type')->getField('type, sum(number)');
         $month = date('Y-m-d', strtotime('+1 month', strtotime($month)));
     }
     $where['target_date'] = date('Y-m-01');
     $data['当前'] = $statModel->where($where)->group('type')->getField('type, sum(number)');
     foreach ($data as &$row) {
         for ($i = 7; $i <= 10; $i++) {
             $row[$i] = number_format(isset($row[$i]) ? $row[$i] / 10000 : 0, 2);
         }
     }
     $this->assign('data', $data);
 }
 public function checkSuccess($id)
 {
     $user = get_user();
     if (!$user) {
         $this->error = '无法读取当前操作人信息!';
         return false;
     }
     // 获取记录
     $contract = $this->find($id);
     if (!$contract) {
         $this->error = '认证尚未提交!';
         return false;
     }
     // 判断状态
     if ($contract['status'] != 0) {
         $this->error = '当前状态不为待审核!';
         return false;
     }
     // 验证资料是否上传完全
     $auth_ids = M('loan_config_auth')->where('type=1 and status=1 and is_required=1')->getField('id', true);
     if ($auth_ids && M('loan_user_contract_auth')->where(['uid' => $contract['uid'], 'auth_id' => ['in', $auth_ids]])->count() != count($auth_ids)) {
         $this->error = '请先上传所有认证资料';
         return false;
     }
     // 读取认证配置
     $auth_data = [];
     // 开启事务
     $this->startTrans();
     // 更新状态
     $this->where('uid=' . $contract['uid'] . ' and status=0')->setField('status', 1);
     // 更新订单状态
     $orderModel = D('Loan/order');
     $orders = $orderModel->where('uid=' . $contract['uid'] . ' and status=' . $orderModel::STATUS_UNCONTRACT)->getField('id', true);
     if ($orders) {
         foreach ($orders as $order_id) {
             if (!$orderModel->userAuthSuccess($order_id)) {
                 $this->rollback();
                 return $this->_error($orderModel->getError());
             }
         }
     }
     // 新增正式认证用户
     if (M('loan_user')->where(['uid' => $contract['uid']])->count() == 0) {
         if (!M('loan_user')->add(['uid' => $contract['uid'], 'created' => time(), 'realname' => $contract['realname'], 'status' => 1, 'money_limit' => $contract['money_limit'], 'total_money_remain' => $contract['money_limit']])) {
             $this->rollback();
             $this->error = '创建认证用户数据失败!';
             return false;
         }
     } else {
         system_warn('检测到重复的认证用户记录!');
     }
     // 额度日志
     if (!M('loan_user_money_limit_log')->add(['user_id' => $contract['uid'], 'created' => time(), 'uid' => $user['uid'], 'uname' => $user['uname'], 'reason' => '认证成功', 'money_limit_before' => 0, 'money_limit_after' => $contract['money_limit']])) {
         $this->rollback();
         $this->error = '创建用户额度记录失败!';
         return false;
     }
     $statModel = new \Loan\Model\StatModel();
     $statModel->update($statModel::TYPE_CONTRACT_SUCCESS, $user['university_id']);
     // 积分
     D('Credit')->addByKey($contract['uid'], 'user_auth');
     // 更新用户基本信息 - 真实姓名
     M('user')->where(['uid' => $contract['uid'], 'realname' => ''])->setField('realname', $contract['realname']);
     // 提交事务
     $this->commit();
     // 发送推送消息
     $this->_push($contract['mobile'], '您的认证已通过审核!');
     return true;
 }