if ($this->openbay->addonLoad('openstock')) {
$openstock = true;
}else{
$openstock = false;
}
$this->load->model('localisation/currency');
$this->load->model('catalog/product');
$currency = $this->model_localisation_currency->getCurrencyByCode($this->config->get('openbay_def_currency'));
if($this->config->get('openbaypro_create_date') == 1){
$createdDateObj = new DateTime((string)$order->order->created);
$offset = ($this->config->get('openbaypro_time_offset') != '' ? (int)$this->config->get('openbaypro_time_offset') : (int)0);
$createdDateObj->modify($offset . ' hour');
$createdDate = $createdDateObj->format('Y-m-d H:i:s');
}else{
$createdDate = date("Y-m-d H:i:s");
$offset = 0;
}
$this->openbay->ebay->log('create() - Order date: '.$createdDate);
$this->openbay->ebay->log('create() - Original date: '.(string)$order->order->created);
$this->openbay->ebay->log('create() - Offset: '.$offset);
$this->openbay->ebay->log('create() - Server time: '.date("Y-m-d H:i:s"));
$this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET
`store_id` = '" . (int)$this->config->get('config_store_id') . "',
`store_name` = '" . $this->db->escape($this->config->get('config_name') . ' / eBay') . "',
`store_url` = '" . $this->db->escape($this->config->get('config_url')) . "',
`invoice_prefix` = '" . $this->db->escape($this->config->get('config_invoice_prefix')) . "',
`comment` = '" . $this->db->escape((string)$order->order->message) . "',
`total` = '" . (double)$order->order->total . "',
`affiliate_id` = '0',
`commission` = '0',
`language_id` = '" . (int)$this->config->get('config_language_id') . "',
`currency_id` = '" . (int)$currency['currency_id'] . "',
`currency_code` = '" . $this->db->escape($currency['code']) . "',
`currency_value` = '" . (double)$currency['value'] . "',
`ip` = '',
`date_added` = '" . $createdDate . "',
`date_modified` = NOW(),
`customer_id` = 0
");
$order_id = $this->db->getLastId();
foreach ($order->txn as $txn) {
$product_id = $this->openbay->ebay->getProductId($txn->item->id);
if($product_id != false){
$this->openbay->ebay->log('create() - Product ID: "'.$product_id.'" from ebay item: '.$txn->item->id.' was returned');
if(!empty($txn->item->variantsku) && $openstock == true){
$model_number = $this->openbay->getProductModelNumber($product_id, $txn->item->variantsku);
}else{
$model_number = $this->openbay->getProductModelNumber($product_id);
}
}else{
$this->openbay->ebay->log('create() - No product ID from ebay item: '.$txn->item->id.' was returned');
$model_number = '';
}
$qty = (int)$txn->item->qty;
$price = (double)$txn->item->price;
$this->openbay->ebay->log('create() - Item price: '.$price);
if($this->tax_type == 1){
//calculate taxes that come in from eBay
$this->openbay->ebay->log('create() - Using tax rates from eBay');
$priceNet = $price;
$this->openbay->ebay->log('create() - Net price: '.$priceNet);
$totalNet = $price * $qty;
$this->openbay->ebay->log('create() - Total net price: '.$totalNet);
$tax = number_format((double)$txn->item->tax->item, 4,'.','');
$this->openbay->ebay->log('create() - Tax: '.$tax);
}else{
//use the store pre-set tax-rate for everything
$this->openbay->ebay->log('create() - Using tax rates from store');
$priceNet = $price / $this->tax;
$this->openbay->ebay->log('create() - Net price: '.$priceNet);
$totalNet = $priceNet * $qty;
$this->openbay->ebay->log('create() - Total net price: '.$totalNet);
$tax = number_format(($price - $priceNet), 4,'.','');
$this->openbay->ebay->log('create() - Tax: '.$tax);
}
$txn->item->name = stripslashes($txn->item->name);
$txn->item->varianttitle = stripslashes($txn->item->varianttitle);
$txn->item->sku = stripslashes($txn->item->sku);
$txn->item->variantsku = stripslashes($txn->item->variantsku);
$this->db->query("INSERT INTO `" . DB_PREFIX . "order_product` SET
`order_id` = '" . (int)$order_id . "',
`product_id` = '" . (int)$product_id . "',
`name` = '" . $this->db->escape(( (isset($txn->item->varianttitle) && !empty($txn->item->varianttitle)) ? $txn->item->varianttitle : $txn->item->name )) . "',
`model` = '" . $this->db->escape($model_number) . "',
`quantity` = '" . (int)$qty . "',
`price` = '" . (double)$priceNet . "',
`total` = '" . (double)$totalNet . "',
`tax` = '" . (double)$tax . "'
");
$order_product_id = $this->db->getLastId();
$this->openbay->ebay->log('create() - Added order product id '.$order_product_id);
if($openstock == true){
$this->openbay->ebay->log('create() - OpenStock enabled');
if(!empty($txn->item->variantsku)){
$this->openbay->ebay->log($txn->item->variantsku);
if($product_id != false){
$skuParts = explode(':', $txn->item->variantsku);
$pOptions = array();
foreach($skuParts as $part){
$sql = "SELECT
`pv`.`product_option_id`,
`pv`.`product_option_value_id`,
`od`.`name`,
`ovd`.`name` as `value`,
`o`.`option_id`,
`o`.`type`
FROM `" . DB_PREFIX . "product_option_value` `pv`
LEFT JOIN `" . DB_PREFIX . "option_value` `ov` ON (`pv`.`option_value_id` = `ov`.`option_value_id`)
LEFT JOIN `" . DB_PREFIX . "option_value_description` `ovd` ON (`ovd`.`option_value_id` = `ov`.`option_value_id`)
LEFT JOIN `" . DB_PREFIX . "option_description` `od` ON (`ov`.`option_id` = `od`.`option_id`)
LEFT JOIN `" . DB_PREFIX . "option` `o` ON (`o`.`option_id` = `od`.`option_id`)
WHERE `pv`.`product_option_value_id` = '".(int)$part."'
AND `pv`.`product_id` = '".(int)$product_id."'";
$option_qry = $this->db->query($sql);
if(!empty($option_qry->row)){
$pOptions[] = array(
'product_option_id' => $option_qry->row['product_option_id'],
'product_option_value_id' => $option_qry->row['product_option_value_id'],
'name' => $option_qry->row['name'],
'value' => $option_qry->row['value'],
'type' => $option_qry->row['type'],
);
}
}
//insert into order_option table
foreach ($pOptions as $option) {
$this->db->query("
INSERT INTO `" . DB_PREFIX . "order_option`
SET
`order_id` = '" . (int)$order_id . "',
`order_product_id` = '" . (int)$order_product_id . "',
`product_option_id` = '" . (int)$option['product_option_id'] . "',
`product_option_value_id` = '" . (int)$option['product_option_value_id'] . "',
`name` = '" . $this->db->escape($option['name']) . "',
`value` = '" . $this->db->escape($option['value']) . "',
`type` = '" . $this->db->escape($option['type']) . "'
");
}
}
}else{
$this->openbay->ebay->log('create() - No variant sku');
}
}
}
return $order_id;