137 lines
6.2 KiB
PHP
137 lines
6.2 KiB
PHP
<?php
|
|
|
|
class WarehouseOfferController extends TTCrud {
|
|
protected string $headerTitle = 'Angebote';
|
|
protected string $singleText = 'Angebot';
|
|
protected bool $createText = false;
|
|
|
|
protected array $columns = [
|
|
['key' => 'id', 'text' => 'ID', 'modal' => false, 'table' => false],
|
|
['key' => 'offerNumber', 'text' => 'Angebotsnummer', 'required' => true, 'modal' => false],
|
|
['key' => 'customerNumber', 'text' => 'Kundennummer', 'required' => true, 'modal' => false],
|
|
['key' => 'customerName', 'text' => 'Kundenname', 'required' => true, 'modal' => false],
|
|
['key' => 'customerCity', 'text' => 'Stadt', 'required' => true, 'modal' => false],
|
|
['key' => 'customerVAT', 'text' => 'UID', 'required' => true, 'modal' => false],
|
|
['key' => 'editor', 'text' => 'Sachbearbeiter', 'required' => true, 'modal' => ['type' => 'select'], 'table' => ['filter' => 'select']],
|
|
['key' => 'totalAmount', 'text' => 'Gesamtbetrag', 'required' => true, 'modal' => false],
|
|
['key' => 'status', 'text' => 'Status', 'required' => true],
|
|
['key' => 'create', 'text' => 'Erstellt', 'required' => true, 'modal' => false],
|
|
['key' => 'createBy', 'text' => 'Erstellt von', 'required' => true, 'modal' => ['type' => 'select'], 'table' => ['filter' => 'select']],
|
|
['key' => 'actions',
|
|
'text' => 'Aktionen',
|
|
'required' => false,
|
|
'modal' => false,
|
|
'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center']],
|
|
];
|
|
protected array $additionalJSVariables = ['WAREHOUSE_ADMIN' => true];
|
|
protected array $permissionCheck = ['WarehouseAdmin'];
|
|
protected array $additionalActions = [['key' => 'openpdf', 'title' => 'PDF öffnen', 'class' => 'fas fa-file-pdf', 'color' => 'primary']];
|
|
|
|
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]));
|
|
if (!$this->user->can('WarehouseAdmin')) $this->additionalJSVariables['WAREHOUSE_ADMIN'] = false;
|
|
}
|
|
|
|
protected function beforeCreate(): bool {
|
|
$currentCount = WarehouseOfferModel::count(['create' => ['from' => strtotime(date('Y-01-01'))]]);
|
|
$this->postData['offerNumber'] = 'AN' . date('Y') . '-' . str_pad($currentCount + 1, 4, '0', STR_PAD_LEFT);
|
|
$this->postData['status'] = 'new';
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function beforeUpdate($postData): bool {
|
|
(new WarehouseHistoryController)->create($postData, $this->mod);
|
|
return true;
|
|
}
|
|
|
|
protected function getHistoryAction() {
|
|
self::returnJson((new WarehouseHistoryController)->getHistory($this->request->id, $this->mod, $this->columns));
|
|
}
|
|
|
|
protected function createTemplateAction() {
|
|
if (!$this->user->can('WarehouseAdmin')) self::sendError("Keine Berechtigung");
|
|
$_POST = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$templateId = WarehouseOfferTemplateModel::create([
|
|
'templateName' => $_POST['name'],
|
|
'positions' => $_POST['positions'],
|
|
'totalDiscount' => $_POST['totalDiscount'],
|
|
'paymentTerms' => $_POST['paymentTerms'],
|
|
'deliveryTerms' => $_POST['deliveryTerms'],
|
|
'closingText' => $_POST['closingText'],
|
|
'notes' => $_POST['notes'],
|
|
]);
|
|
|
|
self::returnJson(['success' => true, 'id' => $templateId]);
|
|
}
|
|
|
|
protected function deleteTemplateAction() {
|
|
if (!$this->user->can('WarehouseAdmin')) self::sendError("Keine Berechtigung");
|
|
WarehouseOfferTemplateModel::delete($this->request->id);
|
|
self::returnJson(['success' => true]);
|
|
}
|
|
|
|
protected function getTemplatesAction() {
|
|
self::returnJson(WarehouseOfferTemplateModel::getAll());
|
|
}
|
|
|
|
public function createPDFAction($returnFilename = false, $idOverride = null) {
|
|
//display errors
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
$id = $idOverride ?? $this->request->id;
|
|
if (strlen($id) < 1) self::sendError('ID fehlt');
|
|
|
|
$offer = WarehouseOfferModel::get($id);
|
|
if (!$offer->id) self::sendError('Angebot nicht gefunden');
|
|
|
|
$positions = json_decode($offer->positions, true);
|
|
$entries = [];
|
|
|
|
foreach ($positions as $position) {
|
|
if (!isset($position['article'])) continue;
|
|
$article = WarehouseArticleModel::get($position['article']);
|
|
$position['articleText'] = WarehouseArticleModel::get($position['article'])->title;
|
|
$position['articleDescription'] = $article->description === $article->title ? "" : $article->description;
|
|
$position['articleUnit'] = $position['unit'] ?? $article->unit ?? 'Stk.';
|
|
|
|
if (isset($position['_group'])) {
|
|
$entries[$position['_group']][] = $position;
|
|
} else {
|
|
$entries[''][] = $position;
|
|
}
|
|
}
|
|
|
|
$pdf_vars = [
|
|
"offer" => $offer,
|
|
"entries" => $entries,
|
|
"bank_iban" => TT_INVOICE_BANK_IBAN,
|
|
"bank_bic" => TT_INVOICE_BANK_BIC,
|
|
"bank_bank" => TT_INVOICE_BANK_BANK,
|
|
"bank_owner" => TT_INVOICE_BANK_OWNER
|
|
];
|
|
|
|
$headerHtml = file_get_contents(BASEDIR . "/Layout/default/WarehouseOffer/PDF_HEADER.html");
|
|
$headerHtml = str_replace("{{ addressLine_header }}",
|
|
// {{ addressLine_1 }} {{ addressLine_2 }} {{ addressLine_3 }} {{ addressLine_4 }} {{ externalReference }}
|
|
$headerHtml = str_replace("{{ addressLine_1 }}", $offer->customerName, $headerHtml));
|
|
$headerHtml = str_replace("{{ addressLine_2 }}", $offer->customerStreet, $headerHtml);
|
|
$headerHtml = str_replace("{{ addressLine_3 }}", $offer->customerZip . ' ' . $offer->customerCity, $headerHtml);
|
|
$headerHtml = str_replace("{{ addressLine_5 }}", $offer->customerVAT, $headerHtml);
|
|
$headerHtml = str_replace("{{ externalReference }}", $offer->customerReference, $headerHtml);
|
|
|
|
|
|
|
|
|
|
exit;
|
|
}
|
|
|
|
|
|
}
|