Finished sending of Preordernotifications

This commit is contained in:
Frank Schubert
2023-01-26 22:01:09 +01:00
parent b8d7bd3956
commit fe26e8f2bc
9 changed files with 288 additions and 81 deletions

View File

@@ -73,10 +73,10 @@
<label class="col-lg-2 col-form-label" for="type">Bestelltyp</label>
<div class="col-lg-10">
<select class="form-control select2" name="type[]" id="type" multiple="multiple">
<option value="interest" <?=(array_key_exists("type", $notification->filter) && in_array("interest", $notification->filter["type"])) ? "selected='selected'" : ""?>>Interessensbekundung</option>
<option value="provision" <?=(array_key_exists("type", $notification->filter) && in_array("provision", $notification->filter["type"])) ? "selected='selected'" : ""?>>Vorsorgeanschluss</option>
<option value="order" <?=(array_key_exists("type", $notification->filter) && in_array("order", $notification->filter["type"])) ? "selected='selected'" : ""?>>Vollanschluss</option>
<option value="reorder" <?=(array_key_exists("type", $notification->filter) && in_array("reorder", $notification->filter["type"])) ? "selected='selected'" : ""?>>Nachbestellung</option>
<option value="interest" <?=($notification && array_key_exists("type", $notification->filter) && in_array("interest", $notification->filter["type"])) ? "selected='selected'" : ""?>>Interessensbekundung</option>
<option value="provision" <?=($notification && array_key_exists("type", $notification->filter) && in_array("provision", $notification->filter["type"])) ? "selected='selected'" : ""?>>Vorsorgeanschluss</option>
<option value="order" <?=($notification && array_key_exists("type", $notification->filter) && in_array("order", $notification->filter["type"])) ? "selected='selected'" : ""?>>Vollanschluss</option>
<option value="reorder" <?=($notification && array_key_exists("type", $notification->filter) && in_array("reorder", $notification->filter["type"])) ? "selected='selected'" : ""?>>Nachbestellung</option>
</select>
</div>
</div>
@@ -85,11 +85,11 @@
<label class="col-lg-2 col-form-label" for="connection_type">Anschlusstyp</label>
<div class="col-lg-10">
<select class="form-control select2" name="connection_type[]" id="connection_type" multiple="multiple">
<option value="single-dwelling" <?=(array_key_exists("connection_type", $notification->filter) && in_array("single-dwelling", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Einfamilienhaus</option>
<option value="multi-dwelling" <?=(array_key_exists("connection_type", $notification->filter) && in_array("multi-dwelling", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Mehrfamilienhaus</option>
<option value="apartment-building" <?=(array_key_exists("connection_type", $notification->filter) && in_array("apartment-building", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Mehrparteienhaus</option>
<option value="apartment" <?=(array_key_exists("connection_type", $notification->filter) && in_array("apartment", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Wohneinheit in Mehrparteienhaus</option>
<option value="business" <?=(array_key_exists("connection_type", $notification->filter) && in_array("business", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Gewerbebetrieb</option>
<option value="single-dwelling" <?=($notification && array_key_exists("connection_type", $notification->filter) && in_array("single-dwelling", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Einfamilienhaus</option>
<option value="multi-dwelling" <?=($notification && array_key_exists("connection_type", $notification->filter) && in_array("multi-dwelling", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Mehrfamilienhaus</option>
<option value="apartment-building" <?=($notification && array_key_exists("connection_type", $notification->filter) && in_array("apartment-building", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Mehrparteienhaus</option>
<option value="apartment" <?=($notification && array_key_exists("connection_type", $notification->filter) && in_array("apartment", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Wohneinheit in Mehrparteienhaus</option>
<option value="business" <?=($notification && array_key_exists("connection_type", $notification->filter) && in_array("business", $notification->filter['connection_type'])) ? "selected='selected'" : ""?>>Gewerbebetrieb</option>
</select>
</div>
</div>
@@ -212,7 +212,8 @@
<div class="form-group row">
<label class="col-lg-2"></label>
<div class="col-lg-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<button type="submit" name="return" value="form" class="btn btn-primary mr-1">Speichern</button>
<button type="submit" name="return" value="index" class="btn btn-primary">Speichern und zur Übersicht</button>
</div>
</div>

View File

@@ -89,7 +89,16 @@
</tr>
<?php foreach($notifications as $notification): ?>
<tr>
<td class="text-center"><?=($notification->sent_date) ? date('d.m.Y', $notification->sent_date) : "<i class='fas fa-remove text-danger' title='Noch nicht versendet'></i>"?></td>
<td class="text-center">
<?php if($notification->send_start && $notification->send_finish): ?>
<i class='fas fa-check text-success' title='Versand erfolgreich abgeschlossen'></i>
<?php elseif($notification->send_start && !$notification->send_finish): ?>
<i class='fas fa-loader text-info' title='Versand gestartet'></i>
<?php elseif(!$notification->send_start && !$notification->send_finish): ?>
<i class='fas fa-remove text-danger' title='Noch nicht versendet'></i>
<?php endif; ?>
<?=($notification->sent_date) ? date('d.m.Y', $notification->sent_date) : ""?>
</td>
<td><?=$notification->campaign->name?></td>
<td><?=$notification->subject?></td>
<td title="Anzahl berechnet am <?=date('d.m.Y H:i', $notification->edit)?>"><?=($notification->recipient_count) ? $notification->recipient_count : ""?></td>

View File

@@ -80,7 +80,7 @@ class Emailnotification {
return false;
}
if(!$this->headers['Subject']) {
if(!array_key_exists("Subject", $this->headers) || !$this->headers['Subject']) {
$this->setHeader("Subject", $this->subject);
}

View File

@@ -9,10 +9,16 @@ class Preordernotification extends mfBaseModel {
private $editor;
public function getPreorders() {
if(!$this->preordercampaign_id) return false;
$preorder_filter = json_decode($this->preorder_filter, true);
$preorder_filter['preordercampaign_id'] = $this->preordercampaign_id;
$preorders = PreorderModel::search($preorder_filter);
return $preorders;
}
public function sendToPreorder(Preorder $preorder, $email_to) {
public function sendToPreorder(Preorder $preorder, $email_to = false) {
$subject = $this->subject;
$body = $this->body_html; // TODO: Variable replacement
$from = $this->sender_email;
@@ -21,10 +27,29 @@ class Preordernotification extends mfBaseModel {
if($this->sender_replyto) {
$reply_to = $this->sender_replyto;
}
$to = $preorder->email;
if($email_to) {
$to = $email_to;
} else {
// check if email was sent to this preorder already
$log = PreordernotificationLogModel::getFirst([
'preordernotification_id' => $this->id,
'preorder_id' => $preorder->id
]);
if(!$log) {
// check if this was sent to the same emailaddress already
$log = PreordernotificationLogModel::getFirst([
'preordernotification_id' => $this->id,
'email' => $preorder->email
]);
}
if($log) {
return true;
}
}
if(!$to) return true;
@@ -61,6 +86,17 @@ class Preordernotification extends mfBaseModel {
}
$email->send();
$this->log->info(__CLASS__."::sendToPreorder(): Sending Preordernotification for Preorder id ".$preorder->id);
if($preorder && !$email_to) {
// save notification log
$log = PreordernotificationLogModel::create([
'preordernotification_id' => $this->id,
'preorder_id' => $preorder->id,
'email' => $preorder->email,
'sent' => date('U')
]);
$log->save();
}
}
return true;

View File

@@ -379,7 +379,11 @@ class PreordernotificationController extends mfBaseController {
}
$this->layout()->setFlash("Erfolgreich gepeichert.", "success");
$this->redirect("Preordernotification", "edit", ['id' => $new_id]);
if($r->return == "index") {
$this->redirect("Preordernotification");
} else {
$this->redirect("Preordernotification", "edit", ['id' => $new_id]);
}
}
}

View File

@@ -1,14 +1,19 @@
<?php
class PreordernotificationModel {
public $sender_email;
public $sender_name;
public $sender_replyto;
public $subject;
public $body_text;
public $body_html;
public $tosend_date;
public $sent_date;
public $send_start;
public $send_finish;
public $recipient_count;
public $preordercampaign_id;
public $preorder_filter;
public $send_lock;
public $note = null;
public $create_by = null;
@@ -145,78 +150,26 @@ class PreordernotificationModel {
}
}
if(array_key_exists("productgroup_id", $filter)) {
$productgroup_id = $filter['productgroup_id'];
if(is_numeric($productgroup_id)) {
$where .= " AND productgroup_id=$productgroup_id";
} elseif(is_array($productgroup_id) && count($productgroup_id)) {
$where .= " AND Preordernotification.productgroup_id IN (". implode(",", $productgroup_id).")";
if(array_key_exists("preordercampaign_id", $filter)) {
$preordercampaign_id = $filter['preordercampaign_id'];
if(is_numeric($preordercampaign_id)) {
$where .= " AND preordercampaign_id=$preordercampaign_id";
} elseif(is_array($preordercampaign_id) && count($preordercampaign_id)) {
$where .= " AND Preordernotification.preordercampaign_id IN (". implode(",", $preordercampaign_id).")";
}
}
if(array_key_exists("producttech_id", $filter)) {
$producttech_id = $filter['producttech_id'];
if(is_numeric($producttech_id)) {
$where .= " AND Preordernotification.producttech_id=$producttech_id";
} elseif(is_array($producttech_id) && count($producttech_id)) {
$where .= " AND Preordernotification.producttech_id IN (". implode(",", $producttech_id).")";
if(array_key_exists("send_finish", $filter)) {
$send_finish = $db->escape($filter['send_finish']);
if($send_finish === null) {
$where .= " AND Preordernotification.send_finish = NULL";
}
}
if(array_key_exists("sla_id", $filter)) {
$sla_id = $filter['sla_id'];
if(is_numeric($sla_id)) {
$where .= " AND sla_id=$sla_id";
} elseif(is_array($sla_id) && count($sla_id)) {
$where .= " AND Preordernotification.sla_id IN (". implode(",", $sla_id).")";
}
}
if(array_key_exists("name", $filter)) {
$name = $db->escape($filter['name']);
if($name) {
$where .= " AND Preordernotification.`name` like '$name'";
}
}
if(array_key_exists("name%", $filter)) {
$name = $db->escape($filter['name%']);
if($name) {
$where .= " AND Preordernotification.`name` like '%$name%'";
}
}
if(array_key_exists("external", $filter)) {
$external = $filter['external'];
if(is_numeric($external)) {
if($external) {
$where .= " AND Preordernotification.external=1";
} else {
$where .= " AND Preordernotification.external=0";
}
}
}
if(array_key_exists("customer_type", $filter)) {
$customer_type = $db->escape($filter['customer_type']);
if($customer_type) {
$where .= " AND Preordernotificationtech.`customer_type` = '$customer_type'";
}
}
if(array_key_exists("attributename", $filter)) {
$attributename = $db->escape($filter['attributename']);
if($attributename) {
$where .= " AND PreordernotificationtechAttribute.name = '$attributename'";
}
}
if(array_key_exists("attributevalue", $filter)) {
$attributevalue = $db->escape($filter['attributevalue']);
if($attributevalue) {
$where .= " AND PreordernotificationAttribute.value = '$attributevalue'";
if(array_key_exists("send_lock", $filter)) {
$send_lock = $db->escape($filter['send_lock']);
if($send_lock === null) {
$where .= " AND Preordernotification.send_lock = NULL";
}
}

View File

@@ -0,0 +1,5 @@
<?php
class PreordernotificationLog extends mfBaseModel {
}

View File

@@ -0,0 +1,156 @@
<?php
class PreordernotificationLogModel {
public $preordernotification_id;
public $preorder_id;
public $email;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new PreordernotificationLog();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$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("PreordernotificationLog", "*", "1 = 1 ORDER BY `create`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreordernotificationLog($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("PreordernotificationLog", "*", "$where ORDER BY `create` LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreordernotificationLog($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 `PreordernotificationLog`
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) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT PreordernotificationLog.* FROM `PreordernotificationLog`
WHERE $where
ORDER BY `create`
";
mfLoghandler::singleton()->debug($sql);
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($count)) {
$sql .= " LIMIT ".$limit['count'];
}
}
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreordernotificationLog($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
$db = FronkDB::singleton();
//var_dump($filter);exit;
if(array_key_exists("id", $filter)) {
$id = $filter['id'];
if(is_numeric($id)) {
$where .= " AND PreordernotificationLog.`id` = $id";
}
}
if(array_key_exists("preordernotification_id", $filter)) {
$preordernotification_id = $filter['preordernotification_id'];
if(is_numeric($preordernotification_id)) {
$where .= " AND preordernotification_id=$preordernotification_id";
} elseif(is_array($preordernotification_id) && count($preordernotification_id)) {
$where .= " AND PreordernotificationLog.preordernotification_id IN (". implode(",", $preordernotification_id).")";
}
}
if(array_key_exists("preorder_id", $filter)) {
$preorder_id = $filter['preorder_id'];
if(is_numeric($preorder_id)) {
$where .= " AND preorder_id=$preorder_id";
} elseif(is_array($preorder_id) && count($preorder_id)) {
$where .= " AND PreordernotificationLog.preorder_id IN (". implode(",", $preorder_id).")";
}
}
if(array_key_exists("email", $filter)) {
$email = $db->escape($filter['email']);
if($email) {
$where .= " AND PreordernotificationLog.email = '$email'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -0,0 +1,43 @@
#!/usr/bin/php
<?php
//require 'vendor/autoload.php';
require("../config/config.php");
define('FRONKDB_SQLDEBUG',false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
define("INTERNAL_USER_ID", 1);
$me = new User(1);
foreach(PreordernotificationModel::search(["send_finish" => null, "send_lock" => null]) as $notification) {
// check tosend date
if(!$notification->tosend_date || $notification->tosend_date > date('U')) continue;
// get notification again from DB in case send_lock was set
$notification = new Preordernotification($notification->id);
if($notification->send_lock) continue;
// first of all set send lock and start date
$mypid = getmypid();
if(!$notification->send_start) {
$notification->send_start = date("U");
}
$notification->send_lock = ($mypid) ? $mypid : 1;
$notification->save();
foreach($notification->getPreorders() as $preorder) {
$notification->sendToPreorder($preorder);
}
// get notification from DB again and update send lock and finish date
$notification = new Preordernotification($notification->id);
$notification->send_finish = date('U');
$notification->send_lock = null;
$notification->save();
}