more filters and testmail in MailtemplateDispatch

This commit is contained in:
Frank Schubert
2025-01-14 21:53:49 +01:00
parent da91c5c0dc
commit 9e392fec68
3 changed files with 255 additions and 5 deletions

View File

@@ -79,6 +79,10 @@
<input class="form-check-input" type="checkbox" value="1" name="filter_order_open" id="filter_order_open" <?=(array_key_exists("order_open", $dispatch->recipient_filter_array) && $dispatch->recipient_filter_array["order_open"]) ? 'checked="checked"' : ''?> />
<label class="form-check-label" for="filter_order_open">Nur offene Bestellungen</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="1" name="filter_order_int_products" id="filter_order_int_products" <?=(array_key_exists("order_int_products", $dispatch->recipient_filter_array) && $dispatch->recipient_filter_array["order_int_products"]) ? 'checked="checked"' : ''?> />
<label class="form-check-label" for="filter_order_int_products">Nur Xinonprodukte</label>
</div>
<h4 class="mt-3">Aktive Contracts</h4>
@@ -86,13 +90,30 @@
<h4 class="mt-3">Allgemein</h4>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="1" name="filter_fibu_account" id="filter_fibu_account" <?=(array_key_exists("fibu_account", $dispatch->recipient_filter_array) && $dispatch->recipient_filter_array["fibu_account"]) ? 'checked="checked"' : ''?> />
<label class="form-check-label" for="filter_fibu_account">Nur mit Fibu Kontonummer</label>
<label class="form-check-label" for="filter_fibu_account">Nur Kunden mit Fibu Kontonummer</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="1" name="filter_billingaddress" id="filter_billingaddress" <?=(array_key_exists("billingaddress", $dispatch->recipient_filter_array) && $dispatch->recipient_filter_array["billingaddress"]) ? 'checked="checked"' : ''?> />
<label class="form-check-label" for="filter_billingaddress">Auch an Rechnungsadresse</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="1" name="filter_techcontact" id="filter_techcontact" <?=(array_key_exists("techcontact", $dispatch->recipient_filter_array) && $dispatch->recipient_filter_array["techcontact"]) ? 'checked="checked"' : ''?> />
<label class="form-check-label" for="filter_techcontact">Auch an Technischen Kontakt</label>
</div>
<div class="form-group row mt-2 col-12">
<label class="form-label" for="filter_zip">PLZ-Beschränkung</label>
<label class="form-label" for="filter_zip">PLZ-Beschränkung (Kundenadresse)</label>
<input type="text" class="form-control" name="filter_zip" id="filter_zip" value="<?=(array_key_exists("zip", $dispatch->recipient_filter_array)) ? implode(", ", $dispatch->recipient_filter_array["zip"]) : ""?>" />
<small class="text-primary">PLZ' mit Leerzeichen oder Komma getrennt</small>
</div>
<input type="hidden" name="filter_zip_source" value="owner" />
<!--div class="form-group row mt-2 col-12">
<label class="form-label" for="filter_zip">PLZ-Beschränkung anhand</label>
<select class="form-control" name="filter_zip_source" id="filter_zip_source">
<option value="owner">Kundenadresse</option>
<option value="termination">Anschlussadresse</option>
</select>
</div-->
</div>
</div>

View File

