added new warehouseorder features

This commit is contained in:
Luca Haid
2025-03-07 08:04:36 +01:00
parent d24c1cc5dc
commit 7e3b1af54a
11 changed files with 460 additions and 119 deletions
@@ -89,85 +89,210 @@ class WarehouseOrderController extends TTCrud {
return $order;
}
protected function createPDFAction() {
protected function createPDFAction($returnFilename = false) {
$order = (array) WarehouseOrderModel::get($this->request->id);
$order['positions'] = json_decode($order['positions'], true);
// check if all positions have the same distributor
$distributorId = $order['positions'][0]['distributorId'];
foreach ($order['positions'] as $key => $position) {
if ($position['distributorId'] !== $distributorId) {
self::returnJson(['error' => 'Die Bestellung enthält Positionen von verschiedenen Lieferanten.']);
}
// we need to get the article name and distributor name for the pdf
$position['distributorName'] = WarehouseDistributorModel::get($position['distributorId'])->name;
$position['articleName'] = WarehouseArticleModel::get($position['article'])->title;
$position['articleDescription'] = WarehouseArticleModel::get($position['article'])->description;
$order['positions'][$key] = $position;
// Validate distributor consistency
$distributorIds = array_column($order['positions'], 'distributorId');
if (count(array_unique($distributorIds)) > 1) {
self::returnJson(['error' => 'Die Bestellung enthält Positionen von verschiedenen Lieferanten.']);
}
$pdf_vars = ['order' => $order,
'distributor' => WarehouseDistributorModel::get($distributorId),
'distributorCountryText' => (new Country(WarehouseDistributorModel::get($distributorId)->countryId))->name,
"bank_iban" => TT_INVOICE_BANK_IBAN,
"bank_bic" => TT_INVOICE_BANK_BIC,
"bank_bank" => TT_INVOICE_BANK_BANK,
"bank_owner" => TT_INVOICE_BANK_OWNER];
// Prepare data
$distributor = WarehouseDistributorModel::get($distributorIds[0]);
$countryText = (new Country($distributor->countryId))->name;
$isGermanSpeaking = in_array($countryText, ['Österreich', 'Deutschland', 'Schweiz']);
$isDefaultAddress = $order['delAddrLine'] === "Fladnitz im Raabtal 150";
$countryText = (new Country(WarehouseDistributorModel::get($distributorId)->countryId))->name;
$shouldGenerateEnglisch = !in_array($countryText, ['Österreich', 'Deutschland', 'Schweiz']);
// Enhance positions
foreach ($order['positions'] as &$position) {
$article = WarehouseArticleModel::get($position['article']);
$position += [
'distributorName' => $distributor->name,
'articleName' => $article->title,
'articleDescription' => $article->description
];
}
$headerHtml = file_get_contents(BASEDIR . "/Layout/default/WarehouseOrder/PDF_HEADER.html");
$headerHtml = str_replace("{{ basedir }}", BASEDIR, $headerHtml);
$headerHtml = str_replace("{{ externalReference }}", !empty($order['extReference']) && count($order['extReference']) > 0 ? "<strong>Ext. Ref.:</strong> ". $order['extReference'] : "", $headerHtml);
$headerHtml = str_replace("{{ addressLine_header }}", $shouldGenerateEnglisch ? "Supplier" : "Lieferant", $headerHtml);
$headerHtml = str_replace("{{ addressLine_1 }}", WarehouseDistributorModel::get($distributorId)->name, $headerHtml);
$headerHtml = str_replace("{{ addressLine_2 }}", WarehouseDistributorModel::get($distributorId)->address, $headerHtml);
$headerHtml = str_replace("{{ addressLine_3 }}", WarehouseDistributorModel::get($distributorId)->plz . " " . WarehouseDistributorModel::get($distributorId)->city, $headerHtml);
$headerHtml = str_replace("{{ addressLine_4 }}", $countryText, $headerHtml);
// Prepare PDF variables
$pdfVars = [
'order' => $order,
'distributor' => $distributor,
'distributorCountryText' => $countryText,
'bank' => [
'iban' => TT_INVOICE_BANK_IBAN,
'bic' => TT_INVOICE_BANK_BIC,
'bank' => TT_INVOICE_BANK_BANK,
'owner' => TT_INVOICE_BANK_OWNER
]
];
$headerHtml = str_replace("{{ billingAddressLine_header }}", $shouldGenerateEnglisch ? "Billing Address" : "Rechnungsadresse", $headerHtml);
$headerHtml = str_replace("{{ billingAddressLine_1 }}", "Xinon GmbH", $headerHtml);
$headerHtml = str_replace("{{ billingAddressLine_2 }}", "Fladnitz im Raabtal 150", $headerHtml);
$headerHtml = str_replace("{{ billingAddressLine_3 }}", "A-8322 Studenzen", $headerHtml);
$headerHtml = str_replace("{{ billingAddressLine_4 }}", "UID: ATU68711968", $headerHtml);
$headerHtml = str_replace("{{ billingAddressLine_5 }}", "EORI-Nr.: ATEOS1000085074", $headerHtml);
$headerHtml = str_replace("{{ billingAddressLine_6 }}", "<strong>Referenz: ". $order["orderNumber"] . "</strong>", $headerHtml);
$chk = $order['delAddrLine'] == "Fladnitz im Raabtal 150";
$headerHtml = str_replace("{{ shippingAddressLine_header }}", $chk ? "" : ($shouldGenerateEnglisch ? "Shipping Address" : "Lieferadresse"), $headerHtml);
$headerHtml = str_replace("{{ shippingAddressLine_1 }}", $chk ? "" : $order['delAddrName'], $headerHtml);
$headerHtml = str_replace("{{ shippingAddressLine_2 }}", $chk ? "" : $order['delAddrLine'], $headerHtml);
$headerHtml = str_replace("{{ shippingAddressLine_3 }}", $chk ? "" : $order['delAddrPLZ'] . " " . $order['delAddrCity'], $headerHtml);
$headerHtml = str_replace("{{ shippingAddressLine_4 }}", $chk ? "" : $order['delAddrEMail'], $headerHtml);
// Generate HTML templates
$replacements = [
'basedir' => BASEDIR,
'externalReference' => !empty($order['extReference']) ?
"<strong>Ext. Ref.:</strong> ".$order['extReference'] : "",
'isGermanSpeaking' => $isGermanSpeaking, // Required for conditional logic
'isDefaultAddress' => $isDefaultAddress, // Needed for address display rules
'distributor' => $distributor,
'countryText' => $countryText,
'order' => $order, // Full order data access
'orderNumber' => $order['orderNumber'],
'bank_iban' => TT_INVOICE_BANK_IBAN_FORMATTED, // Direct footer access
'bank_bic' => TT_INVOICE_BANK_BIC,
'bank_bank' => TT_INVOICE_BANK_BANK,
'bank_owner' => TT_INVOICE_BANK_OWNER
];
// put header and footer into temp files
$headerFile = BASEDIR . "/var/temp/order_header-" . date("U") . "-" . rand(1000, 9999) . ".html";
file_put_contents($headerFile, $headerHtml);
$footerHtml = file_get_contents(BASEDIR . "/Layout/default/WarehouseOrder/PDF_FOOTER.html");
$footerHtml = str_replace("{{ bank_iban }}", TT_INVOICE_BANK_IBAN_FORMATTED, $footerHtml);
$footerHtml = str_replace("{{ bank_bic }}", TT_INVOICE_BANK_BIC, $footerHtml);
$footerHtml = str_replace("{{ bank_bank }}", TT_INVOICE_BANK_BANK, $footerHtml);
$footerHtml = str_replace("{{ bank_owner }}", TT_INVOICE_BANK_OWNER, $footerHtml);
file_put_contents($headerFile, $this->generateTemplate('PDF_HEADER', $replacements));
$footerFile = BASEDIR . "/var/temp/order_footer-" . date("U") . "-" . rand(1000, 9999) . ".html";
file_put_contents($footerFile, $footerHtml);
file_put_contents($footerFile, $this->generateTemplate('PDF_FOOTER', $replacements));
// Generate PDF
$pdf = new PdfForm("WarehouseOrder/PDF_MAIN", $pdfVars);
$filename = $pdf->render("--header-html $headerFile --footer-html $footerFile");
$pdf = new PdfForm("WarehouseOrder/PDF_MAIN", $pdf_vars);
$wkhtmltopdfArgs = "--header-html $headerFile --footer-html $footerFile";
$filename = $pdf->render($wkhtmltopdfArgs);
if ($returnFilename === true) return $filename;
// return the pdf and die so the client sees the pdf not the filename
// Output PDF
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Disposition: inline; filename="' . basename($filename) . '"');
readfile($filename);
exit;
}
protected function sendEmailAction() {
$pdfFilename = $this->createPDFAction(true);
// Get order details for email content
$order = (array) WarehouseOrderModel::get($this->request->id);
$orderNumber = $order['orderNumber'];
$order['positions'] = json_decode($order['positions'], true);
$distributorName = WarehouseDistributorModel::get($order['distributorId'])->name;
// 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($this->request->email, $this->request->email);
// $mail->addCC('cc@example.com'); // Uncomment for CC
// Content
$mail->isHTML(true);
$mail->Subject = "Neue Bestellung #$orderNumber";
$mail->Body = "<!DOCTYPE html>
<html>
<head>
<title>XINON E-Mail Template</title>
<meta charset='utf-8'/>
</head>
<body>
<h2>Neue Bestellung</h2>
<p>
Bestellnummer: <strong>$orderNumber</strong><br>
Datum: " . date('d.m.Y H:i', $order['create']) . "<br>
Lieferant: <strong>$distributorName</strong>
</p>
<p>Die Bestellung liegt als PDF-Anhang bei.</p>
</body>
</html>";
// Attach PDF
$mail->addAttachment($pdfFilename, "Bestellung_$orderNumber.pdf");
if ($order['sendShippingNote'] > 0) {
$shippingNoteController = new WarehouseShippingNoteController();
$shippingNoteFilename = $shippingNoteController->createPDFAction(true, $this->request->shippingNote);
$mail->addAttachment($shippingNoteFilename, "Lieferschein_$orderNumber.pdf");
}
$mail->send();
$log = [
"table" => "WarehouseOrder",
"rowId" => $this->request->id,
"type" => "noChanges",
"message" => "Bestellung per Mail versendet",
"createBy" => intval($this->user->id),
"create" => time()
];
WarehouseLogModel::create($log);
// Cleanup temporary files
if (file_exists($pdfFilename)) {
unlink($pdfFilename);
}
self::returnJson(['success' => true, 'message' => 'E-Mail erfolgreich versendet']);
exit;
} catch (Exception $e) {
http_response_code(500);
self::returnJson([
'success' => false,
'message' => "E-Mail konnte nicht versendet werden: {$mail->ErrorInfo}",
]);
exit;
}
}
private function generateTemplate(string $type, array $data): string {
$template = file_get_contents(BASEDIR . "/Layout/default/WarehouseOrder/{$type}.html");
$replacements = [
'{{ basedir }}' => BASEDIR,
'{{ externalReference }}' => !empty($data['order']['extReference']) ?
"<strong>Ext. Ref.:</strong> ".$data['order']['extReference'] : "",
'{{ addressLine_header }}' => $data['isGermanSpeaking'] ? 'Lieferant' : 'Supplier',
'{{ addressLine_1 }}' => $data['distributor']->name,
'{{ addressLine_2 }}' => $data['distributor']->address,
'{{ addressLine_3 }}' => $data['distributor']->plz . ' ' . $data['distributor']->city,
'{{ addressLine_4 }}' => $data['countryText'],
'{{ billingAddressLine_header }}' => $this->getBillingHeader($data),
'{{ billingAddressLine_1 }}' => 'Xinon GmbH',
'{{ billingAddressLine_2 }}' => 'Fladnitz im Raabtal 150',
'{{ billingAddressLine_3 }}' => 'A-8322 Studenzen',
'{{ billingAddressLine_4 }}' => 'UID: ATU68711968',
'{{ billingAddressLine_5 }}' => 'EORI-Nr.: ATEOS1000085074',
'{{ billingAddressLine_6 }}' => "<strong>Referenz: ".$data['order']['orderNumber']."</strong>",
'{{ shippingAddressLine_header }}' => $data['isDefaultAddress'] ? '' :
($data['isGermanSpeaking'] ? 'Lieferadresse' : 'Shipping Address'),
'{{ shippingAddressLine_1 }}' => $data['isDefaultAddress'] ? '' : $data['order']['delAddrName'],
'{{ shippingAddressLine_2 }}' => $data['isDefaultAddress'] ? '' : $data['order']['delAddrLine'],
'{{ shippingAddressLine_3 }}' => $data['isDefaultAddress'] ? '' :
$data['order']['delAddrPLZ'] . ' ' . $data['order']['delAddrCity'],
'{{ shippingAddressLine_4 }}' => $data['isDefaultAddress'] ? '' : $data['order']['delAddrEMail'],
'{{ bank_iban }}' => TT_INVOICE_BANK_IBAN_FORMATTED,
'{{ bank_bic }}' => TT_INVOICE_BANK_BIC,
'{{ bank_bank }}' => TT_INVOICE_BANK_BANK,
'{{ bank_owner }}' => TT_INVOICE_BANK_OWNER
];
return strtr($template, $replacements);
}
private function getBillingHeader(array $data): string {
$base = $data['isGermanSpeaking'] ? 'Rechnungsadresse' : 'Billing Address';
return $data['isDefaultAddress'] ? $base . ' / ' .
($data['isGermanSpeaking'] ? 'Lieferadresse' : 'Shipping Address'):$base;
}
protected function getLogAction() {
$orderId = $this->request->orderId;
if (empty($orderId)) {
@@ -198,7 +323,7 @@ class WarehouseOrderController extends TTCrud {
];
try {
$order = WarehouseOrderModel::get($log['orderId']);
$order = WarehouseOrderModel::get($log['rowId']);
if ($postData['status'] !== 'noChanges') {
$oldStatusText = array_values(array_filter($this->columns, fn($c) => $c['key'] === 'status'))[0]['modal']['items'][array_search($order->status, array_column(array_values(array_filter($this->columns, fn($c) => $c['key'] === 'status'))[0]['modal']['items'], 'value'))]['text'];
$newStatusText = array_values(array_filter($this->columns, fn($c) => $c['key'] === 'status'))[0]['modal']['items'][array_search($postData['status'], array_column(array_values(array_filter($this->columns, fn($c) => $c['key'] === 'status'))[0]['modal']['items'], 'value'))]['text'];
@@ -33,6 +33,7 @@ class WarehouseOrderModel extends TTCrudBaseModel {
public int $editor;
public ?string $note;
public string $positions;
public ?int $sendShippingNote;
public int $create;
public int $createBy;
}
@@ -8,6 +8,7 @@ class WarehouseOrderRequestController extends TTCrud {
//@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', 'items' => '/Address/Api?do=findAddress', 'type' => 'autocomplete']],
['key' => 'purpose', 'text' => 'Verwendungszweck', 'required' => true],
['key' => 'positions', 'text' => 'Positionen', 'required' => true, 'modal' => ['type' => 'positions-manager', 'config' => [
'header' => 'Positionen',
@@ -35,11 +36,16 @@ class WarehouseOrderRequestController extends TTCrud {
['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-check text-success'],
['value' => 1, 'text' => 'Bestellwunsch erledigt', 'icon' => 'fa-regular fa-circle-xmark text-danger']]], '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'],
@@ -59,37 +65,6 @@ class WarehouseOrderRequestController extends TTCrud {
return true;
}
protected function afterCreate($postData): void {
return;
//TODO: this should be working again
if ($_SERVER['HTTP_HOST'] === 'localhost') return;
die("TODO we need this to work with new positions manager");
$email = new Emailnotification();
$postData['ware'] = is_numeric($postData['ware']) ? WarehouseArticleModel::get((int) $postData['ware'])->title : $postData['ware'];
$paddedId = str_pad($postData['id'], 5, '0', STR_PAD_LEFT);
$email->setSubject("TheTool: Neue Interne Bestellung #$paddedId")
->setBody(<<<BODY
Hallo,
es wurde eine neue interne Bestellung erstellt.
Bestellnummer: #$paddedId
Ware: {$postData['ware']}
Anzahl: {$postData['anzahl']}
Verwendungszweck: {$postData['verwendungszweck']}
Beauftragt von: {$this->user->name}
Beauftragt am: {date('d.m.Y H:i')}
Notiz: {$postData['note']}
BODY
)
->setFrom(TT_OUTGOING_EMAIL_2FA, TT_OUTGOING_EMAIL_2FA)
->setTo("einkauf@xinon.at", "Einkauf")
->send();
}
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]]);
@@ -101,6 +76,17 @@ BODY
self::returnJson(['success' => true]);
}
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 (!(WarehouseOrderRequestModel::get($id))) self::returnJson(['error' => 'Bestellwunsch nicht gefunden']);
WarehouseOrderRequestModel::update(['id' => $id, 'done' => $done]);
self::returnJson(['success' => true]);
}
protected function createNewLogAction() {
$postData = json_decode(file_get_contents('php://input'), true);
@@ -2,11 +2,13 @@
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;
}
@@ -223,8 +223,8 @@ class WarehouseShippingNoteController extends TTCrud {
}
}
protected function createPDFAction() {
$id = $this->request->id;
public function createPDFAction($returnFilename = false, $idOverride = null) {
$id = $idOverride ?? $this->request->id;
if (strlen($id) < 1) {
http_response_code(500);
self::returnJson(['success' => false, 'message' => 'Lieferschein wurde nicht gefunden']);
@@ -376,10 +376,13 @@ class WarehouseShippingNoteController extends TTCrud {
$wkhtmltopdfArgs = "--header-html $headerFile --footer-html $footerFile";
$filename = $pdf->render($wkhtmltopdfArgs);
if ($returnFilename === true) return $filename;
// return the pdf and die so the client sees the pdf not the filename
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
readfile($filename);
die();
}
// TODO: either move this to UserController or make it better