<?PHP

include "pplib.inc.php";

header("Content-type: text/html; charset=utf-8");
$err = "";
if (isset($_REQUEST["login"]) && $_REQUEST["login"] == 1 && isset($_REQUEST["mand"]) && isset($_REQUEST["pwd"])) {
	if (isset($PPSESSION)) unset($PPSESSION);
	$mand = trim(pp_postfield("mand"));
	$pwd = trim(pp_postfield("pwd"));
	$sqlcmd = "SELECT * FROM ".$dbname.".pplant WHERE PPLANT_Caption='".ppaddslashes($mand)."' AND PPLANT_PWD='".ppaddslashes(md5($ppconf["plantpwd_bloat"].$pwd.$ppconf["plantpwd_bloat"]))."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($myrow = mysqli_fetch_array($result)) {
		$PPSESSION = md5(mt_rand().$ppconf["session_bloat1"].mt_rand().$ppconf["session_bloat2"].date("Y-m-d H:i:s").$ppconf["session_bloat1"].mt_rand().$ppconf["session_bloat2"]);
		$PPSESSION .= md5(mt_rand().$ppconf["session_bloat2"].mt_rand().$ppconf["session_bloat1"].date("Y-m-d H:i:s").$ppconf["session_bloat2"].mt_rand().$ppconf["session_bloat1"]);
		$stmp = time() - 1;
		$sqlcmd = "UPDATE ".$dbname.".pplant SET PPLANT_AdmSession='".ppaddslashes($PPSESSION)."', PPLANT_AdmSessionStamp='".ppaddslashes($stmp)."' WHERE PPLANT_ID='".ppaddslashes($myrow["PPLANT_ID"])."' LIMIT 1";
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd); 
	}
	// overwrite browser history to prevent repost of credentials
	pp_openhead();
	$lnk = $ppconf["htroot"]."admin.php?PPSESSION=".$PPSESSION;
	echo "</HEAD><BODY>";
	echo "<SCRIPT language=\"JavaScript\">\n";
	echo "document.location.replace(\"".$lnk."\");\n";
	echo "</SCRIPT>\n";
	echo "<A href=\"".$lnk."\"><SMALL>".htmlspecialchars($lnk)."</SMALL></A>";
	die ("</BODY></HTML>");
	
} elseif (isset($_REQUEST["logout"])) {
	pp_loginbysession();
	$sqlcmd = "UPDATE $dbname.pplant SET PPLANT_AdmSession='' WHERE PPLANT_ID='".ppaddslashes($PPLANTID)."' LIMIT 1";
	mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	$PPLANTID = 0;
	pp_loginbysession();
}
pp_loginbysession();

$dbaction = pp_postfield("dbaction");
$draw = pp_postfield("draw");

function pp_mkcookierand() {
	mt_srand(time() + time());
	$chrs = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567";
	$cookieauth = "";
	for ($i=0;$i<64;$i++) {
		$rnd = mt_rand();
		$rnd = ($rnd % 64);
		$cookieauth .= $chrs[$rnd];
	}
	return $cookieauth;	
}
if ($dbaction == "sethostcookie") {
	$sqlcmd = "SELECT * FROM $dbname.pplant WHERE PPLANT_ID='$PPLANTID' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo mysqli_error($ppconf["mysqli_linkid"]);
	if ($myrow = mysqli_fetch_array($result)) {
		$cookieauth = $myrow["PPLANT_CookieAuth"];
		if ($cookieauth == "") {
			$cookieauth = pp_mkcookierand();
			$sqlcmd = "UPDATE $dbname.pplant SET PPLANT_CookieAuth='".ppaddslashes($cookieauth)."' WHERE PPLANT_ID='".ppaddslashes($PPLANTID)."' LIMIT 1";
			mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		}
		setcookie("PPLANTCOOKIEAUTH",$cookieauth,time()+60*60*24*365,$ppconf["cookiepath"],$ppconf["cookiedomain"],false);
		$PPLANTCOOKIEAUTH=$cookieauth;
		echo sprintf($pplang["ADM_COOKIE_ACK"], htmlspecialchars($cookieauth));		
	}
}
pp_openhead();
if ($draw != "barcode") {
	echo "</HEAD><BODY>\n";
	pp_admtitle();
}
if (isset($dbaction) && $dbaction == "resethostcookie") {
	$sqlcmd = "SELECT * FROM $dbname.pplant WHERE PPLANT_ID='$PPLANTID' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo mysqli_error($ppconf["mysqli_linkid"]);
	if ($myrow = mysqli_fetch_array($result)) {
		$cookieauth = pp_mkcookierand();
		$sqlcmd = "UPDATE $dbname.pplant SET PPLANT_CookieAuth='".ppaddslashes($cookieauth)."' WHERE PPLANT_ID='".ppaddslashes($PPLANTID)."' LIMIT 1";
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		echo "<B>".$pplang["ADM_RESETCOOKIE_ACK"]."</B><BR>";
	}
	
}


