function getBackHuanOrder()
{
    global $ecs, $db, $rebate;
    $result = get_filter();
    if ($result === false) {
        $filter['rid'] = $rid = isset($_REQUEST['rid']) && intval($_REQUEST['rid']) > 0 ? intval($_REQUEST['rid']) : 0;
        $filter['add_time_start'] = !empty($_REQUEST['add_time_start']) ? local_strtotime($_REQUEST['add_time_start']) : 0;
        $filter['add_time_end'] = !empty($_REQUEST['add_time_end']) ? local_strtotime($_REQUEST['add_time_end'] . " 23:59:59") : 0;
        $filter['order_sn'] = isset($_REQUEST['order_sn']) ? trim($_REQUEST['order_sn']) : '';
        //$and = ' rebate_id='.$rid.' and shipping_status in ('.SS_SHIPPED.','.SS_RECEIVED.')';
        $and = ' oi.store_rebate_id=' . $rid . ' and bo.back_type!=3 and bo.status_back<5 and oi.order_id=bo.order_id ';
        $and .= $filter['add_time_start'] ? " AND oi.add_time >= '" . $filter['add_time_start'] . "' " : " ";
        $and .= $filter['add_time_end'] ? " AND oi.add_time <= '" . $filter['add_time_end'] . "' " : " ";
        $and .= $filter['order_sn'] ? " AND oi.order_sn = '" . $filter['order_sn'] . "' " : " ";
        /* 分页大小 */
        $filter['page'] = empty($_REQUEST['page']) || intval($_REQUEST['page']) <= 0 ? 1 : intval($_REQUEST['page']);
        if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0) {
            $filter['page_size'] = intval($_REQUEST['page_size']);
        } elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0) {
            $filter['page_size'] = intval($_COOKIE['ECSCP']['page_size']);
        } else {
            $filter['page_size'] = 15;
        }
        //总数
        $sql = "select count(oi.order_id) " . "from " . $ecs->table('order_info') . " as oi," . $ecs->table('back_order') . " as bo where " . $and;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter['page_count'] = $filter['record_count'] > 0 ? ceil($filter['record_count'] / $filter['page_size']) : 1;
        //记录
        $sql = "select oi.order_id, oi.order_sn, oi.add_time, oi.order_status, oi.shipping_status, oi.order_amount, oi.money_paid," . "oi.pay_status, oi.consignee, oi.address, oi.email, oi.tel, oi.extension_code, oi.extension_id, oi.shipping_time, bo.add_time as back_add_time,bo.status_back,bo.status_refund, " . "(" . order_amount_field('oi.') . ") AS total_fee " . "from " . $ecs->table('order_info') . " as oi," . $ecs->table('back_order') . " as bo where " . $and . " LIMIT " . ($filter['page'] - 1) * $filter['page_size'] . ",{$filter['page_size']}";
        //echo $sql;
        set_filter($filter, $sql);
    } else {
        $sql = $result['sql'];
        $filter = $result['filter'];
    }
    $query = $db->query($sql);
    $ret = array();
    while ($row = $db->fetchRow($query)) {
        $is_order = $is_shipping = $is_pay = 0;
        $row['formated_order_amount'] = price_format($row['order_amount']);
        $row['formated_money_paid'] = price_format($row['money_paid']);
        //$row['formated_rebate_fee'] = 0-price_format($row['total_fee']*$rebate['supplier_rebate']/100);
        $row['formated_rebate_fee'] = getGoodsOrderRebatePrice($row['order_id']);
        $row['formated_rebate_fee'] = 0 - price_format($row['formated_rebate_fee'] * $rebate['rebate'] / 100);
        $row['formated_total_fee'] = price_format($row['total_fee']);
        $row['short_order_time'] = local_date('Y-m-d H:i', $row['add_time']);
        $row['short_back_add_time'] = local_date('Y-m-d H:i', $row['back_add_time']);
        $row['is_rebeat'] = $row['datas'] = 0;
        $ret[$row['order_id']] = $row;
    }
    //echo "<pre>";
    //print_r($ret);
    $arr = array('orders' => $ret, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
    return $arr;
}
function getRebateOrderMoney($rid)
{
    global $ecs, $db;
    $back_and = '';
    if (($back_order_id = getBackOrderByRebate($rid)) != false) {
        //获取退货订单中相关订单
        $back_and = "and order_id not in(" . implode(',', $back_order_id) . ")";
    }
    $pay_id = getPayHoudaofukuan();
    //获取货到付款的id
    $sql = "select (" . order_amount_field() . ") AS total_fee,pay_id,order_id from " . $ecs->table('order_info') . " where store_rebate_id=" . $rid . " {$back_and} and store_rebate_ispay=2";
    $query = $db->query($sql);
    $online = $online_rebate = $onout = $onout_rebate = 0;
    $online_ids = $onout_ids = array();
    while ($row = $db->fetchRow($query)) {
        if ($row['pay_id'] == $pay_id) {
            //货到付款
            $onout += $row['total_fee'];
            $onout_rebate += getGoodsOrderRebatePrice($row['order_id']);
            $onout_ids[] = $row['order_id'];
        } else {
            //在线支付
            $online += $row['total_fee'];
            $online_rebate += getGoodsOrderRebatePrice($row['order_id']);
            $online_ids[] = $row['order_id'];
        }
    }
    if (count($onout_ids) > 0) {
        //货到付款订单当中有商品发生的退货
        $sql = "select (goods_price * is_back) as price_out from " . $ecs->table('order_goods') . " where order_id in(" . implode(',', $onout_ids) . ")";
        $out_price = $db->getOne($sql);
        $onout = $onout - $out_price;
    }
    if (count($online_ids) > 0) {
        //在线支付订单当中有商品发生退货
        $sql = "select (goods_price * is_back) as price_out from " . $ecs->table('order_goods') . " where order_id in(" . implode(',', $online_ids) . ")";
        $line_price = $db->getOne($sql);
        $online = $online - $line_price;
    }
    return array('all' => array('online' => $online, 'onout' => $onout), 'rebate' => array('online' => $online_rebate, 'onout' => $onout_rebate));
}