diff --git a/modules/eksporthistorii/controllers/admin/AdminEksportHistoriiController.php b/modules/eksporthistorii/controllers/admin/AdminEksportHistoriiController.php new file mode 100644 index 0000000..b22d201 --- /dev/null +++ b/modules/eksporthistorii/controllers/admin/AdminEksportHistoriiController.php @@ -0,0 +1,144 @@ +bootstrap = true; + parent::__construct(); + } + + public function initContent() + { + if (Tools::isSubmit('submitExportHistory')) { + $this->processExport(); + return; + } + + parent::initContent(); + + $this->content = $this->renderExportForm(); + + $this->context->smarty->assign([ + 'content' => $this->content, + ]); + } + + protected function renderExportForm() + { + $action = self::$currentIndex . '&token=' . $this->token; + $action = Tools::safeOutput($action); + + $html = ' +
+

' . $this->l('Eksport historii zamówień') . '

+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + +
+
+ '; + + return $html; + } + + /** + * CSV + */ + public function processExport() + { + $date_from = Tools::getValue('date_from'); + $date_to = Tools::getValue('date_to'); + + if (empty($date_from) || empty($date_to)) { + die($this->l('Obie daty są wymagane.')); + } + + // początek / koniec dnia + $date_from .= ' 00:00:00'; + $date_to .= ' 23:59:59'; + + // Zapytanie SQL: zamówienia + klient + produkty + $sql = new DbQuery(); + $sql->select(' + o.id_order AS order_id, + c.id_customer AS client_id, + c.email, + c.firstname AS first_name, + c.lastname AS last_name, + od.product_id, + od.product_name + '); + $sql->from('orders', 'o'); + $sql->innerJoin('customer', 'c', 'c.id_customer = o.id_customer'); + $sql->innerJoin('order_detail', 'od', 'od.id_order = o.id_order'); + $sql->where('o.date_add BETWEEN "' . pSQL($date_from) . '" AND "' . pSQL($date_to) . '"'); + $sql->orderBy('o.id_order ASC'); + + $rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); + + $filename = 'orders_export_' . date('Y-m-d_His') . '.csv'; + + header('Content-Type: text/csv; charset=utf-8'); + header('Content-Disposition: attachment; filename="' . $filename . '"'); + header('Cache-Control: no-store, no-cache, must-revalidate'); + header('Pragma: no-cache'); + + $output = fopen('php://output', 'w'); + + fprintf($output, chr(0xEF) . chr(0xBB) . chr(0xBF)); + + fputcsv( + $output, + ['order_id', 'client_id', 'email', 'first_name', 'last_name', 'product_id', 'product_name'], + ';' + ); + + if (!empty($rows)) { + foreach ($rows as $row) { + fputcsv( + $output, + [ + $row['order_id'], + $row['client_id'], + $row['email'], + $row['first_name'], + $row['last_name'], + $row['product_id'], + $row['product_name'], + ], + ';' + ); + } + } + + fclose($output); + exit; + } +} diff --git a/modules/eksporthistorii/eksporthistorii.php b/modules/eksporthistorii/eksporthistorii.php new file mode 100644 index 0000000..404cc13 --- /dev/null +++ b/modules/eksporthistorii/eksporthistorii.php @@ -0,0 +1,80 @@ +name = 'eksporthistorii'; + $this->tab = 'administration'; + $this->version = '1.0.0'; + $this->author = 'Custom'; + $this->need_instance = 0; + $this->bootstrap = true; + + parent::__construct(); + + $this->displayName = $this->l('Eksport historii zamówień'); + $this->description = $this->l('Eksport historii zamówień do pliku Excel (CSV).'); + } + + public function install() + { + return parent::install() + && $this->installTab(); + } + + public function uninstall() + { + return $this->uninstallTab() + && parent::uninstall(); + } + + /** + * Dodanie zakładki jako podmenu w "Zamówienia" + */ + protected function installTab() + { + // rodzic: Orders / Zamówienia + $id_parent = (int) Tab::getIdFromClassName('AdminParentOrders'); + + $tab = new Tab(); + $tab->active = 1; + $tab->class_name = 'AdminEksportHistorii'; + $tab->name = []; + + foreach (Language::getLanguages(false) as $lang) { + $tab->name[$lang['id_lang']] = 'Eksport historii'; + } + + $tab->id_parent = $id_parent; + $tab->module = $this->name; + + return (bool) $tab->add(); + } + + /** + * Usunięcie zakładki przy deinstalacji + */ + protected function uninstallTab() + { + // Usuwamy naszą aktualną zakładkę + $id_tab = (int) Tab::getIdFromClassName('AdminEksportHistorii'); + if ($id_tab) { + $tab = new Tab($id_tab); + $tab->delete(); + } + + // Sprzątamy ewentualną starą zakładkę (ze starego modułu) + $old_id_tab = (int) Tab::getIdFromClassName('AdminEksportowanieIstorii'); + if ($old_id_tab) { + $old_tab = new Tab($old_id_tab); + $old_tab->delete(); + } + + return true; + } +} diff --git a/modules/eksporthistorii/index.php b/modules/eksporthistorii/index.php new file mode 100644 index 0000000..6028c7f --- /dev/null +++ b/modules/eksporthistorii/index.php @@ -0,0 +1,34 @@ + + * @copyright 2007-2020 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + * International Registered Trademark & Property of PrestaShop SA + */ +header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); +header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../'); +exit;