diff --git a/Layout/default/Preorder/Index.php b/Layout/default/Preorder/Index.php index b47d9e3bb..3fa4d331d 100644 --- a/Layout/default/Preorder/Index.php +++ b/Layout/default/Preorder/Index.php @@ -867,7 +867,7 @@ $pagination_entity_name = "Vorbestellungen"; icon_name = "industry"; } - var marker_popup_content = ''; + var marker_popup_content = ``; // popup fields const preorder_view_url = `/Index?filter[ucode]=${preorder.ucode}#preorder=${preorder.id}`; diff --git a/Layout/default/Preorder/include/preorder-detail.php b/Layout/default/Preorder/include/preorder-detail.php index ed27f501c..35b970bc4 100644 --- a/Layout/default/Preorder/include/preorder-detail.php +++ b/Layout/default/Preorder/include/preorder-detail.php @@ -832,7 +832,7 @@ - data->status_code) ?> + data->status_code) ? $statusLog->data->status_code : $statusLog->data->email_type?> diff --git a/application/PreorderStatusnotificationLog/PreorderStatusnotificationLog.php b/application/PreorderStatusnotificationLog/PreorderStatusnotificationLog.php index 30b189054..58ba09fe3 100644 --- a/application/PreorderStatusnotificationLog/PreorderStatusnotificationLog.php +++ b/application/PreorderStatusnotificationLog/PreorderStatusnotificationLog.php @@ -11,7 +11,7 @@ class PreorderStatusnotificationLog extends mfBaseModel { $model = new PreorderStatusnotificationLog(); $table_fields = [ - "preorder_id", "status_code", "email", + "preorder_id", "status_code", "email", "email_type", "create_by", "edit_by", "create", "edit" ]; diff --git a/db/migrations/20250605080000_preorder_notification_log_modify.php b/db/migrations/20250605080000_preorder_notification_log_modify.php new file mode 100644 index 000000000..1df8dc0f7 --- /dev/null +++ b/db/migrations/20250605080000_preorder_notification_log_modify.php @@ -0,0 +1,58 @@ +getEnvironment() == "thetool") { + $PreorderStatusnotificationLogTable = $this->table("PreorderStatusnotificationLog"); + + if ($PreorderStatusnotificationLogTable->hasColumn("status_code")) { + $PreorderStatusnotificationLogTable + ->changeColumn("status_code", "integer", ["null" => true]) + ->update(); + } + + if (!$PreorderStatusnotificationLogTable->hasColumn("email_type")) { + $PreorderStatusnotificationLogTable + ->addColumn("email_type", "string", [ + "limit" => 50, + "null" => true, + "after" => "email" + ]) + ->update(); + } + + if (!$PreorderStatusnotificationLogTable->hasIndex(["email_type"])) { + $PreorderStatusnotificationLogTable + ->addIndex(["email_type"], ["name" => "idx_email_type"]) + ->update(); + } + } + } + + public function down(): void { + if ($this->getEnvironment() == "thetool") { + $PreorderStatusnotificationLogTable = $this->table("PreorderStatusnotificationLog"); + + if ($PreorderStatusnotificationLogTable->hasIndex(["email_type"])) { + $PreorderStatusnotificationLogTable + ->removeIndex(["email_type"]) + ->update(); + } + + if ($PreorderStatusnotificationLogTable->hasColumn("email_type")) { + $PreorderStatusnotificationLogTable + ->removeColumn("email_type") + ->update(); + } + + if ($PreorderStatusnotificationLogTable->hasColumn("status_code")) { + $PreorderStatusnotificationLogTable + ->changeColumn("status_code", "integer", ["null" => false]) + ->update(); + } + } + } +} diff --git a/scripts/preorder/send-preorder-custom-300-notification.php b/scripts/preorder/send-preorder-custom-300-notification.php new file mode 100644 index 000000000..c0b191ea2 --- /dev/null +++ b/scripts/preorder/send-preorder-custom-300-notification.php @@ -0,0 +1,138 @@ +id); +define("INTERNAL_USER_USERNAME", $me->username); +define("MFBASE_BYPASS_LOGIN", true); + +$sql = "WITH first_status_300_plus AS ( + SELECT + ph.preorder_id, + MIN(ph.create) as first_status_300_timestamp + FROM PreorderHistory ph + INNER JOIN Preorderstatus ps ON CAST(ph.new_value AS UNSIGNED) = ps.id + WHERE ph.key = 'status_id' + AND ps.code >= 300 + AND ph.create <= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 8 WEEK)) + GROUP BY ph.preorder_id +) +SELECT + p.id, + p.type, + p.preordercampaign_id, + pc.name as campaign_name, + ps_current.code as current_status_code, + ps_current.name as current_status_name, + FROM_UNIXTIME(fs.first_status_300_timestamp) as first_reached_300_plus, + DATEDIFF(NOW(), FROM_UNIXTIME(fs.first_status_300_timestamp)) as days_since_first_300, + 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 first_status_300_plus fs ON p.id = fs.preorder_id +LEFT JOIN PreorderStatusnotificationLog psnl ON p.id = psnl.preorder_id AND psnl.email_type = '300-custom' +WHERE p.type = 'order' + AND ps_current.code >= 300 + AND ps_current.code < 500 + AND pcsmt.notification_type = 'logical' + AND JSON_EXTRACT(pcsmt.logical_config, '$.type') = '300-custom' + 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; +} + +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) { + 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"); + + + $subject = $mailtemplate->subject; + $body = ""; + $body .= $mailtemplate->renderBody(); + $body .= "\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()); + } +} \ No newline at end of file