Merge branch 'master' into fronkdev

This commit is contained in:
Frank Schubert
2025-07-29 13:35:31 +02:00
62 changed files with 3160 additions and 1517 deletions

View File

@@ -247,6 +247,9 @@ class ConstructionConsent extends mfBaseModel {
}
foreach($owners as $owner) {
if($owner->result == "open" && $owner->status == "new") {
$owner->result = "new";
}
if(!array_key_exists($owner->result, $counts)) {
$counts[$owner->result] = 0;
}
@@ -425,6 +428,10 @@ class ConstructionConsent extends mfBaseModel {
COUNT(cwo.id) AS total_owners,
CASE
WHEN ConstructionConsent.approve_override = 1 THEN 'green'
WHEN COALESCE(SUM(CASE
WHEN approve_override = 1 THEN 0
ELSE (cwo.result = 'open' AND cwo.status = 'new')
END), 0) > 0 THEN 'blue'
WHEN COALESCE(SUM(CASE
WHEN approve_override = 1 THEN 0
ELSE (cwo.result = 'denied')
@@ -435,7 +442,7 @@ class ConstructionConsent extends mfBaseModel {
END), 0) > 0
OR COALESCE(SUM(CASE
WHEN approve_override = 1 THEN 0
ELSE (cwo.result = 'open')
ELSE (cwo.result = 'open' AND cwo.status != 'new')
END), 0) > 0
OR COALESCE(SUM(CASE
WHEN approve_override = 1 THEN 0

View File

@@ -1112,6 +1112,22 @@ class ConstructionConsentController extends mfBaseController {
$projectId = $this->request->project_id;
$importData = json_decode(file_get_contents('php://input'), true);
// save full post request and project id to file as log with metadata like user id etc as json file
// MFUPLOAD_FILE_SAVE_PATH . /ConstructionConsentImports
$logData = [
"user_id" => $this->me->id,
"project_id" => $projectId,
"import_data" => $importData,
"timestamp" => date("Y-m-d H:i:s")
];
$logFileName = MFUPLOAD_FILE_SAVE_PATH . "/ConstructionConsentImports/import_" . date("Ymd_His") . "_user_{$this->me->id}_project_{$projectId}.json";
if (!file_exists(dirname($logFileName))) {
mkdir(dirname($logFileName), 0777, true);
}
file_put_contents($logFileName, json_encode($logData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
if (empty($importData) || !is_array($importData)) { $this->layout()->setFlash("Keine Daten gefunden", "error"); return $this->redirect("ConstructionConsent"); }
if (!is_numeric($projectId) || $projectId < 1) { $this->layout()->setFlash("Projekt nicht gefunden", "error"); return $this->redirect("ConstructionConsent"); }
if (!($consentProject = new ConstructionConsentProject($projectId))->id) { $this->layout()->setFlash("Projekt nicht gefunden", "error"); return $this->redirect("ConstructionConsent"); }
@@ -1178,7 +1194,10 @@ class ConstructionConsentController extends mfBaseController {
$journal = ConstructionConsentJournal::create([
"constructionconsent_id" => $consentRecord->id,
"text" => "Import: Eigentümer $firstname $lastname wurde hinzugefügt"
"text" =>
$ownerRecord->company ?
"Import: Eigentümer $ownerRecord->company wurde hinzugefügt" :
"Import: Eigentümer $firstname $lastname wurde hinzugefügt"
]);
$journal->save();

View File

@@ -156,6 +156,15 @@ class ConstructionConsentOwnerController extends mfBaseController
$file->delete();
}
$journal = ConstructionConsentJournal::create([
"constructionconsent_id" => $consent->id,
"text" =>
$owner->company ?
"Eigentümer $owner->company wurde gelöscht" :
"Eigentümer $owner->firstname $owner->lastname wurde gelöscht"
]);
$journal->save();
$owner->delete();
$this->layout()->setFlash("Besitzer gelöscht!", "success");

View File

@@ -5,12 +5,11 @@ class CpeprovisioningController extends mfBaseController
protected function init()
{
$this->needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
$this->me = new User();
$this->me->loadMe();
$this->layout()->set("me", $this->me);
if (!$me->is(["Admin"])) {
if (!$this->me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
@@ -283,11 +282,306 @@ class CpeprovisioningController extends mfBaseController
$query["filter"] = $this->request->filter;
}
$qs = http_build_query($query);
$this->layout()->setFlash("Eintrag erfolgreich gespeichert.", "success");
$this->redirect("Cpeprovisioning", "Index", $qs);
$this->redirect("Cpeprovisioning", "Index", http_build_query($query));
}
}
protected function apiSaveAction() {
try {
$p = json_decode(file_get_contents('php://input'), true);
$id = $p['id'] ?? null;
$mode = $id ? "edit" : "add";
if ($mode === 'edit') {
$cpe = new Cpeprovisioning($id);
if (!$cpe->id) throw new Exception("Eintrag nicht gefunden");
}
if (empty($p['termination_id']) && empty($p['order_id'])) throw new Exception("Anschluss oder Bestellung nicht gefunden");
if (!OrderProductModel::getFirst(["order_id" => $p['order_id'], "termination_id" => $p['termination_id']])) throw new Exception("Anschluss gehört nicht zur Bestellung");
if (!empty($p['ont_sn'])) {
$termination = new Termination($p['termination_id']);
$orig_sn = $termination->getWorkflowvalue("ont_sn", "string");
if ($orig_sn === null) {
if ($sn_item = WorkflowitemModel::getFirst(["name" => "ont_sn", "object_type" => "termination"])) {
$sn_item->setObjectId($p['termination_id']);
$termination->workflowitems["ont_sn"] = $sn_item;
} else {
$this->log->error("ont_sn workflow item not found");
}
}
if ($p['ont_sn'] !== $orig_sn && isset($termination->workflowitems["ont_sn"])) {
$termination->workflowitems["ont_sn"]->value->setValue($p['ont_sn']);
$termination->workflowitems["ont_sn"]->value->save();
}
}
$data = [
"termination_id" => $p['termination_id'] ?: null,
"order_id" => $p['order_id'],
"orderproduct_id" => $p['orderproduct_id'],
"routerconfig_finished" => (int)($p['routerconfig_finished'] ?? 0),
"shipping" => (int)($p['shipping'] ?? 0),
"routertype" => $p['routertype'] ?? null,
"wifi_ssid" => $p['wifi_ssid'] ?? null,
"wifi_pass" => $p['wifi_pass'] ?? null,
"mac" => $p['mac'] ?? null,
"note" => $p['note'] ?? null,
"edit_by" => $this->me->id,
];
foreach (['ship_weight', 'ship_length', 'ship_width', 'ship_height'] as $key) $data[$key] = empty($p[$key]) ? null : $p[$key];
foreach (['public', 'nat', 'ipv6'] as $type) $data["vlan_{$type}"] = !empty($p['vlans'][$type]['checked']) ? ($p['vlans'][$type]['tag'] ?? null) : null;
if ($mode === 'add') {
$data["create_by"] = $this->me->id;
$cpe = CpeprovisioningModel::create($data);
} else $cpe->update($data);
if (!$cpe->save()) throw new Exception("Fehler beim Speichern");
if ($cpe->routerconfig_finished) {
$order_product = new OrderProduct($p['orderproduct_id']);
$shipping_text = $cpe->shipping
? "zum Versand vorbereitet"
: "vorbereitet für Techniker zur Vorortinstallation";
$text = "CPE zu Produkt \"{$order_product->product->name}\" {$shipping_text}.\n\n"
. "Router: {$cpe->routertype}\n"
. "Zugangstyp: {$order_product->product->attributes['bras_type']->value}\n";
if ($cpe->vlan_public) $text .= "Vlan Public: {$cpe->vlan_public}\n";
if ($cpe->vlan_nat) $text .= "Vlan NAT: {$cpe->vlan_nat}\n";
if ($cpe->vlan_ipv6) $text .= "Vlan IPv6: {$cpe->vlan_ipv6}\n";
$journal = new OrderJournal();
$journal->order_id = $p['order_id'];
$journal->text = $text;
$journal->create_by = $this->me->id;
$journal->edit_by = $this->me->id;
if (!($journal_id = $journal->save())) {
$this->layout()->setFlash("Konnte nicht ins Bestelljournal schreiben!", "warning");
} else {
$cpe->order_journal_id = $journal_id;
$cpe->save();
}
}
self::returnJson(['success' => true, 'message' => 'Eintrag erfolgreich gespeichert.']);
} catch (Exception $e) {
http_response_code(400);
self::returnJson(['success' => false, 'message' => $e->getMessage()]);
}
}
protected function apiGetAction()
{
$p = json_decode(file_get_contents('php://input'), true) ?? [];
// --- Pagination and Sorting setup ---
$page = (int)($p['pagination']['page'] ?? 1);
$perPage = (int)($p['pagination']['per_page'] ?? 25);
$orderBy = $p['order']['key'] ?? null;
$orderDir = $p['order']['order'] ?? 'asc';
// Calculate start and end indexes for manual pagination, just like in the old indexAction
$start = ($page - 1) * $perPage;
$end = $start + $perPage;
// --- Data Fetching (same as before) ---
$searchFilter = $this->getPreparedFilter($p['filters'] ?? []);
// if routerconfig_finished === 0 then we can add key add-where to searchFilter and only show orders withing the last 180 days either by create or edit (caution this is unix timestamp)
if (isset($searchFilter['routerconfig_finished']) && !$searchFilter['routerconfig_finished']) {
$searchFilter['add-where'] = "`Order`.create > " . (time() - 365 * 86400) . " OR `Order`.edit > " . (time() - 365 * 86400);
}
$orders = OrderModel::search($searchFilter); // cpeprovisioning_enabled
// Use the same uncached precache from indexAction to get all potential products
// $prefetched = OrderProductModel::precache("`Terminationstatus`.`code` < " . TT_TERMSTATUS_CONNECTED . " AND `ProducttechAttribute`.`name` = 'bras_type'");
$sqlWhere = "`ProducttechAttribute`.`name` = 'bras_type'";
// if filter routerconfig_finished === 0 then we can filter the precache by routerconfig_finished = 0 or is null
if (isset($searchFilter['routerconfig_finished']) && !$searchFilter['routerconfig_finished']) {
// $sqlWhere .= " AND (`Cpeprovisioning`.`routerconfig_finished` = 0 OR `Cpeprovisioning`.`routerconfig_finished` IS NULL)";
}
$prefetched = OrderProductModel::precache($sqlWhere);
$paginatedRows = [];
$totalRows = 0;
$orderInfoCache = []; // Cache for order-level info to avoid repeated calculations
foreach ($orders as $order) {
// --- Apply the same initial filters as the old and new methods ---
if (($searchFilter["hide_delayed_finish"] ?? false) && $order->finish_after && $order->finish_after > strtotime('+31 days')) continue;
if (isset($prefetched['terminations'][$order->id][0]) && !$order->cpeprovisioning_enabled && $prefetched['terminations'][$order->id][0]['statuscode'] < TT_TERMSTATUS_CONNECTED) continue;
if (empty($prefetched[$order->id])) continue;
// Loop through the prefetched raw data, similar to indexAction
foreach ($prefetched[$order->id] as $opData) {
// --- Apply the same product-level filters ---
if (!is_array($opData) || ($opData['routerconfig_finished'] xor ($searchFilter['routerconfig_finished'] ?? false)) || empty($opData['attributes']['bras_type'])) continue;
// This item is a valid candidate for the list.
$totalRows++;
// *** THE CORE PERFORMANCE IMPROVEMENT ***
// If the current item is not on the page we want, skip the expensive processing below.
if ($totalRows <= $start || $totalRows > $end) {
continue;
}
// --- Now, do the expensive processing ONLY for the items on the current page ---
// Calculate and cache order-level info only when it's first needed for a visible item
if (!isset($orderInfoCache[$order->id])) {
$orderInfo = ['vot' => false, 'hw' => [], 'voip' => false];
foreach ($order->products as $prod) {
$attrs = $prod->product->attributes ?? [];
if (empty($attrs) || !is_array($attrs)) continue;
if ($attrs['hw_only']->value ?? false) $orderInfo['hw'][] = (int)$prod->amount . "x " . $prod->product->name;
if ($attrs['addon']->value ?? false) $orderInfo['hw'][] = $prod->product->name;
if ($attrs['voip_chan']->value ?? false) $orderInfo['voip'] = true;
if ($attrs['vot']->value ?? false) $orderInfo['vot'] = true;
}
$orderInfoCache[$order->id] = $orderInfo;
}
$orderInfo = $orderInfoCache[$order->id];
// Hydrate the full model object, but only for this one item
$product = OrderProductModel::getOne($opData['id']);
$term = $product->termination;
$attrs = $product->product->attributes;
$cpe = $product->cpeprovisioning;
$vlanPublicDefault = $term ? $term->getPop()->vlan_public : ($attrs['vlan_default_public']->value ?? null);
$vlanNatDefault = $term ? $term->getPop()->vlan_nat : ($attrs['vlan_default_nat']->value ?? null);
$vlanIpv6Default = $term ? $term->getPop()->vlan_ipv6 : ($attrs['vlan_default_ipv6']->value ?? null);
/** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */
$paginatedRows[] = [
'id' => $product->id, 'order_id' => $product->order_id, 'termination_id' => $product->termination_id, 'orderproduct_id' => $product->id,
'network' => $term->building->network->name ?? "{$order->owner->zip} {$order->owner->city}",
'spin' => $order->owner->spin, 'customer' => $order->owner->getCompanyOrName(),
'product_name' => $product->product->name, 'product_code' => $term->code ?? '',
'access_type' => $attrs['bras_type']->value,
'access_type_down' => $attrs["bw_down"]->value,
'access_type_up' => $attrs["bw_up"]->value,
'ont_deployed' => $term ? $term->getWorkflowValue("ont_deployed", "int") : 0,
'ont_sn' => $term ? $term->getWorkflowValue("ont_sn", "string") : null,
'vot' => $orderInfo['vot'] || $order->install_date,
'hw' => !empty($orderInfo['hw']) ? implode("<br />", $orderInfo['hw']) : null,
'voip' => $orderInfo['voip'],
'note' => $order->note,
'show_snopp_button' => ($attrs['hostnetwork_order']->value ?? 0) == 1 && !str_contains($product->product->name, 'XDSL'),
'snopp_url' => 'https://snopp.breitband-steiermark.at/Termination/index?filter[status][]=connected&filter[address]=' . urlencode($order->owner->street),
'vlans' => [
'public' => ['tag' => ($cpe ? $cpe->vlan_public : null) ?? $vlanPublicDefault, 'checked' => ($cpe ? $cpe->vlan_public : null)],
'nat' => ['tag' => ($cpe ? $cpe->vlan_nat : null) ?? $vlanNatDefault, 'checked' => ($cpe ? $cpe->vlan_nat : null)],
'ipv6' => ['tag' => ($cpe ? $cpe->vlan_ipv6 : null) ?? $vlanIpv6Default, 'checked' => ($cpe ? $cpe->vlan_ipv6 : null)],
],
'cpe_id' => $cpe->id ?? null,
'cpe_data' => $this->fixCpeData($cpe->data ?? null),
];
}
}
// Sort the final (small) array of results for the current page
if ($orderBy) {
usort($paginatedRows, fn($a, $b) => ($orderDir === 'asc' ? 1 : -1) * strnatcasecmp($a[$orderBy] ?? '', $b[$orderBy] ?? ''));
}
// No need for array_slice, we already built the paginated list manually
self::returnJson([
'rows' => $paginatedRows,
'pagination' => [
'page' => $page, 'per_page' => $perPage, 'total_rows' => $totalRows,
'filtered_available' => $totalRows, 'total_pages' => ceil($totalRows / $perPage)
]
]);
}
protected function newIndexAction()
{
$this->layout()->set('additionalJS', ['js/pages/Cpeprovisioning/Cpeprovisioning.js']);
$this->layout()->set('additionalHead', ['<link rel="stylesheet" href="/js/pages/Cpeprovisioning/Cpeprovisioning.css">']);
Helper::renderVue(
$this,
"Cpeprovisioning", // The root Vue component name
"CPE Provisioning", // The page title
[
// Pass API URLs and initial data to the frontend
"CPE_PROV_API_GET_URL" => $this->getUrl("Cpeprovisioning", "apiGet"),
"CPE_PROV_API_SAVE_URL" => $this->getUrl("Cpeprovisioning", "apiSave"),
"CPE_PROV_PRINT_PDF_URL" => $this->getUrl("Cpeprovisioning", "printPDF"),
"ORDER_URL" => $this->getUrl("Order"),
"NETWORKS" => NetworkModel::getAll(),
"ROUTER_OPTIONS" => [
// General Options
['value' => 'eigener Router', 'text' => 'Eigener Router'],
['value' => 'anderes CPE', 'text' => 'Anderes CPE'],
// PPPoE/DHCP Routers
['value' => 'TP-Link Archer C80', 'text' => 'TP-Link Archer C80 (Inet, IPTV)'],
['value' => 'FritzBox 4040', 'text' => 'FritzBox 4040 (Inet, IPTV)'],
['value' => 'FritzBox 4050', 'text' => 'FritzBox 4050 (Inet, Phone IPTV)'],
['value' => 'FritzBox 5530', 'text' => 'FritzBox 5530 (Inet FiberP2P, Phone, IPTV)'],
['value' => 'FritzBox 7530', 'text' => 'FritzBox 7530 (Inet, Phone, IPTV)'],
['value' => 'FritzBox 7590', 'text' => 'FritzBox 7590 (Inet, Phone, IPTV)'],
['value' => 'FritzBox 7690', 'text' => 'FritzBox 7690 (Inet, Phone, IPTV)'],
// Static Routers
['value' => 'Mikrotik HAP AC', 'text' => 'Mikrotik HAP AC (Inet, IPTV)'],
['value' => 'Mikrotik HEX S', 'text' => 'Mikrotik HEX S (Inet, IPTV)'],
['value' => 'Mikrotik RB3011', 'text' => 'Mikrotik RB3011 (Inet, IPTV)'],
// CMTS Routers
['value' => 'FritzBox 6490 Cable', 'text' => 'FritzBox 6490 Cable (Inet, Phone, IPTV)'],
],
"ROUTER_SHIPPING_DATA" => [
"TP-Link Archer C80" => ["weight" => 1, "length" => 35, "width" => 24, "height" => 8],
"FritzBox 4040" => ["weight" => 1, "length" => 30, "width" => 24, "height" => 7],
"FritzBox 7530" => ["weight" => 1, "length" => 26, "width" => 19, "height" => 7],
"FritzBox 7590" => ["weight" => 1, "length" => 30, "width" => 24, "height" => 7],
"FritzBox 6490 Cable" => ["weight" => 1, "length" => 30, "width" => 26, "height" => 8]
]
]
);
}
private function fixCpeData($data) {
if (!$data) return [];
$data->shipping = (bool)$data->shipping;
$data->routerconfig_finished = (bool)$data->routerconfig_finished;
return $data;
}
protected function printPDFAction() {
$order_id = $this->request->order_id;
$order = OrderModel::getOne($order_id);
if (!$order) self::sendError("Order not found", 404);
$pdf_vars = [
'firstline' => $order->owner->getCompanyOrName(),
'secondline' => $order->owner->street,
'thirdline' => $order->owner->zip . " " . $order->owner->city,
'fourthline' => $order->owner->customer_number
];
$pdf = new PdfForm("Cpeprovisioning/PDF_MAIN", $pdf_vars);
$wkhtmltopdfArgs = "--page-height 32.5mm --page-width 57.5mm --margin-top 1mm --margin-bottom 0 --margin-left 0 --margin-right 0 --disable-smart-shrinking --encoding utf-8";
$filename = $pdf->render($wkhtmltopdfArgs);
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
readfile($filename);
die();
}
}

View File

@@ -92,27 +92,24 @@ class FileController extends mfBaseController {
$id = $this->request->id;
$size = $this->request->size;
if (!is_numeric($id) || $id < 1) {
http_response_code(400);
self::returnJson(["error" => "Invalid File ID"]);
return;
}
if (!is_numeric($id) || $id < 1) self::sendError("Invalid File ID");
$file = new File($id);
if (!$file->id) {
http_response_code(404);
self::returnJson(["error" => "File record not found"]);
return;
}
if (!$file->id) self::sendError("File record not found");
$originalPath = MFUPLOAD_FILE_SAVE_PATH . ($file->subfolder ? "/{$file->subfolder}" : "") . "/{$file->store_filename}";
if (!is_readable($originalPath)) {
http_response_code(404);
self::returnJson(["error" => "Physical file not found"]);
return;
}
if (!is_readable($originalPath)) self::sendError("Physical file not found");
$imageInfo = @getimagesize($originalPath);
if ($imageInfo === false && mime_content_type($originalPath) === 'application/pdf') {
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . ($file->orig_filename ?: $file->store_filename) . '"');
readfile($originalPath);
exit;
}
if ($imageInfo === false) {
$this->downloadAction();
return;
@@ -129,18 +126,13 @@ class FileController extends mfBaseController {
$cacheDir = TEMP_DIR . "/thumbnails";
@mkdir($cacheDir, 0775, true);
$cachedPath = "{$cacheDir}/{$id}_{$size}." . pathinfo($originalPath, PATHINFO_EXTENSION);
if (!file_exists($cachedPath)) {
$command = "convert " . escapeshellarg($originalPath) . " -resize " . escapeshellarg($sizeDimensions[$size]) . " " . escapeshellarg($cachedPath);
exec($command, $output, $return_var);
if ($return_var !== 0) {
http_response_code(500);
self::returnJson(["error" => "Failed to create thumbnail."]);
return;
}
if ($return_var !== 0) self::sendError("Failed to create thumbnail.");
}
header('Content-Type: ' . $imageInfo['mime']);
@@ -148,5 +140,4 @@ class FileController extends mfBaseController {
readfile($cachedPath);
exit;
}
}

View File

@@ -529,6 +529,13 @@ class OrderModel {
}
}
if(array_key_exists("add-where", $filter)) {
$add_where = $filter['add-where'];
if($add_where) {
$where .= " AND ($add_where)";
}
}
//var_dump($filter, $where);exit;
return $where;

View File

@@ -141,7 +141,7 @@ class OrderProductModel
return $items;
}
public static function precache()
public static function precache($where = false): array
{
$items = [];
$db = FronkDB::singleton();
@@ -162,7 +162,7 @@ class OrderProductModel
";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
$res = $db->query($sql . ($where ? " WHERE $where" : ""));
if ($db->num_rows($res)) {
$oldProduct = "";
$oldOrder = "";

View File

@@ -156,7 +156,7 @@ class PreorderIFrameModel extends mfBaseModel
return [
'oaid' => $row['unit_oaid'] ?? $row['oaid'],
'street' => $row['street'],
'housenumber' => $row['housenumber'],
'housenumber' => $row['hausnummer'],
'hausnummer_id' => $row['hausnummer_id'],
'wohneinheit_id' => $row['wohneinheit_id'],
'building_type' => intval($row['building_type']),
@@ -182,4 +182,4 @@ class PreorderIFrameModel extends mfBaseModel
return $parts ? implode(', ', $parts) : "Top {$counter}";
}
}
}

View File

@@ -1,267 +0,0 @@
<?php
// RMLWorkorderController.php
class RMLWorkorderController extends TTCrud
{
protected string $headerTitle = 'RML Arbeitsaufträge';
protected bool $createText = false; // Workorders are created automatically
protected array $columns = [
['key' => 'id', 'text' => 'Auftrag-Nr.'],
['key' => 'preorderInfo', 'text' => 'Kunde / Projekt', 'modal' => false, 'table' => ['sortable' => false, 'filter' => false]],
['key' => 'companyName', 'text' => 'Zuständige Firma', 'modal' => false, 'table' => ['filter' => 'search']],
['key' => 'status', 'text' => 'Status', 'modal' => ['items' => [
['value' => 'new', 'text' => 'Neu', 'icon' => 'fas fa-star text-primary'],
['value' => 'assigned', 'text' => 'Zugewiesen', 'icon' => 'fas fa-user-check text-info'],
['value' => 'scheduled', 'text' => 'Terminiert', 'icon' => 'fas fa-calendar-check text-warning'],
['value' => 'documented', 'text' => 'Dokumentiert', 'icon' => 'fas fa-file-alt text-success'],
['value' => 'completed', 'text' => 'Abgeschlossen', 'icon' => 'fas fa-check-double text-secondary'],
]], 'table' => ['filter' => 'iconSelect']],
['key' => 'deadlineDate', 'text' => 'Deadline', 'modal' => false, 'table' => ['filter' => 'date']],
['key' => 'appointmentDate', 'text' => 'Termin', 'modal' => false, 'table' => ['filter' => 'date']],
['key' => 'actions', 'text' => 'Aktionen', 'modal' => false, 'table' => ['filter' => false, 'sortable' => false]],
];
protected array $additionalJSVariables = ['RML_ADMIN' => '0', 'COMPANY_ID' => '0'];
protected function prepareCrudConfig() {
// Assume 'RMLAdmin' is a permission.
if ($this->user->can('RMLAdmin')) {
$this->additionalJSVariables['RML_ADMIN'] = '1';
} else {
// If not an admin, find the user's associated company ID
$company = RMLWorkorderCompanyModel::getAll(['addressId' => $this->user->address_id], 1);
if ($company) {
$this->additionalJSVariables['COMPANY_ID'] = $company[0]->id;
} else {
// If user is not an RML admin and not linked to a company, they see nothing.
$this->sendError('Access Denied. You are not associated with a registered RML company.', 403);
}
}
}
protected function getAction()
{
// First, automatically create workorders for any new preorders with status 220.
// In a production environment, this might be a separate cron job.
$this->createWorkordersFromPreorders();
$json = json_decode(file_get_contents('php://input'), true);
$pagination = $json['pagination'] ?? ['page' => 1, 'per_page' => 10];
$filters = $json['filters'] ?? [];
$order = $json['order'] ?? ['key' => 'id', 'order' => 'DESC'];
// If user is a company, filter by their companyId
if ($this->user->can('RMLAdmin') === false) {
$company = RMLWorkorderCompanyModel::getAll(['addressId' => $this->user->address_id], 1);
if($company) {
$filters['companyId'] = $company[0]->id;
}
}
$workorders = RMLWorkorderModel::getAll($filters, $pagination['per_page'], ($pagination['page'] - 1) * $pagination['per_page'], $order);
$totalCount = RMLWorkorderModel::count($filters);
// Enhance rows with data from other tables
$rows = [];
foreach($workorders as $workorder) {
$row = (array)$workorder;
$preorder = new Preorder($workorder->preorderId); // Placeholder for actual Preorder retrieval
$anschlussadresse = '';
if ($preorder->building_id) {
$anschlussadresse = "{$preorder->building->street}<br />{$preorder->building->zip} {$preorder->building->city}";
} elseif ($preorder->adb_hausnummer_id) {
$anschlussadresse = "{$preorder->adb_hausnummer->strasse->name} {$preorder->adb_hausnummer->hausnummer}";
if ($preorder->adb_hausnummer->stiege) {
$anschlussadresse .= "/{$preorder->adb_hausnummer->stiege}";
}
if ($preorder->adb_wohneinheit_id && (string)$preorder->adb_wohneinheit) {
$anschlussadresse .= "<br />{$preorder->adb_wohneinheit}";
}
$anschlussadresse .= "<br />{$preorder->adb_hausnummer->plz->plz} {$preorder->adb_hausnummer->ortschaft->name}";
}
$kunde = ($preorder->company) ? $preorder->company : "{$preorder->firstname} {$preorder->lastname}";
$kunde .= "<br />{$preorder->street}";
if ($preorder->housenumber) {
$kunde .= " {$preorder->housenumber}";
}
$kunde .= "<br />{$preorder->zip} {$preorder->city}";
$kontakt = ($preorder->phone) ? "{$preorder->phone}<br />" : '';
$kontakt .= ($preorder->email) ? $preorder->email : '';
$row['preorderInfo'] = "Anschlussadresse: {$anschlussadresse}<br />" .
"Kunde: {$kunde}<br />" .
"Kontakt: {$kontakt}<br />" .
"OAID: <span class='text-pink'>{$preorder->oaid}</span>";
// Get Company Name
if($workorder->companyId) {
$company = RMLWorkorderCompanyModel::get($workorder->companyId);
$row['companyName'] = $company->name ?? 'N/A';
} else {
$row['companyName'] = 'Nicht zugewiesen';
}
$rows[] = $row;
}
$pagination = [
'page' => $pagination['page'],
'per_page' => $pagination['per_page'],
'filtered_available' => $totalCount,
'total_rows' => $totalCount,
];
self::returnJson([
'rows' => $rows,
'pagination' => $pagination
]);
}
private function createWorkordersFromPreorders() {
// Fetch all active preorders where the status code is 220
$newPreorders = PreorderModel::searchActive(['status_code' => 220]);
// If no new preorders are found, there's nothing to do
if (empty($newPreorders)) {
return;
}
// Iterate through each preorder that needs a workorder
foreach ($newPreorders as $preorder) {
// Check if a workorder for this preorder already exists to prevent duplicates
$existingWorkorder = RMLWorkorderModel::getFirst(['preorderId' => $preorder->id]);
// If no workorder exists, create a new one
if (!$existingWorkorder) {
RMLWorkorderModel::create([
'preorderId' => $preorder->id,
'status' => 'new',
'create' => time(),
'createBy' => $this->user->id // The logged-in user creating the record
]);
}
}
}
protected function assignWorkorderAction() {
if (!$this->user->can('RMLAdmin')) self::sendError("Permission denied.", 403);
$post = json_decode(file_get_contents('php://input'), true);
if (empty($post['workorderId']) || empty($post['companyId'])) {
self::sendError("Required fields are missing.");
}
if (!$rmlWorkorder = RMLWorkorderModel::get($post['workorderId'])) self::sendError("Workorder not found.");
RMLWorkorderModel::update(
array_merge((array) $rmlWorkorder, [
'id' => $post['workorderId'],
'companyId' => $post['companyId'],
'status' => 'assigned',
'assignmentDate' => time(),
'deadlineDate' => strtotime('+6 weeks')
])
);
self::returnJson(['success' => true, 'message' => 'Auftrag erfolgreich zugewiesen.']);
}
protected function scheduleAppointmentAction() {
$post = json_decode(file_get_contents('php://input'), true);
if (empty($post['workorderId']) || empty($post['appointmentDate'])) {
self::sendError("Required fields are missing.");
}
RMLWorkorderModel::update([
'id' => $post['workorderId'],
'appointmentDate' => $post['appointmentDate'],
'status' => 'scheduled'
]);
self::returnJson(['success' => true, 'message' => 'Termin erfolgreich gespeichert.']);
}
protected function uploadDocumentationAction()
{
$file = $_FILES['file'] ?? null;
if (!$file || $file['error'] !== UPLOAD_ERR_OK) {
self::returnJson(['error' => 'File upload failed']);
return;
}
$workorderId = $_POST['workorderId'] ?? null;
$description = $_POST['description'] ?? '';
$documentType = $_POST['documentType'] ?? 'general';
if(!$workorderId) {
self::returnJson(['error' => 'Workorder ID is missing.']);
return;
}
try {
$uploaded = mfUpload::handleFormUpload("file", false, "/RMLWorkorder");
RMLWorkorderDocumentationModel::create([
'workorderId' => $workorderId,
'fileId' => $uploaded->id,
'description' => $description,
'documentType' => $documentType,
'create' => time(),
'createBy' => $this->user->id
]);
// Set status to 'documented' if it was 'scheduled' or 'assigned'
$workorder = RMLWorkorderModel::get($workorderId);
if(in_array($workorder->status, ['assigned', 'scheduled'])) {
RMLWorkorderModel::update(['id' => $workorderId, 'status' => 'documented']);
}
self::returnJson(['success' => true, 'fileId' => $uploaded->id, 'fileName' => $file['name']]);
} catch (Exception $e) {
self::returnJson(['error' => 'Upload error: ' . $e->getMessage()]);
}
}
protected function getDocumentationAction() {
if(empty($this->request->workorderId)) self::sendError("Workorder ID missing.");
$docs = RMLWorkorderDocumentationModel::getAll(['workorderId' => $this->request->workorderId]);
// Enhance with file names
foreach($docs as $doc) {
$file = new File($doc->fileId);
$doc->fileName = $file->filename;
}
self::returnJson($docs);
}
protected function completeWorkorderAction() {
$post = json_decode(file_get_contents('php://input'), true);
if(empty($post['workorderId'])) self::sendError("Workorder ID missing.");
$workorder = RMLWorkorderModel::get($post['workorderId']);
if(!$workorder) self::sendError("Workorder not found.");
// Update Preorder status to 245
// PreorderModel::update(['id' => $workorder->preorderId, 'status_code' => 245]);
// Update Workorder status
RMLWorkorderModel::update([
'id' => $workorder->id,
'status' => 'completed'
]);
self::returnJson(['success' => true, 'message' => 'Auftrag abgeschlossen. Preorder wurde aktualisiert.']);
}
// Action to get companies for the assignment modal
protected function getCompaniesAction() {
if(!$this->user->can('RMLAdmin')) self::sendError("Permission denied.", 403);
$companies = RMLWorkorderCompanyModel::getAll();
$items = array_map(fn($c) => ['value' => $c->id, 'text' => $c->name], $companies);
self::returnJson($items);
}
}

View File

@@ -0,0 +1,157 @@
<?php
// RMLWorkorderAdminController.php
class RMLWorkorderAdminController extends TTCrud
{
protected string $headerTitle = 'RML Arbeitsaufträge (Admin)';
protected bool $createText = false;
protected array $permissionCheck = ['RMLAdmin'];
protected array $columns = [
['key' => 'id', 'text' => 'Auftrag-Nr.', 'table' => ['sortable' => true]],
['key' => 'preorderInfo', 'text' => 'Kunde / Projekt', 'modal' => false, 'table' => ['sortable' => false, 'filter' => 'search']],
['key' => 'companyName', 'text' => 'Zuständige Firma', 'modal' => false, 'table' => ['filter' => 'search']],
['key' => 'status', 'text' => 'Status', 'modal' => false, 'table' => ['filter' => 'iconSelect', 'filterOptions' => [
['value' => 'new', 'text' => 'Neu', 'icon' => 'fas fa-star text-primary'],
['value' => 'assigned', 'text' => 'Zugewiesen', 'icon' => 'fas fa-user-check text-info'],
['value' => 'scheduled', 'text' => 'Terminiert', 'icon' => 'fas fa-calendar-check text-warning'],
['value' => 'documented', 'text' => 'Dokumentiert', 'icon' => 'fas fa-file-alt text-success'],
['value' => 'completed', 'text' => 'Abgeschlossen', 'icon' => 'fas fa-check-double text-secondary'],
]]],
['key' => 'deadlineDate', 'text' => 'Deadline', 'modal' => false, 'table' => ['filter' => 'date']],
['key' => 'appointmentDate', 'text' => 'Termin', 'modal' => false, 'table' => ['filter' => 'date']],
['key' => 'actions', 'text' => 'Aktionen', 'modal' => false, 'table' => ['filter' => false, 'sortable' => false]],
];
protected function indexAction()
{
$this->createWorkordersFromPreorders();
Helper::renderVue($this, 'RMLWorkorderAdmin', $this->headerTitle, [
"CRUD_CONFIG" => $this->getCrudConfig(),
"TABLE_URL" => $this::getUrl("RMLWorkorderAdmin/get"),
]);
}
protected function getAction()
{
$json = json_decode(file_get_contents('php://input'), true);
$pagination = $json['pagination'] ?? ['page' => 1, 'per_page' => 10];
$filters = $json['filters'] ?? [];
$order = $json['order'] ?? ['key' => 'id', 'order' => 'DESC'];
// Custom filter logic for preorderInfo
if (!empty($filters['preorderInfo'])) {
$searchTerm = $filters['preorderInfo'];
unset($filters['preorderInfo']);
// This is a simplified search. A more robust implementation might involve a full-text search or a more complex query.
$preorders = PreorderModel::getAll(['firstname|lastname|company|oaid' => $searchTerm]);
$preorderIds = array_map(fn($p) => $p->id, $preorders);
if (!empty($preorderIds)) {
$filters['preorderId'] = $preorderIds;
} else {
// No preorders found, so no workorders will be found
$filters['id'] = -1;
}
}
$workorders = RMLWorkorderModel::getAll($filters, $pagination['per_page'], ($pagination['page'] - 1) * $pagination['per_page'], $order);
$totalCount = RMLWorkorderModel::count($filters);
$rows = [];
foreach($workorders as $workorder) {
$row = (array)$workorder;
$preorder = new Preorder($workorder->preorderId);
$anschlussadresse = 'N/A';
if ($preorder->adb_hausnummer_id) {
$hn = $preorder->adb_hausnummer;
$anschlussadresse = "{$hn->strasse->name} {$hn->hausnummer}";
if ($hn->stiege) $anschlussadresse .= "/{$hn->stiege}";
if ($preorder->adb_wohneinheit_id) $anschlussadresse .= " / WE: {$preorder->adb_wohneinheit->bezeichner}";
$anschlussadresse .= ", {$hn->plz->plz} {$hn->ortschaft->name}";
}
$kunde = ($preorder->company) ?: "{$preorder->firstname} {$preorder->lastname}";
$row['preorderInfo'] = "<strong>Kunde:</strong> {$kunde}<br>" .
"<strong>Anschluss:</strong> {$anschlussadresse}<br>" .
"<strong>OAID:</strong> <span class='text-pink'>{$preorder->oaid}</span>";
if($workorder->companyId) {
$company = RMLWorkorderCompanyModel::get($workorder->companyId);
$row['companyName'] = $company->name ?? 'N/A';
} else {
$row['companyName'] = 'Nicht zugewiesen';
}
$rows[] = $row;
}
self::returnJson([
'rows' => $rows,
'pagination' => [
'page' => $pagination['page'],
'per_page' => $pagination['per_page'],
'total_rows' => $totalCount,
'total_pages' => ceil($totalCount / $pagination['per_page']),
'filtered_available' => $totalCount
]
]);
}
private function createWorkordersFromPreorders() {
$newPreorders = PreorderModel::searchActive(['status_code' => 220]);
if (empty($newPreorders)) return;
foreach ($newPreorders as $preorder) {
if (!RMLWorkorderModel::getFirst(['preorderId' => $preorder->id])) {
RMLWorkorderModel::create([
'preorderId' => $preorder->id,
'status' => 'new',
'create' => time(),
'createBy' => $this->user->id
]);
}
}
}
protected function assignWorkorderAction() {
$post = json_decode(file_get_contents('php://input'), true);
if (empty($post['workorderId']) || empty($post['companyId'])) self::sendError("Required fields are missing.");
$workorder = RMLWorkorderModel::get($post['workorderId']);
if (!$workorder) self::sendError("Workorder not found.");
$workorder->companyId = $post['companyId'];
$workorder->status = 'assigned';
$workorder->assignmentDate = time();
$workorder->deadlineDate = strtotime('+6 weeks');
RMLWorkorderModel::update((array)$workorder);
self::returnJson(['success' => true, 'message' => 'Auftrag erfolgreich zugewiesen.']);
}
protected function getDocumentationAction() {
if(empty($this->request->workorderId)) self::sendError("Workorder ID missing.");
$docs = RMLWorkorderDocumentationModel::getAll(['workorderId' => $this->request->workorderId], null, 0, ['key' => 'create', 'order' => 'DESC']);
$users = UserModel::search(['employee' => true]);
$userMap = array_reduce($users, fn($carry, $user) => $carry + [$user->id => $user->name], []);
foreach($docs as $doc) {
$file = new File($doc->fileId);
$doc->fileName = $file->orig_filename ?? $file->filename;
$doc->userName = $userMap[$doc->createBy] ?? 'Unbekannt';
}
self::returnJson($docs);
}
protected function getCompaniesAction() {
$companies = RMLWorkorderCompanyModel::getAll([], null, 0, ['key' => 'name', 'order' => 'ASC']);
$items = array_map(fn($c) => ['value' => $c->id, 'text' => $c->name], $companies);
self::returnJson($items);
}
}

View File

@@ -0,0 +1,244 @@
<?php
// RMLWorkorderCompanyController.php
class RMLWorkorderCompanyController extends TTCrud
{
protected string $headerTitle = 'Meine Arbeitsaufträge';
protected bool $createText = false;
protected array $columns = [
['key' => 'id', 'text' => 'Auftrag-Nr.', 'table' => ['sortable' => true]],
['key' => 'preorderInfo', 'text' => 'Kunde / Projekt', 'modal' => false, 'table' => ['sortable' => false, 'filter' => 'search']],
['key' => 'status', 'text' => 'Status', 'modal' => false, 'table' => ['filter' => 'iconSelect', 'filterOptions' => [
['value' => 'new', 'text' => 'Neu', 'icon' => 'fas fa-star text-primary'],
['value' => 'assigned', 'text' => 'Zugewiesen', 'icon' => 'fas fa-user-check text-info'],
['value' => 'scheduled', 'text' => 'Terminiert', 'icon' => 'fas fa-calendar-check text-warning'],
['value' => 'documented', 'text' => 'Dokumentiert', 'icon' => 'fas fa-file-alt text-success'],
['value' => 'completed', 'text' => 'Abgeschlossen', 'icon' => 'fas fa-check-double text-secondary'],
]]],
['key' => 'deadlineDate', 'text' => 'Deadline', 'modal' => false, 'table' => ['filter' => 'date']],
['key' => 'appointmentDate', 'text' => 'Termin', 'modal' => false, 'table' => ['filter' => 'date']],
['key' => 'actions', 'text' => 'Aktionen', 'modal' => false, 'table' => ['filter' => false, 'sortable' => false]],
];
protected array $additionalJSVariables = ['COMPANY_ID' => '0'];
protected function prepareCrudConfig() {
$company = RMLWorkorderCompanyModel::getFirst(['addressId' => $this->user->address_id]);
if ($company) {
$this->additionalJSVariables['COMPANY_ID'] = $company->id;
} else {
$this->sendError('Access Denied. You are not associated with a registered RML company.', 403);
}
}
protected function indexAction()
{
Helper::renderVue($this, 'RMLWorkorderCompany', $this->headerTitle, [
"CRUD_CONFIG" => $this->getCrudConfig(),
"TABLE_URL" => $this::getUrl("RMLWorkorderCompany/get"),
"COMPANY_ID" => $this->additionalJSVariables['COMPANY_ID'],
]);
}
protected function getAction()
{
$json = json_decode(file_get_contents('php://input'), true);
$pagination = $json['pagination'] ?? ['page' => 1, 'per_page' => 10];
$filters = $json['filters'] ?? [];
$order = $json['order'] ?? ['key' => 'id', 'order' => 'DESC'];
$company = RMLWorkorderCompanyModel::getFirst(['addressId' => $this->user->address_id]);
if(!$company) self::sendError("Company not found for user.", 403);
$filters['companyId'] = $company->id;
if (!empty($filters['preorderInfo'])) {
$searchTerm = $filters['preorderInfo'];
//todo: fix this preordermodel search shit
$preorders = PreorderModel::getAll(['firstname|lastname|company|oaid' => $searchTerm]);
$preorderIds = array_map(fn($p) => $p->id, $preorders);
if (!empty($preorderIds)) {
$filters['preorderId'] = $preorderIds;
} else {
$filters['id'] = -1;
}
}
unset($filters['preorderInfo']);
// only show workorders that are assigned to the company and have the status assigned or scheduled
$filters['status'] = ['assigned', 'scheduled'];
$filters['companyId'] = $company->id;
$workorders = RMLWorkorderModel::getAll($filters, $pagination['per_page'], ($pagination['page'] - 1) * $pagination['per_page'], $order);
$totalCount = RMLWorkorderModel::count($filters);
$rows = [];
foreach($workorders as $workorder) {
$row = (array)$workorder;
$row['preorderInfo'] = $this->getPreorderInfoText($workorder->preorderId);
$rows[] = $row;
}
self::returnJson([
'rows' => $rows,
'pagination' => [
'page' => $pagination['page'],
'per_page' => $pagination['per_page'],
'total_rows' => $totalCount,
'total_pages' => ceil($totalCount / $pagination['per_page']),
'filtered_available' => $totalCount
]
]);
}
public function getWorkorderByIdAction() {
$id = $this->request->id;
if(!$id) self::sendError("ID missing");
$workorder = RMLWorkorderModel::get($id);
if(!$workorder) self::sendError("Workorder not found");
$workorder->preorderInfo = $this->getPreorderInfoText($workorder->preorderId);
self::returnJson((array) $workorder);
}
private function getPreorderInfoText($preorderId) {
$preorder = new Preorder($preorderId);
$anschlussadresse = 'N/A';
if ($preorder->adb_hausnummer_id) {
$hn = $preorder->adb_hausnummer;
$anschlussadresse = "{$hn->strasse->name} {$hn->hausnummer}";
if ($hn->stiege) $anschlussadresse .= "/{$hn->stiege}";
if ($preorder->adb_wohneinheit_id) $anschlussadresse .= " / WE: {$preorder->adb_wohneinheit->bezeichner}";
$anschlussadresse .= ", {$hn->plz->plz} {$hn->ortschaft->name}";
}
$kunde = ($preorder->company) ?: "{$preorder->firstname} {$preorder->lastname}";
return "<strong>Kunde:</strong> {$kunde}<br>" .
"<strong>Anschluss:</strong> {$anschlussadresse}<br>" .
"<strong>Kontakt:</strong> {$preorder->phone} / {$preorder->email}<br>" .
"<strong>OAID:</strong> <span class='text-pink'>{$preorder->oaid}</span>";
}
protected function scheduleAppointmentAction() {
$post = json_decode(file_get_contents('php://input'), true);
if (empty($post['workorderId']) || empty($post['appointmentDate'])) self::sendError("Required fields are missing.");
$workorder = RMLWorkorderModel::get($post['workorderId']);
if(!$workorder) self::sendError("Workorder not found");
$workorder->appointmentDate = $post['appointmentDate'];
$workorder->status = 'scheduled';
RMLWorkorderModel::update((array)$workorder);
self::returnJson(['success' => true, 'message' => 'Termin erfolgreich gespeichert.']);
}
protected function uploadDocumentationAction()
{
if (empty($_FILES['files']) || empty($_POST['workorderId'])) {
self::returnJson(['error' => 'Required data is missing.']);
return;
}
$workorderId = $_POST['workorderId'];
$description = $_POST['description'] ?? '';
$documentType = $_POST['documentType'] ?? 'general';
$files = $_FILES['files'];
$uploadCount = 0;
foreach ($files['name'] as $index => $name) {
if ($files['error'][$index] === UPLOAD_ERR_OK) {
$_FILES['file'] = [
'name' => $files['name'][$index],
'type' => $files['type'][$index],
'tmp_name' => $files['tmp_name'][$index],
'error' => $files['error'][$index],
'size' => $files['size'][$index]
];
try {
$uploaded = mfUpload::handleFormUpload("file", false, "/RMLWorkorder");
RMLWorkorderDocumentationModel::create([
'workorderId' => $workorderId,
'fileId' => $uploaded->id,
'description' => $description,
'documentType' => $documentType,
'create' => time(),
'createBy' => $this->user->id
]);
$uploadCount++;
} catch (Exception $e) {
var_dump($e->getMessage());exit;
// Log error but continue with other files
error_log("File upload failed for $name: " . $e->getMessage());
}
}
}
self::returnJson(['success' => true, 'message' => "$uploadCount Datei(en) erfolgreich hochgeladen."]);
}
protected function getDocumentationAction() {
if(empty($this->request->workorderId)) self::sendError("Workorder ID missing.");
// Order by creation date to ensure consistent numbering (_1, _2, etc.)
$docs = RMLWorkorderDocumentationModel::getAll(['workorderId' => $this->request->workorderId], null, 0, ['key' => 'create', 'order' => 'ASC']);
$responseDocs = [];
$typeCounts = [];
$translationMap = [
'photo_before' => 'Foto vorher',
'photo_during' => 'Foto währenddessen',
'photo_after' => 'Foto nachher',
'measurement_protocol' => 'Messprotokoll',
'customer_signature' => 'Kundenunterschrift',
];
foreach($docs as $doc) {
$file = new File($doc->fileId);
// Increment counter for the specific document type
$documentTypeKey = $doc->documentType;
if (!isset($typeCounts[$documentTypeKey])) {
$typeCounts[$documentTypeKey] = 1;
} else {
$typeCounts[$documentTypeKey]++;
}
// Construct the new filename using the original key
$originalFilename = $file->orig_filename ?? $file->filename;
$extension = pathinfo($originalFilename, PATHINFO_EXTENSION);
$translatedType = $translationMap[$documentTypeKey] ?? $documentTypeKey;
$newFilename = "{$translatedType} {$typeCounts[$documentTypeKey]}." . strtolower($extension);
// Get the translated text, with a fallback to the original key
// Build the response object with 'id' mapped from 'fileId' and the translated type
$responseDocs[] = [
'id' => $doc->fileId,
'fileName' => $newFilename,
'documentType' => $documentTypeKey,
'mimetype' => $file->mimetype,
];
}
self::returnJson($responseDocs);
}
protected function completeWorkorderAction() {
$post = json_decode(file_get_contents('php://input'), true);
if(empty($post['workorderId'])) self::sendError("Workorder ID missing.");
$workorder = RMLWorkorderModel::get($post['workorderId']);
if(!$workorder) self::sendError("Workorder not found.");
$workorder->status = 'documented';
RMLWorkorderModel::update((array)$workorder);
self::returnJson(['success' => true, 'message' => 'Auftrag abgeschlossen.']);
}
}

View File

@@ -20,6 +20,8 @@ class WarehouseArticleController extends TTCrud {
['key' => 'isSerialDocumentation', 'text' => 'Seriennummern', 'required' => false,'modal' => ['type' => 'checkbox'], 'table' => false],
['key' => 'isEShop', 'text' => 'Ist E-Shop', 'required' => false,'modal' => ['type' => 'checkbox'], 'table' => false],
['key' => 'isEShopHide', 'text' => 'E-Shop Versteckt', 'required' => false,'modal' => ['type' => 'checkbox'], 'table' => false],
['key' => 'isSbidiShop', 'text' => 'Ist SBIDI-Shop', 'required' => false,'modal' => ['type' => 'checkbox'], 'table' => false],
['key' => 'isSbidiShopHide', 'text' => 'SBIDI-Shop Versteckt', 'required' => false,'modal' => ['type' => 'checkbox'], 'table' => false],
['key' => 'actions', 'text' => 'Aktionen', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center', 'priority' => 8]]
];

View File

@@ -12,6 +12,8 @@ class WarehouseArticleModel extends TTCrudBaseModel {
public int $criticalAmount;
public ?int $isEShop;
public ?int $isEShopHide;
public ?int $isSbidiShop;
public ?int $isSbidiShopHide;
public string $unit;
public ?int $isSerialDocumentation;
public int $revenueAccount;

View File

@@ -1,9 +0,0 @@
<?php
/**
* @property mixed|null $name
*/
class WarehouseArticlePacket extends mfBaseModel
{
}

View File

@@ -13,20 +13,25 @@ class WarehouseArticlePacketController extends TTCrud {
['key' => 'overrideSellPrice', 'text' => 'Überschriebener Verkaufspreis', 'required' => false, 'modal' => ['type' => 'number'], 'table' => false],
['key' => 'calculatedSellPrice', 'text' => 'Verkaufspreis', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false]],
['key' => 'subItems', 'text' => 'Unterartikel', 'required' => true],
['key' => 'isEShop', 'text' => 'E-Shop', 'required' => false, 'modal' => ['type' => 'checkbox', 'items' => [['value' => 1, 'icon' => 'fas fa-check-circle text-success', 'text' => 'Ja'], ['value' => 0, 'icon' => 'fas fa-times-circle text-danger', 'text' => 'Nein']]], 'table' => ['filter' => 'iconSelect']],
['key' => 'isEShopHide', 'text' => 'Hide', 'required' => false, 'modal' => ['type' => 'checkbox', 'items' => [['value' => 1, 'icon' => 'fas fa-check-circle text-success', 'text' => 'Ja'], ['value' => 0, 'icon' => 'fas fa-times-circle text-danger', 'text' => 'Nein']]], 'table' => ['filter' => 'iconSelect']],
['key' => 'isSbidiShop', 'text' => 'S-Shop', 'required' => false, 'modal' => ['type' => 'checkbox', 'items' => [['value' => 1, 'icon' => 'fas fa-check-circle text-success', 'text' => 'Ja'], ['value' => 0, 'icon' => 'fas fa-times-circle text-danger', 'text' => 'Nein']]], 'table' => ['filter' => 'iconSelect']],
['key' => 'isSbidiShopHide', 'text' => 'Hide', 'required' => false, 'modal' => ['type' => 'checkbox', 'items' => [['value' => 1, 'icon' => 'fas fa-check-circle text-success', 'text' => 'Ja'], ['value' => 0, 'icon' => 'fas fa-times-circle text-danger', 'text' => 'Nein']]], 'table' => ['filter' => 'iconSelect']],
['key' => 'actions', 'text' => 'Aktionen', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center', 'priority' => 10]],
];
// @formatter:on
protected array $infoMessages = ['create' => 'Artikel-Paket wurde erstellt',
'update' => 'Artikel-Paket wurde aktualisiert',
'delete' => 'Artikel-Paket wurde gelöscht',
'noChanges' => 'Keine Änderungen'];
'update' => 'Artikel-Paket wurde aktualisiert',
'delete' => 'Artikel-Paket wurde gelöscht',
'noChanges' => 'Keine Änderungen'];
protected function prepareCrudConfig() {
$articles = array_map(function ($article) {
return ['value' => $article->id, 'text' => $article->title];
}, WarehouseArticleModel::getAll(
['isEShop' => 1],
// Filter articles based on the user's address_id for the shop context
($this->user->address_id === 209) ? ['isEShop' => 1] : (($this->user->address_id === 210) ? ['isSbidiShop' => 1] : []),
));
$this->columns[6]['modal']['items'] = $articles;
@@ -35,9 +40,21 @@ class WarehouseArticlePacketController extends TTCrud {
//TODO: make this so it does not update all packets at the same time
protected function updatePacketPricesAction() {
$packets = WarehouseArticlePacketModel::getAll();
$articles = WarehouseArticleModel::getAll(['isEShop' => 1]);
// packet has $calculatedSellPrice for this but when overrideSellPrice is set, it should be used
// Determine which shop's articles to use for price calculation based on the current user's shop context
// This is a simplification; in a multi-tenant system, this might need to be more robust,
// e.g., by iterating through all possible shop types or having a dedicated price calculation service.
$shopPriceTitle = '';
$articleFilter = [];
if ($this->user->address_id === 209) {
$shopPriceTitle = 'Energie Steiermark';
$articleFilter['isEShop'] = 1;
} elseif ($this->user->address_id === 210) {
$shopPriceTitle = 'Sbidi';
$articleFilter['isSbidiShop'] = 1;
}
$articles = WarehouseArticleModel::getAll($articleFilter);
foreach ($packets as $packet) {
if ($packet->overrideSellPrice) {
@@ -47,20 +64,29 @@ class WarehouseArticlePacketController extends TTCrud {
$calculatedSellPrice = 0;
foreach ($subItems as $subItem) {
$article = WarehouseArticleModel::get($subItem->id);
$cheapestSellPrices = json_decode($article->cheapestSellPrice, true);
// find in array cheapestSellPrices by title === 'Energie Steiermark' and get the price
$articlePrice = array_values(array_filter($cheapestSellPrices, function ($cheapestSellPrice) {
return $cheapestSellPrice['title'] === 'Energie Steiermark';
}));
$article = null;
// Find the article by ID from the already fetched articles to avoid N+1 queries
foreach ($articles as $a) {
if ($a->id == $subItem->id) {
$article = $a;
break;
}
}
$articlePrice = $articlePrice[0]['price'] ?? 0;
$calculatedSellPrice += $subItem->amount * $articlePrice;
if ($article) {
$cheapestSellPrices = json_decode($article->cheapestSellPrice, true);
$articlePrice = 0;
// Find price for the specific shop
$foundPrice = array_values(array_filter($cheapestSellPrices, function ($cheapestSellPrice) use ($shopPriceTitle) {
return $cheapestSellPrice['title'] === $shopPriceTitle;
}));
$articlePrice = $foundPrice[0]['price'] ?? 0;
$calculatedSellPrice += $subItem->amount * $articlePrice;
}
}
}
WarehouseArticlePacketModel::update(array_merge(get_object_vars($packet), ['calculatedSellPrice' => $calculatedSellPrice]));
WarehouseArticlePacketModel::update(array_merge(get_object_vars($packet), ['calculatedSellPrice' => $calculatedSellPrice]));
}
return true;

View File

@@ -9,4 +9,8 @@ class WarehouseArticlePacketModel extends TTCrudBaseModel {
public ?float $overrideSellPrice;
public ?float $calculatedSellPrice;
public string $subItems;
}
public ?int $isEShop; // New field for Energie Steiermark shop visibility
public ?int $isEShopHide; // New field to hide from Energie Steiermark shop
public ?int $isSbidiShop; // New field for Sbidi shop visibility
public ?int $isSbidiShopHide; // New field to hide from Sbidi shop
}

View File

@@ -1,9 +0,0 @@
<?php
/**
* @property mixed|null $name
*/
class WarehouseEShop extends mfBaseModel
{
}

View File

@@ -1,66 +1,73 @@
<?php
// Warrenkorb löschen
// File Upload ermöglichen
// Hide Articles
class WarehouseEShopController extends TTCrud {
protected string $headerTitle = 'Energie Steiermark Shop';
protected bool $createText = false;
//@formatter:off
protected array $columns = [
['key' => 'title', 'text' => 'Artikel', 'priority' => 11],
['key' => 'category', 'text' => 'Kategorie', 'table' => false],
['key' => 'price', 'text' => 'Preis', 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-right']],
['key' => 'price', 'text' => 'Preis', 'table' => false],
['key' => 'amount', 'text' => 'Menge', 'table' => ['filter' => false, 'sortable' => false, 'class' => 'p-0 width-80'], 'priority' => 9],
['key' => 'add', 'text' => 'Hinzufügen', 'table' => ['filter' => false, 'sortable' => false, 'class' => 'width-120 text-center'], 'priority' => 5000]
];
//@formatter:on
protected array $permissionCheck = ['WarehouseEShop'];
protected array $infoMessages = [
'create' => 'Not possible',
'update' => 'Not possible',
'delete' => 'Not possible',
'noChanges' => 'Keine Änderungen',
];
protected function afterInit() {
if (!$this->user->isAdmin()) return;
if (!$this->user->getFlag('WarehouseSelectedShop') && !isset($_GET['shop'])) self::sendError("Bitte wählen Sie einen Shop aus.");
protected function prepareCrudConfig() {
if (!$this->user->can('WarehouseAdmin')) {
$this->columns[2]['table'] = false;
if (in_array($_GET['shop'], ['e', 'sbidi'])) {
$flag = new WorkerFlag($this->user->id, 'WarehouseSelectedShop');
$flag->value($_GET['shop']);
$flag->save();
$this->user->address_id = ($_GET['shop'] === 'e') ? '209' : '9633';
return;
}
$this->user->address_id = ($this->user->getFlag('WarehouseSelectedShop')->value() === 'e') ? '209' : '9633';
}
protected function prepareCrudConfig() {
if (!in_array(intval($this->user->address_id), [209, 9633])) self::sendError("Keine Berechtigung für diesen Shop");
$this->additionalJSVariables['userAddressId'] = $this->user->address_id ?? null;
$this->headerTitle = $this->user->address_id == 209 ? 'Energie Steiermark Shop' : 'SBIDI Shop';
}
public function getAction() {
if (!in_array(intval($this->user->address_id), [209, 9633])) self::sendError("Keine Berechtigung für diesen Shop");
$filter = $this->postData['filters'] ?? [];
$order = $this->postData['order'] ?? ['key' => null, 'order' => 'ASC'];
$page = $this->postData['pagination']['page'] ?? 1;
$perPage = $this->postData['pagination']['per_page'] ?? 10;
$warehouseArticleFilter = $filter;
$warehouseArticleFilter['isEShop'] = 1;
$warehouseArticleFilter['isEShopHide'] = 0;
$warehouseArticles = WarehouseArticleModel::getAll($warehouseArticleFilter, null, 0, $order);
$warehouseArticlesTotal = WarehouseArticleModel::count(['isEShop' => 1, 'isEShopHide' => 0]);
$warehouseArticlesAvailable = WarehouseArticleModel::count($warehouseArticleFilter);
$shopType = (intval($this->user->address_id) === 209) ? 'EShop' : 'SbidiShop';
$filter["is{$shopType}"] = 1;
$filter["is{$shopType}Hide"] = 0;
$warehouseArticles = WarehouseArticleModel::getAll($filter, null, 0, $order);
$warehousePackets = WarehouseArticlePacketModel::getAll($filter, null, 0, $order);
$warehousePacketsTotal = WarehouseArticlePacketModel::count();
$warehousePacketsAvailable = WarehouseArticlePacketModel::count($filter);
$filteredAvailable = $warehouseArticlesAvailable + $warehousePacketsAvailable;
$totalRows = $warehouseArticlesTotal + $warehousePacketsTotal;
$filteredAvailable = WarehouseArticlePacketModel::count($filter) + WarehouseArticleModel::count($filter);
$rows = [...$warehouseArticles, ...$warehousePackets];
$rows = array_slice($rows, ($page - 1) * $perPage, $perPage);
usort($rows, function($a, $b) { return strcmp($a->title, $b->title); });
self::returnJson(["rows" => $rows,
"pagination" => ["page" => $page,
"total_pages" => ceil($filteredAvailable / $perPage),
"per_page" => $perPage,
"filtered_available" => $filteredAvailable,
"total_rows" => $totalRows]]);
self::returnJson([
"rows" => array_slice($rows, ($page - 1) * $perPage, $perPage),
"pagination" => [
"page" => $page,
"total_pages" => ceil($filteredAvailable / $perPage),
"per_page" => $perPage,
"filtered_available" => $filteredAvailable,
"total_rows" => $filteredAvailable
]
]);
}
}

View File

@@ -1,9 +0,0 @@
<?php
/**
* @property mixed|null $name
*/
class WarehouseEShopOrder extends mfBaseModel
{
}

View File

@@ -8,6 +8,7 @@ class WarehouseEShopOrderController extends TTCrud {
protected array $columns = [
['key' => 'id', 'text' => 'ID', 'modal' => false],
['key' => 'extRef', 'text' => 'Externe Referenz', 'required' => true],
['key' => 'addressId', 'text' => 'Shop', 'modal' => false, 'table' => ['filter' => 'select'], 'type' => 'select', 'items' => []], // New column for address ID
['key' => 'status', 'text' => 'Status', 'required' => true, 'modal' => ['type' => 'select', 'items' => [['value' => 'new', 'text' => 'Neu'], ['value' => 'accepted', 'text' => 'An Lieferant übergeben'], ['value' => 'acceptedInternally', 'text' => 'Interne verarbeitung'], ['value' => 'sent', 'text' => 'Gesendet'], ['value' => 'done', 'text' => 'Erledigt'],]], 'table' => ['filter' => 'select']],
['key' => 'shippingNoteStatus', 'text' => 'LS-Status', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'order' => false]],
['key' => 'deliveryMode', 'text' => 'Liefermodus', 'required' => true, 'modal' => ['type' => 'select', 'items' => [['value' => 'singleAddress', 'text' => 'Einzelne Adresse']]]],
@@ -69,6 +70,16 @@ class WarehouseEShopOrderController extends TTCrud {
$createByIndex = array_search('createBy', array_column($this->columns, 'key'));
$this->columns[$createByIndex]['modal']['items'] = $users;
// Add options for the new addressId column filter
$addressIdColumnIndex = array_search('addressId', array_column($this->columns, 'key'));
if ($addressIdColumnIndex !== false) {
$this->columns[$addressIdColumnIndex]['items'] = [
['value' => 209, 'text' => 'Energie Steiermark'],
['value' => 9633, 'text' => 'SBIDI'],
];
$this->columns[$addressIdColumnIndex]['modal']['items'] = $this->columns[$addressIdColumnIndex]['items'];
}
}
protected function createShippingNote() {
@@ -81,8 +92,8 @@ class WarehouseEShopOrderController extends TTCrud {
$existingShippingNote = WarehouseShippingNoteModel::getAll(['eShopOrderId' => $id]);
if (!empty($existingShippingNote)) {
self::returnJson(['success' => false,
'message' => 'Für diese Bestellung existiert bereits ein Lieferschein',
'shippingNoteId' => $existingShippingNote[0]->id]);
'message' => 'Für diese Bestellung existiert bereits ein Lieferschein',
'shippingNoteId' => $existingShippingNote[0]->id]);
die();
}
@@ -101,11 +112,19 @@ class WarehouseEShopOrderController extends TTCrud {
$articleTitle = $item->articleId ? $articles[$article]->title : $articlePackets[$articlePacket]->title;
$quantity = $item->quantity;
$price = 0;
$priceTitle = '';
if ($order->addressId === 209) {
$priceTitle = 'Energie Steiermark';
} elseif ($order->addressId === 9633) {
$priceTitle = 'SBIDI';
}
if ($item->articleId) {
$cheapestSellPrice = json_decode($articles[$article]->cheapestSellPrice, true);
foreach ($cheapestSellPrice as $price) {
if ($price['title'] === 'Energie Steiermark') {
$price = $price['price'];
foreach ($cheapestSellPrice as $p) {
if ($p['title'] === $priceTitle) {
$price = $p['price'];
break;
}
}
@@ -126,20 +145,20 @@ class WarehouseEShopOrderController extends TTCrud {
$positions = json_encode($positions);
$shippingNoteId = WarehouseShippingNoteModel::create(['billingAddressId' => 3265,
'deliveryAddressName' => $order->deliveryAddressName,
'deliveryAddressLine' => $order->deliveryAddressLine,
'deliveryAddressPLZ' => $order->deliveryAddressPLZ,
'deliveryAddressCity' => $order->deliveryAddressCity,
'deliveryAddressEMail' => '',
'note' => 'Erstellung aus Energie Steiermark Shop Bestellung #' . $id,
'status' => 'new',
'positions' => $positions,
'textElements' => '[]',
'hoursEntries' => '[]',
'eShopOrderId' => $id,
'create' => time(),
'createBy' => $this->user->id]);
$shippingNoteId = WarehouseShippingNoteModel::create(['billingAddressId' => 3265, // Assuming a default billing address
'deliveryAddressName' => $order->deliveryAddressName,
'deliveryAddressLine' => $order->deliveryAddressLine,
'deliveryAddressPLZ' => $order->deliveryAddressPLZ,
'deliveryAddressCity' => $order->deliveryAddressCity,
'deliveryAddressEMail' => '',
'note' => 'Erstellung aus Shop Bestellung #' . $id,
'status' => 'new',
'positions' => $positions,
'textElements' => '[]',
'hoursEntries' => '[]',
'eShopOrderId' => $id,
'create' => time(),
'createBy' => $this->user->id]);
self::returnJson(['success' => true, 'message' => 'Lieferschein wurde erstellt', 'shippingNoteId' => $shippingNoteId]);
@@ -167,8 +186,9 @@ class WarehouseEShopOrderController extends TTCrud {
$article = $item->articleId ? array_search($item->articleId, array_column($articles, 'id')) : null;
$articlePacket = $item->articlePacketId ? array_search($item->articlePacketId, array_column($articlePackets, 'id')) : null;
$articleExtRef = $articleDistributor[array_search($item->articleId, array_column($articleDistributor, 'articleId'))];
$articleExtRef = $item->articleId ? $articleExtRef->externalArticleNumber : (!empty($articlePacket->externalArticleNumber) ? $articlePacket->externalArticleNumber : null);
$articleExtRef = $item->articleId && isset($articleDistributor[array_search($item->articleId, array_column($articleDistributor, 'articleId'))]) ? $articleDistributor[array_search($item->articleId, array_column($articleDistributor, 'articleId'))]->externalArticleNumber : null;
$articleExtRef = $item->articlePacketId && isset($articlePackets[$articlePacket]) && !empty($articlePackets[$articlePacket]->externalArticleNumber) ? $articlePackets[$articlePacket]->externalArticleNumber : $articleExtRef;
$articleTitle = $item->articleId ? $articles[$article]->title : $articlePackets[$articlePacket]->title;
$quantity = $item->quantity;
$body .= $articleExtRef !== null ? "$quantity x $articleExtRef ($articleTitle)\n" : "$quantity x $articleTitle\n";
@@ -183,7 +203,16 @@ class WarehouseEShopOrderController extends TTCrud {
} else {
$csvContent = $this->CSVExportNewOrdersMarkAcceptedAction(true, [$id]);
foreach (["ftth-versand@triotronik.com", "eshop-versand@xinon.at"] as $emailAddr) {
// Determine recipient emails based on addressId
$recipientEmails = ["eshop-versand@xinon.at"]; // Default for all orders
if ($order->addressId === 209) {
$recipientEmails[] = "ftth-versand@triotronik.com"; // Energie Steiermark specific
} elseif ($order->addressId === 9633) {
$recipientEmails[] = "sbidi-versand@xinon.at"; // SBIDI specific (example, adjust as needed)
}
foreach ($recipientEmails as $emailAddr) {
$email = new Emailnotification();
$email->setSubject("Bestellbestätigung Bestellung #$paddedId");
$email->setBody($body);
@@ -263,23 +292,32 @@ class WarehouseEShopOrderController extends TTCrud {
return "$quantity x $articleExtRef ($articleTitle)";
}, $orderItems));
$rows[] = ['AddressNumber' => '23000539',
'Name' => $order['deliveryAddressName'],
'Straße' => $order['deliveryAddressLine'],
'Postleitzahl' => $order['deliveryAddressPLZ'],
'Ort' => $order['deliveryAddressCity'],
'Land' => 'AT',
'Anschriftenzusatz 1' => $order['deliveryAddressAdditional'],
'Produkte' => $orderItemsStr];
// Determine AddressNumber based on order's addressId
$addressNumber = '';
if ($order['addressId'] === 209) {
$addressNumber = '23000539'; // Energie Steiermark
} elseif ($order['addressId'] === 9633) {
$addressNumber = 'SBIDI_CUSTOMER_NUMBER'; // Placeholder for SBIDI, replace with actual
}
$rows[] = ['AddressNumber' => $addressNumber,
'Name' => $order['deliveryAddressName'],
'Straße' => $order['deliveryAddressLine'],
'Postleitzahl' => $order['deliveryAddressPLZ'],
'Ort' => $order['deliveryAddressCity'],
'Land' => 'AT',
'Anschriftenzusatz 1' => $order['deliveryAddressAdditional'],
'Produkte' => $orderItemsStr];
WarehouseHistoryModel::create(['table' => 'WarehouseEShopOrder',
'row_id' => $order['id'],
'key' => 'status',
'old_value' => 'new',
'new_value' => 'accepted',
'note' => 'CSV Export',
'user_id' => $this->user->id,
'create' => time()]);
'row_id' => $order['id'],
'key' => 'status',
'old_value' => 'new',
'new_value' => 'accepted',
'note' => 'CSV Export',
'user_id' => $this->user->id,
'create' => time()]);
$order['status'] = 'accepted';
WarehouseEShopOrderModel::update($order);
@@ -310,15 +348,19 @@ class WarehouseEShopOrderController extends TTCrud {
$article = $item['articleId'] ? array_search($item['articleId'], array_column($articles, 'id')) : null;
$articlePacket = $item['articlePacketId'] ? array_search($item['articlePacketId'], array_column($articlePackets, 'id')) : null;
$articleExtRef = array_search($item['articleId'], array_column($articleDistributor, 'articleId'))['externalArticleNumber'] ?? null;
$articleExtRef = null;
if ($item['articleId'] && isset($articleDistributor[array_search($item['articleId'], array_column($articleDistributor, 'articleId'))])) {
$articleExtRef = $articleDistributor[array_search($item['articleId'], array_column($articleDistributor, 'articleId'))]->externalArticleNumber;
}
$orderItems[$item['orderId']][] = ['id' => $item['id'],
'articleId' => $item['articleId'],
'articleExtRef' => $articleExtRef,
'articleTitle' => isset($articles[$article]) ? $articles[$article]->title : null,
'articlePacketId' => $item['articlePacketId'],
'articlePacketTitle' => isset($articlePackets[$articlePacket]) ? $articlePackets[$articlePacket]->title : null,
'quantity' => $item['quantity']];
'articleId' => $item['articleId'],
'articleExtRef' => $articleExtRef,
'articleTitle' => isset($articles[$article]) ? $articles[$article]->title : null,
'articlePacketId' => $item['articlePacketId'],
'articlePacketTitle' => isset($articlePackets[$articlePacket]) ? $articlePackets[$articlePacket]->title : null,
'quantity' => $item['quantity']];
}
return $orderItems;
@@ -339,19 +381,22 @@ class WarehouseEShopOrderController extends TTCrud {
$json['status'] = 'new';
$json['create'] = time();
$json['createBy'] = $this->user->id;
$json['addressId'] = $this->user->address_id; // Store the address_id of the ordering user
Helper::validateArray($json, $this->getCheckArray());
$id = WarehouseEShopOrderModel::create(['status' => 'new',
'extRef' => $json['extRef'],
'deliveryMode' => $json['deliveryMode'],
'deliveryAddressAdditional' => $json['deliveryAddressAdditional'] ?? '',
'deliveryAddressName' => $json['deliveryAddressName'],
'deliveryAddressLine' => $json['deliveryAddressLine'],
'deliveryAddressPLZ' => $json['deliveryAddressPLZ'],
'deliveryAddressCity' => $json['deliveryAddressCity'],
'create' => $json['create'],
'createBy' => $json['createBy'],]);
'extRef' => $json['extRef'],
'deliveryMode' => $json['deliveryMode'],
'deliveryAddressAdditional' => $json['deliveryAddressAdditional'] ?? '',
'deliveryAddressName' => $json['deliveryAddressName'],
'deliveryAddressLine' => $json['deliveryAddressLine'],
'deliveryAddressPLZ' => $json['deliveryAddressPLZ'],
'deliveryAddressCity' => $json['deliveryAddressCity'],
'create' => $json['create'],
'createBy' => $json['createBy'],
'addressId' => $json['addressId'], // Pass the addressId
]);
// now create WarehouseEShopOrderItems for each item in the shopping cart
foreach ($shoppingCart as $item) {
@@ -359,12 +404,12 @@ class WarehouseEShopOrderController extends TTCrud {
// parse this and either fill articleId or articlePacketId for warehouseEShopOrderItem
if (strpos($item['itemId'], 'P-') === 0) {
WarehouseEShopOrderItemModel::create(['orderId' => $id,
'articlePacketId' => intval(substr($item['itemId'], 2)),
'quantity' => intval($item['amount']),]);
'articlePacketId' => intval(substr($item['itemId'], 2)),
'quantity' => intval($item['amount']),]);
} else if (strpos($item['itemId'], 'I-') === 0) {
WarehouseEShopOrderItemModel::create(['orderId' => $id,
'articleId' => intval(substr($item['itemId'], 2)),
'quantity' => intval($item['amount']),]);
'articleId' => intval(substr($item['itemId'], 2)),
'quantity' => intval($item['amount']),]);
} else {
self::returnJson(['success' => false, 'message' => 'Invalid item id']);
die();
@@ -390,8 +435,17 @@ class WarehouseEShopOrderController extends TTCrud {
$user = UserModel::getOne($json['createBy']);
if ($_SERVER['HTTP_HOST'] !== 'localhost')
foreach (["office@xinon.at", $user->email] as $emailAddr) {
if ($_SERVER['HTTP_HOST'] !== 'localhost') {
$recipientEmails = ["office@xinon.at", $user->email];
// Add shop-specific email if applicable
if ($json['addressId'] === 209) {
$recipientEmails[] = "ftth-versand@triotronik.com";
} elseif ($json['addressId'] === 9633) {
$recipientEmails[] = "sbidi-versand@xinon.at"; // Example for SBIDI
}
$recipientEmails = array_unique($recipientEmails); // Remove duplicates
foreach ($recipientEmails as $emailAddr) {
$email = new Emailnotification();
$email->setSubject("Bestellbestätigung Bestellung #$subjectId - Referenz: " . $json['extRef']);
$email->setBody($body);
@@ -399,10 +453,11 @@ class WarehouseEShopOrderController extends TTCrud {
$email->setTo($emailAddr);
$email->send();
}
}
self::returnJson(['success' => true,
'message' => $this->infoMessages['create'],
'id' => $id]);
'message' => $this->infoMessages['create'],
'id' => $id]);
}
protected function beforeCreate(): bool {
@@ -551,8 +606,8 @@ class WarehouseEShopOrderController extends TTCrud {
$orders = WarehouseEShopOrderModel::getAll(['deliveryAddressLine' => $addressLine,
'deliveryAddressPLZ' => $plz,
'deliveryAddressCity' => $city]);
'deliveryAddressPLZ' => $plz,
'deliveryAddressCity' => $city]);
if (empty($orders)) {
echo "No order found with address: $addressLine, $plz, $city" . PHP_EOL;
continue;
@@ -570,13 +625,13 @@ class WarehouseEShopOrderController extends TTCrud {
WarehouseEShopOrderModel::update($order);
WarehouseHistoryModel::create(['table' => 'WarehouseEShopOrder',
'row_id' => $order['id'],
'key' => 'trackingNumber',
'old_value' => '',
'new_value' => $trackingNumber,
'note' => '',
'user_id' => 1,
'create' => date('U')]);
'row_id' => $order['id'],
'key' => 'trackingNumber',
'old_value' => '',
'new_value' => $trackingNumber,
'note' => '',
'user_id' => 1,
'create' => date('U')]);
// echo "Subject: " . $overview[0]->subject . "\n";
// echo "From: " . $overview[0]->from . "\n";

View File

@@ -11,6 +11,7 @@
* @property string $deliveryAddressCity
* @property int $create
* @property int $createBy
* @property int $addressId // Added addressId property
*/
class WarehouseEShopOrderModel extends TTCrudBaseModel {
@@ -28,4 +29,5 @@ class WarehouseEShopOrderModel extends TTCrudBaseModel {
public ?string $trackingNumber;
public int $create;
public int $createBy;
}
public ?int $addressId; // New field to store the address_id of the ordering entity
}

View File

@@ -1,9 +0,0 @@
<?php
/**
* @property mixed|null $name
*/
class WarehouseEShopOrderItem extends mfBaseModel
{
}

View File

@@ -53,11 +53,13 @@ class WarehouseOfferController extends TTCrud
$this->postData['offerNumber'] = 'AN' . date('Y') . '-' . str_pad($currentCount + 1, 4, '0', STR_PAD_LEFT);
$this->postData['status'] = 'new';
$this->postData['version'] = 1;
$this->postData['alternativePositions'] = json_encode([]);
return true;
}
protected function afterCreate($id): void
protected function afterCreate($offer): void
{
$id = $offer['id'];
$offer = WarehouseOfferModel::get($id);
$this->createHistoryEntry($id, 1, $offer);
}