added new custom preorder notification
This commit is contained in:
83
scripts/preorder/custom_notifications/init.php
Normal file
83
scripts/preorder/custom_notifications/init.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
require("../../../config/config.php");
|
||||
require_once("init.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");
|
||||
|
||||
$me = new User(1);
|
||||
define("INTERNAL_USER_ID", $me->id);
|
||||
define("INTERNAL_USER_USERNAME", $me->username);
|
||||
define("MFBASE_BYPASS_LOGIN", true);
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
$db = $db->link;
|
||||
|
||||
function logMessage($message): void {
|
||||
echo "[" . date("Y-m-d H:i:s") . "] " . $message . "\n";
|
||||
}
|
||||
|
||||
function sendPreorderEmail($mailtemplate, $preordercampaign, $preorder, $email_type = "300-custom") {
|
||||
$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);
|
||||
} else {
|
||||
$data[$prop] = null;
|
||||
}
|
||||
}
|
||||
|
||||
$replacers = [];
|
||||
foreach($data as $values) {
|
||||
foreach($values as $key => $value) {
|
||||
if($key[0] !== "\0") $replacers[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$subject = $mailtemplate->getVariableReplacedSubject($replacers);
|
||||
$body = "<html><head></head><body>";
|
||||
$body .= $mailtemplate->renderBody($replacers);
|
||||
$body .= "</body></html>\n";
|
||||
|
||||
$body_type = $mailtemplate->body_html ? "html" : "text";
|
||||
|
||||
$email = new Emailnotification();
|
||||
$email->setSubject($subject);
|
||||
if($body_type == "html") {
|
||||
$email->setHtmlBody($body);
|
||||
} else {
|
||||
$email->setBody($body);
|
||||
}
|
||||
$email->setFrom($preordercampaign->from_email, $preordercampaign->from_email_name);
|
||||
$email->setTo($preorder->email);
|
||||
$email->setHeader("X-".ucfirst(MFAPPNAME)."-pid", $preorder->id);
|
||||
$email->setHeader("X-".ucfirst(MFAPPNAME)."-ps", '300-custom');
|
||||
// add attachments
|
||||
try {
|
||||
foreach ($mailtemplate->files as $file) {
|
||||
if (!$file->filename || !$file->file_id || !$file->file->store_filename) continue;
|
||||
logMessage("Adding attachment: " . $file->filename);
|
||||
$email->addAttachment($file->file->getFullPath(), null, $file->filename, $file->file->mimetype ?: null);
|
||||
}
|
||||
$email->send();
|
||||
logMessage("Email sent successfully for Preorder ID: $preorder->id, Campaign: $preordercampaign->name, Type: $email_type");
|
||||
$psn_log = PreorderStatusnotificationLog::create([
|
||||
"preorder_id" => $preorder->id,
|
||||
"email_type" => $email_type,
|
||||
"email" => $preorder->email,
|
||||
]);
|
||||
$psn_log->save();
|
||||
} catch (Exception $e) {
|
||||
logMessage("Error sending email for Preorder ID $preorder->id: " . $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +1,7 @@
|
||||
<?php
|
||||
|
||||
require("../../config/config.php");
|
||||
|
||||
define('FRONKDB_SQLDEBUG',false);
|
||||
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
|
||||
|
||||
function logMessage($message): void {
|
||||
echo "[" . date("Y-m-d H:i:s") . "] " . $message . "\n";
|
||||
}
|
||||
|
||||
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
|
||||
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
|
||||
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
|
||||
|
||||
$me = new User(1);
|
||||
define("INTERNAL_USER_ID", $me->id);
|
||||
define("INTERNAL_USER_USERNAME", $me->username);
|
||||
define("MFBASE_BYPASS_LOGIN", true);
|
||||
global $db;
|
||||
require_once 'init.php';
|
||||
|
||||
$sql = "WITH first_status_300_plus AS (
|
||||
SELECT
|
||||
@@ -54,11 +39,8 @@ WHERE p.type = 'order'
|
||||
AND psnl.id IS NULL -- No existing '300-custom' notification
|
||||
ORDER BY fs.first_status_300_timestamp ASC;";
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
$db = $db->link;
|
||||
|
||||
$res = $db->query($sql);
|
||||
// exit if no results
|
||||
|
||||
if ($res->num_rows == 0) {
|
||||
logMessage("No preorders found for custom 300 notification.");
|
||||
exit;
|
||||
@@ -68,12 +50,7 @@ while($row = $res->fetch_assoc()) {
|
||||
logMessage("Processing Preorder ID: " . $row['id']);
|
||||
$preorder_id = $row['id'];
|
||||
$campaign_name = $row['campaign_name'];
|
||||
$current_status_code = $row['current_status_code'];
|
||||
$current_status_name = $row['current_status_name'];
|
||||
$first_reached_300 = $row['first_reached_300_plus'];
|
||||
$days_since_first_300 = $row['days_since_first_300'];
|
||||
$mailtemplate_id = $row['mailtemplate_id'];
|
||||
$logical_config = json_decode($row['logical_config'], true);
|
||||
|
||||
$preorder = new Preorder($preorder_id);
|
||||
if (!$preorder || !$preorder->id) {
|
||||
@@ -95,44 +72,11 @@ while($row = $res->fetch_assoc()) {
|
||||
|
||||
logMessage("Preparing to send email for Preorder ID: $preorder_id, Campaign: $campaign_name");
|
||||
|
||||
sendPreorderEmail(
|
||||
$mailtemplate,
|
||||
$preordercampaign,
|
||||
$preorder,
|
||||
"300-custom"
|
||||
);
|
||||
|
||||
$subject = $mailtemplate->subject;
|
||||
$body = "<html><head></head><body>";
|
||||
$body .= $mailtemplate->renderBody();
|
||||
$body .= "</body></html>\n";
|
||||
|
||||
$body_type = $mailtemplate->body_html ? "html" : "text";
|
||||
|
||||
$email = new Emailnotification();
|
||||
$email->setSubject($subject);
|
||||
if($body_type == "html") {
|
||||
$email->setHtmlBody($body);
|
||||
} else {
|
||||
$email->setBody($body);
|
||||
}
|
||||
$email->setFrom($preordercampaign->from_email, $preordercampaign->from_email_name);
|
||||
$email->setTo($preorder->email);
|
||||
$email->setHeader("X-".ucfirst(MFAPPNAME)."-pid", $preorder->id);
|
||||
$email->setHeader("X-".ucfirst(MFAPPNAME)."-ps", '300-custom');
|
||||
// add attachments
|
||||
try {
|
||||
foreach ($mailtemplate->files as $file) {
|
||||
if (!$file->filename || !$file->file_id || !$file->file->store_filename) continue;
|
||||
logMessage("Adding attachment: " . $file->filename);
|
||||
$email->addAttachment($file->file->getFullPath(), null, $file->filename, $file->file->mimetype ?: null);
|
||||
}
|
||||
$email->send();
|
||||
logMessage("Email sent successfully for Preorder ID: $preorder_id");
|
||||
$psn_log = PreorderStatusnotificationLog::create([
|
||||
"preorder_id" => $preorder_id,
|
||||
"email_type" => "300-custom",
|
||||
"email" => $preorder->email,
|
||||
]);
|
||||
$psn_log->save();
|
||||
// Log the notification
|
||||
$log = new PreorderStatusnotificationLog();
|
||||
|
||||
} catch (Exception $e) {
|
||||
logMessage("Error sending email for Preorder ID $preorder_id: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
global $db;
|
||||
require_once 'init.php';
|
||||
|
||||
$sql = "SELECT
|
||||
p.id,
|
||||
p.type,
|
||||
p.preordercampaign_id,
|
||||
p.adb_wohneinheit_id,
|
||||
pc.name as campaign_name,
|
||||
ps_current.code as current_status_code,
|
||||
ps_current.name as current_status_name,
|
||||
w.id as wohneinheit_id,
|
||||
adb_status.code as wohneinheit_status_code,
|
||||
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'
|
||||
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 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
|
||||
ORDER BY p.id ASC;";
|
||||
|
||||
$res = $db->query($sql);
|
||||
|
||||
if ($res->num_rows == 0) {
|
||||
logMessage("No preorders found for custom 300-new-order notification.");
|
||||
exit;
|
||||
}
|
||||
|
||||
logMessage("Found " . $res->num_rows . " preorders for 300-custom-new-order notification.");
|
||||
|
||||
while($row = $res->fetch_assoc()) {
|
||||
logMessage("Processing Preorder ID: " . $row['id']);
|
||||
$preorder_id = $row['id'];
|
||||
$campaign_name = $row['campaign_name'];
|
||||
$mailtemplate_id = $row['mailtemplate_id'];
|
||||
$wohneinheit_status_code = $row['wohneinheit_status_code'];
|
||||
$wohneinheit_status_name = $row['wohneinheit_status_name'];
|
||||
|
||||
logMessage("Wohneinheit Status: {$wohneinheit_status_code} ({$wohneinheit_status_name})");
|
||||
|
||||
$preorder = new Preorder($preorder_id);
|
||||
if (!$preorder || !$preorder->id) {
|
||||
logMessage("Preorder with ID $preorder_id not found. Skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
$preordercampaign = new Preordercampaign($preorder->preordercampaign_id);
|
||||
if (!$preordercampaign || !$preordercampaign->id) {
|
||||
logMessage("Preordercampaign with ID {$preorder->preordercampaign_id} not found. Skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
$mailtemplate = new Mailtemplate($mailtemplate_id);
|
||||
if(!$mailtemplate || !$mailtemplate->id) {
|
||||
logMessage("Mailtemplate with ID $mailtemplate_id not found. Skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
logMessage("Preparing to send email for Preorder ID: $preorder_id, Campaign: $campaign_name");
|
||||
|
||||
sendPreorderEmail(
|
||||
$mailtemplate,
|
||||
$preordercampaign,
|
||||
$preorder,
|
||||
"300-custom-new-order"
|
||||
);
|
||||
}
|
||||
|
||||
logMessage("Completed processing 300-custom-new-order notifications.");
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user