'id', 'text' => 'ID', 'modal' => false], ['key' => 'extRef', 'text' => 'Externe Referenz', 'required' => true], ['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']]]], ['key' => 'deliveryAddressName', 'text' => 'Name', 'required' => true], ['key' => 'deliveryAddressLine', 'text' => 'Adresse', 'required' => true, 'required_length' => 4], ['key' => 'deliveryAddressPLZ', 'text' => 'PLZ', 'required' => true, 'regex' => '/^\d{4}$/'], ['key' => 'deliveryAddressCity', 'text' => 'Stadt', 'required' => true, 'required_length' => 3], ['key' => 'trackingNumber', 'text' => 'Trackingnummer', 'required' => false, 'modal' => false], ['key' => 'positions', 'text' => 'Positionen', 'required' => false, 'table' => false], ['key' => 'create', 'text' => 'Erstellt', 'required' => true, 'modal' => false, 'filter' => 'datetime'], ['key' => 'createBy', 'text' => 'Erstellt von', 'required' => true, 'table' => ['filter' => 'select'], 'modal' => ['type' => 'select', 'items' => []]], ['key' => 'actions', 'text' => 'Aktionen', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center']] ]; protected array $additionalActions = [ ['key' => 'openHistory', 'title' => 'Historie', 'class' => 'fas fa-history text-primary'], ['key' => 'showTrackingHistory', 'title' => 'Tracking Historie', 'class' => 'fas fa-truck text-primary'], ['key' => 'createShippingNote', 'title' => 'Lieferschein erstellen', 'class' => 'fas fa-file-invoice text-primary'], ['key' => 'openSingleOrderEmail', 'title' => 'Bestellbestätigung', 'class' => 'fas fa-envelope text-primary']# ]; protected array $infoMessages = [ 'create' => 'Bestellung wurde erfolgreich erstellt, sie erhalten in Kürze eine Bestätigungsmail', 'update' => 'Bestellung wurde aktualisiert', 'delete' => 'Bestellung wurde gelöscht', 'noChanges' => 'Keine Änderungen' ]; //@formatter:on public function permissionCheck(): bool { return $this->user->can(["WarehouseEShop"]); } protected function customRowsHandler($rows): array { $statusToText = [ 'new' => 'Neu', 'accepted' => 'Akzeptiert', 'invoiced' => 'Verrechnet', 'in_progress' => 'In Bearbeitung', ]; foreach ($rows as $row) { $shippingNote = WarehouseShippingNoteModel::getAll(['eShopOrderId' => $row->id]); if (empty($shippingNote)) { $row->shippingNoteStatus = 'Kein LS'; } else { $row->shippingNoteStatus = $statusToText[$shippingNote[0]->status]; } } return $rows; } protected function prepareCrudConfig() { $users = array_map(function ($user) { return ['value' => intval($user->id), 'text' => $user->name]; }, UserModel::search()); $createByIndex = array_search('createBy', array_column($this->columns, 'key')); $this->columns[$createByIndex]['modal']['items'] = $users; } protected function createShippingNote() { $id = intval($this->request->id); if (!$id) { self::returnJson(['success' => false, 'message' => 'Keine ID angegeben']); die(); } $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]); die(); } $order = WarehouseEShopOrderModel::get($id); $orderItems = WarehouseEShopOrderItemModel::getAll(['orderId' => $id]); $articles = WarehouseArticleModel::getAll(); $articlePackets = WarehouseArticlePacketModel::getAll(); $positions = []; foreach ($orderItems as $item) { $newEntry = []; $article = $item->articleId ? array_search($item->articleId, array_column($articles, 'id')) : null; $articlePacket = $item->articlePacketId ? array_search($item->articlePacketId, array_column($articlePackets, 'id')) : null; $articleTitle = $item->articleId ? $articles[$article]->title : $articlePackets[$articlePacket]->title; $quantity = $item->quantity; $price = 0; if ($item->articleId) { $cheapestSellPrice = json_decode($articles[$article]->cheapestSellPrice, true); foreach ($cheapestSellPrice as $price) { if ($price['title'] === 'Energie Steiermark') { $price = $price['price']; break; } } } else { $price = $articlePackets[$articlePacket]->overrideSellPrice ?? $articlePackets[$articlePacket]->calculatedSellPrice; } if ($item->articleId) { $newEntry['article'] = $item->articleId; } else { $newEntry['articlePacket'] = $item->articlePacketId; } $newEntry['amount'] = $quantity; $newEntry['price'] = $price; $positions[] = $newEntry; } $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]); self::returnJson(['success' => true, 'message' => 'Lieferschein wurde erstellt', 'shippingNoteId' => $shippingNoteId]); } protected function singleOrderEmailAction() { // this has dryRun and id in get parameter create a E-Mail Body like the things below $isDryRun = $this->request->dryRun ?? false; $id = $this->request->id; $order = WarehouseEShopOrderModel::get($id); $orderItems = WarehouseEShopOrderItemModel::getAll(['orderId' => $id]); $articles = WarehouseArticleModel::getAll(); $articlePackets = WarehouseArticlePacketModel::getAll(); $paddedId = str_pad($id, 3, '0', STR_PAD_LEFT); $body = "Bestellung #$paddedId\n\n"; $body .= "Lieferadresse:\n"; $body .= $order->deliveryAddressName . "\n"; $body .= $order->deliveryAddressLine . "\n"; $body .= $order->deliveryAddressPLZ . ' ' . $order->deliveryAddressCity . "\n\n"; $body .= "Bestellte Artikel:\n"; foreach ($orderItems as $item) { $article = $item->articleId ? array_search($item->articleId, array_column($articles, 'id')) : null; $articlePacket = $item->articlePacketId ? array_search($item->articlePacketId, array_column($articlePackets, 'id')) : null; $articleTitle = $item->articleId ? $articles[$article]->title : $articlePackets[$articlePacket]->title; $quantity = $item->quantity; $body .= "$quantity x $articleTitle\n"; } $body .= "\n\n"; $body .= "CSV der Bestellung ist im Anhang.\n\n"; $body .= "XINON GmbH\nFladnitz im Raabtal 150\n8322 Studenzen\n"; if ($isDryRun) { self::returnJson(['success' => true, 'message' => 'E-Mail Body wurde erstellt', 'body' => $body]); die(); } else { $csvContent = $this->CSVExportNewOrdersMarkAcceptedAction(true, [$id]); foreach (["ftth-versand@triotronik.com", "eshop-versand@xinon.at"] as $emailAddr) { $email = new Emailnotification(); $email->setSubject("Bestellbestätigung Bestellung #$paddedId"); $email->setBody($body); $email->setFrom(TT_OUTGOING_EMAIL_2FA, TT_OUTGOING_EMAIL_2FA); $email->setTo($emailAddr); $csvContent = utf8_decode($csvContent); $email->addAttachment(null, $csvContent, "Bestellung_$paddedId.csv", "text/csv", "attachment", "base64", "utf-8"); $email->send(); } self::returnJson(['success' => true, 'message' => 'E-Mail wurde versendet']); die(); } } protected function CSVExportNewOrdersMarkAcceptedAction($returnCSV = false, $orderIds = []) { $orders = WarehouseEShopOrderModel::getAll(['status' => 'new']); $orders = array_map(function ($order) { return (array) $order; }, $orders); if (!empty($orderIds)) { $orders = array_filter($orders, function ($order) use ($orderIds) { // use for each loop and use intval for order id and orderIds $found = false; foreach ($orderIds as $orderId) { if (intval($order['id']) === intval($orderId)) { $found = true; break; } } return $found; }); } if (empty($orders)) { $msg = ''; switch (count($orderIds)) { case 0: $msg = 'Keine neuen Bestellungen'; break; case 1: $msg = 'Keine neue Bestellung mit dieser ID gefunden'; break; default: $msg = 'Keine neuen Bestellungen mit diesen IDs gefunden'; break; } self::returnJson(['success' => false, 'message' => $msg]); die(); } $ordersItems = $this->getAllOrderItemsPerOrder(); $rows = []; foreach ($orders as $order) { $realOrderItems = null; foreach ($ordersItems as $orderId => $items) { if ($orderId == $order['id']) { $realOrderItems = $items; break; } } // if it is still null, die with order id: if ($realOrderItems === null) { self::returnJson(['success' => false, 'message' => 'Bestellung mit ID ' . $order['id'] . ' hat keine Artikel. Bitte überprüfen.']); die(); } $orderItems = $realOrderItems; $orderItemsStr = join('; ', array_map(function ($item) { $articleTitle = $item['articleTitle'] ?? $item['articlePacketTitle']; $quantity = $item['quantity']; return "$quantity x $articleTitle"; }, $orderItems)); $rows[] = ['AddressNumber' => '23000539', 'Name' => $order['deliveryAddressName'], 'Straße' => $order['deliveryAddressLine'], 'Postleitzahl' => $order['deliveryAddressPLZ'], 'Ort' => $order['deliveryAddressCity'], 'Land' => 'AT', 'Anschriftenzusatz 1' => '', '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()]); $order['status'] = 'accepted'; WarehouseEShopOrderModel::update($order); } if ($returnCSV === true) { return Helper::arrayToCsv($rows); } die(json_encode($rows)); self::returnJson($rows); return; } protected function getAllOrderItemsPerOrder(): array { if (isset($_GET['orderId'])) return WarehouseEShopOrderItemModel::getAll(['orderId' => $_GET['orderId']]); else $items = WarehouseEShopOrderItemModel::getAll(); $articles = WarehouseArticleModel::getAll(); $articlePackets = WarehouseArticlePacketModel::getAll(); $orderItems = []; foreach ($items as $item) { $item = (array) $item; if (!isset($orderItems[$item['orderId']])) { $orderItems[$item['orderId']] = []; } $article = $item['articleId'] ? array_search($item['articleId'], array_column($articles, 'id')) : null; $articlePacket = $item['articlePacketId'] ? array_search($item['articlePacketId'], array_column($articlePackets, 'id')) : null; $orderItems[$item['orderId']][] = ['id' => $item['id'], 'articleId' => $item['articleId'], 'articleTitle' => isset($articles[$article]) ? $articles[$article]->title : null, 'articlePacketId' => $item['articlePacketId'], 'articlePacketTitle' => isset($articlePackets[$articlePacket]) ? $articlePackets[$articlePacket]->title : null, 'quantity' => $item['quantity']]; } return $orderItems; } protected function getAllItemsPerOrderAction() { $orderItems = $this->getAllOrderItemsPerOrder(); self::returnJson($orderItems); } protected function createOrderAction() { //TODO: change this to beforeCreate and afterCreate $json = json_decode(file_get_contents('php://input'), true); $shoppingCart = $json['shoppingCart']; unset($json['shoppingCart']); $json['status'] = 'new'; $json['create'] = time(); $json['createBy'] = $this->user->id; Helper::validateArray($json, $this->getCheckArray()); $id = WarehouseEShopOrderModel::create(['status' => 'new', 'extRef' => $json['extRef'], 'deliveryMode' => $json['deliveryMode'], 'deliveryAddressName' => $json['deliveryAddressName'], 'deliveryAddressLine' => $json['deliveryAddressLine'], 'deliveryAddressPLZ' => $json['deliveryAddressPLZ'], 'deliveryAddressCity' => $json['deliveryAddressCity'], 'create' => $json['create'], 'createBy' => $json['createBy'],]); // now create WarehouseEShopOrderItems for each item in the shopping cart foreach ($shoppingCart as $item) { // itemId can either be P-[PACKETID] or I-[ARTICLEID] // 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']),]); } else if (strpos($item['itemId'], 'I-') === 0) { WarehouseEShopOrderItemModel::create(['orderId' => $id, 'articleId' => intval(substr($item['itemId'], 2)), 'quantity' => intval($item['amount']),]); } else { self::returnJson(['success' => false, 'message' => 'Invalid item id']); die(); } } // send confirmation email $subjectId = str_pad($id, 3, '0', STR_PAD_LEFT); // create email body with all items (use $shoppingCart) $body = "Bestellung #$subjectId\n\nIhre Referenz: " . $json['extRef'] . "\n\n"; $body .= "Lieferadresse:\n"; $body .= $json['deliveryAddressName'] . "\n"; $body .= $json['deliveryAddressLine'] . "\n"; $body .= $json['deliveryAddressPLZ'] . ' ' . $json['deliveryAddressCity'] . "\n\n"; $body .= "Bestellte Artikel:\n"; foreach ($shoppingCart as $item) { $body .= $item['amount'] . ' x ' . $item['title'] . "\n"; } $body .= "\n\n"; $body .= "Bestellung wurde nun zur weitere Bearbeitung angenommen.\n"; $body .= "XINON GmbH"; $user = UserModel::getOne($json['createBy']); foreach (["eshop-versand@xinon.at", $user->email] as $emailAddr) { $email = new Emailnotification(); $email->setSubject("Bestellbestätigung Bestellung #$subjectId - Referenz: " . $json['extRef']); $email->setBody($body); $email->setFrom(TT_OUTGOING_EMAIL_2FA, TT_OUTGOING_EMAIL_2FA); $email->setTo($emailAddr); $email->send(); } self::returnJson(['success' => true, 'message' => $this->infoMessages['create'], 'id' => $id]); $json['id'] = $id; die(json_encode($json)); } protected function beforeCreate(): bool { unset($this->postData['shippingNoteStatus']); return true; } protected function beforeUpdate($postData): bool { unset($this->postData['shippingNoteStatus']); (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 fetchGLSTrackingAction() { $orderId = $this->request->id; $order = WarehouseEShopOrderModel::get($orderId); $trackingNumber = $order->trackingNumber; $plz = $order->deliveryAddressPLZ; $tracking = $this->GLSTrackingApi($trackingNumber, $plz); $this->checkAndUpdateTrackingState($tracking, $orderId); die($tracking); } protected function GLSTrackingApi($trackingNumber, $plz) { $url = "https://gls-group.eu/app/service/open/rest/AT/de/rstt028/$trackingNumber?postalCode=$plz"; return file_get_contents($url); } protected function checkAndUpdateTrackingState($tracking, $orderId) { // check inside the tracking json if data.history[xxx].evtDscr (lowercase) contains "erfolgreich zugestellt" $tracking = json_decode($tracking, true); $history = $tracking['history']; $successfulDelivery = false; foreach ($history as $entry) { if (strpos(strtolower($entry['evtDscr']), 'erfolgreich zugestellt') !== false) { $successfulDelivery = true; break; } } if ($successfulDelivery) { $order = (array) WarehouseEShopOrderModel::get($orderId); $order['status'] = 'done'; WarehouseEShopOrderModel::update($order); } } protected function readGLSEmailAction() { function decode_utf8($str) { # paterns $err = "(=\?.{10,13}q\?_?|\?\=)"; $pat = "/=([0-9A-F]{2})/"; $cha = "'.chr(hexdec("; # erase null signs in string $str = str_replace("\x00", "", $str); # to decode with eval and replace eval("\$str='" . preg_replace($pat, $cha . "'$1')).'", $str) . "';"); # return return $str; } $host = '{mail.xinon.at:993/imap/ssl/novalidate-cert}INBOX'; $mbox = imap_open($host, 'eshop-versand@xinon.at', 'savemanfb545aw'); $emails = imap_search($mbox, 'ALL'); if ($emails) { // Sort emails from newest to oldest rsort($emails); // Loop through each email foreach ($emails as $email_number) { $overview = imap_fetch_overview($mbox, $email_number, 0); if (strpos($overview[0]->from, 'gls') === false) { continue; } $message = imap_fetchbody($mbox, $email_number, 1); // 1 for plain text part // START ADDRESS PARSING $address = ''; $lines = explode("\n", $message); $start = false; foreach ($lines as $line) { if (strpos($line, 'ZUSTELLADRESSE') !== false) { $start = true; } if ($start) { $address .= $line . "\n"; } if ($start && preg_match('/[a-zA-Z]/', $line) === 0) { break; } } $address = substr($address, strpos($address, "\n") + 1); $addressLines = explode("\n", $address); $addressLine = trim(array_shift($addressLines)); $plzCity = trim(array_shift($addressLines)); $plzCityParts = explode(' ', $plzCity); $plz = $plzCityParts[0]; if (!isset($plzCityParts[1])) { continue; } echo "---------------------------------------\n\n"; $city = $plzCityParts[1]; // convert special characters to normal ones because i get J=C3=B6ss instead of Jöss $addressLine = decode_utf8($addressLine); $city = decode_utf8($city); echo "Found address: $addressLine, $plz, $city" . PHP_EOL; // END ADDRESS PARSING // START TRACKING NUMBER PARSING // $trackingNumber = ''; // preg_match('/\d{6,}/', $message, $matches); // if (!empty($matches)) { // // if addrline includes 27 // if (strpos($addressLine, '27') !== false) { // var_dump($matches); // die($message); // } // // $trackingNumber = $matches[0]; // } // grab tracking number from https://gls-group.eu/track/53158006143 (example) from url preg_match('/track\/(\d+)/', $message, $matches); if (!empty($matches)) { $trackingNumber = $matches[1]; } echo "Found tracking number: $trackingNumber" . PHP_EOL; // END TRACKING NUMBER PARSING $orders = WarehouseEShopOrderModel::getAll(['deliveryAddressLine' => $addressLine, 'deliveryAddressPLZ' => $plz, 'deliveryAddressCity' => $city]); if (empty($orders)) { echo "No order found with address: $addressLine, $plz, $city" . PHP_EOL; continue; } // now check if the trackingNumber is already set and if not set it and create a history entry $order = (array) $orders[0]; echo "Found order with address: " . $order['id'] . PHP_EOL; // if ($order['trackingNumber']) { continue; } $order['trackingNumber'] = $trackingNumber; 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')]); // echo "Subject: " . $overview[0]->subject . "\n"; // echo "From: " . $overview[0]->from . "\n"; // echo "Date: " . $overview[0]->date . "\n"; // echo "Message:\n" . $message . "\n\n"; } } else { echo "No emails found."; } // Close the IMAP connection imap_close($mbox); die(); // do without ssl $mbox = imap_open("{mail.xino.at:143/novalidate-cert}INBOX", "eshop-versand@xinon.at", "savemanfb545aw"); // print all mails in mailbox with content $mails = imap_search($mbox, 'ALL'); $mails = array_reverse($mails); $mails = array_slice($mails, 0, 10); $result = []; } protected function updatePositionAction() { $json = json_decode(file_get_contents('php://input'), true); $id = intval($json['id']); $articleId = $json['articleId'] ? intval($json['articleId']) : null; $articlePacketId = $json['articlePacketId'] ? intval($json['articlePacketId']) : null; $quantity = intval($json['quantity']); if (!$id || !$quantity) { self::returnJson(['success' => false, 'message' => 'Ungültige Daten']); die(); } $position = (array) WarehouseEShopOrderItemModel::get($id); if (!$position) { self::returnJson(['success' => false, 'message' => 'Position nicht gefunden']); die(); } $position['articleId'] = $articleId; $position['articlePacketId'] = $articlePacketId; $position['quantity'] = $quantity; WarehouseEShopOrderItemModel::update($position); self::returnJson(['success' => true, 'position' => $position]); } protected function deletePositionAction() { $json = json_decode(file_get_contents('php://input'), true); $id = intval($json['id']); if (!$id) { self::returnJson(['success' => false, 'message' => 'Keine ID angegeben']); die(); } $position = WarehouseEShopOrderItemModel::get($id); if (!$position) { self::returnJson(['success' => false, 'message' => 'Position nicht gefunden']); die(); } WarehouseEShopOrderItemModel::delete($id); self::returnJson(['success' => true, 'message' => 'Position gelöscht']); } protected function addPositionAction() { $json = json_decode(file_get_contents('php://input'), true); $orderId = intval($json['orderId']); $articleId = $json['articleId'] ? intval($json['articleId']) : null; $articlePacketId = $json['articlePacketId'] ? intval($json['articlePacketId']) : null; $quantity = intval($json['quantity']); if (!$orderId || !$quantity) { self::returnJson(['success' => false, 'message' => 'Ungültige Daten']); die(); } $position = WarehouseEShopOrderItemModel::create(['orderId' => $orderId, 'articleId' => $articleId, 'articlePacketId' => $articlePacketId, 'quantity' => $quantity]); $position = (array) WarehouseEShopOrderItemModel::get($position); self::returnJson(['success' => true, 'position' => $position]); } }