if (isset($dbaction) && $dbaction == "createworker") {
	$wrkbarcode = "".mt_rand().mt_rand().mt_rand().mt_rand().mt_rand().mt_rand().mt_rand().mt_rand().mt_rand().mt_rand().mt_rand().mt_rand();
	$WORKER_Barcode = "9".sprintf("%02d",$PPLANTID).substr($wrkbarcode,0,7);
	$WORKER_Name = trim(pp_postfield("WORKER_Name"));
	if (strlen($WORKER_Name) < 1) {
		echo "<SPAN style=\"color:red;\"><B>".$pplang["ADM_SAVEWORKER_NAMEERR"]."</B></SPAN><BR><BR>";
		$draw = "addworker";
	} else {
		$sqlcmd = "INSERT INTO $dbname.pworker(WORKER_PlantPTR, WORKER_Name, WORKER_Barcode) VALUES ('".ppaddslashes($PPLANTID)."', '".ppaddslashes($WORKER_Name)."', '".ppaddslashes($WORKER_Barcode)."')";
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	}
} elseif (isset($dbaction) && $dbaction == "saveworker") {
	$workerid = (int)pp_postfield("workerid");
	$WORKER_Name = trim(pp_postfield("WORKER_Name"));
	$WORKER_Invisible = (int)trim(pp_postfield("WORKER_Invisible"));
	$WORKER_StdWorkHoursPerDay = pp_parsedecimal(pp_postfield("WORKER_StdWorkHoursPerDay"));
	if (strlen($WORKER_Name) < 1) {
		echo "<SPAN style=\"color:red;\"><B>".$pplang["ADM_SAVEWORKER_NAMEERR"]."</B></SPAN><BR><BR>";
		$draw = "edworker";
	} else {
		$sqlcmd = "UPDATE $dbname.pworker SET"
		." WORKER_Name='".ppaddslashes($WORKER_Name)."'"
		.", WORKER_Invisible='".ppaddslashes($WORKER_Invisible)."'"
		.", WORKER_StdWorkHoursPerDay='".ppaddslashes($WORKER_StdWorkHoursPerDay)."'"
		." WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".addslashes($PPLANTID)."' LIMIT 1";
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	}
} elseif (isset($dbaction) && $dbaction == "savecontract") {
	$workerid = (int)pp_postfield("workerid");
	$contractid = (int)pp_postfield("contractid");
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {			
		$err = "";
		if (!pp_parsedate(pp_postfield("CONTRACT_Start"), $CONTRACT_Start2)) {
			$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_STARTDATE"]."</B></SPAN>";
		} elseif (!pp_parsedate(pp_postfield("CONTRACT_End"), $CONTRACT_End2)) {
			$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_ENDDATE"]."</B></SPAN>";
		} else {
			$CONTRACT_Name = trim(pp_postfield("CONTRACT_Name"));
			$CONTRACT_Text = pp_postfield("CONTRACT_Text");
			$CONTRACT_Type = (int)pp_postfield("CONTRACT_Type");
			$fmCONTRACT_Type = (int)pp_postfield("fmCONTRACT_Type");			
			
			$sqlcmd = "UPDATE $dbname.pcontract SET"
			." CONTRACT_Name='".ppaddslashes($CONTRACT_Name)."'"
			.", CONTRACT_Text='".ppaddslashes($CONTRACT_Text)."'"
			.", CONTRACT_Start='".ppaddslashes($CONTRACT_Start2)."'"
			.", CONTRACT_End='".ppaddslashes($CONTRACT_End2)."'"
			.", CONTRACT_Type='".ppaddslashes($CONTRACT_Type)."'";
			
			if ($fmCONTRACT_Type == 1) {
				$CONTRACT_T1_DestHours = (int)trim(pp_postfield("CONTRACT_T1_DestHours"));
				$sqlcmd .= ",CONTRACT_T1_DestHours='".ppaddslashes($CONTRACT_T1_DestHours)."'";
			} elseif ($fmCONTRACT_Type == 2) {
				$CONTRACT_T2_DestHours = (int)trim(pp_postfield("CONTRACT_T2_DestHours"));
				$CONTRACT_T2_VacationDays = (int)trim(pp_postfield("CONTRACT_T2_VacationDays"));
				$sqlcmd .= ",CONTRACT_T2_DestHours='".ppaddslashes($CONTRACT_T2_DestHours)."', CONTRACT_T2_VacationDays='".ppaddslashes($CONTRACT_T2_VacationDays)."'";
			}
			
			$sqlcmd .= " WHERE CONTRACT_ID='".ppaddslashes($contractid)."' AND CONTRACT_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."' LIMIT 1";
			if ($err == "") {
				mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
				echo mysqli_error($ppconf["mysqli_linkid"]);
			}
		}
		$draw = "contracts";
		if (pp_postfield("redrw") == "1") {
			$draw = "edcontract";
		}
	}
	mysqli_free_result($wrkres);
} elseif (isset($dbaction) && $dbaction == "setadmdefaultcontract") {
	$workerid = (int)pp_postfield("workerid");
	$contractid = (int)pp_postfield("contractid");
	$sqlcmd = "SELECT pworker.* FROM $dbname.pworker INNER JOIN $dbname.pcontract ON pcontract.CONTRACT_ID='".ppaddslashes($contractid)."' AND pcontract.CONTRACT_WorkerPTR='".ppaddslashes($workerid)."'"
	." WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {
		$sqlcmd = "UPDATE $dbname.pworker SET WORKER_AdminDefaultContractPTR='".ppaddslashes($contractid)."' WHERE WORKER_ID='".ppaddslashes($workerrow["WORKER_ID"])."' LIMIT 1";
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		$draw = "contracts";
	}
	mysqli_free_result($wrkres);
} elseif (isset($dbaction) && $dbaction == "delcontract") {
	$workerid = (int)pp_postfield("workerid");
	$contractid = (int)pp_postfield("contractid");
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {
		$sqlcmd = "DELETE FROM $dbname.pcontract WHERE CONTRACT_ID='".ppaddslashes($contractid)."' AND CONTRACT_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."' LIMIT 1"; 
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		echo mysqli_error($ppconf["mysqli_linkid"]);
		$draw = "contracts";
	}
	mysqli_free_result($wrkres);
} elseif (isset($dbaction) && $dbaction == "createcontract") {
	$workerid = (int)pp_postfield("workerid");
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {
		$daty = date("Y");
		$dat = date("Y-m-d");
		$sqlcmd = "INSERT INTO $dbname.pcontract (CONTRACT_WorkerPTR, CONTRACT_Name, CONTRACT_Start, CONTRACT_End, CONTRACT_Type)"
		." VALUES "
		."('".ppaddslashes($workerrow["WORKER_ID"])."', '".ppaddslashes($pplang["ADM_NEWCONTRACTDEFAULTNAME"])."', '".ppaddslashes($dat)."', '".ppaddslashes($dat)."', '1')";
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		echo mysqli_error($ppconf["mysqli_linkid"]);
		$draw = "contracts";		
	}
} elseif (isset($dbaction) && $dbaction == "ureghost") {
	$newpwd = md5("slhf".mt_rand().mt_rand().mt_rand().mt_rand().mt_rand());
	$sqlcmd = "UPDATE $dbname.pplant SET PPLANT_HostPWD='".ppaddslashes($newpwd)."' WHERE PPLANT_ID='".ppaddslashes($PPLANTID)."' LIMIT 1";
	mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	$draw = "none";
	echo sprintf($pplang["ADM_HRT_CHANGED"],htmlspecialchars($newpwd)).".<BR><BR><BR>";
	echo $pplang["ADM_HRT_CMDHDR"]."<BR><BR>";
	echo "curl -o tmp.txt &quot;".$ppconf["htroot"]."reghost.php?mand=".rawurlencode($PPLANTCAPTION)."&auth=".rawurlencode($newpwd)."&quot;";

	//echo "<BR><BR><BR>Auth-Parameter Autorisation durch ShiftyPrinter ActivePoll ist nun:<BR><BR>";
	//echo "mand=".rawurlencode($PPLANTCAPTION)."&auth=".rawurlencode($newpwd);
	
} elseif (isset($dbaction) && $dbaction == "savepr") {
	
	$workerid = (int)pp_postfield("workerid");
	$PR_ID = (int)pp_postfield("PR_ID");
		
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($myrow = mysqli_fetch_array($result)) {
		$PR_Come2 = "0000-00-00 00:00:00";
		$PR_Go2 = "0000-00-00 00:00:00";
		if (!pp_parsedatetime(pp_postfield("PR_Come"), $PR_Come2)) {
			$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_COMEDATE"]."</B></SPAN>";
		} else {
			$PR_Go = trim(pp_postfield("PR_Go"));
			if (strlen($PR_Go) > 0 && !pp_parsedatetime(pp_postfield("PR_Go"), $PR_Go2)) {
				$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_GODATE"]."</B></SPAN>";
			} else {
				$comestmp = strtotime($PR_Come2);
				if (strlen($PR_Go) == 0) {
					$PR_Go2 = "0000-00-00 00:00:00";
					$PR_Minutes = 0;
				} else {				
					$gostmp = strtotime($PR_Go2);
					$PR_Minutes = (int)round(($gostmp - $comestmp) / 60);
				}
				$PR_Year = date("Y",$comestmp);
				$PR_Month = date("m",$comestmp);
				$PR_Day = date("d",$comestmp);
				$sqlcmd = "UPDATE $dbname.ppresence SET"
				." PR_Come='".ppaddslashes($PR_Come2)."'"
				.", PR_Go='".ppaddslashes($PR_Go2)."'"
				.", PR_Minutes='".ppaddslashes($PR_Minutes)."'"
				.", PR_Year='".ppaddslashes($PR_Year)."'"
				.", PR_Month='".ppaddslashes($PR_Month)."'"
				.", PR_Day='".ppaddslashes($PR_Day)."'"
				." WHERE PR_ID='".ppaddslashes($PR_ID)."' AND PR_WorkerPTR='".ppaddslashes($workerid)."' LIMIT 1";
				mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			}
		}
	}	
	mysqli_free_result($result);	
} elseif (isset($dbaction) && $dbaction == "crtadj") {
	$workerid = (int)pp_postfield("workerid");

	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($myrow = mysqli_fetch_array($result)) {
		if (!pp_parsedatetime(pp_postfield("ADJ_Date"), $ADJ_Date2)) {
			$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_DATE"]."</B></SPAN>";
		} else {
			$ADJ_PlantPTR = $PPLANTID;
			$ADJ_WorkerPTR = $myrow["WORKER_ID"];
			$ADJ_Minutes = (int)trim(pp_postfield("ADJ_Minutes"));
			$ADJ_Text = trim(pp_postfield("ADJ_Text"));
			$ADJ_LastModified = date("Y-m-d H:i:s");
			$sqlcmd = "INSERT INTO $dbname.padjustment(ADJ_PlantPTR, ADJ_WorkerPTR, ADJ_Date, ADJ_Minutes, ADJ_Text, ADJ_LastModified)"
			." VALUES "
			."('".ppaddslashes($ADJ_PlantPTR)."'"
			.", '".ppaddslashes($ADJ_WorkerPTR)."'"
			.", '".ppaddslashes($ADJ_Date2)."'"
			.", '".ppaddslashes($ADJ_Minutes)."'"
			.", '".ppaddslashes($ADJ_Text)."'"
			.", '".ppaddslashes($ADJ_LastModified)."')";
			mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			$greenadj = mysqli_insert_id($ppconf["mysqli_linkid"]);			
		}
	}
	mysqli_free_result($result);
} elseif (isset($dbaction) && $dbaction == "saveadj") {
	$workerid = (int)pp_postfield("workerid");
	$ADJ_ID = (int)pp_postfield("ADJ_ID");
	
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($myrow = mysqli_fetch_array($result)) {
		if (!pp_parsedatetime(pp_postfield("ADJ_Date"), $ADJ_Date2)) {
			$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_DATE"]."</B></SPAN>";
		} else {
			$ADJ_Minutes = (int)trim(pp_postfield("ADJ_Minutes"));
			$ADJ_Text = trim(pp_postfield("ADJ_Text"));
			$ADJ_LastModified = date("Y-m-d H:i:s");
			$sqlcmd = "UPDATE $dbname.padjustment SET"
			." ADJ_Minutes='".ppaddslashes($ADJ_Minutes)."'"
			.", ADJ_Date='".ppaddslashes($ADJ_Date2)."'"
			.", ADJ_Text='".ppaddslashes($ADJ_Text)."'"
			.", ADJ_LastModified='".ppaddslashes($ADJ_LastModified)."'"
			." WHERE ADJ_ID='".ppaddslashes($ADJ_ID)."' AND ADJ_WorkerPTR='".ppaddslashes($myrow["WORKER_ID"])."' LIMIT 1";
			mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		}
	}
	mysqli_free_result($result);
	
} elseif (isset($dbaction) && $dbaction == "holidaysaddweekends") {
	$year = (int)trim(pp_postfield("year"));
	if ($year >= 2000 && $year <= 2999) {
		$t = strtotime("$year-01-01 12:00:00");
		for ($d = 0;$d < 370;$d++) {
			if (0 + date("Y",$t) > $year) break;
			$date = date("Y-m-d",$t);
			$wd = date("w",$t);
			if ($wd == 0 || $wd == 6) {
				// Samstag oder Sonntag
				$sqlcmd = "DELETE FROM $dbname.pholiday WHERE HOLIDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND HOLIDAY_Date='".ppaddslashes($date)."' LIMIT 1";
				mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
				echo mysqli_error($ppconf["mysqli_linkid"]);
				$sqlcmd = "INSERT INTO $dbname.pholiday (HOLIDAY_PlantPTR, HOLIDAY_Date, HOLIDAY_Description)"
				." VALUES "
				."('".ppaddslashes($PPLANTID)."', '".ppaddslashes($date)."', '".ppaddslashes($pplang["ADM_WEEKENDNAME"])."')";
				mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
				echo mysqli_error($ppconf["mysqli_linkid"]);
			}			
			$t += 24*60*60;
		}
	}
} elseif (isset($dbaction) && $dbaction == "holidaysdelyear") {
	$year = (int)trim(pp_postfield("year"));
	if ($year >= 2000 && $year <= 2999) {
		$sqlcmd = "DELETE FROM $dbname.pholiday WHERE HOLIDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND HOLIDAY_Date >='{$year}-01-01' AND HOLIDAY_Date <='{$year}-12-31'";
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		echo mysqli_error($ppconf["mysqli_linkid"]);		
	}

} elseif (isset($dbaction) && $dbaction == "addholiday") {
	if (!pp_parsedate(pp_postfield("HOLIDAY_Date"), $HOLIDAY_Date2)) {
		$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_DATE"]."</B></SPAN>";
	} else {
		$HOLIDAY_Description= trim(pp_postfield("HOLIDAY_Description"));
		$sqlcmd = "DELETE FROM $dbname.pholiday WHERE HOLIDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND HOLIDAY_Date='".ppaddslashes($HOLIDAY_Date2)."' LIMIT 1";
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		echo mysqli_error($ppconf["mysqli_linkid"]);
		$sqlcmd = "INSERT INTO $dbname.pholiday (HOLIDAY_PlantPTR, HOLIDAY_Date, HOLIDAY_Description)"
		." VALUES "
		."('".ppaddslashes($PPLANTID)."', '".ppaddslashes($HOLIDAY_Date2)."', '".ppaddslashes($HOLIDAY_Description)."')";
		mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		echo mysqli_error($ppconf["mysqli_linkid"]);
		
		$err = "<SPAN style=\"color:#008000;\">".sprintf($pplang["ADM_HDY_ADDED"],date($ppconf["datefmt"], strtotime($HOLIDAY_Date2))."/".htmlspecialchars($HOLIDAY_Description))."</SPAN>";
	}
} elseif (isset($dbaction) && $dbaction == "delholiday") {
	$hi = (int)pp_postfield("hi");
	$sqlcmd = "DELETE FROM $dbname.pholiday WHERE HOLIDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND HOLIDAY_ID='".ppaddslashes($hi)."' LIMIT 1";
	mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo mysqli_error($ppconf["mysqli_linkid"]);
	$draw = "holidaylist";
} elseif (isset($dbaction) && $dbaction == "setvac") {
	$w = (int)pp_postfield("w");
	if (!pp_parsedate(trim(pp_postfield("day")), $day2)) {
		$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_DATE"]."</B></SPAN>";
	} else {
		$sqlcmd = "SELECT WORKER_ID, WORKER_PlantPTR FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($w)."' AND WORKER_PlantPTR='".addslashes($PPLANTID)."' LIMIT 1";
		$wresult = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		if ($row = mysqli_fetch_assoc($wresult)) {
			$sqlcmd = "INSERT INTO $dbname.pvacationday(VACDAY_PlantPTR, VACDAY_WorkerPTR, VACDAY_Date, VACDAY_Lastmodified)"
			." VALUES "
			."('".ppaddslashes($PPLANTID)."'"
			.", '".ppaddslashes($row["WORKER_ID"])."'"
			.", '".ppaddslashes($day2)."'"
			.", '".ppaddslashes(date("Y-m-d H:i:s"))."')";
			mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		}
		mysqli_free_result($wresult);
		$draw = "calendar";
		$ppioREQUEST["year"] = substr($day2,0,4);
	}
} elseif (isset($dbaction) && $dbaction == "clrvac") {
	$w = (int)pp_postfield("w");
	if (!pp_parsedate(trim(pp_postfield("day")), $day2)) {
		$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_DATE"]."</B></SPAN>";
	} else {
		$sqlcmd = "SELECT WORKER_ID, WORKER_PlantPTR FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($w)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
		$wresult = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		if ($row = mysqli_fetch_assoc($wresult)) {
			$sqlcmd = "DELETE FROM $dbname.pvacationday WHERE VACDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND VACDAY_WorkerPTR='".ppaddslashes($row["WORKER_ID"])."' AND VACDAY_Date='".ppaddslashes($day2)."' LIMIT 1";
			mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		}
		mysqli_free_result($wresult);
		$draw = "calendar";
		$ppioREQUEST["year"] = substr($day2,0,4);
	}
} elseif (isset($dbaction) && $dbaction == "setsick") {
	$w = (int)pp_postfield("w");
	if (!pp_parsedate(trim(pp_postfield("day")), $day2)) {
		$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_DATE"]."</B></SPAN>";
	} else {
		$sqlcmd = "SELECT WORKER_ID, WORKER_PlantPTR, WORKER_StdWorkHoursPerDay FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($w)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
		$wresult = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		if ($row = mysqli_fetch_assoc($wresult)) {						
			$sqlcmd = "DELETE FROM $dbname.psickday WHERE SICKDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND SICKDAY_WorkerPTR='".ppaddslashes($row["WORKER_ID"])."' AND SICKDAY_Date='".ppaddslashes($day2)."' LIMIT 1";
			mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			
			// Anzahl Arbeitsstunden
			$wh = round(pp_countworkhours($w, $day2, $day2) * 60.0);
			$std= round($row["WORKER_StdWorkHoursPerDay"] * 60.0);
			$miss = (int)round($std - $wh);
			if ($miss > 0) {
				$sqlcmd = "INSERT INTO $dbname.psickday(SICKDAY_PlantPTR, SICKDAY_WorkerPTR, SICKDAY_Date, SICKDAY_Minutes, SICKDAY_LastModified)"
				." VALUES "
				."('".ppaddslashes($PPLANTID)."'"
				.", '".ppaddslashes($row["WORKER_ID"])."'"
				.", '".ppaddslashes($day2)."'"
				.", '".ppaddslashes($miss)."'"
				.", '".ppaddslashes(date("Y-m-d H:i:s"))."')";
				mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			}
		}
		mysqli_free_result($wresult);
		$draw = "calendar";
		$ppioREQUEST["year"] = substr($day2,0,4);
	}
} elseif (isset($dbaction) && $dbaction == "clrsick") {
	$w = (int)pp_postfield("w");
	if (!pp_parsedate(trim(pp_postfield("day")), $day2)) {
		$err = "<SPAN style=\"color:red;\"><B>".$pplang["ADM_ERR_DATE"]."</B></SPAN>";
	} else {
		$sqlcmd = "SELECT WORKER_ID, WORKER_PlantPTR, WORKER_StdWorkHoursPerDay FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($w)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
		$wresult = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		if ($row = mysqli_fetch_assoc($wresult)) {						
			$sqlcmd = "DELETE FROM $dbname.psickday WHERE SICKDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND SICKDAY_WorkerPTR='".ppaddslashes($row["WORKER_ID"])."' AND SICKDAY_Date='".ppaddslashes($day2)."' LIMIT 1";
			mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		}
		mysqli_free_result($wresult);
		$draw = "calendar";
		$ppioREQUEST["year"] = substr($day2,0,4);
	}	
}


