$this->load->model('localisation/currency');
$this->load->model('catalog/product');
$totals_language = $this->language->load('openbay/ebay_order');
$currency = $this->model_localisation_currency->getCurrencyByCode($this->config->get('openbay_def_currency'));
$address_format = $this->model_openbay_ebay_order->getCountryAddressFormat((string)$order->address->iso2);
//try to get zone id - this will only work if the zone name and country id exist in the DB.
$zone_id = $this->openbay->getZoneId($order->address->state, $user['country_id']);
if(empty($address_format)){
$address_format = (string)$this->config->get('openbay_default_addressformat');
}
//try to get the friendly name for the shipping service
$shipping_service = $this->openbay->ebay->getShippingServiceInfo($order->shipping->service);
if($shipping_service != false){
$shipping_service_name = $shipping_service['description'];
}else{
$shipping_service_name = $order->shipping->service;
}
$this->db->query("
UPDATE `" . DB_PREFIX . "order`
SET
`customer_id` = '" . (int)$user['id'] . "',
`customer_group_id` = '" . (int)$this->config->get('openbay_def_customer_grp') . "',
`firstname` = '" . $this->db->escape($user['fname']) . "',
`lastname` = '" . $this->db->escape($user['lname']) . "',
`email` = '" . $this->db->escape($order->user->email) . "',
`telephone` = '" . $this->db->escape($order->address->phone) . "',
`shipping_firstname` = '" . $this->db->escape($user['fname']) . "',
`shipping_lastname` = '" . $this->db->escape($user['lname']) . "',
`shipping_address_1` = '" . $this->db->escape($order->address->street1) . "',
`shipping_address_2` = '" . $this->db->escape($order->address->street2) . "',
`shipping_city` = '" . $this->db->escape($order->address->city) . "',
`shipping_postcode` = '" . $this->db->escape($order->address->postcode) . "',
`shipping_country` = '" . $this->db->escape($user['country']) . "',
`shipping_country_id` = '" . (int)$user['country_id'] . "',
`shipping_zone` = '" . $this->db->escape($order->address->state) . "',
`shipping_zone_id` = '" . (int)$zone_id . "',
`shipping_method` = '" . $this->db->escape($shipping_service_name) . "',
`shipping_address_format` = '" . $this->db->escape($address_format) . "',
`payment_firstname` = '" . $this->db->escape($user['fname']) . "',
`payment_lastname` = '" . $this->db->escape($user['lname']) . "',
`payment_address_1` = '" . $this->db->escape($order->address->street1) . "',
`payment_address_2` = '" . $this->db->escape($order->address->street2) . "',
`payment_city` = '" . $this->db->escape($order->address->city) . "',
`payment_postcode` = '" . $this->db->escape($order->address->postcode) . "',
`payment_country` = '" . $this->db->escape($user['country']) . "',
`payment_country_id` = '" . (int)$user['country_id'] . "',
`payment_zone` = '" . $this->db->escape($order->address->state) . "',
`payment_zone_id` = '" . (int)$zone_id . "',
`comment` = '" . $this->db->escape($order->order->message) . "',
`payment_method` = '" . $this->db->escape($order->payment->method) . "',
`payment_address_format` = '" . $address_format . "',
`total` = '" . (double)$order->order->total . "',
`date_modified` = NOW()
WHERE `order_id` = '".$this->db->escape($order_id)."'
");
$totalTax = 0;
$totalNet = 0;
/* force array type */
if(!is_array($order->txn)) {
$order->txn = array($order->txn);
}
foreach ($order->txn as $txn) {
$qty = (int)$txn->item->qty;
$price = (double)$txn->item->price;
if($this->tax_type == 1){
//calculate taxes that come in from eBay
$this->openbay->ebay->log('updateOrderWithConfirmedData() - Using tax rates from eBay');
$totalTax += (double)$txn->item->tax->total;
$totalNet += $price * $qty;
}else{
//use the store pre-set tax-rate for everything
$this->openbay->ebay->log('updateOrderWithConfirmedData() - Using tax rates from store');
$itemNet = $price / $this->tax;
$itemTax = $price - $itemNet;
$lineNet = $itemNet * $qty;
$lineTax = $itemTax * $qty;
$totalTax += number_format($lineTax, 4,'.','');
$totalNet += $lineNet;
}
}
if($this->tax_type == 1){
$discountNet = (double)$order->order->discount;
$shippingNet = (double)$order->shipping->cost;
$tax = number_format($totalTax, 4,'.','');
}else{
$discountNet = (double)$order->order->discount / $this->tax;
$discountTax = (double)$order->order->discount - $discountNet;
$shippingNet = (double)$order->shipping->cost / $this->tax;
$shippingTax = (double)$order->shipping->cost - $shippingNet;
$tax = number_format($shippingTax + $totalTax + $discountTax, 4,'.','');
}
$totals = number_format((double)$totalNet + (double)$shippingNet + (double)$tax + (double)$discountNet, 4,'.','');
$data = array();
$data['totals'][0] = array(
'code' => 'sub_total',
'title' => $totals_language['lang_subtotal'],
'text' => $this->db->escape($currency['symbol_left']).number_format($totalNet, $currency['decimal_place'],'.','').$this->db->escape($currency['symbol_right']),
'value' => number_format((double)$totalNet, 4,'.',''),
'sort_order' => '1'
);
$data['totals'][1] = array(
'code' => 'shipping',
'title' => $totals_language['lang_shipping'],
'text' => $this->db->escape($currency['symbol_left']).number_format($shippingNet, $currency['decimal_place'],'.','').$this->db->escape($currency['symbol_right']),
'value' => number_format((double)$shippingNet, 4,'.',''),
'sort_order' => '3'
);
$data['totals'][2] = array(
'code' => 'credit',
'title' => $totals_language['lang_discount'],
'text' => $this->db->escape($currency['symbol_left']).number_format($discountNet, $currency['decimal_place'],'.','').$this->db->escape($currency['symbol_right']),
'value' => number_format((double)$discountNet, 4,'.',''),
'sort_order' => '4'
);
$data['totals'][3] = array(
'code' => 'tax',
'title' => $totals_language['lang_tax'],
'text' => $this->db->escape($currency['symbol_left']).number_format($tax, $currency['decimal_place'],'.','').$this->db->escape($currency['symbol_right']),
'value' => number_format((double)$tax, 3,'.',''),
'sort_order' => '5'
);
$data['totals'][4] = array(
'code' => 'total',
'title' => $totals_language['lang_total'],
'text' => $this->db->escape($currency['symbol_left']).number_format($totals, $currency['decimal_place'],'.','').$this->db->escape($currency['symbol_right']),
'value' => $totals,
'sort_order' => '6'
);
foreach ($data['totals'] as $total) {
$this->db->query("INSERT INTO `" . DB_PREFIX . "order_total` SET `order_id` = '" . (int)$order_id . "', `code` = '" . $this->db->escape($total['code']) . "', `title` = '" . $this->db->escape($total['title']) . "', `text` = '" . $this->db->escape($total['text']) . "', `value` = '" . (double)$total['value'] . "', `sort_order` = '" . (int)$total['sort_order'] . "'");
}