$this->openbay->ebay->log('Starting item import');
$this->load->model('catalog/product');
//check for ebay import img table
$res = $this->db->query("SHOW TABLES LIKE '".DB_PREFIX."ebay_image_import'");
if($res->num_rows == 0) {
$this->db->query("
CREATE TABLE IF NOT EXISTS `".DB_PREFIX."ebay_image_import` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_original` text NOT NULL,
`image_new` text NOT NULL,
`name` text NOT NULL,
`product_id` int(11) NOT NULL,
`imgcount` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
");
}
if ($this->openbay->addonLoad('openstock')) {
$openstock = true;
$this->load->model('openstock/openstock');
}else{
$openstock = false;
$this->openbay->ebay->log('Openstock module not found');
}
$categories = array();
$data['data'] = unserialize(gzuncompress(stripslashes(base64_decode(strtr($data['data'], '-_,', '+/=')))));
$newData = base64_decode($data['data']);
$options = json_decode($data['options'], 1);
unset($data['data']);
$this->openbay->ebay->log('Decoded data');
$newData1 = unserialize($newData);
unset($newData);
$this->openbay->ebay->log('Data unserialized');
if ($options['cat'] == 1) {
$itemCountLoop = 0;
foreach($newData1 as $item) {
$itemCountLoop++;
$this->openbay->ebay->log('Processing item: '.$itemCountLoop);
$parts = explode(':', $item['CategoryName']);
//skip the first category as they are likely to be selling in that
if(isset($parts[1])) {
if(!isset($categories[$parts[0]][$parts[1]])) {
if(!empty($parts[1])) {
$categories[$parts[0]][$parts[1]] = array();
}
}
}
if(isset($parts[2])) {
if(!isset($categories[$parts[0]][$parts[1]][$parts[2]])) {
if(!empty($parts[2])) {
$categories[$parts[0]][$parts[1]][$parts[2]] = array();
}
}
}
if(isset($parts[3])) {
if(!isset($categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]])) {
if(!empty($parts[3])) {
$categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]] = array();
}
}
}
if(isset($parts[4])) {
if(!isset($categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]])) {
if(!empty($parts[4])) {
$categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]] = array();
}
}
}
if(isset($parts[5])) {
if(!isset($categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]][$parts[5]])) {
if(!empty($parts[5])) {
$categories[$parts[0]][$parts[1]][$parts[2]][$parts[3]][$parts[4]][$parts[5]] = array();
}
}
}
}
$catLink = array();
foreach($categories as $key1=>$cat1) {
foreach($cat1 as $key2=>$cat2) {
//final cat, add to array as node
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category`, `" . DB_PREFIX . "category_description` WHERE `" . DB_PREFIX . "category`.`parent_id` = '0' AND `" . DB_PREFIX . "category_description`.`name` = '".$this->db->escape($key2)."' LIMIT 1");
if($qry->num_rows != 0) {
$id1 = $qry->row['category_id'];
}else{
$this->db->query("INSERT INTO `" . DB_PREFIX . "category` SET `parent_id` = '0', `status` = '1', `top` = '1'");
$id1 = $this->db->getLastId();
$this->db->query("INSERT INTO `" . DB_PREFIX . "category_description` SET `name` = '".$this->db->escape($key2)."', `language_id` = '".(int)$this->config->get('config_language_id')."', `category_id` = '".$this->db->escape($id1)."'");
$this->db->query("INSERT INTO `" . DB_PREFIX . "category_to_store` SET `category_id` = '".$this->db->escape($id1)."', `store_id` = '0'");
}
if(!empty($cat2)) {
foreach($cat2 as $key3=>$cat3) {
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category`, `" . DB_PREFIX . "category_description` WHERE `" . DB_PREFIX . "category`.`parent_id` = '".$this->db->escape($id1)."' AND `" . DB_PREFIX . "category_description`.`name` = '".$this->db->escape($key3)."' LIMIT 1");
if($qry->num_rows != 0) {
$id2 = $qry->row['category_id'];
}else{
$this->db->query("INSERT INTO `" . DB_PREFIX . "category` SET `parent_id` = '".$this->db->escape($id1)."', `status` = '1', `top` = '1'");
$id2 = $this->db->getLastId();
$this->db->query("INSERT INTO `" . DB_PREFIX . "category_description` SET `name` = '".$this->db->escape($key3)."', `language_id` = '".(int)$this->config->get('config_language_id')."', `category_id` = '".$this->db->escape($id2)."'");
$this->db->query("INSERT INTO `" . DB_PREFIX . "category_to_store` SET `category_id` = '".$this->db->escape($id2)."', `store_id` = '0'");
}
if(!empty($cat3)) {
foreach($cat3 as $key4=>$cat4) {
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category`, `" . DB_PREFIX . "category_description` WHERE `" . DB_PREFIX . "category`.`parent_id` = '".$this->db->escape($id3)."' AND `" . DB_PREFIX . "category_description`.`name` = '".$this->db->escape($key4)."' LIMIT 1");
if($qry->num_rows != 0) {
$id3 = $qry->row['category_id'];
}else{
$this->db->query("INSERT INTO `" . DB_PREFIX . "category` SET `parent_id` = '".$this->db->escape($id2)."', `status` = '1', `top` = '1'");
$id3 = $this->db->getLastId();
$this->db->query("INSERT INTO `" . DB_PREFIX . "category_description` SET `name` = '".$this->db->escape($key4)."', `language_id` = '".(int)$this->config->get('config_language_id')."', `category_id` = '".$this->db->escape($id3)."'");
$this->db->query("INSERT INTO `" . DB_PREFIX . "category_to_store` SET `category_id` = '".$this->db->escape($id3)."', `store_id` = '0'");
}
if(!empty($cat4)) {
foreach($cat4 as $key5=>$cat5) {
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category`, `" . DB_PREFIX . "category_description` WHERE `" . DB_PREFIX . "category`.`parent_id` = '".$this->db->escape($id4)."' AND `" . DB_PREFIX . "category_description`.`name` = '".$this->db->escape($key5)."' LIMIT 1");
if($qry->num_rows != 0) {
$id4 = $qry->row['category_id'];
}else{
$this->db->query("INSERT INTO `" . DB_PREFIX . "category` SET `parent_id` = '".$this->db->escape($id3)."', `status` = '1', `top` = '1'");
$id4 = $this->db->getLastId();
$this->db->query("INSERT INTO `" . DB_PREFIX . "category_description` SET `name` = '".$this->db->escape($key5)."', `language_id` = '".(int)$this->config->get('config_language_id')."', `category_id` = '".$this->db->escape($id4)."'");
$this->db->query("INSERT INTO `" . DB_PREFIX . "category_to_store` SET `category_id` = '".$this->db->escape($id4)."', `store_id` = '0'");
}
$catLink[$key1.':'.$key2.':'.$key3.':'.$key4.':'.$key5] = $id4;
}
}else{
$catLink[$key1.':'.$key2.':'.$key3.':'.$key4] = $id3;
}
}
}else{
$catLink[$key1.':'.$key2.':'.$key3] = $id2;
}
}
}else{
$catLink[$key1.':'.$key2] = $id1;
}
}
}
$this->openbay->ebay->log('Categories done');
} else {
$this->openbay->ebay->log('Categories set not to be created');
}
$imgCount = 0;
$current = $this->openbay->ebay->getLiveListingArray();
foreach($newData1 as $item) {
if(!in_array($item['ItemID'], $current)) {
$this->openbay->ebay->log('New item being created: '.$item['ItemID']);
//get the manufacturer id
$manufacturer_id = 0;
if(!empty($item['Brand'])) {
$manufacturer_id = $this->manufacturerExists($item['Brand']);
}
//get the length class id
$length_class_id = 1;
if (isset($item['advanced']['package']['size']['width_unit']) && !empty($item['advanced']['package']['size']['width_unit'])) {
$length_class_id = $this->lengthClassExists($item['advanced']['package']['size']['width_unit']);
}
//get the weight class id
$weight_class_id = 1;
if (isset($item['advanced']['package']['weight']['major_unit']) && !empty($item['advanced']['package']['weight']['major_unit'])) {
$weight_class_id = $this->weightClassExists($item['advanced']['package']['weight']['major_unit']);
}
$tax = $this->config->get('tax');
$net_price = $item['priceGross'] / (($tax / 100) + 1);
//openstock variant check
$osSql = '';
if(!empty($item['variation']) && $openstock == true) {
$osSql = "`has_option` = '1',";
}
//package weight
if (isset($item['advanced']['package']['weight']['major'])) {
$weight = $item['advanced']['package']['weight']['major'].'.'.$item['advanced']['package']['weight']['minor'];
} else {
$weight = 0;
}
//package length
if (isset($item['advanced']['package']['size']['length'])) {
$length = $item['advanced']['package']['size']['length'];;
} else {
$length = 0;
}
//package width
if (isset($item['advanced']['package']['size']['width'])) {
$width = $item['advanced']['package']['size']['width'];;
} else {
$width = 0;
}
//package height
if (isset($item['advanced']['package']['size']['height'])) {
$height = $item['advanced']['package']['size']['height'];;
} else {
$height = 0;
}
$this->db->query("
INSERT INTO `" . DB_PREFIX . "product` SET
`quantity` = '".$this->db->escape($item['Quantity'])."',
`manufacturer_id` = '".$this->db->escape($manufacturer_id)."',
`stock_status_id` = '6',
`price` = '".$this->db->escape($net_price)."',
`tax_class_id` = '9',
`location` = '".$this->db->escape(isset($item['note']) ? $item['note'] : '')."',
`mpn` = '".$this->db->escape(isset($item['advanced']['brand']['mpn']) ? $item['advanced']['brand']['mpn'] : '')."',
`sku` = '".$this->db->escape(isset($item['SKU']) ? $item['SKU'] : '')."',
`model` = '".$this->db->escape(isset($item['SKU']) ? $item['SKU'] : '')."',
`isbn` = '".$this->db->escape(isset($item['advanced']['isbn']) ? $item['advanced']['isbn'] : '')."',
`ean` = '".$this->db->escape(isset($item['advanced']['ean']) ? $item['advanced']['ean'] : '')."',
`upc` = '".$this->db->escape(isset($item['advanced']['upc']) ? $item['advanced']['upc'] : '')."',
`weight` = '".(double)$weight."',
`weight_class_id` = '".$this->db->escape($weight_class_id)."',
`length` = '".(double)$length."',
`width` = '".(double)$width."',
`height` = '".(double)$height."',
`length_class_id` = '".$this->db->escape($length_class_id)."',
`subtract` = '1',
`minimum` = '1',
`status` = '1',
".$osSql."
`date_available` = 'now()',
`date_added` = 'now()',
`date_modified` = 'now()'
");
$product_id = $this->db->getLastId();
$this->openbay->ebay->log('Product insert done');
//Insert product description
$originalDescription = $item['Description'];
if(!empty($originalDescription)) {
if ( false !== ($item['Description'] = gzuncompress($originalDescription))) {
$item['Description'] = html_entity_decode($item['Description']);
}else{
$this->openbay->ebay->log('Description could not be decompressed, output below');
$this->openbay->ebay->log($originalDescription);
$item['Description'] = '';
}
}
$sql = " INSERT INTO `" . DB_PREFIX . "product_description` SET
`product_id` = '".(int)$product_id."',
`language_id` = '".(int)$this->config->get('config_language_id')."',
`name` = '".$this->db->escape(htmlspecialchars(base64_decode($item['Title']), ENT_COMPAT, 'UTF-8'))."',
`description` = '".$this->db->escape(htmlspecialchars(utf8_encode($item['Description']), ENT_COMPAT, 'UTF-8'))."'";
$this->db->query($sql);
$this->openbay->ebay->log('Product description done');
//Insert product store link
$this->db->query("INSERT INTO `" . DB_PREFIX . "product_to_store` SET `product_id` = '".(int)$product_id."', `store_id` = '0'");
$this->openbay->ebay->log('Store link done');
//Create any attributes from eBay for the item
if(!empty($item['specs'])) {
//check the main group exists, if not create
$groupId = $this->attributeGroupExists(base64_decode($item['CategoryNameSingle']));
foreach($item['specs'] as $spec) {
//check if the attribute exists in the group, if not create
$attrId = $this->attributeExists($groupId, base64_decode($spec['name']));
//insert the attribute value into the product attribute table
$this->attributeAdd($product_id, $attrId, base64_decode($spec['value']));
}
}
//Create the product variants for OpenStock
$variant = 0;
if(!empty($item['variation'])) {
$variant = 1;
if($openstock == true) {
$this->openbay->ebay->log('OpenStock Loaded');
$this->createVariants($product_id, $item);
}
$this->openbay->ebay->log('Variants done');
}
//insert store to eBay item link
$this->openbay->ebay->createLink($product_id, $item['ItemID'], $variant);
//Insert product/category link
if ($options['cat'] == 1) {
$this->createCategoryLink($product_id, $catLink[$item['CategoryName']]);
}
//images
$imgCount = 0;
if(is_array($item['pictures'])) {
foreach($item['pictures'] as $img) {
if(!empty($img)) {
$name = rand(500000, 1000000000);
$this->addImage($img, DIR_IMAGE.'data/'.$name.'.jpg', $name.'.jpg', $product_id, $imgCount);
$imgCount++;
}
}
}
$this->openbay->ebay->log('Product import completed.');
}else{
$this->openbay->ebay->log($item['ItemID'].' exists already');
}
}
$this->openbay->ebay->log('Product data import done');
$this->openbay->ebay->getImages();