<?php
$methodName = "";
$db = null;

// aktualizuje oferte o status
// w pole `type` nalezy podac `id_presta` zamowienia z CRM
function create_guid_2() {
	$microTime = microtime ();
	list ( $a_dec, $a_sec ) = explode ( " ", $microTime );
	
	$dec_hex = dechex ( $a_dec * 1000000 );
	$sec_hex = dechex ( $a_sec );
	
	ensure_length_2 ( $dec_hex, 5 );
	ensure_length_2 ( $sec_hex, 6 );
	
	$guid = "";
	$guid .= $dec_hex;
	$guid .= create_guid_section_2 ( 3 );
	$guid .= '-';
	$guid .= create_guid_section_2 ( 4 );
	$guid .= '-';
	$guid .= create_guid_section_2 ( 4 );
	$guid .= '-';
	$guid .= create_guid_section_2 ( 4 );
	$guid .= '-';
	$guid .= $sec_hex;
	$guid .= create_guid_section_2 ( 6 );
	
	return $guid;
}
function create_guid_section_2($characters) {
	$return = "";
	for($i = 0; $i < $characters; $i ++) {
		$return .= dechex ( mt_rand ( 0, 15 ) );
	}
	return $return;
}
function ensure_length_2(&$string, $length) {
	$strlen = strlen ( $string );
	if ($strlen < $length) {
		$string = str_pad ( $string, $length, "0" );
	} else if ($strlen > $length) {
		$string = substr ( $string, 0, $length );
	}
}

// *******************************CATEGORIES*

$server->wsdl->addComplexType ( 'ecmcategories', 'complexType', 'struct', 'all', '', array (
		'id' => array (
				'name' => 'id',
				'type' => 'xsd:string' 
		),
		'name' => array (
				'name' => 'name',
				'type' => 'xsd:string' 
		),
		'name_en' => array (
				'name' => 'name_en',
				'type' => 'xsd:string' 
		),
		'description' => array (
				'name' => 'description',
				'type' => 'xsd:string' 
		) 
) );

