Completed 300-custom-new-order Mailtemplate handling for RML

This commit is contained in:
Frank Schubert
2025-06-13 05:40:17 +02:00
parent 05fe36679f
commit fdcac66a84
5 changed files with 140 additions and 17 deletions

View File

@@ -151,10 +151,13 @@
function initEditor() {
tinymce.init({
//font_formats: "Arial=arial,sans-serif;",
images_dataimg_filter: function(img) {
return img.hasAttribute('internal-blob');
},
selector: '#body_html',
language: 'de',
skin: "tinymce-5",
plugins: ' code link autolink lists table',
plugins: ' code link autolink lists table image media',
paste_block_drop: true,
paste_as_text: true,
paste_data_images: true,

View File

@@ -414,7 +414,7 @@ class Preorder extends mfBaseModel {
return $to;
}
private function getPropertiesAsArray() {
public function getPropertiesAsArray() {
$preorder = clone $this;
unset($preorder->data, $preorder->_old_data, $preorder->db, $preorder->log);
$status = $this->getProperty("status");
@@ -426,6 +426,11 @@ class Preorder extends mfBaseModel {
if(substr($key, 0, 1) == "\0") continue;
$return[$key] = $value;
}
$return["status_code"] = $status->code;
$return["status_name"] = $status->name;
$return["contact_fullname"] = $this->getCompanyOrName();
return $return;
}
@@ -635,6 +640,30 @@ class Preorder extends mfBaseModel {
return true;
}
public function getCompanyOrName() {
$company = trim($this->company);
if($company) {
return $company;
}
return $this->getFullName();
}
public function getFullName() {
// Assumes "Firma1 Firma2" or "firstname lastname" as readable form
$name = "";
if($this->firstname && $this->lastname) {
$name = $this->firstname . " " . $this->lastname;
} elseif($this->lastname) {
$name = $this->lastname;
} elseif($this->firstname) {
$name = $this->firstname;
}
return $name;
}
public function setOrCreateOaid($oaid_attributes = false) {
$campaign = new Preordercampaign($this->preordercampaign_id);
//var_dump($campaign);exit;

View File

@@ -366,7 +366,11 @@ class mfBaseModel {
}
public function __unset($name) {
unset($this->data->$name);
if($name === "data") {
unset($this->data);
} else {
unset($this->data->$name);
}
}
public function __debugInfo() {

View File

@@ -22,15 +22,19 @@ function logMessage($message): void {
echo "[" . date("Y-m-d H:i:s") . "] " . $message . "\n";
}
function sendPreorderEmail($mailtemplate, $preordercampaign, $preorder, $email_type = "300-custom") {
function sendPreorderEmail($mailtemplate, $preordercampaign, $preorder, $email_type = "300-custom", $custom_replacers = []) {
$data = ["preorder" => $preorder->getPropertiesAsArray()];
foreach(["adb_hausnummer", "adb_wohneinheit"] as $prop) {
$obj = $preorder->getProperty($prop);
if($obj) {
$obj_data = (array) $obj->data;
unset($obj->data, $obj->_old_data, $obj->db, $obj->log);
$data[$prop] = array_merge((array) $obj, $obj_data);
//unset($obj->data, $obj->_old_data, $obj->db, $obj->log);
foreach($obj as $key => $value) {
if($key == "data" || $key == "_old_data" || $key == "db" || $key == "log") continue;
$data[$prop][$key] = $value;
}
$data[$prop] = array_merge((array) $data[$prop], $obj_data);
} else {
$data[$prop] = null;
}
@@ -43,6 +47,13 @@ function sendPreorderEmail($mailtemplate, $preordercampaign, $preorder, $email_t
}
}
if(count($custom_replacers)) {
foreach($custom_replacers as $key => $value) {
if(!$key) continue;
$replacers[$key] = $value;
}
}
$subject = $mailtemplate->getVariableReplacedSubject($replacers);
$body = "<html><head></head><body>";
$body .= $mailtemplate->renderBody($replacers);

View File

@@ -1,7 +1,12 @@
<?php
global $db;
require_once 'init.php';
require_once __DIR__.'/init.php';
$earliest_order_date = new DateTime("2025-05-01");
$earliest_order_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$allowed_netowners = [4807]; // vorerst nur RML
$sql = "SELECT
p.id,
@@ -16,20 +21,22 @@ $sql = "SELECT
adb_status.name as wohneinheit_status_name,
pcsmt.mailtemplate_id,
pcsmt.logical_config
FROM Preorder p
INNER JOIN Preordercampaign pc ON p.preordercampaign_id = pc.id
INNER JOIN PreordercampaignStatusnotificationMailtemplate pcsmt ON pc.id = pcsmt.preordercampaign_id
INNER JOIN Preorderstatus ps_current ON p.status_id = ps_current.id
INNER JOIN addressdb.Wohneinheit w ON p.adb_wohneinheit_id = w.id
INNER JOIN addressdb.Status adb_status ON w.status_id = adb_status.id
LEFT JOIN PreorderStatusnotificationLog psnl ON p.id = psnl.preorder_id AND psnl.email_type = '300-custom-new-order'
FROM `" . FRONKDB_DBNAME . "`.Preorder p
INNER JOIN `" . FRONKDB_DBNAME . "`.Preordercampaign pc ON p.preordercampaign_id = pc.id
INNER JOIN `" . FRONKDB_DBNAME . "`.PreordercampaignStatusnotificationMailtemplate pcsmt ON pc.id = pcsmt.preordercampaign_id
INNER JOIN `" . FRONKDB_DBNAME . "`.Preorderstatus ps_current ON p.status_id = ps_current.id
INNER JOIN `" . ADDRESSDB_DBNAME . "`.Wohneinheit w ON p.adb_wohneinheit_id = w.id
INNER JOIN `" . ADDRESSDB_DBNAME . "`.Status adb_status ON w.status_id = adb_status.id
LEFT JOIN `" . FRONKDB_DBNAME . "`.PreorderStatusnotificationLog psnl ON p.id = psnl.preorder_id AND psnl.email_type = '300-custom-new-order'
WHERE p.type = 'order'
AND p.adb_wohneinheit_id IS NOT NULL -- Must have a wohneinheit linked
AND adb_status.code = 300 -- Wohneinheit status must be 300
AND adb_status.type = 'wohneinheit' -- Ensure we're checking the right status type
AND adb_status.code < 300 -- Wohneinheit status must be less than 300
-- AND adb_status.type = 'wohneinheit' -- Ensure we're checking the right status type
AND pcsmt.notification_type = 'logical'
AND JSON_EXTRACT(pcsmt.logical_config, '$.type') = '300-custom-new-order'
AND psnl.id IS NULL -- No existing '300-custom-new-order' notification
AND p.create > ".$earliest_order_date->getTimestamp()." -- no older than \$earliest_order_date
-- AND p.create < (UNIX_TIMESTAMP(NOW()) - 86400) -- Only consider preorders older than 24 hours
ORDER BY p.id ASC;";
$res = $db->query($sql);
@@ -57,12 +64,27 @@ while($row = $res->fetch_assoc()) {
continue;
}
if($preorder->status->code >= 899) {
logMessage("Preorder with ID $preorder_id has status {$preorder->status->code} ({$preorder->status->name}). Skipping.");
continue;
}
$preordercampaign = new Preordercampaign($preorder->preordercampaign_id);
if (!$preordercampaign || !$preordercampaign->id) {
logMessage("Preordercampaign with ID {$preorder->preordercampaign_id} not found. Skipping.");
continue;
}
$netowner_id = $preordercampaign->network->owner_id;
if(!$netowner_id) {
logMessage("No Netowner ID found for Preorder ID $preorder_id. Skipping.");
continue;
}
if(!in_array($netowner_id, $allowed_netowners)) {
continue;
}
$mailtemplate = new Mailtemplate($mailtemplate_id);
if(!$mailtemplate || !$mailtemplate->id) {
logMessage("Mailtemplate with ID $mailtemplate_id not found. Skipping.");
@@ -71,12 +93,66 @@ while($row = $res->fetch_assoc()) {
logMessage("Preparing to send email for Preorder ID: $preorder_id, Campaign: $campaign_name");
//var_dump($preorder);exit;
// find correct price embed based on actual setup price
$po = PreordercampaignOperatorModel::getFirst(["preordercampaign_id" => $preordercampaign->id, "isp_id" => $preorder->partner_id]);
if(!$po) {
$po = PreordercampaignOperatorModel::getFirst(["preordercampaign_id" => $preordercampaign->id, "operator_id" => $preorder->partner_id]);
}
if(!$po) {
logMessage("No Netoperator found for Preorder ID $preorder_id and Campaign ID {$preordercampaign->id}. Skipping.");
continue;
}
//var_dump($po);exit;
$netoperator = new Address($po->operator_id);
if(!$netoperator) {
logMessage("No Netoperator found for Preorder ID $preorder_id and Campaign ID {$preordercampaign->id}. Skipping.");
continue;
}
if($preorder->order_date) {
$order_date = new DateTime('@'.$preorder->order_date);
} else {
$order_date = new DateTime('@'.$preorder->create);
}
$order_date->setTime(4,0,0);
$order_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$product = PreorderProduct::getFirst(["type" => "enduser_setup"]);
if(!$product) {
logMessage("operator_setup price not found! Preorder $preorder_id");
continue;
}
$product->setNetownerId($netowner_id);
$product->setNetoperatorId($netoperator->id);
$price = $product->getCampaignPrice($preorder->preordercampaign_id, $order_date->format("Y-m-d"));
if(!$price) {
logMessage("No price found for Preorder ID $preorder_id and Campaign ID {$preordercampaign->id}. Skipping.");
continue;
}
$price_setup = $price->price_setup;
$template_embed_name = "{{EMBED:tpl-rml-neue-bestellung-preistext-option1-gratis}}";
if($price_setup > 0.0000) {
$template_embed_name = "{{EMBED:tpl-rml-neue-bestellung-preistext-option2-preis}}";
}
sendPreorderEmail(
$mailtemplate,
$preordercampaign,
$preorder,
"300-custom-new-order"
"300-custom-new-order",
[
"RML_NEW_ORDER_PRICE_OPTION_EMBED" => $template_embed_name,
"price_setup" => number_format($price_setup, 2, ",", "."),
],
);
}
logMessage("Completed processing 300-custom-new-order notifications.");