<?php

	require_once '../cron/class.db.php';
	$usuario = (isset($_GET['u']) && $_GET['u'] != '') ? $_GET['u'] : '';
	$modalidad = (isset($_GET['m']) && $_GET['m'] != '') ? $_GET['m'] : '';
	$anyo = (isset($_GET['y']) && $_GET['y'] != '') ? $_GET['y'] : date('Y');
	$inicio = (isset($_GET['s']) && $_GET['s'] != '') ? $_GET['s'] : '';
	$fin = (isset($_GET['e']) && $_GET['e'] != '') ? $_GET['e'] : '';
    $barco = (isset($_GET['b']) && $_GET['b'] != '') ? $_GET['b'] : '';
	
	$d1 = strtotime($inicio);
	$d2 = strtotime($fin);
	$min_date = min($d1,$d2);
	$max_date = max($d1,$d2);
	$numMeses = 0;

	while ( ($min_date = strtotime( '+1 MONTH', $min_date) ) <= $max_date  ) {
		$numMeses++;
	}
	
	if ($usuario != '' && $modalidad != '' && $numMeses > 0) {
		DataBase::getInstancia();
		
		if ($barco == ''){
		$sqlBarcos = 'SELECT boat_id FROM boat_customer WHERE customer_id = ' . $usuario;
    	$barcos = DataBase::execute_query($sqlBarcos, 'S');
		} else {
		    $barcos[0]['boat_id'] = $barco;
		}

		if ($modalidad == '7') {
			// for ( $i = 1; $i <= $numMeses ; $i++){
		    for ($i = 0; $i < count($barcos); $i++){
    			$min_date = min($d1,$d2);
    			$max_date = max($d1,$d2);
    			while ( $min_date <= $max_date  ) {
    				$sql0 = 'SELECT COUNT(1) as existe FROM credit_register WHERE customer_id = '.$usuario.' AND boat_id = '.$barcos[$i]['boat_id'].' AND date_month = '.date('m', $min_date).' AND date_year = '. date('Y', $min_date);
    				$res0 = DataBase::execute_query($sql0,'S');
    				if ($res0[0]['existe'] == 0 ){
    					if ( (date('m', $min_date) % 2) == 0) {
    						$sql = 'INSERT INTO credit_register (customer_id, boat_id, date_month, date_year, festivo_morning, festivo_afternoon, morning, afternoon) VALUES ( '.$usuario.', '.$barcos[$i]['boat_id'].' ,'. date('m', $min_date) .', '.date('Y', $min_date).', 1,1,3,2)';
    					} else { 
    						$sql = 'INSERT INTO credit_register (customer_id, boat_id, date_month, date_year, festivo_morning, festivo_afternoon, morning, afternoon) VALUES ( '.$usuario.', '.$barcos[$i]['boat_id'].' , '. date('m', $min_date) .', '.date('Y', $min_date).', 1,1,2,3)';
    					}
    					$result = DataBase::execute_query($sql,'S');
    					// echo $sql . '<br/>';
    				}else{
                        if ( (date('m', $min_date) % 2) == 0) {
                            $sql = 'UPDATE credit_register SET festivo_morning = 1, festivo_afternoon = 1, morning = 3, afternoon = 2 WHERE customer_id = '.$usuario.' AND boat_id = '.$barcos[$i]['boat_id'].' AND date_month = '.date('m', $min_date).' AND date_year = '.date('Y', $min_date);
                        } else {
                            $sql = 'UPDATE credit_register SET festivo_morning = 1, festivo_afternoon = 1, morning = 2, afternoon = 3 WHERE customer_id = '.$usuario.' AND boat_id = '.$barcos[$i]['boat_id'].' AND date_month = '.date('m', $min_date).' AND date_year = '.date('Y', $min_date);
                        }
                        $result = DataBase::execute_query($sql,'S');
                    }
    				$min_date = strtotime( '+1 MONTH', $min_date);
    			}
		    }

		}elseif ($modalidad == '3') {
		    
		    for ($i = 0; $i < count($barcos); $i++){
		    
    			$min_date = min($d1,$d2);
    			$max_date = max($d1,$d2);
    			while ( $min_date <= $max_date  ) {	
    				$sql0 = 'SELECT COUNT(1) as existe FROM credit_register WHERE customer_id = '.$usuario.' AND boat_id = '.$barcos[$i]['boat_id'].' AND date_month = '.date('m', $min_date).' AND date_year = '. date('Y', $min_date);
    				$res0 = DataBase::execute_query($sql0,'S');
    				if ($res0[0]['existe'] == 0 ){
    					if ( (date('m', $min_date) % 2) == 0) {
    						$sql = 'INSERT INTO credit_register (customer_id, boat_id, date_month, date_year, festivo_morning, festivo_afternoon, morning, afternoon) VALUES ( '.$usuario.', '.$barcos[$i]['boat_id'].' , '. date('m', $min_date) .', '.date('Y', $min_date).', 1,0,1,1)';
    					} else { 
    						$sql = 'INSERT INTO credit_register (customer_id, boat_id, date_month, date_year, festivo_morning, festivo_afternoon, morning, afternoon) VALUES ( '.$usuario.', '.$barcos[$i]['boat_id'].' , '. date('m', $min_date) .', '.date('Y', $min_date).', 0,1,1,1)';
    					}
    					$result = DataBase::execute_query($sql,'S');
    					// echo $sql . '<br/>';
    				}else{
                        if ( (date('m', $min_date) % 2) == 0) {
                            $sql = 'UPDATE credit_register SET festivo_morning = 1, festivo_afternoon = 0, morning = 1, afternoon = 1 WHERE customer_id = '.$usuario.' AND boat_id = '.$barcos[$i]['boat_id'].' AND date_month = '.date('m', $min_date).' AND date_year = '.date('Y', $min_date);
                        } else {
                            $sql = 'UPDATE credit_register SET festivo_morning = 0, festivo_afternoon = 1, morning = 1, afternoon = 1 WHERE customer_id = '.$usuario.' AND boat_id = '.$barcos[$i]['boat_id'].' AND date_month = '.date('m', $min_date).' AND date_year = '.date('Y', $min_date);
                        }
                        $result = DataBase::execute_query($sql,'S');
    				}
    				$min_date = strtotime( '+1 MONTH', $min_date);
    			}
		    }
		}
		echo 'Salidas cargadas correctamente';
	}
	
?>