$server->wsdl->addComplexType ( 'ecmcategories_array', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmcategories[]' 
		) 
), 'tns:ecmcategories' );
$server->register ( 'ecm_get_categories', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string',
		'pricelist' => 'xsd:string' 
), array (
		'return' => 'tns:ecmcategories_array' 
), $NAMESPACE );
function ecm_get_categories($user_name, $password, $pricelist) {
	
	// $GLOBALS['db']->query("INSERT INTO log VALUES ('!!!!!')");
	$return_array = array ();
	
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
	
	$query = $db->executeQuery ( "select `c`.`id`, `c`.`name`, `c`.`name_en`, `c`.`description` from `ecmproductcategories` as `c`
inner join `ecmproducts` as `p`
on `p`.`product_category_id` = `c`.`id`
where
`p`.`deleted` = '0'
and `c`.`deleted`= '0'
and `p`.`www_available` = '1'
group by `c`.`id`" );
	
	foreach ( $db->executeReader ( $query ) as $r ) {
		$return_array [] = array (
				"id" => $r ['id'],
				"name" => $r ['name'],
				"name_en" => $r ['name_en'],
				"description" => $r ['description'] 
		);
	}
	
	/*
	 * $query = $db->executeQuery("SELECT `c`.`id`, `c`.`name`, `c`.`name_en`, `c`.`description` FROM `ecmproductcategories` `c` where (SELECT count(`p`.`id`) FROM `ecmproducts` `p` JOIN `ecmpricebooks_ecmproducts` `pbp` ON `pbp`.`ecmpricebook_id`='".$pricelist."' AND `pbp`.`ecmproduct_id`=`p`.`id` WHERE `p`.`product_category_id`=`c`.`id` AND `pbp`.`deleted`='0') > 0 AND `c`.`deleted`='0'"); foreach($db->executeReader($query) as $r) { $return_array[]=array("id"=>$r['id'], "name"=>$r['name'], "name_en"=>$r['name_en'], "description"=>$r['description']); }
	 */
	
	return $return_array;
}

/**
 * ********************
 */

// //////////

//
$server->wsdl->addComplexType ( 'ecmcategories_a', 'complexType', 'struct', 'all', '', array (
		'id' => array (
				'name' => 'id',
				'type' => 'xsd:string' 
		),
		'name' => array (
				'name' => 'name',
				'type' => 'xsd:string' 
		),
		'description' => array (
				'name' => 'description',
				'type' => 'xsd:string' 
		),
		'ecmpricebook_id' => array (
				'name' => 'ecmpricebook_id',
				'type' => 'xsd:string' 
		),
		'ecmprice_id' => array (
				'name' => 'ecmprice_id',
				'type' => 'xsd:string' 
		),
		'billing_address_street' => array (
				'name' => 'billing_address_street',
				'type' => 'xsd:string' 
		),
		'billing_address_postalcode' => array (
				'name' => 'billing_address_postalcode',
				'type' => 'xsd:string' 
		),
		'billing_address_city' => array (
				'name' => 'billing_address_city',
				'type' => 'xsd:string' 
		),
		'phone_office' => array (
				'name' => 'phone_office',
				'type' => 'xsd:string' 
		),
		'email' => array (
				'name' => 'email',
				'type' => 'xsd:string'
		),
		'vatid' => array (
				'name' => 'vatid',
				'type' => 'xsd:string'
		)
) );

$server->wsdl->addComplexType ( 'ecmcategories_a_array', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmcategories[]' 
		) 
), 'tns:ecmcategories_a' );
$server->register ( 'ecm_get_categories_a', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string',
		'pricelist' => 'xsd:string' 
), array (
		'return' => 'tns:ecmcategories_a_array' 
), $NAMESPACE );
function ecm_get_categories_a($user_name, $password, $date = "") {
	$return_array = array ();
	
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
	
	$query = $db->executeQuery ( "SELECT `a`.`ecmprice_id`, `a`.`ecmpricebook_id`, `a`.`shop_user`, `a`.`id`, `a`.`name`, `a`.`deleted`, `a`.`billing_address_street`, `a`.`billing_address_postalcode`, `a`.`billing_address_city`, `a`.`phone_office`, `a`.`to_vatid`, `a`.`is_vat_free` FROM `accounts` `a` WHERE `a`.`shop_user` = 'b2b' AND `a`.`website_update`=1" );
	foreach ( $db->executeReader ( $query ) as $r ) {
	
		//get email
		$e_res = $db->executeQuery("
								select `email_address` as `email` from `email_addresses` as `ea`
inner join `email_addr_bean_rel` as `rel`
on `rel`.`email_address_id` = `ea`.`id`
inner join `accounts` as `a`
on `rel`.`bean_id` = `a`.`id`
where
`rel`.`primary_address`='1' and
`a`.`id`='".$r['id']."';
				");
		foreach ($db->executeReader($e_res) as $e)
			$email = $e['email'];
		
		$return_array [] = array (
				"id" => $r ['id'],
				"name" => $r ['name'],
				"description" => $r ['description'],
				"ecmpricebook_id" => $r ['ecmpricebook_id'],
				"ecmprice_id" => $r ['ecmprice_id'],
				"billing_address_street" => $r ['billing_address_street'],
				"billing_address_postalcode" => $r ['billing_address_postalcode'],
				"billing_address_city" => $r ['billing_address_city'],
				"phone_office" => $r ['phone_office'],
				"email" => $email,
				"vatid" => $r['to_vatid'],
		);
	}
	return $return_array;
}

//

// *******************************PRODUCTS*

$server->wsdl->addComplexType ( 'ecmproducts2', 'complexType', 'struct', 'all', '', array (
		'id' => array (
				'name' => 'id',
				'type' => 'xsd:string' 
		),
		'name' => array (
				'name' => 'name',
				'type' => 'xsd:string' 
		),
		'name_en' => array (
				'name' => 'name_en',
				'type' => 'xsd:string' 
		),
		'description' => array (
				'name' => 'description',
				'type' => 'xsd:string' 
		),
		'description_en' => array (
				'name' => 'description_en',
				'type' => 'xsd:string' 
		),
		'code' => array (
				'name' => 'code',
				'type' => 'xsd:string' 
		),
		'manufacturer_id' => array (
				'name' => 'manufacturer_id',
				'type' => 'xsd:string' 
		),
		'srp_price' => array (
				'name' => 'srp_price',
				'type' => 'xsd:float' 
		),
		'srp_price_eur' => array (
				'name' => 'srp_price_eur',
				'type' => 'xsd:float' 
		),
		'pl_vat' => array (
				'name' => 'pl_vat',
				'type' => 'xsd:float' 
		),
		'image' => array (
				'name' => 'image',
				'type' => 'xsd:string' 
		),
		'popular' => array (
				'name' => 'popular',
				'type' => 'xsd:string' 
		),
		'stock_value' => array (
				'name' => 'stock_value',
				'type' => 'xsd:int' 
		),
		'category' => array (
				'name' => 'category',
				'type' => 'xsd:string' 
		),
		'stock_desc_pl' => array (
				'name' => 'stock_desc_pl',
				'type' => 'xsd:string' 
		),
		'stock_desc_en' => array (
				'name' => 'stock_desc_en',
				'type' => 'xsd:string' 
		) 
) );

$server->wsdl->addComplexType ( 'ecmproducts_array2', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmproducts2[]' 
		) 
), 'tns:ecmproducts2' );
$server->register ( 'ecm_get_products_one', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string',
		'category' => 'xsd:string',
		'lang' => 'xsd:int' 
), array (
		'return' => 'tns:ecmproducts_array2' 
), $NAMESPACE );
function ecm_get_products_one($user_name, $password, $category, $lang, $pricebook, $product_id, $stock = "") {
	if ($lang == 1)
		$lang = "pl";
	if ($lang == 2)
		$lang = "en";
	
	$return_array = array ();
	
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
	
	$query = $db->executeQuery ( "select `p`.`id`,`p`.`code`, `pl`.`short_description` as `name`, `en`.`short_description` as `name_en`, `pl`.`long_description`, `en`.`long_description` as `long_description_en`, `p`.`manufacturer_id`,`p`.`www_popular`, `p`.`www_price_pln`, `p`.`www_price_eur`, `v`.`value`, `p`.`product_picture`, `p`.`www_state`, `p`.`product_category_id`, `p`.`www_state_desc_pl`, `p`.`www_state_desc_en`
			from `ecmproducts` as `p`
			inner join `ecmvats` as `v`
			on `v`.`id` = `p`.`vat_id`
			inner join `ecmproduct_language` as `pl`
			on `pl`.`ecmproduct_id` = `p`.`id` and `pl`.`language`='pl'
			inner join `ecmproduct_language` as `en`
			on `en`.`ecmproduct_id` = `p`.`id` and `en`.`language`='en'
			where `p`.`id`='" . $product_id . "';" );
	foreach ( $db->executeReader ( $query ) as $r ) {
		
		$return_array [] = array (
				"id" => $r ['id'],
				"name" => $r ['name'],
				"name_en" => $r ['name_en'],
				"description" => $r ['long_description'],
				"description_en" => $r ['long_description_en'],
				"code" => $r ['code'],
				"manufacturer_id" => $r ['manufacturer_id'],
				"srp_price" => $r ['www_price_pln'],
				"srp_price_eur" => $r ['www_price_eur'],
				"pl_vat" => $r ['value'],
				"image" => $r ['product_picture'],
				"popular" => $r ['www_popular'],
				"stock_value" => $r ['www_state'],
				"category" => $r ['product_category_id'],
				"stock_desc_pl" => $r ['www_state_desc_pl'],
				"stock_desc_en" => $r ['www_state_desc_en'] 
		);
	}
	return $return_array;
}

/**
 * *******************
 */

$server->wsdl->addComplexType ( 'ecmproducts', 'complexType', 'struct', 'all', '', array (
		'id' => array (
				'name' => 'id',
				'type' => 'xsd:string' 
		),
		'name' => array (
				'name' => 'name',
				'type' => 'xsd:string' 
		),
		'name_en' => array (
				'name' => 'name_en',
				'type' => 'xsd:string' 
		),
		'description' => array (
				'name' => 'description',
				'type' => 'xsd:string' 
		),
		'description_en' => array (
				'name' => 'description_en',
				'type' => 'xsd:string' 
		),
		'deleted' => array (
				'name' => 'deleted',
				'type' => 'xsd:int' 
		),
		'code' => array (
				'name' => 'code',
				'type' => 'xsd:string' 
		),
		'manufacturer_id' => array (
				'name' => 'manufacturer_id',
				'type' => 'xsd:string' 
		),
		'srp_price' => array (
				'name' => 'srp_price',
				'type' => 'xsd:float' 
		),
		'srp_price_eur' => array (
				'name' => 'srp_price_eur',
				'type' => 'xsd:float' 
		),
		'pl_vat' => array (
				'name' => 'pl_vat',
				'type' => 'xsd:float' 
		),
		'image' => array (
				'name' => 'image',
				'type' => 'xsd:string' 
		),
		'popular' => array (
				'name' => 'popular',
				'type' => 'xsd:string' 
		),
		'stock_value' => array (
				'name' => 'stock_value',
				'type' => 'xsd:int' 
		) 
) );

$server->wsdl->addComplexType ( 'ecmproducts_array', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmproducts[]' 
		) 
), 'tns:ecmproducts' );
$server->register ( 'ecm_get_products2', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string',
		'category' => 'xsd:string',
		'lang' => 'xsd:int' 
), array (
		'return' => 'tns:ecmproducts_array' 
), $NAMESPACE );
function ecm_get_products2($user_name, $password, $category, $lang, $pricebook, $stock = "", $date = "") {
	if ($lang == 1)
		$lang = "pl";
	if ($lang == 2)
		$lang = "en";
	
	$return_array = array ();
	
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
	
	/*
	 * $query = $db->executeQuery("SELECT `stock`.`quantity`, `p`.`id`, `p`.`name`, `l`.`long_description`, `l_en`.`short_description` as `name_en`, `l_en`.`long_description` as `long_description_en`, `pbp`.`deleted`, `p`.`code`, `p`.`manufacturer_id`, `p`.`srp_price`, `p`.`srp_price_eur`, `v`.`value`, `p`.`product_picture`, `pbp`.`popular` FROM `crm`.`ecmproducts` `p` JOIN `crm`.`ecmvats` `v` ON `v`.`id`=`p`.`vat_id` AND `v`.`deleted`='0' JOIN `crm`.`ecmpricebooks_ecmproducts` `pbp` ON `pbp`.`ecmproduct_id`=`p`.`id` AND `pbp`.`product_category_id`='".$category."' JOIN `crm`.`ecmproduct_language` `l` ON `l`.`ecmproduct_id`=`p`.`id` AND `l`.`language`='pl' JOIN `crm`.`ecmproduct_language` `l_en` ON `l_en`.`ecmproduct_id`=`p`.`id` AND `l_en`.`language`='en' LEFT JOIN `crm`.`ecmstockstates` `stock` ON `stock`.`stock_id`='".$stock."' and `stock`.`product_id`=`p`.`id` ".($date != "" ? "WHERE `p`.`date_modified`>'".$date."'" : "")." group by `p`.`id`");
	 */
	$query = $db->executeQuery ( "SELECT `stock`.`quantity`, `p`.`id`, `p`.`name`, `l`.`long_description`, 
	`l_en`.`short_description` as `name_en`, `l_en`.`long_description` as `long_description_en`, 
	`pbp`.`deleted`, 
`p`.`code`, `p`.`manufacturer_id`, `p`.`srp_price`, `p`.`srp_price_eur`, 
`v`.`value`, `p`.`product_picture`, `pbp`.`popular` FROM `crm`.`ecmproducts` `p` 
JOIN `crm`.`ecmvats` `v` ON `v`.`id`=`p`.`vat_id` AND `v`.`deleted`='0' 
JOIN `crm`.`ecmpricebooks_ecmproducts` `pbp` ON `pbp`.`ecmproduct_id`=`p`.`id` AND `p`.`product_category_id`='" . $category . "' 
JOIN `crm`.`ecmproduct_language` `l` 
ON `l`.`ecmproduct_id`=`p`.`id` AND `l`.`language`='pl' 
JOIN `crm`.`ecmproduct_language` `l_en` 
ON `l_en`.`ecmproduct_id`=`p`.`id` AND `l_en`.`language`='en' 

LEFT JOIN `crm`.`ecmstockstates` `stock` ON `stock`.`stock_id`='" . $stock . "' and `stock`.`product_id`=`p`.`id`

" . ($date != "" ? "WHERE `p`.`date_modified`>'" . $date . "'" : "") . " group by `p`.`id`" );
	foreach ( $db->executeReader ( $query ) as $r ) {
		
		$return_array [] = array (
				"id" => $r ['id'],
				"name" => $r ['name'],
				"name_en" => $r ['name_en'],
				"description" => $r ['long_description'],
				"description_en" => $r ['long_description_en'],
				"deleted" => $r ['deleted'],
				"code" => $r ['code'],
				"manufacturer_id" => $r ['manufacturer_id'],
				"srp_price" => $r ['srp_price'],
				"srp_price_eur" => $r ['srp_price_eur'],
				"pl_vat" => $r ['value'],
				"image" => $r ['product_picture'],
				"popular" => $r ['popular'],
				"stock_value" => $r ['quantity'] 
		);
	}
	return $return_array;
}

$server->wsdl->addComplexType ( 'ecmproducts', 'complexType', 'struct', 'all', '', array (
		'id' => array (
				'name' => 'id',
				'type' => 'xsd:string' 
		),
		'name' => array (
				'name' => 'name',
				'type' => 'xsd:string' 
		),
		'name_en' => array (
				'name' => 'name_en',
				'type' => 'xsd:string' 
		),
		'description' => array (
				'name' => 'description',
				'type' => 'xsd:string' 
		),
		'description_en' => array (
				'name' => 'description_en',
				'type' => 'xsd:string' 
		),
		'deleted' => array (
				'name' => 'deleted',
				'type' => 'xsd:int' 
		),
		'code' => array (
				'name' => 'code',
				'type' => 'xsd:string' 
		),
		'manufacturer_id' => array (
				'name' => 'manufacturer_id',
				'type' => 'xsd:string' 
		),
		'srp_price' => array (
				'name' => 'srp_price',
				'type' => 'xsd:float' 
		),
		'srp_price_eur' => array (
				'name' => 'srp_price_eur',
				'type' => 'xsd:float' 
		),
		'pl_vat' => array (
				'name' => 'pl_vat',
				'type' => 'xsd:float' 
		),
		'image' => array (
				'name' => 'image',
				'type' => 'xsd:string' 
		),
		'popular' => array (
				'name' => 'popular',
				'type' => 'xsd:string' 
		),
		'stock_value' => array (
				'name' => 'stock_value',
				'type' => 'xsd:int' 
		) 
) );

$server->wsdl->addComplexType ( 'ecmproducts_array', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmproducts[]' 
		) 
), 'tns:ecmproducts' );
$server->register ( 'ecm_get_products', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string',
		'category' => 'xsd:string',
		'lang' => 'xsd:int' 
), array (
		'return' => 'tns:ecmproducts_array' 
), $NAMESPACE );
function ecm_get_products($user_name, $password, $category, $lang, $pricebook, $stock, $date = "") {
	if ($lang == 1)
		$lang = "pl";
	if ($lang == 2)
		$lang = "en";
	
	$return_array = array ();
	
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
	
	$query = $db->executeQuery ( "SELECT `stock`.`quantity`, `p`.`id`, `p`.`name`, `l`.`long_description`, `l_en`.`short_description` as `name_en`, `l_en`.`long_description` as `long_description_en`, `pbp`.`deleted`, `p`.`code`, `p`.`manufacturer_id`, `p`.`srp_price`, `p`.`srp_price_eur`, `v`.`value`, `p`.`product_picture`, `pbp`.`popular` FROM `ecmproducts` `p` JOIN `ecmvats` `v` ON `v`.`id`=`p`.`vat_id` AND `v`.`deleted`='0' JOIN `ecmpricebooks_ecmproducts` `pbp` ON `pbp`.`ecmproduct_id`=`p`.`id` AND `pbp`.`ecmpricebook_id`='" . $pricebook . "' JOIN `ecmproduct_language` `l` ON `l`.`ecmproduct_id`=`p`.`id` AND `l`.`language`='pl' JOIN `ecmproduct_language` `l_en` ON `l_en`.`ecmproduct_id`=`p`.`id` AND `l_en`.`language`='en' 
				LEFT JOIN `ecmstockstates` `stock` ON `stock`.`stock_id`='" . $stock . "' and `stock`.`product_id`=`p`.`id`
				WHERE `p`.`product_category_id`='" . $category . "'" . ($date != "" ? " AND `p`.`date_modified`>'" . $date . "'" : "") . "  group by `p`.`id`" );
	foreach ( $db->executeReader ( $query ) as $r ) {
		
		$return_array [] = array (
				"id" => $r ['id'],
				"name" => $r ['name'],
				"name_en" => $r ['name_en'],
				"description" => $r ['long_description'],
				"description_en" => $r ['long_description'],
				"deleted" => $r ['deleted'],
				"code" => $r ['code'],
				"manufacturer_id" => $r ['manufacturer_id'],
				"srp_price" => $r ['srp_price'],
				"srp_price_eur" => $r ['srp_price_eur'],
				"pl_vat" => $r ['value'],
				"image" => $r ['product_picture'],
				"popular" => $r ['popular'],
				"stock_value" => $r ['quantity'] 
		);
	}
	return $return_array;
}

// add mz
// mark product as updated
$server->register ( 'ecm_mark_product_as_updated', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string',
		'product_id' => 'xsd:string' 
), array (), $NAMESPACE );
function ecm_mark_product_as_updated($product_id) {
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
	
	$db->executeQuery ( "UPDATE ecmproducts SET website_update='0' WHERE id='$product_id'" );
	
	return null;
}

