<?php

##################################################################################
# PHP-RESIDENCE
# Copyright (C) 2001-2010 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/>.
##################################################################################

$pag = "costi.php";
$titolo = "Costi di gestione.";

include("./dati/dati_connessione.php");
include("./includes/funzioni_$PHPR_DB_TYPE.php");
$numconnessione = connetti_db($PHPR_DB_NAME,$PHPR_DB_HOST,$PHPR_DB_PORT,$PHPR_DB_USER,$PHPR_DB_PASS,$PHPR_LOAD_EXT);
include("./includes/funzioni.php");
$tablecosti = $PHPR_TAB_PRE."costi".$anno;
$tableutenti = $PHPR_TAB_PRE."utenti";


$id_utente = controlla_login($numconnessione,$PHPR_TAB_PRE,$id_sessione,$nome_utente_phpr,$password_phpr,$anno);
if ($id_utente) {

if ($id_utente != 1) {
$tableprivilegi = $PHPR_TAB_PRE."privilegi";
$privilegi_annuali_utente = esegui_query("select * from $tableprivilegi where idutente = '$id_utente' and anno = '$anno'");
if (numlin_query($privilegi_annuali_utente) == 0) $anno_utente_attivato = "NO";
else {
$anno_utente_attivato = "SI";
$priv_ins_costi = risul_query($privilegi_annuali_utente,0,'priv_ins_costi');
$priv_ins_spese = substr($priv_ins_costi,0,1);
$priv_ins_entrate = substr($priv_ins_costi,1,1);
$priv_entrate_da_prenota = substr($priv_ins_costi,2,1);
$priv_persona_ins_costi = substr($priv_ins_costi,3,1);
} # fine else if (numlin_query($privilegi_annuali_utente) == 0)
$nome_utente = esegui_query("select * from $tableutenti where idutenti = '$id_utente'");
$nome_utente = risul_query($nome_utente,0,'nome_utente');
} # fine if ($id_utente != 1)
else {
$anno_utente_attivato = "SI";
$priv_ins_spese = "s";
$priv_ins_entrate = "s";
$priv_entrate_da_prenota = "c";
$priv_persona_ins_costi = "c";
} # fine else if ($id_utente != 1)

if (defined("C_MASSIMO_NUM_COSTI") and C_MASSIMO_NUM_COSTI != 0) {
$num_costi_esistenti = esegui_query("select idcosti from $tablecosti");
$num_costi_esistenti = numlin_query($num_costi_esistenti);
if ($num_costi_esistenti >= (C_MASSIMO_NUM_COSTI + 1)) {
$priv_ins_spese = "n";
$priv_ins_entrate = "n";
} # fine if ($num_costi_esistenti >= (C_MASSIMO_NUM_COSTI + 1))
} # fine if (defined("C_MASSIMO_NUM_COSTI") and C_MASSIMO_NUM_COSTI != 0)

if ($anno_utente_attivato == "SI") {


if ($tema[$id_utente] and $tema[$id_utente] != "base" and @is_dir("./themes/".$tema[$id_utente]."/php")) include("./themes/".$tema[$id_utente]."/php/head.php");
else include("./includes/head.php");



$Euro = nome_valuta();

if ($inserisci_entrata and $priv_ins_entrate == "s") {

if (get_magic_quotes_gpc()) $nome_costo = stripslashes($nome_costo);
$nome_costo = htmlspecialchars($nome_costo);
$nome_costo = aggslashdb($nome_costo);
if (get_magic_quotes_gpc()) $persona_costo = stripslashes($persona_costo);
$persona_costo = htmlspecialchars($persona_costo);
$persona_costo = aggslashdb($persona_costo);
unset($provenienza_costo);
if (!$nome_costo) {
echo mex("Si deve inserire un nome per l' entrata",$pag).".<br>";
$inserire = "NO";
} # fine if (!$nome_costo)

if (!$val_costo) {
echo mex("Si deve inserire il valore dell' entrata",$pag).".<br>";
$inserire = "NO";
} # fine if (!$val_costo)
else {
$val_costo = formatta_soldi($val_costo);
if (controlla_soldi($val_costo) == "NO") {
echo mex("Il valore dell' entrata è sbagliato",$pag).".<br>";
$inserire = "NO";
} # fine if (controlla_soldi($val_costo) == "NO")
} # fine else if (!$val_costo)

if ($inserire != "NO") {
$tabelle_lock = array($tablecosti);
$tabelle_lock = lock_tabelle($tabelle_lock);
$datainserimento = date("Y-m-d H:i:s",(time() + (C_DIFF_ORE * 3600)));
if ($priv_persona_ins_costi != "c") unset($persona_costo);
if ($priv_persona_ins_costi == "u") $persona_costo = $nome_utente;
if ($priv_entrate_da_prenota != "c") unset($entrata_da_prenota);
if ($priv_entrate_da_prenota == "s") $entrata_da_prenota = "SI";
if ($entrata_da_prenota) {
$provenienza_costo = "p";
if ($anno >= substr($datainserimento,0,4)) {
$costo0 = esegui_query("select * from $tablecosti where idcosti = 0");
$costo0 = risul_query($costo0,0,'val_costo');
$costo0 = $costo0 + $val_costo;
esegui_query("update $tablecosti set val_costo = '$costo0' where idcosti = 0 ");
} # fine if ($anno >= substr($datainserimento,0,4))
} # fine if ($entrata_da_prenota)
$idcosti = esegui_query("select max(idcosti) from $tablecosti");
$idcosti = risul_query($idcosti,0,0) + 1;
esegui_query("insert into $tablecosti (idcosti,nome_costo,val_costo,persona_costo,provenienza_costo,tipo_costo,datainserimento,hostinserimento,utente_inserimento) values ('$idcosti','$nome_costo','$val_costo','$persona_costo','$provenienza_costo','e','$datainserimento','$HOSTNAME','$id_utente') ");
echo mex("L' entrata è stata inserita",$pag).".<br>";
unlock_tabelle($tabelle_lock);
} # fine if ($inserire != "NO")

echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"costi.php\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input class=\"sbutton\" type=\"submit\" name=\"torna\" value=\"OK\">
</div></form>";
} # fine if ($inserisci_entrata and $priv_ins_entrate == "s")


else {


if ($inserisci_spesa and $priv_ins_spese == "s") {

if (get_magic_quotes_gpc()) $nome_costo = stripslashes($nome_costo);
$nome_costo = htmlspecialchars($nome_costo);
$nome_costo = aggslashdb($nome_costo);
if (get_magic_quotes_gpc()) $persona_costo = stripslashes($persona_costo);
$persona_costo = htmlspecialchars($persona_costo);
$persona_costo = aggslashdb($persona_costo);
if (!$nome_costo) {
echo mex("Si deve inserire un nome per la spesa",$pag).".<br>";
$inserire = "NO";
} # fine if (!$nomecosto)

if (!$val_costo) {
echo mex("Si deve inserire il valore della spesa",$pag).".<br>";
$inserire = "NO";
} # fine if (!$val_costo)
else {
$val_costo = formatta_soldi($val_costo);
if (controlla_soldi($val_costo) == "NO") {
echo mex("Il valore della spesa è sbagliato",$pag).".<br>";
$inserire = "NO";
} # fine if (controlla_soldi($val_costo) == "NO")
} # fine else if (!$val_costo)

if ($inserire != "NO") {
$tabelle_lock = array($tablecosti);
$tabelle_lock = lock_tabelle($tabelle_lock);
$datainserimento = date("Y-m-d H:i:s",(time() + (C_DIFF_ORE * 3600)));
if ($priv_persona_ins_costi != "c") unset($persona_costo);
if ($priv_persona_ins_costi == "u") $persona_costo = $nome_utente;
$idcosti = esegui_query("select max(idcosti) from $tablecosti");
$idcosti = risul_query($idcosti,0,0) + 1;
esegui_query("insert into $tablecosti (idcosti,nome_costo,val_costo,persona_costo,tipo_costo,datainserimento,hostinserimento,utente_inserimento) values ('$idcosti','$nome_costo','$val_costo','$persona_costo','s','$datainserimento','$HOSTNAME','$id_utente') ");
echo mex("La spesa è stata inserita",$pag).".<br>";
unlock_tabelle($tabelle_lock);
} # fine if ($inserire != "NO")

echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"costi.php\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input class=\"sbutton\" type=\"submit\" name=\"torna\" value=\"OK\">
</div></form>";
} # fine if ($inserisci_spesa and $priv_ins_spese == "s")


else {



if ($priv_ins_spese == "s") {
echo "
<h3>".mex("Inserisci i costi di gestione per l'anno",$pag)." $anno.</h3>
<form accept-charset=\"utf-8\" method=\"post\" action=\"costi.php\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\"><br>
<table style=\"margin-left: auto; margin-right: auto;\" cellspacing=2 cellpadding=5>
<tr><td>
".mex("Natura spesa",$pag).":&nbsp;
<input type=\"text\" name=\"nome_costo\" size=\"30\" value=\"\">
</td></tr>
<tr><td>
".mex("Importo",$pag).":&nbsp;
<input type=\"text\" name=\"val_costo\" size=\"10\" value=\"\">&nbsp;$Euro.
</td></tr>";
if ($priv_persona_ins_costi == "c") {
echo "<tr><td>".mex("Persona che inserisce",$pag).":&nbsp;
<input type=\"text\" name=\"persona_costo\" size=\"20\" value=\"\">&nbsp;(".mex("opzionale",$pag).").
</td></tr>";
} # fine if ($priv_persona_ins_costi == "c")
echo "</table><div style=\"text-align: center;\">
<input class=\"sbutton\" type=\"submit\" name=\"inserisci_spesa\" value=\"".mex("Inserisci la spesa",$pag)."\"><br>
</div></div></form>
<table><tr><td style=\"height: 8px;\"></td></tr></table><hr style=\"width: 95%\">";
} # fine if ($priv_ins_spese == "s")

if ($priv_ins_entrate == "s") {
echo "<h3>".mex("Inserisci le entrate in cassa per l'anno",$pag)." $anno.</h3>
<form accept-charset=\"utf-8\" method=\"post\" action=\"costi.php\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\"><br>
<table style=\"margin-left: auto; margin-right: auto;\" cellspacing=2 cellpadding=5>
<tr><td>
".mex("Natura entrata",$pag).":&nbsp;
<input type=\"text\" name=\"nome_costo\" size=\"30\" value=\"\">
</td></tr>
<tr><td>
".mex("Importo",$pag).":&nbsp;
<input type=\"text\" name=\"val_costo\" size=\"10\" value=\"\">&nbsp;$Euro.
</td></tr>";
if ($priv_persona_ins_costi == "c") {
echo "<tr><td>".mex("Persona che inserisce",$pag).":&nbsp;
<input type=\"text\" name=\"persona_costo\" size=\"20\" value=\"\">&nbsp;(".mex("opzionale",$pag).").
</td></tr>";
} # fine if ($priv_persona_ins_costi == "c")
if ($priv_entrate_da_prenota == "c") {
echo "<tr><td><input type=\"checkbox\" name=\"entrata_da_prenota\" value=\"SI\" checked>
".mex("Sottrai l'importo dal totale delle prenotazioni",$pag).".
</td></tr>";
} # fine if ($priv_entrate_da_prenota == "c")
echo "</table><div style=\"text-align: center;\">
<input class=\"sbutton\" type=\"submit\" name=\"inserisci_entrata\" value=\"".mex("Inserisci l' entrata",$pag)."\"><br>
</div></div></form>
<table><tr><td style=\"height: 8px;\"></td></tr></table><hr style=\"width: 95%\">";
} # fine if ($priv_ins_entrate == "s")

echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"visualizza_tabelle.php\"><div style=\"text-align: center;\">
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"tipo_tabella\" value=\"costi\">
<input class=\"sbutton\" type=\"submit\" name=\"visualizza\" value=\"".mex("Visualizza la tabella con tutte le spese e le entrate",$pag)."\">
</div></form><table><tr><td style=\"height: 8px;\"></td></tr></table>
<form accept-charset=\"utf-8\" method=\"post\" action=\"inizio.php\"><div style=\"text-align: center;\">
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input class=\"sbutton\" type=\"submit\" name=\"indietro\" value=\"".mex("Torna al menù principale",$pag)."\">
</div></form><table><tr><td style=\"height: 20px;\"></td></tr></table>";


} # fine else if ($inserisci_spesa and $priv_ins_spese == "s")

} # fine else if ($inserisci_entrata and $priv_ins_entrate == "s")



if ($tema[$id_utente] and $tema[$id_utente] != "base" and @is_dir("./themes/".$tema[$id_utente]."/php")) include("./themes/".$tema[$id_utente]."/php/foot.php");
else include("./includes/foot.php");


} # fine if ($anno_utente_attivato == "SI")
} # fine if ($id_utente)


?>