<?php 
/****************************************************************/
/*          e-mo.pl © 2017                                      */
/*          http://www.e-mo.com.pl                              */
/*          e-mail: info@e-mo.pl                                */
/*          produkt: Action2osCommerce                          */
/*          wersja: 1.9.7                                       */
/*          webserwis: oscGold                                  */
/*          sciezka: https://www.e-mo.com.pl/plikit,1918,533545b244d7d.php*/
/*          data: 2017-09-25                                    */
/*          godz: 10:45:19                                      */
/****************************************************************/


@include_once('source/orders.php');
@include_once('source/orders_products.php');
@include_once('source/orders_status_history.php');
@include_once('source/dok_cechy.php');

if( isset($_POST['action']) && $_POST['action']=='status')
{
	$zmienStatus = isset($_POST['bezStatusu']) && $_POST['bezStatusu'] == 1 ? false : true;
	$obj = new orders();
	$obj->Conn = $db->Conn;
		
	$obj->dok_KluczObcy= $_POST['dok_KluczObcy'];
	$obj->orders_status = $_POST['dok_Status'];
	$obj->orders_id =  $_POST['dok_Id'];
	$obj->bezStatusu =  (int)$_POST['bezStatusu'];
        

	if($zmienStatus && $obj->ZmienStatus())
	{
		if((int)$_POST['bezStatusu']==0)
		{
			$status = new orders_status_history();
			$status->Conn = $db->Conn;
			
			
			$status->orders_id=$obj->orders_id;
			$status->orders_status_id=$_POST['dok_Status'];
			$status->date_added=date("Y-m-d H:i:s");
			$status->customer_notified=0;
			$status->comments=$_POST['komunikat'];
			$status->SK=md5($_POST['komunikat']);
			$status->orders_status_history_id=$status->GetId();

			if($status->orders_status_history_id==0 && $status->Dodaj())
			{
				echo '<error>0</error>';
				echo '<msg>OK</msg>';
			}
			elseif($status->orders_status_history_id>0)
			{
				echo '<error>0</error>';
				echo '<msg>JEST</msg>';
			}
			else
			{
				echo '<error>1</error>';
				echo '<msg>'.$status->errMsg[2].'</msg>';	
			}
		}
		else
		{
			echo '<error>1</error>';
			echo '<msg>'.$status->errMsg[2].'</msg>';	
		}


	}
	else if (!$zmienStatus && $obj->DodajKluczObcy())
	{
		echo '<error>0</error>';
		echo '<msg>OK</msg>';
	}
	else
	{
		echo '<error>1</error>';
		echo '<msg>'.$obj->errMsg[2].'</msg>';		
	}
}
elseif( isset($_POST['action']) && $_POST['action']=='status2')
{
	$zmienStatus = isset($_POST['bezStatusu']) && $_POST['bezStatusu'] == 1 ? false : true;
	$obj = new orders();
	$obj->Conn = $db->Conn;
		
	$obj->dok_KluczObcy= $_POST['dok_KluczObcy'];
	$obj->orders_status = $_POST['dok_Status'];

	if($zmienStatus && $obj->ZmienStatus2())
	{
		$status = new orders_status_history();
		$status->Conn = $db->Conn;
		
		
		$status->orders_id=orders::GetIdObce($_POST['dok_KluczObcy'], $db->Conn);
		$status->orders_status_id=$_POST['dok_Status'];
		$status->date_added=date("Y-m-d H:i:s");
		$status->customer_notified=0;
		$status->comments=$_POST['komunikat'];
		$status->SK=md5($_POST['komunikat']);
		$status->orders_status_history_id=$status->GetId();

		if($status->orders_status_history_id==0 && $status->Dodaj())
		{
			echo '<error>0</error>';
			echo '<msg>OK</msg>';
		}
		elseif($status->orders_status_history_id>0)
		{
			echo '<error>0</error>';
			echo '<msg>JEST</msg>';
		}
		else
		{
			echo '<error>1</error>';
			echo '<msg>'.$status->errMsg[2].'</msg>';	
		}

	}
	else if (!$zmienStatus && $obj->DodajKluczObcy())
	{
		echo '<error>0</error>';
		echo '<msg>OK</msg>';
	}
	else
	{
		echo '<error>1</error>';
		echo '<msg>'.$obj->errMsg[2].'</msg>';		
	}
}
elseif( isset($_POST['action']) && $_POST['action']=='flagi')
{
	$zmienStatus = isset($_POST['bezStatusu']) && $_POST['bezStatusu'] == 1 ? false : true;
	$obj = new orders();
	$obj->Conn = $db->Conn;
		
	$obj->dok_KluczObcy= $_POST['dok_KluczObcy'];
	$obj->orders_status = $_POST['dok_Status'];

	if($zmienStatus && $obj->ZmienStatus2())
	{
		$status = new orders_status_history();
		$status->Conn = $db->Conn;
		
		
		$status->orders_id=orders::GetIdObce($_POST['dok_KluczObcy'], $db->Conn);
		$status->orders_status_id=$_POST['dok_Status'];
		$status->date_added=date("Y-m-d H:i:s");
		$status->customer_notified=0;
		$status->comments=$_POST['komunikat'];
		$status->SK=md5($_POST['aktualizacja']);
		$status->orders_status_history_id=$status->GetId();

		if($status->orders_status_history_id==0 && $status->Dodaj())
		{
			echo '<error>0</error>';
			echo '<msg>OK</msg>';
		}
		elseif($status->orders_status_history_id>0)
		{
			echo '<error>0</error>';
			echo '<msg>JEST</msg>';
		}
		else
		{
			echo '<error>1</error>';
			echo '<msg>'.$status->errMsg[2].'</msg>';	
		}

	}
	else if (!$zmienStatus && $obj->DodajKluczObcy())
	{
		echo '<error>0</error>';
		echo '<msg>OK</msg>';
	}
	else
	{
		echo '<error>1</error>';
		echo '<msg>'.$obj->errMsg[2].'</msg>';		
	}
}
else
{
	
	$obj = new orders();
	$obj->Conn = $db->Conn;
	
	if(isset($_POST['data']) && $_POST['data']!="0")
	{
		$zam=$obj->Lista2($_POST['data']);
		
	}
	else
	{
		$zam=$obj->Lista();
	
	}

	echo "<webserwis>osCommerce</webserwis>";
	echo "<dokumenty>\r\n";
    
	for($i=0;$i<count($zam);$i++)
	{
		echo "<dokument>\r\n";
		foreach ($zam[$i] as $k => $v)
		{
			echo "<$k>$v</$k>\r\n";
		}
		
		echo "<user>\r\n";
				
		$usr = $obj->ListaUser($zam[$i]['dok_Id']);
		foreach ($usr as $kk => $vv)
		{				
			echo "<$kk>$vv</$kk>\r\n";			
		}
		
		echo "</user>\r\n";
		echo "<adresy>\r\n";
		

		$adresy = $obj->ListaAdresy((int)$zam[$i]['dok_Id']);
		
		foreach ($adresy as $k => $v)
		{
			echo "<adres typ=\"".$v['dka_Typ']."\">\r\n";
				foreach($v as $y => $z)
				{
					echo "<$y>$z</$y>\r\n";
				}
			echo "</adres>\r\n";
		}	
		echo "</adresy>\r\n";
		echo "<linie>\r\n";
		$obj3 = new orders_products();
		
		$obj3->Conn = $db->Conn;
		$objC = new dok_cechy();
		$objC->Conn = $db->Conn;

		$linie = $obj3->Lista((int)$zam[$i]['dok_Id']);
		
		foreach ($linie as $k => $v)
		{
			echo "<linia>\r\n";
			$_linia=0;
				foreach($v as $y => $z)
				{
					if($y == 'dl_Id')
						$_linia=$z;
					echo "<$y>$z</$y>\r\n";
				}

				$cechy = $objC->Linia($_linia);
			
				echo "<cechy>";
				
				foreach($cechy as $y1)
				{
					echo "<cecha ";
					foreach($y1 as $y2 => $z2)
					{
						echo "$y2=\"".htmlspecialchars($z2)."\" ";
					}
					echo "/>";
				}
				echo "</cechy>";
			echo "</linia>\r\n";
		}	

		echo "</linie>\r\n";
		echo "</dokument>\r\n";
	}

	echo "</dokumenty>\r\n";
}

?>