// *******************************PRICEBOOK PRODUCTS*

$server->wsdl->addComplexType ( 'ecmpricebook_products2', 'complexType', 'struct', 'all', '', array (
		'ecmproduct_id' => array (
				'name' => 'ecmproduct_id',
				'type' => 'xsd:string' 
		),
		'popular' => array (
				'name' => 'popular',
				'type' => 'xsd:int' 
		),
		'website_update' => array (
				'name' => 'deleted',
				'type' => 'xsd:int' 
		) 
) );

$server->wsdl->addComplexType ( 'ecmpricebook_products_array2', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmpricebook_products2[]' 
		) 
), 'tns:ecmpricebook_products2' );
$server->register ( 'ecm_get_pricebook_products_category', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string' 
), array (
		'return' => 'tns:ecmpricebook_products_array2' 
), $NAMESPACE );
function ecm_get_pricebook_products_category($user_name, $password, $category) {
	$return_array = array ();
	
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
	
	$query = $db->executeQuery ( "select `id`, `website_update`, `www_popular` from `ecmproducts`
			where `www_available` = '1'
			and `product_category_id`='" . $category . "';" );
	foreach ( $db->executeReader ( $query ) as $r ) {
		
		$return_array [] = array (
				"ecmproduct_id" => $r ['id'],
				"popular" => $r ['www_popular'],
				"website_update" => $r ['website_update'] 
		);
	}
	return $return_array;
}

// *******************************PRICEBOOK PRODUCTS*

$server->wsdl->addComplexType ( 'ecmpricebook_products', 'complexType', 'struct', 'all', '', array (
		'ecmproduct_id' => array (
				'name' => 'ecmproduct_id',
				'type' => 'xsd:string' 
		),
		'price' => array (
				'name' => 'price',
				'type' => 'xsd:float' 
		), 
) );

$server->wsdl->addComplexType ( 'ecmpricebook_products_array', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmpricebook_products[]' 
		) 
), 'tns:ecmpricebook_products' );
$server->register ( 'ecm_get_pricebook_products', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string' 
), array (
		'return' => 'tns:ecmpricebook_products_array' 
), $NAMESPACE );
function ecm_get_pricebook_products($user_name, $password, $pricebook) {
	$return_array = array ();

	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );

	$query = $db->executeQuery ( "SELECT `ecmproduct_id`,`price` FROM `ecmpricebooks_ecmproducts` 
WHERE `ecmpricebook_id`='" . $pricebook . "' AND `deleted`='0'");
	foreach ( $db->executeReader ( $query ) as $r ) {		
		$return_array [] = array (
				"ecmproduct_id" => $r ['ecmproduct_id'],
				"price" => $r ['price'],
		);
	}
	return $return_array;
}

