Exemple #1
0
 protected function _calculate_profit_rate($order_data)
 {
     $shipping_code = $order_data['shipping_code'];
     $item_ids = explode(',', $order_data['item_id_str']);
     $product_cost = $order_data['price'];
     $transaction_ids = $order_data['transaction_ids'];
     $currency = $order_data['currency'];
     $gross = price(calc_currency($currency, $order_data['gross']), 2);
     $net = price(calc_currency($currency, $order_data['net']), 2);
     $shipping_weight = $order_data['shipping_weight'];
     $country = $order_data['country'];
     $trade_fee = 0;
     $phone = '';
     $item_ids = array_unique($item_ids);
     $i = 0;
     $listing_fee = 0;
     // Todo: chinese auction
     foreach ($item_ids as $item_id) {
         if (is_array($transaction_ids)) {
             $transaction_id = $transaction_ids[$i];
         } else {
             $transaction_id = $transaction_ids;
         }
         $ebay_order = $this->sale_model->fetch_ebay_order($item_id, $transaction_id);
         // it's not correct for merged or splited orders.
         if (!empty($ebay_order)) {
             $currency = $ebay_order->currency;
             $trade_fee += price(calc_currency($currency, $ebay_order->final_value_fee), 2);
             $phone = '';
             if ($ebay_order->transaction_id == 0) {
                 $listing = $this->ebay_order_model->fetch_listing_fee($item_id);
                 if (isset($listing->listing_fee)) {
                     $listing_fee += price(calc_currency($listing->listing_fee_currency, $listing->listing_fee));
                 }
             }
             if ($phone == 'Invalid Request') {
                 $phone = '';
             }
         }
         $i++;
     }
     $country_cn = get_country_name_cn($country);
     $max_length = 0;
     $max_width = 0;
     $total_height = 0;
     $shipping_rule = _fetch_shipping_cost($shipping_weight, $shipping_code, $country_cn, $max_length, $max_width, $total_height);
     $shipping_cost = isset($shipping_rule['price']) ? $shipping_rule['price'] : 0;
     $profit = price(to_foreigh_currency('USD', $net - $product_cost - $shipping_cost - $trade_fee - $listing_fee));
     $profit_rate = price($profit / $gross, 4);
     if ($profit_rate == 0) {
         $profit_rate = 0.001;
     }
     return array('profit' => $profit, 'profit_rate' => $profit_rate, 'trade_fee' => $trade_fee, 'listing_fee' => $listing_fee, 'product_cost' => $product_cost, 'shipping_cost' => $shipping_cost, 'phone' => $phone);
 }
