Ejemplo n.º 1
0
/**
 * 合并订单
 * @param   string  $from_order_sn  从订单号
 * @param   string  $to_order_sn    主订单号
 * @return  成功返回true,失败返回错误信息
 */
function merge_order($from_order_sn, $to_order_sn)
{
    /* 订单号不能为空 */
    if (trim($from_order_sn) == '' || trim($to_order_sn) == '') {
        return $GLOBALS['_LANG']['order_sn_not_null'];
    }
    /* 订单号不能相同 */
    if ($from_order_sn == $to_order_sn) {
        return $GLOBALS['_LANG']['two_order_sn_same'];
    }
    /* 取得订单信息 */
    $from_order = order_info(0, $from_order_sn);
    $to_order = order_info(0, $to_order_sn);
    /* 检查订单是否存在 */
    if (!$from_order) {
        return sprintf($GLOBALS['_LANG']['order_not_exist'], $from_order_sn);
    } elseif (!$to_order) {
        return sprintf($GLOBALS['_LANG']['order_not_exist'], $to_order_sn);
    }
    /* 检查合并的订单是否为普通订单,非普通订单不允许合并 */
    if ($from_order['extension_code'] != '' || $to_order['extension_code'] != 0) {
        return $GLOBALS['_LANG']['merge_invalid_order'];
    }
    /* 检查订单状态是否是已确认或未确认、未付款、未发货 */
    if ($from_order['order_status'] != OS_UNCONFIRMED && $from_order['order_status'] != OS_CONFIRMED) {
        return sprintf($GLOBALS['_LANG']['os_not_unconfirmed_or_confirmed'], $from_order_sn);
    } elseif ($from_order['pay_status'] != PS_UNPAYED) {
        return sprintf($GLOBALS['_LANG']['ps_not_unpayed'], $from_order_sn);
    } elseif ($from_order['shipping_status'] != SS_UNSHIPPED) {
        return sprintf($GLOBALS['_LANG']['ss_not_unshipped'], $from_order_sn);
    }
    if ($to_order['order_status'] != OS_UNCONFIRMED && $to_order['order_status'] != OS_CONFIRMED) {
        return sprintf($GLOBALS['_LANG']['os_not_unconfirmed_or_confirmed'], $to_order_sn);
    } elseif ($to_order['pay_status'] != PS_UNPAYED) {
        return sprintf($GLOBALS['_LANG']['ps_not_unpayed'], $to_order_sn);
    } elseif ($to_order['shipping_status'] != SS_UNSHIPPED) {
        return sprintf($GLOBALS['_LANG']['ss_not_unshipped'], $to_order_sn);
    }
    /* 检查订单用户是否相同 */
    if ($from_order['user_id'] != $to_order['user_id']) {
        return $GLOBALS['_LANG']['order_user_not_same'];
    }
    /* 合并订单 */
    $order = $to_order;
    $order['order_id'] = '';
    $order['add_time'] = gmtime();
    // 合并商品总额
    $order['goods_amount'] += $from_order['goods_amount'];
    // 合并折扣
    $order['discount'] += $from_order['discount'];
    if ($order['shipping_id'] > 0) {
        // 重新计算配送费用
        $weight_price = order_weight_price($to_order['order_id']);
        $from_weight_price = order_weight_price($from_order['order_id']);
        $weight_price['weight'] += $from_weight_price['weight'];
        $weight_price['amount'] += $from_weight_price['amount'];
        $weight_price['number'] += $from_weight_price['number'];
        $region_id_list = array($order['country'], $order['province'], $order['city'], $order['district']);
        $shipping_area = shipping_area_info($order['shipping_id'], $region_id_list);
        $order['shipping_fee'] = shipping_fee($shipping_area['shipping_code'], unserialize($shipping_area['configure']), $weight_price['weight'], $weight_price['amount'], $weight_price['number']);
        // 如果保价了,重新计算保价费
        if ($order['insure_fee'] > 0) {
            $order['insure_fee'] = shipping_insure_fee($shipping_area['shipping_code'], $order['goods_amount'], $shipping_area['insure']);
        }
    }
    // 重新计算包装费、贺卡费
    if ($order['pack_id'] > 0) {
        $pack = pack_info($order['pack_id']);
        $order['pack_fee'] = $pack['free_money'] > $order['goods_amount'] ? $pack['pack_fee'] : 0;
    }
    if ($order['card_id'] > 0) {
        $card = card_info($order['card_id']);
        $order['card_fee'] = $card['free_money'] > $order['goods_amount'] ? $card['card_fee'] : 0;
    }
    // 红包不变,合并积分、余额、已付款金额
    $order['integral'] += $from_order['integral'];
    $order['integral_money'] = value_of_integral($order['integral']);
    $order['surplus'] += $from_order['surplus'];
    $order['money_paid'] += $from_order['money_paid'];
    // 计算应付款金额(不包括支付费用)
    $order['order_amount'] = $order['goods_amount'] - $order['discount'] + $order['shipping_fee'] + $order['insure_fee'] + $order['pack_fee'] + $order['card_fee'] - $order['bonus'] - $order['integral_money'] - $order['surplus'] - $order['money_paid'];
    // 重新计算支付费
    if ($order['pay_id'] > 0) {
        // 货到付款手续费
        $cod_fee = $shipping_area ? $shipping_area['pay_fee'] : 0;
        $order['pay_fee'] = pay_fee($order['pay_id'], $order['order_amount'], $cod_fee);
        // 应付款金额加上支付费
        $order['order_amount'] += $order['pay_fee'];
    }
    /* 插入订单表 */
    do {
        $order['order_sn'] = get_order_sn();
        if ($GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('order_info'), addslashes_deep($order), 'INSERT')) {
            break;
        } else {
            if ($GLOBALS['db']->errno() != 1062) {
                die($GLOBALS['db']->errorMsg());
            }
        }
    } while (true);
    // 防止订单号重复
    /* 订单号 */
    $order_id = $GLOBALS['db']->insert_id();
    /* 更新订单商品 */
    $sql = 'UPDATE ' . $GLOBALS['ecs']->table('order_goods') . " SET order_id = '{$order_id}' " . "WHERE order_id " . db_create_in(array($from_order['order_id'], $to_order['order_id']));
    $GLOBALS['db']->query($sql);
    include_once ROOT_PATH . 'includes/lib_clips.php';
    /* 插入支付日志 */
    insert_pay_log($order_id, $order['order_amount'], PAY_ORDER);
    /* 删除原订单 */
    $sql = 'DELETE FROM ' . $GLOBALS['ecs']->table('order_info') . " WHERE order_id " . db_create_in(array($from_order['order_id'], $to_order['order_id']));
    $GLOBALS['db']->query($sql);
    /* 删除原订单支付日志 */
    $sql = 'DELETE FROM ' . $GLOBALS['ecs']->table('pay_log') . " WHERE order_id " . db_create_in(array($from_order['order_id'], $to_order['order_id']));
    $GLOBALS['db']->query($sql);
    /* 返还 from_order 的红包,因为只使用 to_order 的红包 */
    if ($from_order['bonus_id'] > 0) {
        unuse_bonus($from_order['bonus_id']);
    }
    /* 返回成功 */
    return true;
}
Ejemplo n.º 2
0
 /* 如果不存在实体商品,退出 */
 if (!exist_real_goods($order_id)) {
     die('Hacking Attemp');
 }
 /* 取得订单信息 */
 $order_info = order_info($order_id);
 $region_id_list = array($order_info['country'], $order_info['province'], $order_info['city'], $order_info['district']);
 /* 保存订单 */
 $shipping_id = $_POST['shipping'];
 $shipping = shipping_area_info($shipping_id, $region_id_list);
 $weight_amount = order_weight_price($order_id);
 $shipping_fee = shipping_fee($shipping['shipping_code'], $shipping['configure'], $weight_amount['weight'], $weight_amount['amount'], $weight_amount['number']);
 $order = array('shipping_id' => $shipping_id, 'shipping_name' => addslashes($shipping['shipping_name']), 'shipping_fee' => $shipping_fee);
 if (isset($_POST['insure'])) {
     /* 计算保价费 */
     $order['insure_fee'] = shipping_insure_fee($shipping['shipping_code'], order_amount($order_id), $shipping['insure']);
 } else {
     $order['insure_fee'] = 0;
 }
 update_order($order_id, $order);
 update_order_amount($order_id);
 /* 更新 pay_log */
 update_pay_log($order_id);
 /* 清除首页缓存:发货单查询 */
 clear_cache_files('index.dwt');
 /* todo 记录日志 */
 $sn = $old_order['order_sn'];
 $new_order = order_info($order_id);
 if ($old_order['total_fee'] != $new_order['total_fee']) {
     $sn .= ',' . sprintf($_LANG['order_amount_change'], $old_order['total_fee'], $new_order['total_fee']);
 }
