Browse Source

Fix Pending Orders pages - refs #7768

Angel Fernando Quiroz Campos 9 years ago
parent
commit
3d1c8d23d9

+ 52 - 0
plugin/buycourses/src/buy_course_plugin.class.php

@@ -909,4 +909,56 @@ class BuyCoursesPlugin extends Plugin
         ];
     }
 
+    /**
+     * Get a list of sales by the status
+     * @param type $status
+     * @return type
+     */
+    public function getSaleListByStatus($status = self::SALE_STATUS_PENDING)
+    {
+        $saleTable = Database::get_main_table(BuyCoursesUtils::TABLE_SALE);
+        $currencyTable = Database::get_main_table(BuyCoursesUtils::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
+        ";
+
+        return Database::select(
+            ['c.iso_code', 'u.firstname', 'u.lastname', 's.*'],
+            "$saleTable s $innerJoins",
+            [
+                'where' => ['s.status = ?' => intval($status)]
+            ]
+        );
+    }
+
+    /**
+     * Get the statuses for sales
+     * @return array
+     */
+    public function getSaleStatuses()
+    {
+        return [
+            self::SALE_STATUS_CANCELED => $this->get_lang('SaleCanceled'),
+            self::SALE_STATUS_PENDING => $this->get_lang('SalePending'),
+            self::SALE_STATUS_COMPLETED => $this->get_lang('SaleCompleted')
+        ];
+    }
+
+    /**
+     * Get the list of product types
+     * @return array
+     */
+    public function getProductTypes()
+    {
+        return [
+            self::PRODUCT_TYPE_COURSE => get_lang('Course'),
+            self::PRODUCT_TYPE_SESSION => get_lang('Session')
+        ];
+    }
+
 }

+ 105 - 29
plugin/buycourses/src/pending_orders.php

@@ -4,37 +4,113 @@
  * List of pending payments of the Buy Courses plugin
  * @package chamilo.plugin.buycourses
  */
-/**
- * Initialization
- */
+//Initialization
+$cidReset = true;
+
 require_once '../config.php';
 require_once dirname(__FILE__) . '/buy_course.lib.php';
 
+api_protect_admin_script();
+
 $plugin = BuyCoursesPlugin::create();
