Files
thetool/scripts/preorder/custom_notifications/send-preorder-custom-new-noticiation.php
2025-06-10 10:54:59 +02:00

84 lines
3.0 KiB
PHP

<?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.");
?>