<?php
if (! defined ( 'sugarEntry' ) || ! sugarEntry)
	die ( '-1' );

if (! $_POST ['job'] || $_POST ['job'] == '')
	die ( '-1' );

switch ($_POST ['job']) {
	case 'getWorkerName' :
		getWorkerName ( $_POST ['worker'] );
		break;
	case 'ecmProductNameAutocomplite':
		ecmProductNameAutocomplite($_POST['name']);
		break;
	case 'ecmProductIndeksAutocomplite':
		ecmProductIndeksAutocomplite($_POST['indeks']);
		break;
	case 'getProductActions':
		getProductActions($_POST['id']);
		break;
	case 'getProductActionsLoad':
		getProductActionsLoad($_POST['id']);
		break;
	case 'getEcmActionsCost':
		getEcmActionsCost($_POST['id']);
		break;
}

function ecmProductNameAutocomplite($name) {
	$db = $GLOBALS ['db'];
	$result = array();

	$nazwa = '';
	if ($name != '') {
		$nazwa = ' AND  UPPER(name) LIKE "%' . $name . '%" ';
	}
	$q = 'SELECT id, name, code FROM ecmproducts WHERE deleted=0 ' . $nazwa . ' ORDER BY name LIMIT 0,50';

	$rs = $db->query($q);
	if ($rs->num_rows > 0) {
		while ($row = $db->fetchByAssoc($rs)) {
			$a_json_row ['id'] = $row ['id'];
			$a_json_row ['ecmproductname'] = $row ['name'];
			$a_json_row ['code'] = $row ['code'];
			$a_json_row ['label'] = "</span>Indeks: <span style='color: blue;'>" . $row ['code'] . "</span><br>Nazwa: <span style='color: blue;'>" . $row ['name'] . '</span>';
			$a_json_row ['value'] = $row ['name'];
			$result [] = $a_json_row;
		}
	} else {
		$a_json_row ['id'] = '';
		$a_json_row ['ecmproductname'] = '';
		$a_json_row ['code'] = '';
		$a_json_row ["label"] = "<span style='color: red;'>Brak produktu o szukanej nazwie</span>";
		$a_json_row ['value'] = '';
		$result [] = $a_json_row;
	}
	$json = json_encode($result);
	print $json;
}

function ecmProductIndeksAutocomplite($indeks){
	$db = $GLOBALS ['db'];
	$result = array();

	$nazwa = '';
	if ($indeks != '') {
		$nazwa = ' AND  UPPER(code) LIKE "%' . $indeks . '%" ';
	}
	$q = 'SELECT id, name, code FROM ecmproducts WHERE deleted=0 ' . $nazwa . ' ORDER BY name LIMIT 0,50';

	$rs = $db->query($q);
	if ($rs->num_rows > 0) {
		while ($row = $db->fetchByAssoc($rs)) {
			$a_json_row ['id'] = $row ['id'];
			$a_json_row ['ecmproductname'] = $row ['name'];
			$a_json_row ['code'] = $row ['code'];
			$a_json_row ['label'] = "</span>Indeks: <span style='color: blue;'>" . $row ['code'] . "</span><br>Nazwa: <span style='color: blue;'>" . $row ['name'] . '</span>';
			$a_json_row ['value'] = $row ['code'];
			$result [] = $a_json_row;
		}
	} else {
		$a_json_row ['id'] = '';
		$a_json_row ['ecmproductname'] = '';
		$a_json_row ['code'] = '';
		$a_json_row ["label"] = "<span style='color: red;'>Brak produktu o szukanym indeksie</span>";
		$a_json_row ['value'] = '';
		$result [] = $a_json_row;
	}
	$json = json_encode($result);
	print $json;
}

function getProductActionsLoad($id){
	$db = $GLOBALS ['db'];
	$result = array();
	$q = 'SELECT CONCAT(a.indeks, " ", a.name) as name, a.id as id FROM ecmproductactions pa, ecmactions a WHERE pa.ecmproduct_id="' . $id . '" AND pa.ecmaction_id = a.id ORDER BY pa.position DESC';
	$rs = $db->query($q);
	if ($rs->num_rows > 0) {
		while ($row = $db->fetchByAssoc($rs)) {
			$a_json_row ['id'] = $row ['id'];
			$a_json_row ['name'] = $row ['name'];
			$result [] = $a_json_row;
		}
	}
	$json = json_encode($result);
	//$json = json_encode($q);
	print $json;
}

function getProductActions($id){
	$db = $GLOBALS ['db'];
	$result = array();
	$q = 'SELECT CONCAT(a.indeks, " ", a.name) as name, a.id as id, a.cost_other as ecmactioncost, a.cost_action as costbrutto FROM ecmproductactions pa, ecmactions a WHERE a.deleted=0 AND pa.ecmproduct_id="' . $id . '" AND pa.ecmaction_id = a.id ORDER BY pa.position DESC';
	
	$rs = $db->query($q);
	if ($rs->num_rows > 0) {
		while ($row = $db->fetchByAssoc($rs)) {
			$a_json_row ['id'] = $row ['id'];
			$a_json_row ['name'] = $row ['name'];
			$a_json_row ['ecmactioncost'] = $row ['ecmactioncost'];
			$a_json_row ['costbrutto'] = $row ['costbrutto'];
			$result [] = $a_json_row;
		}
	}
	$json = json_encode($result);
//	$json = json_encode($q);
	print $json;
}

function getEcmActionsCost($id){
	$db = $GLOBALS ['db'];
	$q = 'SELECT cost_other, cost_action FROM ecmactions WHERE id="' . $id . '"';
	$rs = $db->query($q);
	if ($rs->num_rows > 0) {
		$row = $db->fetchByAssoc($rs);
			$a_json_row['cost_other'] = $row ['cost_other'];
			$a_json_row['cost_action'] = $row ['cost_action'];
	}
	$json = json_encode($a_json_row);
	print $json;
}

?>