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

@@ -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) {