From 27e4f6cd82ec6fa93e84acbe32e79e0a12fb9631 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Fri, 2 Aug 2024 10:40:15 +0200 Subject: [PATCH] switched csv export to excel export --- .../WarehouseEShopOrderController.php | 23 ++++++++----- .../WarehouseEShopOrder.js | 34 ++++++++++++++----- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/application/WarehouseEShopOrder/WarehouseEShopOrderController.php b/application/WarehouseEShopOrder/WarehouseEShopOrderController.php index 9e3014b13..2533d5018 100644 --- a/application/WarehouseEShopOrder/WarehouseEShopOrderController.php +++ b/application/WarehouseEShopOrder/WarehouseEShopOrderController.php @@ -60,8 +60,8 @@ class WarehouseEShopOrderController extends TTCrud { $orderItems[$item['orderId']] = []; } - $article = array_search($item['articleId'], array_column($articles, 'id')); - $articlePacket = array_search($item['articlePacketId'], array_column($articlePackets, 'id')); + $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'], @@ -147,18 +147,26 @@ class WarehouseEShopOrderController extends TTCrud { } $ordersItems = $this->getAllOrderItemsPerOrder(); + $rows = []; - // Create CSV with these Headers: Adressnummer, Name, Straße, Postleitzahl, Ort, Land, Anschriftenzusatz 1 - $csv = "Adressnummer,Name,Straße,Postleitzahl,Ort,Land,Anschriftenzusatz 1,Produkte\n"; foreach ($orders as $order) { $orderItems = $ordersItems[$order['id']]; - $orderItemsStr = join(' : ', array_map(function($item) { + $orderItemsStr = join('; ', array_map(function($item) { $articleTitle = $item['articleTitle'] ?? $item['articlePacketTitle']; $quantity = $item['quantity']; return "$quantity x $articleTitle"; }, $orderItems)); - $csv .= '23000539,' . $order['deliveryAddressName'] . ',' . $order['deliveryAddressLine'] . ',' . $order['deliveryAddressPLZ'] . ',' . $order['deliveryAddressCity'] . ',AT,,'. $orderItemsStr . "\n"; + $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', @@ -173,10 +181,9 @@ class WarehouseEShopOrderController extends TTCrud { $order['status'] = 'accepted'; WarehouseEShopOrderModel::update($order); - } - die($csv); + self::returnJson($rows); } protected function beforeUpdate($postData): bool { diff --git a/public/js/pages/WarehouseEShopOrder/WarehouseEShopOrder.js b/public/js/pages/WarehouseEShopOrder/WarehouseEShopOrder.js index 891756cf6..5e8bc1878 100644 --- a/public/js/pages/WarehouseEShopOrder/WarehouseEShopOrder.js +++ b/public/js/pages/WarehouseEShopOrder/WarehouseEShopOrder.js @@ -8,7 +8,7 @@ Vue.component('warehouse-e-shop-order', { @@ -48,16 +48,32 @@ Vue.component('warehouse-e-shop-order', { return; } - const blob = new Blob([response.data], {type: 'text/csv charset=utf-8;'}); - const url = window.URL.createObjectURL(blob); + await new Promise((resolve, reject) => { + const script = document.createElement('script'); + script.src = '/plugins/xlsx/xlsx.min.js'; + script.onload = resolve; + script.onerror = reject; + document.head.appendChild(script); + }) - const link = document.createElement("a"); - link.setAttribute("href", url); - link.setAttribute("download", "Export.csv"); - document.body.appendChild(link); // Required for FF + const wb = this.window.XLSX.utils.book_new(); - link.click(); // This will download the data file named "my_data.csv". - link.remove(); + const ws = this.window.XLSX.utils.json_to_sheet(response.data); + + for (const cell in ws) { + if (cell.startsWith('!')) continue; // Skip non-cell properties like '!ref' + const cellValue = ws[cell].v; + if (cellValue?.toString().includes('\n')) { + // console.log('Found newline in cell:', cell, cellValue); + if (!ws[cell].s) ws[cell].s = {}; + ws[cell].s.alignment = {wrapText: true, vertical: 'center'}; + } else { + ws[cell].s = {alignment: {vertical: 'center'}}; + } + } + + this.window.XLSX.utils.book_append_sheet(wb, ws, "Export"); + this.window.XLSX.writeFile(wb, 'Export.xlsx'); await this.$refs.table.$refs.table.fetchData(); }