<?php
	require_once 'cron/class.db.php';
	$id = (isset($_GET['id']) && $_GET['id'] != '') ? $_GET['id'] : '';
	$usuario = (isset($_GET['usuario']) && $_GET['usuario'] != '') ? $_GET['usuario'] : '';

	if ($usuario > 0 && $id != '') {

		$valores = explode('_',$id);
		$isNext = ($valores[0] == 'N') ? 1 : 0;
		$isPrevious = ($valores[0] == 'P') ? 1 : 0;
		$isWeekend = ($valores[1] == 'FS') ? 1 : 0;
		$isDay = ($valores[2] == 'D') ? 1 : 0;
		$yearDate = $valores[3];
		$monthDate =  $valores[4];
		$boatId = $valores[5];

		DataBase::getInstancia();
        echo 'alert("Hola")'; 
		if ($isWeekend > 0){
			if ($isDay > 0){
					$campo1 = 'festivo_morning = festivo_morning + 1';
					$campo2 = 'festivo_morning = festivo_morning - 1';
			}else {
					$campo1 = 'festivo_afternoon = festivo_afternoon + 1';
					$campo2 = 'festivo_afternoon = festivo_afternoon - 1';
			}
		}else {
			if ($isDay > 0){
					$campo1 = 'morning = morning + 1';
					$campo2 = 'morning = morning - 1';
			}else {
					$campo1 = 'afternoon = afternoon + 1';
					$campo2 = 'afternoon = afternoon - 1';
			}
		}

		if ($isNext > 0){
			if ($monthDate == 12){
				$sql_1 = 'UPDATE credit_register SET '. $campo1 .' WHERE date_year = '. ($yearDate+1) .' AND date_month = 1 AND customer_id = '.$usuario.' AND boat_id = '. $boatId ;
				$sql_2 = 'UPDATE credit_register SET '. $campo2 .' WHERE date_year = '. $yearDate .' AND date_month = ' . ($monthDate). ' AND customer_id = '.$usuario.' AND boat_id = '. $boatId;
				DataBase::execute_query($sql_2,'S');
				DataBase::execute_query($sql_1,'S');
			}else{
				$sql_1 = 'UPDATE credit_register SET '. $campo1 .' WHERE date_year = '. $yearDate .' AND date_month = ' . ($monthDate+1) . ' AND customer_id = '.$usuario.' AND boat_id = '. $boatId;
				$sql_2 = 'UPDATE credit_register SET '. $campo2 .' WHERE date_year = '. $yearDate .' AND date_month = ' . ($monthDate). ' AND customer_id = '.$usuario.' AND boat_id = '. $boatId;
				DataBase::execute_query($sql_2,'S');
				DataBase::execute_query($sql_1,'S');
			}
		} elseif( $isPrevious > 0) {
			if ($monthDate == 1){
				$sql_1 = 'UPDATE credit_register SET '. $campo1 .' WHERE date_year = '. ($yearDate-1) .' AND date_month = 12 AND customer_id = '.$usuario.' AND boat_id = '. $boatId;
				$sql_2 = 'UPDATE credit_register SET '. $campo2 .' WHERE date_year = '. $yearDate .' AND date_month = ' . ($monthDate) . ' AND customer_id = '.$usuario.' AND boat_id = '. $boatId;
				DataBase::execute_query($sql_2,'S');
				DataBase::execute_query($sql_1,'S');
			}else{
				$sql_1 = 'UPDATE credit_register SET '. $campo1 .' WHERE date_year = '. $yearDate .' AND date_month = ' . ($monthDate-1) . ' AND customer_id = '.$usuario.' AND boat_id = '. $boatId;
				$sql_2 = 'UPDATE credit_register SET '. $campo2 .' WHERE date_year = '. $yearDate .' AND date_month = ' . ($monthDate) . ' AND customer_id = '.$usuario.' AND boat_id = '. $boatId;
				DataBase::execute_query($sql_2,'S');
				DataBase::execute_query($sql_1,'S');
			}
		}
	}
?>
