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
@@ -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) {
@@ -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");
}
}