if ($draw == "addworker") {
	$WORKER_Name = trim(pp_postfield("WORKER_Name"));
	echo "<FORM method=post action=\"admin.php?PPSESSION=$PPSESSION\">";
	echo ppformelemts();
	echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"createworker\">";
	echo "<TABLE border=0 cellpadding=0 cellspacing=5><TR><TD colspan=2><B>".$pplang["ADM_ADDWORKER_TITLE"].":</B><BR>&nbsp;</TD></TR>\n";
	echo "<TR><TD>".$pplang["ADM_WORKER_NAME"].":</TD><TD><INPUT type=\"text\" name=\"WORKER_Name\" size=40 maxlength=80 value=\"".htmlspecialchars($WORKER_Name)."\"></TD></TR>\n";
	echo "<TR><TD></TD><TD>&nbsp;<BR><INPUT type=\"submit\" value=\"".$pplang["ADM_FORM_CREATE"]."\"></TD></TR>\n";
	echo "</TABLE>";	
	echo "</FORM>";
} elseif ($draw == "edworker") {
	$workerid = trim(pp_postfield("workerid"));
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($myrow = mysqli_fetch_array($result)) {
		$WORKER_Name = $myrow["WORKER_Name"];
		$WORKER_Invisible = $myrow["WORKER_Invisible"];
		$WORKER_StdWorkHoursPerDay = number_format($myrow["WORKER_StdWorkHoursPerDay"],2,$ppconf["decimalseparator"],"");
		echo "<FORM method=post action=\"admin.php?PPSESSION=$PPSESSION\">";
		echo ppformelemts();
		echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"saveworker\">";
		echo "<INPUT type=\"hidden\" name=\"workerid\" value=\"".$myrow["WORKER_ID"]."\">";
		echo "<TABLE border=0 cellpadding=0 cellspacing=5><TR><TD colspan=2><B>".$pplang["ADM_EDWORKER_TITLE"].":</B><BR>&nbsp;</TD></TR>\n";
		echo "<TR><TD>".$pplang["ADM_WORKER_NAME"].":</TD><TD><INPUT type=\"text\" name=\"WORKER_Name\" size=\"40\" maxlength=\"80\" value=\"".htmlspecialchars($WORKER_Name)."\"></TD></TR>\n";
		echo "<TR><TD>".$pplang["ADM_VISIBLE"].":</TD><TD><SELECT name=\"WORKER_Invisible\">\n";
		echo "<OPTION value=\"0\"".($WORKER_Invisible=="0"?" selected":"").">".$pplang["ADM_YES"]."</OPTION>\n";
		echo "<OPTION value=\"1\"".($WORKER_Invisible=="1"?" selected":"").">".$pplang["ADM_NO"]."</OPTION>\n";
		echo "</SELECT></TD></TR>\n";
		echo "<TR><TD>".$pplang["ADM_StdWorkHoursPerDay"]."</TD><TD><INPUT type=\"text\" size=\"20\" name=\"WORKER_StdWorkHoursPerDay\" value=\"".htmlspecialchars($WORKER_StdWorkHoursPerDay)."\"></TD></TR>\n";
		echo "<TR><TD></TD><TD>&nbsp;<BR><INPUT type=\"submit\" value=\"".$pplang["ADM_FORM_SAVE"]."\"></TD></TR>\n";
		echo "</TABLE>";	
		echo "</FORM>";
	}
// added 13-12-2013 by KAZ
} elseif ($draw == "showreport") {
    $dateFrom = $_POST['frmdateFROM'];
    $dateTo = date("Y-m-d", strtotime($_POST['frmdateTO']." 12:00:00"));
    $year = date("Y",strtotime($dateTo));
    if (empty($_POST['chknames'])) {echo "out1"; exit();}
if ($_POST['printtype']=="XLS") {
/******* Export to Excel ******/
require_once './Excel/Writer.php';
$wb = new Spreadsheet_Excel_Writer();
$wb->send('export.xls');
$ws =& $wb->addWorksheet('diary');
}
	echo "<FORM id=\"fm\" method=\"post\" action=\"admin.php\">";
	echo ppformelemts();
	echo "<INPUT type=\"hidden\" name=\"PPSESSION\" value=\"$PPSESSION\">";
	echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"\">";
	echo "<INPUT type=\"hidden\" name=\"w\" value=\"\">";
	echo "<INPUT type=\"hidden\" name=\"day\" value=\"\">";
	echo "</FORM>";
    pp_drawjsreports();
    foreach($_POST['chknames'] as $check)
    {
        $sqlcmd = "SELECT WORKER_Name, WORKER_StdWorkHoursPerDay FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' AND WORKER_ID='".$check."'";
        $result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
        $row = mysqli_fetch_row($result); 
        echo "<b>".$row[0]."</b><br>\n"; 
        $worker["vaccount"] = 0; $worker["sickcount"] = 0; $worker["m_wh"] = 0.0; $worker["m_sh"] = 0.0; $worker["m_xh"] = 0.0;
		$worker["y_wh"] = 0.0; $worker["y_sh"] = 0.0; $worker["y_xh"] = 0.0;
        mysqli_free_result($result);
        $stamp = strtotime($dateFrom." 12:00:00");
        $dat = date("Y-m-d",$stamp);
        $today = date("Y-m-d");
        $lastmonth = "0";
        //echo "<table>\n";
        echo "<table><tr><td valign=top>\n"; //Try month next to other
        while (true) 
        {
            $month = (int)date("m",$stamp);
			$last_day_found = false; if ($dateTo == date("Y-m-d", $stamp)) $last_day_found = true; //Σταματάνε οι εκτυπώσεις μία μέρα πριν την επιλεγόμενη και όχι στο τέλος της χρονιάς.
            if (($month != $lastmonth) || $last_day_found)
            {
                if (($lastmonth != "0") || $last_day_found) // Αθροίσματα μηνών. Πρώτη γραμμή μετά την τελευταία μέρα κάθε μήνα
                {
                    echo "<TR><TH>".sprintf($pplang["ADM_CAL_MONTHSUM"],$pplang["monthnameslong"][$lastmonth])."</TH>";
					echo "<TH>";
					echo number_format($worker["m_wh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_WORK"]." "; // A
					echo number_format($worker["m_sh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_SICK"]." "; // K
					echo number_format($worker["m_xh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_ADJ"]." "; // X
                    echo "<TR><TD>&nbsp;</TD></TR>\n";
					echo "</table></td><td valign=top>\n"; //Try month next to other
                }
                if ($last_day_found) //(substr($dat, 0, 4) != $year) // Τυπώνει Ετήσιο Άθροισμα στο τέλος.
                {
                    echo "<TR><TH>".sprintf($pplang["ADM_CAL_YEARSUM"], "".$year)."</TH>";
                    echo "<TH>";
                    echo number_format($worker["y_wh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_WORK"]." ";
                    echo number_format($worker["y_sh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_SICK"]." ";
                    echo number_format($worker["y_xh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_ADJ"]." ";
                    echo "<TR><TD>&nbsp;</TD></TR>\n</table><br>\n"; //Try month next to other
                    break;
                }
				echo "<td><table>\n"; //Try month next to other
                echo "<TR><TH>".htmlspecialchars($pplang["monthnameslong"][$month])."</TH>";
				echo "<TH></TH>";
				$worker["m_wh"] = 0.0;
				$worker["m_sh"] = 0.0;
				$worker["m_xh"] = 0.0;
                echo "</TR>\n";
                $lastmonth = $month;
            }
			echo "<TR>";
			echo "<TH>".htmlspecialchars(date($ppconf["datefmt"],$stamp)." ".$pplang["weekdays"][date("w",$stamp)])."</TH>";
//			echo "<TH>".htmlspecialchars(date("d.m.Y",$stamp)." ".$pplang["weekdays"][date("w",$stamp)])."</TH>";
			$sqlcmd = "SELECT HOLIDAY_Description FROM $dbname.pholiday WHERE HOLIDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND HOLIDAY_Date='".ppaddslashes($dat)."' LIMIT 1";
			$qtres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			$holiday = false;
			if ($qtrow = mysqli_fetch_assoc($qtres)) { $holiday = true; } 
			mysqli_free_result($qtres);
			$worker["vacation"] = 0;
			$worker["sick"] = 0;
			$sqlcmd = "SELECT VACDAY_ID, VACDAY_WorkerPTR FROM $dbname.pvacationday WHERE VACDAY_WorkerPTR='".$check."' AND VACDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND VACDAY_Date='".ppaddslashes($dat)."'";
			$vres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			$row = mysqli_fetch_assoc($vres);
			$worker["vacation"] = $row["VACDAY_ID"];
			mysqli_free_result($vres);
			$sqlcmd = "SELECT SICKDAY_WorkerPTR, SICKDAY_Minutes FROM $dbname.psickday WHERE SICKDAY_WorkerPTR='".$check."' AND SICKDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND SICKDAY_Date='".ppaddslashes($dat)."'";
			$sres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			while ($row = mysqli_fetch_assoc($sres)) { $worker["sick"] += $row["SICKDAY_Minutes"]; }
			mysqli_free_result($sres);
		
		 
			$sqlcmd = "SELECT SUM(PR_Minutes) AS wrkm FROM $dbname.ppresence WHERE PR_WorkerPTR='".$check."' AND PR_Come>='".ppaddslashes($dat)." 00:00:00' AND PR_Come<='".ppaddslashes($dat)." 23:59:59'";
			$wh = pp_countworkhours($check, $dat, $dat);	
			$sh = $worker["sick"] / 60.0;
			$xh = pp_countadjustmenthours($check, $dat, $dat);	
			$ph = $wh + $sh;
			$vacation = $worker["vacation"] > 0?true:false;
			$bgc = "#C0C0C0";
			$worker["m_wh"] += $wh; $worker["m_sh"] += $sh; $worker["m_xh"] += $xh;
			$worker["y_wh"] += $wh; $worker["y_sh"] += $sh; $worker["y_xh"] += $xh;
			if ($ph > 0)
            {
				if ($vacation) { $bgc = "#FF8080"; } // ergasia mesa stis diakopes
				if ($holiday && $sh > 0.0) { $bgc = "#FF8080"; } // arrostia stis diakopes
				if ($sh > 0 && round($ph*60) != round($worker["WORKER_StdWorkHoursPerDay"]*60)) { $bgc = "#FF8080"; }
            }
            else
            {
				if ($holiday) { $bgc = "#808080"; } elseif ($vacation) { $bgc = "#C0C0FF"; } else { if ($dat < $today) $bgc = "#FFFF00"; }
				if ($holiday && $vacation) { $bgc = "#FF8080"; }
			}
			if ($wh < 0 || $sh < 0) { $bgc = "#FF0000";	}
			echo "<TD bgcolor=\"$bgc\" nowrap>";
			
			if ($worker["vacation"] == 0) {
				echo "<A href=\"#\" onclick=\"return sv(".$check.", '".date("d-m-Y",$stamp)."', $month);\" oncontextmenu=\"return false;\">".$pplang["ADM_CAL_SUFFIX_VAC"]."</A>";
			} else {
				echo "<A href=\"#\" onclick=\"return cv(".$check.", '".date("d-m-Y",$stamp)."', $month);\" oncontextmenu=\"return false;\"><B>".$pplang["ADM_CAL_SUFFIX_VAC"]."</B></A>";
				$worker["vaccount"] += 1;
			}
			echo " ".number_format($wh, 1, ".","")."A";
			if ($worker["sick"] == 0.0) {
				echo " <A href=\"#\" onclick=\"return ss(".$check.", '".date("d-m-Y",$stamp)."', $month);\" oncontextmenu=\"return false;\">".$pplang["ADM_CAL_SUFFIX_SICK"]."</A>";
			} else {
				echo " <A href=\"#\" onclick=\"return cs(".$check.", '".date("d-m-Y",$stamp)."', $month);\" oncontextmenu=\"return false;\">".number_format($sh, 1, ".","").$pplang["ADM_CAL_SUFFIX_SICK"]."</A>";
				$worker["sickcount"] += 1;
			}
			if ($xh != 0.0) { echo " <SPAN style=\"color:#800000;\"><B>".number_format($xh, 1, ".","").$pplang["ADM_CAL_SUFFIX_ADJ"]."</B></SPAN>";}
			echo "</TD>";
		
			// if ($holiday) { echo "<TH>".htmlspecialchars($qtrow["HOLIDAY_Description"])."</TH>\n"; } // Ονομασία αργίας
            echo "</TR>\n";
            $stamp += 24*60*60;
            $dat = date("Y-m-d",$stamp);
        }
		echo "<table>\n";
		echo "<TR><TH>".$pplang["ADM_CAL_TBLVACDAYS"]."</TH>";
		echo "<TH>".htmlspecialchars($worker["vaccount"])."</TH>";
		echo "</TR>\n";
		echo "<TR><TH>".$pplang["ADM_CAL_TBLSICKDAYS"]."</TH>";
		echo "<TH>".htmlspecialchars($worker["sickcount"])."</TH>";
		echo "</TR>\n";
		echo "</TABLE>";
        echo "<hr><br><br>\n";
    }
if ($_POST['printtype']=="XLS") {
$wb->close();    
}
// added 20-11-2013 by KAZ
} elseif ($draw == "reports") {
    require_once('calendar/classes/tc_date.php'); //KAZ required for reports
    require_once('calendar/classes/tc_calendar.php'); //KAZ required for reports
	echo "<FORM method=post action=\"admin.php?draw=showreport&PPSESSION=$PPSESSION\" >";
	// echo "<FORM method=post action=\"reports.php?plant=".$PPLANTID."\" target=\"_new\" >";
//	echo "<FORM method=post action=\"forms.php?plant=".$PPLANTID."\" target=\"_new\" >"; //test pdf
	echo ppformelemts();
//	echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"showreport\">";
	echo "<table><tr><td>";
	//choose workers
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' ORDER BY WORKER_Name ASC";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	$rowcolor = 0;
	while ($myrow = mysqli_fetch_array($result))
	{
		echo "<input type=\"checkbox\" name=\"chknames[]\" value=\"".ppaddslashes($myrow["WORKER_ID"])."\" >".htmlspecialchars($myrow["WORKER_Name"])."<br>\n";
	}
	mysqli_free_result($result);
echo "<br>\n<input type=\"checkbox\" onchange=\"checkAll()\" name=\"chknames[]\" value=\"Select All\" />\n";
echo "<script language=\"javascript\">\n";
echo "function checkAll(){var checkboxes = new Array();\n";
echo "checkboxes = document.getElementsByTagName('input');\n";
// echo "var what= true;\n alert what;\n what = checkboxes[0].getAttribute('checked');\n";
echo "for (var i=0;i<checkboxes.length;i++){\n";
echo "if (checkboxes[i].type=='checkbox'){\n";
echo "checkboxes[i].setAttribute('checked', true);";
echo "}}}\n";
echo "</script>\n";
	echo "</td><td>";
	echo "<table><tr><td>";
	//choose months
	echo "FROM DATE =</td><td> ";
	$myCalendarpub = new tc_calendar("frmdateFROM", true, false);
	$myCalendarpub->setIcon("calendar/images/iconCalendar.gif");
	$myCalendarpub->setDate(date('d'), date('m'), date('Y'));
	$myCalendarpub->setPath("calendar/");
	$myCalendarpub->setYearInterval(2000, 2015);
	//$myCalendarpub->dateAllow('2008-05-13', '2015-03-01');
	$myCalendarpub->setDateFormat('d-m-Y');
	//$myCalendarpub->setAlignment('left', 'bottom');
	$myCalendarpub->writeScript();
	echo "</td></tr>\n<tr><td>";
	
	echo "TO DATE = </td><td>";
	$myCalendarpub = new tc_calendar("frmdateTO", true, false);
	$myCalendarpub->setIcon("calendar/images/iconCalendar.gif");
	$myCalendarpub->setDate(date('d'), date('m'), date('Y'));
	$myCalendarpub->setPath("calendar/");
	$myCalendarpub->setYearInterval(2000, 2015);
	//$myCalendarpub->dateAllow('2008-05-13', '2015-03-01');
	$myCalendarpub->setDateFormat('d-m-Y');
	//$myCalendarpub->setAlignment('left', 'bottom');
	$myCalendarpub->writeScript();
	echo "</td></tr></table>"; //second column of main table
	echo "</td><td>";
    //select PDF, XLS, SCREEN
    echo "<select  name=\"printtype\" >\n";
    echo "<option value=\"XLS\">XLS</option>\n";
    echo "<option value=\"SCR\" selected>SCREEN</option>\n";
    echo "</select>\n";
	echo "<INPUT type=\"submit\" value=\" [SHOW_REPORT] \">";
	echo "</td></tr></table>";
	echo "</FORM>";

	echo "<BR><BR>";
	// $sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' ORDER BY WORKER_Name ASC";


	
// added 15-11-2013 by KAZ
} elseif ($draw == "delworker") {
	$workerid = trim(pp_postfield("workerid"));
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($myrow = mysqli_fetch_array($result)) {
		$WORKER_Name = $myrow["WORKER_Name"];
		//$WORKER_Invisible = $myrow["WORKER_Invisible"];
		//$WORKER_StdWorkHoursPerDay = number_format($myrow["WORKER_StdWorkHoursPerDay"],2,$ppconf["decimalseparator"],"");
		echo "<FORM method=post action=\"admin.php?PPSESSION=$PPSESSION\">";
		echo ppformelemts();
		echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"delworkerconfirmed\">";
		echo "<INPUT type=\"hidden\" name=\"workerid\" value=\"".$myrow["WORKER_ID"]."\">";
		echo $pplang["ADM_FORM_REALLYDEL"].htmlspecialchars($WORKER_Name)."<BR>\n";
		echo "<INPUT type=\"submit\" value=\"".$pplang["ADM_YES"]."\">";
		echo "</FORM>";
	}
} elseif ($dbaction == "delworkerconfirmed") {
	$workerid = trim(pp_postfield("workerid"));
	$sqlcmd = "DELETE FROM $dbname.psickday WHERE SICKDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND SICKDAY_WorkerPTR='".ppaddslashes($workerid)."'";
	mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo "Table psickday updated.<br>";
	$sqlcmd = "DELETE FROM $dbname.padjustment WHERE ADJ_PlantPTR='".ppaddslashes($PPLANTID)."' AND ADJ_WorkerPTR='".ppaddslashes($workerid)."'";
	mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo "Table padjustment updated.<br>";
	$sqlcmd = "DELETE FROM $dbname.pvacationday WHERE VACDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND VACDAY_WorkerPTR='".ppaddslashes($workerid)."'";
	mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo "Table pvacationday updated.<br>";
	$sqlcmd = "DELETE FROM $dbname.pcontract WHERE CONTRACT_WorkerPTR='".ppaddslashes($workerid)."'";
	mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo "Table pcontract updated.<br>";
	$sqlcmd = "DELETE FROM $dbname.ppresence WHERE PR_WorkerPTR='".ppaddslashes($workerid)."'";
	mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo "Table ppresence updated.<br>";
	$sqlcmd = "DELETE FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' LIMIT 1";
	mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo "Table pworker updated.<br>";



} elseif ($draw == "barcode") {
	$workerid = trim(pp_postfield("workerid"));
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($myrow = mysqli_fetch_array($result)) {
		echo "</HEAD><BODY style=\"margin:0px; padding:0px; spacing:0px;\">";
		echo "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"1\">";
		echo "<TR><TD align=center><U>".htmlspecialchars(strtoupper($PPLANTCAPTION))."</U></TD></TR>\n";
		echo "<TR><TD align=center></TD></TR>\n";
		echo "<TR><TD align=center></TD></TR>\n";
		echo "<TR><TD align=center><IMG width=\"220\" height=\"80\" src=\"adminbc.php?workerid=".rawurlencode($myrow["WORKER_ID"])."&amp;PPSESSION=$PPSESSION\" border=0></TD></TR>\n";
		echo "<TR><TD align=center></TD></TR>\n";
		echo "<TR><TD align=center></TD></TR>\n";
		$wbs = "";
		for ($i=0;$i < strlen($myrow["WORKER_Barcode"]); $i++) {
			$wbs.=htmlspecialchars($myrow["WORKER_Barcode"][$i])."&nbsp;";
		}
		echo "<TR><TD align=center nowrap><B>*&nbsp;$wbs*</B></TD></TR>\n";
		echo "<TR><TD align=center></TD></TR>\n";
		echo "<TR><TD align=center></TD></TR>\n";
		echo "<TR><TD align=center>".htmlspecialchars($myrow["WORKER_Name"])."</TD></TR>\n";
		echo "</TABLE>";
	}
} elseif ($draw == "times") {
	$workerid = trim(pp_postfield("workerid"));
	$drwspc = trim(pp_postfield("drwspc"));
	$y = trim(pp_postfield("y"));
	$m = trim(pp_postfield("m"));
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {
		if ($drwspc=="") {
			$sqlcmd = "SELECT PR_WorkerPTR, PR_Year, SUM(PR_Minutes) AS mins FROM $dbname.ppresence WHERE PR_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."' GROUP BY PR_WorkerPTR, PR_Year ORDER BY PR_Year DESC";
			$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			echo "<B>".$pplang["ADM_ATT_PERYEAR"].": ".htmlspecialchars($workerrow["WORKER_Name"])."</B><BR>";
			echo "<TABLE border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
			echo "<TR><TH>".$pplang["ADM_ATT_TBLYEAR"]."</TH><TH>".$pplang["ADM_ATT_TBLMINUTES"]."</TH><TH>".$pplang["ADM_ATT_TBLHOURS"]."</TH></TR>\n";
			$rowcolor = 0;
			while ($myrow = mysqli_fetch_array($result)) {
				$rowc = $ppconf["html_rowc".$rowcolor]; $rowcolor = 1 - $rowcolor;
				echo "<TR><TD bgcolor=$rowc><A href=\"admin.php?PPSESSION=$PPSESSION&amp;draw=times&workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;drwspc=y&amp;y=".$myrow["PR_Year"]."\">".$myrow["PR_Year"]."</A></TD>";
				echo "<TD bgcolor=$rowc align=right>".$myrow["mins"]."</TD>";
				$h = round($myrow["mins"]*10.0 / 60.0) / 10.0;
				$h = number_format($h,1,$ppconf["decimalseparator"],"");
				echo "<TD bgcolor=$rowc align=right>$h</TD>";
				echo "</TR>\n";
			}
			mysqli_free_result($result);
			echo  "</TABLE>";
			echo "<A href=\"admin.php?PPSESSION=$PPSESSION\">".$pplang["ADM_MENU"]."</A>";
		} elseif ($drwspc == "y") {
			$sqlcmd = "SELECT PR_WorkerPTR, PR_Year, PR_Month, SUM(PR_Minutes) AS mins FROM $dbname.ppresence WHERE PR_WorkerPTR='".$workerrow["WORKER_ID"]."' AND PR_Year='".ppaddslashes($y)."' GROUP BY PR_WorkerPTR, PR_Year, PR_Month ORDER BY PR_Month ASC";
			$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			echo "<B>".$pplang["ADM_ATT_PERMONTH"].": ".htmlspecialchars($workerrow["WORKER_Name"])."</B><BR>";
			echo "<TABLE border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
			echo "<TR><TH>".$pplang["ADM_ATT_TBLYEAR"]."</TH><TH>".$pplang["ADM_ATT_TBLMONTH"]."</TH><TH>".$pplang["ADM_ATT_TBLMINUTES"]."</TH><TH>".$pplang["ADM_ATT_TBLHOURS"]."</TH></TR>\n";
			$rowcolor = 0;
			while ($myrow = mysqli_fetch_array($result)) {
				$rowc = $ppconf["html_rowc".$rowcolor]; $rowcolor = 1 - $rowcolor;
				echo "<TR><TD bgcolor=$rowc>".$myrow["PR_Year"]."</TD>";
				echo "<TD bgcolor=$rowc align=center><A href=\"admin.php?PPSESSION=$PPSESSION&amp;draw=times&workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;drwspc=m&amp;y=".$myrow["PR_Year"]."&amp;m=".$myrow["PR_Month"]."\">".$myrow["PR_Month"]."</A></TD>";
				echo "<TD bgcolor=$rowc align=right>".$myrow["mins"]."</TD>";
				$h = round($myrow["mins"]*10.0 / 60.0) / 10.0;
				$h = number_format($h,1,$ppconf["decimalseparator"],"");
				echo "<TD bgcolor=$rowc align=right>$h</TD>";
				echo "</TR>\n";
			}
			mysqli_free_result($result);
			echo  "</TABLE>";
			echo "<A href=\"admin.php?PPSESSION=$PPSESSION\">".$pplang["ADM_MENU"]."</A>";
			echo " &nbsp; <A href=\"admin.php?PPSESSION=$PPSESSION&amp;draw=times&workerid=".rawurlencode($workerrow["WORKER_ID"])."\">".$pplang["ADM_ATT_SHOWPERYEAR"]."</A>";
		} elseif ($drwspc == "m") {
			$sqlcmd = "SELECT PR_WorkerPTR, PR_Year, PR_Month, PR_Day, SUM(PR_Minutes) AS mins FROM $dbname.ppresence WHERE PR_WorkerPTR='".$workerrow["WORKER_ID"]."' AND PR_Year='".ppaddslashes($y)."' AND PR_Month='".ppaddslashes($m)."' GROUP BY PR_WorkerPTR, PR_Year, PR_Month, PR_Day ORDER BY PR_Day ASC";
			$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			echo "<B>".$pplang["ADM_ATT_PERDAY"].": ".htmlspecialchars($workerrow["WORKER_Name"])."</B><BR>";
			echo "<TABLE border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
			echo "<TR><TH>".$pplang["ADM_ATT_TBLYEAR"]."</TH><TH>".$pplang["ADM_ATT_TBLMONTH"]."</TH><TH>".$pplang["ADM_ATT_TBLDAY"]."</TH><TH>".$pplang["ADM_ATT_TBLMINUTES"]."</TH><TH>".$pplang["ADM_ATT_TBLHOURS"]."</TH></TR>\n";
			$rowcolor = 0;
			while ($myrow = mysqli_fetch_array($result)) {
				$rowc = $ppconf["html_rowc".$rowcolor]; $rowcolor = 1 - $rowcolor;
				echo "<TR><TD bgcolor=$rowc>".$myrow["PR_Year"]."</TD>";
				echo "<TD bgcolor=$rowc align=center>".$myrow["PR_Month"]."</TD>";
				echo "<TD bgcolor=$rowc align=center>".$myrow["PR_Day"]."</TD>";
				echo "<TD bgcolor=$rowc align=right>".$myrow["mins"]."</TD>";
				$h = round($myrow["mins"]*10.0 / 60.0) / 10.0;
				$h = number_format($h,1,$ppconf["decimalseparator"],"");
				echo "<TD bgcolor=$rowc align=right>$h</TD>";
				echo "</TR>\n";
			}
			mysqli_free_result($result);
			echo  "</TABLE>";
			echo "<A href=\"admin.php?PPSESSION=$PPSESSION\">".$pplang["ADM_MENU"]."</A>";
			echo " &nbsp; <A href=\"admin.php?PPSESSION=$PPSESSION&amp;draw=times&workerid=".rawurlencode($workerrow["WORKER_ID"])."\">".$pplang["ADM_ATT_SHOWPERYEAR"]."</A>";
			echo " &nbsp; <A href=\"admin.php?PPSESSION=$PPSESSION&amp;draw=times&workerid=".rawurlencode($workerrow["WORKER_ID"])."&drwspc=y&y=".rawurlencode($y)."\">".$pplang["ADM_ATT_SHOWPERMONTH"]."</A>";
		}
	}
} elseif ($draw == "journal") {
	$workerid = trim(pp_postfield("workerid"));
	$lim0 = (int)pp_postfield("lim0");
	$edpr = (int)pp_postfield("edpr");
    
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {
		
		echo "<B>".sprintf($pplang["ADM_JOURNAL_HEADER"], htmlspecialchars($workerrow["WORKER_Name"]))."</B><BR><BR>";		
		$lim1 = $lim0 + 101;
		$cnt = 0;
		$sqlcmd = "SELECT COUNT(PR_ID) AS cnt FROM $dbname.ppresence WHERE PR_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."'";
		$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		if ($cntrow = mysqli_fetch_array($result)) {
			if (isset($cntrow["cnt"])) $cnt = (int)(0 + $cntrow["cnt"]);
		}
		mysqli_free_result($result);
		for ($i=0;$i <= $cnt;$i+=100) {
			$i2 = $i+100;
			if ($i2 > $cnt) $i2 = $cnt;
			echo "<A href=\"admin.php?draw=journal&amp;workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;lim0=".rawurlencode($i)."&amp;PPSESSION=$PPSESSION\">";
			echo $i == $lim0 ? ("<B>[".($i+1)."-".($i2)."]</B>") : ("[".($i+1)."-".($i2)."]");
			echo "</A> ";
		}
		if (strlen($err) > 0) echo "<BR><BR>".$err."<BR><BR>";
		$sqlcmd = "SELECT * FROM $dbname.ppresence WHERE PR_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."' ORDER BY PR_Come DESC LIMIT $lim0, $lim1";
		$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		echo "<TABLE border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
		echo "<TR><TH>".$pplang["ADM_JOURNAL_CAME"]."</TH><TH>".$pplang["ADM_JOURNAL_LEFT"]."</TH><TH>".$pplang["ADM_JOURNAL_MINUTES"]."</TH></TR>\n";
		$rowcolor = 0;
		while ($myrow = mysqli_fetch_array($result)) {
			$rowc = $ppconf["html_rowc".$rowcolor]; $rowcolor = 1 - $rowcolor;
			if ($myrow["PR_Minutes"] < 0) $rowc = "\"#FF8080\"";
			if ($myrow["PR_Go"] == "0000-00-00 00:00:00") $rowc = "\"#FF8080\"";
			if (isset($greenpr) && $greenpr == $myrow["PR_ID"]) $rowc = $ppconf["html_green"];
			$come = date($ppconf["datetimefmt"],strtotime($myrow["PR_Come"]));
			$go = "";
			if ($myrow["PR_Go"] != "0000-00-00 00:00:00") {
				$go = date($ppconf["datetimefmt"],strtotime($myrow["PR_Go"]));
			}
			if ($edpr == $myrow["PR_ID"]) {
				echo "<TR>"; $rowc = $ppconf["html_red"];
				echo "<FORM method=\"post\" action=\"admin.php?PPSESSION=$PPSESSION\">";
				echo ppformelemts();
				echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"savepr\">";
				echo "<INPUT type=\"hidden\" name=\"draw\" value=\"journal\">";
				echo "<INPUT type=\"hidden\" name=\"workerid\" value=\"".htmlspecialchars($workerrow["WORKER_ID"])."\">";
				echo "<INPUT type=\"hidden\" name=\"lim0\" value=\"".htmlspecialchars($lim0)."\">";
				echo "<INPUT type=\"hidden\" name=\"PR_ID\" value=\"".htmlspecialchars($myrow["PR_ID"])."\">";
				echo "<INPUT type=\"hidden\" name=\"greenpr\" value=\"".htmlspecialchars($myrow["PR_ID"])."\">";
				echo "<TD bgcolor=$rowc align=center><INPUT type=\"text\" name=\"PR_Come\" value=\"".htmlspecialchars($come)."\"></TD>";
				echo "<TD bgcolor=$rowc align=center><INPUT type=\"text\" name=\"PR_Go\" value=\"".htmlspecialchars($go)."\"></TD>";
				echo "<TD colspan=2><INPUT type=submit value=\"".$pplang["ADM_FORM_SAVE"]."\"></TD>";
				echo "</FORM></TR>\n";
			} else {	
				echo "<TR>";
				echo "<TD bgcolor=$rowc align=center>".htmlspecialchars($come)."</TD>";
				echo "<TD bgcolor=$rowc align=center>".htmlspecialchars($go)."</TD>";
				echo "<TD bgcolor=$rowc align=center>".htmlspecialchars($myrow["PR_Minutes"])."</TD>";
				echo "</TR>\n";
			}
		}
		mysqli_free_result($result);
		echo "</TABLE>";
	}
	mysqli_free_result($wrkres);
} elseif ($draw == "ajournal") {	
	$workerid = trim(pp_postfield("workerid"));
	$lim0 = (int)pp_postfield("lim0");
	$edadj = (int)pp_postfield("edadj");
	
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {
		echo "<B>".sprintf($pplang["ADM_AJOURNAL_HEADER"], htmlspecialchars($workerrow["WORKER_Name"]))."</B><BR><BR>";
		$lim1 = $lim0 + 101;
		$cnt = 0;
		$sqlcmd = "SELECT COUNT(ADJ_ID) AS cnt FROM $dbname.padjustment WHERE ADJ_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."'";
		$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		if ($cntrow = mysqli_fetch_array($result)) {
			if (isset($cntrow["cnt"])) $cnt = (int)(0 + $cntrow["cnt"]);
		}
		mysqli_free_result($result);
		for ($i=0;$i <= $cnt;$i+=100) {
			$i2 = $i+100;
			if ($i2 > $cnt) $i2 = $cnt;
			echo "<A href=\"admin.php?draw=ajournal&amp;workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;lim0=".rawurlencode($i)."&amp;PPSESSION=$PPSESSION\">";
			echo $i == $lim0 ? ("<B>[".($i+1)."-".($i2)."]</B>") : ("[".($i+1)."-".($i2)."]");
			echo "</A> ";
		}
		if (strlen($err) > 0) echo "<BR><BR>".$err."<BR><BR>";
		$sqlcmd = "SELECT * FROM $dbname.padjustment WHERE ADJ_WorkerPTR='".ppaddslashes($workerrow["WORKER_ID"])."' ORDER BY ADJ_Date DESC LIMIT $lim0, $lim1";
		$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		echo "<TABLE border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
		echo "<TR><TH>".$pplang["ADM_AJOURNAL_TBLDAY"]."</TH><TH>".$pplang["ADM_AJOURNAL_TBLMINS"]."</TH><TH>".$pplang["ADM_AJOURNAL_TBLTEXT"]."</TH></TR>\n";
		$rowcolor = 0;
		while ($myrow = mysqli_fetch_array($result)) {
			$rowc = $ppconf["html_rowc".$rowcolor]; $rowcolor = 1 - $rowcolor;
			if (isset($greenadj) && $greenadj == $myrow["ADJ_ID"]) $rowc = $ppconf["html_green"];
			$date = date($ppconf["datefmt"],strtotime($myrow["ADJ_Date"]));

			if ($edadj == $myrow["ADJ_ID"]) {
				echo "<TR>"; $rowc = $ppconf["html_red"];
				echo "<FORM method=\"post\" action=\"admin.php?PPSESSION=$PPSESSION\">";
				echo ppformelemts();
				echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"saveadj\">";
				echo "<INPUT type=\"hidden\" name=\"draw\" value=\"ajournal\">";
				echo "<INPUT type=\"hidden\" name=\"workerid\" value=\"".htmlspecialchars($workerrow["WORKER_ID"])."\">";
				echo "<INPUT type=\"hidden\" name=\"lim0\" value=\"".htmlspecialchars($lim0)."\">";
				echo "<INPUT type=\"hidden\" name=\"ADJ_ID\" value=\"".htmlspecialchars($myrow["ADJ_ID"])."\">";
				echo "<INPUT type=\"hidden\" name=\"greenadj\" value=\"".htmlspecialchars($myrow["ADJ_ID"])."\">";
				echo "<TD bgcolor=$rowc align=center><INPUT type=\"text\" name=\"ADJ_Date\" size=\"12\" value=\"".htmlspecialchars($date)."\"></TD>";
				echo "<TD bgcolor=$rowc align=center><INPUT type=\"text\" name=\"ADJ_Minutes\" size=\"6\" value=\"".htmlspecialchars($myrow["ADJ_Minutes"])."\"></TD>";
				echo "<TD bgcolor=$rowc align=center><INPUT type=\"text\" name=\"ADJ_Text\" size=\"50\" value=\"".htmlspecialchars($myrow["ADJ_Text"])."\"></TD>";
				echo "<TD colspan=2><INPUT type=submit value=\"".$pplang["ADM_FORM_SAVE"]."\"></TD>";
				echo "</FORM></TR>\n";
			} else {	
				echo "<TR>";
				echo "<TD bgcolor=$rowc align=center>".htmlspecialchars($date)."</TD>";
				echo "<TD bgcolor=$rowc align=center>";
				if ($myrow["ADJ_Minutes"] > 0.0) {
					echo "<SPAN style=\"color:#FF0000;\">+</SPAN>";
				}
				echo htmlspecialchars($myrow["ADJ_Minutes"]. " (".number_format($myrow["ADJ_Minutes"] / 60.0, 1, $ppconf["decimalseparator"],"")."h)")."</TD>";
				echo "<TD bgcolor=$rowc align=center>".htmlspecialchars($myrow["ADJ_Text"])."</TD>";
				echo "<TD><A href=\"admin.php?draw=ajournal&amp;edadj=".rawurlencode($myrow["ADJ_ID"])."&amp;workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;lim0=".rawurlencode($lim0)."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_TBL_EDIT"]."</A>";
				echo "</TR>\n";
			}
		}
		mysqli_free_result($result);
		echo "<TR><TH colspan=\"3\">".$pplang["ADM_AJOURNAL_CRTHEADER"]."</TH></TR>\n";
		echo "<TR>"; $rowc = $ppconf["html_rowc".$rowcolor];
		echo "<FORM method=\"post\" action=\"admin.php?PPSESSION=$PPSESSION\">";
		echo ppformelemts();
		echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"crtadj\">";
		echo "<INPUT type=\"hidden\" name=\"draw\" value=\"ajournal\">";
		echo "<INPUT type=\"hidden\" name=\"workerid\" value=\"".htmlspecialchars($workerrow["WORKER_ID"])."\">";
		echo "<INPUT type=\"hidden\" name=\"lim0\" value=\"".htmlspecialchars($lim0)."\">";
		echo "<TD bgcolor=$rowc align=center><INPUT type=\"text\" name=\"ADJ_Date\" size=\"12\" value=\"".htmlspecialchars(date($ppconf["datefmt"]))."\"></TD>";
		echo "<TD bgcolor=$rowc align=center><INPUT type=\"text\" name=\"ADJ_Minutes\" size=\"6\" value=\"0\"></TD>";
		echo "<TD bgcolor=$rowc align=center><INPUT type=\"text\" name=\"ADJ_Text\" size=\"50\" value=\"\"></TD>";
		echo "<TD colspan=2><INPUT type=submit value=\"".$pplang["ADM_FORM_CREATE"]."\"></TD>";
		echo "</FORM></TR>\n";		
		echo "</TABLE>";
		echo "&nbsp;<BR><SPAN style=\"color:red;\">".$pplang["ADM_AJOURNAL_HINT"]."</SPAN>";
	}
	mysqli_free_result($wrkres);
} elseif ($draw == "contracts") {
	$workerid = (int)trim(pp_postfield("workerid"));
	
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {		
		echo "<B>".sprintf($pplang["ADM_CTR_HEADER"] , htmlspecialchars($workerrow["WORKER_Name"]))."</B><BR><BR>";
		if (strlen($err) > 0) echo $err."<BR><BR>";
		echo "<TABLE border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
		echo "<TR><TH>".$pplang["ADM_CTR_TBLCAPTION"]."</TH><TH>".$pplang["ADM_CTR_TBLFROM"]."</TH><TH>".$pplang["ADM_CTR_TBLTO"]."</TH><TH>".$pplang["ADM_CTR_TBLTYPE"]."</TH><TH>".$pplang["ADM_CTR_TBLSALDO"]."</TH></TR>\n";
		$sqlcmd = "SELECT * FROM $dbname.pcontract WHERE CONTRACT_WorkerPTR='".ppaddslashes($workerid)."' ORDER BY CONTRACT_Start ASC";
		$cresult = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		while ($crow = mysqli_fetch_assoc($cresult)) {
			echo "<TR>";
			if ($crow["CONTRACT_ID"] == $workerrow["WORKER_AdminDefaultContractPTR"]) {
				$bin = "<B>"; $bout = "</B>";
			} else {
				$bin = ""; $bout = "";
			}
			echo "<TD>$bin".htmlspecialchars($crow["CONTRACT_Name"])."$bout</TD>";
			echo "<TD>$bin".htmlspecialchars(date($ppconf["datefmt"], strtotime($crow["CONTRACT_Start"]." 12:00:00")))."$bout</TD>";
//			echo "<TD>$bin".htmlspecialchars(date("d.m.Y", strtotime($crow["CONTRACT_Start"]." 12:00:00")))."$bout</TD>";
			echo "<TD>$bin".htmlspecialchars(date($ppconf["datefmt"], strtotime($crow["CONTRACT_End"]." 12:00:00")))."$bout</TD>";
//			echo "<TD>$bin".htmlspecialchars(date("d.m.Y", strtotime($crow["CONTRACT_End"]." 12:00:00")))."$bout</TD>";
			echo "<TD>$bin".htmlspecialchars(pp_contracttypeliteral($crow["CONTRACT_Type"]))."$bout</TD>";
			
			$saldo = pp_getcontractsaldo($crow["CONTRACT_ID"], time());
			echo "<TD>$bin".$saldo["htmlshort"]."$bout</TD>";
			echo "<TD><A href=\"admin.php?draw=showcontract&amp;workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;contractid=".rawurlencode($crow["CONTRACT_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_CTR_TBLRESULTS"]."</A></TD>";
			echo "<TD><A href=\"admin.php?draw=edcontract&amp;workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;contractid=".rawurlencode($crow["CONTRACT_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_TBL_EDIT"]."</A></TD>";
			echo "<TD><A href=\"admin.php?dbaction=setadmdefaultcontract&amp;workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;contractid=".rawurlencode($crow["CONTRACT_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_CTR_TBLSETDEFAULT"]."</A></TD>";
			echo "<TD><A href=\"admin.php?dbaction=delcontract&amp;workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;contractid=".rawurlencode($crow["CONTRACT_ID"])."&amp;PPSESSION=$PPSESSION\" onclick=\"return confirm('".$pplang["ADM_CONFIRMDELETE"]."');\" oncontextmenu=\"return false;\">".$pplang["ADM_TBL_DEL"]."</A></TD>";
			echo "</TR>\n";
		}
		mysqli_free_result($cresult);
		echo "</TABLE>\n";
		echo "&nbsp;<BR>";
		echo "<A href=\"admin.php?dbaction=createcontract&amp;workerid=".rawurlencode($workerrow["WORKER_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_CTR_NEWCONTRACT"]."</A>";
	}
	mysqli_free_result($wrkres);
} elseif ($draw == "showcontract") {
	if (strlen($err) > 0) echo $err."<BR><BR>";
	
	$workerid = (int)trim(pp_postfield("workerid"));
	$contractid = (int)trim(pp_postfield("contractid"));
	$showto = trim(pp_postfield("showto"));
	
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {
		$sqlcmd = "SELECT * FROM $dbname.pcontract WHERE CONTRACT_ID='".ppaddslashes($contractid)."' AND CONTRACT_WorkerPTR='".ppaddslashes($workerid)."' LIMIT 1";
		$cresult = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		if ($crow = mysqli_fetch_assoc($cresult)) {		
			echo "<FORM method=post id=\"frm\" action=\"admin.php?PPSESSION=$PPSESSION\">";
			echo ppformelemts();
			echo "<INPUT type=\"hidden\" name=\"draw\" value=\"showcontract\">";
			echo "<INPUT type=\"hidden\" name=\"workerid\" value=\"".$crow["CONTRACT_WorkerPTR"]."\">";
			echo "<INPUT type=\"hidden\" name=\"contractid\" value=\"".$crow["CONTRACT_ID"]."\">";
			echo $pplang["ADM_CTR_SHOWATHDR"]." <INPUT type=\"text\" name=\"showto\"> <INPUT type=\"submit\" value=\"".$pplang["ADM_FORM_SHOW"]."\">";
			echo "</FORM>";
			if ($showto=="") $showto = date($ppconf["datefmt"]);
			if (!pp_parsedate($showto, $showto2)) {
				// $showto2 = date("Y-m-d");
				$showto2 = date($ppconf["datefmt"]);
				echo "<SPAN style=\"color:red;\">".$pplang["ADM_ERR_DATE"]." $showto</SPAN>";
			}
			echo "<HR>";
			// $pplang["ADM_CTR_RESULTSHDR"] = utf8_encode("Vertragsdetails [name]/[contract] zum Datum [date]:");
			echo "<B>".strtr($pplang["ADM_CTR_RESULTSHDR"], array("[name]"=>htmlspecialchars($workerrow["WORKER_Name"]),"[contract]"=>htmlspecialchars($crow["CONTRACT_Name"]),"[date]"=>htmlspecialchars(date($ppconf["datefmt"],strtotime($showto2))) ))."</B><BR><BR>";
//			echo "<B>".strtr($pplang["ADM_CTR_RESULTSHDR"], array("[name]"=>htmlspecialchars($workerrow["WORKER_Name"]),"[contract]"=>htmlspecialchars($crow["CONTRACT_Name"]),"[date]"=>htmlspecialchars(date("d.m.Y",strtotime($showto2))) ))."</B><BR><BR>";
			$saldo = pp_getcontractsaldo($crow["CONTRACT_ID"], strtotime($showto2));
			echo $saldo["htmlshort"]."<BR><BR>";
			echo $saldo["htmllong"];
			
		}
		mysqli_free_result($cresult);
	}
	mysqli_free_result($wrkres);

} elseif ($draw == "edcontract") {
	if (strlen($err) > 0) echo $err."<BR><BR>";
	
	$workerid = (int)trim(pp_postfield("workerid"));
	$contractid = (int)trim(pp_postfield("contractid"));
	
	$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
	$wrkres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	if ($workerrow = mysqli_fetch_array($wrkres)) {
		$sqlcmd = "SELECT * FROM $dbname.pcontract WHERE CONTRACT_ID='".ppaddslashes($contractid)."' AND CONTRACT_WorkerPTR='".ppaddslashes($workerid)."' LIMIT 1";
		$cresult = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		if ($crow = mysqli_fetch_assoc($cresult)) {		
			echo "<FORM method=post id=\"frm\" action=\"admin.php?PPSESSION=$PPSESSION\">";
			echo ppformelemts();
			echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"savecontract\">";
			echo "<INPUT type=\"hidden\" name=\"workerid\" value=\"".$crow["CONTRACT_WorkerPTR"]."\">";
			echo "<INPUT type=\"hidden\" name=\"contractid\" value=\"".$crow["CONTRACT_ID"]."\">";
			echo "<INPUT type=\"hidden\" name=\"redrw\" id=\"redrw\" value=\"0\">";
			echo "<INPUT type=\"hidden\" name=\"fmCONTRACT_Type\" value=\"".$crow["CONTRACT_Type"]."\">";
			
			echo "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"5\"><TR><TD colspan=\"2\"><B>".sprintf($pplang["ADM_CTR_EDHEADER"], htmlspecialchars($workerrow["WORKER_Name"]))."</B><BR>&nbsp;</TD></TR>\n";
			echo "<TR><TD>".$pplang["ADM_CTR_EDCAPTION"].":</TD><TD><INPUT type=\"text\" name=\"CONTRACT_Name\" size=40 maxlength=80 value=\"".htmlspecialchars($crow["CONTRACT_Name"])."\"></TD></TR>\n";
			echo "<TR><TD>".$pplang["ADM_CTR_EDSTART"].":</TD><TD><INPUT type=\"text\" name=\"CONTRACT_Start\" size=20 maxlength=80 value=\"".htmlspecialchars(date($ppconf["datefmt"], strtotime($crow["CONTRACT_Start"])))."\"></TD></TR>\n";
//			echo "<TR><TD>".$pplang["ADM_CTR_EDSTART"].":</TD><TD><INPUT type=\"text\" name=\"CONTRACT_Start\" size=20 maxlength=80 value=\"".htmlspecialchars(date("d.m.Y", strtotime($crow["CONTRACT_Start"])))."\"></TD></TR>\n";
			echo "<TR><TD>".$pplang["ADM_CTR_EDSTART"].":</TD><TD><INPUT type=\"text\" name=\"CONTRACT_Start\" size=20 maxlength=80 value=\"".htmlspecialchars(date($ppconf["datefmt"], strtotime($crow["CONTRACT_Start"])))."\"></TD></TR>\n";
//			echo "<TR><TD>".$pplang["ADM_CTR_EDEND"].":</TD><TD><INPUT type=\"text\" name=\"CONTRACT_End\" size=20 maxlength=80 value=\"".htmlspecialchars(date("d.m.Y", strtotime($crow["CONTRACT_End"])))."\"></TD></TR>\n";
			echo "<TR><TD>".$pplang["ADM_CTR_EDTYPE"].":</TD><TD><SELECT name=\"CONTRACT_Type\" onchange=\"document.getElementById('redrw').value=1; document.getElementById('frm').submit();\">";
			for ($i=1;$i<=2;$i+=1) {
				echo "<OPTION value=\"$i\"".($crow["CONTRACT_Type"] == $i?" selected":"").">".htmlspecialchars(pp_contracttypeliteral($i))."</OPTION>\n";
			}
			echo "</SELECT></TD></TR>\n";
			if ($crow["CONTRACT_Type"] == 1) {
				echo "<TR><TD>".$pplang["CS_1_EDDESTH"].":</TD><TD><INPUT type=\"text\" name=\"CONTRACT_T1_DestHours\" size=10 maxlength=20 value=\"".htmlspecialchars(number_format($crow["CONTRACT_T1_DestHours"],0,",",""))."\"></TD></TR>\n";
			} elseif ($crow["CONTRACT_Type"] == 2) {
				echo "<TR><TD>".$pplang["CS_2_EDDESTH"].":</TD><TD><INPUT type=\"text\" name=\"CONTRACT_T2_DestHours\" size=10 maxlength=20 value=\"".htmlspecialchars(number_format($crow["CONTRACT_T2_DestHours"],0,",",""))."\"></TD></TR>\n";
				echo "<TR><TD>".$pplang["CS_2_EDVACD"].":</TD><TD><INPUT type=\"text\" name=\"CONTRACT_T2_VacationDays\" size=10 maxlength=20 value=\"".htmlspecialchars(number_format($crow["CONTRACT_T2_VacationDays"],0,",",""))."\"></TD></TR>\n";
				
			}
			echo "<TR><TD>".$pplang["ADM_CTR_EDCOMMENT"].":</TD><TD><TEXTAREA name=\"CONTRACT_Text\" cols=\"60\" rows=\"12\" wrap=\"soft\" >".htmlspecialchars($crow["CONTRACT_Text"])."</TEXTAREA></TD></TR>\n";
			echo "<TR><TD></TD><TD>&nbsp;<BR><INPUT type=\"submit\" value=\"".$pplang["ADM_FORM_SAVE"]."\"></TD></TR>\n";
			echo "</TABLE>";
			echo "</FORM>";
			$saldo = pp_getcontractsaldo($crow["CONTRACT_ID"], time());
			echo $saldo["htmllong"];
			
		}
		
		mysqli_free_result($cresult);		
	}
	mysqli_free_result($wrkres);
} elseif ($draw == "none") {
	
} elseif ($draw == "holidays") {
	$cy = date("Y");
	echo "<B>".$pplang["ADM_HDY_HEADER"]."</B>";
	if (strlen($err) > 0) echo "<BR>".$err;
	echo "<HR>";
	echo "<FORM method=post action=\"admin.php?draw=holidays&amp;PPSESSION=$PPSESSION\">";
	echo ppformelemts();
	echo $pplang["ADM_HDY_FDATE"]." (".$ppconf["datefmt"] .") : <INPUT type=\"text\" name=\"HOLIDAY_Date\" size=\"14\" value=\"\"> ";
	echo $pplang["ADM_HDY_FTEXT"].": <INPUT type=\"text\" name=\"HOLIDAY_Description\" size=\"20\" value=\"\">";
	echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"addholiday\">";
	echo "<INPUT type=\"submit\" value=\"".$pplang["ADM_HDY_FADD"]."\">";
	echo "</FORM>\n";
	echo "<HR>\n";
	echo "<FORM method=post action=\"admin.php?draw=holidaylist&amp;PPSESSION=$PPSESSION\">";
	echo ppformelemts();
	echo "Show holiday list (Year) : <INPUT type=\"text\" name=\"year\" size=\"6\" value=\"$cy\"> ";
	echo "<INPUT type=\"submit\" value=\"".$pplang["ADM_FORM_SHOW"]."\">";
	echo "</FORM>\n";
	echo "<HR>\n";
	echo "<FORM method=post action=\"admin.php?draw=holidaylist&amp;PPSESSION=$PPSESSION\">";
	echo sprintf($pplang["ADM_HDY_ADDWEEKENDS"], "<INPUT type=\"text\" name=\"year\" size=\"6\" value=\"$cy\">")." ";
	echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"holidaysaddweekends\">";
	echo "<INPUT type=\"submit\" value=\"".$pplang["ADM_HDY_ADDWEEKENDSBTN"]."\">";
	echo "</FORM>\n";
	echo "<HR>\n";
	echo "<FORM method=post action=\"admin.php?draw=holidays&amp;PPSESSION=$PPSESSION\" onsubmit=\"return confirm('".$pplang["ADM_HDY_CONFIRMSTRIP"]."');\">";
	echo ppformelemts();
	echo sprintf($pplang["ADM_HDY_STRIP"], "<INPUT type=\"text\" name=\"year\" size=\"6\" value=\"$cy\">")." ";
	echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"holidaysdelyear\">";
	echo "<INPUT type=\"submit\" value=\"".$pplang["ADM_FORM_DEL"]."\">";
	echo "</FORM>\n";
} elseif ($draw == "holidaylist") {
	if (strlen($err) > 0) echo "<BR><BR>".$err."<BR><BR>";
	
	$year = (int)trim(pp_postfield("year"));
	
	if ($year < 2000 || $year > 2099) die($pplang["ADM_ERR_YEAR"]);
	$sqlcmd = "SELECT * FROM $dbname.pholiday WHERE HOLIDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND HOLIDAY_Date >= '".ppaddslashes("{$year}-01-01")."' AND HOLIDAY_Date <= '".ppaddslashes("{$year}-12-31")."' ORDER BY HOLIDAY_Date ASC";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo "<TABLE border=\"0\" cellpadding=\"1\" cellspacing=\"1\"><TR><TH>".$pplang["ADM_HDY_TBLDATE"]."</TH><TH>&nbsp;</TH><TH>".$pplang["ADM_HDY_TBLTEXT"]."</TH></TR>\n";
	while ($row = mysqli_fetch_assoc($result)) {
		$dat = strtotime($row["HOLIDAY_Date"]);
		$wt = $pplang["weekdays"][date("w",$dat)];
		
		echo "<TR>";
		echo "<TD>".date($ppconf["datefmt"],$dat)."</TD>";
		echo "<TD>".htmlspecialchars($wt)."</TD>";
		echo "<TD>".htmlspecialchars($row["HOLIDAY_Description"])."</TD>";
		echo "<TD><A href=\"admin.php?dbaction=delholiday&amp;hi=".rawurlencode($row["HOLIDAY_ID"])."&amp;year=".rawurlencode($year)."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_TBL_DEL"]."</A></TD>";
		echo "</TR>\n";
	}
	echo "</TABLE>";
	mysqli_free_result($result);
} elseif ($draw == "calendar") {
	$year = (int)trim(pp_postfield("year"));
	
	if ($year < 2000 || $year > 2999) die($pplang["ADM_ERR_YEAR"]);
	
	$workercount = 0;
	$workers = array();
	$workeridx = array();
	$sqlcmd = "SELECT WORKER_ID, WORKER_Name, WORKER_StdWorkHoursPerDay FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' AND WORKER_Invisible='0' ORDER BY WORKER_Name ASC";
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	while ($row = mysqli_fetch_assoc($result)) {
		$workers[$workercount] = $row;
		$workers[$workercount]["vaccount"] = 0;
		$workers[$workercount]["sickcount"] = 0;

		$workers[$workercount]["m_wh"] = 0.0;
		$workers[$workercount]["m_sh"] = 0.0;
		$workers[$workercount]["m_xh"] = 0.0;
		$workers[$workercount]["y_wh"] = 0.0;
		$workers[$workercount]["y_sh"] = 0.0;
		$workers[$workercount]["y_xh"] = 0.0;

		$workeridx["I".$row["WORKER_ID"]] = $workercount;
		$workercount += 1;
	}
	mysqli_free_result($result);
	echo "<FORM id=\"fm\" method=\"post\" action=\"admin.php\">";
	echo ppformelemts();
	echo "<INPUT type=\"hidden\" name=\"PPSESSION\" value=\"$PPSESSION\">";
	echo "<INPUT type=\"hidden\" name=\"dbaction\" value=\"\">";
	echo "<INPUT type=\"hidden\" name=\"w\" value=\"\">";
	echo "<INPUT type=\"hidden\" name=\"day\" value=\"\">";
	echo "</FORM>";
pp_drawjsreports();	
	echo "<TABLE border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
	
	
	$stamp = strtotime($year."-01-01 12:00:00");
	$dat = date("Y-m-d",$stamp);
	$today = date("Y-m-d");
	$lastmonth = "0";
	while (true) {
		
		$month = (int)date("m",$stamp);
		if ($month != $lastmonth) {
			if ($lastmonth != "0") {
				echo "<TR><TH>".sprintf($pplang["ADM_CAL_MONTHSUM"],htmlspecialchars($pplang["monthnameslong"][$lastmonth]))."</TH>";
				for ($i=0;$i < $workercount;$i++) {
					echo "<TH>";
					echo number_format($workers[$i]["m_wh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_WORK"]." "; // A
					echo number_format($workers[$i]["m_sh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_SICK"]." "; // K
					echo number_format($workers[$i]["m_xh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_ADJ"]." "; // X
				}
				echo "<TR><TD>&nbsp;</TD></TR>\n";
			}
			if (substr($dat,0,4) != $year) {
				echo "<TR><TH>".sprintf($pplang["ADM_CAL_YEARSUM"], "".$year)."</TH>";
				for ($i=0;$i < $workercount;$i++) {
					echo "<TH>";
					echo number_format($workers[$i]["y_wh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_WORK"]." ";
					echo number_format($workers[$i]["y_sh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_SICK"]." ";
					echo number_format($workers[$i]["y_xh"], 1, $ppconf["decimalseparator"],"").$pplang["ADM_CAL_SUFFIX_ADJ"]." ";
				}
				echo "<TR><TD>&nbsp;</TD></TR>\n";
				break;
			}
			echo "<TR><TH><A id=\"MO".$month."\">".htmlspecialchars($pplang["monthnameslong"][$month])."</A></TH>";
			for ($i=0;$i < $workercount;$i++) {
				echo "<TH>".htmlspecialchars($workers[$i]["WORKER_Name"])."</TH>";
				$workers[$i]["m_wh"] = 0.0;
				$workers[$i]["m_sh"] = 0.0;
				$workers[$i]["m_xh"] = 0.0;
			}
			echo "</TR>\n";
			$lastmonth = $month;
			
		}
		
		echo "<TR>";
		echo "<TH>".htmlspecialchars(date($ppconf["datefmt"],$stamp)." ".$pplang["weekdays"][date("w",$stamp)])."</TD>";
//		echo "<TH>".htmlspecialchars(date("d.m.Y",$stamp)." ".$pplang["weekdays"][date("w",$stamp)])."</TD>";
		$sqlcmd = "SELECT HOLIDAY_Description FROM $dbname.pholiday WHERE HOLIDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND HOLIDAY_Date='".ppaddslashes($dat)."' LIMIT 1";
		$qtres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		$holiday = false;
		if ($qtrow = mysqli_fetch_assoc($qtres)) {
			$holiday = true;
		} 
		mysqli_free_result($qtres);
		
		for ($i=0;$i < $workercount;$i++) {
			$workers[$i]["vacation"] = 0;
			$workers[$i]["sick"] = 0;
		}
		$sqlcmd = "SELECT VACDAY_ID, VACDAY_WorkerPTR FROM $dbname.pvacationday WHERE VACDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND VACDAY_Date='".ppaddslashes($dat)."'";
		$vres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		while ($row = mysqli_fetch_assoc($vres)) {
			if (isset($workeridx["I".$row["VACDAY_WorkerPTR"]])) {
				$idx  = $workeridx["I".$row["VACDAY_WorkerPTR"]];
				$workers[$idx]["vacation"] = $row["VACDAY_ID"];
			}
		}
		mysqli_free_result($vres);
		
		$sqlcmd = "SELECT SICKDAY_WorkerPTR, SICKDAY_Minutes FROM $dbname.psickday WHERE SICKDAY_PlantPTR='".ppaddslashes($PPLANTID)."' AND SICKDAY_Date='".ppaddslashes($dat)."'";
		$sres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		while ($row = mysqli_fetch_assoc($sres)) {
			if (isset($workeridx["I".$row["SICKDAY_WorkerPTR"]])) {
				$idx  = $workeridx["I".$row["SICKDAY_WorkerPTR"]];
				$workers[$idx]["sick"] += $row["SICKDAY_Minutes"];
			}
		}
		mysqli_free_result($sres);
		
		
		for ($i=0;$i < $workercount;$i++) {
			$sqlcmd = "SELECT SUM(PR_Minutes) AS wrkm FROM $dbname.ppresence WHERE PR_Come>='".ppaddslashes($dat)." 00:00:00' AND PR_Come<='".ppaddslashes($dat)." 23:59:59'";
			$wh = pp_countworkhours($workers[$i]["WORKER_ID"], $dat, $dat);	
			$sh = $workers[$i]["sick"] / 60.0;
			$xh = pp_countadjustmenthours($workers[$i]["WORKER_ID"], $dat, $dat);	
			
			$ph = $wh + $sh;
			$vacation = $workers[$i]["vacation"] > 0?true:false;
			
			$bgc = "#C0C0C0";
						
			$workers[$i]["m_wh"] += $wh;
			$workers[$i]["m_sh"] += $sh;
			$workers[$i]["m_xh"] += $xh;

			$workers[$i]["y_wh"] += $wh;
			$workers[$i]["y_sh"] += $sh;
			$workers[$i]["y_xh"] += $xh;
			
			
			if ($ph > 0) {
				if ($vacation) {
					$bgc = "#FF8080";	// Arbeit im Urlaub
				}
				if ($holiday && $sh > 0.0) {
					$bgc = "#FF8080";	// Krank am Feiertag
				}
				if ($sh > 0 && round($ph*60) != round($workers[$i]["WORKER_StdWorkHoursPerDay"]*60)) {
					$bgc = "#FF8080";
				}
			} else {
				if ($holiday) {
					$bgc = "#808080";
				} elseif ($vacation) {
					$bgc = "#C0C0FF";
				} else {				
					if ($dat < $today) $bgc = "#FFFF00";
				}
				if ($holiday && $vacation) {
					$bgc = "#FF8080";
				}
			}
			if ($wh < 0 || $sh < 0) {
				$bgc = "#FF0000";
			}
				
			echo "<TD bgcolor=\"$bgc\" nowrap>";
			
			if ($workers[$i]["vacation"] == 0) {
				echo "<A href=\"#\" onclick=\"return sv(".$workers[$i]["WORKER_ID"].", '".date("d-m-Y",$stamp)."', $month);\" oncontextmenu=\"return false;\">".$pplang["ADM_CAL_SUFFIX_VAC"]."</A>";
				//echo "<A href=\"admin.php?dbaction=setvac&amp;w=".rawurlencode($workers[$i]["WORKER_ID"])."&amp;day=".rawurlencode(date("d.m.Y",$stamp))."&amp;PPSESSION=$PPSESSION#MO".$month."\">U</A>";
			} else {
				echo "<A href=\"#\" onclick=\"return cv(".$workers[$i]["WORKER_ID"].", '".date("d-m-Y",$stamp)."', $month);\" oncontextmenu=\"return false;\"><B>".$pplang["ADM_CAL_SUFFIX_VAC"]."</B></A>";
				// echo "<A href=\"admin.php?dbaction=clrvac&amp;w=".rawurlencode($workers[$i]["WORKER_ID"])."&amp;day=".rawurlencode(date("d.m.Y",$stamp))."&amp;PPSESSION=$PPSESSION#MO".$month."\"><B>U</B></A>";
				$workers[$i]["vaccount"] += 1;
			}
			
			echo " ".number_format($wh, 1, ".","")."A";
			
			if ($workers[$i]["sick"] == 0.0) {
				echo " <A href=\"#\" onclick=\"return ss(".$workers[$i]["WORKER_ID"].", '".date("d-m-Y",$stamp)."', $month);\" oncontextmenu=\"return false;\">".$pplang["ADM_CAL_SUFFIX_SICK"]."</A>";
				//echo " <A href=\"admin.php?dbaction=setsic&amp;w=".rawurlencode($workers[$i]["WORKER_ID"])."&amp;day=".rawurlencode(date("d.m.Y",$stamp))."&amp;PPSESSION=$PPSESSION#MO".$month."\">K</A>";
			} else {
				echo " <A href=\"#\" onclick=\"return cs(".$workers[$i]["WORKER_ID"].", '".date("d-m-Y",$stamp)."', $month);\" oncontextmenu=\"return false;\">".number_format($sh, 1, ".","").$pplang["ADM_CAL_SUFFIX_SICK"]."</A>";
				//echo " <A href=\"admin.php?dbaction=clrsic&amp;w=".rawurlencode($workers[$i]["WORKER_ID"])."&amp;day=".rawurlencode(date("d.m.Y",$stamp))."&amp;PPSESSION=$PPSESSION#MO".$month."\">".number_format($sh, 1, ".","")."K</A>";
				$workers[$i]["sickcount"] += 1;
			}
			
			if ($xh != 0.0) {
				echo " <SPAN style=\"color:#800000;\"><B>".number_format($xh, 1, ".","").$pplang["ADM_CAL_SUFFIX_ADJ"]."</B></SPAN>";
			}

			
			echo "</TD>";
		}
		if ($holiday) {
			echo "<TH>".htmlspecialchars($qtrow["HOLIDAY_Description"])."</TH>\n";
		}
		echo "</TR>\n";
		
		$stamp += 24*60*60;
		$dat = date("Y-m-d",$stamp);
	}
	echo "<TR><TH>&nbsp;</TH>";
	for ($i=0;$i < $workercount;$i++) {
		echo "<TH>".htmlspecialchars($workers[$i]["WORKER_Name"])."</TH>";
	}
	echo "</TR>\n";
	echo "<TR><TH>".$pplang["ADM_CAL_TBLVACDAYS"]."</TH>";
	for ($i=0;$i < $workercount;$i++) {
		echo "<TH>".htmlspecialchars($workers[$i]["vaccount"])."</TH>";
	}
	echo "</TR>\n";

	echo "<TR><TH>".$pplang["ADM_CAL_TBLSICKDAYS"]."</TH>";
	for ($i=0;$i < $workercount;$i++) {
		echo "<TH>".htmlspecialchars($workers[$i]["sickcount"])."</TH>";
	}
	echo "</TR>\n";
	echo "</TABLE>";
	
} else {
	echo "<A href=\"admin.php?draw=addworker&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_MAIN_ADDWORKER"]."</A>";
	echo " &nbsp; <A href=\"admin.php?dbaction=ureghost&amp;PPSESSION=$PPSESSION\" onclick=\"return confirm('".$pplang["ADM_MAIN_UREGHOSTCONFIRM"]."');\">".$pplang["ADM_MAIN_UREGHOST"]."</A>";
	echo " &nbsp; <A href=\"admin.php?dbaction=sethostcookie&amp;PPSESSION=$PPSESSION\" onclick=\"return confirm('".$pplang["ADM_MAIN_SETCOOKIECONFIRM"]."');\">".$pplang["ADM_MAIN_SETCOOKIE"]."</A>";
	echo " &nbsp; <A href=\"admin.php?dbaction=resethostcookie&amp;PPSESSION=$PPSESSION\" onclick=\"return confirm('".$pplang["ADM_MAIN_RESETCOOKIECONFIRM"]."');\">".$pplang["ADM_MAIN_RESETCOOKIE"]."</A>";
	echo " &nbsp; <A href=\"admin.php?draw=holidays&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_MAIN_MAINTHOLIDAYS"]."</A>";
	//20-11-2013 added by KAZ Reports button
	echo " &nbsp; <A href=\"admin.php?draw=reports&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_MAIN_REPORTS"]."</A>";

	$y = date("Y");
	echo "<BR>".$pplang["ADM_MAIN_CALENDAR"].": ";
	for ($i=$y - 2;$i < $y + 3;$i++) {echo " <A href=\"admin.php?draw=calendar&amp;year=$i&amp;PPSESSION=$PPSESSION\">[$i]</A>";}

	echo "<BR><BR>";
	if (pp_postfield("showinvisible") == "1") {
		$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' ORDER BY WORKER_Name ASC";
	} else {
		$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' AND WORKER_Invisible='0' ORDER BY WORKER_Name ASC";
	}
	$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
	echo "<TABLE border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
	$rowcolor = 0;
	echo "<TR><TH>".$pplang["ADM_MAIN_TBLWORKER"]."</TH><TH>".$pplang["ADM_MAIN_TBLCAME"]."</TH><TH>".$pplang["ADM_MAIN_TBLGONE"]."</TH><TH>".$pplang["ADM_MAIN_TBLSTATE"]."</TH><TH>".$pplang["ADM_MAIN_TBLSALDO"]."</TH></TR>\n";
	while ($myrow = mysqli_fetch_array($result)) {
		$rowc = $ppconf["html_rowc".$rowcolor]; $rowcolor = 1 - $rowcolor;
		echo "<TR>";
		echo "<TD bgcolor=$rowc>".htmlspecialchars($myrow["WORKER_Name"])."</TD>";
		$come = "0000-00-00 00:00:00";
		$go = "0000-00-00 00:00:00";
		$sqlcmd = "SELECT * FROM $dbname.ppresence WHERE PR_WorkerPTR='".ppaddslashes($myrow["WORKER_ID"])."' ORDER BY PR_ID DESC LIMIT 1";
		$res2 = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
		if ($wrow = mysqli_fetch_array($res2)) {
			$come = $wrow["PR_Come"];
			$go = $wrow["PR_Go"];
		}
		mysqli_free_result($res2);	
		if ($come != "0000-00-00 00:00:00" && $go == "0000-00-00 00:00:00") {
			$status = "<FONT color=\"green\">".$pplang["ADM_MAIN_TBLPRESENT"]."</FONT>";
		} else {
			$status = "<FONT color=\"red\">".$pplang["ADM_MAIN_TBLABSENT"]."</FONT>";
		}
		
		if ($come == "0000-00-00 00:00:00") {
			$come = "-";
		} else {
			$come = date($ppconf["datetimefmt"],strtotime($come));
		}
		if ($go == "0000-00-00 00:00:00") {			
			
			$go = "-";
		} else {
			
			$go = date($ppconf["datetimefmt"],strtotime($go));
		}
		echo "<TD bgcolor=$rowc>".htmlspecialchars($come)."</TD>";
		echo "<TD bgcolor=$rowc>".htmlspecialchars($go)."</TD>";
		echo "<TD bgcolor=$rowc>$status</TD>";
		$stand = "";
		if ($myrow["WORKER_AdminDefaultContractPTR"] != 0) {
			$sqlcmd = "SELECT CONTRACT_ID, CONTRACT_Name, CONTRACT_WorkerPTR FROM $dbname.pcontract WHERE CONTRACT_ID='".ppaddslashes($myrow["WORKER_AdminDefaultContractPTR"])."' AND CONTRACT_WorkerPTR='".ppaddslashes($myrow["WORKER_ID"])."' LIMIT 1";
			$defcres = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
			if ($defcrow = mysqli_fetch_assoc($defcres)) {
				$saldo = pp_getcontractsaldo($defcrow["CONTRACT_ID"], time());
				$stand = $saldo["htmlshort"]." ".htmlspecialchars("(".$defcrow["CONTRACT_Name"].")");
			}
			mysqli_free_result($defcres);
		}
		echo "<TD bgcolor=$rowc>$stand</TD>";
		echo "<TD><A href=\"admin.php?draw=edworker&workerid=".rawurlencode($myrow["WORKER_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_TBL_EDIT"]."</A></TD>";
		echo "<TD><A target=\"_blank\" href=\"admin.php?draw=barcode&workerid=".rawurlencode($myrow["WORKER_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_MAIN_TBLBARCODE"]."</A></TD>";
		echo "<TD><A href=\"admin.php?draw=times&workerid=".rawurlencode($myrow["WORKER_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_MAIN_TBLSTATS"]."</A></TD>";
		echo "<TD><A href=\"admin.php?draw=journal&workerid=".rawurlencode($myrow["WORKER_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_MAIN_TBLJOURNAL"]."</A></TD>";
		echo "<TD><A href=\"admin.php?draw=ajournal&workerid=".rawurlencode($myrow["WORKER_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_MAIN_TBLADJUSTMENTS"]."</A></TD>";
		echo "<TD><A href=\"admin.php?draw=contracts&workerid=".rawurlencode($myrow["WORKER_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_MAIN_TBLCONTRACTS"]."</A></TD>";
		echo "<TD><A href=\"admin.php?draw=delworker&workerid=".rawurlencode($myrow["WORKER_ID"])."&amp;PPSESSION=$PPSESSION\">".$pplang["ADM_TBL_DEL"]."</A></TD>";
		echo "</TR>\n";
	}
	mysqli_free_result($result);
	echo "</TABLE>";
	if (!isset($showinvisible)) {
		echo "<A href=\"admin.php?showinvisible=1&amp;PPSESSION=$PPSESSION\"><SMALL>[".$pplang["ADM_MAIN_TBLSHOWHIDDEN"]."]</SMALL></A><BR>";
	}
}
echo "<DIV style=\"padding-top:20px; font-size:7pt;color:#808080;text-align:right;\">Version ".htmlspecialchars($phppunch_VER)."</DIV>";
echo "</BODY></HTML>";
