public function contact()
 {
     $id = intval($_REQUEST['id']);
     if ($id == 0) {
         echo "不存在的债权";
         die;
     }
     //先执行更新借贷信息
     $deal_id = $GLOBALS['db']->getOne("SELECT deal_id FROM " . DB_PREFIX . "deal_load WHERE id=" . $id);
     if ($deal_id == 0) {
         echo "不存在的债权";
         die;
     } else {
         syn_deal_status($deal_id);
     }
     $condition = ' AND dlt.id=' . $id . ' AND d.deal_status >= 4 and d.is_effect=1 and d.is_delete=0 and d.loantype = 0 and d.repay_time_type =1 and  d.publish_wait=0 and (dlt.user_id=' . $GLOBALS['user_info']['id'] . ' or dlt.t_user_id = ' . $GLOBALS['user_info']['id'] . ') ';
     $union_sql = " LEFT JOIN " . DB_PREFIX . "deal_load_transfer dlt ON dlt.deal_id = dl.deal_id ";
     $sql = 'SELECT dlt.id,dlt.deal_id,dlt.load_id,dlt.transfer_amount,dlt.near_repay_time,dlt.user_id,d.next_repay_time,d.last_repay_time,d.rate,d.repay_start_time,d.repay_time,dlt.load_money,dlt.id as dltid,dlt.status as tras_status,dlt.t_user_id,dlt.transfer_amount,dlt.create_time as tras_create_time,dlt.transfer_time,d.user_id as duser_id FROM ' . DB_PREFIX . 'deal_load dl LEFT JOIN ' . DB_PREFIX . 'deal d ON d.id = dl.deal_id ' . $union_sql . ' WHERE 1=1 ' . $condition;
     $transfer = $GLOBALS['db']->getRow($sql);
     if ($transfer) {
         //下个还款日
         $transfer['next_repay_time_format'] = to_date($transfer['near_repay_time'], "Y 年 m 月 d 日");
         $transfer['near_repay_time_format'] = to_date(next_replay_month($transfer['near_repay_time'], " -1 "), "Y 年 m 月 d 日");
         //什么时候开始借
         $transfer['repay_start_time_format'] = to_date($transfer['repay_start_time'], "Y 年 m 月 d 日");
         //还款日
         $transfer['final_repay_time'] = next_replay_month($transfer['repay_start_time'], $transfer['repay_time']);
         $transfer['final_repay_time_format'] = to_date($transfer['final_repay_time'], "Y 年 m 月 d 日");
         //剩余期数
         $transfer['how_much_month'] = how_much_month($transfer['near_repay_time'], $transfer['final_repay_time']) + 1;
         //本息还款金额
         $transfer['month_repay_money'] = pl_it_formula($transfer['load_money'], $transfer['rate'] / 12 / 100, $transfer['repay_time']);
         $transfer['month_repay_money_format'] = format_price($transfer['month_repay_money']);
         //剩余多少钱未回
         $transfer['all_must_repay_money'] = $transfer['month_repay_money'] * $transfer['how_much_month'];
         $transfer['all_must_repay_money_format'] = format_price($transfer['all_must_repay_money']);
         //剩余多少本金未回
         $transfer['left_benjin'] = get_benjin($transfer['repay_time'] - $transfer['how_much_month'] - 1, $transfer['repay_time'], $transfer['load_money'], $transfer['month_repay_money'], $transfer['rate']);
         $transfer['left_benjin_format'] = format_price($transfer['left_benjin']);
         //剩多少利息
         $transfer['left_lixi'] = $transfer['all_must_repay_money'] - $transfer['left_benjin'];
         $transfer['left_lixi_format'] = format_price($transfer['left_lixi']);
         //投标价格
         $transfer['load_money_format'] = format_price($transfer['load_money']);
         //转让价格
         $transfer['transfer_amount_format'] = format_price($transfer['transfer_amount']);
         //转让管理费
         $transfer_fee = $transfer['transfer_amount'] * (double) app_conf("USER_LOAD_TRANSFER_FEE");
         if ($transfer_fee != 0) {
             $transfer_fee = $transfer_fee / 100;
         }
         $transfer['transfer_fee_format'] = format_price($transfer_fee);
         //转让收益
         $transfer['transfer_income_format'] = format_price($transfer['all_must_repay_money'] - $transfer['transfer_amount']);
         if ($transfer['tras_create_time'] != "") {
             $transfer['tras_create_time_format'] = to_date($transfer['tras_create_time'], "Y 年 m 月 d 日");
         }
         $transfer['transfer_time_format'] = to_date($transfer['transfer_time'], "Y 年 m 月 d 日");
         $transfer['user'] = get_user("user_name,real_name,idno,level_id", $transfer['user_id']);
         $transfer['tuser'] = get_user("user_name,real_name,idno,level_id", $transfer['t_user_id']);
         $transfer['duser'] = get_user("user_name,real_name,idno,level_id", $transfer['duser_id']);
         $GLOBALS['tmpl']->assign('transfer', $transfer);
     } else {
         echo "不存在的债权";
         die;
     }
     $contract = $GLOBALS['tmpl']->fetch("str:" . app_conf("TCONTRACT"));
     $GLOBALS['tmpl']->assign('contract', $contract);
     $GLOBALS['tmpl']->display("inc/uc/transfer_contact.html");
 }
 public function index()
 {
     if (trim($_REQUEST['search']) == "do") {
         $start_time = trim($_REQUEST['start_time']);
         if ($start_time == "") {
             $this->error("请选择开始时间");
             die;
         }
         $this->assign("start_time", $start_time);
         $end_time = trim($_REQUEST['end_time']);
         if ($end_time == "") {
             $this->error("请选择结束时间");
             die;
         }
         $this->assign("end_time", $end_time);
         $start_time = to_timespan($start_time, "Y-m-d");
         $end_time = to_timespan($end_time, "Y-m-d");
         if ($end_time < $start_time) {
             $this->error("结束时间必须大于开始时间");
             die;
         }
         $now_time = to_timespan(to_date(TIME_UTC, "Y-m-d"), "Y-m-d");
         if ($end_time > $now_time) {
             $end_time = $now_time;
         }
         //开始时间跟结束时间差多少天
         $day = ($end_time - $start_time) / 24 / 3600;
         $list = array();
         $day_time = $start_time;
         //标分类
         $deal_cate = load_auto_cache("cache_deal_cate");
         /*foreach($deal_cate as $kk=>$vv){
          		if(strpos($vv['name'],"智能")!==false){
          			unset($deal_cate[$kk]);
          		}
          	}*/
         $this->assign("deal_cate", $deal_cate);
         //获取改时间段内所有的 还款中和 已还清的贷款
         //$deals = $GLOBALS['db']->getAll("SELECT id FROM ".DB_PREFIX."deal where deal_status in(4,5) and is_effect=1 and is_delete=0 and publish_wait=0 AND success_time >= $start_time and  ((loantype=1 and (success_time + repay_time*31*24*3600) >=$end_time) or (loantype=0 and (success_time + (repay_time+1)*24*3600)>=$end_time))");
         $deals = $GLOBALS['db']->getAll("SELECT id FROM " . DB_PREFIX . "deal where deal_status in(4,5) and is_effect=1 and is_delete=0 and publish_wait=0 AND success_time >= {$start_time}");
         $temp_deals = array();
         require_once APP_ROOT_PATH . "app/Lib/common.php";
         require_once APP_ROOT_PATH . "app/Lib/deal.php";
         require_once APP_ROOT_PATH . "system/libs/user.php";
         for ($i = 0; $i <= $day; $i++) {
             $list[$i]['day'] = $day_time;
             //线上充值金额
             $list[$i]['online_pay'] = floatval($GLOBALS['db']->getOne("SELECT sum(deal_total_price) FROM " . DB_PREFIX . "deal_order where pay_status=2 and type = 1 and is_delete = 0 and create_time between {$day_time} and {$day_time}+24*3600 and payment_id not in (SELECT id from " . DB_PREFIX . "payment where class_name='Otherpay') "));
             //线下充值金额
             $list[$i]['below_pay'] = floatval($GLOBALS['db']->getOne("SELECT sum(deal_total_price) FROM " . DB_PREFIX . "deal_order where pay_status=2 and type = 1 and is_delete = 0 and create_time between {$day_time} and {$day_time}+24*3600 and payment_id in (SELECT id from " . DB_PREFIX . "payment where class_name='Otherpay') "));
             foreach ($deal_cate as $kk => $vv) {
                 //if(strpos($vv['name'],"智能")===false)
                 $list[$i][$vv['id']]['borrow_amount'] = floatval($GLOBALS['db']->getOne("SELECT sum(borrow_amount) FROM " . DB_PREFIX . "deal where deal_status=4 and is_delete = 0 and publish_wait = 0 and success_time between {$day_time} and {$day_time}+24*3600 and cate_id=" . $vv['id']));
             }
             //投资总额[投标者]
             $list[$i]['load_amount'] = floatval($GLOBALS['db']->getOne("SELECT sum(money) FROM " . DB_PREFIX . "deal_load where is_repay=0 and create_time between {$day_time} and {$day_time}+24*3600"));
             //已获利息总额[投标者]
             $list[$i]['load_lixi_amount'] = floatval($GLOBALS['db']->getOne("SELECT (sum(repay_money) - sum(self_money)) FROM " . DB_PREFIX . "deal_load_repay where true_repay_time between {$day_time} and {$day_time}+24*3600"));
             //应付本金
             $list[$i]['benjin_amount'] = 0;
             //应付利息
             $list[$i]['pay_lxi_amount'] = 0;
             //应付罚息
             $list[$i]['impose_amount'] = 0;
             //已付本金
             $list[$i]['has_repay_benjin_amount'] = 0;
             //已付利息
             $list[$i]['has_repay_lxi_amount'] = 0;
             //已付罚息
             $list[$i]['has_repay_impose_amount'] = 0;
             foreach ($deals as $kk => $vv) {
                 if (!isset($temp_deals[$vv['id']])) {
                     $temp_deals[$vv['id']]['deal'] = get_deal($vv['id']);
                     $temp_deals[$vv['id']]['loan'] = get_deal_load_list($temp_deals[$vv['id']]['deal']);
                 }
                 foreach ($temp_deals[$vv['id']]['loan'] as $kkk => $vvv) {
                     //如果刚好等于传入的时间就开始计算
                     if ($vvv['true_repay_time'] >= $day_time && $vvv['true_repay_time'] <= $day_time + 24 * 3600 - 1 || $vvv['repay_day'] == $day_time) {
                         if ($temp_deals[$vv['id']]['deal']['month_repay_money'] > 0 || $temp_deals[$vv['id']]['deal']['last_month_repay_money'] > 0) {
                             if ($temp_deals[$vv['id']]['deal']['loantype'] == 0) {
                                 $benj = get_benjin($kkk, count($temp_deals[$vv['id']]['loan']), $temp_deals[$vv['id']]['deal']['borrow_amount'], $temp_deals[$vv['id']]['deal']['month_repay_money'], $temp_deals[$vv['id']]['deal']['rate']);
                                 $list[$i]['benjin_amount'] += $benj;
                                 $list[$i]['pay_lxi_amount'] += $temp_deals[$vv['id']]['deal']['month_repay_money'] - $benj;
                             } elseif ($temp_deals[$vv['id']]['deal']['loantype'] == 1) {
                                 if ($kkk + 1 == count($temp_deals[$vv['id']]['loan'])) {
                                     $list[$i]['benjin_amount'] += $temp_deals[$vv['id']]['deal']['borrow_amount'];
                                     $list[$i]['pay_lxi_amount'] += $temp_deals[$vv['id']]['deal']['last_month_repay_money'] - $list[$i]['benjin_amount'];
                                 } else {
                                     $list[$i]['pay_lxi_amount'] += $temp_deals[$vv['id']]['deal']['month_repay_money'];
                                 }
                             } elseif ($temp_deals[$vv['id']]['deal']['loantype'] == 2) {
                                 if ($kkk + 1 == count($temp_deals[$vv['id']]['loan'])) {
                                     $list[$i]['benjin_amount'] += $temp_deals[$vv['id']]['deal']['borrow_amount'];
                                     $list[$i]['pay_lxi_amount'] += $temp_deals[$vv['id']]['deal']['last_month_repay_money'] - $temp_deals[$vv['id']]['deal']['borrow_amount'];
                                 }
                             }
                             $list[$i]['impose_amount'] += $vvv['impose_money'];
                             if ($vvv['has_repay'] == 1) {
                                 if ($temp_deals[$vv['id']]['deal']['loantype'] == 0) {
                                     $benj = get_benjin($kkk, count($temp_deals[$vv['id']]['loan']), $temp_deals[$vv['id']]['deal']['borrow_amount'], $temp_deals[$vv['id']]['deal']['month_repay_money'], $temp_deals[$vv['id']]['deal']['rate']);
                                     $list[$i]['has_repay_benjin_amount'] += $benj;
                                     $list[$i]['has_repay_lxi_amount'] += $temp_deals[$vv['id']]['deal']['month_repay_money'] - $benj;
                                 } elseif ($temp_deals[$vv['id']]['deal']['loantype'] == 1) {
                                     if ($kkk + 1 == count($temp_deals[$vv['id']]['loan'])) {
                                         $list[$i]['has_repay_benjin_amount'] += $temp_deals[$vv['id']]['deal']['borrow_amount'];
                                         $list[$i]['has_repay_lxi_amount'] += $temp_deals[$vv['id']]['deal']['last_month_repay_money'] - $list[$i]['benjin_amount'];
                                     } else {
                                         $list[$i]['has_repay_lxi_amount'] += $temp_deals[$vv['id']]['deal']['month_repay_money'];
                                     }
                                 } elseif ($temp_deals[$vv['id']]['deal']['loantype'] == 2) {
                                     if ($kkk + 1 == count($temp_deals[$vv['id']]['loan'])) {
                                         $list[$i]['has_repay_benjin_amount'] += $temp_deals[$vv['id']]['deal']['borrow_amount'];
                                         $list[$i]['has_repay_lxi_amount'] += $temp_deals[$vv['id']]['deal']['last_month_repay_money'] - $temp_deals[$vv['id']]['deal']['borrow_amount'];
                                     }
                                 }
                                 $list[$i]['has_repay_impose_amount'] += $vvv['impose_money'];
                             }
                         }
                     }
                 }
             }
             //待还本金
             $list[$i]['wait_repay_benjin_amount'] = $list[$i]['benjin_amount'] - $list[$i]['has_repay_benjin_amount'];
             //待还利息
             $list[$i]['wait_repay_lxi_amount'] = $list[$i]['pay_lxi_amount'] - $list[$i]['has_repay_lxi_amount'];
             //待还罚息
             $list[$i]['wait_repay_impose_amount'] = $list[$i]['impose_amount'] - $list[$i]['has_repay_impose_amount'];
             //申请提现总额
             $list[$i]['carry'] = floatval($GLOBALS['db']->getOne("SELECT sum(money) FROM " . DB_PREFIX . "user_carry where create_time between {$day_time} and {$day_time}+24*3600 "));
             //成功提现金额
             $list[$i]['suc_carry'] = floatval($GLOBALS['db']->getOne("SELECT sum(money) FROM " . DB_PREFIX . "user_carry where status=1 and update_time between {$day_time} and {$day_time}+24*3600 "));
             $day_time += 24 * 3600;
         }
         //待投资资金
         $user_amount = M("User")->where("is_delete=0 AND is_effect=1")->sum("money");
         $this->assign("user_amount", $user_amount);
         $this->assign("list", $list);
     }
     $this->display();
 }