-$_cid = 0;
-$tableName = $plugin->get_lang('AvailableCoursesConfiguration');
-$interbreadcrumb[] = array("url" => "list.php", "name" => $plugin->get_lang('CourseListOnSale'));
-$interbreadcrumb[] = array("url" => "paymentsetup.php", "name" => $plugin->get_lang('PaymentsConfiguration'));
-
-$tpl = new Template($tableName);
-
-$teacher = api_is_platform_admin();
-api_protect_admin_script(true);
-
-if ($teacher) {
-    $pendingList = pendingList($_SESSION['bc_codetext']);
-    $confirmationImg = api_get_path(WEB_PLUGIN_PATH) . 'buycourses/resources/message_confirmation.png';
-    $deleteImg = api_get_path(WEB_PLUGIN_PATH) . 'buycourses/resources/delete.png';
-    $currencyType = findCurrency();
-
-    $tpl->assign('server', $_configuration['root_web']);
-    $tpl->assign('pending', $pendingList);
-    $tpl->assign('confirmation_img', $confirmationImg);
-    $tpl->assign('delete_img', $deleteImg);
-    $tpl->assign('currency', $currencyType);
-
-    $listing_tpl = 'buycourses/view/pending_orders.tpl';
-    $content = $tpl->fetch($listing_tpl);
-    $tpl->assign('content', $content);
-    $tpl->display_one_col_template();
+
+if (isset($_GET['order'])) {
+    $sale = $plugin->getSale($_GET['order']);
+
+    if (empty($sale)) {
+        api_not_allowed(true);
+    }
+
+    $urlToRedirect = api_get_self() . '?';
+
+    switch ($_GET['action']) {
+        case 'confirm':
+            $plugin->completeSale($sale['id']);
+
+            Display::addFlash(
+                Display::return_message(
+                    sprintf($plugin->get_lang('SubscriptionToCourseXSuccessful'), $sale['product_name']),
+                    'success'
+                )
+            );
+
+            $urlToRedirect .= http_build_query([
+                'status' => BuyCoursesPlugin::SALE_STATUS_COMPLETED,
+                'sale' =>  $sale['id']
+            ]);
+            break;
+        case 'cancel':
+            $plugin->cancelSale($sale['id']);
+
+            Display::addFlash(
+                Display::return_message(
+                    $plugin->get_lang('OrderCanceled'),
+                    'warning'
+                )
+            );
+
+            $urlToRedirect .= http_build_query([
+                'status' => BuyCoursesPlugin::SALE_STATUS_CANCELED,
+                'sale' =>  $sale['id']
+            ]);
+            break;
+    }
+
+    header("Location: $urlToRedirect");
+    exit;
 }
+
+$productTypes = $plugin->getProductTypes();
+$saleStatuses = $plugin->getSaleStatuses();
+$selectedStatus = isset($_GET['status']) ? $_GET['status'] : BuyCoursesPlugin::SALE_STATUS_PENDING;
+$selectedSale = isset($_GET['sale']) ? intval($_GET['sale']) : 0;
+
+$form = new FormValidator('search', 'get');
+
+if ($form->validate()) {
+    $selectedStatus = $form->getSubmitValue('status');
+
+    if ($selectedStatus === false) {
+        $selectedStatus = BuyCoursesPlugin::SALE_STATUS_PENDING;
+    }
+}
+
+$form->addSelect('status', $plugin->get_lang('OrderStatus'), $saleStatuses);
+$form->addButtonFilter($plugin->get_lang('SearchByStatus'));
+$form->setDefaults(['status' => $selectedStatus]);
+
+$sales = $plugin->getSaleListByStatus($selectedStatus);
+$saleList = [];
+
+foreach ($sales as $sale) {
+    $saleList[] = [
+        'id' => $sale['id'],
+        'status' => $sale['status'],
+        'date' => api_format_date($sale['date'], DATE_FORMAT_LONG_NO_DAY),
+        'currency' => $sale['iso_code'],
+        'price' => $sale['price'],
+        'product_name' => $sale['product_name'],
+        'product_type' => $productTypes[$sale['product_type']],
+        'complete_user_name' => api_get_person_name($sale['firstname'], $sale['lastname'])
+    ];
+}
+
+//View
+$interbreadcrumb[] = ['url' => '../index.php', 'name' => $plugin->get_lang('plugin_title')];
+
+$templateName = $plugin->get_lang('SalesReport');
+
+$template = new Template($templateName);
+$template->assign('form', $form->returnForm());
+$template->assign('selected_sale', $selectedSale);
+$template->assign('selected_status', $selectedStatus);
+$template->assign('sale_list', $saleList);
+$template->assign('sale_status_canceled', BuyCoursesPlugin::SALE_STATUS_CANCELED);
+$template->assign('sale_status_pending', BuyCoursesPlugin::SALE_STATUS_PENDING);
+$template->assign('sale_status_completed', BuyCoursesPlugin::SALE_STATUS_COMPLETED);
+
+$content = $template->fetch('buycourses/view/pending_orders.tpl');
+
+$template->assign('header', $templateName);
+$template->assign('content', $content);
+$template->display_one_col_template();

+ 1 - 1
plugin/buycourses/view/index.tpl

@@ -70,7 +70,7 @@
                             <img src="resources/img/128/backlogs.png">
                         </a>
                         <div class="caption">
-                            <a class="btn btn-default btn-sm" href="src/pending_orders.php"> {{ 'OrdersPendingOfPayment'|get_plugin_lang('BuyCoursesPlugin') }} </a>
+                            <a class="btn btn-default btn-sm" href="src/pending_orders.php"> {{ 'SalesReport'|get_plugin_lang('BuyCoursesPlugin') }} </a>
                         </div>
                     </div>
                 </div>

+ 44 - 32
plugin/buycourses/view/pending_orders.tpl

@@ -1,39 +1,51 @@
-<script type='text/javascript' src="../js/buycourses.js"></script>
+{{ form }}
 
-<link rel="stylesheet" type="text/css" href="../resources/plugin.css"/>
-
-<div class="row">
-    <div class="span12">
-        <table id="orders_table" class="data_table">
-            <tr class="row_odd">
-                <th class="ta-center">{{ 'ReferenceOrder'|get_plugin_lang('BuyCoursesPlugin') }}</th>
+<div class="table-responsive">
+    <table class="table table-striped table-hover">
+        <thead>
+            <tr>
+                <th class="text-center">{{ 'OrderStatus'|get_plugin_lang('BuyCoursesPlugin') }}</th>
+                <th class="text-center">{{ 'OrderDate'|get_plugin_lang('BuyCoursesPlugin') }}</th>
+                <th class="text-center">{{ 'Price'|get_plugin_lang('BuyCoursesPlugin') }}</th>
+                <th class="text-center">{{ 'ProductType'|get_plugin_lang('BuyCoursesPlugin') }}</th>
                 <th>{{ 'Name'|get_lang }}</th>
-                <th>{{ 'Title'|get_lang }}</th>
-                <th class="span2">{{ 'Price'|get_lang }}</th>
-                <th class="ta-center">{{ 'Date'|get_lang }}</th>
-                <th class="span2 ta-center">{{ 'Options'|get_lang }}</th>
+                <th>{{ 'UserName'|get_lang }}</th>
+                {% if selected_status == sale_status_pending %}
+                    <th class="text-center">{{ 'Options'|get_lang }}</th>
+                {% endif %}
             </tr>
-            {% set i = 0 %}
-
-            {% for order in pending %}
-                {{ i%2==0 ? '<tr class="row_even">' : '<tr class="row_odd">' }}
-                    {% set i = i + 1 %}
-                    <td class="ta-center">{{ order.reference }}</td>
-                    <td>{{ order.name }}</td>
-                    <td>{{ order.title }}</td>
-                    <td>{{ order.price }} {{ currency }}</td>
-                    <td class="ta-center">{{ order.date }}</td>
-                    <td class="ta-center" id="order{{ order.cod }}">
-                        <img src="{{ confirmation_img }}" alt="ok" class="cursor confirm_order"
-                             title="{{ 'SubscribeUser'|get_plugin_lang('BuyCoursesPlugin') }}"/>
-                        &nbsp;&nbsp;
-                        <img src="{{ delete_img }}" alt="delete" class="cursor clear_order"
-                             title="{{ 'DeleteTheOrder'|get_plugin_lang('BuyCoursesPlugin') }}"/>
+        </thead>
+        <tbody>
+            {% for sale in sale_list %}
+                <tr {{ sale.id == selected_sale ? 'class="warning"' : '' }}>
+                    <td class="text-center">
+                        {% if sale.status == sale_status_canceled %}
+                            {{ 'SaleCanceled'|get_plugin_lang('BuyCoursesPlugin') }}
+                        {% elseif sale.status == sale_status_pending %}
+                            {{ 'SalePending'|get_plugin_lang('BuyCoursesPlugin') }}
+                        {% elseif sale.status == sale_status_completed %}
+                            {{ 'SaleCompleted'|get_plugin_lang('BuyCoursesPlugin') }}
+                        {% endif %}
                     </td>
+                    <td class="text-center">{{ sale.date }}</td>
+                    <td class="text-right">{{ sale.currency ~ ' ' ~ sale.price }}</td>
+                    <td class="text-center">{{ sale.product_type }}</td>
+                    <td>{{ sale.product_name }}</td>
+                    <td>{{ sale.complete_user_name }}</td>
+                    {% if selected_status == sale_status_pending %}
+                        <td class="text-center">
+                            {% if sale.status == sale_status_pending %}
+                                <a href="{{ _p.web_self ~ '?' ~ {'order': sale.id, 'action': 'confirm'}|url_encode() }}" class="btn btn-success btn-sm">
+                                    <i class="fa fa-user-plus fa-fw"></i> {{ 'SubscribeUser'|get_plugin_lang('BuyCoursesPlugin') }}
+                                </a>
+                                <a href="{{ _p.web_self ~ '?' ~ {'order': sale.id, 'action': 'cancel'}|url_encode() }}" class="btn btn-danger btn-sm">
+                                    <i class="fa fa-times fa-fw"></i> {{ 'DeleteOrder'|get_plugin_lang('BuyCoursesPlugin') }}
+                                </a>
+                            {% endif %}
+                        </td>
+                    {% endif %}
                 </tr>
             {% endfor %}
-
-        </table>
-    </div>
-    <div class="cleared"></div>
+        </tbody>
+    </table>
 </div>