Files
FHC-Core/include/wawi_konto.class.php
T
2010-10-11 13:45:20 +00:00

282 lines
7.8 KiB
PHP

<?php
/* Copyright (C) 2010 Technikum-Wien
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Authors: Christian Paminger <christian.paminger@technikum-wien.at>,
* Andreas Oesterreicher <andreas.oesterreicher@technikum-wien.at> and
* Karl Burkhart <burkhart@technikum-wien.at>.
*/
/**
* Klasse WaWi Konto
*/
require_once(dirname(__FILE__).'/basis_db.class.php');
class wawi_konto extends basis_db
{
public $new; // boolean
public $result = array(); // adresse Objekt
//Tabellenspalten
public $adresse_id; // integer
public $person_id; // integer
public $name; // string
public $strasse; // string
public $plz; // string
public $ort; // string
public $gemeinde; // string
public $nation; // string
public $typ; // string
public $heimatadresse; // boolean
public $zustelladresse; // boolean
public $firma_id; // integer
public $updateamum; // timestamp
public $updatevon; // string
public $insertamum; // timestamp
public $insertvon; // string
public $ext_id; // integer
/**
* Konstruktor
* @param $konto_id ID des Kontos das geladen werden soll (Default=null)
*/
public function __construct($konto_id=null)
{
parent::__construct();
if(!is_null($konto_id))
$this->load($konto_id);
}
/**
* Laedt das Konto mit der ID $konto_id
* @param $konto_id ID des zu ladenden Kontos
* @return true wenn ok, false im Fehlerfall
*/
public function load($konto_id)
{
//Pruefen ob konto_id eine gueltige Zahl ist
if(!is_numeric($konto_id) || $konto_id == '')
{
$this->errormsg = 'Konto_id muss eine Zahl sein';
return false;
}
//Daten aus der Datenbank lesen
$qry = "SELECT * FROM wawi.tbl_konto WHERE konto_id='".addslashes($konto_id)."'";
if(!$this->db_query($qry))
{
$this->errormsg = 'Fehler bei einer Datenbankabfrage';
return false;
}
if($row = $this->db_fetch_object())
{
$this->konto_id = $row->konto_id;
$this->aktiv = ($row->aktiv=='t'?true:false);
//...
}
else
{
$this->errormsg = 'Es ist kein Datensatz mit dieser ID vorhanden';
return false;
}
return true;
}
/**
* Laedt alle Konten
* @param $aktiv wenn true werden nur die aktiven Datensaetze geladen, sonst alle
* @param $order Sortierreihenfolge
* @return true wenn ok, false im Fehlerfall
*/
public function getAll($aktiv=null, $order='beschreibung DESC')
{
//Daten aus der Datenbank lesen
$qry = 'SELECT * FROM wawi.tbl_konto';
if(!is_null($aktiv))
{
$qry.='WHERE aktiv='.($aktiv?'true':'false');
}
if($order!='')
$qry .= ' ORDER BY '.$order;
if(!$this->db_query($qry))
{
$this->errormsg = 'Fehler bei einer Datenbankabfrage';
return false;
}
while($row = $this->db_fetch_object())
{
$obj = new wawi_konto();
$obj->konto_id = $row->konto_id;
//...
$this->result[] = $obj;
}
return true;
}
/**
* Prueft die Variablen auf Gueltigkeit
* @return true wenn ok, false im Fehlerfall
*/
protected function validate()
{
if(mb_strlen($this->bezeichnung)>265)
{
$this->errormsg = 'Bezeichnung darf nicht laenger als 256 Zeichen sein.';
return false;
}
$this->errormsg = '';
return true;
}
/**
* Speichert den aktuellen Datensatz in die Datenbank
* Wenn $neu auf true gesetzt ist wird ein neuer Datensatz angelegt
* andernfalls wird der Datensatz mit der ID in $adresse_id aktualisiert
* @param $new wenn true wird ein Insert durchgefuehrt, wenn false ein Update
* und wenn null wird das new-Objekt der Klasse verwendet
* @return true wenn ok, false im Fehlerfall
*/
public function save($new=null)
{
if(is_null($new))
$new = $this->new;
//Variablen pruefen
if(!$this->validate())
return false;
if($this->new)
{
//Neuen Datensatz einfuegen
$qry='BEGIN;INSERT INTO public.tbl_adresse (person_id, name, strasse, plz, typ, ort, nation, insertamum, insertvon,
gemeinde, heimatadresse, zustelladresse, firma_id, updateamum, updatevon, ext_id) VALUES('.
$this->addslashes($this->person_id).', '.
$this->addslashes($this->name).', '.
$this->addslashes($this->strasse).', '.
$this->addslashes($this->plz).', '.
$this->addslashes(trim($this->typ)).', '.
$this->addslashes($this->ort).', '.
$this->addslashes($this->nation).', now(), '.
$this->addslashes($this->insertvon).', '.
$this->addslashes($this->gemeinde).', '.
($this->heimatadresse?'true':'false').', '.
($this->zustelladresse?'true':'false').', '.
($this->firma_id!=null?$this->addslashes($this->firma_id):'null').', now(), '.
$this->addslashes($this->updatevon).', '.
$this->addslashes($this->ext_id).');';
}
else
{
//Pruefen ob adresse_id eine gueltige Zahl ist
if(!is_numeric($this->adresse_id))
{
$this->errormsg = 'adresse_id muss eine gültige Zahl sein: '.$this->adresse_id."\n";
return false;
}
$qry='UPDATE public.tbl_adresse SET'.
' person_id='.$this->addslashes($this->person_id).', '.
' name='.$this->addslashes($this->name).', '.
' strasse='.$this->addslashes($this->strasse).', '.
' plz='.$this->addslashes($this->plz).', '.
' typ='.$this->addslashes(trim($this->typ)).', '.
' ort='.$this->addslashes($this->ort).', '.
' nation='.$this->addslashes($this->nation).', '.
' gemeinde='.$this->addslashes($this->gemeinde).', '.
' firma_id='.$this->addslashes($this->firma_id).','.
' updateamum= now(), '.
' updatevon='.$this->addslashes($this->updatevon).', '.
' heimatadresse='.($this->heimatadresse?'true':'false').', '.
' zustelladresse='.($this->zustelladresse?'true':'false').' '.
'WHERE adresse_id='.$this->adresse_id.';';
}
if($this->db_query($qry))
{
if($this->new)
{
//naechste ID aus der Sequence holen
$qry="SELECT currval('public.tbl_adresse_adresse_id_seq') as id;";
if($this->db_query($qry))
{
if($row = $this->db_fetch_object())
{
$this->adresse_id = $row->id;
$this->db_query('COMMIT');
}
else
{
$this->db_query('ROLLBACK');
$this->errormsg = "Fehler beim Auslesen der Sequence";
return false;
}
}
else
{
$this->db_query('ROLLBACK');
$this->errormsg = 'Fehler beim Auslesen der Sequence';
return false;
}
}
}
else
{
$this->errormsg = 'Fehler beim Speichern des Adress-Datensatzes';
return false;
}
return $this->adresse_id;
}
/**
* Loescht den Datenensatz mit der ID die uebergeben wird
* @param $adresse_id ID die geloescht werden soll
* @return true wenn ok, false im Fehlerfall
*/
public function delete($adresse_id)
{
//Pruefen ob adresse_id eine gueltige Zahl ist
if(!is_numeric($adresse_id) || $adresse_id == '')
{
$this->errormsg = 'adresse_id muss eine gültige Zahl sein'."\n";
return false;
}
//loeschen des Datensatzes
$qry="DELETE FROM public.tbl_adresse WHERE adresse_id='".addslashes($adresse_id)."';";
if($this->db_query($qry))
{
return true;
}
else
{
$this->errormsg = 'Fehler beim Löschen der Daten'."\n";
return false;
}
}
}
?>