<?php
ini_set ( 'display_errors', 1 );
/*
 * Wgrywanie i czytanie plików XML do obiektu Autor: Dominik 'DranZi' Brzóska
 */

define ( "FILE_DIR", "/home/saas/invoice/" ); // ścieżka do katalogu z xml'kami
class readXML {
	private $db;
	private $file_name; // nazwa wgranego pliku
	private $content; // zaladowany plik xml
	private $nodes; // załadowane elementy
	public $parent_iln;
	public $parent_nip;
	public $parent_id;
	public $parent_name;
	public $ecmpaymentcondition_id;
	public $ecmpaymentcondition_name;
	public $supplier_code;
	public $register_date;
	public $delivery_date;
	public $assigned_user_id;
	public $parent_document_no;
	public $shipping_addresses;
	public $parent_address_street;
	public $parent_address_country;
	public $parent_address_postalcode;
	public $parent_address_city;
	public $shipping_address_name;
	public $shipping_address_street;
	public $shipping_address_country;
	public $shipping_address_postalcode;
	public $shipping_address_city;
	public $shipping_nip;
	public $shipping_iln;
	public $position_list;
	private $c;
	/*
	 * Wgrywa plik na serwer
	 */
	function UploadXMLFile() {
		$allowedExts = array (
				"xml" 
		);
		$temp = explode ( ".", $_FILES ["file"] ["name"] );
		$extension = end ( $temp );
		$this->file_name = date ( "Y_d_m" ) . str_replace ( " ", "", str_replace ( "_", "", str_replace ( ".", "", microtime () ) ) ) . '.' . $extension;
		
		if ($_FILES ["file"] ["error"] > 0) {
			return '0';
		} else {
			if (file_exists ( FILE_DIR . $_FILES ["file"] ["name"] )) {
				return '0';
			} else {
				move_uploaded_file ( $_FILES ["file"] ["tmp_name"], FILE_DIR . $this->file_name );
				return $this->file_name;
			}
		}
	}
	
