From a9c54a8d80bce97e359d38a63ced3412d7390847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Arag=C3=B3n?= Date: Mon, 4 Nov 2019 12:42:02 -0500 Subject: [PATCH] adding functionality to export sales report in XLS #3020 --- plugin/buycourses/lang/brazilian.php | 2 + plugin/buycourses/lang/english.php | 2 + plugin/buycourses/lang/french.php | 2 + plugin/buycourses/lang/spanish.php | 3 + .../src/buy_course_plugin.class.php | 92 +++++++++++++++++++ plugin/buycourses/src/export_report.php | 63 +++++++++++++ plugin/buycourses/src/sales_report.php | 13 ++- plugin/buycourses/view/export_report.tpl | 1 + 8 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 plugin/buycourses/src/export_report.php create mode 100644 plugin/buycourses/view/export_report.tpl diff --git a/plugin/buycourses/lang/brazilian.php b/plugin/buycourses/lang/brazilian.php index 2dc9fb66d7..f6939a59f2 100644 --- a/plugin/buycourses/lang/brazilian.php +++ b/plugin/buycourses/lang/brazilian.php @@ -94,3 +94,5 @@ $strings['ByUser'] = "Por usuário"; $strings['PaymentMethod'] = "Método de pagamento"; $strings['SWIFT'] = "Código SWIFT"; $strings['SWIFT_help'] = "Formato padrão de Códigos de Identificação Bancária (BIC) e serve como um identificador exclusivo de um banco ou instituição financeira"; +$strings['ExportReport'] = "Exportar Relatório de Vendas"; +$strings['SelectDateRange'] = "Selecione uma data de início e uma data de término para o relatório"; diff --git a/plugin/buycourses/lang/english.php b/plugin/buycourses/lang/english.php index 887a11c6e0..9b0f518be0 100644 --- a/plugin/buycourses/lang/english.php +++ b/plugin/buycourses/lang/english.php @@ -189,3 +189,5 @@ $strings['SubscriptionToServiceXSuccessful'] = "Subscription to service %s compl $strings['ClickHereToFinish'] = "Click here to finish"; $strings['OrderCancelled'] = "Order cancelled"; $strings['use_currency_symbol'] = "Use currency symbol"; +$strings['ExportReport'] = "Export Sales Report"; +$strings['SelectDateRange'] = "Select a start date and end date for the report"; diff --git a/plugin/buycourses/lang/french.php b/plugin/buycourses/lang/french.php index 5931920271..edb76d8a92 100644 --- a/plugin/buycourses/lang/french.php +++ b/plugin/buycourses/lang/french.php @@ -161,3 +161,5 @@ $strings['BoughtBy'] = "Acheté par"; $strings['PurchaserUser'] = "Utilisateur acheteur"; $strings['Pending'] = "En attente"; $strings['Names'] = "Nom"; +$strings['ExportReport'] = "Rapport des ventes à l'exportation"; +$strings['SelectDateRange'] = "Sélectionnez une date de début et une date de fin pour le rapport"; \ No newline at end of file diff --git a/plugin/buycourses/lang/spanish.php b/plugin/buycourses/lang/spanish.php index 8943deeb78..e7b14d4c44 100644 --- a/plugin/buycourses/lang/spanish.php +++ b/plugin/buycourses/lang/spanish.php @@ -210,3 +210,6 @@ $strings['PurchaseDetailsEnd'] = "Atentamente"; $strings['ProductName'] = "Nombre producto"; $strings['BankAccountIntro'] = "Información cuentas bancarias"; $strings['CurrencyIsNotConfigured'] = "Por favor, confogure una moneda antes de continuar."; +$strings['ExportReport'] = "Exportar reporte de ventas"; +$strings['OrderTime'] = "Hora de pedido"; +$strings['SelectDateRange'] = "Seleccione una fecha de inicio y fecha de fin para el reporte"; \ No newline at end of file diff --git a/plugin/buycourses/src/buy_course_plugin.class.php b/plugin/buycourses/src/buy_course_plugin.class.php index e61a209413..ed7711735c 100644 --- a/plugin/buycourses/src/buy_course_plugin.class.php +++ b/plugin/buycourses/src/buy_course_plugin.class.php @@ -1278,6 +1278,98 @@ class BuyCoursesPlugin extends Plugin ); } + /** + * Get the list statuses for sales. + * + * @param null $dateStart + * @param null $dateEnd + * @return array + */ + public function getSaleListReport($dateStart = null, $dateEnd = null) + { + $saleTable = Database::get_main_table(self::TABLE_SALE); + $currencyTable = Database::get_main_table(self::TABLE_CURRENCY); + $userTable = Database::get_main_table(TABLE_MAIN_USER); + $innerJoins = " + INNER JOIN $currencyTable c ON s.currency_id = c.id + INNER JOIN $userTable u ON s.user_id = u.id + "; + $list = Database::select( + ['c.iso_code', 'u.firstname', 'u.lastname', 'u.email' , 's.*'], + "$saleTable s $innerJoins", + [ + 'order' => 'id DESC', + ] + ); + $listExportTemp = []; + $listExport = []; + $textStatus = null; + $paymentTypes = $this->getPaymentTypes(); + $productTypes = $this->getProductTypes(); + foreach($list as $item){ + $statusSaleOrder = $item['status']; + switch ($statusSaleOrder){ + case 0: + $textStatus = $this->get_lang('SaleStatusPending'); + break; + case 1: + $textStatus = $this->get_lang('SaleStatusCompleted'); + break; + case -1: + $textStatus = $this->get_lang('SaleStatusCanceled'); + break; + } + $dateFilter = new DateTime($item['date']); + $listExportTemp[] = [ + 'id' => $item['id'], + 'reference' => $item['reference'], + 'status' => $textStatus, + 'status_filter' => $item['status'], + 'date' => $dateFilter->format('Y-m-d'), + 'order_time' => $dateFilter->format('H:i:s'), + 'price' => $item['iso_code'].' '.$item['price'], + 'product_type' => $productTypes[$item['product_type']], + 'product_name' => $item['product_name'], + 'payment_type' => $paymentTypes[$item['payment_type']], + 'complete_user_name' => api_get_person_name($item['firstname'], $item['lastname']), + 'email' => $item['email'], + ]; + } + $listExport[] = [ + get_lang('Number'), + $this->get_lang('OrderStatus'), + $this->get_lang('OrderDate'), + $this->get_lang('OrderTime'), + $this->get_lang('PaymentMethod'), + $this->get_lang('SalePrice'), + $this->get_lang('ProductType'), + $this->get_lang('ProductName'), + $this->get_lang('UserName'), + get_lang('Email') + ]; + //Validation Export + $dateStart = strtotime($dateStart); + $dateEnd = strtotime($dateEnd); + foreach ($listExportTemp as $item){ + $dateFilter = strtotime($item['date']); + if(($dateFilter >= $dateStart) && ($dateFilter <= $dateEnd)){ + $listExport[] = [ + 'id' => $item['id'], + 'status' => $item['status'], + 'date' => $item['date'], + 'order_time' => $item['order_time'], + 'payment_type' => $item['payment_type'], + 'price' => $item['price'], + 'product_type' => $item['product_type'], + 'product_name' => $item['product_name'], + 'complete_user_name' => $item['complete_user_name'], + 'email' => $item['email'], + ]; + } + } + return $listExport; + } + /** * Get the statuses for sales. * diff --git a/plugin/buycourses/src/export_report.php b/plugin/buycourses/src/export_report.php new file mode 100644 index 0000000000..ad31001d67 --- /dev/null +++ b/plugin/buycourses/src/export_report.php @@ -0,0 +1,63 @@ +addDatePicker('date_start', get_lang('DateStart'), false); +$form->addDatePicker('date_end', get_lang('DateEnd'), false); +$form->addButton('export_sales',get_lang('ExportExcel'),'check','primary'); +$salesStatus = []; + +if ($form->validate()) { + $reportValues = $form->getSubmitValues(); + + $dateStart = $reportValues['date_start']; + $dateEnd = $reportValues['date_end']; + + if($dateStart == null || $dateEnd == null){ + Display::addFlash( + Display::return_message($plugin->get_lang('SelectDateRange'),'error', false) + ); + } else if($dateStart>$dateEnd) { + Display::addFlash( + Display::return_message(get_lang('EndDateCannotBeBeforeTheStartDate'),'error', false) + ); + } else { + $salesStatus = $plugin->getSaleListReport($dateStart,$dateEnd); + } +} + +if(!empty($salesStatus)){ + $archiveFile = 'export_report_sales_'.api_get_local_time(); + Export::arrayToXls($salesStatus, $archiveFile); +} +$interbreadcrumb[] = [ + 'url' => '../index.php', 'name' => $plugin->get_lang('plugin_title') +]; +$interbreadcrumb[] = [ + 'url' => api_get_path(WEB_PLUGIN_PATH).'buycourses/src/sales_report.php', + 'name' => $plugin->get_lang('SalesReport') +]; + +$templateName = $plugin->get_lang('ExportReport'); +$toolbar = Display::url( + Display::return_icon('back.png',get_lang('GoBack'),[],ICON_SIZE_MEDIUM), + api_get_path(WEB_PLUGIN_PATH).'buycourses/src/sales_report.php' +); +$template = new Template($templateName); +$template->assign( + 'actions', + Display::toolbarAction('toolbar', [$toolbar]) +); +$template->assign('form', $form->returnForm()); +$content = $template->fetch('buycourses/view/export_report.tpl'); +$template->assign('header', $templateName); +$template->assign('content', $content); +$template->display_one_col_template(); \ No newline at end of file diff --git a/plugin/buycourses/src/sales_report.php b/plugin/buycourses/src/sales_report.php index 5bbecd4696..1a4f801450 100644 --- a/plugin/buycourses/src/sales_report.php +++ b/plugin/buycourses/src/sales_report.php @@ -124,7 +124,14 @@ foreach ($sales as &$sale) { $interbreadcrumb[] = ['url' => '../index.php', 'name' => $plugin->get_lang('plugin_title')]; $templateName = $plugin->get_lang('SalesReport'); $template = new Template($templateName); -$toolbar = ''; + +$toolbar = Display::url( + Display::returnFontAwesomeIcon('file-excel-o'). + get_lang('GenerateReport'), + api_get_path(WEB_PLUGIN_PATH).'buycourses/src/export_report.php', + ['class' => 'btn btn-primary'] +); + if ($paypalEnable === 'true' && $commissionsEnable === 'true') { $toolbar .= Display::toolbarButton( $plugin->get_lang('PaypalPayoutCommissions'), @@ -154,6 +161,10 @@ if ($commissionsEnable === 'true') { Display::toolbarAction('toolbar', [$toolbar]) ); } +$template->assign( + 'actions', + Display::toolbarAction('toolbar', [$toolbar]) +); $template->assign('form', $form->returnForm()); $template->assign('selected_sale', $selectedSale); $template->assign('selected_status', $selectedStatus); diff --git a/plugin/buycourses/view/export_report.tpl b/plugin/buycourses/view/export_report.tpl new file mode 100644 index 0000000000..f9a859ae9f --- /dev/null +++ b/plugin/buycourses/view/export_report.tpl @@ -0,0 +1 @@ +{{ form }}