$server->register ( 'ecm_get_price_products', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string'
), array (
		'return' => 'tns:ecmpricebook_products_array'
), $NAMESPACE );
function ecm_get_price_products($user_name, $password, $price) {
	$return_array = array ();

	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );

	$query = $db->executeQuery ( "SELECT `ecmproduct_id`,`price` FROM `ecmprices_ecmproducts`
WHERE `ecmprice_id`='" . $price . "'");
	foreach ( $db->executeReader ( $query ) as $r ) {
		$return_array [] = array (
				"ecmproduct_id" => $r ['ecmproduct_id'],
				"price" => $r ['price'],
		);
	}
	return $return_array;
}

// *******************************PRICEBOOKS*

$server->wsdl->addComplexType ( 'ecmpricebooks', 'complexType', 'struct', 'all', '', array (
		'id' => array (
				'name' => 'id',
				'type' => 'xsd:string' 
		),
		'name' => array (
				'name' => 'name',
				'type' => 'xsd:string' 
		), 
		'currency' => array (
				'name' => 'currency',
				'type' => 'xsd:string'
		),
		'active' => array (
				'name' => 'active',
				'type' => 'xsd:string'
		)
) );

$server->wsdl->addComplexType ( 'ecmpricebooks_array', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmpricebooks[]' 
		) 
), 'tns:ecmpricebooks' );
$server->register ( 'ecm_get_pricebooks', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string' 
), array (
		'return' => 'tns:ecmpricebooks_array' 
), $NAMESPACE );
function ecm_get_pricebooks($user_name, $password, $pricebook) {
	$return_array = array ();	
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );	
	$query = $db->executeQuery ( "SELECT `id`, `name`, `exchange_rate_id`, `active` FROM `ecmpricebooks` WHERE `id`='" . $pricebook . "'" );
	foreach ( $db->executeReader ( $query ) as $r ) {
		if ($r['exchange_rate_id']=='-99')
			$currency = 'PLN';
		if ($r['exchange_rate_id']=='6336d9a0-ee5f-52e3-7d0c-4e6f1472b2bf')
			$currency = 'EUR';
		$return_array [] = array (
				"id" => $r ['id'],
				"name" => $r ['name'],
				"currency" => $currency, 
				"active" => $r['active'],
		);
	}
	return $return_array;
}
$server->register ( 'ecm_get_prices', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string'
), array (
		'return' => 'tns:ecmpricebooks_array'
), $NAMESPACE );
function ecm_get_prices($user_name, $password, $price) {
	$return_array = array ();
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
	$query = $db->executeQuery ( "SELECT `id`, `name`, `exchange_rate_id`, `active` FROM `ecmprices` WHERE `id`='" . $price . "'" );
	foreach ( $db->executeReader ( $query ) as $r ) {
		if ($r['exchange_rate_id']=='-99')
			$currency = 'PLN';
		if ($r['exchange_rate_id']=='6336d9a0-ee5f-52e3-7d0c-4e6f1472b2bf')
			$currency = 'EUR';
		$return_array [] = array (
				"id" => $r ['id'],
				"name" => $r ['name'],
				"currency" => $currency,
				"active" => $r['active'],
		);
	}
	return $return_array;
}

