<?php 
/****************************************************************/
/*          e-mo.pl © 2017                                      */
/*          http://www.e-mo.com.pl                              */
/*          e-mail: info@e-mo.pl                                */
/*          produkt: Action2osCommerce                          */
/*          wersja: 1.9.5                                       */
/*          webserwis: oscGold                                  */
/*          sciezka: https://www.e-mo.com.pl/plikit,1606,51cc3f3a4b10c.php*/
/*          data: 2017-09-07                                    */
/*          godz: 13:19:55                                      */
/****************************************************************/


$doc = new DOMDocument();
$doc->loadXML(stripslashes($_POST['rozmiary']));


$xpath = new DOMXPath($doc);
$query = '//root/produkty/produkt';

$entries = $xpath->query($query);


$insertCecha = false;


foreach ($entries as $entry2)
{

	$products_id = 0;
	echo $query = "SELECT products_id  FROM products Where products_model='".$entry2->getAttribute('Id')."' and emo_source='".$entry2->getAttribute('emo_source')."' ";
	$result = @mysql_query($query,$Conn);			
	while ($row = @mysql_fetch_array  ($result)) 
	{
		$products_id =$row['products_id'];
	}

	echo '<produkt id="'.$products_id.'">';
	$xpath2 = new DOMXPath($doc);
	$query2 = '//root/produkty/produkt[@Id=\''.$entry2->getAttribute('Id').'\' and @emo_source=\''.$entry2->getAttribute('emo_source').'\']/rozmiary/rozmiar';

	$entries2 = $xpath2->query($query2);

	$query = "Delete from products_attributes Where products_id='$products_id'";
	$result = @mysql_query($query,$Conn);
	
	$query = "Delete from products_stock Where products_id='$products_id'";
	$result = @mysql_query($query,$Conn);
	
	echo '<cechy>';
	
	$insertCecha = false;

	foreach ($entries2 as $entry)
	{
		
			$rozmiar_id=0;
			$kolor_id = 0;
			$rozmiar_value_id = 0;
			$kolor_value_id = 0;
			$NazwaCecha = $entry->getAttribute('nazwa2');
			$NazwaCecha2 = $entry->getAttribute('nazwa');
			$kolor_value=$entry->getAttribute('kolor');
			$rozmiar_value = $entry->getAttribute('rozmiar');

			if(empty($NazwaCecha))
			{
				$NazwaCecha ='Rozmiar';
			}
			
			if(empty($NazwaCecha2))
			{
				$NazwaCecha2 = 'Kolor';
			}
			
								
			$insertCecha = true;
			$query = "SELECT products_options_id  FROM products_options Where language_id='$language_id' and products_options_name='$NazwaCecha'";
			$result = @mysql_query($query,$Conn);			
			$rozmiar_id=0;
			while ($row = @mysql_fetch_array  ($result)) 
			{
				$rozmiar_id =$row['products_options_id'];
			}

			if($rozmiar_id==0 && !empty($NazwaCecha) && $rozmiar_value!='-brak opcji-')
			{
				$query = "SELECT case when MAX(products_options_id) is not null then MAX(products_options_id)+1 else 1 end as Max FROM products_options";
				$result = @mysql_query($query,$Conn);	
				while ($row = @mysql_fetch_array  ($result)) 
				{
					$rozmiar_id =$row['Max'];
				}

				$query = "Insert INTO products_options (products_options_id,language_id,products_options_name) VALUES( $rozmiar_id, $language_id, '$NazwaCecha')";
				$result = @mysql_query($query,$Conn);
			}


			$query = "SELECT products_options_id  FROM products_options Where language_id='$language_id' and products_options_name='$NazwaCecha2'";
			$result = @mysql_query($query,$Conn);			
			$kolor_id=0;
			while ($row = @mysql_fetch_array  ($result)) 
			{
				$kolor_id =$row['products_options_id'];
			}

			if($kolor_id==0 && !empty($NazwaCecha2) && $kolor_value!='-brak opcji-')
			{
				$query = "SELECT case when MAX(products_options_id) is not null then MAX(products_options_id)+1 else 1 end as Max FROM products_options";
				$result = @mysql_query($query,$Conn);	
				while ($row = @mysql_fetch_array  ($result)) 
				{
					$kolor_id =$row['Max'];
				}

				$query = "Insert INTO products_options (products_options_id,language_id,products_options_name) VALUES( $kolor_id , $language_id, '$NazwaCecha2')";
				$result = @mysql_query($query,$Conn);
			}
		
		
		
		
		echo '<cecha  kolor="'.$kolor_value.'" rozmiar="'.$entry->getAttribute('rozmiar').'" />';
		
		
		$ilosc = $entry->getAttribute('ilosc');
		
		$kolor_value_id=0;
		if(!empty($kolor_value) && $kolor_value!='-brak opcji-')
		{
			$query = "SELECT products_options_values_id  FROM products_options_values Where language_id='$language_id' and  products_options_values_name='$kolor_value'";
			$result = @mysql_query($query,$Conn);			
			
			
			while ($row = @mysql_fetch_array  ($result)) 
			{
				$kolor_value_id =$row['products_options_values_id'];
			}

			if($kolor_value_id==0)
			{
				$query = "SELECT case when MAX(products_options_values_id) is not null then MAX(products_options_values_id)+1 else 1 end as Max FROM products_options_values";
				
				$result = @mysql_query($query,$Conn);	
				while ($row = @mysql_fetch_array  ($result)) 
				{
					$kolor_value_id =$row['Max'];
				}

				$query = "Insert INTO products_options_values (products_options_values_id,language_id,products_options_values_name) VALUES( $kolor_value_id , $language_id, '$kolor_value')";
				$result = @mysql_query($query,$Conn);
			}
		}
		
		$rozmiar_value_id=0;

		if(!empty($rozmiar_value) && $rozmiar_value!='-brak opcji-')
		{
			$query = "SELECT products_options_values_id  FROM products_options_values Where language_id='$language_id' and  products_options_values_name='$rozmiar_value'";
			$result = @mysql_query($query,$Conn);			
			$rozmiar_value_id=0;
			
			while ($row = @mysql_fetch_array  ($result)) 
			{
				$rozmiar_value_id =$row['products_options_values_id'];
			}

			if($rozmiar_value_id==0)
			{
				$query = "SELECT case when MAX(products_options_values_id) is not null then MAX(products_options_values_id)+1 else 1 end as Max FROM products_options_values";
				
				$result = @mysql_query($query,$Conn);	
				while ($row = @mysql_fetch_array  ($result)) 
				{
					$rozmiar_value_id =$row['Max'];
				}

				$query = "Insert INTO products_options_values (products_options_values_id,language_id,products_options_values_name) VALUES( $rozmiar_value_id , $language_id, '$rozmiar_value')";
				$result = @mysql_query($query,$Conn);
			}
		}

		$query = "SELECT products_options_values_to_products_options_id FROM products_options_values_to_products_options Where products_options_id=$kolor_id and products_options_values_id=$kolor_value_id";
		$result = @mysql_query($query,$Conn);			
		$kolor_relacja_id=0;
		
		while ($row = @mysql_fetch_array  ($result)) 
		{
			$kolor_relacja_id =$row['products_options_values_to_products_options_id'];
		}

		if($kolor_relacja_id==0 && $kolor_value_id>0)
		{
			$query = "Insert INTO products_options_values_to_products_options (products_options_id,products_options_values_id) VALUES( $kolor_id , $kolor_value_id)";
			$result = @mysql_query($query,$Conn);
			
		}


		$query = "SELECT products_options_values_to_products_options_id FROM products_options_values_to_products_options Where products_options_id=$rozmiar_id and products_options_values_id=$rozmiar_value_id";
		$result = @mysql_query($query,$Conn);			
		$rozmiar_relacja_id=0;
		
		while ($row = @mysql_fetch_array  ($result)) 
		{
			$rozmiar_relacja_id =$row['products_options_values_to_products_options_id'];
		}

		if($rozmiar_relacja_id==0 && $rozmiar_value_id>0)
		{
			$query = "Insert INTO products_options_values_to_products_options (products_options_id,products_options_values_id) VALUES( $rozmiar_id,$rozmiar_value_id)";
			$result = @mysql_query($query,$Conn);
			
		}


		$query = "SELECT products_attributes_id FROM products_attributes Where  products_id='$products_id' and  options_id='$kolor_id' and options_values_id='$kolor_value_id'";
		$result = @mysql_query($query,$Conn);			
		$attributes_id=0;
		echo mysql_error();
		while ($row = @mysql_fetch_array  ($result)) 
		{
			$attributes_id =$row['products_attributes_id'];
		}
		
		if($attributes_id==0 &&  $kolor_value_id>0)
		{		
			$query = "Insert INTO products_attributes (products_id, options_id, options_values_id, options_values_price, price_prefix) VALUES($products_id, $kolor_id, $kolor_value_id, '0', '+')";
			$result = @mysql_query($query,$Conn);
			$attributes_id=mysql_insert_id();
			echo mysql_error();
		}

		
		$query = "SELECT products_attributes_id FROM products_attributes Where  products_id='$products_id' and  options_id='$rozmiar_id' and options_values_id='$rozmiar_value_id'";
		$result = @mysql_query($query,$Conn);			
		$attributes_id=0;
		echo mysql_error();
		while ($row = @mysql_fetch_array  ($result)) 
		{
			$attributes_id =$row['products_attributes_id'];
		}
		
		if($attributes_id==0 && $rozmiar_value_id>0)
		{		
			$query = "Insert INTO products_attributes (products_id, options_id, options_values_id, options_values_price, price_prefix) VALUES($products_id, $rozmiar_id, $rozmiar_value_id, '0', '+')";
			$result = @mysql_query($query,$Conn);
			$attributes_id=mysql_insert_id();
			echo mysql_error();
		}	
		
		if($rozmiar_id > 0 && $kolor_id > 0 && $rozmiar_id < $kolor_id && $kolor_value_id > 0 && $rozmiar_value_id > 0 )
			$attrybuty = sprintf("%s-%s,%s-%s", $rozmiar_id,$rozmiar_value_id, $kolor_id, $kolor_value_id);
		elseif ($rozmiar_id > 0 &&  $kolor_id > 0 && $rozmiar_id >  $kolor_id && $kolor_value_id > 0 && $rozmiar_value_id > 0)
			$attrybuty = sprintf("%s-%s,%s-%s",  $kolor_id, $kolor_value_id, $rozmiar_id,$rozmiar_value_id);
		elseif ($rozmiar_id > 0 && $kolor_value_id==0)
			$attrybuty =sprintf("%s-%s", $rozmiar_id, $rozmiar_value_id);
		elseif ( $kolor_id > 0 && $rozmiar_value_id==0)
			$attrybuty =sprintf("%s-%s",  $kolor_id , $kolor_value_id);
		
		$query = "SELECT products_stock_id FROM products_stock Where products_id='$products_id' and  products_stock_attributes='$attrybuty'";
		$result = @mysql_query($query,$Conn);			
		$products_stock_id=0;
		echo mysql_error();
		while ($row = @mysql_fetch_array  ($result)) 
		{
			$products_stock_id =$row['products_stock_id'];
		}
		
		
		if($products_stock_id==0)
		{
			$query = "INSERT INTO  `products_stock` (  `products_stock_id` ,  `products_id` ,  `products_stock_attributes` ,  `products_stock_quantity` ) VALUES (NULL , $products_id,  '$attrybuty',  '$ilosc')";
			$result = @mysql_query($query,$Conn);
			echo mysql_error();
		}
		else
		{
			$query = "Udpate `products_stock` set `products_stock_quantity`='$ilosc' Where products_stock_id='$products_stock_id'";
			$result = @mysql_query($query,$Conn);
			echo mysql_error();
		}
		
		$query = "Update `products` set `products_quantity`=(SELECT SUM(a.`products_stock_quantity`) FROM `products_stock` a WHERE a.`products_id`=$products_id), products_status=CASE WHEN(SELECT SUM(b.`products_stock_quantity`) FROM `products_stock` b WHERE b.`products_id`=$products_id)>0 THEN 1 ELSE 0 END Where products_id=$products_id";
		$result = @mysql_query($query,$Conn);
		echo mysql_error();	

	}
	echo '</cechy>';
	echo '</produkt>';
}




?>
