Merge branch 'WarehouseOrderRequest/add-email-notification' into 'master'

added email notification

See merge request fronk/thetool!1195
This commit is contained in:
Luca Haid
2025-04-10 08:39:28 +00:00
3 changed files with 81 additions and 28 deletions

View File

@@ -1,9 +1,15 @@
<?php
/**
* @property mixed|null $name
*/
class WarehouseOrderRequest extends mfBaseModel
{
class WarehouseOrderRequest extends TTCrudBaseModel {
public int $id;
public ?int $addressId;
public string $purpose;
public string $positions;
public ?string $note;
public ?string $linkedOrderIds;
public ?int $cancelled;
public ?int $done;
public int $create;
public int $createBy;
}
}

View File

@@ -1,4 +1,7 @@
<?php /** @noinspection PhpVoidFunctionResultUsedInspection */
<?php /** @noinspection PhpUndefinedClassInspection */
/** @noinspection PhpUndefinedNamespaceInspection */
/** @noinspection PhpVoidFunctionResultUsedInspection */
class WarehouseOrderRequestController extends TTCrud {
protected string $headerTitle = 'Bestellwünsche';
@@ -10,6 +13,7 @@ class WarehouseOrderRequestController extends TTCrud {
['key' => 'id', 'text' => 'Bestellnummer', 'table' => ['filter' => false], 'modal' => false],
['key' => 'addressId', 'text' => 'Kunde', 'required' => false, 'type' => 'autocomplete', 'table' => ['class' => 'text-nowrap', 'filter' => 'autocomplete'], 'modal' => ['apiUrl' => 'Address/api?do=findAddress&fibu_primary_account=1', 'items' => '/Address/Api?do=findAddress&fibu_primary_account=1', 'type' => 'autocomplete']],
['key' => 'purpose', 'text' => 'Verwendungszweck', 'required' => true],
['key' => 'note', 'text' => 'Notiz', 'required' => false],
['key' => 'positions', 'text' => 'Positionen', 'required' => true, 'modal' => ['type' => 'positions-manager', 'config' => [
'header' => 'Positionen',
'fields' => [
@@ -69,10 +73,10 @@ class WarehouseOrderRequestController extends TTCrud {
$cancel = filter_var($this->request->cancel, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'max_range' => 1]]);
if (!$id || $cancel === false) self::returnJson(['error' => 'Ungültige Anfrage']);
if (!(WarehouseOrderRequestModel::get($id))) self::returnJson(['error' => 'Bestellwunsch nicht gefunden']);
if (!(WarehouseOrderRequest::get($id))) self::returnJson(['error' => 'Bestellwunsch nicht gefunden']);
$currentData = (array) WarehouseOrderRequestModel::get($id);
WarehouseOrderRequestModel::update(array_merge($currentData, ['id' => $id, 'cancelled' => $cancel]));
$currentData = (array) WarehouseOrderRequest::get($id);
WarehouseOrderRequest::update(array_merge($currentData, ['id' => $id, 'cancelled' => $cancel]));
self::returnJson(['success' => true]);
}
@@ -86,13 +90,71 @@ class WarehouseOrderRequestController extends TTCrud {
$done = filter_var($this->request->done, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'max_range' => 1]]);
if (!$id || $done === false) self::returnJson(['error' => 'Ungültige Anfrage']);
if (!(WarehouseOrderRequestModel::get($id))) self::returnJson(['error' => 'Bestellwunsch nicht gefunden']);
if (!(WarehouseOrderRequest::get($id))) self::returnJson(['error' => 'Bestellwunsch nicht gefunden']);
$currentData = (array) WarehouseOrderRequestModel::get($id);
WarehouseOrderRequestModel::update(array_merge($currentData, ['id' => $id, 'done' => $done]));
$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();
// Recipients luca@haid.in
$mail->setFrom('einkauf@xinon.at', 'XINON Einkauf');
$mail->addAddress('luca@haid.in', '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;
}
}
protected function createNewLogAction() {
$postData = json_decode(file_get_contents('php://input'), true);

View File

@@ -1,15 +0,0 @@
<?php
class WarehouseOrderRequestModel extends TTCrudBaseModel {
public int $id;
public ?int $addressId;
public string $purpose;
public string $positions;
public ?string $note;
public ?string $linkedOrderIds;
public ?int $cancelled;
public ?int $done;
public int $create;
public int $createBy;
}