159 lines
9.8 KiB
PHP
159 lines
9.8 KiB
PHP
<?php
|
|
|
|
class WarehouseOrderController extends TTCrud {
|
|
protected string $headerTitle = 'Lieferantenbestellungen';
|
|
protected bool $createText = false;
|
|
protected array $permissionCheck = ['WarehouseAdmin'];
|
|
|
|
//@formatter:off
|
|
protected array $columns = [
|
|
['key' => 'id', 'text' => 'ID', 'modal' => false, 'table' => false],
|
|
['key' => 'orderNumber', 'text' => 'Bestellnummer', 'required' => true, 'modal' => false],
|
|
['key' => 'distributor', 'text' => 'Lieferant', 'required' => false, 'modal' => false, 'table' => ['filter' => false]],
|
|
['key' => 'delAddrCity', 'text' => 'Stadt', 'required' => true, 'modal' => false, 'table' => false],
|
|
['key' => 'delAddrEMail', 'text' => 'E-Mail', 'required' => true, 'modal' => false, 'table' => false],
|
|
['key' => 'delAddrLine', 'text' => 'Adresse', 'required' => true, 'modal' => false, 'table' => false],
|
|
['key' => 'delAddrName', 'text' => 'Name', 'required' => true, 'modal' => false, 'table' => false],
|
|
['key' => 'delAddrPLZ', 'text' => 'PLZ', 'required' => true, 'modal' => false, 'table' => false],
|
|
['key' => 'editor', 'text' => 'Bearbeiter', 'required' => true, 'modal' => ['type' => 'select'], 'table' => ['filter' => 'select']],
|
|
['key' => 'note', 'text' => 'Notiz', 'required' => true, 'modal' => false, 'table' => false],
|
|
['key' => 'sum', 'text' => 'Summe', 'required' => false, 'modal' => false, 'table' => ['class' => 'text-right']],
|
|
['key' => 'status', 'text' => 'Status', 'required' => false, 'modal' => ['type' => 'select', 'items' => []], 'table' => ['filter' => 'select']],
|
|
['key' => 'positions', 'text' => 'Positionen', 'required' => true, 'modal' => false, 'table' => false],
|
|
['key' => 'extReference', 'text' => 'Externe Referenz', 'required' => true, 'modal' => false],
|
|
['key' => 'createBy', 'text' => 'Erstellt von', 'required' => true, 'modal' => ['type' => 'select'], 'table' => ['filter' => 'select']],
|
|
['key' => 'create', 'text' => 'Erstellt', 'required' => true, 'modal' => false],
|
|
['key' => 'actions', 'text' => 'Aktionen', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center']],
|
|
];
|
|
//@formatter:on
|
|
|
|
protected array $infoMessages = ['create' => 'Bestellung wurde erfolgreich erstellt.',
|
|
'update' => 'Bestellung wurde aktualisiert.',
|
|
'delete' => 'Bestellung wurde gelöscht',
|
|
'noChanges' => 'Keine Änderungen',];
|
|
|
|
protected function prepareCrudConfig(): void {
|
|
$editorColumnIndex = array_search('editor', array_column($this->columns, 'key'));
|
|
$this->columns[$editorColumnIndex]['modal']['items'] = array_map(function ($user) {
|
|
return ['value' => intval($user->id), 'text' => $user->name];
|
|
}, UserModel::search(['employee' => true]));
|
|
|
|
$statusIndex = array_search('status', array_column($this->columns, 'key'));
|
|
$this->columns[$statusIndex]['modal']['items'] = [
|
|
['value' => 'new', 'text' => 'Neu'],
|
|
['value' => 'accepted', 'text' => 'Akzeptiert'],
|
|
['value' => 'ordered', 'text' => 'Bestellt'],
|
|
['value' => 'sent', 'text' => 'Versendet'],
|
|
['value' => 'partiallyDelivered', 'text' => 'Teilweise geliefert'],
|
|
['value' => 'fullyDelivered', 'text' => 'Geliefert'],
|
|
['value' => 'cancelled', 'text' => 'Storniert'],
|
|
];
|
|
}
|
|
|
|
protected function beforeCreate(): bool {
|
|
$this->postData['orderNumber'] = 'PO' . date('Y') . '-' . str_pad(WarehouseOrderModel::count(['create' => ['from' => strtotime(date('Y-01-01'))]]) + 1, 4, '0', STR_PAD_LEFT);
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function getArticleDistributorDataAction() {
|
|
$articleId = $this->request->articleId;
|
|
if ($this->request->allDistributor === 'true') self::returnJson(array_map(fn($d) => ['id' => $d->id,
|
|
'name' => $d->name], WarehouseDistributorModel::getAll()));
|
|
else if (!empty($articleId)) self::returnJson(array_map(fn($d) => ['id' => $d->distributorId,
|
|
'name' => WarehouseDistributorModel::get($d->distributorId)->name,
|
|
'purchasePrice' => $d->purchasePrice,
|
|
'externalArticleNumber' => $d->externalArticleNumber], WarehouseArticleDistributorModel::getAll(['articleId' => $articleId])));
|
|
else self::returnJson([]);
|
|
}
|
|
|
|
protected function getByIdParse(array $order): array {
|
|
$order['positions'] = json_decode($order['positions'], true);
|
|
|
|
foreach ($order['positions'] as &$position) {
|
|
$position['distributorName'] = WarehouseDistributorModel::get($position['distributorId'])->name;
|
|
$position['articleName'] = WarehouseArticleModel::get($position['article'])->title;
|
|
}
|
|
|
|
return $order;
|
|
}
|
|
|
|
protected function createPDFAction() {
|
|
$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;
|
|
|
|
$order['positions'][$key] = $position;
|
|
}
|
|
|
|
$pdf_vars = ['order' => $order,
|
|
'distributor' => WarehouseDistributorModel::get($distributorId),
|
|
"bank_iban" => TT_INVOICE_BANK_IBAN,
|
|
"bank_bic" => TT_INVOICE_BANK_BIC,
|
|
"bank_bank" => TT_INVOICE_BANK_BANK,
|
|
"bank_owner" => TT_INVOICE_BANK_OWNER];
|
|
|
|
|
|
$countryText = CountryModel::search(['id' => WarehouseDistributorModel::get($distributorId)->countryId])[0]->name;
|
|
|
|
$headerHtml = file_get_contents(BASEDIR . "/Layout/default/WarehouseOrder/PDF_HEADER.html");
|
|
$headerHtml = str_replace("{{ basedir }}", BASEDIR, $headerHtml);
|
|
$headerHtml = str_replace("{{ externalReference }}","<strong>Ihre Referenz:</strong> ". $order['extReference'], $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);
|
|
|
|
$headerHtml = str_replace("{{ billingAddressLine_1 }}", "Xinon GmbH", $headerHtml);
|
|
$headerHtml = str_replace("{{ billingAddressLine_2 }}", "Fladnitz im Raabtal 150", $headerHtml);
|
|
$headerHtml = str_replace("{{ billingAddressLine_3 }}", "8322 Studenzen", $headerHtml);
|
|
$headerHtml = str_replace("{{ billingAddressLine_4 }}", "Österreich", $headerHtml);
|
|
$headerHtml = str_replace("{{ billingAddressLine_5 }}", "einkauf@xinon.at", $headerHtml);
|
|
$headerHtml = str_replace("{{ billingAddressLine_6 }}", "<strong>Referenz: ". $order["orderNumber"] . "</strong>", $headerHtml);
|
|
|
|
// if order dellAddrLine is Fladnitz im Raabtal 150 we need to set all template strings to empty
|
|
|
|
$chk = $order['delAddrLine'] == "Fladnitz im Raabtal 150";
|
|
|
|
$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);
|
|
|
|
|
|
$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);
|
|
|
|
$footerFile = BASEDIR . "/var/temp/order_footer-" . date("U") . "-" . rand(1000, 9999) . ".html";
|
|
file_put_contents($footerFile, $footerHtml);
|
|
|
|
|
|
$pdf = new PdfForm("WarehouseOrder/PDF_MAIN", $pdf_vars);
|
|
$wkhtmltopdfArgs = "--header-html $headerFile --footer-html $footerFile";
|
|
$filename = $pdf->render($wkhtmltopdfArgs);
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
} |