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); }
public function merge_order_for_auto_assign($order_id) { $order = $this->get_order($order_id); if (empty($order)) { return FALSE; } $epacket_shipping_code = 'H'; $status = fetch_status_id('order_status', 'wait_for_confirmation'); $can_merge_where = array('id !=' => $order_id, 'name' => $order->name, 'country' => $order->country, 'state_province' => $order->state_province, 'town_city' => $order->town_city, 'address_line_1' => $order->address_line_1, 'address_line_2' => $order->address_line_2, 'input_user' => $order->input_user, 'order_status' => $status, 'is_register !=' => $epacket_shipping_code, 'ebay_id' => $order->ebay_id); $this->db->select('*'); $this->db->where($can_merge_where); $this->db->from('order_list'); $query = $this->db->get(); $to_merge_order = $query->row(); if (empty($to_merge_order->id)) { return FALSE; } // starting merging if ($to_merge_order->currency == $order->currency) { $new_gross = $to_merge_order->gross + $order->gross; $new_fee = $to_merge_order->fee + $order->fee; } else { $tmp_rmb = calc_currency($order->currency, $order->gross); $new_gross = $to_merge_order->gross + price(to_foreigh_currency($to_merge_order->currency, $tmp_rmb)); $tmp_rmb = calc_currency($order->currency, $order->fee); $new_fee = $to_merge_order->fee + price(to_foreigh_currency($to_merge_order->currency, $tmp_rmb)); } echo "new gross: {$new_gross}\n"; echo "new fee: {$new_fee}\n"; echo "to merge gross: {$to_merge_order->gross}\n"; echo "to merge fee: {$to_merge_order->fee}\n"; echo "to merge currency: {$to_merge_order->currency}\n"; echo "order gross: {$order->gross}\n"; echo "order gross: {$order->fee}\n"; echo "gross: {$order->currency}\n"; $new_net = $new_gross - $new_fee; $new_title = $to_merge_order->item_title_str . ITEM_TITLE_SEP . $order->item_title_str; $new_id = $to_merge_order->item_id_str . ',' . $order->item_id_str; $new_sku_str = $to_merge_order->sku_str . ',' . $order->sku_str; $new_amount = $to_merge_order->qty_str . ',' . $order->qty_str; $sys_remark = $to_merge_order->sys_remark . ', ' . sprintf(lang('merge_log'), get_current_time(), $order->transaction_id); $data = array('gross' => $new_gross, 'fee' => $new_fee, 'net' => $new_net, 'item_title_str' => $new_title, 'item_id_str' => $new_id, 'sku_str' => $new_sku_str, 'qty_str' => $new_amount, 'sys_remark' => $sys_remark); $this->update('order_list', array('id' => $to_merge_order->id), $data); $this->db->insert('order_merged_list', array('transaction_id' => $order->transaction_id)); $this->delete('order_list', array('id' => $order->id)); if (!isset($this->CI->order_role_model)) { $this->CI->load->model('order_role_model'); } $this->CI->order_role_model->add_order_role($order_id); $data = array('import_date' => get_current_time(), 'user_name' => lang('program'), 'descript' => sprintf(lang('merge_log'), get_current_time(), $order->transaction_id), 'user_login' => lang('program')); $this->import_log($data); echo 'auto merge: to merge id: ', $to_merge_order->id, "\n"; return TRUE; }
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); }
public function make_pi() { $product_count = $this->input->post('product_count'); $user_id = $this->get_current_user_id(); $user_info = $this->user_model->fetch_user_by_id($user_id); for ($i = 0; $i < $product_count; $i++) { $sku = trim($this->input->post('sku_' . $i)); $product = $this->product_model->fetch_product_by_sku($sku); if (!$product) { echo $this->create_json(0, sprintf(lang('product_sku_doesnot_exists_with_sku'), $sku)); return; } $price = $product->price / 0.75; $product_price[$i] = price(to_foreigh_currency('USD', $price)); $product_sku[$i] = $product->sku; $product_image_url[$i] = $product->image_url; $product_name_en[$i] = $product->name_en; $qty = trim($this->input->post('qty_' . $i)); if (!is_numeric($qty) or $qty <= 0) { echo $this->create_json(0, lang('qty_not_natural')); return; } $sub_total[$i] = $product_price[$i] * $qty; $qtys[$i] = $qty; } $sum = array_sum($sub_total); $user_name = $user_info->name_en; $date = date('Y-m-d'); $currency = 'USD'; $data = array('product_count' => $product_count, 'product_price' => $product_price, 'product_sku' => $product_sku, 'product_image_url' => $product_image_url, 'product_name_en' => $product_name_en, 'qtys' => $qtys, 'sub_total' => $sub_total, 'user_info' => $user_info, 'product' => $product, 'sum' => $sum, 'user_name' => $user_name, 'date' => $date, 'currency' => $currency); $this->load->view('sale/price/make_price_pi', $data); }
function _get_currency_str($currency_code, $default_currency_code, $price) { return "<div style='padding:5px;'>" . $currency_code . ": " . price(to_foreigh_currency($currency_code, $price)) . br() . br() . $default_currency_code . ": " . price($price) . "</div>"; }
private function _merge_order($to_merged_id, $transaction_details) { $order_obj = $this->paypal_model->get_order_info_for_merge($to_merged_id); if (!$order_obj) { return $this->_waiting_comfirmed_order($transaction_details); } $gross = $order_obj->gross; $fee = $order_obj->fee; $shippingamt = $order_obj->shippingamt; $currency = $order_obj->currency; $item_title = $order_obj->item_title_str; $item_id = $order_obj->item_id_str; $sm_products_code = $order_obj->sku_str; $amount = $order_obj->qty_str; $item_no = $order_obj->item_no; $is_register = $order_obj->is_register; $item_info = array('item_titles' => array_merge(explode(ITEM_TITLE_SEP, $item_title), $this->item_info['item_titles']), 'item_ids' => array_merge(explode(',', $item_id), $this->item_info['item_ids']), 'item_codes' => array_merge(explode(',', $sm_products_code), $this->item_info['item_codes']), 'item_qties' => array_merge(explode(',', $amount), $this->item_info['item_qties'])); $item_info = $this->_merge_duplicated_order($item_info); if ($currency == $transaction_details['CURRENCYCODE']) { $new_gross = $gross + $transaction_details['AMT']; $new_fee = $fee + $transaction_details['FEEAMT']; $new_shippingamt = $shippingamt + $transaction_details['SHIPPINGAMT']; } else { $tmp_rmb = calc_currency($transaction_details['CURRENCYCODE'], $transaction_details['AMT']); $new_gross = $gross + price(to_foreigh_currency($currency, $tmp_rmb)); $tmp_rmb = calc_currency($transaction_details['CURRENCYCODE'], $transaction_details['FEEAMT']); $new_fee = $fee + price(to_foreigh_currency($currency, $tmp_rmb)); $tmp_rmb = calc_currency($transaction_details['CURRENCYCODE'], $transaction_details['SHIPPINGAMT']); $new_shippingamt = $shippingamt + price(to_foreigh_currency($currency, $tmp_rmb)); } echo "{$new_gross}, {$new_fee}, {$new_shippingamt}\n"; echo "currency: {$currency}\n"; echo "transaction_details: {$transaction_details['CURRENCYCODE']}\n"; echo "gross: {$gross}, {$transaction_details['AMT']}\n"; echo "fee: {$fee}, {$transaction_details['FEEAMT']}\n"; echo "shippingamt: {$shippingamt}, {$transaction_details['SHIPPINGAMT']}\n"; $paypal_transaction_id = array($transaction_details['TRANSACTIONID'], $order_obj->transaction_id); $new_net = $new_gross - $new_fee; $new_title = implode(ITEM_TITLE_SEP, $item_info['item_titles']); $new_id = implode(',', $item_info['item_ids']); $new_sm_products_code = implode(',', $item_info['item_codes']); $new_amount = implode(',', $item_info['item_qties']); $new_gross_usd = $this->order_model->to_usd($currency, $new_gross); $item_info['gross_usd'] = $new_gross_usd; $item_info['gross'] = $new_gross; $new_is_register = get_register($item_info, $this->user, $currency, $paypal_transaction_id); $new_item_no = change_item_register($item_no, $is_register, $new_is_register); $order_status = $this->order_statuses['wait_for_confirmation']; $data = array('transaction_id' => $transaction_details['TRANSACTIONID'], 'gross' => $new_gross, 'fee' => $new_fee, 'net' => $new_net, 'shippingamt' => $new_shippingamt, 'item_title_str' => $new_title, 'item_id_str' => $new_id, 'sku_str' => $new_sm_products_code, 'qty_str' => $new_amount, 'is_register' => $new_is_register, 'item_no' => $new_item_no, 'order_status' => $order_status); $sys_remark = $this->_create_sys_remark('merged_order', $transaction_details['TRANSACTIONID']); $this->paypal_model->merge_order($to_merged_id, $data, $sys_remark); $this->report['merged_order']++; }