Browse Source

Add filter by user on sales report - refs #7768

Angel Fernando Quiroz Campos 9 years ago
parent
commit
edf83bdc0d

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

@@ -1127,4 +1127,40 @@ class BuyCoursesPlugin extends Plugin
         );
     }
 
+    /**
+     * Get a list of sales by the user
+     * @param string $term The search term
+     * @return array The sale list. Otherwise return false
+     */
+    public function getSaleListByUser($term)
+    {
+        $term = trim($term);
+
+        if (empty($term)) {
+            return [];
+        }
+
+        $saleTable = Database::get_main_table(BuyCoursesPlugin::TABLE_SALE);
+        $currencyTable = Database::get_main_table(BuyCoursesPlugin::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' => [
+                    'u.username LIKE %?% OR ' => $term,
+                    'u.lastname LIKE %?% OR ' => $term,
+                    'u.firstname LIKE %?%' => $term
+                ],
+                'order' => 'id DESC'
+            ]
+        );
+    }
+
 }

+ 32 - 2
plugin/buycourses/src/sales_report.php

@@ -61,24 +61,54 @@ if (isset($_GET['order'])) {
 
 $productTypes = $plugin->getProductTypes();
 $saleStatuses = $plugin->getSaleStatuses();
+
+$selectedFilterType = '0';
 $selectedStatus = isset($_GET['status']) ? $_GET['status'] : BuyCoursesPlugin::SALE_STATUS_PENDING;
 $selectedSale = isset($_GET['sale']) ? intval($_GET['sale']) : 0;
+$searchTerm = '';
 
 $form = new FormValidator('search', 'get');
 
 if ($form->validate()) {
+    $selectedFilterType = $form->getSubmitValue('filter_type');
     $selectedStatus = $form->getSubmitValue('status');
+    $searchTerm = $form->getSubmitValue('user');
 
     if ($selectedStatus === false) {
         $selectedStatus = BuyCoursesPlugin::SALE_STATUS_PENDING;
     }
+
+    if ($selectedFilterType === false) {
+        $selectedFilterType = '0';
+    }
 }
 
+$form->addRadio(
+    'filter_type',
+    get_lang('FilterBy'),
+    [$plugin->get_lang('ByStatus'), $plugin->get_lang('ByUser')]
+);
+$form->addHtml('<div id="report-by-status" ' . ($selectedFilterType !== '0' ? 'style="display:none"' : '') . '>');
 $form->addSelect('status', $plugin->get_lang('OrderStatus'), $saleStatuses);
+$form->addHtml('</div>');
+$form->addHtml('<div id="report-by-user" ' . ($selectedFilterType !== '1' ? 'style="display:none"' : '') . '>');
+$form->addText('user', get_lang('UserName'), false);
+$form->addHtml('</div>');
 $form->addButtonFilter($plugin->get_lang('SearchByStatus'));
-$form->setDefaults(['status' => $selectedStatus]);
+$form->setDefaults([
+    'filter_type' => $selectedFilterType,
+    'status' => $selectedStatus
+]);
+
+switch ($selectedFilterType) {
+    case '0':
+        $sales = $plugin->getSaleListByStatus($selectedStatus);
+        break;
+    case '1':
+        $sales = $plugin->getSaleListByUser($searchTerm);
+        break;
+}
 
-$sales = $plugin->getSaleListByStatus($selectedStatus);
 $saleList = [];
 
 foreach ($sales as $sale) {

+ 16 - 4
plugin/buycourses/view/sales_report.tpl

@@ -11,9 +11,7 @@
                 <th class="text-center">{{ 'ProductType'|get_plugin_lang('BuyCoursesPlugin') }}</th>
                 <th>{{ 'Name'|get_lang }}</th>
                 <th>{{ 'UserName'|get_lang }}</th>
-                {% if selected_status == sale_status_pending %}
                     <th class="text-center">{{ 'Options'|get_lang }}</th>
-                {% endif %}
             </tr>
         </thead>
         <tbody>
@@ -34,7 +32,6 @@
                     <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">
@@ -45,9 +42,24 @@
                                 </a>
                             {% endif %}
                         </td>
-                    {% endif %}
                 </tr>
             {% endfor %}
         </tbody>
     </table>
 </div>
+
+<script>
+    $(document).on('ready', function () {
+        $('[name="filter_type"]').on('change', function () {
+            var self = $(this);
+
+            if (self.val() === '0') {
+                $('#report-by-user').hide();
+                $('#report-by-status').show();
+            } else {
+                $('#report-by-status').hide();
+                $('#report-by-user').show();
+            }
+        });
+    });
+</script>