	//add mz 2014-10-15
	//Pobiera jedynie nazwę składającego zamówienie
	function loadAccount($file) {
		$this->db = $GLOBALS ['db'];
		if ($file == '')
			$file = $this->file_name;
		else
			$this->file_name = $file;
		$this->content = file_get_contents ( FILE_DIR . $this->file_name );
		$this->nodes = new SimpleXMLElement ( $this->content );
		
		$result = $this->db->fetchByAssoc ( $this->db->query ( "select name from accounts where iln like '" . $this->nodes->{'Order-Parties'}->Buyer->ILN . "'" ) );
		
		return $result['name'];
	}

	
	/*
	 * Otwiera plik XML :)
	 */
	function loadXMLFile($file) {
		$this->db = $GLOBALS ['db'];
		if ($file == '')
			$file = $this->file_name;
		else
			$this->file_name = $file;
		$this->content = file_get_contents ( FILE_DIR . $this->file_name );
		$this->nodes = new SimpleXMLElement ( $this->content );
		
		// pobierz dane z klienta
		$result = $this->db->fetchByAssoc ( $this->db->query ( "select to_vatid,register_address_street,register_address_city,register_address_postalcode,register_address_country,id,assigned_user_id,name,parent_id,supplier_code,ecmpaymentcondition_id,ecmpaymentcondition_name,payment_date_days from accounts where iln like '" . $this->nodes->{'Order-Parties'}->Buyer->ILN . "'" ) );

		$this->parent_id = $result ['id'];
		$this->parent_name = $result ['name'];
		$this->parent_iln = $this->nodes->{'Order-Parties'}->Buyer->ILN;
		$this->parent_nip = $result ['to_vatid'];
		$this->parent_address_street = $result ['register_address_street'];
		$this->parent_address_city = $result ['register_address_city'];
		$this->parent_address_postalcode = $result ['register_address_postalcode'];
		$this->parent_address_country = $result ['register_address_country'];
		$this->parent_document_no = $this->nodes->{'Order-Header'}->OrderNumber;
		// adres dostawy
		// pobierz dane z klienta
		$result3 = $this->db->fetchByAssoc ( $this->db->query ( "
SELECT 
    a.to_vatid,
    dr.street as register_address_street,
    dr.city as register_address_city,
    dr.postalcode as register_address_postalcode,
    dr.country as register_address_country,
    a.id,
    a.assigned_user_id,
    a.name,
    a.parent_id,
    a.supplier_code,
    a.ecmpaymentcondition_id,
    a.ecmpaymentcondition_name,
    a.payment_date_days
FROM
    accounts a
inner join account_addresses dr on dr.account_id=a.id
WHERE
    a.iln LIKE '" . $this->nodes->{'Order-Parties'}->DeliveryPoint->ILN . "' and a.deleted=0 and dr.deleted=0 order by dr. position asc" ) );
		$this->shipping_address_name = $result3 ['name'];
		$this->shipping_addresses = $result3 ['name'];
		$this->shipping_address_street = $result3 ['register_address_street'];
		$this->shipping_address_city = $result3 ['register_address_city'];
		$this->shipping_address_postalcode = $result3 ['register_address_postalcode'];
		$this->shipping_address_country = $result3 ['register_address_country'];
		$this->shipping_iln = $this->nodes->{'Order-Parties'}->DeliveryPoint->ILN;
		$this->shipping_nip = $result3 ['to_vatid'];
		if ($result ['payment_date_days'])
			$this->payment_date_days= $result ['payment_date_days'];
		if ($result ['supplier_code'])
			$this->supplier_code = $result ['supplier_code'];
		
		if ($result2 ['supplier_code'])
			$this->supplier_code = $result2 ['supplier_code'];
		
		$this->register_date = date ( "d.m.Y", strtotime ( $this->nodes->{'Order-Header'}->OrderDate ) );
		$this->delivery_date = date ( "d.m.Y", strtotime ( $this->nodes->{'Order-Header'}->ExpectedDeliveryDate ) );
		$date = new DateTime(date ( "d.m.Y", strtotime ( $this->nodes->{'Order-Header'}->OrderDate )) );
		if($this->payment_date_days!=""){
	
			$date->modify("+".$this->payment_date_days." day");

		}
		$this->payment_date = $date->format("d.m.Y");
		$this->assigned_user_id = $_SESSION ['authenticated_user_id'];
		$this->position_list = array ();
	}
	
	/*
	 * Pobiera produkty z xml
	 */
	function getPositionList($array = false) {
		$this->c = 0;
		foreach ( $this->nodes->{'Order-Lines'}->Line as $prod ) {
			$w = $this->db->query ( "select p.id from ecmproducts as p where (TRIM(p.ean)='" . trim ( $prod->{'Line-Item'}->EAN ) . "' OR TRIM(p.ean2)='" . trim ( $prod->{'Line-Item'}->EAN ) . "') and p.deleted='0'" );

			$r = $GLOBALS ['db']->fetchByAssoc ( $w );
			$ecmp = new EcmProduct ();
			$ecmp->retrieve ( $r ['id'] );
			$this->position_list [$this->c] ['product_id'] = $ecmp->id;
			$this->position_list [$this->c] ['position'] = ( string ) $prod->{'Line-Item'}->LineNumber;
			$this->position_list [$this->c] ['product_code'] = $ecmp->code;
			$this->position_list [$this->c] ['name'] = $ecmp->name;
			$this->position_list [$this->c] ['quantity'] = ( string ) $prod->{'Line-Item'}->OrderedQuantity;
			$this->position_list [$this->c] ['price_start'] = ( string ) $prod->{'Line-Item'}->OrderedUnitNetPrice;
			$this->position_list [$this->c] ['price_netto'] = ( string ) $prod->{'Line-Item'}->OrderedUnitNetPrice;
			$this->position_list [$this->c] ['discount'] = 0;
			$this->position_list [$this->c] ['total_netto'] = ( string ) $prod->{'Line-Item'}->OrderedUnitNetPrice * $prod->{'Line-Item'}->OrderedQuantity;
			$this->position_list [$this->c] ['unit_id'] = 1;
			$this->position_list [$this->c] ['unit_name'] = 'szt.';
			$this->position_list [$this->c] ['ecmvat_id'] = $ecmp->vat_id;
			$this->position_list [$this->c] ['ecmvat_name'] = $ecmp->vat_name;
			$this->position_list [$this->c] ['ecmvat_value'] = $ecmp->vat_value;
			$this->position_list [$this->c] ['recipient_code'] = ( string ) $prod->{'Line-Item'}->BuyerItemCode;
			$this->position_list [$this->c] ['product_ean'] = ( string ) trim ( $prod->{'Line-Item'}->EAN );
			$this->c ++;
		}
		$json = getJSONobj ();
		return $array ? $this->position_list : $json->encode ( $this->position_list );
	}
	/*
	 * załaduj dane do zewnętrznej klasy
	 */
	function loader($klasa) {
		foreach ( $this as $key => $value ) {
			$rp = new ReflectionProperty ( $this, $key );
			if ($rp->isPrivate () == 1)
				continue;
			$klasa->$key = $this->$key;
		}
	}
}

?>