Finished MaintenanceNotification

This commit is contained in:
Frank Schubert
2024-11-13 18:15:24 +01:00
parent 97dc4446dc
commit 47f71f2310
8 changed files with 465 additions and 14 deletions

View File

@@ -56,7 +56,6 @@
<small class="text-primary">PLZ' mit Leerzeichen oder Komma getrennt</small>
</div>
<hr />
<div class="form-group row mt-3 col-12">
@@ -76,6 +75,56 @@
</div>
</div>
<div class="card">
<div class="card-body">
<h4>Versand</h4>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="tosend_day">Versandzeitpunkt</label>
<div class="col-lg-2">
<label>Datum</label>
<input type="text" class="form-control datepicker" name="sendts_day" id="sendts_day" value="<?=($notification->send_ts) ? date("d.m.Y", $notification->send_ts) : ""?>" />
<small>Emailversand wird zu diesem Zeitpunkt gestartet</small>
</div>
<div class="col-lg-2">
<label>Uhrzeit</label>
<select name="sendts_hour" class="form-control">
<?php for($h = 0; $h < 24; $h++): ?>
<option value="<?=$h?>" <?=($notification->send_ts && date('H', $notification->send_ts) == $h) ? "selected='selected'" : ""?>><?=str_pad($h, 2, "0", STR_PAD_LEFT)?>:00</option>
<?php endfor; ?>
</select>
</div>
</div>
<hr />
<!--div class="form-group row">
<label class="col-lg-2 col-form-label" for="testmail_to">Test Email versenden</label>
<div class="col-lg-10">
<div class="row">
<div class="col-6">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-envelope"></i></span>
</div>
<input type="text" class="form-control" name="testmail_to" id="testmail_to" value="" placeholder="Testempfänger" />
</div>
</div>
<div class="col-6">
<button type="submit" name="send_testmail" value="1" class="btn btn-outline-success"><i class="far fa-paper-plane"></i> Testmail jetzt abschicken</button>
</div>
</div>
</div>
</div-->
</div>
</div>
<div class="form-group row">
<label class="col-lg-2"></label>
<div class="col-lg-10">

View File

