<?php
if (! defined ( 'sugarEntry' ) || ! sugarEntry)
	die ( '-1' );
if (! $_POST ['job'] || $_POST ['job'] == '')
	die ( '-1' );
switch ($_POST ['job']) {
	case 'getParentInfo' :
		getParentInfo ( $_POST ['id'], $_POST ['type'] );
		break;
	case 'generateNumber' :
		generateNumber ( $_POST ['stock_id'], $_POST ['date'] );
		break;
	case 'generateNumberCorrect' :
		generateNumberCorrect ();
		break;
	case 'searchProducts' :
		searchProducts ( $_POST ['searchKey'], $_POST ['searchCategory'], $_POST ['searchStock'], $_POST ['searchSort'] );
		break;
	case 'getProduct' :
		getProduct ( $_POST ['id'], $_POST ['pricebook'], $_POST ['account_id'], $_POST ['language'], $_POST ['stock_id'] );
		break;
	case 'getItems' :
		getItems ( $_POST ['record'], $_POST ['type'] );
		break;
	case 'getItemsFromWZ' :
		getItemsFromWZ ( $_POST ['record'], $_POST ['type'] );
		break;
	case 'getItemsFromSale' :
		getItemsFromSale ( $_POST ['record'], $_POST ['type'] );
		break;
	case 'getCategoriesList' :
		getCategoriesList ();
		break;
	case 'getTranslation' :
		getTranslation ( $_POST ['product_id'], $_POST ['language'], $_REQUEST ['unit_id'] );
		break;
	case 'getPricesInfo' :
		getPricesInfo ( $_POST ['product_id'], $_POST ['pricebook_id'], $_POST ['account_id'] );
		break;
	case 'getStockArray' :
		getStockArray ( $_POST ['product_id'] );
		break;
	case 'getPurchaseArray' :
		getPurchaseArray ( $_POST ['product_id'] );
		break;
	case 'getAddresses' :
		getAddresses ( $_POST ['account_id'] );
		break;
	case 'getPayers' :
		getPayers ( $_POST ['account_id'] );
		break;
	case 'getPayer' :
		getPayers ( $_POST ['account_id'] );
		break;
	case 'getAddress' :
		getAddress ( $_POST ['account_id'] );
		break;
	case 'SendMail' :
		SendMail ( $_POST ['id'], $_POST ['modulee'], $_POST ['parent_id'] );
		break;
	case 'getNBPCurrencyExchange' :
		getNBPCurrencyExchange ( $_POST ['c_id'], $_POST ['date'] );
		break;
	case 'getCurrencyValues' :
		getCurrencyValues ( $_POST ['c_id'], $_POST ['date'] );
		break;
	case 'getStockState' :
		getStockState ( $_POST ['id'], $_POST ['stock_id'] );
		break;
	case 'calculatePaymentDate' :
		calculatePaymentDate ( $_POST ['date'], $_POST ['days'] );
		break;
	case 'calculateDateDiff' :
		calculateDateDiff ( $_POST ['date1'], $_POST ['date2'] );
		break;
	case 'getBankAccounts' :
		getBankAccounts ();
		break;
	case 'getStockBankAccountId' :
		getStockBankAccountId ( $_POST ['stock_id'], $_POST ['parent_id'] );
		break;
	case 'getDataForAccount' :
		getDataForAccount ( $_POST ['id'] );
		break;
	case 'changePrice' :
		changePrice ( $_POST ['id'], $_POST ['price'] );
		break;
	case 'getVatValues' :
		getVatValues ();
		break;
}
function getVatValues(){
	$ecmvat = new EcmVat();
	$ecmvats = $ecmvat->get_full_list ( null, "" );
	$return_array=[];
	foreach ($ecmvats as $vat){
		$tmp=[];
		$tmp['id']=$vat->id;
		$tmp['name']=$vat->name;
		$tmp['value']=$vat->value;
		$return_array[]=$tmp;
	}
	echo json_encode($return_array);
	return;
}
function changePrice($id, $price) {
	$db = $GLOBALS ['db'];
	if ($id != '' && $price != '') {
		$query = "update ecminvoiceoutitems set price_purchase='" . $price . "' where id='" . $id . "'";
		$db->query ( $query );
	}
	
	return 'ok';
}
function getDataForAccount($id) {
	$a = new Account ();
	$a->retrieve ( $id );
	
	$back = array ();
	$tmp ['email'] = $a->email1;
	$tmp ['tele'] = $a->phone_office;
	
	$back [] = $tmp;
	print json_encode ( $back );
}
function getStockBankAccountId($stock_id, $parent_id) {
	global $db;
	$acc = $db->query ( "SELECT invoice_bank_account FROM accounts WHERE id ='" . $parent_id . "'" );
	$row_acc = $db->fetchByAssoc ( $acc );
	if ($row_acc ['invoice_bank_account'] == "") {
		$re = $db->query ( "SELECT value9 FROM operating_values where id = (SELECT default_bank_id FROM ecmstocks WHERE id='" . $stock_id . "')" );
		$rowstock = $db->fetchByAssoc ( $re );
		$Bank_account_number = $rowstock ['value9'];
	} else {
		$Bank_account_number = $row_acc ['invoice_bank_account'];
	}
	if ($Bank_account_number == "" || $Bank_account_number == NULL) {
		$Bank_account_number = "49 1050 1025 1000 0090 3129 6339"; // default
	}
	print json_encode ( $Bank_account_number );
}
function getBankAccounts() {
	require_once ('modules/EcmSysInfos/EcmSysInfo.php');
	$EcmSysInfo = new EcmSysInfo ();
	$lista = $EcmSysInfo->getBanks ();
	echo json_encode ( $lista );
	return;
}
function getStockState($id, $stock_id) {
	echo EcmStockOperation::getStock ( $id, $stock_id );
	return;
}
function SendMail($id, $modulee, $parent_id) {
	global $current_user;
	
	$db = $GLOBALS ['db'];
	
	$uq = $db->query ( "select google_login,CAST(AES_DECRYPT(google_password, 'jakistamhash123') as CHAR) 
AS google_password FROM users where id='" . $current_user->id . "'" );
	$rul = $db->fetchByAssoc ( $uq );
	
	if ($rul ['google_login'] != '' && $rul ['google_password'] != '') {
		
		require_once ("modules/" . $modulee . "/createPDF.php");
		require_once ("include/phpmailer/class.phpmailer.php");
		require_once ("include/phpmailer/class.smtp.php");
		
		$mailClassS = new PHPMailer ();
		
		$mailClassS->isSMTP (); // Set mailer to use SMTP
		
		$mailClassS->Host = 'smtp.gmail.com'; // Specify main and backup server
		
		$mailClassS->SMTPAuth = true; // Enable SMTP authentication
		$mailClassS->Username = $rul ['google_login']; // SMTP username
		$mailClassS->Password = $rul ['google_password']; // SMTP password
		$mailClassS->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
		$mailClassS->Port = 587; // Set the SMTP port number - 587 for
		$mailClassS->addBCC ( $rul ['google_login'], "" );
		// get email from accounts
		$sea = new SugarEmailAddress ();
		// Grab the array of addresses
		$addresses = $sea->getAddressesByGUID ( $parent_id, 'Accounts' );
		
		foreach ( $addresses as $address ) {
			if ($address ['email_address'] != '' && $address ['opt_out'] == 1) {
				$mailClassS->addAddress ( $address ['email_address'] ); // Add address
			}
		}
		// set sender
		$mailClassS->Sender = $rul ['google_login'];
		$mailClassS->From = $rul ['google_login'];
		$mailClassS->FromName = $current_user->first_name . ' ' . $current_user->last_name;
		
		$mailClassS->WordWrap = 50; // Set word wrap to 50 characters
		
		$mailClassS->isHTML ( true ); // Set email format to HTML
		
		$mailClassS->Subject = 'Dokument od Saas SystemS Sp. z o.o.';
		$module = substr ( $modulee, 0, strlen ( $modulee ) - 1 );
		$d = new $module ();
		$d->retrieve ( $id );
		
		$mailClassS->Body = 'Witam,<br><br>Przesyłam w załączniku dokument ' . $d->document_no . '.<br>Proszę o potwierdzenie otrzymania wiadomości e-mail z załączona Fakturą VAT.';
		// załącznik
		$path = createEcmInvoiceOutPdf ( $id, 'FILE' );
		
		if (file_exists ( '/var/www/html/crm/' . $path )) {
			
			$mailClassS->addAttachment ( '/var/www/html/crm/' . $path );
		}
		// Read an HTML message body from an external file, convert
		// referenced images to embedded,
		// convert HTML into a basic plain-text alternative body
		// $mailClassS->msgHTML(file_get_contents('contents.html'),
		// dirname(__FILE__));
		
		if (count ( $mailClassS->to ) > 0) {
			
			if (! $mailClassS->send ()) {
				
				unlink ( $path );
				echo '-1';
			} else {
				unlink ( $path );
				echo '1';
			}
		} else {
			echo '-1';
		}
	} else {
		echo '-1';
	}
}
function calculatePaymentDate($d, $days) {
	global $timedate;
	$date = new DateTime ( $d );
	$date->add ( new DateInterval ( 'P' . $days . 'D' ) );
	echo json_encode ( $date->format ( 'd.m.Y' ) );
	return;
}
function calculateDateDiff($d1, $d2) {
	$db = $GLOBALS ['db'];
	global $timedate;
	$d1 = $timedate->to_db_date ( $d1 );
	$d2 = $timedate->to_db_date ( $d2 );
	$ret = $db->fetchByAssoc ( $db->query ( "SELECT TIMESTAMPDIFF(DAY, '$d1','$d2') AS diff;" ) );
	echo json_encode ( $ret ['diff'] );
	return;
}
function getNBPCurrencyExchange($c_id, $d) {
	global $timedate;
	$d = explode ( '-', reset ( explode ( " ", $timedate->to_db ( $d ) ) ) );
	$date = date ( "Y-m-d", @mktime ( 0, 0, 0, $d [1], $d [2], $d [0] ) + 3600 * 24 );
	
	// what day is it?
	$dn = $GLOBALS ['db']->fetchByAssoc ( $GLOBALS ['db']->query ( "SELECT DAYNAME('$date') as dayname" ) );
	
	if ($dn ['dayname'] == 'Sunday') // - 2 days
		$q = "SELECT value, nbp_table_name FROM currency_nbp_archive WHERE currency_id='$c_id' AND date = DATE_ADD('$date', INTERVAL -2 DAY)";
	elseif ($dn ['dayname'] == 'Saturday') // - 1 day
		$q = "SELECT value FROM currency_nbp_archive WHERE currency_id='$c_id' AND date = DATE_ADD('$date', INTERVAL -1 DAY)";
	else // any other day - just get exchange
		$q = "SELECT value, nbp_table_name FROM currency_nbp_archive WHERE currency_id='$c_id' AND date='$date'";
	
	$w = $GLOBALS ['db']->fetchByAssoc ( $GLOBALS ['db']->query ( $q ) );
	
	$ret = array ();
	$ret ['value'] = $w ['value'];
	$ret ['name'] = $w ['nbp_table_name'];
	
	echo json_encode ( $ret );
	return;
}
function getCurrencyValues($c_id, $d) {
	global $timedate;
	$d = explode ( '-', reset ( explode ( " ", $timedate->to_db ( $d ) ) ) );
	$date = date ( "Y-m-d", @mktime ( 0, 0, 0, $d [1], $d [2], $d [0] ) + 3600 * 24 );
	
	$q = "SELECT value, nbp_table_name FROM currency_nbp_archive WHERE currency_id='$c_id' AND date < '$date' ORDER BY date DESC LIMIT 0,10";
	$db = $GLOBALS ['db'];
	
	$ret = $db->query ( $q );
	$result = array ();
	while ( $row = $db->fetchByAssoc ( $ret ) ) {
		$tmp = array ();
		$tmp ['value'] = $row ['value'];
		$tmp ['name'] = $row ['nbp_table_name'];
		$result [] = $tmp;
		unset ( $tmp );
	}
	
	echo json_encode ( $result );
	return;
}
function getAddress($account_id) {
	$a = new Account ();
	echo json_encode ( $a->getAddress ( $account_id ) );
	unset ( $a );
	return;
}
function getPayer($account_id) {
	$a = new Account ();
	echo json_encode ( $a->getAddress ( $account_id ) );
	unset ( $a );
	return;
}
function getAddresses($account_id) {
	$a = new Account ();
	
	echo json_encode ( $a->getAddresses ( $account_id ) );
	unset ( $a );
	return;
}
function getPayers($account_id) {
	$a = new Account ();
	
	echo json_encode ( $a->getPayers ( $account_id ) );
	unset ( $a );
	return;
}
function getParentInfo($id, $type) {
	if (! $id || $id == '')
		die ( '-1' );
	if (! $type || $type == '')
		die ( '-1' );
	
	if ($type == 'Accounts')
		$a = new Account ();
	elseif ($type == 'Leads')
		$a = new Lead ();
	else
		die ( - 1 );
	
	$a->retrieve ( $id );
	$data = array ();
	$data ['name'] = html_entity_decode ( $a->name );
	$data ['parent_nip'] = $a->to_vatid;
	$data ['parent_iln'] = $a->iln;
	$data ['parent_index'] = $a->index_dbf;
	$data ['parent_address_street'] = $a->register_address_street;
	$data ['parent_address_postalcode'] = $a->register_address_postalcode;
	$data ['parent_address_city'] = $a->register_address_city;
	$data ['parent_address_country'] = $a->register_address_country;
	$data ['invoice_type'] = $a->invoice_type;
	$data ['currency_id'] = $a->currency_id;
	$data ['invoice_bank_account'] = $a->invoice_bank_account;
	$data ['payment_date_days'] = $a->payment_date_days;
	$data ['payment_method'] = $a->payment_method;
	$data ['vat_payer'] = $a->vat_payer;
	$data ['pdf_text'] = $a->pdf_text;
	
	if ($a->ecmdeliverycondition_id && $a->ecmdeliverycondition_id != '') {
		$pc = new EcmdeliveryCondition ();
		$pc->retrieve ( $a->ecmdeliverycondition_id );
		$data ['ecmdeliverycondition_id'] = $pc->id;
		$data ['ecmdeliverycondition_name'] = $pc->name;
	}
	
	if ($a->ecmprice_id && $a->ecmprice_id != '') {
		$pr = new EcmPrice ();
		$pr->retrieve ( $a->ecmprice_id );
		$data ['ecmprice_id'] = $pr->id;
		$data ['ecmprice_name'] = $pr->name;
	}
	// get pricebooks, ownership pricebooks
	$pricebooks = array ();
	$db = $GLOBALS ['db'];
	$res = $db->query ( "SELECT id, name FROM ecmpricebooks WHERE account_id IN ('" . $a->id . "','" . $a->parent_id . "') AND active='1' AND deleted='0'" );
	while ( $row = $db->fetchByAssoc ( $res ) ) {
		$tmp = array ();
		$tmp ['id'] = $row ['id'];
		$tmp ['name'] = $row ['name'];
		$pricebooks [] = $tmp;
		unset ( $tmp );
	}
	$data ['pricebooks'] = $pricebooks;
	echo json_encode ( $data );
	unset ( $data );
	unset ( $a );
	unset ( $res );
	return;
}
function generateNumber($stock_id, $date) {
	$data = array ();
	$data ['number'] = EcmInvoiceOut::generateNumber ( $stock_id, $date );
	$data ['document_no'] = EcmInvoiceOut::formatNumber ( $data ['number'], $stock_id );
	echo json_encode ( $data );
	unset ( $data );
	return;
}
function generateNumberCorrect() {
	$data = array ();
	$data ['number'] = EcmInvoiceOut::generateNumberCorrect ();
	$data ['document_no'] = EcmInvoiceOut::formatNumberCorrect ( $data ['number'] );
	echo json_encode ( $data );
	unset ( $data );
	return;
}
function searchProducts($searchKey, $searchCategory, $searchStock, $searchSort) {
	$db = $GLOBALS ['db'];
	$q = "SELECT id, code, name FROM ecmproducts WHERE
					(UPPER(code) LIKE '%$searchKey%' OR
					UPPER(name) LIKE '%$searchKey%')
					AND deleted='0' ";
	if ($searchCategory && $searchCategory != "")
		$q .= "AND product_category_id='$searchCategory' ";
	
	if ($searchSort == '1')
		$q .= "ORDER BY code";
	else if ($searchSort == '2')
		$q .= "ORDER BY code DESC";
	else if ($searchSort == '3')
		$q .= "ORDER BY name";
	else if ($searchSort == '4')
		$q .= "ORDER BY name DESC";
	
	$q .= " LIMIT 0,50";
	
	$res = $db->query ( $q );
	$result = array ();
	
	if ($searchStock != '1') {
		// get main stock id
		$stock_res = $db->fetchByAssoc ( $db->query ( "SELECT id FROM ecmstocks WHERE main='1' LIMIT 0,1" ) );
		$stock_id = $stock_res ['id'];
	}
	while ( $row = $db->fetchByAssoc ( $res ) ) {
		$tmp = array ();
		$tmp ['id'] = $row ['id'];
		$tmp ['name'] = $row ['name'];
		$tmp ['code'] = $row ['code'];
		
		// get stock if necessary
		if ($searchStock != '1') {
			$tmp ['stock_state'] = EcmStockOperation::getStock ( $row ['id'], $stock_id );
		}
		
		if ($searchStock == '3' && $tmp ['stock_state'] == 0)
			continue; // don't show null stock
		
		$result [] = $tmp;
	}
	echo json_encode ( $result );
	return;
}
function getProduct($id, $pricebook_id, $account_id, $language, $stock_id) {
	$db = $GLOBALS ['db'];
	$p = $db->fetchByAssoc ( $db->query ( "SELECT p.id, p.code,p.OO, p.name,p.product_category_id, p.is_consignment, v.id as ecmvat_id, v.name as ecmvat_name, v.value as ecmvat_value,p.ks_group, p.ean, p.ean2, p.unit_id FROM ecmproducts as p INNER JOIN ecmvats as v ON v.id=p.vat_id WHERE p.id='$id'" ) );
	global $app_list_strings;
	$p ['unit_name'] = $app_list_strings ['ecmproducts_unit_dom'] [$p ['unit_id']];
	$p ['unit_precision'] = $app_list_strings ['ecmproducts_unit_dom_precision'] [$p ['unit_id']];
	if ($stock_id != '') {
		$p ['stock_state'] = EcmStockOperation::getStock ( $p ['id'], $stock_id );
	}
	// try get recipient_code and price from pricebook
	if ($pricebook_id && $pricebook_id != '') {
		$pr = $db->fetchByAssoc ( $db->query ( "SELECT price, recipient_code FROM ecmpricebooks_ecmproducts WHERE ecmpricebook_id='$pricebook_id' AND ecmproduct_id='$id' AND deleted='0'" ) );
		$p ['price_start'] = $pr ['price'];
		$p ['recipient_code'] = $pr ['recipient_code'];
	}
	if (! $p ['price_start'] || floatval ( $p ['price_start'] ) == 0) {
		// try price from default prices
		$pr = $db->fetchByAssoc ( $db->query ( "SELECT pp.price FROM ecmprices_ecmproducts AS pp INNER JOIN ecmprices AS p ON pp.ecmprice_id=p.id INNER JOIN accounts AS a ON p.id=a.ecmprice_id WHERE a.id='$account_id' AND pp.ecmproduct_id='$id'" ) );
		$p ['price_start'] = $pr ['price'];
	}
	$a = new Account ();
	$a->retrieve ( $account_id );
	
	if ($a->invoice_type == 'U' || $a->invoice_type == 'E') {
		$p ['ecmvat_id'] = '9b783d21-5548-6653-e1d6-49610eb3f9dd';
		$p ['ecmvat_name'] = '0%';
		$p ['ecmvat_value'] = '0';
	}
	if ($language == 'en_us') {
		$r = $db->fetchByAssoc ( $db->query ( "SELECT short_description FROM ecmproduct_language WHERE ecmproduct_id='$id' AND language='en'" ) );
		$p ['name'] = htmlspecialchars_decode ( $r ['short_description'] );
		
		$lists = return_app_list_strings_language ( $language );
		if($p ['unit_id']=='1'){
			$p ['unit_name']='pcs';
		} else {
		$p ['unit_name'] = $lists ['ecmproducts_unit_dom'] [$p ['unit_id']];
		}
		unset ( $lists );
	}
	echo json_encode ( $p );
	return;
}
function getItems($record, $type) {
	$of = new EcmInvoiceOut ();
	$of->retrieve ( $record );
	$pl = $of->getPositionList ( true, $type );
	unset ( $of );
	echo json_encode ( $pl );
	return;
}
function getItemsFromWZ($record, $type) {
	$lists = explode ( '@', $record );
	$items = array ();
	$of = new EcmStockDocOut ();
	$poz = 0;
	foreach ( $lists as $list ) {
		
		$of->retrieve ( $list );
		$pl = $of->getPositionList ( true );
		foreach ( $pl as $pls ) {
			$pls ['position'] = $poz;
			$items [] = $pls;
			$poz ++;
		}
	}
	
	unset ( $of );
	echo json_encode ( $items );
	return;
}
function getItemsFromSale($record, $type) {
	$of = new EcmSale ();
	$of->retrieve ( $record );
	$pl = $of->getPositionList ( true );
	unset ( $of );
	echo json_encode ( $pl );
	return;
}
function getCategoriesList() {
	$db = $GLOBALS ['db'];
	$res = $db->query ( "SELECT id, name FROM ecmproductcategories WHERE deleted='0'" );
	$result = array ();
	while ( $row = $db->fetchByAssoc ( $res ) ) {
		$tmp = array ();
		$tmp ['id'] = $row ['id'];
		$tmp ['name'] = $row ['name'];
		$result [] = $tmp;
	}
	echo json_encode ( $result );
	return;
}
function getTranslation($product_id, $language, $unit_id) {
	$db = $GLOBALS ['db'];
	$result = array ();
	if ($language == 'en_us') {
		$r = $db->fetchByAssoc ( $db->query ( "SELECT short_description FROM ecmproduct_language WHERE ecmproduct_id='$product_id' AND language='en'" ) );
		$result ['name'] = htmlspecialchars_decode ( $r ['short_description'] );
	} else if ($language == 'pl_pl') {
		$p = new EcmProduct ();
		$p->retrieve ( $product_id );
		$result ['name'] = htmlspecialchars_decode ( $p->name );
		unset ( $p );
	}
	$lists = return_app_list_strings_language ( $language );
	$result ['unit_name'] = $lists ['ecmproducts_unit_dom'] [$unit_id];
	unset ( $lists );
	
	echo json_encode ( $result );
	return;
}
function getPricesInfo($product_id, $pricebook_id, $account_id) {
	$db = $GLOBALS ['db'];
	
	$result = array ();
	if ($pricebook_id && $pricebook_id != '') {
		// try get price from pricebook
		$res = $db->fetchByAssoc ( $db->query ( "SELECT price FROM ecmpricebooks_ecmproducts WHERE ecmpricebook_id='$pricebook_id' AND ecmproduct_id='$product_id' AND deleted='0'" ) );
		if ($res ['price'] && $res ['price'] != '' && $res ['price'] != 0) {
			$tmp = array ();
			$tmp ['name'] = 'pricebook';
			$tmp ['price'] = $res ['price'];
			$result [] = $tmp;
		}
	}
	
	// get from ecmprices
	$res = $db->query ( "SELECT p.name, pp.price FROM ecmprices_ecmproducts AS pp 
					   INNER JOIN ecmprices AS p
					   ON p.id=pp.ecmprice_id
					   WHERE
					   pp.ecmproduct_id='$product_id'
					   AND pp.price!=0" );
	while ( $row = $db->fetchByAssoc ( $res ) ) {
		$tmp = array ();
		$tmp ['name'] = $row ['name'];
		$tmp ['price'] = $row ['price'];
		$result [] = $tmp;
	}
	
	// get last invoice price
	if ($account_id && $account_id != '') {
		$res = $db->fetchByAssoc ( $db->query ( "SELECT ii.subprice, i.id, i.document_no FROM ecminvoiceoutitems AS ii
										     INNER JOIN ecminvoiceouts AS i
											 ON ii.ecminvoiceout_id=i.id
											 WHERE ii.ecmproduct_id='$product_id'
											 AND i.parent_id='$account_id'
											 AND ii.deleted='0'
											 AND i.deleted='0'
											 AND i.canceled='0'
											 ORDER BY i.register_date DESC
											 LIMIT 0,1" ) );
		if ($res && $res ['subprice'] != '') {
			$tmp = array ();
			$tmp ['name'] = $res ['document_no'];
			$tmp ['price'] = $res ['subprice'];
			$result [] = $tmp;
		}
	}
	
	echo json_encode ( $result );
	return;
}
function getStockArray($product_id) {
	$o = new EcmStockOperation ();
	echo json_encode ( $o->getStockArray ( $product_id ) );
	unset ( $o );
	return;
}
function getPurchaseArray($product_id) {
	$o = new EcmStockOperation ();
	echo json_encode ( $o->getPurchaseArray ( $product_id ) );
	unset ( $o );
	return;
}
	