Esempio n. 3
0
/**
 * 获取该期本金
 * int $Idx  第几期
 * floatval $amount_money 总的借款多少
 * floatval $month_repay_money 月还本息
 * floatval $rate 费率
 */
function get_self_money($idx, $amount_money, $month_repay_money, $rate)
{
    return $month_repay_money - get_benjin($idx, $idx, $amount_money, $month_repay_money, $rate) * $rate / 12 / 100;
}
Esempio n. 4
0
function getUcTransferBuys($page, $status)
{
    if ($page == 0) {
        $page = 1;
    }
    $limit = ($page - 1) * app_conf("PAGE_SIZE") . "," . app_conf("PAGE_SIZE");
    $page_args = array();
    $condition = ' and d.is_effect=1 and d.is_delete=0  and d.repay_time_type =1 and  d.publish_wait=0 and dlt.t_user_id=' . $GLOBALS['user_info']['id'] . "  ";
    $union_sql = " LEFT JOIN " . DB_PREFIX . "deal_load_transfer dlt ON dlt.deal_id = dl.deal_id  and dlt.load_id=dl.id ";
    switch ($status) {
        case 1:
            //回收中
            $condition .= " AND d.deal_status = 4 ";
            break;
        case 2:
            //已回收
            $condition .= " AND d.deal_status = 5 ";
            break;
        default:
            //默认
            $condition .= " AND d.deal_status >= 4 ";
            break;
    }
    $count_sql = 'SELECT count(dl.id) FROM ' . DB_PREFIX . 'deal_load dl LEFT JOIN ' . DB_PREFIX . 'deal d ON d.id = dl.deal_id ' . $union_sql . ' WHERE 1=1 ' . $condition;
    $rs_count = $GLOBALS['db']->getOne($count_sql . " LIMIT {$limit} ");
    $list = array();
    if ($rs_count > 0) {
        $list_sql = 'SELECT dl.id as dlid,d.*,dl.money as load_money,dlt.id as dltid,dlt.status as tras_status,dlt.t_user_id,dlt.transfer_amount,dlt.create_time as tras_create_time,dlt.transfer_time FROM ' . DB_PREFIX . 'deal_load dl LEFT JOIN ' . DB_PREFIX . 'deal d ON d.id = dl.deal_id ' . $union_sql . ' WHERE 1=1 ' . $condition;
        $list = $GLOBALS['db']->getAll($list_sql);
        foreach ($list as $k => $v) {
            //最后还款日
            $list[$k]['final_repay_time'] = next_replay_month($v['repay_start_time'], $v['repay_time']);
            $list[$k]['final_repay_time_format'] = to_date($list[$k]['final_repay_time'], "Y-m-d");
            //剩余期数
            if ($v['deal_status'] == 4) {
                if (intval($v['last_repay_time']) > 0) {
                    $list[$k]['how_much_month'] = how_much_month($v['last_repay_time'], $list[$k]['final_repay_time']);
                } else {
                    $list[$k]['how_much_month'] = how_much_month($v['repay_start_time'], $list[$k]['final_repay_time']);
                }
            } else {
                $list[$k]['how_much_month'] = 0;
            }
            if ($v['loantype'] == 0) {
                $list[$k]['month_repay_money'] = pl_it_formula($v['load_money'], $v['rate'] / 12 / 100, $v['repay_time']);
            } elseif ($v['loantype'] == 1) {
                $list[$k]['month_repay_money'] = av_it_formula($v['load_money'], $v['rate'] / 12 / 100);
            } elseif ($v['loantype'] == 2) {
                $list[$k]['month_repay_money'] = 0;
            }
            if ($v['deal_status'] == 4) {
                if ($v['loantype'] == 0) {
                    //剩余多少钱未回
                    $list[$k]['all_must_repay_money'] = $list[$k]['month_repay_money'] * $list[$k]['how_much_month'];
                    //剩余多少本金未回
                    $list[$k]['left_benjin'] = get_benjin($v['repay_time'] - $list[$k]['how_much_month'] - 1, $v['repay_time'], $v['load_money'], $list[$k]['month_repay_money'], $v['rate']);
                } elseif ($v['loantype'] == 1) {
                    //剩余多少钱未回
                    $list[$k]['all_must_repay_money'] = $list[$k]['month_repay_money'] * $list[$k]['how_much_month'] + $v['load_money'];
                    //剩余多少本金未回
                    $list[$k]['left_benjin'] = $v['load_money'];
                } elseif ($v['loantype'] == 2) {
                    //剩余多少钱未回
                    $list[$k]['all_must_repay_money'] = $v['load_money'] * $v['rate'] / 12 / 100 * $v['repay_time'] + $v['load_money'];
                    //剩余多少本金未回
                    $list[$k]['left_benjin'] = $v['load_money'];
                }
                $list[$k]['left_benjin_format'] = format_price($list[$k]['left_benjin'] / 10000) . "万";
                //剩多少利息
                $list[$k]['left_lixi'] = $list[$k]['all_must_repay_money'] - $list[$k]['left_benjin'];
                $list[$k]['left_lixi_format'] = format_price($list[$k]['left_lixi']);
            } else {
                $list[$k]['left_benjin_format'] = format_price(0);
                $list[$k]['left_lixi_format'] = format_price(0);
            }
            //转让价格
            $list[$k]['transfer_amount_format'] = format_price($v['transfer_amount'] / 10000) . "万";
            if ($v['tras_create_time'] != "") {
                $list[$k]['tras_create_time_format'] = to_date($v['tras_create_time'], "Y-m-d");
            }
            if (intval($v['transfer_time']) > 0) {
                $list[$k]['transfer_time_format'] = to_date($v['transfer_time'], "Y-m-d");
            }
            $list[$k]['tras_status_op'] = 5;
            if ($v['deal_status'] == 4) {
                $list[$k]['tras_status_format'] = '回收中';
            } elseif ($v['deal_status'] == 5) {
                $list[$k]['tras_status_format'] = '已回收';
            }
            $durl = "/index.php?ctl=deal&act=mobile&id=" . $v['id'];
            $list[$k]['app_url'] = str_replace("/mapi", "", SITE_DOMAIN . $durl);
        }
        return array('list' => $list, 'count' => $rs_count);
    } else {
        return array('list' => null, 'count' => 0);
    }
}
Esempio n. 5
0
 /**
  * 债券转让计算
  */
 function transfer($transfer)
 {
     $return['month_repay_money'] = pl_it_formula($transfer['load_money'], $transfer['rate'] / 12 / 100, $transfer['repay_time']);
     //剩余多少钱未回
     $return['all_must_repay_money'] = $return['month_repay_money'] * $transfer['how_much_month'];
     //剩余多少本金未回
     if ($transfer['repay_time'] == $transfer['how_much_month']) {
         $return['left_benjin'] = $transfer['load_money'];
     } else {
         $return['left_benjin'] = get_benjin($transfer['repay_time'] - $transfer['how_much_month'], $transfer['repay_time'], $transfer['load_money'], $return['month_repay_money'], $transfer['rate']);
     }
     return $return;
 }
