<?php

##################################################################################
# PHP-RESIDENCE
# Copyright (C) 2001-2009 by Marco Maria Francesco De Santis (marco@digitaldruid.net)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version accepted by Marco Maria Francesco De Santis, which
# shall act as a proxy as defined in Section 14 of version 3 of the
# license.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##################################################################################



function trova_prenota_succ ($anno,$d_id_data_fine,$PHPR_TAB_PRE,$id_prenota,&$fineperiodo_succ) {
$fineperiodo_succ = "";
$d_data_fine = esegui_query("select datafine from ".$PHPR_TAB_PRE."periodi".$anno." where idperiodi = '$d_id_data_fine'");
$d_data_fine = risul_query($d_data_fine,0,'datafine');
if (substr($d_data_fine,0,4) != $anno) {
$anno_succ_esistente = esegui_query("select idanni from ".$PHPR_TAB_PRE."anni where idanni = '".($anno + 1)."'");
if (numlin_query($anno_succ_esistente) == 1) {
$id_prenota_succ = "soloanno";
$data_succ_esistente = esegui_query("select idperiodi from $PHPR_TAB_PRE"."periodi".($anno + 1)." where datafine <= '$d_data_fine'");
if (numlin_query($data_succ_esistente) >= 1) {
$prenota_succ = esegui_query("select idprenota,iddatafine from $PHPR_TAB_PRE"."prenota".($anno + 1)." where iddatainizio = '0' and commento = '$id_prenota'");
if (numlin_query($prenota_succ) == 1) {
$id_prenota_succ = risul_query($prenota_succ,0,'idprenota');
$fineperiodo_succ = risul_query($prenota_succ,0,'iddatafine');
} # fine if (numlin_query($prenota_in_anno_succ) == 1)
} # fine if (numlin_query($data_succ_esistente) >= 1)
} # fine if (numlin_query($anno_succ_esistente) == 1)
} # fine if (substr($d_data_fine,0,4) != $anno)
return $id_prenota_succ;
} # fine function trova_prenota_succ




function trova_prenota_prec ($anno,$d_id_data_inizio,$PHPR_TAB_PRE,$dati_prenota_modifica,&$n_dati_prenota_modifica) {
$n_dati_prenota_modifica = "";
if ($d_id_data_inizio == 0) {
$id_prenota_prec = "SI";
$id_prenota_prec_esist = aggslashdb(risul_query($dati_prenota_modifica,0,'commento'));
if ($id_prenota_prec_esist) {
$anno_prec_esistente = esegui_query("select idanni from ".$PHPR_TAB_PRE."anni where idanni = '".($anno - 1)."'");
if (numlin_query($anno_prec_esistente) == 1) {
$n_dati_prenota_modifica = esegui_query("select * from $PHPR_TAB_PRE"."prenota".($anno - 1)." where idprenota = '$id_prenota_prec_esist'");
if (numlin_query($n_dati_prenota_modifica) == 1) $id_prenota_prec = $id_prenota_prec_esist;
} # fine if (numlin_query($anno_prec_esistente) == 1)
} # fine if ($id_prenota_prec_esist)
} # fine if ($d_id_data_inizio == 0)
return $id_prenota_prec;
} # fine function trova_prenota_prec