@@ -10,29 +10,234 @@ class MailtemplateDispatch extends mfBaseModel {
public function sendToRecipients($test_to_email = false) {
$emails = $this->getRecipients();
if(!$emails) return true;
if(!$test_to_email) {
$emails = $this->getRecipients();
if(!$emails) return true;
if(!$this->tosend_date || $this->tosend_date > date('U')) return false;
if($this->send_lock) return false;
}
if($test_to_email) {
$emails = [$test_to_email];
}
if(!$this->subject) return false;
if(!$this->sender_email) return false;
if(!$this->sender_name) return false;
$template = $this->getProperty("mailtemplate");
if(!$template) return false;
$body = "<html><head></head><body>";
$body .= $template->renderBody();
$body .= "</body></html>\n"; // explicitly add new line, or some mail clients wont show attachment
$subject = $this->subject;
$from_email = $this->sender_email;
$from_name = $this->sender_name;
$attachments = [];
foreach($this->getProperty("mailtemplate")->files as $file) {
$att = [];
$att["path"] = MFUPLOAD_FILE_SAVE_PATH."/".$file->file->subfolder."/".$file->file->store_filename;
$att["filename"] = $file->filename;
$att["mimetype"] = $file->file->mimetype;
$attachments[] = $att;
}
foreach($emails as $email) {
$to = $email;
// check for NotificationLog
/*$mnlog = MaintenanceNotificationLog::getFirst(["maintenancenotification_id" => $this->id, "email" => $to]);
if($mnlog) {
// was sent already
continue;
}*/
/*fwrite(STDERR, "Subject: $subject\n\n");
fwrite(STDERR, "$body\n\n");
exit;*/
$email = new Emailnotification();
$email->setSubject($subject);
$email->setBody($body);
$email->setFrom($from_email, $from_name);
$email->setTo($to);
$email->setHeader("X-".ucfirst(MFAPPNAME)."-mnid", $this->id);
if(count($attachments)) {
foreach($attachments as $file) {
$email->addAttachment($file["path"], null, $file["filename"], $file['mimetype']);
}
}
$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();
}
public function getRecipients($list_recipients = false) {
public function getRecipients($list_recipients = false, $return_recipients = false) {
$datasources = $this->getProperty("filter_datasource_array");
$filter = $this->getProperty("recipient_filter_array");
$zips = [];
$zip_source = "owner";
$to_billcontact = false;
$to_techcontact = false;
$fibu_account = false;
if(array_key_exists("zip", $filter) && count($filter["zip"])) {
$zips = $filter["zip"];
}
// only owner is implemented yet
if(array_key_exists("zip_source", $filter) && $filter["zip_source"] == "termination") {
$zip_source = "term";
}
if(array_key_exists("billingaddress", $filter) && $filter["billingaddress"]) {
$to_billcontact = true;
}
if(array_key_exists("techcontact", $filter) && $filter["techcontact"]) {
$to_techcontact = true;
}
if(array_key_exists("fibu_account", $filter) && $filter["fibu_account"]) {
$fibu_account = true;
}
if(!is_array($datasources) || !count($datasources)) {
$this->log->warn(__METHOD__.": Mailtemplate ".$this->id.": no datasource");
return false;
}
$owner_ids = [];
$emails = [];
if(in_array("contract", $datasources)) {
foreach(ContractModel::searchActive([]) as $contract) {
if($fibu_account && !$contract->owner->fibu_account_number) continue;
if(($zips && in_array($contract->owner->zip, $zips)) || !$zips ) {
if(!in_array($contract->owner_id, $owner_ids)) {
$owner_ids[] = $contract->owner_id;
}
if($contract->owner->email) {
$emails[] = trim($contract->owner->email);
}
if($to_billcontact && $contract->billingaddress_id && $contract->billingaddress->email) {
if(($zips && in_array($contract->billingaddress->zip, $zips)) || !$zips ) {
$emails[] = trim($contract->billingaddress->email);
}
}
if($to_techcontact && 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);
}
}
}
}
}
}
if(in_array("order", $datasources)) {
$order_search = [];
$order_search["finish_date"] = true;
if(array_key_exists("order_open", $filter) && $filter["order_open"]) {
$order_search["finish_date"] = false;
}
foreach(OrderModel::search($order_search) as $order) {
if($fibu_account && !$order->owner->fibu_account_number) continue;
if(array_key_exists("order_int_products", $filter) && $filter["order_int_products"]) {
$include_order = true;
foreach($order->products as $op) {
if($op->product->external) {
//fwrite(STDERR, "found external product (".$op->product->name.")\n");
$include_order = false;
break;
}
}
if(!$include_order) continue;
}
if(($zips && in_array($order->owner->zip, $zips)) || !$zips ) {
if(!in_array($order->owner_id, $owner_ids)) {
$owner_ids[] = $order->owner_id;
}
if($order->owner->email) {
$emails[] = trim($order->owner->email);
}
if($to_billcontact && $order->billingaddress_id && $order->billingaddress->email) {
$emails[] = trim($order->billingaddress->email);
}
if($to_techcontact && $order->techcontact_id && $order->techcontact->email) {
$emails[] = trim($order->techcontact->email);
}
}
}
}
if($list_recipients || $return_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(["\r\n", "\n", "\r"]," ", $address->company) . '";';
$csv .= '"' . $address->firstname . '";';
$csv .= '"' . $address->lastname . '";';
$csv .= '"' . $address->street . '";';
$csv .= '"' . $address->zip . '";';
$csv .= '"' . $address->city . '";';
$csv .= '"' . $address->email . '";' . "\n";
}
if($return_recipients) {
return $csv;
} else {
echo $csv;
exit;
}
}
$emails = array_unique($emails);
return $emails;
}
public function getProperty($name) {

View File

@@ -118,7 +118,19 @@ class MailtemplateDispatchController extends mfBaseController {
if($r->filter_order_open) {
$filter["order_open"] = true;
}
if($r->filter_order_int_products) {
$filter["order_int_products"] = true;
}
if($r->filter_billingaddress) {
$filter["billingaddress"] = true;
}
if($r->filter_techcontact) {
$filter["techcontact"] = true;
}
if($r->filter_zip_source) {
$filter["zip_source"] = $r->filter_zip_source;
}
if($r->filter_zip) {
foreach(preg_split("/[, ]+/", $r->filter_zip) as $zip) {
$filter["zip"][] = trim($zip);
@@ -142,6 +154,18 @@ class MailtemplateDispatchController extends mfBaseController {
return $this->addAction();
}
$dispatch->recipient_count = count($dispatch->getRecipients());
$dispatch->save();
if($r->send_testmail && $r->testmail_to) {
$dispatch->sendToRecipients($r->testmail_to);
$this->layout()->setFlash("Test Email erfolgreich versendet", "success");
$this->redirect("MailtemplateDispatch", "edit", ['id' => $dispatch->id]);
}
$this->layout()->setFlash("Emailversand erfolgreich gespeichert", "success");
$this->redirect("MailtemplateDispatch");