fixed multiple stuff

This commit is contained in:
Luca Haid
2025-04-23 22:41:28 +02:00
parent 092e9c558a
commit 82a2889e92
13 changed files with 296 additions and 735 deletions
@@ -75,7 +75,7 @@ class WarehouseOrderRequestController extends TTCrud {
if (!$id || $cancel === false) self::returnJson(['error' => 'Ungültige Anfrage']);
if (!(WarehouseOrderRequest::get($id))) self::returnJson(['error' => 'Bestellwunsch nicht gefunden']);
$currentData = (array) WarehouseOrderRequest::get($id);
$currentData = (array)WarehouseOrderRequest::get($id);
WarehouseOrderRequest::update(array_merge($currentData, ['id' => $id, 'cancelled' => $cancel]));
self::returnJson(['success' => true]);
}
@@ -92,66 +92,29 @@ class WarehouseOrderRequestController extends TTCrud {
if (!$id || $done === false) self::returnJson(['error' => 'Ungültige Anfrage']);
if (!(WarehouseOrderRequest::get($id))) self::returnJson(['error' => 'Bestellwunsch nicht gefunden']);
$currentData = (array) WarehouseOrderRequest::get($id);
$currentData = (array)WarehouseOrderRequest::get($id);
WarehouseOrderRequest::update(array_merge($currentData, ['id' => $id, 'done' => $done]));
self::returnJson(['success' => true]);
}
private function getPHPMailer() {
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = TT_WAREHOUSE_ORDER_SMTP_HOST;
$mail->SMTPAuth = true;
$mail->Username = TT_WAREHOUSE_ORDER_SMTP_USER;
$mail->Password = TT_WAREHOUSE_ORDER_SMTP_PASS;
$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
return $mail;
} catch (Exception $e) {
self::returnJson(['error' => 'Mailer Error: ' . $mail->ErrorInfo]);
exit;
}
}
protected function afterCreate($orderRequest) {
try {
$mail = $this->getPHPMailer();
$mail->setFrom('einkauf@xinon.at', 'XINON Einkauf');
$mail->addAddress('einkauf@xinon.at', 'XINON Einkauf');
$mail->isHTML(true);
$mail->Subject = "Neuer Bestellwunsch #" . $orderRequest['id'] . " von " . $this->user->name . ' eingelangt';
// build html table and fetch articleId if set else use articleId_text if its a text article
$html = '<table style="width: 100%; border-collapse: collapse;">';
$html .= '<tr><th style="border: 1px solid #000; padding: 8px;">Artikel</th><th style="border: 1px solid #000; padding: 8px;">Menge</th><th style="border: 1px solid #000; padding: 8px;">Zweck</th></tr>';
foreach ($orderRequest['positions'] as $position) {
$articleId = isset($position['articleId']) ? WarehouseArticleModel::get($position['articleId'])->title : $position['articleId_text'];
$html .= '<tr>';
$html .= '<td style="border: 1px solid #000; padding: 8px;">' . htmlspecialchars($articleId) . '</td>';
$html .= '<td style="border: 1px solid #000; padding: 8px;">' . htmlspecialchars($position['amount']) . '</td>';
$html .= '<td style="border: 1px solid #000; padding: 8px;">' . htmlspecialchars($position['purpose']) . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
// Set the HTML content
$mail->Body = "Neuer Bestellwunsch #" . $orderRequest['id'] . " von " . $this->user->name . ' eingelangt<br><br>' .
'Notiz: ' . htmlspecialchars($orderRequest['note']) . '<br><br>' . $html;
// Send the email
if (!$mail->send()) {
self::returnJson(['error' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo]);
exit;
}
} catch (Exception $e) {
self::returnJson(['error' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo]);
exit;
$rows = '';
foreach ($orderRequest['positions'] as $position) {
$rows .= sprintf(
'<tr><td style="border: 1px solid #000; padding: 8px;">%s</td><td style="border: 1px solid #000; padding: 8px;">%s</td><td style="border: 1px solid #000; padding: 8px;">%s</td></tr>',
htmlspecialchars(isset($position['articleId']) ? WarehouseArticleModel::get($position['articleId'])->title : $position['articleId_text']),
htmlspecialchars($position['amount']),
htmlspecialchars($position['purpose'])
);
}
$html = '<table style="width: 100%; border-collapse: collapse;"><tr><th style="border: 1px solid #000; padding: 8px;">Artikel</th><th style="border: 1px solid #000; padding: 8px;">Menge</th><th style="border: 1px solid #000; padding: 8px;">Zweck</th></tr>' . $rows . '</table>';
WarehouseEmailQueue::create([
'from' => 'einkauf@xinon.at', 'fromName' => 'XINON Einkauf', 'to' => 'einkauf@xinon.at', 'toName' => 'XINON Einkauf',
'subject' => "Neuer Bestellwunsch #{$orderRequest['id']} eingelangt",
'body' => sprintf("Neuer Bestellwunsch #%s von %s eingelangt<br><br>Notiz: %s<br><br>%s", $orderRequest['id'], $this->user->name, htmlspecialchars($orderRequest['note']), $html),
'create' => time(), 'createBy' => intval($this->user->id),
]);
}
protected function createNewLogAction() {
@@ -162,34 +125,17 @@ class WarehouseOrderRequestController extends TTCrud {
return;
}
// send email to einkauf@xinon.at
// Instantiate PHPMailer
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = TT_WAREHOUSE_ORDER_SMTP_HOST;
$mail->SMTPAuth = true;
$mail->Username = TT_WAREHOUSE_ORDER_SMTP_USER;
$mail->Password = TT_WAREHOUSE_ORDER_SMTP_PASS;
$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Recipients
$mail->setFrom('einkauf@xinon.at', 'XINON Einkauf');
$mail->addAddress('einkauf@xinon.at', 'XINON Einkauf');
// Content
$mail->isHTML(true);
$mail->Subject = "Neue Nachricht zu Besteallwunsch #" . $postData['orderRequestId'];
$mail->Body = $postData['note'];
$mail->send();
} catch (Exception $e) {
self::returnJson(['error' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo]);
exit;
}
if (intval($this->user->id !== 1) && intval($this->user->id !== 7))
WarehouseEmailQueue::create([
'from' => 'einkauf@xinon.at',
'fromName' => 'XINON Einkauf',
'to' => 'einkauf@xinon.at',
'toName' => 'XINON Einkauf',
'subject' => 'Neue Nachricht zu Besteallwunsch #' . $postData['orderRequestId'],
'body' => $postData['note'],
'create' => date('U'),
'createBy' => intval($this->user->id),
]);
WarehouseLogModel::create([
"table" => "WarehouseOrderRequest",