/**
 * ***********************************
 */
/* potwierdzenie sprzedazy */

$server->register ( 'ecm_add_order_from_shop', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string',
		'order' => 'xsd:string' 
), array (
		'return' => 'xsd:string' 
), $NAMESPACE );
function ecm_add_order_from_shop($user_name, $password, $order, $presta, $user, $a, $b, $c, $d) {
	$x = "";
	$x = "S";
	$y = "false";
	
	if (! empty ( $order )) {
		
		$y = "ok";
		
		try {
			
			$order = unserialize ( base64_decode ( $order ) );
			
			// return print_r($order);
			if (! empty ( $order ) && is_array ( $order )) {
				
				if ($order ['Order-Header'] ['OrderNumber'] == "ORDERTEST1234")
					return "IDTEST1234";
				
				else {
					require_once ("modules/EcmSales/EcmSale.php");
					$focus = new EcmSale ();
					
					$template_id = "97700b0d-fbe9-e366-4016-4b260f058a47";
					
					$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
					
					$focus->template_id = $template_id;
					// $r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select name from ecmdocumenttemplates where id='".$template_id."'"));
					// $query = $db->executeQuery(query("select name from ecmdocumenttemplates where id='".$template_id."'"));
					
					$row = $db->fetchFirstRow ( "select name from ecmdocumenttemplates where id='" . $template_id . "'" );
					$focus->template_name = $row ['name'];
					$focus->setTemplate ();
					$focus->number = $focus->generateNumber ();
					$focus->document_no = $focus->formatNumber ();
					$focus->currency_id = "PLN";
					$focus->ecmlanguage = "pl_pl";
					$focus->status = "s10";
					$focus->type = "sales_order";
					$focus->modified_user_id = "d09b87cf-efff-2f7c-a859-4ba38402488f";
					$focus->created_by = "d09b87cf-efff-2f7c-a859-4ba38402488f";
					
					$pr = $order ['Order-Lines'];
					$x = "ok";
					include_once ("modules/Accounts/Account.php");
					// $ww=$GLOBALS['db']->query("select count(*) as c from accounts where b2clogin ='".$order['Order-Parties']['Buyer']['Login']."' and deleted='0'");
					// $ww=$GLOBALS['db']->query("select count(*) as c from accounts where `id` ='".$order['Order-Parties']['Buyer']['Login']."' and deleted='0'");
					$row = $db->fetchFirstRow ( "select count(*) as c from accounts where `id` ='" . $order ['Order-Parties'] ['Buyer'] ['Login'] . "' and deleted='0'" );
					// $rr=$GLOBALS['db']->fetchByAssoc($ww);
					$focus->parent_id = "getmir";
					$focus->description = iconv ( "ISO-8859-2", "UTF-8", $order ['Order-Parties'] ['Buyer'] ['Description'] );
					
					// if($rr['c']==0){
					// $account=new Account();
					$account->ownership = "getmir";
					// $account->name=iconv("ISO-8859-2","UTF-8",$order['Order-Parties']['Buyer']['Name']);
					// $account->b2clogin=iconv("ISO-8859-2","UTF-8",$order['Order-Parties']['Buyer']['Login']);
					// $account->billing_address_street=iconv("ISO-8859-2","UTF-8",$order['Order-Parties']['Buyer']['StreetAndNumber']);
					// $account->billing_address_postalcode=iconv("ISO-8859-2","UTF-8",$order['Order-Parties']['Buyer']['PostalCode']);
					// $account->billing_address_city=iconv("ISO-8859-2","UTF-8",$order['Order-Parties']['Buyer']['CityName']);
					// $account->shipping_address_street=iconv("ISO-8859-2","UTF-8",$order['Order-Parties']['DeliveryPoint']['StreetAndNumber']);
					// $account->shipping_address_postalcode=iconv("ISO-8859-2","UTF-8",$order['Order-Parties']['DeliveryPoint']['PostalCode']);
					// $account->shipping_address_city=iconv("ISO-8859-2","UTF-8",$order['Order-Parties']['DeliveryPoint']['CityName']);
					// $focus->parent_id=create_guid_2();
					// $GLOBALS['db']->query("insert into accounts set id='".$focus->parent_id."',name='".$account->name."',billing_address_street='".$account->billing_address_street."',billing_address_postalcode='".$account->billing_address_postalcode."',billing_address_city='".$account->billing_address_city."',shipping_address_street='".$account->shipping_address_street."',shipping_address_postalcode='".$account->shipping_address_postalcode."',shipping_address_city='".$account->shipping_address_city."', b2clogin='".$account->b2clogin."'");
					// }
					// else{
					
					// $w=$GLOBALS['db']->query("select id from accounts where b2clogin = '".$order['Order-Parties']['Buyer']['Login']."' and deleted='0'");
					// $w=$GLOBALS['db']->query("select `id` from `accounts` where `id` = '".$order['Order-Parties']['Buyer']['Login']."' and `deleted`='0'");
					$row = $db->fetchFirstRow ( "select `id` from `accounts` where `id` = '" . $order ['Order-Parties'] ['Buyer'] ['Login'] . "' and `deleted`='0'" );
					// $r=$GLOBALS['db']->fetchByAssoc($w);
					$account = new Account ();
					// update account, maybe sth. change
					
					// $GLOBALS['db']->query("update accounts set name='".$account->name."',billing_address_street='".$account->billing_address_street."',billing_address_postalcode='".$account->billing_address_postalcode."',billing_address_city='".$account->billing_address_city."'"."',shipping_address_street='".$account->shipping_address_street."',shipping_address_postalcode='".$account->shipping_address_postalcode."',shipping_address_city='".$account->shipping_address_city."'where id='".$r['id']."'");
					
					// $account->retrieve($r['id']);
					$focus->parent_id = $row ['id'];
					// }
					$focus->assigned_user_id = "d09b87cf-efff-2f7c-a859-4ba38402488f";
					$focus->register_date = $order ['Order-Header'] ['OrderDate'];
					$focus->delivery_date = $order ['Order-Header'] ['ExpectedDeliveryDate'];
					// $focus->supplier_code=3442;
					$focus->order_no = $order ['Order-Header'] ['OrderNumber'];
					$focus->ecmlanguage = "pl_pl";
					$focus->ecmpaymentcondition_id = $order ['Order-Parties'] ['Buyer'] ['PaymentConditionId'];
					
					$focus->parent_name = $row ["name"]; // $account->name;
					$focus->parent_address_street = $a; // $account->shipping_address_street;
					$focus->parent_address_city = $b; // $account->shipping_address_city;
					$focus->parent_address_postalcode = $c; // $account->shipping_address_postalcode;
					$focus->parent_address_country = $d; // $account->shipping_address_country;
					
					$ra = array ();
					$total = 0;
					
					if (count ( $pr ) > 0) {
						foreach ( $pr as $prod ) {
							$product_id = "";
							// $w=$GLOBALS['db']->query("select id as ecmproduct_id from ecmproducts where code='".$prod['Line']['Line-Item']['CODE']."' and deleted='0' and code NOT LIKE '%_w' and code NOT LIKE '%_z' and code NOT LIKE '%_W' and code NOT LIKE '%_Z'");
							// $w=$GLOBALS['db']->query("select `id` as `ecmproduct_id` from `ecmproducts` where `id`='".$prod['Line']['Line-Item']['CODE']."' and `deleted`='0' and `code` NOT LIKE '%_w' and `code` NOT LIKE '%_z' and `code` NOT LIKE '%_W' and `code` NOT LIKE '%_Z'");
							$row = $db->fetchFirstRow ( "select `id` as `ecmproduct_id` from `ecmproducts` where `id`='" . $prod ['Line'] ['Line-Item'] ['CODE'] . "' and `deleted`='0' and `code` NOT LIKE '%_w' and `code` NOT LIKE '%_z' and `code` NOT LIKE '%_W' and `code` NOT LIKE '%_Z'" );
							// $r=$GLOBALS['db']->fetchByAssoc($w);
							$product_id = $row ['ecmproduct_id'];
							
							$wp = $GLOBALS ['db']->query ( "select * from `ecmproducts` where `id`='" . $product_id . "'" );
							$rppp = $GLOBALS ['db']->fetchByAssoc ( $wp );
							$product_code = $rppp ['code'];
							$product_name = $rppp ['name'];
							$vat_id = $rppp ['vat_id'];
							$vat_name = $rppp ['vat_name'];
							$vat_value = $rppp ['vat_value'];
							
							$return_array ['id'] = $product_id;
							$return_array ['code'] = $product_code;
							$return_array ['name'] = $product_name;
							$return_array ['quantity'] = $prod ['Line'] ['Line-Item'] ['OrderedQuantity'];
							$return_array ['price'] = $prod ['Line'] ['Line-Item'] ['OrderedUnitNetPrice'];
							$return_array ['discount'] = 0;
							$return_array ['total'] = $prod ['Line'] ['Line-Item'] ['OrderedQuantity'] * $prod ['Line'] ['Line-Item'] ['OrderedUnitNetPrice'];
							$return_array ['unit_id'] = 1;
							$return_array ['unit_name'] = 'szt.';
							$return_array ['vat_id'] = $vat_id;
							$return_array ['vat_name'] = $vat_name;
							$return_array ['vat_value'] = $vat_value;
							$return_array ['currency_id'] = 'PLN';
							$return_array ['recipient_code'] = $prod ['Line'] ['Line-Item'] ['BuyerItemCode'];
							$total += $return_array ['quantity'] * $return_array ['price'] * (1 + $vat_value / 100);
							$ra [] = $return_array;
						}
					}
					$focus->position_list = $ra;
					$return_id = $focus->save_from_shop ( $presta, $user );
					$GLOBALS ['db']->query ( "update `ecmsales` set `total`='" . $total . "' where `id`='" . $return_id . "'" );
					
					return $return_id;
					
					// return print_r(mysql_error()."mm", true);
				}
			}
		} catch ( Exception $e ) {
		}
	}
	
	return $y;
}

