Files
thetool/application/MaintenanceNotification/MaintenanceNotification.php

410 lines
12 KiB
PHP

<?php
class MaintenanceNotification extends mfBaseModel {
private $subject;
private $plzs;
private $editor;
private $creator;
public function sendToRecipients($test_to_email = false) {
$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;
if($test_to_email) {
$to = $test_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->setBody($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");
if($test_to_email) {
return true;
}
$mnlog = MaintenanceNotificationLog::create([
"maintenancenotification_id" => $this->id,
"email" => $to,
"sent" => date("U"),
]);
$mnlog->save();
}
$this->sent = date("U");
$this->save();
// send to xinon
$mnlog = MaintenanceNotificationLog::getFirst(["maintenancenotification_id" => $this->id, "email" => "internal-email"]);
if($mnlog) {
// was sent already
return true;
}
$to = "wartungsverteiler.team@xinon.at";
$body .= "\n\nPLZ Liste:\n\n";
$body .= implode("\n", $this->getProperty("plzs"));
$email = new Emailnotification();
$email->setSubject($subject);
$email->setBody($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" => "internal-email",
"sent" => date("U"),
]);
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($list_recipients = false) {
$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(!$address->customer_number) continue;
if($address->customer_number > 900000) continue;
if(!$address->active_contracts) continue;
if(!in_array($address->id, $owner_ids)) {
$owner_ids[] = $address->id;
} else {
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);
}
}
}
}
if($list_recipients) {
$csv = "Kundennummer;Firma;Vorname;Nachname;Straße;PLZ;Ort;Email\n";
foreach ($owner_ids as $id) {
$address = new Address($id);
if (!$address->id) {
echo "Adresse mit id $id nicht gefunden\n";
}
$csv .= $address->customer_number . ";";
$csv .= '"' . str_replace("\n"," ", $address->company) . '";';
$csv .= '"' . $address->firstname . '";';
$csv .= '"' . $address->lastname . '";';
$csv .= '"' . $address->street . '";';
$csv .= '"' . $address->zip . '";';
$csv .= '"' . $address->city . '";';
$csv .= '"' . $address->email . '";' . "\n";
}
echo $csv;
exit;
}
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) {
if($name == "subject") {
$subject = new MaintenanceNotificationTemplate($this->subject_id);
if($subject) {
$this->subject = $subject;
}
return $this->subject;
}
if($name == "plzs") {
if(!$this->plz) return [];
$plzs = json_decode($this->plz);
if(!is_array($plzs)) return [];
$this->plzs = $plzs;
return $this->plzs;
}
if($name == "creator") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($user) {
$this->creator = $user;
return $this->creator;
}
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
}
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$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 MaintenanceNotification();
$table_fields = [
"subject_id", "text", "plz", "from", "to", "send_ts", "sent", "sent_by",
"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("MaintenanceNotification", "*", "1 = 1 ORDER BY `create`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new MaintenanceNotification($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM MaintenanceNotification
WHERE $where
ORDER BY adb_hausnummer_id LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new MaintenanceNotification($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 MaintenanceNotification
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 = "`create` ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM MaintenanceNotification
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 MaintenanceNotification($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("termination_id", $filter)) {
$termination_id = $filter['termination_id'];
if(is_numeric($termination_id)) {
$where .= " AND MaintenanceNotification.termination_id=$termination_id";
}
}
if(array_key_exists("object_type", $filter)) {
$object_type = FronkDB::singleton()->escape($filter["object_type"]);
if($object_type) {
$where .= " AND object_type='$object_type'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}