classes.inc
author viric@vicerveza
Thu, 18 May 2006 23:47:03 +0200
changeset 0 c270c8b5ddea
permissions -rw-r--r--
Initial release. Usable.

<?php

include("strings.inc");

class ingres
{
var $dia;
var $mes;
var $any;
var $hora;
var $minut;
var $modificat; // Timestamp
var $rao;
var $butxaca;
var $tipus;
var $diners;
var $comentari;

function data()
{
	return mktime($this->hora, $this->minut, 0, $this->mes, $this->dia,
		$this->any);
}

function setData($data)
{
	$this->dia = date('d',$data);
	$this->mes = date('m',$data);
	$this->any = date('Y',$data);
	$this->hora = date('H',$data);
	$this->minut = date('i',$data);
}

function DBAfegir($connexio)
{
	$query = "INSERT INTO ingressos
	(data,data_modif,rao,quantitat,
		butxaca,comentari) 
		VALUES(";
	$query .= "FROM_UNIXTIME(" . mktime($this->hora,
			$this->minut,
			0,
			$this->mes,
			$this->dia,
			$this->any) . "), ";
	$query .= "FROM_UNIXTIME(". time() . "), ";
	$query .= "'" . strtosql( $this->rao) . "', ";
	if($this->tipus == "ingres")
		$query .= $this->diners . ", ";
	else
		$query .= "-". $this->diners . ", ";
	$query .= $this->butxaca . ", ";
	$query .= "'" . strtosql( $this->comentari) . "'); ";


	$resultat = mysql_query($query, $connexio);

	if (!$resultat)
	{
		echo mysql_errno().": ".mysql_error()."<BR>";
		return 1;
	}

	return 0;
}

function DBModificar($connexio, $data_vella)
{
	$query = "UPDATE ingressos SET ";
	$query .= "data=FROM_UNIXTIME(" . $this->data() . "), ";
	$query .= "data_modif=FROM_UNIXTIME(". time() . "), ";
	$query .= "rao='" . strtosql( $this->rao) . "', ";
	$query .= "quantitat=";
	if($this->tipus == "ingres")
		$query .= $this->diners . ", ";
	else
		$query .= "-". $this->diners . ", ";
	$query .= "butxaca=" . $this->butxaca . ", ";
	$query .= "comentari='" . strtosql( $this->comentari) . "' ";
	$query .= "WHERE data=FROM_UNIXTIME(" . $data_vella . ");";


	$resultat = mysql_query($query, $connexio);

	if (!$resultat)
	{
		echo mysql_errno().": ".mysql_error()."<BR>";
		return 1;
	}

	return 0;
}

function DBBorrar($connexio)
{
	$query = "DELETE FROM ingressos WHERE UNIX_TIMESTAMP(data) = ";
	$query .= $this->data() . ";";

	$resultat = mysql_query($query,$connexio);
	if (!$resultat)
	{
		echo mysql_errno().": ".mysql_error()."<BR>";
		return 1;
	}
	return 0;
	
}

function ingres($connexio=-1, $data=0)
{
	// De nul
	if ($connexio==-1)
	{
		$this->dia = date('d');
		$this->mes = date('m');
		$this->any = date('Y');
		$this->hora = date('H');
		$this->minut = date('i');
		$this->rao = "";
		$this->butxaca = -1;
		$this->tipus = "despesa";
		$this->diners = 0;
		$this->comentari = "";
	}
	else
	{
		$query = "SELECT UNIX_TIMESTAMP(data),
		UNIX_TIMESTAMP(data_modif), rao, butxaca, quantitat,
		comentari FROM ingressos WHERE UNIX_TIMESTAMP(data) = ";
		$query .= $data . ";";

		$resultat = mysql_query($query,$connexio);
		if (!$resultat)
		{
			echo mysql_errno().": ".mysql_error()."<BR>";
			return 1; // Què passa amb un return aquí?
		}
		
		// Només hi haurà un resultat
		$fila = mysql_fetch_row($resultat);
		
		settype($fila[4], float);

		$this->dia = date('d',$fila[0]);
		$this->mes = date('m',$fila[0]);
		$this->any = date('Y',$fila[0]);
		$this->hora = date('H',$fila[0]);
		$this->minut = date('i',$fila[0]);
		$this->modificat = $fila[1];
		$this->rao = $fila[2];
		$this->butxaca = $fila[3];
		if ($fila[4] > 0)
			$this->tipus = "ingres";
		else
			$this->tipus = "despesa";
		$this->diners = abs($fila[4]);
		$this->comentari = $fila[5];

	}
}

}

class moviment
{
var $dia;
var $mes;
var $any;
var $hora;
var $minut;
var $modificat; // Timestamp
var $rao;
var $butxaca_origen;
var $butxaca_desti;
var $diners;
var $comentari;


function data()
{
	return mktime($this->hora, $this->minut, 0, $this->mes, $this->dia,
		$this->any);
}
function setData($data)
{
	$this->dia = date('d',$data);
	$this->mes = date('m',$data);
	$this->any = date('Y',$data);
	$this->hora = date('H',$data);
	$this->minut = date('i',$data);
}

function DBAfegir($connexio)
{
	$query = "INSERT INTO moviments
	(data,data_modif,rao,quantitat,
		butxaca_origen,butxaca_desti,comentari) 
		VALUES(";
	$query .= "FROM_UNIXTIME(" . $this->data() . "), ";
	$query .= "FROM_UNIXTIME(". time() . "), ";
	$query .= "'" . strtosql( $this->rao) . "', ";
	$query .= $this->diners . ", ";
	$query .= $this->butxaca_origen . ", ";
	$query .= $this->butxaca_desti . ", ";
	$query .= "'" . strtosql( $this->comentari) . "'); ";


	$resultat = mysql_query($query, $connexio);

	if (!$resultat)
	{
		echo mysql_errno().": ".mysql_error()."<BR>";
		return 1;
	}

	return 0;
}

function DBBorrar($connexio)
{
	$query = "DELETE FROM moviments WHERE UNIX_TIMESTAMP(data) = ";
	$query .= mktime($this->hora,
			$this->minut,
			0,
			$this->mes,
			$this->dia,
			$this->any) . ";";

	$resultat = mysql_query($query,$connexio);
	if (!$resultat)
	{
		echo mysql_errno().": ".mysql_error()."<BR>";
		return 1;
	}
	return 0;
	
}

function moviment($connexio=-1, $data=0)
{
	// De nul
	if ($connexio==-1)
	{
		$this->dia = date('d');
		$this->mes = date('m');
		$this->any = date('Y');
		$this->hora = date('H');
		$this->minut = date('i');
		$this->rao = "";
		$this->butxaca_origen = -1;
		$this->butxaca_desti = -1;
		$this->diners = 0;
		$this->comentari = "";
	}
	else
	{
		$query = "SELECT UNIX_TIMESTAMP(data),
		UNIX_TIMESTAMP(data_modif), rao, butxaca_origen, butxaca_desti,
		quantitat,
		comentari FROM moviments WHERE UNIX_TIMESTAMP(data) = ";
		$query .= $data . ";";

		$resultat = mysql_query($query,$connexio);
		if (!$resultat)
		{
			echo mysql_errno().": ".mysql_error()."<BR>";
			return 1; // Què passa amb un return aquí?
		}
		
		// Només hi haurà un resultat
		$fila = mysql_fetch_row($resultat);
		
		settype($fila[4], float);

		$this->dia = date('d',$fila[0]);
		$this->mes = date('m',$fila[0]);
		$this->any = date('Y',$fila[0]);
		$this->hora = date('H',$fila[0]);
		$this->minut = date('i',$fila[0]);
		$this->modificat = $fila[1];
		$this->rao = $fila[2];
		$this->butxaca_origen = $fila[3];
		$this->butxaca_desti = $fila[4];
		$this->diners = abs($fila[5]);
		$this->comentari = $fila[6];

	}
}

}

?>