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

define ( "FILE_DIR", "/var/edi/e5/orders/" ); // ś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,billing_address_street,billing_address_city,billing_address_postalcode,billing_address_country,id,assigned_user_id,name,parent_id,supplier_code,ecmpaymentcondition_id,ecmpaymentcondition_name 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 ['billing_address_street'];
		$this->parent_address_city = $result ['billing_address_city'];
		$this->parent_address_postalcode = $result ['billing_address_postalcode'];
		$this->parent_address_country = $result ['billing_address_country'];
		$this->parent_document_no = $this->nodes->{'Order-Header'}->OrderNumber;
		$this->shop_number = $this->nodes->{'Order-Header'}->Remarks;
		$this->edi_zs_id = $this->nodes->{'Order-Header'}->OrderZsId;
		$this->edi_zs_document_no = $this->nodes->{'Order-Header'}->OrderZsNumber;
		// adres dostawy
		// pobierz dane z klienta
		$result3 = $this->db->fetchByAssoc ( $this->db->query ( "select to_vatid,shipping_address_street,shipping_address_city,shipping_address_postalcode,shipping_address_country,id,assigned_user_id,name,parent_id,supplier_code,ecmpaymentcondition_id,ecmpaymentcondition_name from accounts where iln like '" . $this->nodes->{'Order-Parties'}->DeliveryPoint->ILN . "'" ) );
		$this->shipping_address_name = $result3 ['name'];
		$this->shipping_addresses = $result3 ['name'];
		$this->shipping_address_street = $result3 ['shipping_address_street'];
		$this->shipping_address_city = $result3 ['shipping_address_city'];
		$this->shipping_address_postalcode = $result3 ['shipping_address_postalcode'];
		$this->shipping_address_country = $result3 ['shipping_address_country'];
		$this->shipping_iln = $this->nodes->{'Order-Parties'}->DeliveryPoint->ILN;
		$this->shipping_nip = $result3 ['to_vatid'];
		if ($result ['ecmpaymentcondition_id'])
			$this->ecmpaymentcondition_id = $result ['ecmpaymentcondition_id'];
		if ($result ['supplier_code'])
			$this->supplier_code = $result ['supplier_code'];
		
		$result2 = $this->db->fetchByAssoc ( $this->db->query ( "select supplier_code,ecmpaymentcondition_id,ecmpaymentcondition_name from accounts where id='" . $result ['parent_id'] . "'" ) );
		if ($result2 ['ecmpaymentcondition_id'])
			$this->ecmpaymentcondition_id = $result2 ['ecmpaymentcondition_id'];
		if ($result2 ['supplier_code'])
			$this->supplier_code = $result2 ['supplier_code'];
		
		if (isset ( $this->ecmpaymentcondition_id ) && $this->ecmpaymentcondition_id != '') {
			$pc = new EcmPaymentCondition ();
			$pc->retrieve ( $this->ecmpaymentcondition_id );
			$this->ecmpaymentcondition_name = $pc->name;
			unset ( $pc );
		}
		$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 ) );
		$this->assigned_user_id = $_SESSION ['authenticated_user_id'];
		$this->position_list = array ();
	}
	
	function checkProducts($position_list) {
		$xmlSize = count($this->nodes->{'Order-Lines'}->Line);
		$plSize = count($position_list);
		$missedProducts = [];
		if ($xmlSize == $plSize) {
			return $missedProducts; // Everything is fine
		}
		// If there are some missed products we have to find them!
		$i = 0;
		foreach ( $this->nodes->{'Order-Lines'}->Line as $prod ) {
			$i++;
			$ean =  ( string ) trim ( $prod->{'Line-Item'}->EAN );
			if ($i == 5) $ean.='TEST';
			$fromPl = array_map(function ($prod) { return $prod['product_ean']; }, $position_list);
			if (!in_array($ean, $fromPl)) {
				array_push($missedProducts, $ean);
			}
		}
		return $missedProducts;
	}
	/*
	 * 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'] );
			if($this->parent_id=='1b9643ca-5b1a-8f9b-b809-586b5619b068'){
				$w2 = $this->db->query ( "select price,recipient_code from ecmpricebooks_ecmproducts where ecmpricebook_id='13b29aa1-48f0-de58-7630-59c22756c5e4' and ecmproduct_id='".$ecmp->id."' and deleted=0" );
				$r2 = $GLOBALS ['db']->fetchByAssoc ( $w2 );
				$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 ) $r2['price'];
				$this->position_list [$this->c] ['price_netto'] = ( string ) $r2['price'];
				$this->position_list [$this->c] ['discount'] = 0;
				$this->position_list [$this->c] ['total_netto'] = ( string ) $r2['price'] * $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 )$r2['recipient_code'];
				$this->position_list [$this->c] ['product_ean'] = ( string ) trim ( $prod->{'Line-Item'}->EAN );
			} else {
				$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;
		}
	}
}

?>