function cancella_prenotazione ($id_prenota,$cancella_pagato,$metodo_pagamento,$priv_mod_pagato,$lock,$anno,$PHPR_TAB_PRE,$id_utente,&$stato_canc) {
$pag = "modifica_prenota.php";
$tableprenota = $PHPR_TAB_PRE."prenota".$anno;
$tableperiodi = $PHPR_TAB_PRE."periodi".$anno;
$tablesoldi = $PHPR_TAB_PRE."soldi".$anno;
$tableanni = $PHPR_TAB_PRE."anni";
$tablepersonalizza = $PHPR_TAB_PRE."personalizza";
$tablecostiprenota = $PHPR_TAB_PRE."costiprenota".$anno;
$tablerclientiprenota = $PHPR_TAB_PRE."rclientiprenota".$anno;
$tableinterconnessioni = $PHPR_TAB_PRE."interconnessioni";

$continua_canc = "SI";
$tabelle_lock = array("$tableprenota","$tablecostiprenota","$tablerclientiprenota","$tablesoldi","$tableinterconnessioni");
$altre_tab_lock = array("$tableanni","$tableperiodi","$tablepersonalizza");
$dati_prenota = esegui_query("select * from $tableprenota where idprenota = '$id_prenota' and idclienti != '0' ");
$num_dati_prenota = numlin_query($dati_prenota);
if ($num_dati_prenota == 0) {
$testo .= mex("La prenotazione",$pag)." $id_prenota ".mex("era già stata cancellata",$pag)."!<br>";
$continua_canc = "NO";
} # fine if ($num_dati_prenota == 0)

# Controlli per vedere se la prenotazione è anche nell'anno precedente o successivo
if ($continua_canc != "NO") {
$id_prenota_succ = "";
$id_prenota_orig = "";
$d_id_data_inizio = risul_query($dati_prenota,0,'iddatainizio');
$d_id_data_fine = risul_query($dati_prenota,0,'iddatafine');
$id_prenota_succ = trova_prenota_succ($anno,$d_id_data_fine,$PHPR_TAB_PRE,$id_prenota,$fineperiodo_succ);
if ($id_prenota_succ == "soloanno") $id_prenota_succ = "";
if ($id_prenota_succ) {
$tableprenota_succ = $PHPR_TAB_PRE."prenota".($anno + 1);
$tablecostiprenota_succ = $PHPR_TAB_PRE."costiprenota".($anno + 1);
$tablerclientiprenota_succ = $PHPR_TAB_PRE."rclientiprenota".($anno + 1);
} # fine if ($id_prenota_succ)
$id_prenota_prec = trova_prenota_prec($anno,$d_id_data_inizio,$PHPR_TAB_PRE,$dati_prenota,$n_dati_prenota_modifica);
if ($id_prenota_prec == "SI") $id_prenota_prec = "";
if ($id_prenota_prec) {
$id_prenota_succ = $id_prenota;
$id_prenota = $id_prenota_prec;
$tableprenota_succ = $tableprenota;
$tablecostiprenota_succ = $tablecostiprenota;
$tablerclientiprenota_succ = $tablerclientiprenota;
$tableprenota = $PHPR_TAB_PRE."prenota".($anno - 1);
$tablecostiprenota = $PHPR_TAB_PRE."costiprenota".($anno - 1);
$tablerclientiprenota = $PHPR_TAB_PRE."rclientiprenota".($anno - 1);
$tablesoldi = $PHPR_TAB_PRE."soldi".($anno - 1);
} # fine if ($id_prenota_prec)
if ($id_prenota_succ) {
$tabelle_lock = array("$tableprenota","$tableprenota_succ","$tablecostiprenota","$tablecostiprenota_succ","$tablerclientiprenota","$tablerclientiprenota_succ","$tablesoldi","$tableinterconnessioni");
$altre_tab_lock = array("$tablepersonalizza");
} # fine if ($id_prenota_succ)
} # fine if ($continua_canc != "NO")

if ($lock) $tabelle_lock = lock_tabelle($tabelle_lock,$altre_tab_lock);
else $tabelle_lock = "";
$dati_prenota = esegui_query("select * from $tableprenota where idprenota = '$id_prenota' and idclienti != '0'");
$num_dati_prenota = numlin_query($dati_prenota);
if ($num_dati_prenota == 0 and $continua_canc != "NO") {
$testo .= mex("La prenotazione",$pag)." $id_prenota ".mex("era già stata cancellata",$pag)."!<br>";
$continua_canc = "NO";
} # fine if ($num_dati_prenota == 0 and $continua_canc != "NO")

if ($continua_canc != "NO") {
$d_pagato = risul_query($dati_prenota,0,pagato);
if ($d_pagato != 0) {
if ($cancella_pagato and $priv_mod_pagato == "s") {
if (get_magic_quotes_gpc()) $metodo_pagamento = (string) stripslashes($metodo_pagamento);
$metodo_pagamento = htmlspecialchars($metodo_pagamento);
if ($metodo_pagamento != "") {
$metodi_pagamento = esegui_query("select valpersonalizza from $tablepersonalizza where idpersonalizza = 'metodi_pagamento' and idutente = '$id_utente'");
$metodi_pagamento = risul_query($metodi_pagamento,0,'valpersonalizza');
$metodi_pagamento = explode(",",$metodi_pagamento);
$metodo_trovato = "NO";
for ($num1 = 0 ; $num1 < count($metodi_pagamento) ; $num1++) if ($metodi_pagamento[$num1] == $metodo_pagamento) $metodo_trovato = "SI";
if ($metodo_trovato == "NO") $metodo_pagamento = "";
} # fine if ($metodo_pagamento != "")
$metodo_pagamento = aggslashdb($metodo_pagamento);
$datainserimento_saldo = date("Y-m-d H:i:s",(time() + (C_DIFF_ORE * 3600)));
$motivazione_saldo = risul_query($dati_prenota,0,'idclienti');
$d_idinizioprenota = risul_query($dati_prenota,0,'iddatainizio');
$d_idfineprenota = risul_query($dati_prenota,0,'iddatafine');
$motivazione_saldo .= ";$d_idinizioprenota;$d_idfineprenota;$id_prenota";
$saldo = 0 - $d_pagato;
$idsoldi = esegui_query("select max(idsoldi) from $tablesoldi");
$idsoldi = risul_query($idsoldi,0,0) + 1;
esegui_query("insert into $tablesoldi (idsoldi,motivazione,saldo_prenota,soldi_prima,data_inserimento,metodo_pagamento,utente_inserimento) values ('$idsoldi','$motivazione_saldo','$saldo','$d_pagato','$datainserimento_saldo','$metodo_pagamento','$id_utente')");
} # fine if ($cancella_pagato and $priv_mod_pagato == "s")
else {
$soldi_prenota_cancellate = esegui_query("select * from $tablesoldi where idsoldi = 1");
$soldi_prenota_cancellate = risul_query($soldi_prenota_cancellate,0,'soldi_prima');
$soldi_prenota_cancellate = $soldi_prenota_cancellate + $d_pagato;
esegui_query("update $tablesoldi set soldi_prima = '$soldi_prenota_cancellate' where idsoldi = 1");
} # fine else if ($cancella_pagato and $priv_mod_pagato == "s")
} # fine if ($d_pagato != 0)
$d_prenota_comp = aggslashdb(risul_query($dati_prenota,0,'idprenota_compagna'));
if ($d_prenota_comp) {
$d_prenota_comp_vett = explode(",",$d_prenota_comp);
for ($num1 = 0 ; $num1 < count($d_prenota_comp_vett) ; $num1++) {
$idprenota_compagna = substr(str_replace(",".$d_prenota_comp_vett[$num1].",",",",",$d_prenota_comp,"),1,-1);
esegui_query("update $tableprenota set idprenota_compagna = '$idprenota_compagna' where idprenota = '".$d_prenota_comp_vett[$num1]."' ");
} # fine for $num1
} # fine if ($d_prenota_comp)
esegui_query("delete from $tableprenota where idprenota = '$id_prenota'");
esegui_query("delete from $tablecostiprenota where idprenota = '$id_prenota'");
esegui_query("delete from $tablerclientiprenota where idprenota = '$id_prenota'");
if ($id_prenota_succ) {
esegui_query("delete from $tableprenota_succ where idprenota = '$id_prenota_succ'");
esegui_query("delete from $tablecostiprenota_succ where idprenota = '$id_prenota_succ'");
esegui_query("delete from $tablerclientiprenota_succ where idprenota = '$id_prenota_succ'");
if ($d_id_data_inizio == 0 and $id_prenota_prec) {
esegui_query("delete from $tableinterconnessioni where idlocale = '$id_prenota' and tipoid = 'prenota' and anno = '".($anno - 1)."' ");
esegui_query("delete from $tableinterconnessioni where idlocale = '$id_prenota_succ' and tipoid = 'prenota' and anno = '$anno' ");
$id_prenota = $id_prenota_succ;
} # fine if ($d_id_data_inizio == 0 and $id_prenota_prec)
else {
esegui_query("delete from $tableinterconnessioni where idlocale = '$id_prenota' and tipoid = 'prenota' and anno = '$anno' ");
esegui_query("delete from $tableinterconnessioni where idlocale = '$id_prenota_succ' and tipoid = 'prenota' and anno = '".($anno + 1)."' ");
} # fine else if ($d_id_data_inizio == 0 and $id_prenota_prec)
} # fine if ($id_prenota_succ)
else esegui_query("delete from $tableinterconnessioni where idlocale = '$id_prenota' and tipoid = 'prenota' and anno = '$anno' ");
$testo .= mex("Prenotazione",$pag)." $id_prenota ".mex("cancellata",$pag)."!<br>";
} # fine if ($continua_canc != "NO")
$stato_canc = $continua_canc;
if ($tabelle_lock) unlock_tabelle($tabelle_lock);

return $testo;

} # fine function cancella_prenotazione



?>