<?php
require_once 'GenericAdmin/gui/control/GenadControl.php';
require_once 'gui/view/WorkFlowView.php';
require_once 'gui/model/LohnInterface.php';
require_once 'util/Session.php';

class LohnControl {
	public function doIt() {
		// check page
		$redirect_url = $_SERVER["REDIRECT_URL"];
		$page = preg_replace("/.php$/", "", $redirect_url);
		$page = preg_replace("!^.*/!", "", $page);
		if ($page == "index") {
			
			// restore monat
			$session = Session::getInstance();
			$mon_str = $session->get("Monat", "201205");
			$monat = substr($mon_str, 4,2);
			$jahr  = substr($mon_str, 0,4);
			$view = new WorkFlowView($monat, $jahr);
			$view->view();
		}
		else if ($page == "WorkFlowView") {
			// build Monat-String
			$monat = $_POST["jahr"].str_pad($_POST["monat"], 2, '0', STR_PAD_LEFT);
			if (strlen($monat) != 6) throw new Exception("Invalid month format");
			
			// store monat
			$session = Session::getInstance();
			$session->set("Monat", $monat);
			
			if ($_POST["monat_anlegen"]){
				$if = new LohnInterface();
				$if->monatAnlegen($monat);
				
				// jump into generic admin
				header("Location:ListView.php?event=ClassSelect&appclass=LohnAdministration+3&filter=monat-$monat");
			}
			else if ($_POST["brutto_rechnen"]) {
				$if = new LohnInterface();
				$if->bruttoBerechnen($monat);
				
				// jump into generic admin
				header("Location:ListView.php?event=ClassSelect&appclass=LohnAdministration+3&filter=monat-$monat");
			}
			else if ($_POST["berechnen"]) {
				$if = new LohnInterface();
				$if->monatBerechnen($monat);
				
				// jump into generic admin
				header("Location:ListView.php?event=ClassSelect&appclass=LohnAdministration+3&filter=monat-$monat");
			}
			else if ($_POST["erstellen"]) {
				$if = new LohnInterface();
				$if->abrechnungenErstellen($monat);
				
				// show start page again
				header("Location:index.php");
			}
			else if ($_POST["verschicken"]) {
				$if = new LohnInterface();
				$if->verschicken($monat);
				
				// show start page again
				header("Location:index.php");
			}
			else if ($_POST["ag_anteil_ber"]) {
				$if = new LohnInterface();
				$if->agAnteileBerechnen($monat);
				
				// jump into generic admin
				header("Location:ListView.php?event=ClassSelect&appclass=LohnAdministration+3&filter=monat-$monat");
			}
			else throw new Exception("Invalid POST requesst");
		}
		else {
			$genadControl = new GenadControl();
			$genadControl->doIt();
		}
	}
}