135 lines
4.2 KiB
PHP
135 lines
4.2 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../../core/Database.php';
|
|
require_once 'AnschriftModel.php';
|
|
require_once 'BankverbindungModel.php';
|
|
|
|
class HotelModel {
|
|
|
|
private PDO $db;
|
|
private AnschriftModel $mdlAnschr;
|
|
private $allowedFields = [
|
|
'hotelname', 'hotelnummer', 'provsatz', 'rg_hotelname', 'rg_interval',
|
|
'rg_vondatum', 'rg_bisdatum', 'rg_laufdatum', 'rg_ustid', 'rg_ustfrei',
|
|
'rg_provmode', 'status', 'notiz', 'intern', 'frueherechnung',
|
|
'passwort', 'loginname', 'nichtanzeigen', 'anschriftid', 'rg_anschriftid',
|
|
'bankid', 'rg_bankid', 'betreuer'
|
|
];
|
|
|
|
public function __construct() {
|
|
$this->db = Database::connect();
|
|
$this->mdlAnschr = new AnschriftModel();
|
|
}
|
|
|
|
public function getHotelById($id) {
|
|
$stmt = $this->db->prepare("SELECT * FROM vwl_hotel WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
return $stmt->fetch();
|
|
}
|
|
|
|
public function getAnschriftById($id) {
|
|
$stmt = $this->db->prepare("SELECT * FROM vwl_anschrift WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
return $stmt->fetch();
|
|
}
|
|
|
|
public function getBankverbindungById($id) {
|
|
$stmt = $this->db->prepare("SELECT * FROM vwl_bankverbindung WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
return $stmt->fetch();
|
|
}
|
|
|
|
public function updateHotel($data) {
|
|
|
|
$status = $data['status'] ?? 'inaktiv';
|
|
$data['status'] = $status;
|
|
|
|
$intern = $data['intern'] ?? 0;
|
|
$data['intern'] = $intern;
|
|
|
|
$nichtanzeigen = $data['nichtanzeigen'] ?? 0;
|
|
$data['nichtanzeigen'] = $nichtanzeigen;
|
|
|
|
|
|
$fieldsToUpdate = array_intersect_key($data, array_flip($this->allowedFields));
|
|
if (empty($fieldsToUpdate)) {
|
|
return false;
|
|
}
|
|
|
|
// Baue SQL-Statement
|
|
$setParts = [];
|
|
foreach ($fieldsToUpdate as $field => $value) {
|
|
$setParts[] = "`$field` = :$field";
|
|
}
|
|
$sql = "UPDATE vwl_hotel SET " . implode(', ', $setParts) . " WHERE id = :id";
|
|
|
|
$stmt = $this->db->prepare($sql);
|
|
|
|
// Bind-Werte
|
|
$fieldsToUpdate['id'] = $data['id'];
|
|
return $stmt->execute($fieldsToUpdate);
|
|
}
|
|
|
|
public function findByIdWithRelations($id) {
|
|
$stmt = $this->db->prepare("
|
|
SELECT * FROM vwl_hotel WHERE id = :id
|
|
");
|
|
$stmt->execute(['id' => $id]);
|
|
$hotel = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($hotel) {
|
|
$hotel['anschrift'] = $this->mdlAnschr->findById($hotel['anschriftid']);
|
|
$hotel['bankverbindung'] = BankverbindungModel::findById($hotel['bankid']);
|
|
$hotel['rg_anschrift'] = $this->mdlAnschr->findById($hotel['rg_anschriftid']);
|
|
$hotel['rg_bankverbindung'] = BankverbindungModel::findById($hotel['rg_bankid']);
|
|
}
|
|
|
|
return $hotel;
|
|
}
|
|
|
|
public function getRechnungen($id) {
|
|
$stmt = $this->db->prepare("
|
|
SELECT r.*, h.hotelnummer
|
|
FROM vwl_hotel_rechnung r
|
|
JOIN vwl_hotel h ON r.hotelid = h.id
|
|
WHERE h.id = :hotelid
|
|
ORDER BY r.rechnungsdatum DESC
|
|
");
|
|
|
|
$stmt->execute(['hotelid' => $id]);
|
|
$stmt->fetchAll();
|
|
}
|
|
|
|
public function updateWithRelations($id, $data) {
|
|
$this->db->beginTransaction();
|
|
try {
|
|
// 1. Anschrift aktualisieren
|
|
|
|
$subid = $this->mdlAnschr->update($data['anschriftid'], $data, false);
|
|
if (empty($subid) == false) {
|
|
$data['anschriftid'] = $subid;
|
|
}
|
|
// 1.1 Rechungsanschrift aktualisieren
|
|
$subidRg = $this->mdlAnschr->update($data['rg_anschriftid'], $data, true);
|
|
if (empty($subidRg) == false) {
|
|
$data['rg_anschriftid'] = $subidRg;
|
|
}
|
|
|
|
// 2. Bankverbindung aktualisieren
|
|
BankverbindungModel::update($data['bankid'], $data);
|
|
|
|
// 3. Hotel aktualisieren
|
|
if ($this->updateHotel($data) == false) {
|
|
$this->db->rollBack();
|
|
return false;
|
|
}
|
|
|
|
$this->db->commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
$this->db->rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
}
|