Esempio n. 1
0
 /**
  * 每天更新所有账单的滞纳金
  * 规则:逾期第10天起,每天增加贷款总金额1%的滞纳金(多期逾期仅算一期)
  */
 public function updateDelayDay()
 {
     // 逾期百分点
     $rate = 1;
     // 获取所有已经逾期的账单
     $orderModel = new \Loan\Model\OrderModel();
     $model = $this->alias('b')->join('zj_loan_order o on o.id=b.order_id');
     $bills = $model->field('o.status order_status, o.loan_money, b.order_id,b.issue,b.status,b.money_principal,b.return_limit_time')->where(['b.issue' => ['gt', 0], 'b.status' => ['in', '0,1'], 'b.return_limit_time' => ['elt', strtotime(date('Y-m-d 23:59:59', strtotime('-10 day')))]])->order('b.return_limit_time asc')->select();
     // 合法的订单状态
     $check_order_status = array_merge($orderModel->allowRepayStatus(), [$orderModel::STATUS_BREACH_CONTRACT]);
     $exitst_order = [];
     $total_success = 0;
     $date = date('Y-m-d');
     $delayModel = M('loan_bill_delay');
     foreach ($bills as $bill) {
         // 同一个订单,仅发送一次
         $id = $bill['order_id'];
         if (in_array($id, $exitst_order)) {
             continue;
         }
         $exitst_order[] = $id;
         // 判断订单状态是否正常
         if (!in_array($bill['order_status'], $check_order_status)) {
             system_warn('更新滞纳金时发现异常记录:' . print_r($bill, true));
             continue;
         }
         // 组装数据
         $data = ['order_id' => $bill['order_id'], 'issue' => $bill['issue'], 'delay_date' => $date, 'type' => 1];
         // 判断重复处理
         if ($delayModel->where($data)->count() > 0) {
             continue;
         }
         // 计算滞纳金
         $delay_money = $bill['loan_money'] * $rate / 100;
         // 启用事务
         $this->startTrans();
         if ($this->where(['order_id' => $bill['order_id'], 'issue' => $bill['issue'], 'status' => $bill['status']])->save(['money_delay' => ['exp', 'money_delay+' . $delay_money], 'money_total' => ['exp', 'money_total+' . $delay_money]])) {
             $data['created'] = time();
             $data['uid'] = 0;
             $data['uname'] = '系统';
             $data['money_principal'] = $bill['money_principal'];
             $data['delay_money'] = $delay_money;
             $delayModel->add($data);
             // 若逾期达到了16天,则更新订单状态(避免继续执行还款操作)
             if ($bill['order_status'] != $orderModel::STATUS_BREACH_CONTRACT) {
                 $diff_day = (strtotime(date('Y-m-d')) - strtotime(date('Y-m-d', $bill['return_limit_time']))) / 86400;
                 if ($diff_day >= 16) {
                     if (!$orderModel->breach($bill['order_id'])) {
                         system_warn('更新订单违约时失败:' . $orderModel->getError());
                         $this->rollback();
                         continue;
                     }
                 }
             }
             // 提交事务
             $this->commit();
             $total_success++;
         } else {
             system_warn('更新滞纳金时,无法正确更新记录:' . print_r($bill, true));
             $this->rollback();
         }
     }
     return $total_success;
 }