/**
 * ***********************************
 */

// *******************************accounts_shop*

//

//

$server->wsdl->addComplexType ( 'ecmcustomer_add', 'complexType', 'struct', 'all', '', array (
		'status' => array (
				'name' => 'status',
				'type' => 'xsd:string' 
		) 
) );

$server->wsdl->addComplexType ( 'ecmcustomer_add_array', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmcategories[]' 
		) 
), 'tns:ecmcustomer_add' );
$server->register ( 'ecm_get_customer_add', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string',
		'pricelist' => 'xsd:string' 
), array (
		'return' => 'tns:ecmcustomer_add_array' 
), $NAMESPACE );
function ecm_get_customer_add($user_name, $password, $fullname = "", $pricebook = "", $shop_user = "", $email = "", $edit = false, $id = "", $street = "", $company = "", $city = "", $code = "", $nip = "", $phone = "") {
	$return_array = array ();
	
	if (! $edit) {
		$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
		$getID = create_guid ();
		$query = $db->executeQuery ( "INSERT INTO `accounts` SET `id`='" . $getID . "', `name`='" . $fullname . "', `date_entered`='" . date ( "Y-m-d H:i:s" ) . "', `date_modified`='" . date ( "Y-m-d H:i:s" ) . "', `ecmpricebook_id`='" . $pricebook . "', `shop_user`='" . $shop_user . "', `email`='" . $email . "'" );
		
		$return_array [] = array (
				"status" => $getID 
		);
	} else {
		
		if ($company != "")
			$fullname = $company;
		$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
		$query = $db->executeQuery ( "UPDATE `accounts` SET `name`='" . $fullname . "', `shipping_address_postalcode`='" . $code . "', `shipping_address_street`='" . $street . "', `shipping_address_city`='" . $city . "', `billing_address_postalcode`='" . $code . "', `billing_address_street`='" . $street . "', `billing_address_city`='" . $city . "', `vatid`='" . $nip . "', `phone_office`='" . $phone . "', `date_modified`='" . date ( "Y-m-d H:i:s" ) . "' WHERE `id`='" . $id . "'" );
		
		$return_array [] = array (
				"status" => 'ok' 
		);
	}
	return $return_array;
}

