<?php
	require_once('config.php');
	
	$database = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
	if($database)
	{
		mysqli_select_db($database, DB_DATABASE) or die ("DB: " . DB_DATABASE . " NOT FOUND");
	}
	else
	{
		print 'db connection error';
		error_log('db connection error');
		exit;
	}

	$products = db_get_array("SELECT p.product_id, p.model, p.image as main_image, p.quantity, p.price, p.stock_status_id, pd.name as product, pd.description, pm.name as manufacturer FROM hk_product p
							 LEFT JOIN hk_product_description pd ON pd.product_id = p.product_id AND pd.language_id = '2'
							 LEFT JOIN hk_manufacturer pm ON pm.manufacturer_id = p.manufacturer_id
							 WHERE p.status = '1' GROUP BY p.product_id");
	
	$xml = new SimpleXMLExtended('<xml/>');
	$xmlproducts = $xml->addChild('Products');
	
	foreach($products as $product)
	{
		$xmlproduct = $xmlproducts->addChild('Product');
		$product_id = $product['product_id'];
		
		$special_price = db_get_field("SELECT price FROM hk_product_special WHERE product_id = '$product_id' AND customer_group_id='1' AND (date_end = '0000-00-00' OR date_end > '".date('Y-m-d')."')");
		if(!empty($special_price))
		{
			$product['price'] = $special_price;
		}
		
		$main_category_id = db_get_field("SELECT category_id FROM hk_product_to_category WHERE product_id = '$product_id' AND category_id IN (SELECT category_id FROM hk_category_path ORDER BY level DESC)");
		$category_path = db_get_array("SELECT path_id, level FROM hk_category_path WHERE category_id = '$main_category_id' ORDER BY level ASC");
		$product['category'] = array();
		
		foreach($category_path as $category)
		{
			$category_id = $category['path_id'];
			$product['category'][] = db_get_field("SELECT name FROM hk_category_description WHERE category_id = '$category_id' AND language_id = '2'");
		}
		
		$product['category'] = implode(">", array_filter($product['category']));
		$product['url'] = db_get_field("SELECT keyword FROM hk_url_alias WHERE query = 'product_id=".$product_id."'");
		$delivery_status = db_get_field("SELECT name FROM hk_stock_status WHERE stock_status_id = '$product[stock_status_id]' AND language_id = '2'");
		
		$delivery_time = '1 zile';
		if(strpos($delivery_status, '35 zile') !== false)
		{
			$delivery_time = '35 zile';
		}
		
		if(strpos($delivery_status, '90 zile') !== false)
		{
			$delivery_time = '90 zile';
		}
		
		
		$xmlproduct->addChild('Identifier', $product['model']);
		$xmlproduct->addChild('Manufacturer', $product['manufacturer']);
		$xmlproduct->addChild('Name', $product['product']);
		$xmlproduct->addChild('Product_url', 'https://genarom.ro/'.$product['url']);
		$xmlproduct->addChild('Price', $product['price']);
		$xmlproduct->addChild('Image_url', 'https://genarom.ro/image/'.str_replace(' ', '%20', $product['main_image']));
		$xmlproduct->addChild('Category', $product['category']);
		
		$xmlproduct->addChild('Description', str_replace('&nbsp;', '', strip_tags(htmlspecialchars_decode($product['description']))));
		
		$xmlproduct->addChild('Delivery_Time', $delivery_time);
		$xmlproduct->addChild('Delivery_Cost', fn_calculate_shipping($product['price']));
		
		$attributes = db_get_array("SELECT attribute_id, text FROM hk_product_attribute WHERE product_id = '$product_id' AND language_id = '2'");
		if(!empty($attributes))
		{
			$xmlattribute = $xmlproduct->addChild('Attributes');
			foreach($attributes as $attribute)
			{
				$attribute_name = db_get_field("SELECT name FROM hk_attribute_description WHERE attribute_id = '$attribute[attribute_id]' AND language_id = '2'");
				$prod_attr = $xmlattribute->addChild('Attribute');
				
				$prod_attr->addChild('Attribute_name', $attribute_name);
				$prod_attr->addChild('Attribute_value', $attribute['text']);
			}
		}
	}
	
	$xml->asXML('feeds/compari.xml');
	
	class SimpleXMLExtended extends SimpleXMLElement {
	  public function addCData($cdata_text) {
		$node = dom_import_simplexml($this); 
		$no   = $node->ownerDocument; 
		$node->appendChild($no->createCDATASection($cdata_text)); 
	  } 
	}
	
	function fn_calculate_shipping($price)
	{
		if($price > 0 && $price < 500)
		{
			 return 50; 
		}
		if($price > 501 && $price < 1000)
		{
			 return 100; 
		}
		if($price > 1001 && $price < 3000)
		{
			 return 150; 
		}
		if($price > 3001 && $price < 3500)
		{
			 return 200; 
		}
		if($price > 3501 && $price < 4000)
		{
			 return 250; 
		}
		if($price > 4001 && $price < 4500)
		{
			 return 300; 
		}
		if($price > 4501 && $price < 5000)
		{
			 return 350; 
		}
		if($price > 5001 && $price < 6000)
		{
			 return 450; 
		}
		if($price > 6001 && $price < 7000)
		{
			 return 550; 
		}
		if($price > 7001 && $price < 8000)
		{
			 return 650; 
		}
		if($price > 8001 && $price < 9000)
		{
			 return 750; 
		}
		if($price > 9001 && $price < 10000)
		{
			 return 850; 
		}
		if($price > 10001 && $price < 11000)
		{
			 return 950; 
		}
		if($price > 11001 && $price < 12000)
		{
			 return 1050; 
		}
		if($price > 12001 && $price < 13000)
		{
			 return 1150; 
		}
		if($price > 13001 && $price < 14000)
		{
			 return 1250; 
		}
		if($price > 14001 && $price < 15000)
		{
			 return 1350; 
		}
		if($price > 15001 && $price < 16000)
		{
			 return 1450; 
		}
		if($price > 16001 && $price < 17000)
		{
			 return 1550; 
		}
		if($price > 17001 && $price < 18000)
		{
			 return 1650; 
		}
		if($price > 18001 && $price < 19000)
		{
			 return 1750; 
		}
		if($price > 19001 && $price < 20000)
		{
			 return 1850; 
		}
		if($price > 20001 && $price < 30000)
		{
			 return 2000; 
		}
		if($price > 30001 && $price < 40000)
		{
			 return 2300; 
		}
		if($price > 40001 && $price < 50000)
		{
			 return 2600;
		}
	}
	
	
	
	function db_query($query, $repair = 0)
	{
		global $database;
		mysqli_set_charset($database, "utf8");
		$queryResult = mysqli_query($database, $query);
		
		if($queryResult)
		{
			if(strpos(strtolower($query), "insert into") !== false)
			{
				return mysqli_insert_id($database);
			}
			else
			{
				return $queryResult;
			}
		}
		else
		{
			$queryError = mysqli_error($database);
			print $queryError;
			print '<blockquote>'.$qery.'</blockquote>';
			error_log($queryError);
			if(preg_match("/'(\S+)\.(MYI|MYD)/", $queryError, $matchError) && $repair == 0)
			{
				//db_query("REPAIR TABLE $matchError[1]");
				//db_query($query);
			}
			else
			{
				return false;
			}
		}
	}
	
	function db_get_array($query)
	{
		$result = db_query($query);
		$array = array();
		
		if($result)
		{
			while($row = mysqli_fetch_assoc($result))
			{
				$array[] = $row;
			}
		}
		
		return $array;
	}
	
	function db_get_field($query)
	{
		$result = db_query($query);
		$field = '';
		
		if($result)
		{
			$result = mysqli_fetch_assoc($result);
			$field = @reset($result);
		} 
		
		return $field;
	}
	
	function db_get_fields($query)
	{
		$result = db_query($query ." LIMIT 0,10");
		return mysqli_fetch_assoc($result);
	}
	
?>