355 lines
11 KiB
PHP
355 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Buchung Klasse für manuelle EÜR-Buchungen
|
|
*
|
|
* @package steuer
|
|
* @subpackage class
|
|
*/
|
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
|
|
|
|
/**
|
|
* Klasse zur Verwaltung von manuellen EÜR-Buchungen
|
|
*/
|
|
class SteuerBuchung extends CommonObject
|
|
{
|
|
public $element = 'steuer_buchung';
|
|
public $table_element = 'steuer_buchung';
|
|
public $picto = 'bill';
|
|
|
|
public $id;
|
|
public $ref;
|
|
public $datum;
|
|
public $belegnummer;
|
|
public $beschreibung;
|
|
public $fk_konto;
|
|
public $betrag_netto;
|
|
public $betrag_ust;
|
|
public $betrag_brutto;
|
|
public $ust_satz;
|
|
public $typ; // 'einnahme' oder 'ausgabe'
|
|
public $zahlungsart;
|
|
public $fk_soc;
|
|
public $fk_facture;
|
|
public $fk_facture_fourn;
|
|
public $note_private;
|
|
public $date_creation;
|
|
public $tms;
|
|
public $fk_user_creat;
|
|
public $fk_user_modif;
|
|
public $status;
|
|
public $entity;
|
|
|
|
// Verknüpfte Objekte
|
|
public $konto_bezeichnung;
|
|
public $konto_nummer;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param DoliDB $db Database handler
|
|
*/
|
|
public function __construct($db)
|
|
{
|
|
global $conf;
|
|
$this->db = $db;
|
|
$this->entity = $conf->entity;
|
|
}
|
|
|
|
/**
|
|
* Erstellt eine neue Buchung
|
|
*
|
|
* @param User $user Benutzer
|
|
* @param int $notrigger Keine Trigger ausführen
|
|
* @return int >0 bei Erfolg, <0 bei Fehler
|
|
*/
|
|
public function create($user, $notrigger = 0)
|
|
{
|
|
global $conf;
|
|
|
|
$error = 0;
|
|
$now = dol_now();
|
|
|
|
// Standardwerte
|
|
if (empty($this->status)) {
|
|
$this->status = 1;
|
|
}
|
|
$this->entity = $conf->entity;
|
|
|
|
// Referenz generieren
|
|
if (empty($this->ref)) {
|
|
$this->ref = $this->getNextRef();
|
|
}
|
|
|
|
// Brutto berechnen falls nicht gesetzt
|
|
if (empty($this->betrag_brutto)) {
|
|
$this->betrag_brutto = $this->betrag_netto + $this->betrag_ust;
|
|
}
|
|
|
|
$this->db->begin();
|
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."steuer_buchung (";
|
|
$sql .= "ref, datum, belegnummer, beschreibung, fk_konto,";
|
|
$sql .= " betrag_netto, betrag_ust, betrag_brutto, ust_satz,";
|
|
$sql .= " typ, zahlungsart, fk_soc, fk_facture, fk_facture_fourn,";
|
|
$sql .= " note_private, date_creation, fk_user_creat, status, entity";
|
|
$sql .= ") VALUES (";
|
|
$sql .= "'".$this->db->escape($this->ref)."',";
|
|
$sql .= "'".$this->db->escape($this->datum)."',";
|
|
$sql .= ($this->belegnummer ? "'".$this->db->escape($this->belegnummer)."'" : "NULL").",";
|
|
$sql .= "'".$this->db->escape($this->beschreibung)."',";
|
|
$sql .= ((int) $this->fk_konto).",";
|
|
$sql .= ((float) $this->betrag_netto).",";
|
|
$sql .= ((float) $this->betrag_ust).",";
|
|
$sql .= ((float) $this->betrag_brutto).",";
|
|
$sql .= ((float) $this->ust_satz).",";
|
|
$sql .= "'".$this->db->escape($this->typ)."',";
|
|
$sql .= ($this->zahlungsart ? "'".$this->db->escape($this->zahlungsart)."'" : "NULL").",";
|
|
$sql .= ($this->fk_soc > 0 ? ((int) $this->fk_soc) : "NULL").",";
|
|
$sql .= ($this->fk_facture > 0 ? ((int) $this->fk_facture) : "NULL").",";
|
|
$sql .= ($this->fk_facture_fourn > 0 ? ((int) $this->fk_facture_fourn) : "NULL").",";
|
|
$sql .= ($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "NULL").",";
|
|
$sql .= "'".$this->db->idate($now)."',";
|
|
$sql .= ((int) $user->id).",";
|
|
$sql .= ((int) $this->status).",";
|
|
$sql .= ((int) $this->entity);
|
|
$sql .= ")";
|
|
|
|
$resql = $this->db->query($sql);
|
|
if (!$resql) {
|
|
$error++;
|
|
$this->errors[] = "Error ".$this->db->lasterror();
|
|
}
|
|
|
|
if (!$error) {
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."steuer_buchung");
|
|
}
|
|
|
|
if (!$error) {
|
|
$this->db->commit();
|
|
return $this->id;
|
|
} else {
|
|
$this->db->rollback();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Aktualisiert eine Buchung
|
|
*
|
|
* @param User $user Benutzer
|
|
* @param int $notrigger Keine Trigger ausführen
|
|
* @return int >0 bei Erfolg, <0 bei Fehler
|
|
*/
|
|
public function update($user, $notrigger = 0)
|
|
{
|
|
$error = 0;
|
|
|
|
// Brutto berechnen
|
|
$this->betrag_brutto = $this->betrag_netto + $this->betrag_ust;
|
|
|
|
$this->db->begin();
|
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."steuer_buchung SET";
|
|
$sql .= " datum = '".$this->db->escape($this->datum)."',";
|
|
$sql .= " belegnummer = ".($this->belegnummer ? "'".$this->db->escape($this->belegnummer)."'" : "NULL").",";
|
|
$sql .= " beschreibung = '".$this->db->escape($this->beschreibung)."',";
|
|
$sql .= " fk_konto = ".((int) $this->fk_konto).",";
|
|
$sql .= " betrag_netto = ".((float) $this->betrag_netto).",";
|
|
$sql .= " betrag_ust = ".((float) $this->betrag_ust).",";
|
|
$sql .= " betrag_brutto = ".((float) $this->betrag_brutto).",";
|
|
$sql .= " ust_satz = ".((float) $this->ust_satz).",";
|
|
$sql .= " typ = '".$this->db->escape($this->typ)."',";
|
|
$sql .= " zahlungsart = ".($this->zahlungsart ? "'".$this->db->escape($this->zahlungsart)."'" : "NULL").",";
|
|
$sql .= " fk_soc = ".($this->fk_soc > 0 ? ((int) $this->fk_soc) : "NULL").",";
|
|
$sql .= " note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "NULL").",";
|
|
$sql .= " fk_user_modif = ".((int) $user->id);
|
|
$sql .= " WHERE rowid = ".((int) $this->id);
|
|
|
|
$resql = $this->db->query($sql);
|
|
if (!$resql) {
|
|
$error++;
|
|
$this->errors[] = "Error ".$this->db->lasterror();
|
|
}
|
|
|
|
if (!$error) {
|
|
$this->db->commit();
|
|
return 1;
|
|
} else {
|
|
$this->db->rollback();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Lädt eine Buchung
|
|
*
|
|
* @param int $id ID der Buchung
|
|
* @param string $ref Referenz der Buchung
|
|
* @return int >0 bei Erfolg, <0 bei Fehler
|
|
*/
|
|
public function fetch($id, $ref = '')
|
|
{
|
|
global $conf;
|
|
|
|
$sql = "SELECT b.*, k.kontonummer, k.bezeichnung as konto_bezeichnung";
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."steuer_buchung as b";
|
|
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."steuer_konto as k ON b.fk_konto = k.rowid";
|
|
$sql .= " WHERE b.entity = ".((int) $conf->entity);
|
|
if ($id > 0) {
|
|
$sql .= " AND b.rowid = ".((int) $id);
|
|
} elseif ($ref) {
|
|
$sql .= " AND b.ref = '".$this->db->escape($ref)."'";
|
|
}
|
|
|
|
$resql = $this->db->query($sql);
|
|
if ($resql) {
|
|
if ($this->db->num_rows($resql)) {
|
|
$obj = $this->db->fetch_object($resql);
|
|
|
|
$this->id = $obj->rowid;
|
|
$this->ref = $obj->ref;
|
|
$this->datum = $this->db->jdate($obj->datum);
|
|
$this->belegnummer = $obj->belegnummer;
|
|
$this->beschreibung = $obj->beschreibung;
|
|
$this->fk_konto = $obj->fk_konto;
|
|
$this->betrag_netto = $obj->betrag_netto;
|
|
$this->betrag_ust = $obj->betrag_ust;
|
|
$this->betrag_brutto = $obj->betrag_brutto;
|
|
$this->ust_satz = $obj->ust_satz;
|
|
$this->typ = $obj->typ;
|
|
$this->zahlungsart = $obj->zahlungsart;
|
|
$this->fk_soc = $obj->fk_soc;
|
|
$this->fk_facture = $obj->fk_facture;
|
|
$this->fk_facture_fourn = $obj->fk_facture_fourn;
|
|
$this->note_private = $obj->note_private;
|
|
$this->date_creation = $this->db->jdate($obj->date_creation);
|
|
$this->tms = $obj->tms;
|
|
$this->fk_user_creat = $obj->fk_user_creat;
|
|
$this->fk_user_modif = $obj->fk_user_modif;
|
|
$this->status = $obj->status;
|
|
$this->entity = $obj->entity;
|
|
|
|
$this->konto_bezeichnung = $obj->konto_bezeichnung;
|
|
$this->konto_nummer = $obj->kontonummer;
|
|
|
|
return 1;
|
|
}
|
|
return 0;
|
|
} else {
|
|
$this->error = $this->db->lasterror();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Löscht eine Buchung
|
|
*
|
|
* @param User $user Benutzer
|
|
* @param int $notrigger Keine Trigger ausführen
|
|
* @return int >0 bei Erfolg, <0 bei Fehler
|
|
*/
|
|
public function delete($user, $notrigger = 0)
|
|
{
|
|
$error = 0;
|
|
|
|
$this->db->begin();
|
|
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."steuer_buchung";
|
|
$sql .= " WHERE rowid = ".((int) $this->id);
|
|
|
|
$resql = $this->db->query($sql);
|
|
if (!$resql) {
|
|
$error++;
|
|
$this->errors[] = "Error ".$this->db->lasterror();
|
|
}
|
|
|
|
if (!$error) {
|
|
$this->db->commit();
|
|
return 1;
|
|
} else {
|
|
$this->db->rollback();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generiert die nächste Referenznummer
|
|
*
|
|
* @return string Nächste Referenz
|
|
*/
|
|
private function getNextRef()
|
|
{
|
|
global $conf;
|
|
|
|
$sql = "SELECT MAX(CAST(SUBSTRING(ref, 5) AS UNSIGNED)) as maxref";
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."steuer_buchung";
|
|
$sql .= " WHERE ref LIKE 'BUC-%'";
|
|
$sql .= " AND entity = ".((int) $conf->entity);
|
|
|
|
$resql = $this->db->query($sql);
|
|
if ($resql) {
|
|
$obj = $this->db->fetch_object($resql);
|
|
$num = isset($obj->maxref) ? $obj->maxref + 1 : 1;
|
|
return 'BUC-'.sprintf('%06d', $num);
|
|
}
|
|
return 'BUC-000001';
|
|
}
|
|
|
|
/**
|
|
* Gibt den Link zur Buchung zurück
|
|
*
|
|
* @param int $withpicto Mit Piktogramm
|
|
* @param string $option Option
|
|
* @return string HTML-Link
|
|
*/
|
|
public function getNomUrl($withpicto = 0, $option = '')
|
|
{
|
|
global $langs;
|
|
|
|
$result = '';
|
|
$url = dol_buildpath('/steuer/buchung_card.php', 1).'?id='.$this->id;
|
|
|
|
$label = '<u>'.$langs->trans("Buchung").'</u><br>';
|
|
$label .= '<b>'.$langs->trans("Ref").':</b> '.$this->ref;
|
|
|
|
$linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
|
|
$linkend = '</a>';
|
|
|
|
if ($withpicto) {
|
|
$result .= img_object($label, $this->picto, 'class="classfortooltip"');
|
|
}
|
|
$result .= $linkstart.$this->ref.$linkend;
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Holt alle Konten für Dropdown
|
|
*
|
|
* @param string $typ Optional: 'einnahme' oder 'ausgabe'
|
|
* @return array Konten-Array
|
|
*/
|
|
public static function getKonten($db, $typ = '')
|
|
{
|
|
global $conf;
|
|
|
|
$konten = array();
|
|
$sql = "SELECT rowid, kontonummer, bezeichnung, kategorie";
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."steuer_konto";
|
|
$sql .= " WHERE aktiv = 1";
|
|
$sql .= " AND entity = ".((int) $conf->entity);
|
|
if ($typ) {
|
|
$sql .= " AND kategorie = '".$db->escape($typ)."'";
|
|
}
|
|
$sql .= " ORDER BY kontonummer";
|
|
|
|
$resql = $db->query($sql);
|
|
if ($resql) {
|
|
while ($obj = $db->fetch_object($resql)) {
|
|
$konten[$obj->rowid] = $obj->kontonummer.' - '.$obj->bezeichnung;
|
|
}
|
|
}
|
|
return $konten;
|
|
}
|
|
}
|