diff --git a/Layout/default/MaintenanceNotification/Form.php b/Layout/default/MaintenanceNotification/Form.php index 3d5548cf0..938e9eb9e 100644 --- a/Layout/default/MaintenanceNotification/Form.php +++ b/Layout/default/MaintenanceNotification/Form.php @@ -56,7 +56,6 @@ PLZ' mit Leerzeichen oder Komma getrennt -
@@ -76,6 +75,56 @@
+
+
+ +

Versand

+
+ +
+ + send_ts) : ""?>" /> + Emailversand wird zu diesem Zeitpunkt gestartet +
+
+ + +
+ +
+ +
+ + + + +
+
+
diff --git a/application/Address/AddressModel.php b/application/Address/AddressModel.php index c62dd5711..643330c4b 100644 --- a/application/Address/AddressModel.php +++ b/application/Address/AddressModel.php @@ -354,12 +354,17 @@ class AddressModel { } } - if (array_key_exists("zip", $filter)) { - $zip = FronkDB::singleton()->escape($filter["zip"]); - if ($zip) { - $where .= " AND zip like '%$zip%'"; + if(array_key_exists("zip", $filter)) { + $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%'"; + } } - } if (array_key_exists("city", $filter)) { $city = FronkDB::singleton()->escape($filter["city"]); @@ -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"; diff --git a/application/Contract/ContractModel.php b/application/Contract/ContractModel.php index a35908560..e52d99a9a 100644 --- a/application/Contract/ContractModel.php +++ b/application/Contract/ContractModel.php @@ -511,7 +511,18 @@ class ContractModel { $where .= " AND (Contract.`product_name` like '%$product_name%' OR Product.name like '%$product_name%')"; } } - + + 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']); diff --git a/application/MaintenanceNotification/MaintenanceNotification.php b/application/MaintenanceNotification/MaintenanceNotification.php index 1696d0248..03a0ca9eb 100644 --- a/application/MaintenanceNotification/MaintenanceNotification.php +++ b/application/MaintenanceNotification/MaintenanceNotification.php @@ -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) { diff --git a/application/MaintenanceNotification/MaintenanceNotificationController.php b/application/MaintenanceNotification/MaintenanceNotificationController.php index b1d57f6be..ca5d220b0 100644 --- a/application/MaintenanceNotification/MaintenanceNotificationController.php +++ b/application/MaintenanceNotification/MaintenanceNotificationController.php @@ -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"); + + } } \ No newline at end of file diff --git a/application/MaintenanceNotificationLog/MaintenanceNotificationLog.php b/application/MaintenanceNotificationLog/MaintenanceNotificationLog.php new file mode 100644 index 000000000..0435f3b3d --- /dev/null +++ b/application/MaintenanceNotificationLog/MaintenanceNotificationLog.php @@ -0,0 +1,202 @@ +$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; + } +} \ No newline at end of file diff --git a/application/Termination/TerminationModel.php b/application/Termination/TerminationModel.php index e7308e3a7..e27c06f6a 100644 --- a/application/Termination/TerminationModel.php +++ b/application/Termination/TerminationModel.php @@ -200,7 +200,18 @@ class TerminationModel { $where .= " AND Building.street LIKE '%$building_street%'"; } } - + + 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)) { diff --git a/db/migrations/20241112130541_create_maintanence_notification.php b/db/migrations/20241112130541_create_maintanence_notification.php index dd4fad788..66a25fe17 100644 --- a/db/migrations/20241112130541_create_maintanence_notification.php +++ b/db/migrations/20241112130541_create_maintanence_notification.php @@ -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]);