@@ -355,9 +355,14 @@ class AddressModel {
}
if(array_key_exists("zip", $filter)) {
$zip = FronkDB::singleton()->escape($filter["zip"]);
if ($zip) {
$where .= " AND zip like '%$zip%'";
$zip = $filter['zip'];
if(is_array($zip)) {
if(count($zip)) {
$where .= " AND Address.zip IN ('".implode("','", $zip)."')";
}
} elseif($zip) {
$zip = FronkDB::singleton()->escape($filter['zip']);
$where .= " AND Contract.zip LIKE '%$zip%'";
}
}
@@ -427,7 +432,7 @@ class AddressModel {
//var_dump($filter);exit;
if (array_key_exists("parent_id", $filter)) {
$parentid = $filter['parent_id'];
if ($parentid === null || $parent_id == "null") {
if ($parentid === null || $parentid == "null") {
$where .= " AND parent_id IS NULL";
} elseif (is_numeric($parentid)) {
$where .= " AND parent_id=$parentid";

View File

@@ -512,6 +512,17 @@ class ContractModel {
}
}
if(array_key_exists("termination_id", $filter)) {
$termination_id = $filter['termination_id'];
if(is_array($termination_id)) {
if(count($termination_id)) {
$where .= " AND Contract.termination_id IN (".implode(",", $termination_id).")";
}
} elseif($termination_id) {
$termination_id = FronkDB::singleton()->escape($filter['termination_id']);
$where .= " AND Contract.termination_id = $termination_id";
}
}
if(array_key_exists("matchcode", $filter)) {
$matchcode = $db->escape($filter['matchcode']);

View File

@@ -3,8 +3,150 @@
class MaintenanceNotification extends mfBaseModel {
private $subject;
private $plzs;
private $editor;
private $creator;
public function sendToRecipients() {
$emails = $this->getRecipients();
if(!$emails) return true;
if($this->sent > 0) return true;
if(!$this->from || !$this->to) return false;
if(date("U") < $this->sendts) return true;
if(!$this->getProperty("subject")->subject) return false;
if(!$this->text) return false;
$body = $this->getReplacedBody();
$subject = $this->getProperty("subject")->subject;
$from = "office@xinon.at";
$from_name = "XINON";
foreach($emails as $email) {
$to = $email;
// check for NotificationLog
$mnlog = MaintenanceNotificationLog::getFirst(["maintenancenotification_id" => $this->id, "email" => $to]);
if($mnlog) {
// was sent already
continue;
}
$email = new Emailnotification();
$email->setSubject($subject);
$email->setHtmlBody($body);
$email->setFrom($from, $from_name);
$email->setTo($to);
$email->setHeader("X-".ucfirst(MFAPPNAME)."-mnid", $this->id);
$email->send();
$this->log->info(__METHOD__.": Sending MaintenanceNotification to $to");
$mnlog = MaintenanceNotificationLog::create([
"maintenancenotification_id" => $this->id,
"email" => $to,
"sent" => date("U"),
]);
$mnlog->save();
}
return true;
}
private function getReplacedBody() {
$body = $this->text;
$from = new DateTime("@".$this->from);
$from->setTimezone(new DateTimeZone("Europe/Vienna"));
$to = new DateTime("@".$this->to);
$to->setTimezone(new DateTimeZone("Europe/Vienna"));
$body = str_replace("{{FROM}}", $from->format("d.m.Y H:i"), $body);
$body = str_replace("{{TO}}", $to->format("d.m.Y H:i"), $body);
return $body;
}
public function getRecipients() {
$plz_list = $this->getProperty("plzs");
$emails = [];
$owner_ids = [];
$term_ids = [];
$term_count = 0;
foreach(TerminationModel::search(["building_zip" => $plz_list]) as $termination) {
$term_count++;
$term_ids[] = $termination->id;
}
$term_contract_count = 0;
foreach(ContractModel::search(["termination_id" => $term_ids]) as $contract) {
$term_contract_count++;
if(!in_array($contract->owner_id, $owner_ids)) {
$owner_ids[] = $contract->owner_id;
}
if(!$contract->owner->email) {
$emails[] = trim($contract->owner->email);
}
if($contract->billingaddress_id && $contract->billingaddress->email) {
$emails[] = trim($contract->billingaddress->email);
}
if(array_key_exists("techcontact", $contract->owner->links) && is_array($contract->owner->links["techcontact"])) {
foreach($contract->owner->links["techcontact"] as $techlink) {
if($techlink->address->email) {
$emails[] = trim($techlink->address->email);
}
}
}
}
$address_count = 0;
// find addresses with contracts we haven't found from Terminations yet
foreach(AddressModel::search(["zip" => $plz_list]) as $address) {
if(!in_array($address->id, $owner_ids)) {
$owner_ids[] = $address->id;
} else {
continue;
}
if(!$address->customer_number) continue;
if($address->customer_number > 900000) continue;
if(!$address->active_contracts) continue;
$address_count++;
if($address->email) {
$emails[] = $address->email;
}
if(array_key_exists("techcontact", $address->links) && is_array($address->links["techcontact"])) {
foreach($address->links["techcontact"] as $techlink) {
if($techlink->address->email) {
$emails[] = trim($techlink->address->email);
}
}
}
}
echo "$term_count Terminations\n";
echo "$term_contract_count Contracts from Terminations\n";
echo "$address_count Adressen sonst\n";
$emails = array_unique($emails);
return $emails;
}
public function getProperty($name) {
if($this->$name == null) {

View File

@@ -86,9 +86,10 @@ class MaintenanceNotificationController extends mfBaseController {
protected function saveAction() {
$r = $this->request;
var_dump($r->get());
$id = $r->id;
//var_dump($r);exit;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$notification = new MaintenanceNotification($id);
@@ -111,10 +112,10 @@ class MaintenanceNotificationController extends mfBaseController {
$data["plz"] = json_encode(explode(",", $plz_string));
}
// from / to -> date/time -> DateTime
try {
$from = new DateTime($r->from_date." ".$r->from_time);
$from->setTimezone(new DateTimeZone("Europe/Vienna"));
} catch(Exception $e) {
$this->layout()->setFlash("Ungültiges Wartungsfenster Von-Zeitpunkt", "error");
if($mode = "edit") {
@@ -126,6 +127,7 @@ class MaintenanceNotificationController extends mfBaseController {
try {
$to = new DateTime($r->to_date." ".$r->to_time);
$to->setTimezone(new DateTimeZone("Europe/Vienna"));
} catch(Exception $e) {
$this->layout()->setFlash("Ungültiges Wartungsfenster Bis-Zeitpunkt", "error");
if($mode = "edit") {
@@ -135,10 +137,19 @@ class MaintenanceNotificationController extends mfBaseController {
}
$data["to"] = $to->getTimestamp();
// send_ts - feld einbauen -> DateTime
try {
$send_ts = new DateTime($r->sendts_day." ".$r->sendts_hour.":00");
//var_dump($send_ts);exit;
} catch(Exception $e) {
$this->layout()->setFlash("Ungültige Versandzeitpunkt", "error");
if($mode = "edit") {
return $this->editAction();
}
return $this->addAction();
}
$data["send_ts"] = $send_ts->getTimestamp();
//var_dump($data);exit;
if($mode == "add") {
$notification = MaintenanceNotification::create($data);
@@ -159,4 +170,25 @@ class MaintenanceNotificationController extends mfBaseController {
}
protected function delete() {
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Wartungsmeldung nicht gefunden", "error");
$this->redirect("MaintenanceNotification");
}
$notification = new MaintenanceNotification($id);
if(!$notification || !$notification->id) {
$this->layout()->setFlash("Wartungsmeldung nicht gefunden", "error");
$this->redirect("MaintenanceNotification");
}
$notification->delete();
$this->layout()->setFlash("Wartungsmeldung erfolgreich gelöscht", $notification);
$this->redirect("MaintenanceNotification");
}
}

View File

@@ -0,0 +1,202 @@
<?php
class MaintenanceNotificationLog extends mfBaseModel {
public function getProperty($name) {
if($this->$name == null) {
if($name == "adb_hausnummer") {
if(!$this->adb_hausnummer_id) return null;
$hausnummer = new ADBHausnummer($this->adb_hausnummer_id);
if($hausnummer->id) {
$this->adb_hausnummer = $hausnummer;
}
return $this->adb_hausnummer;
}
if($name == "adb_strasse") {
if(!$this->adb_strasse_id) return null;
$strasse = new ADBStrasse($this->adb_strasse_id);
if($strasse->id) {
$this->adb_strasse = $strasse;
}
return $this->adb_strasse;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new MaintenanceNotificationLog();
$table_fields = [
"maintenancenotification_id", "email", "sent",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("MaintenanceNotificationLog", "*", "1 = 1 ORDER BY email");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new MaintenanceNotificationLog($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM MaintenanceNotificationLog
WHERE $where
ORDER BY email LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new MaintenanceNotificationLog($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM MaintenanceNotificationLog
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "adb_hausnummer_id ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM MaintenanceNotificationLog
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new MaintenanceNotificationLog($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("maintenancenotification_id", $filter)) {
$maintenancenotification_id = $filter['maintenancenotification_id'];
if(is_numeric($maintenancenotification_id)) {
$where .= " AND MaintenanceNotificationLog.maintenancenotification_id=$maintenancenotification_id";
}
}
if(array_key_exists("email", $filter)) {
$email = FronkDB::singleton()->escape($filter["email"]);
if($email) {
$where .= " AND email='$email'";
}
}
if(array_key_exists("sent", $filter)) {
$sent = $filter['sent'];
if($sent === true) {
$where .= " AND `sent` > 0";
} elseif($sent === false || $sent === null) {
$where .= " AND (`sent` IS NULL OR `sent` = 0)";
} elseif(is_numeric($sent)) {
$where .= " AND `sent`=$sent";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -201,6 +201,17 @@ class TerminationModel {
}
}
if(array_key_exists("building_zip", $filter)) {
$building_zip = $filter['building_zip'];
if(is_array($building_zip)) {
if(count($building_zip)) {
$where .= " AND Building.zip IN ('".implode("','", $building_zip)."')";
}
} elseif($building_zip) {
$building_zip = FronkDB::singleton()->escape($filter['building_zip']);
$where .= " AND Building.zip = '$building_zip'";
}
}
if(array_key_exists("status", $filter)) {

View File

@@ -26,7 +26,6 @@ final class CreateMaintanenceNotification extends AbstractMigration
$log = $this->table("MaintenanceNotificationLog");
$log->addColumn("maintenancenotification_id", "integer", ["null" => false]);
$log->addColumn("address_id", "integer", ["null" => true, "default" => null]);
$log->addColumn("email", "string", ["null" => false]);
$log->addColumn("sent", "integer", ["null" => false, "default" => 0]);