<?php
require_once("../../base/php/startSession.php");
require_once("../../base/php//classDatabase.php");
require_once( "../../base/php/Functions.php" );

global $database;
global $action;
global $xmldef;


$action = @$_POST["action"];
if ($action == "") {
	$action = @$_GET["action"];
}

$xmldef = @$_POST["xmldef"];
if ($xmldef == "") {
	$xmldef = @$_GET["xmldef"];
}
$xmldef = @simplexml_load_string($xmldef);

$database = new Database("iCTI");

	$httpString = "";
	$httpString = $httpString . "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	$httpString = $httpString . "\n<HTTP_SERVICE>";

	$oid = floatval($xmldef["oid"]);
	$code = toSQL(utf8_decode($xmldef["code"]));
	$texte = toSQL(utf8_decode($xmldef["texte"]));


	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			
		}
		else {
			
			$sqlcmd = "UPDATE activite.t_budgets SET 
							code = '$code', 
							texte = '$texte'
						WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
			
			
			foreach ($xmldef->PARENT as $parentNode) {
				$oid = floatval($parentNode["oid"]);
				$type = trim($parentNode["type"]);
				$budget_value = floatval($parentNode["budget_value"]);
				$repartition_type = floatval($parentNode["repartition_type"]);
				
				$sqlcmd = "UPDATE activite.t_budgets_saisis SET 
								repartition_type = '$repartition_type'
							WHERE oid = $oid; ";
							
				foreach ($parentNode->ITEM as $itemNode) {
					$sqlcmd = $sqlcmd . "UPDATE activite.t_budgets_saisis SET " . indicatorUpdateQuery($itemNode) . " WHERE oid = $oid; ";
				}

				
				
				$result  =  $database->exec($sqlcmd);
			}

		}
				

	}

	if ($action == "copy") {
	
		

	}


	if ($action == "delete") {
		
	}



$httpString = $httpString . "\n</HTTP_SERVICE>";
 
$httpString = compress64($httpString);


echo "<CTICONTENT>$httpString</CTICONTENT>";


	
function indicatorUpdateQuery($itemNode) {

	$item_indicator = trim($itemNode["item_indicator"]);
	$budget_nb_array_total = floatval($itemNode["budget_value_0"]);
	$budget_nb_array_mois_1 = floatval($itemNode["budget_value_1"]);
	$budget_nb_array_mois_2 = floatval($itemNode["budget_value_2"]);
	$budget_nb_array_mois_3 = floatval($itemNode["budget_value_3"]);
	$budget_nb_array_mois_4 = floatval($itemNode["budget_value_4"]);
	$budget_nb_array_mois_5 = floatval($itemNode["budget_value_5"]);
	$budget_nb_array_mois_6 = floatval($itemNode["budget_value_6"]);
	$budget_nb_array_mois_7 = floatval($itemNode["budget_value_7"]);
	$budget_nb_array_mois_8 = floatval($itemNode["budget_value_8"]);
	$budget_nb_array_mois_9 = floatval($itemNode["budget_value_9"]);
	$budget_nb_array_mois_10 = floatval($itemNode["budget_value_10"]);
	$budget_nb_array_mois_11 = floatval($itemNode["budget_value_11"]);
	$budget_nb_array_mois_12 = floatval($itemNode["budget_value_12"]);
	$sqlcmd = "budget_$item_indicator" . "_total = $budget_nb_array_total, budget_$item_indicator" . "_mois = Array[$budget_nb_array_mois_1, $budget_nb_array_mois_2,$budget_nb_array_mois_3, $budget_nb_array_mois_4, $budget_nb_array_mois_5, $budget_nb_array_mois_6, $budget_nb_array_mois_7, $budget_nb_array_mois_8, $budget_nb_array_mois_9, $budget_nb_array_mois_10, $budget_nb_array_mois_11, $budget_nb_array_mois_12] ";
				
	return $sqlcmd;

}
	

    
?>