Ejemplo n.º 3
0
 $sql = "SELECT order_id, SUM(goods_number * goods_price) AS goods_amount " . "FROM " . $ecs->table('order_goods') . " WHERE order_id " . db_create_in($order_id_list) . " GROUP BY order_id";
 $res = $db->query($sql);
 while ($row = $db->fetchRow($res)) {
     $order_id = $row['order_id'];
     $goods_amount = floatval($row['goods_amount']);
     /* 取得订单信息 */
     $order = order_info($order_id);
     /* 判断订单是否有效:余额支付金额 + 已付款金额 >= 保证金 */
     if ($order['surplus'] + $order['money_paid'] >= $group_buy['deposit']) {
         /* 有效,设为已确认,更新订单 */
         // 更新商品总额
         $order['goods_amount'] = $goods_amount;
         // 如果保价,重新计算保价费用
         if ($order['insure_fee'] > 0) {
             $shipping = shipping_info($order['shipping_id']);
             $order['insure_fee'] = shipping_insure_fee($shipping['shipping_code'], $goods_amount, $shipping['insure']);
         }
         // 重算支付费用
         $order['order_amount'] = $order['goods_amount'] + $order['shipping_fee'] + $order['insure_fee'] + $order['pack_fee'] + $order['card_fee'] - $order['money_paid'] - $order['surplus'];
         if ($order['order_amount'] > 0) {
             $order['pay_fee'] = pay_fee($order['pay_id'], $order['order_amount']);
         } else {
             $order['pay_fee'] = 0;
         }
         // 计算应付款金额
         $order['order_amount'] += $order['pay_fee'];
         // 计算付款状态
         if ($order['order_amount'] > 0) {
             $order['pay_status'] = PS_UNPAYED;
             $order['pay_time'] = 0;
         } else {
Ejemplo n.º 4
0
 /**
  * 获得订单中的费用信息
  *
  * @access  public
  * @param   array   $order
  * @param   array   $goods
  * @param   array   $consignee
  * @param   bool    $is_gb_deposit  是否团购保证金(如果是,应付款金额只计算商品总额和支付费用,可以获得的积分取 $gift_integral)
  * @return  array
  */
 function order_fee($order, $goods, $consignee)
 {
     /* 初始化订单的扩展code */
     if (!isset($order['extension_code'])) {
         $order['extension_code'] = '';
     }
     if ($order['extension_code'] == 'group_buy') {
         $group_buy = model('GroupBuyBase')->group_buy_info($order['extension_id']);
     }
     $total = array('real_goods_count' => 0, 'gift_amount' => 0, 'goods_price' => 0, 'market_price' => 0, 'discount' => 0, 'pack_fee' => 0, 'card_fee' => 0, 'shipping_fee' => 0, 'shipping_insure' => 0, 'integral_money' => 0, 'bonus' => 0, 'surplus' => 0, 'cod_fee' => 0, 'pay_fee' => 0, 'tax' => 0);
     $weight = 0;
     /* 商品总价 */
     foreach ($goods as $val) {
         /* 统计实体商品的个数 */
         if ($val['is_real']) {
             $total['real_goods_count']++;
         }
         $total['goods_price'] += $val['goods_price'] * $val['goods_number'];
         $total['market_price'] += $val['market_price'] * $val['goods_number'];
     }
     $total['saving'] = $total['market_price'] - $total['goods_price'];
     $total['save_rate'] = $total['market_price'] ? round($total['saving'] * 100 / $total['market_price']) . '%' : 0;
     $total['goods_price_formated'] = price_format($total['goods_price'], false);
     $total['market_price_formated'] = price_format($total['market_price'], false);
     $total['saving_formated'] = price_format($total['saving'], false);
     /* 折扣 */
     if ($order['extension_code'] != 'group_buy') {
         $discount = model('Order')->compute_discount();
         $total['discount'] = $discount['discount'];
         if ($total['discount'] > $total['goods_price']) {
             $total['discount'] = $total['goods_price'];
         }
     }
     $total['discount_formated'] = price_format($total['discount'], false);
     /* 税额 */
     if (!empty($order['need_inv']) && $order['inv_type'] != '') {
         /* 查税率 */
         $rate = 0;
         $invoice_type = C('invoice_type');
         foreach ($invoice_type['type'] as $key => $type) {
             if ($type == $order['inv_type']) {
                 $rate = floatval($invoice_type['rate'][$key]) / 100;
                 break;
             }
         }
         if ($rate > 0) {
             $total['tax'] = $rate * $total['goods_price'];
         }
     }
     $total['tax_formated'] = price_format($total['tax'], false);
     /* 包装费用 */
     if (!empty($order['pack_id'])) {
         $total['pack_fee'] = pack_fee($order['pack_id'], $total['goods_price']);
     }
     $total['pack_fee_formated'] = price_format($total['pack_fee'], false);
     /* 贺卡费用 */
     if (!empty($order['card_id'])) {
         $total['card_fee'] = card_fee($order['card_id'], $total['goods_price']);
     }
     $total['card_fee_formated'] = price_format($total['card_fee'], false);
     /* 红包 */
     if (!empty($order['bonus_id'])) {
         $bonus = model('Order')->bonus_info($order['bonus_id']);
         $total['bonus'] = $bonus['type_money'];
     }
     $total['bonus_formated'] = price_format($total['bonus'], false);
     /* 线下红包 */
     if (!empty($order['bonus_kill'])) {
         $bonus = model('Order')->bonus_info(0, $order['bonus_kill']);
         $total['bonus_kill'] = $order['bonus_kill'];
         $total['bonus_kill_formated'] = price_format($total['bonus_kill'], false);
     }
     /* 配送费用 */
     $shipping_cod_fee = NULL;
     if ($order['shipping_id'] > 0 && $total['real_goods_count'] > 0) {
         $region['country'] = $consignee['country'];
         $region['province'] = $consignee['province'];
         $region['city'] = $consignee['city'];
         $region['district'] = $consignee['district'];
         $shipping_info = model('Shipping')->shipping_area_info($order['shipping_id'], $region);
         if (!empty($shipping_info)) {
             if ($order['extension_code'] == 'group_buy') {
                 $weight_price = model('Order')->cart_weight_price(CART_GROUP_BUY_GOODS);
             } else {
                 $weight_price = model('Order')->cart_weight_price();
             }
             // 查看购物车中是否全为免运费商品,若是则把运费赋为零
             $sql = 'SELECT count(*) as count FROM ' . $this->pre . "cart WHERE  `session_id` = '" . SESS_ID . "' AND `extension_code` != 'package_buy' AND `is_shipping` = 0";
             $res = $this->row($sql);
             $shipping_count = $res['count'];
             $total['shipping_fee'] = ($shipping_count == 0 and $weight_price['free_shipping'] == 1) ? 0 : shipping_fee($shipping_info['shipping_code'], $shipping_info['configure'], $weight_price['weight'], $total['goods_price'], $weight_price['number']);
             if (!empty($order['need_insure']) && $shipping_info['insure'] > 0) {
                 $total['shipping_insure'] = shipping_insure_fee($shipping_info['shipping_code'], $total['goods_price'], $shipping_info['insure']);
             } else {
                 $total['shipping_insure'] = 0;
             }
             if ($shipping_info['support_cod']) {
                 $shipping_cod_fee = $shipping_info['pay_fee'];
             }
         }
     }
     $total['shipping_fee_formated'] = price_format($total['shipping_fee'], false);
     $total['shipping_insure_formated'] = price_format($total['shipping_insure'], false);
     // 购物车中的商品能享受红包支付的总额
     $bonus_amount = model('Order')->compute_discount_amount();
     // 红包和积分最多能支付的金额为商品总额
     $max_amount = $total['goods_price'] == 0 ? $total['goods_price'] : $total['goods_price'] - $bonus_amount;
     /* 计算订单总额 */
     if ($order['extension_code'] == 'group_buy' && $group_buy['deposit'] > 0) {
         $total['amount'] = $total['goods_price'];
     } else {
         $total['amount'] = $total['goods_price'] - $total['discount'] + $total['tax'] + $total['pack_fee'] + $total['card_fee'] + $total['shipping_fee'] + $total['shipping_insure'] + $total['cod_fee'];
         // 减去红包金额
         $use_bonus = min($total['bonus'], $max_amount);
         // 实际减去的红包金额
         if (isset($total['bonus_kill'])) {
             $use_bonus_kill = min($total['bonus_kill'], $max_amount);
             $total['amount'] -= $price = number_format($total['bonus_kill'], 2, '.', '');
             // 还需要支付的订单金额
         }
         $total['bonus'] = $use_bonus;
         $total['bonus_formated'] = price_format($total['bonus'], false);
         $total['amount'] -= $use_bonus;
         // 还需要支付的订单金额
         $max_amount -= $use_bonus;
         // 积分最多还能支付的金额
     }
     /* 余额 */
     $order['surplus'] = $order['surplus'] > 0 ? $order['surplus'] : 0;
     if ($total['amount'] > 0) {
         if (isset($order['surplus']) && $order['surplus'] > $total['amount']) {
             $order['surplus'] = $total['amount'];
             $total['amount'] = 0;
         } else {
             $total['amount'] -= floatval($order['surplus']);
         }
     } else {
         $order['surplus'] = 0;
         $total['amount'] = 0;
     }
     $total['surplus'] = $order['surplus'];
     $total['surplus_formated'] = price_format($order['surplus'], false);
     /* 积分 */
     $order['integral'] = $order['integral'] > 0 ? $order['integral'] : 0;
     if ($total['amount'] > 0 && $max_amount > 0 && $order['integral'] > 0) {
         $integral_money = value_of_integral($order['integral']);
         // 使用积分支付
         $use_integral = min($total['amount'], $max_amount, $integral_money);
         // 实际使用积分支付的金额
         $total['amount'] -= $use_integral;
         $total['integral_money'] = $use_integral;
         $order['integral'] = integral_of_value($use_integral);
     } else {
         $total['integral_money'] = 0;
         $order['integral'] = 0;
     }
     $total['integral'] = $order['integral'];
     $total['integral_formated'] = price_format($total['integral_money'], false);
     /* 保存订单信息 */
     $_SESSION['flow_order'] = $order;
     $se_flow_type = isset($_SESSION['flow_type']) ? $_SESSION['flow_type'] : '';
     /* 支付费用 */
     if (!empty($order['pay_id']) && ($total['real_goods_count'] > 0 || $se_flow_type != CART_EXCHANGE_GOODS)) {
         $total['pay_fee'] = pay_fee($order['pay_id'], $total['amount'], $shipping_cod_fee);
     }
     $total['pay_fee_formated'] = price_format($total['pay_fee'], false);
     $total['amount'] += $total['pay_fee'];
     // 订单总额累加上支付费用
     $total['amount_formated'] = price_format($total['amount'], false);
     /* 取得可以得到的积分和红包 */
     if ($order['extension_code'] == 'group_buy') {
         $total['will_get_integral'] = $group_buy['gift_integral'];
     } elseif ($order['extension_code'] == 'exchange_goods') {
         $total['will_get_integral'] = 0;
     } else {
         $total['will_get_integral'] = model('Order')->get_give_integral($goods);
     }
     $total['will_get_bonus'] = $order['extension_code'] == 'exchange_goods' ? 0 : price_format(model('Order')->get_total_bonus(), false);
     $total['formated_goods_price'] = price_format($total['goods_price'], false);
     $total['formated_market_price'] = price_format($total['market_price'], false);
     $total['formated_saving'] = price_format($total['saving'], false);
     if ($order['extension_code'] == 'exchange_goods') {
         $sql = 'SELECT SUM(eg.exchange_integral) ' . 'as sum FROM ' . $this->pre . 'cart AS c,' . $this->pre . 'exchange_goods AS eg ' . "WHERE c.goods_id = eg.goods_id AND c.session_id= '" . SESS_ID . "' " . "  AND c.rec_type = '" . CART_EXCHANGE_GOODS . "' " . '  AND c.is_gift = 0 AND c.goods_id > 0 ' . 'GROUP BY eg.goods_id';
         $res = $this->row($sql);
         $exchange_integral = $res['sum'];
         $total['exchange_integral'] = $exchange_integral;
     }
     return $total;
 }
Ejemplo n.º 5
0
/**
 * 添加/编辑预售活动的提交
 */
function action_insert_update()
{
    $user = $GLOBALS['user'];
    $_CFG = $GLOBALS['_CFG'];
    $_LANG = $GLOBALS['_LANG'];
    $smarty = $GLOBALS['smarty'];
    $db = $GLOBALS['db'];
    $ecs = $GLOBALS['ecs'];
    $user_id = $_SESSION['user_id'];
    /* 取得预售活动id */
    $pre_sale_id = intval($_POST['act_id']);
    if (isset($_POST['finish']) || isset($_POST['succeed']) || isset($_POST['fail']) || isset($_POST['mail'])) {
        if ($pre_sale_id <= 0) {
            sys_msg($_LANG['error_pre_sale'], 1);
        }
        $pre_sale = pre_sale_info($pre_sale_id);
        if (empty($pre_sale)) {
            sys_msg($_LANG['error_pre_sale'], 1);
        }
    }
    if (isset($_POST['finish'])) {
        /* 设置活动结束 */
        /* 判断活动状态 */
        if ($pre_sale['status'] != PSS_UNDER_WAY) {
            sys_msg($_LANG['error_status'], 1);
            // 此处怀疑是如果活动进行中突然要结束掉,应该抛出禁止的页面,貌似去掉了,所以程序继续执行
        }
        /* 结束预售活动,修改结束时间为当前时间 */
        $sql = "UPDATE " . $ecs->table('goods_activity') . " SET end_time = '" . gmtime() . "' " . "WHERE act_id = '{$pre_sale_id}' LIMIT 1";
        $db->query($sql);
        /* 清除缓存 */
        clear_cache_files();
        /* 提示信息 */
        $links = array(array('href' => 'pre_sale.php?act=list', 'text' => $_LANG['back_list']));
        sys_msg($_LANG['edit_success'], 0, $links);
    } elseif (isset($_POST['succeed'])) {
        /* 设置活动成功 */
        /* 判断订单状态 */
        if ($pre_sale['status'] != PSS_FINISHED) {
            sys_msg($_LANG['error_status'], 1);
        }
        /* 如果有订单,更新订单信息 */
        if ($pre_sale['total_order'] > 0) {
            /* 查找该预售活动的已确认或未确认订单(已取消的就不管了) */
            $sql = "SELECT order_id " . "FROM " . $ecs->table('order_info') . " WHERE extension_code = '" . PRE_SALE_CODE . "' " . "AND extension_id = '{$pre_sale_id}' " . "AND (order_status = '" . OS_CONFIRMED . "' or order_status = '" . OS_UNCONFIRMED . "')";
            $order_id_list = $db->getCol($sql);
            /* 更新订单商品价 */
            $final_price = $pre_sale['trans_price'];
            $sql = "UPDATE " . $ecs->table('order_goods') . " SET goods_price = '{$final_price}' " . "WHERE order_id " . db_create_in($order_id_list);
            $db->query($sql);
            /* 查询订单商品总额 */
            $sql = "SELECT order_id, SUM(goods_number * goods_price) AS goods_amount " . "FROM " . $ecs->table('order_goods') . " WHERE order_id " . db_create_in($order_id_list) . " GROUP BY order_id";
            $res = $db->query($sql);
            while ($row = $db->fetchRow($res)) {
                $order_id = $row['order_id'];
                $goods_amount = floatval($row['goods_amount']);
                /* 取得订单信息 */
                $order = order_info($order_id);
                /* 判断订单是否有效:余额支付金额 + 已付款金额 >= 保证金 */
                if ($order['surplus'] + $order['money_paid'] >= $pre_sale['deposit']) {
                    /* 有效,设为已确认,更新订单 */
                    // 更新商品总额
                    $order['goods_amount'] = $goods_amount;
                    // 如果保价,重新计算保价费用
                    if ($order['insure_fee'] > 0) {
                        $shipping = shipping_info($order['shipping_id']);
                        $order['insure_fee'] = shipping_insure_fee($shipping['shipping_code'], $goods_amount, $shipping['insure']);
                    }
                    // 重算支付费用
                    $order['order_amount'] = $order['goods_amount'] + $order['shipping_fee'] + $order['insure_fee'] + $order['pack_fee'] + $order['card_fee'] - $order['money_paid'] - $order['surplus'];
                    if ($order['order_amount'] > 0) {
                        $order['pay_fee'] = pay_fee($order['pay_id'], $order['order_amount']);
                    } else {
                        $order['pay_fee'] = 0;
                    }
                    // 计算应付款金额
                    $order['order_amount'] += $order['pay_fee'];
                    // 计算付款状态
                    if ($order['order_amount'] > 0) {
                        $order['pay_status'] = PS_UNPAYED;
                        $order['pay_time'] = 0;
                    } else {
                        $order['pay_status'] = PS_PAYED;
                        $order['pay_time'] = gmtime();
                    }
                    // 如果需要退款,退到帐户余额
                    if ($order['order_amount'] < 0) {
                        // todo (现在手工退款)
                    }
                    // 订单状态
                    $order['order_status'] = OS_CONFIRMED;
                    $order['confirm_time'] = gmtime();
                    // 更新订单
                    $order = addslashes_deep($order);
                    update_order($order_id, $order);
                } else {
                    /* 无效,取消订单,退回已付款 */
                    // 修改订单状态为已取消,付款状态为未付款
                    $order['order_status'] = OS_CANCELED;
                    $order['to_buyer'] = $_LANG['cancel_order_reason'];
                    $order['pay_status'] = PS_UNPAYED;
                    $order['pay_time'] = 0;
                    /* 如果使用余额或有已付款金额,退回帐户余额 */
                    $money = $order['surplus'] + $order['money_paid'];
                    if ($money > 0) {
                        $order['surplus'] = 0;
                        $order['money_paid'] = 0;
                        $order['order_amount'] = $money;
                        // 退款到帐户余额
                        order_refund($order, 1, $_LANG['cancel_order_reason'] . ':' . $order['order_sn']);
                    }
                    /* 更新订单 */
                    $order = addslashes_deep($order);
                    update_order($order['order_id'], $order);
                }
            }
        }
        /* 修改预售活动状态为成功 */
        $sql = "UPDATE " . $ecs->table('goods_activity') . " SET is_finished = '" . PSS_SUCCEED . "' " . "WHERE act_id = '{$pre_sale_id}' LIMIT 1";
        $db->query($sql);
        /* 清除缓存 */
        clear_cache_files();
        /* 提示信息 */
        $links = array(array('href' => 'pre_sale.php?act=list', 'text' => $_LANG['back_list']));
        sys_msg($_LANG['edit_success'], 0, $links);
    } elseif (isset($_POST['fail'])) {
        /* 设置活动失败 */
        /* 判断订单状态 */
        if ($pre_sale['status'] != PSS_FINISHED) {
            sys_msg($_LANG['error_status'], 1);
        }
        /* 如果有有效订单,取消订单 */
        if ($pre_sale['valid_order'] > 0) {
            /* 查找未确认或已确认的订单 */
            $sql = "SELECT * " . "FROM " . $ecs->table('order_info') . " WHERE extension_code = '" . PRE_SALE_CODE . "' " . "AND extension_id = '{$pre_sale_id}' " . "AND (order_status = '" . OS_CONFIRMED . "' OR order_status = '" . OS_UNCONFIRMED . "') ";
            $res = $db->query($sql);
            while ($order = $db->fetchRow($res)) {
                // 修改订单状态为已取消,付款状态为未付款
                $order['order_status'] = OS_CANCELED;
                $order['to_buyer'] = $_LANG['cancel_order_reason'];
                $order['pay_status'] = PS_UNPAYED;
                $order['pay_time'] = 0;
                /* 如果使用余额或有已付款金额,退回帐户余额 */
                $money = $order['surplus'] + $order['money_paid'];
                if ($money > 0) {
                    $order['surplus'] = 0;
                    $order['money_paid'] = 0;
                    $order['order_amount'] = $money;
                    // 退款到帐户余额
                    order_refund($order, 1, $_LANG['cancel_order_reason'] . ':' . $order['order_sn'], $money);
                }
                /* 更新订单 */
                $order = addslashes_deep($order);
                update_order($order['order_id'], $order);
            }
        }
        /* 修改预售活动状态为失败,记录失败原因(活动说明) */
        $sql = "UPDATE " . $ecs->table('goods_activity') . " SET is_finished = '" . PSS_FAIL . "', " . "act_desc = '{$_POST['act_desc']}' " . "WHERE act_id = '{$pre_sale_id}' LIMIT 1";
        $db->query($sql);
        /* 清除缓存 */
        clear_cache_files();
        /* 提示信息 */
        $links = array(array('href' => 'pre_sale.php?act=list', 'text' => $_LANG['back_list']));
        sys_msg($_LANG['edit_success'], 0, $links);
    } elseif (isset($_POST['mail'])) {
        /* 发送通知邮件 */
        /* 判断订单状态 */
        if ($pre_sale['status'] != PSS_SUCCEED) {
            sys_msg($_LANG['error_status'], 1);
        }
        /* 取得邮件模板 */
        $tpl = get_mail_template('pre_sale');
        /* 初始化订单数和成功发送邮件数 */
        $count = 0;
        $send_count = 0;
        /* 取得有效订单 */
        $sql = "SELECT o.consignee, o.add_time, g.goods_number, o.order_sn, " . "o.order_amount, o.order_id, o.email " . "FROM " . $ecs->table('order_info') . " AS o, " . $ecs->table('order_goods') . " AS g " . "WHERE o.order_id = g.order_id " . "AND o.extension_code = '" . PRE_SALE_CODE . "' " . "AND o.extension_id = '{$pre_sale_id}' " . "AND o.order_status = '" . OS_CONFIRMED . "'";
        $res = $db->query($sql);
        while ($order = $db->fetchRow($res)) {
            /* 邮件模板赋值 */
            $smarty->assign('consignee', $order['consignee']);
            $smarty->assign('add_time', local_date($_CFG['time_format'], $order['add_time']));
            $smarty->assign('goods_name', $pre_sale['goods_name']);
            $smarty->assign('goods_number', $order['goods_number']);
            $smarty->assign('order_sn', $order['order_sn']);
            $smarty->assign('order_amount', price_format($order['order_amount']));
            $smarty->assign('shop_url', $ecs->url() . 'user.php?act=order_detail&order_id=' . $order['order_id']);
            $smarty->assign('shop_name', $_CFG['shop_name']);
            $smarty->assign('send_date', local_date($_CFG['date_format']));
            /* 取得模板内容,发邮件 */
            $content = $smarty->fetch('str:' . $tpl['template_content']);
            if (send_mail($order['consignee'], $order['email'], $tpl['template_subject'], $content, $tpl['is_html'])) {
                $send_count++;
            }
            $count++;
        }
        /* 提示信息 */
        sys_msg(sprintf($_LANG['mail_result'], $count, $send_count));
    } else {
        /* 保存预售信息 */
        $goods_id = intval($_POST['goods_id']);
        if ($goods_id <= 0) {
            sys_msg($_LANG['error_goods_null']);
        }
        $info = goods_pre_sale($goods_id);
        if ($info && $info['act_id'] != $pre_sale_id) {
            sys_msg($_LANG['error_goods_exist']);
        }
        $goods_name = $db->getOne("SELECT goods_name FROM " . $ecs->table('goods') . " WHERE goods_id = '{$goods_id}'");
        $act_name = empty($_POST['act_name']) ? $goods_name : sub_str($_POST['act_name'], 0, 255, false);
        // 预售价格
        $sale_price = floatval($_POST['sale_price']);
        if ($sale_price < 0) {
            $sale_price = 0;
        }
        // 定金
        $deposit = floatval($_POST['deposit']);
        if ($deposit < 0) {
            $deposit = 0;
        }
        // 限购数量
        $restrict_amount = intval($_POST['restrict_amount']);
        if ($restrict_amount < 0) {
            $restrict_amount = 0;
        }
        // 赠送积分
        $gift_integral = intval($_POST['gift_integral']);
        if ($gift_integral < 0) {
            $gift_integral = 0;
        }
        $price_ladder = array();
        $count = count($_POST['ladder_amount']);
        for ($i = $count - 1; $i >= 0; $i--) {
            /* 如果数量小于等于0,不要 */
            $amount = intval($_POST['ladder_amount'][$i]);
            if ($amount <= 0) {
                continue;
            }
            /* 如果价格小于等于0,不要 */
            $price = round(floatval($_POST['ladder_price'][$i]), 2);
            if ($price <= 0) {
                continue;
            }
            /* 加入价格阶梯 */
            $price_ladder[$amount] = array('amount' => $amount, 'price' => $price);
        }
        if (count($price_ladder) < 1) {
            sys_msg($_LANG['error_price_ladder']);
        }
        /* 限购数量不能小于价格阶梯中的最大数量 */
        $amount_list = array_keys($price_ladder);
        if ($restrict_amount > 0 && max($amount_list) > $restrict_amount) {
            sys_msg($_LANG['error_restrict_amount']);
        }
        ksort($price_ladder);
        $price_ladder = array_values($price_ladder);
        /* 检查开始时间和结束时间是否合理 */
        $start_time = local_strtotime($_POST['start_time']);
        $end_time = local_strtotime($_POST['end_time']);
        if ($start_time >= $end_time) {
            // $_LANG['invalid_time']
            sys_msg('您输入了一个无效的时间,活动结束时间不能早于活动开始时间!');
        }
        if ($deposit > 0) {
            $retainage_start = local_strtotime($_POST['retainage_start']);
            $retainage_end = local_strtotime($_POST['retainage_end']);
            /* 检查活动结束时间和尾款开始支付时间是否合理 */
            if ($end_time >= $retainage_start) {
                // $_LANG['invalid_time']
                sys_msg('您输入了一个无效的时间,尾款开始支付时间不能早于活动结束时间!');
            }
            /* 检查尾款支付开始时间和结束时间是否合理 */
            if ($retainage_start >= $retainage_end) {
                // $_LANG['invalid_time']
                sys_msg('您输入了一个无效的时间,尾款结束支付时间不能早于尾款开始支付时间!');
            }
        } else {
            $retainage_start = '';
            $retainage_end = '';
        }
        // 预计发货时间描述
        $deliver_goods = $_POST['deliver_goods'];
        $pre_sale = array('act_name' => $act_name, 'act_desc' => $_POST['act_desc'], 'act_type' => GAT_PRE_SALE, 'goods_id' => $goods_id, 'goods_name' => $goods_name, 'start_time' => $start_time, 'end_time' => $end_time, 'ext_info' => serialize(array('sale_price' => $sale_price, 'retainage_start' => $retainage_start, 'retainage_end' => $retainage_end, 'price_ladder' => $price_ladder, 'restrict_amount' => $restrict_amount, 'gift_integral' => $gift_integral, 'deposit' => $deposit, 'deliver_goods' => $deliver_goods)));
        // 开始发货时间描述
        /* 清除缓存 */
        clear_cache_files();
        /* 保存数据 */
        if ($pre_sale_id > 0) {
            /* update */
            $db->autoExecute($ecs->table('goods_activity'), $pre_sale, 'UPDATE', "act_id = '{$pre_sale_id}'");
            /* log */
            admin_log(addslashes($goods_name) . '[' . $pre_sale_id . ']', 'edit', 'pre_sale');
            /* todo 更新活动表 */
            /* 提示信息 */
            $links = array(array('href' => 'pre_sale.php?act=list&' . list_link_postfix(), 'text' => $_LANG['back_list']));
            sys_msg($_LANG['edit_success'], 0, $links);
        } else {
            /* insert */
            $db->autoExecute($ecs->table('goods_activity'), $pre_sale, 'INSERT');
            /* log */
            admin_log(addslashes($goods_name), 'add', 'pre_sale');
            /* 提示信息 */
            $links = array(array('href' => 'pre_sale.php?act=add', 'text' => $_LANG['continue_add']), array('href' => 'pre_sale.php?act=list', 'text' => $_LANG['back_list']));
            sys_msg($_LANG['add_success'], 0, $links);
        }
    }
}