Files
thetool/application/WarehouseOrderRequest/WarehouseOrderRequestController.php
2025-04-10 08:41:27 +00:00

221 lines
11 KiB
PHP

<?php /** @noinspection PhpUndefinedClassInspection */
/** @noinspection PhpUndefinedNamespaceInspection */
/** @noinspection PhpVoidFunctionResultUsedInspection */
class WarehouseOrderRequestController extends TTCrud {
protected string $headerTitle = 'Bestellwünsche';
protected string $createText = 'Neuer Bestellwunsch';
protected string $singleText = 'Bestellwunsch';
//@formatter:off
protected array $columns = [
['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' => [
'articleId' => [
'apiUrl' => '/WarehouseArticle/autoComplete',
'type' => 'autocomplete',
'emitDisplayValue' => true,
'customFieldReference' => 'WarehouseArticle',
'label' => 'Artikel',
],
'amount' => ['type' => 'input', 'label' => 'Menge', 'inputType' => 'number'],
'purpose' => ['type' => 'input', 'label' => 'Zweck'],
],
'validateFormOptions' => [
['key' => 'articleId', 'message' => 'Bitte füllen Sie den Artikel aus'],
['key' => 'amount', 'message' => 'Bitte füllen Sie die Menge aus'],
],
]], 'table' => false],
['key' => 'linkedOrderIds', 'text' => 'Verlinkte Bestellung', 'modal' => false],
['key' => 'createBy', 'text' => 'Erstellt von', 'required' => true, 'modal' => ['visible' => false, 'type' => 'select'], 'table' => ['filter' => 'select']],
['key' => 'create', 'text' => 'Erstellt am', 'required' => true, 'modal' => false],
['key' => 'cancelled', 'text' => 'Storniert', 'modal' => ['visible' => false, 'type' => 'icon-select', 'items' => [
['value' => 0, 'text' => 'Bestellwunsch nicht storniert', 'icon' => 'fa-regular fa-circle-check text-success'],
['value' => 1, 'text' => 'Bestellwunsch storniert', 'icon' => 'fa-regular fa-circle-xmark text-danger']]], 'table' => ['filter' => 'iconSelect']
],
['key' => 'done', 'text' => 'Erledigt', 'modal' => ['visible' => false, 'type' => 'icon-select', 'items' => [
['value' => 0, 'text' => 'Bestellwunsch nicht erledigt', 'icon' => 'fa-regular fa-circle-xmark text-danger'],
['value' => 1, 'text' => 'Bestellwunsch erledigt', 'icon' => 'fa-regular fa-circle-check text-success']]], 'table' => ['filter' => 'iconSelect']
],
['key' => 'actions', 'text' => 'Aktionen', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center']],
];
//@formatter:on
protected array $permissionCheck = ['WarehouseUser'];
protected array $defaultOrder = ['key' => 'create', 'order' => 'DESC'];
protected array $additionalActions = [
['key' => 'openHistory', 'title' => 'Historie', 'class' => 'fas fa-history text-primary'],
['key' => 'createLog', 'title' => 'Log-Eintrag erstellen', 'class' => 'fas fa-plus text-primary'],
];
protected function prepareCrudConfig() {
$this->additionalJSVariables = [
'user_id' => $this->user->id,
'BASE_URL' => '/WarehouseOrderRequest',
'WAREHOUSE_ADMIN' => $this->user->can('WarehouseAdmin')
];
}
protected function beforeUpdate($postData): bool {
(new WarehouseHistoryController)->create($postData, $this->mod);
return true;
}
protected function cancelAction() {
$id = filter_var($this->request->id, FILTER_VALIDATE_INT);
$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 (!(WarehouseOrderRequest::get($id))) self::returnJson(['error' => 'Bestellwunsch nicht gefunden']);
$currentData = (array) WarehouseOrderRequest::get($id);
WarehouseOrderRequest::update(array_merge($currentData, ['id' => $id, 'cancelled' => $cancel]));
self::returnJson(['success' => true]);
}
public function customAutoCompleteAddressId($id): array {
$address = new Address($id);
return ['title' => $address->getCompanyOrName(), 'value' => $address->id];
}
protected function doneAction() {
$id = filter_var($this->request->id, FILTER_VALIDATE_INT);
$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 (!(WarehouseOrderRequest::get($id))) self::returnJson(['error' => 'Bestellwunsch nicht gefunden']);
$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;
}
}
protected function createNewLogAction() {
$postData = json_decode(file_get_contents('php://input'), true);
if (empty($postData['orderRequestId']) || empty($postData['note'])) {
self::returnJson(['error' => 'Order Request ID is required']);
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;
}
WarehouseLogModel::create([
"table" => "WarehouseOrderRequest",
"rowId" => intval($postData['orderRequestId']),
"type" => 'noChanges',
"message" => $postData['note'],
"createBy" => intval($this->user->id),
"create" => time()
]);
self::returnJson(['success' => 'Log entry created']);
}
protected function getLogByIdAction() {
$orderRequestId = $this->request->orderRequestId;
if (empty($orderRequestId)) {
self::returnJson(['error' => 'Order ID is required']);
return;
}
$log = WarehouseLogModel::getAll(['table' => 'WarehouseOrderRequest', 'rowId' => $orderRequestId]);
self::returnJson($log);
}
protected function getHistoryAction() {
$this->prepareCrudConfig();
self::returnJson((new WarehouseHistoryController)->getHistory($this->request->id, $this->mod, $this->columns));
}
}