<?php

	if ( !isset($_POST["json"]) ){ die('kein $_POST["json"]'); }
	ini_set('allow_url_fopen ','ON');

	$JSON = $_POST["json"];
	$arr = json_decode($JSON, true);
	$lieferDatum;
	$lieferPO;
	$lieferTyp;
	$anzahlBoxen;

	$lieferDatum = $arr["boxmesswertekomplett"]["lieferdaten"]["lieferdatum"];
	$lieferPO = $arr["boxmesswertekomplett"]["lieferdaten"]["poNr"];
	$lieferTyp = $arr["boxmesswertekomplett"]["lieferdaten"]["liefertyp"];
	$anzahlBoxen = $arr["boxmesswertekomplett"]["lieferdaten"]["anzahlBoxen"];


	switch(json_last_error()) {
		case JSON_ERROR_NONE:
			if ($arr["func"] == "createXLSX") {
				messwerteZuExcel($arr["boxmesswertekomplett"]);
				print("Ok\n");
			} elseif ($arr["func"] == "aa") { 
				print("nüscht"); 
			}
		break;
		case JSON_ERROR_DEPTH: echo ' - Maximale Stacktiefe überschritten'; break;
		case JSON_ERROR_STATE_MISMATCH: echo ' - Unterlauf oder Nichtübereinstimmung der Modi'; break;
		case JSON_ERROR_CTRL_CHAR: echo ' - Unerwartetes Steuerzeichen gefunden'; break;
		case JSON_ERROR_SYNTAX: echo ' - Syntaxfehler, ungültiges JSON'; break;
		case JSON_ERROR_UTF8: echo ' - Missgestaltete UTF-8 Zeichen, möglicherweise fehlerhaft kodiert'; break;
		default: echo ' - Unbekannter Fehler'; break;
	}



	function messwerteZuExcel($arr){
		#var_dump($arr["lieferdaten"]);

		$inputFile = '';
		$dir = './xlsx_tmp';

		// auswaehlen, welches template (basierend auf den zu liefernden boxen) genutzt werden soll
		switch($GLOBALS["anzahlBoxen"]){
			case 1: $inputFile  = 'testfiles/1.xlsx'; break;
			case 2: $inputFile  = 'testfiles/2.xlsx'; break;
			case 3: $inputFile  = 'testfiles/3.xlsx'; break;
			case 4: $inputFile  = 'testfiles/4.xlsx'; break;
			case 5: $inputFile  = 'testfiles/5.xlsx'; break;
			case 6: $inputFile  = 'testfiles/6.xlsx'; break;
			case 7: $inputFile  = 'testfiles/7.xlsx'; break;
			case 8: $inputFile  = 'testfiles/8.xlsx'; break;
			case 9: $inputFile  = 'testfiles/9.xlsx'; break;
			case 10: $inputFile = 'testfiles/10.xlsx'; break;
			case 11: $inputFile = 'testfiles/11.xlsx'; break;
			case 12: $inputFile = 'testfiles/12.xlsx'; break;
			case 13: $inputFile = 'testfiles/13.xlsx'; break;
			case 14: $inputFile = 'testfiles/14.xlsx'; break;
			case 15: $inputFile = 'testfiles/15.xlsx'; break;
		}

		// Unzip
		$zip = new ZipArchive();
		$zip->open($inputFile);
		if ($zip->extractTo($dir)){
			print("DEBUG: $inputFile extracted to $dir | NrOfBoxes: ". $GLOBALS["anzahlBoxen"] ."\n");
		} else {
			die("Fehler beim entpacken des Templates.");
		}

		require 'includes/boxpos2.php';
		fillMesswertArray($arr);
		writeToXLSX($arr);
	} // end messwerteZuExcel()


	include("includes/createExcelFile.php");

	// print('$nrOfBoxes: '. $nrOfBoxes ."\n");
	// print('$lieferDatum: '. $lieferDatum ."\n");
	// print('$lieferPO: '. $lieferPO ."\n");

	function writeToXLSX($arr){
		error_log("###################### Start writeToXLSX() ######################\n");
		require 'includes/boxpos2.php';

		$m = $arr["lieferdaten"]["anzahlBoxen"]; // m = Menge
		switch($m){
			case 1:
				writeBoxStrings($boxPos1["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				break;
			case 2:
				writeBoxStrings($boxPos2["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos2["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				break;
			case 3:
				writeBoxStrings($boxPos3["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos3["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos3["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				break;
			case 4:
				writeBoxStrings($boxPos4["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos4["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos4["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos4["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				break;
			case 5:
				writeBoxStrings($boxPos5["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos5["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos5["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos5["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos5["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				break;
			case 6:
				writeBoxStrings($boxPos6["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos6["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos6["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos6["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos6["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos6["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				break;
			case 7:
				writeBoxStrings($boxPos7["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos7["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos7["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos7["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos7["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos7["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				writeBoxStrings($boxPos7["box7"], $GLOBALS["box7Messwerte"]); writeBoxMesswerte(7, $GLOBALS["box7Messwerte"]);
				break;
			case 8:
				writeBoxStrings($boxPos8["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos8["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos8["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos8["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos8["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos8["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				writeBoxStrings($boxPos8["box7"], $GLOBALS["box7Messwerte"]); writeBoxMesswerte(7, $GLOBALS["box7Messwerte"]);
				writeBoxStrings($boxPos8["box8"], $GLOBALS["box8Messwerte"]); writeBoxMesswerte(8, $GLOBALS["box8Messwerte"]);
				break;
			case 9:
				writeBoxStrings($boxPos9["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos9["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos9["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos9["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos9["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos9["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				writeBoxStrings($boxPos9["box7"], $GLOBALS["box7Messwerte"]); writeBoxMesswerte(7, $GLOBALS["box7Messwerte"]);
				writeBoxStrings($boxPos9["box8"], $GLOBALS["box8Messwerte"]); writeBoxMesswerte(8, $GLOBALS["box8Messwerte"]);
				writeBoxStrings($boxPos9["box9"], $GLOBALS["box9Messwerte"]); writeBoxMesswerte(9, $GLOBALS["box9Messwerte"]);
				break;
			case 10:
				writeBoxStrings($boxPos10["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos10["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos10["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos10["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos10["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos10["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				writeBoxStrings($boxPos10["box7"], $GLOBALS["box7Messwerte"]); writeBoxMesswerte(7, $GLOBALS["box7Messwerte"]);
				writeBoxStrings($boxPos10["box8"], $GLOBALS["box8Messwerte"]); writeBoxMesswerte(8, $GLOBALS["box8Messwerte"]);
				writeBoxStrings($boxPos10["box9"], $GLOBALS["box9Messwerte"]); writeBoxMesswerte(9, $GLOBALS["box9Messwerte"]);
				writeBoxStrings($boxPos10["box10"], $GLOBALS["box10Messwerte"]); writeBoxMesswerte(10, $GLOBALS["box10Messwerte"]);
				break;
			case 11:
				writeBoxStrings($boxPos11["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos11["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos11["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos11["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos11["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos11["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				writeBoxStrings($boxPos11["box7"], $GLOBALS["box7Messwerte"]); writeBoxMesswerte(7, $GLOBALS["box7Messwerte"]);
				writeBoxStrings($boxPos11["box8"], $GLOBALS["box8Messwerte"]); writeBoxMesswerte(8, $GLOBALS["box8Messwerte"]);
				writeBoxStrings($boxPos11["box9"], $GLOBALS["box9Messwerte"]); writeBoxMesswerte(9, $GLOBALS["box9Messwerte"]);
				writeBoxStrings($boxPos11["box10"], $GLOBALS["box10Messwerte"]); writeBoxMesswerte(10, $GLOBALS["box10Messwerte"]);
				writeBoxStrings($boxPos11["box11"], $GLOBALS["box11Messwerte"]); writeBoxMesswerte(11, $GLOBALS["box11Messwerte"]);
				break;
			case 12:
				writeBoxStrings($boxPos12["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos12["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos12["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos12["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos12["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos12["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				writeBoxStrings($boxPos12["box7"], $GLOBALS["box7Messwerte"]); writeBoxMesswerte(7, $GLOBALS["box7Messwerte"]);
				writeBoxStrings($boxPos12["box8"], $GLOBALS["box8Messwerte"]); writeBoxMesswerte(8, $GLOBALS["box8Messwerte"]);
				writeBoxStrings($boxPos12["box9"], $GLOBALS["box9Messwerte"]); writeBoxMesswerte(9, $GLOBALS["box9Messwerte"]);
				writeBoxStrings($boxPos12["box10"], $GLOBALS["box10Messwerte"]); writeBoxMesswerte(10, $GLOBALS["box10Messwerte"]);
				writeBoxStrings($boxPos12["box11"], $GLOBALS["box11Messwerte"]); writeBoxMesswerte(11, $GLOBALS["box11Messwerte"]);
				writeBoxStrings($boxPos12["box12"], $GLOBALS["box12Messwerte"]); writeBoxMesswerte(12, $GLOBALS["box12Messwerte"]);
				break;
			case 13:
				writeBoxStrings($boxPos13["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos13["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos13["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos13["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos13["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos13["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				writeBoxStrings($boxPos13["box7"], $GLOBALS["box7Messwerte"]); writeBoxMesswerte(7, $GLOBALS["box7Messwerte"]);
				writeBoxStrings($boxPos13["box8"], $GLOBALS["box8Messwerte"]); writeBoxMesswerte(8, $GLOBALS["box8Messwerte"]);
				writeBoxStrings($boxPos13["box9"], $GLOBALS["box9Messwerte"]); writeBoxMesswerte(9, $GLOBALS["box9Messwerte"]);
				writeBoxStrings($boxPos13["box10"], $GLOBALS["box10Messwerte"]); writeBoxMesswerte(10, $GLOBALS["box10Messwerte"]);
				writeBoxStrings($boxPos13["box11"], $GLOBALS["box11Messwerte"]); writeBoxMesswerte(11, $GLOBALS["box11Messwerte"]);
				writeBoxStrings($boxPos13["box12"], $GLOBALS["box12Messwerte"]); writeBoxMesswerte(12, $GLOBALS["box12Messwerte"]);
				writeBoxStrings($boxPos13["box13"], $GLOBALS["box13Messwerte"]); writeBoxMesswerte(13, $GLOBALS["box13Messwerte"]);
				break;
			case 14:
				writeBoxStrings($boxPos14["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos14["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos14["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos14["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos14["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos14["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				writeBoxStrings($boxPos14["box7"], $GLOBALS["box7Messwerte"]); writeBoxMesswerte(7, $GLOBALS["box7Messwerte"]);
				writeBoxStrings($boxPos14["box8"], $GLOBALS["box8Messwerte"]); writeBoxMesswerte(8, $GLOBALS["box8Messwerte"]);
				writeBoxStrings($boxPos14["box9"], $GLOBALS["box9Messwerte"]); writeBoxMesswerte(9, $GLOBALS["box9Messwerte"]);
				writeBoxStrings($boxPos14["box10"], $GLOBALS["box10Messwerte"]); writeBoxMesswerte(10, $GLOBALS["box10Messwerte"]);
				writeBoxStrings($boxPos14["box11"], $GLOBALS["box11Messwerte"]); writeBoxMesswerte(11, $GLOBALS["box11Messwerte"]);
				writeBoxStrings($boxPos14["box12"], $GLOBALS["box12Messwerte"]); writeBoxMesswerte(12, $GLOBALS["box12Messwerte"]);
				writeBoxStrings($boxPos14["box13"], $GLOBALS["box13Messwerte"]); writeBoxMesswerte(13, $GLOBALS["box13Messwerte"]);
				writeBoxStrings($boxPos14["box14"], $GLOBALS["box14Messwerte"]); writeBoxMesswerte(14, $GLOBALS["box14Messwerte"]);
				break;
			case 15:
				writeBoxStrings($boxPos15["box1"], $GLOBALS["box1Messwerte"]); writeBoxMesswerte(1, $GLOBALS["box1Messwerte"]);
				writeBoxStrings($boxPos15["box2"], $GLOBALS["box2Messwerte"]); writeBoxMesswerte(2, $GLOBALS["box2Messwerte"]);
				writeBoxStrings($boxPos15["box3"], $GLOBALS["box3Messwerte"]); writeBoxMesswerte(3, $GLOBALS["box3Messwerte"]);
				writeBoxStrings($boxPos15["box4"], $GLOBALS["box4Messwerte"]); writeBoxMesswerte(4, $GLOBALS["box4Messwerte"]);
				writeBoxStrings($boxPos15["box5"], $GLOBALS["box5Messwerte"]); writeBoxMesswerte(5, $GLOBALS["box5Messwerte"]);
				writeBoxStrings($boxPos15["box6"], $GLOBALS["box6Messwerte"]); writeBoxMesswerte(6, $GLOBALS["box6Messwerte"]);
				writeBoxStrings($boxPos15["box7"], $GLOBALS["box7Messwerte"]); writeBoxMesswerte(7, $GLOBALS["box7Messwerte"]);
				writeBoxStrings($boxPos15["box8"], $GLOBALS["box8Messwerte"]); writeBoxMesswerte(8, $GLOBALS["box8Messwerte"]);
				writeBoxStrings($boxPos15["box9"], $GLOBALS["box9Messwerte"]); writeBoxMesswerte(9, $GLOBALS["box9Messwerte"]);
				writeBoxStrings($boxPos15["box10"], $GLOBALS["box10Messwerte"]); writeBoxMesswerte(10, $GLOBALS["box10Messwerte"]);
				writeBoxStrings($boxPos15["box11"], $GLOBALS["box11Messwerte"]); writeBoxMesswerte(11, $GLOBALS["box11Messwerte"]);
				writeBoxStrings($boxPos15["box12"], $GLOBALS["box12Messwerte"]); writeBoxMesswerte(12, $GLOBALS["box12Messwerte"]);
				writeBoxStrings($boxPos15["box13"], $GLOBALS["box13Messwerte"]); writeBoxMesswerte(13, $GLOBALS["box13Messwerte"]);
				writeBoxStrings($boxPos15["box14"], $GLOBALS["box14Messwerte"]); writeBoxMesswerte(14, $GLOBALS["box14Messwerte"]);
				writeBoxStrings($boxPos15["box15"], $GLOBALS["box15Messwerte"]); writeBoxMesswerte(15, $GLOBALS["box15Messwerte"]);
				break;
		}
		error_log("###################### Ende writeToXLSX() ######################\n");
	}

	function fillMesswertArray($arr){
		error_log("###################### Start fillMesswertArray() ######################\n");
		// print_r($box1Messwerte);

		$m = $GLOBALS["anzahlBoxen"]; // m = Menge
		if ($m >= 1 ) { $GLOBALS["box1Messwerte"] = [ array(), $arr["Box1Pos1"], $arr["Box1Pos2"], $arr["Box1Pos3"], $arr["Box1Pos4"], $arr["Box1Pos5"], $arr["Box1Pos6"], $arr["Box1Pos7"], $arr["Box1Pos8"], $arr["Box1Pos9"] ]; }
		if ($m >= 2 ) { $GLOBALS["box2Messwerte"] = [ array(), $arr["Box2Pos1"], $arr["Box2Pos2"], $arr["Box2Pos3"], $arr["Box2Pos4"], $arr["Box2Pos5"], $arr["Box2Pos6"], $arr["Box2Pos7"], $arr["Box2Pos8"], $arr["Box2Pos9"] ]; }
		if ($m >= 3 ) { $GLOBALS["box3Messwerte"] = [ array(), $arr["Box3Pos1"], $arr["Box3Pos2"], $arr["Box3Pos3"], $arr["Box3Pos4"], $arr["Box3Pos5"], $arr["Box3Pos6"], $arr["Box3Pos7"], $arr["Box3Pos8"], $arr["Box3Pos9"] ]; }
		if ($m >= 4 ) { $GLOBALS["box4Messwerte"] = [ array(), $arr["Box4Pos1"], $arr["Box4Pos2"], $arr["Box4Pos3"], $arr["Box4Pos4"], $arr["Box4Pos5"], $arr["Box4Pos6"], $arr["Box4Pos7"], $arr["Box4Pos8"], $arr["Box4Pos9"] ]; }
		if ($m >= 5 ) { $GLOBALS["box5Messwerte"] = [ array(), $arr["Box5Pos1"], $arr["Box5Pos2"], $arr["Box5Pos3"], $arr["Box5Pos4"], $arr["Box5Pos5"], $arr["Box5Pos6"], $arr["Box5Pos7"], $arr["Box5Pos8"], $arr["Box5Pos9"] ]; }
		if ($m >= 6 ) { $GLOBALS["box6Messwerte"] = [ array(), $arr["Box6Pos1"], $arr["Box6Pos2"], $arr["Box6Pos3"], $arr["Box6Pos4"], $arr["Box6Pos5"], $arr["Box6Pos6"], $arr["Box6Pos7"], $arr["Box6Pos8"], $arr["Box6Pos9"] ]; }
		if ($m >= 7 ) { $GLOBALS["box7Messwerte"] = [ array(), $arr["Box7Pos1"], $arr["Box7Pos2"], $arr["Box7Pos3"], $arr["Box7Pos4"], $arr["Box7Pos5"], $arr["Box7Pos6"], $arr["Box7Pos7"], $arr["Box7Pos8"], $arr["Box7Pos9"] ]; }
		if ($m >= 8 ) { $GLOBALS["box8Messwerte"] = [ array(), $arr["Box8Pos1"], $arr["Box8Pos2"], $arr["Box8Pos3"], $arr["Box8Pos4"], $arr["Box8Pos5"], $arr["Box8Pos6"], $arr["Box8Pos7"], $arr["Box8Pos8"], $arr["Box8Pos9"] ]; }
		if ($m >= 9 ) { $GLOBALS["box9Messwerte"] = [ array(), $arr["Box9Pos1"], $arr["Box9Pos2"], $arr["Box9Pos3"], $arr["Box9Pos4"], $arr["Box9Pos5"], $arr["Box9Pos6"], $arr["Box9Pos7"], $arr["Box9Pos8"], $arr["Box9Pos9"] ]; }
		if ($m >= 10 ) { $GLOBALS["box10Messwerte"] = [ array(), $arr["Box10Pos1"], $arr["Box10Pos2"], $arr["Box10Pos3"], $arr["Box10Pos4"], $arr["Box10Pos5"], $arr["Box10Pos6"], $arr["Box10Pos7"], $arr["Box10Pos8"], $arr["Box10Pos9"] ]; }
		if ($m >= 11 ) { $GLOBALS["box11Messwerte"] = [ array(), $arr["Box11Pos1"], $arr["Box11Pos2"], $arr["Box11Pos3"], $arr["Box11Pos4"], $arr["Box11Pos5"], $arr["Box11Pos6"], $arr["Box11Pos7"], $arr["Box11Pos8"], $arr["Box11Pos9"] ]; }
		if ($m >= 12 ) { $GLOBALS["box12Messwerte"] = [ array(), $arr["Box12Pos1"], $arr["Box12Pos2"], $arr["Box12Pos3"], $arr["Box12Pos4"], $arr["Box12Pos5"], $arr["Box12Pos6"], $arr["Box12Pos7"], $arr["Box12Pos8"], $arr["Box12Pos9"] ]; }
		if ($m >= 13 ) { $GLOBALS["box13Messwerte"] = [ array(), $arr["Box13Pos1"], $arr["Box13Pos2"], $arr["Box13Pos3"], $arr["Box13Pos4"], $arr["Box13Pos5"], $arr["Box13Pos6"], $arr["Box13Pos7"], $arr["Box13Pos8"], $arr["Box13Pos9"] ]; }
		if ($m >= 14 ) { $GLOBALS["box14Messwerte"] = [ array(), $arr["Box14Pos1"], $arr["Box14Pos2"], $arr["Box14Pos3"], $arr["Box14Pos4"], $arr["Box14Pos5"], $arr["Box14Pos6"], $arr["Box14Pos7"], $arr["Box14Pos8"], $arr["Box14Pos9"] ]; }
		if ($m >= 15 ) { $GLOBALS["box15Messwerte"] = [ array(), $arr["Box15Pos1"], $arr["Box15Pos2"], $arr["Box15Pos3"], $arr["Box15Pos4"], $arr["Box15Pos5"], $arr["Box15Pos6"], $arr["Box15Pos7"], $arr["Box15Pos8"], $arr["Box15Pos9"] ]; }
		error_log("###################### Ende fillMesswertArray() ######################\n");
	} // end fillMesswertArray()

	function writeBoxMesswerte($boxNr, $messwerte){
		error_log("###################### Start writeBoxMesswerte() ######################\n");

		#        0     1           2            3       4       5       6      7     8     9    10    11    12     13     14       15     16
		# array("", "1-0100", "GW101050100", 429.52, 429.52, 310.62, 310.62, 3.71, 3.71, 3.71, 3.71, 1234, 987, 422.25, 422.25, 303.30, 303.30)
		$sheet = simplexml_load_file( "xlsx_tmp/xl/worksheets/sheet$boxNr.xml" );

		for ( $i=2; $i<=10; $i++) {
			$sheet->sheetData->row[$i]->c[1]->v  = $messwerte[$i - 1][3];			# OL1
			$sheet->sheetData->row[$i]->c[2]->v  = $messwerte[$i - 1][4];			# Ol2
			$sheet->sheetData->row[$i]->c[3]->v  = $messwerte[$i - 1][5];			# OW1
			$sheet->sheetData->row[$i]->c[4]->v  = $messwerte[$i - 1][6];			# OW2
			$sheet->sheetData->row[$i]->c[5]->v  = $messwerte[$i - 1][7];			# R1
			$sheet->sheetData->row[$i]->c[6]->v  = $messwerte[$i - 1][8];			# R2
			$sheet->sheetData->row[$i]->c[7]->v  = $messwerte[$i - 1][9];			# R3
			$sheet->sheetData->row[$i]->c[8]->v  = $messwerte[$i - 1][10];		# R4
			$sheet->sheetData->row[$i]->c[15]->v = $messwerte[$i - 1][11];		# LW
			$sheet->sheetData->row[$i]->c[16]->v = $messwerte[$i - 1][12];		# CW
			$sheet->sheetData->row[$i]->c[17]->v = $messwerte[$i - 1][13];		# IL1
			$sheet->sheetData->row[$i]->c[18]->v = $messwerte[$i - 1][14];		# IL2
			$sheet->sheetData->row[$i]->c[19]->v = $messwerte[$i - 1][15];		# IW1
			$sheet->sheetData->row[$i]->c[20]->v = $messwerte[$i - 1][16];		# IW2
		}

		$sheet->asXML( "/var/www/TZUdev/newone2/php/testing/xlsx_tmp/xl/worksheets/sheet$boxNr.xml" );
	} // end writeBoxMesswerte()


	function writeBoxStrings($strPos, $messwerte){
		$strings = simplexml_load_file("xlsx_tmp/xl/sharedStrings.xml");
		$strings->si[$strPos[0]]->t = $messwerte[1][1];
		$strings->si[$strPos[1]]->t = $messwerte[2][1];
		$strings->si[$strPos[2]]->t = $messwerte[3][1];
		$strings->si[$strPos[3]]->t = $messwerte[4][1];
		$strings->si[$strPos[4]]->t = $messwerte[5][1];
		$strings->si[$strPos[5]]->t = $messwerte[6][1];
		$strings->si[$strPos[6]]->t = $messwerte[7][1];
		$strings->si[$strPos[7]]->t = $messwerte[8][1];
		$strings->si[$strPos[8]]->t = $messwerte[9][1];
		$strings->si[$strPos[9]]->t = $messwerte[1][2];
		$strings->si[$strPos[10]]->t = $messwerte[2][2];
		$strings->si[$strPos[11]]->t = $messwerte[3][2];
		$strings->si[$strPos[12]]->t = $messwerte[4][2];
		$strings->si[$strPos[13]]->t = $messwerte[5][2];
		$strings->si[$strPos[14]]->t = $messwerte[6][2];
		$strings->si[$strPos[15]]->t = $messwerte[7][2];
		$strings->si[$strPos[16]]->t = $messwerte[8][2];
		$strings->si[$strPos[17]]->t = $messwerte[9][2];
		$strings->asXML('/var/www/TZUdev/newone2/php/testing/xlsx_tmp/xl/sharedStrings.xml');
		error_log("###################### Ende writeBoxMesswerte() ######################\n");
	} // end writeBoxStrings()


	function getString($v, $strings){
		$s  = array();
		$si = $strings->si[(int) $v];
		// Register & alias the default namespace or you'll get empty results in the xpath query
		$si->registerXPathNamespace('n', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
		// Cat together all of the 't' (text?) node values
		foreach($si->xpath('.//n:t') as $t) {
		    $s[] = (string) $t;
		}
		return implode($s);
	}

?>