Esempio n. 6
0
function getUCInrepayRepayBorrowMoney($id)
{
    $id = intval($id);
    $root = array();
    $root["status"] = 0;
    //0:出错;1:正确;
    if ($id == 0) {
        $root["show_err"] = "操作失败!";
        return $root;
    }
    $deal = get_deal($id);
    if (!$deal) {
        $root["show_err"] = "借款不存在!";
        return $root;
    }
    if ($deal['user_id'] != $GLOBALS['user_info']['id']) {
        $root["show_err"] = "不属于你的借款!";
        return $root;
    }
    if ($deal['deal_status'] != 4) {
        $root["show_err"] = "借款不是还款状态!";
        return $root;
    }
    $time = TIME_UTC;
    $impose_money = 0;
    //还了几期了
    $has_repay_count = $GLOBALS['db']->getOne("SELECT count(*) FROM " . DB_PREFIX . "deal_repay WHERE deal_id=" . $id);
    //计算罚息
    $loan_list = get_deal_load_list($deal);
    $k_repay_time = 0;
    foreach ($loan_list as $k => $v) {
        if ($k > $has_repay_count - 1) {
            if ($k_repay_time == 0) {
                $k_repay_time = $v['repay_day'];
            }
            $impose_money += $v['impose_money'];
        }
    }
    if ($impose_money > 0) {
        $root["show_err"] = "请将逾期未还的借款还完才可以进行此操作!";
        return $root;
    }
    //月利率
    $rate = $deal['rate'] / 12 / 100;
    $impose_money = 0;
    //计算剩多少本金
    $benjin = $deal['borrow_amount'];
    if ($deal['loantype'] == 0) {
        //等额本息的时候才通过公式计算剩余多少本金
        for ($i = 1; $i <= $has_repay_count; $i++) {
            $benjin = $benjin - ($deal['month_repay_money'] - $benjin * $rate);
        }
        $impose_money = ($benjin - $deal['month_repay_money'] + $benjin * $rate) * (double) trim(app_conf('COMPENSATE_FEE')) / 100;
        $total_repay_money = $benjin + $benjin * $rate;
    } elseif ($deal['loantype'] == 1) {
        //每月付息,到期还本
        $impose_money = $benjin * (double) trim(app_conf('COMPENSATE_FEE')) / 100;
        $total_repay_money = $benjin + $deal['month_repay_money'];
    } elseif ($deal['loantype'] == 2) {
        //到期还本息
        $impose_money += $benjin * (double) trim(app_conf('COMPENSATE_FEE')) / 100;
        $total_repay_money = $benjin + $benjin * $rate;
        //计算应缴多罚息 多少管理费
        $now_ym = to_date($time, "Y-m");
        $i = 0;
        foreach ($loan_list as $k => $v) {
            ++$i;
            if ($now_ym == to_date($v['repay_day'], "Y-m")) {
                $deal['month_manage_money'] = $benjin * trim(app_conf('MANAGE_FEE')) / 100 * $i;
            }
        }
    }
    $GLOBALS['tmpl']->assign("impose_money", $impose_money);
    $GLOBALS['tmpl']->assign("total_repay_money", $total_repay_money);
    $true_total_repay_money = $total_repay_money + $impose_money + $deal['month_manage_money'];
    if ($total_repay_money + $impose_money + $deal['month_manage_money'] > $GLOBALS['user_info']['money']) {
        $root["show_err"] = "对不起,您的余额不足!";
        return $root;
    }
    //录入到提前还款列表
    $inrepay_data['deal_id'] = $id;
    $inrepay_data['user_id'] = $GLOBALS['user_info']['id'];
    $inrepay_data['repay_money'] = round($total_repay_money);
    $inrepay_data['impose_money'] = round($impose_money, 2);
    $inrepay_data['manage_money'] = round($deal['month_manage_money']);
    $inrepay_data['repay_time'] = $k_repay_time;
    $inrepay_data['true_repay_time'] = $time;
    $GLOBALS['db']->autoExecute(DB_PREFIX . "deal_inrepay_repay", $inrepay_data, "INSERT");
    $inrepay_id = $GLOBALS['db']->insert_id();
    if ($inrepay_id == 0) {
        $root["show_err"] = "对不起,数据处理失败,请联系客服!";
        return $root;
    }
    //录入还款列表
    $after_time = $GLOBALS['db']->getOne("SELECT repay_time FROM " . DB_PREFIX . "deal_repay WHERE deal_id=" . $id . " ORDER BY repay_time DESC");
    if ($after_time == "") {
        $after_time = $deal['repay_start_time'];
    }
    $temp_ids[] = array();
    for ($i = 0; $i < $deal['repay_time'] - $has_repay_count; $i++) {
        $repay_data['id'] = $v['repay_id'];
        $repay_data['has_repay'] = 1;
        $repay_data['deal_id'] = $id;
        $repay_data['user_id'] = $GLOBALS['user_info']['id'];
        $repay_data['repay_time'] = $after_time = next_replay_month($after_time);
        $repay_data['true_repay_time'] = $time;
        $repay_data['status'] = 0;
        if ($i == 0) {
            $repay_data['repay_money'] = round($deal['month_repay_money'], 2);
            $repay_data['impose_money'] = round($impose_money, 2);
            $repay_data['manage_money'] = round($deal['month_manage_money']);
        } else {
            if ($deal['loantype'] == 0) {
                //等额本息
                $repay_data['repay_money'] = $benjin / ($deal['repay_time'] - $has_repay_count);
            } elseif ($deal['loantype'] == 1) {
                //每月还息
                if ($i + 1 == $deal['repay_time'] - $has_repay_count) {
                    $repay_data['repay_money'] = $benjin;
                } else {
                    $repay_data['repay_money'] = 0;
                }
            } elseif ($deal['loantype'] == 2) {
                //每月还息
                if ($i + 1 == $deal['repay_time'] - $has_repay_count) {
                    $repay_data['repay_money'] = $benjin;
                } else {
                    $repay_data['repay_money'] = 0;
                }
            }
            $repay_data['impose_money'] = 0;
            $repay_data['manage_money'] = 0;
        }
        $deal_repay_id = $v['repay_id'];
        $GLOBALS['db']->autoExecute(DB_PREFIX . "deal_repay", $repay_data, "UPDATE", "id=" . $deal_repay_id);
        //假如出错 删除掉原来的以插入的数据
        if ($GLOBALS['db']->affected_rows() == 0) {
            if ($temp_ids) {
                $GLOBALS['db']->query("UPDATE " . DB_PREFIX . "deal_repay set has_repay = 0 WHERE id in (" . implode(",", $temp_ids) . ")");
            }
            $root["show_err"] = "对不起,处理数据失败请联系客服!";
            return $root;
        } else {
            $temp_ids[] = $deal_repay_id;
        }
    }
    //更新用户账户资金记录
    require APP_ROOT_PATH . 'system/libs/user.php';
    modify_account(array("money" => -round($impose_money)), $GLOBALS['user_info']['id'], "标:" . $deal['id'] . ",提前还款违约金");
    modify_account(array("money" => -round($total_repay_money + $deal['month_manage_money'], 2)), $GLOBALS['user_info']['id'], "标:" . $deal['id'] . ",提前还款");
    //用户获得额度
    modify_account(array("quota" => trim(app_conf('USER_REPAY_QUOTA'))), $GLOBALS['user_info']['id'], "标:" . $deal['id'] . ",还清借款获得额度");
    $content = "您好,您在" . app_conf("SHOP_TITLE") . "的借款 “<a href=\"" . $deal['url'] . "\">" . $deal['name'] . "</a>”成功提前还款" . number_format($true_total_repay_money, 2) . "元,";
    $content .= "其中违约金为:" . number_format($impose_money, 2) . "元,本笔借款已还款完毕!";
    send_user_msg("", $content, 0, $GLOBALS['user_info']['id'], $time, 0, true, 8);
    //短信通知
    if (app_conf("SMS_ON") == 1 && app_conf('SMS_SEND_REPAY') == 1) {
        $sms_content = "尊敬的" . app_conf("SHOP_TITLE") . "用户" . $GLOBALS['user_info']['user_name'] . ",您成功提前还款" . number_format($true_total_repay_money, 2) . "元,其中违约金为:" . number_format($impose_money, 2) . "元,感谢您的关注和支持。【" . app_conf("SHOP_TITLE") . "】";
        $msg_data['dest'] = $GLOBALS['user_info']['mobile'];
        $msg_data['send_type'] = 0;
        $msg_data['title'] = $msg_data['content'] = addslashes($sms_content);
        $msg_data['send_time'] = 0;
        $msg_data['is_send'] = 0;
        $msg_data['create_time'] = $time;
        $msg_data['user_id'] = $GLOBALS['user_info']['id'];
        $msg_data['is_html'] = 0;
        $GLOBALS['db']->autoExecute(DB_PREFIX . "deal_msg_list", $msg_data);
        //插入
    }
    //判断获取的信用是否超过限制
    if ($GLOBALS['db']->getOne("SELECT sum(point) FROM " . DB_PREFIX . "user_log WHERE (log_info='标:" . $deal['id'] . ",还清借款' or log_info='还清借款') AND user_id=" . $GLOBALS['user_info']['id']) < (int) trim(app_conf('REPAY_SUCCESS_LIMIT'))) {
        //获取上一次还款时间
        $befor_repay_time = $GLOBALS['db']->getOne("SELECT MAX(log_time) FROM " . DB_PREFIX . "user_log WHERE (log_info='标:" . $deal['id'] . ",还清借款' or log_info='还清借款') AND user_id=" . $GLOBALS['user_info']['id']);
        $day = ceil(($time - $befor_repay_time) / 24 / 3600);
        //当天数大于等于间隔时间 获得信用
        if ($day >= (int) trim(app_conf('REPAY_SUCCESS_DAY'))) {
            modify_account(array("point" => trim(app_conf('REPAY_SUCCESS_POINT'))), $GLOBALS['user_info']['id'], "标:" . $deal['id'] . ",还清借款");
        }
        //用户获得额度
        modify_account(array("quota" => trim(app_conf('USER_REPAY_QUOTA'))), $GLOBALS['user_info']['id'], "标:" . $deal['id'] . ",还清借款获得额度");
    }
    syn_deal_status($id);
    sys_user_status($GLOBALS['user_info']['id'], false, true);
    //用户回款
    $user_loan_list = get_deal_user_load_list($deal);
    foreach ($user_loan_list as $lllk => $lllv) {
        foreach ($lllv as $kk => $vv) {
            //本金
            $user_self_money = 0;
            //本息
            $user_repay_money = 0;
            //违约金
            $user_impose_money = 0;
            //管理费
            $user_manage_money = 0;
            $in_user_id = $vv['user_id'];
            //判断是否转让了债权
            if ($vv['t_user_id'] > 0) {
                $in_user_id = $vv['t_user_id'];
                $loan_user_info['user_name'] = $vv['user_name'];
                $loan_user_info['t_email'] = $vv['email'];
                $loan_user_info['t_mobile'] = $vv['mobile'];
            } else {
                $loan_user_info['user_name'] = $vv['t_user_name'];
                $loan_user_info['t_email'] = $vv['t_email'];
                $loan_user_info['t_mobile'] = $vv['t_mobile'];
            }
            //借入者已还款,但是没打款到借出用户中心
            if ($vv['has_repay'] == 0) {
                $user_load_data['deal_id'] = $v['deal_id'];
                $user_load_data['user_id'] = $v['user_id'];
                $user_load_data['repay_time'] = $vv['repay_day'];
                $user_load_data['true_repay_time'] = $time;
                $user_load_data['is_site_repay'] = 0;
                $user_load_data['status'] = 0;
                //小于提前还款按正常还款
                if ($vv['repay_day'] < $k_repay_time) {
                    //等额本息的时候才通过公式计算剩余多少本金
                    $user_load_data['self_money'] = $vv['self_money'];
                    $user_load_data['repay_money'] = $vv['month_repay_money'];
                    $user_load_data['manage_money'] = $vv['month_manage_money'];
                    $user_load_data['impose_money'] = $vv['impose_money'];
                    if ($vv['status'] > 0) {
                        $user_load_data['status'] = $vv['status'] - 1;
                    }
                    $content = "您好,您在" . app_conf("SHOP_TITLE") . "的投标 “<a href=\"" . $deal['url'] . "\">" . $deal['name'] . "</a>”成功还款" . number_format($vv['month_repay_money'] + $vv['impose_money'], 2) . "元,";
                    $unext_loan = $user_loan_list[$kk + 1];
                    if ($unext_loan) {
                        $content .= "本笔投标的下个还款日为" . to_date($unext_loan['repay_day'], "Y年m月d日") . ",需要本息" . number_format($unext_loan['month_repay_money'], 2) . "元。";
                    }
                    $user_self_money += (double) $user_load_data['self_money'];
                    if ($user_load_data['impose_money'] != 0 || $user_load_data['manage_money'] != 0 || $user_load_data['repay_money'] != 0) {
                        //更新用户账户资金记录
                        modify_account(array("money" => $user_load_data['impose_money']), $in_user_id, "标:" . $deal['id'] . ",期:" . ($kk + 1) . ",逾期罚息");
                        modify_account(array("money" => -$user_load_data['manage_money']), $in_user_id, "标:" . $deal['id'] . ",期:" . ($kk + 1) . ",投标管理费");
                        modify_account(array("money" => $user_load_data['repay_money']), $in_user_id, "标:" . $deal['id'] . ",期:" . ($kk + 1) . ",回报本息");
                        $msg_conf = get_user_msg_conf($in_user_id);
                        //站内信
                        if ($msg_conf['sms_bidrepaid'] == 1) {
                            send_user_msg("", $content, 0, $in_user_id, $time, 0, true, 9);
                        }
                        //邮件
                        if ($msg_conf['mail_bidrepaid'] == 1) {
                        }
                    }
                } else {
                    if ($vv['repay_day'] == $k_repay_time) {
                        if ($deal['loantype'] == 0) {
                            //等额本息的时候才通过公式计算剩余多少本金
                            $user_load_data['self_money'] = $vv['month_repay_money'] - get_benjin($kk, $deal['repay_time'], $v['money'], $vv['month_repay_money'], $deal['rate']) * $deal['rate'] / 12 / 100;
                            $user_load_data['impose_money'] = ($user_load_data['self_money'] - $vv['month_repay_money'] + $user_load_data['self_money'] * $v['rate']) * (double) trim(app_conf('COMPENSATE_FEE')) / 100;
                        } elseif ($deal['loantype'] == 1) {
                            //每月还息,到期还本
                            $user_load_data['self_money'] = $vv['money'];
                            $user_load_data['impose_money'] = $vv['money'] * floatval(trim($deal['compensate_fee'])) / 100;
                        } elseif ($deal['loantype'] == 2) {
                            //每月还息,到期还本
                            $user_load_data['self_money'] = $vv['money'];
                            $user_load_data['impose_money'] = $vv['money'] * floatval(trim($deal['compensate_fee'])) / 100;
                        }
                        $user_self_money += (double) $user_load_data['self_money'];
                        if ($deal['loantype'] == 0) {
                            //等额本息的时候才通过公式计算剩余多少本金
                            $user_load_data['repay_money'] = $vv['month_repay_money'];
                            $user_load_data['manage_money'] = $vv['month_manage_money'];
                        } elseif ($deal['loantype'] == 1) {
                            $user_load_data['repay_money'] = $vv['month_repay_money'] + $v['money'];
                            $user_load_data['manage_money'] = $vv['month_manage_money'];
                        } elseif ($deal['loantype'] == 2) {
                            $user_load_data['repay_money'] = $vv['money'];
                            $user_load_data['manage_money'] = $vv['money'] * floatval(trim($deal['user_loan_manage_fee'])) / 100 * ($kk + 1);
                        }
                        $user_repay_k = $kk + 1;
                    } else {
                        //其他月份
                        //等额本息
                        if ($deal['loantype'] == 0) {
                            if ($user_self_money == 0) {
                                $user_load_data['self_money'] = $vv['month_repay_money'] - get_benjin($kk, $deal['repay_time'], $v['money'], $vv['month_repay_money'], $deal['rate']) * $deal['rate'] / 12 / 100;
                                $user_load_data['impose_money'] = ($user_load_data['self_money'] - $vv['month_repay_money'] + $user_load_data['self_money'] * $v['rate']) * (double) trim(app_conf('COMPENSATE_FEE')) / 100;
                            } else {
                                $user_load_data['self_money'] = $user_load_data['repay_money'] = ($v['money'] - $user_self_money) / ($v['repay_time'] - $user_repay_k);
                                $user_load_data['manage_money'] = 0;
                                $user_load_data['impose_money'] = 0;
                            }
                        } elseif ($deal['loantype'] == 1) {
                            if ($user_self_money == 0) {
                                $user_self_money = $user_load_data['self_money'] = $v['money'];
                                $user_load_data['repay_money'] = $vv['month_repay_money'] + $v['money'];
                                $user_load_data['impose_money'] = $vv['money'] * floatval(trim($deal['compensate_fee'])) / 100;
                                $user_load_data['manage_money'] = $vv['month_manage_money'];
                            } else {
                                $user_load_data['self_money'] = $user_load_data['repay_money'] = 0;
                                $user_load_data['manage_money'] = 0;
                                $user_load_data['impose_money'] = 0;
                            }
                        } elseif ($deal['loantype'] == 2) {
                            if ($user_self_money == 0) {
                                $user_self_money = $user_load_data['self_money'] = $v['money'];
                                $user_load_data['repay_money'] = $vv['month_repay_money'] + $v['money'];
                                $user_load_data['impose_money'] = $vv['money'] * floatval(trim($deal['compensate_fee'])) / 100;
                                $user_load_data['manage_money'] = $vv['money'] * floatval(trim($deal['user_loan_manage_fee'])) / 100 * ($kk + 1);
                            } else {
                                $user_load_data['self_money'] = $user_load_data['repay_money'] = 0;
                                $user_load_data['manage_money'] = 0;
                                $user_load_data['impose_money'] = 0;
                            }
                        }
                    }
                    $user_repay_money += (double) $user_load_data['repay_money'];
                    $user_impose_money += (double) $user_load_data['impose_money'];
                    $user_manage_money += (double) $user_load_data['manage_money'];
                    $user_load_data['l_key'] = $kk;
                    $user_load_data['u_key'] = $k;
                }
                $user_load_data['has_repay'] = 1;
                $GLOBALS['db']->autoExecute(DB_PREFIX . "deal_load_repay", $user_load_data, "INSERT");
            }
            if ($user_repay_money > 0) {
                $all_repay_money = number_format($GLOBALS['db']->getOne("SELECT (sum(repay_money)-sum(self_money) + sum(impose_money)) as shouyi FROM " . DB_PREFIX . "deal_load_repay WHERE deal_id=" . $v['deal_id'] . " AND user_id=" . $v['user_id']), 2);
                $all_impose_money = number_format($GLOBALS['db']->getOne("SELECT sum(impose_money) FROM " . DB_PREFIX . "deal_load_repay WHERE deal_id=" . $v['deal_id'] . " AND user_id=" . $v['user_id']), 2);
                $content = "您好,您在" . app_conf("SHOP_TITLE") . "的投标 “<a href=\"" . $deal['url'] . "\">" . $deal['name'] . "</a>”提前还款,";
                $content .= "本次投标共获得收益:" . $all_repay_money . "元,其中违约金为:" . $all_impose_money . "元,本次投标已回款完毕!";
                //更新用户账户资金记录
                modify_account(array("money" => $user_impose_money), $in_user_id, "标:" . $deal['id'] . ",违约金");
                modify_account(array("money" => -$user_manage_money), $in_user_id, "标:" . $deal['id'] . ",投标管理费");
                modify_account(array("money" => $user_repay_money), $in_user_id, "标:" . $deal['id'] . ",回报本息");
                $msg_conf = get_user_msg_conf($in_user_id);
                //短信通知
                if (app_conf("SMS_ON") == 1 && app_conf('SMS_REPAY_TOUSER_ON') == 1) {
                    $tmpl = $GLOBALS['db']->getRowCached("select * from " . DB_PREFIX . "msg_template where name = 'TPL_DEAL_LOAD_REPAY_SMS'");
                    $tmpl_content = $tmpl['content'];
                    $notice['user_name'] = $loan_user_info['user_name'];
                    $notice['deal_name'] = $deal['sub_name'];
                    $notice['deal_url'] = $deal['url'];
                    $notice['site_name'] = app_conf("SHOP_TITLE");
                    $notice['repay_money'] = $vv['month_repay_money'] + $vv['impose_money'];
                    $notice['all_repay_money'] = $all_repay_money;
                    $notice['impose_money'] = $all_impose_money;
                    $GLOBALS['tmpl']->assign("notice", $notice);
                    $sms_content = $GLOBALS['tmpl']->fetch("str:" . $tmpl_content);
                    $msg_data['dest'] = $loan_user_info['mobile'];
                    $msg_data['send_type'] = 0;
                    $msg_data['title'] = $msg_data['content'] = addslashes($sms_content);
                    $msg_data['send_time'] = 0;
                    $msg_data['is_send'] = 0;
                    $msg_data['create_time'] = $time;
                    $msg_data['user_id'] = $in_user_id;
                    $msg_data['is_html'] = 0;
                    $GLOBALS['db']->autoExecute(DB_PREFIX . "deal_msg_list", $msg_data);
                    //插入
                }
                //站内信
                if ($msg_conf['sms_bidrepaid'] == 1) {
                    send_user_msg("", $content, 0, $in_user_id, $time, 0, true, 9);
                }
                //邮件
                if ($msg_conf['mail_bidrepaid'] == 1 && app_conf('MAIL_ON') == 1) {
                    $tmpl = $GLOBALS['db']->getRowCached("select * from " . DB_PREFIX . "msg_template where name = 'TPL_DEAL_LOAD_REPAY_EMAIL'");
                    $tmpl_content = $tmpl['content'];
                    $notice['user_name'] = $loan_user_info['user_name'];
                    $notice['deal_name'] = $deal['sub_name'];
                    $notice['deal_url'] = $deal['url'];
                    $notice['site_name'] = app_conf("SHOP_TITLE");
                    $notice['site_url'] = SITE_DOMAIN . APP_ROOT;
                    $notice['help_url'] = SITE_DOMAIN . url("index", "helpcenter");
                    $notice['msg_cof_setting_url'] = SITE_DOMAIN . url("index", "uc_msg#setting");
                    $notice['repay_money'] = $vv['month_repay_money'] + $vv['impose_money'];
                    $notice['all_repay_money'] = $all_repay_money;
                    $notice['impose_money'] = $all_impose_money;
                    $GLOBALS['tmpl']->assign("notice", $notice);
                    $msg = $GLOBALS['tmpl']->fetch("str:" . $tmpl_content);
                    $msg_data['dest'] = $loan_user_info['email'];
                    $msg_data['send_type'] = 1;
                    $msg_data['title'] = "“" . $deal['name'] . "”回款通知";
                    $msg_data['content'] = addslashes($msg);
                    $msg_data['send_time'] = 0;
                    $msg_data['is_send'] = 0;
                    $msg_data['create_time'] = $time;
                    $msg_data['user_id'] = $in_user_id;
                    $msg_data['is_html'] = $tmpl['is_html'];
                    $GLOBALS['db']->autoExecute(DB_PREFIX . "deal_msg_list", $msg_data);
                    //插入
                }
            }
        }
    }
    $root["status"] = 1;
    //0:出错;1:正确;
    $root["show_err"] = "操作成功!";
    return $root;
}