function price_profit_rate($data)
{
    $CI =& get_instance();
    $eshop_code = $data['eshop_code'];
    $buyer_shipping_cost = $data['buyer_shipping_cost'];
    $shipping_type = $data['shipping_type'];
    $shipping_country = $data['shipping_country'];
    $total_weight = $data['total_weight'];
    $sale_mode = $data['sale_mode'];
    $eshop_category = $data['eshop_category'];
    $suggest_price = $data['suggest_price'];
    $key = $data['key'];
    $balance_profit = $data['balance_profit'];
    $total_price = $data['total_price'];
    $eshop_list_count = $data['eshop_list_count'];
    $eshop_list_fee_multiply = $data['eshop_list_fee_multiply'];
    $pay_option = $data['pay_option'];
    $pay_discount = $data['pay_discount'];
    $other_cost = $data['other_cost'];
    $max_length = $data['max_length'];
    $max_width = $data['max_width'];
    $total_height = $data['total_height'];
    $currency_code = $CI->fee_price_model->fetch_eshop_currency_code($eshop_code);
    $buyer_shipping_cost_original = $buyer_shipping_cost;
    $buyer_shipping_cost = calc_currency($currency_code, $buyer_shipping_cost);
    $special_types = array('X');
    if (in_array($shipping_type, $special_types)) {
        $shipping_method_oject = $CI->shipping_code_model->fetch_shipping_method($shipping_type);
        $shipping_cost = 0;
        $shipping_type_name = $shipping_method_oject ? $shipping_method_oject->name_cn : '';
    } else {
        $shipping_rule = _fetch_shipping_cost($total_weight, $shipping_type, $shipping_country, $max_length, $max_width, $total_height);
        if (!$shipping_rule) {
            return lang('no_suitable_shipping_type');
        }
        $shipping_cost = $shipping_rule['price'];
        $shipping_type_name = $shipping_rule['type']->type_name;
    }
    $product_cost = $total_price;
    // calculate profit
    $price = $key ? calc_currency($currency_code, $suggest_price) : $product_cost;
    $test_profit = 0;
    $deviation = 1.0E-14;
    $min_profit = $balance_profit - $deviation;
    $max_profit = $balance_profit + $deviation;
    $try_count = 0;
    do {
        $eshop_price = to_foreigh_currency($currency_code, $price);
        $eshop_list_fee = $CI->fee_price_model->fetch_eshop_list_formula($eshop_code, $sale_mode, $eshop_category, $eshop_price);
        if ($eshop_list_fee === NULL) {
            return lang('eshop_list_fee_not_set');
        }
        if ($eshop_code == 'ebay-USA') {
            $price_for_trade = $key ? calc_currency($currency_code, $suggest_price) + $buyer_shipping_cost : $price + $buyer_shipping_cost;
            $price_for_trade = to_foreigh_currency($currency_code, $price_for_trade);
        } else {
            $price_for_trade = $eshop_price;
        }
        $eshop_trade_fee = $CI->fee_price_model->fetch_eshop_trade_formula($eshop_code, $sale_mode, $eshop_category, $price_for_trade);
        if ($eshop_trade_fee === NULL) {
            return lang('eshop_trade_fee_not_set') . $price_for_trade;
        }
        $eshop_list_fee = _eval_fee($eshop_list_fee, $eshop_price) / $eshop_list_count;
        // for auction;
        $eshop_list_fee *= $eshop_list_fee_multiply;
        $eshop_trade_fee = _eval_fee($eshop_trade_fee, $price_for_trade);
        $eshop_trade_fee = $eshop_trade_fee * (1 - $pay_discount);
        $pay_fee = isset($data['paypal_cost']) ? $data['paypal_cost'] : _eval_fee($pay_option, $eshop_price + $buyer_shipping_cost_original);
        $eshop_list_fee_default = calc_currency($currency_code, $eshop_list_fee);
        $eshop_trade_fee_default = calc_currency($currency_code, $eshop_trade_fee);
        $pay_fee_default = calc_currency($currency_code, $pay_fee);
        $total_cost = $product_cost + $pay_fee_default + $eshop_list_fee_default + $eshop_trade_fee_default + $shipping_cost + $other_cost;
        $test_profit = ($price + $buyer_shipping_cost - $total_cost) / ($price + $buyer_shipping_cost - $pay_fee_default);
        if ($key) {
            break;
        }
        if ($test_profit >= $min_profit && $test_profit <= $max_profit) {
            break;
        }
        if ($test_profit < $min_profit) {
            // too low
            $price = $price + $price * (($min_profit + $max_profit) / 2 - $test_profit);
        } else {
            // too hight
            $price = $price - $price * ($test_profit - ($min_profit + $max_profit) / 2);
        }
    } while (1);
    return array('suggest_price' => $price, 'buyer_shipping_cost' => $buyer_shipping_cost, 'list_fee' => $eshop_list_fee_default, 'trade_fee' => $eshop_trade_fee_default, 'pay_fee' => $pay_fee_default, 'shipping_cost' => $shipping_cost, 'shipping_type_name' => $shipping_type_name, 'other_cost' => $other_cost, 'product_cost' => $product_cost, 'total_cost' => $total_cost, 'total_profit' => $price + $buyer_shipping_cost - $total_cost, 'total_profit_rate' => $test_profit, 'currency_code' => $currency_code, 'default_currency_code' => DEFAULT_CURRENCY_CODE, 'total_weight' => $total_weight);
}
function order_raw_cost($order_id)
{
    $CI =& get_instance();
    if (!isset($CI->order_model)) {
        $CI->load->model('order_model');
    }
    if (!isset($CI->product_model)) {
        $CI->load->model('product_model');
    }
    if (!function_exists('_fetch_shipping_cost')) {
        $CI->load->helper('shipping');
    }
    $order = $CI->order_model->get_order($order_id);
    if (empty($order)) {
        return;
    }
    $skus = explode(',', $order->sku_str);
    $qties = explode(',', $order->qty_str);
    $product_cost = 0;
    $shipping_weight = 0;
    $i = 0;
    foreach ($skus as $sku) {
        $qty = $qties[$i];
        $product = $CI->product_model->fetch_product_by_sku($sku);
        $product_cost += $product->price * $qty;
        $shipping_weight += $product->total_weight * $qty;
        $i++;
    }
    $country_cn = get_country_name_cn($order->country);
    $shipping_rule = _fetch_shipping_cost($shipping_weight, $order->is_register, $country_cn);
    $shipping_cost = isset($shipping_rule['price']) ? $shipping_rule['price'] : 0;
    return array('product_cost' => $product_cost, 'shipping_cost' => $shipping_cost);
}