$server->wsdl->addComplexType ( 'ecmcategories2', 'complexType', 'struct', 'all', '', array (
		'id' => array (
				'name' => 'id',
				'type' => 'xsd:string' 
		) 
// 'name' => array('name'=>'name', 'type'=>'xsd:string'),
// 'name_en' => array('name'=>'name_en', 'type'=>'xsd:string'),
// 'description' => array('name'=>'description', 'type'=>'xsd:string'),
) );

$server->wsdl->addComplexType ( 'ecmcategories_array2', 'complexType', 'array', '', 'SOAP-ENC:Array', array (), array (
		array (
				'ref' => 'SOAP-ENC:arrayType',
				'wsdl:arrayType' => 'tns:ecmcategories2[]' 
		) 
), 'tns:ecmcategories2' );
$server->register ( 'ecm_get_categories2', array (
		'user_name' => 'xsd:string',
		'password' => 'xsd:string',
		'pricelist' => 'xsd:string' 
), array (
		'return' => 'tns:ecmcategories_array2' 
), $NAMESPACE );
function ecm_get_categories2($user_name, $password, $pricelist) {
	$return_array = array ();
	
	$db = new PDODB ( "mysql:host=" . MYSQL_API_HOST . ";dbname=" . MYSQL_API_DATABASE . ";port=" . MYSQL_API_PORT, MYSQL_API_USER, MYSQL_API_PASSWORD );
	
	$query = $db->executeQuery ( "SELECT `id` FROM `ecmproducts` LIMIT 10" );
	foreach ( $db->executeReader ( $query ) as $row ) {
		
		// echo $row[0]."<br>";
		
		$return_array [] = array (
				"id" => $row [0] 
		);
	}
	
	return $return_array;
}

?>
