Skip to content

Commit

Permalink
Merge pull request #9 from ryanmitchell/v2
Browse files Browse the repository at this point in the history
Merge V2 into master
  • Loading branch information
ryanmitchell authored Jun 16, 2021
2 parents e4d49f9 + 20be83e commit 87200b8
Show file tree
Hide file tree
Showing 27 changed files with 2,223 additions and 24 deletions.
40 changes: 32 additions & 8 deletions Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,56 @@ public function boot()

}

public function registerPermissions()
public function registerFormWidgets()
{
return [
'Thoughtco.Reports.View' => [
'description' => 'View reports',
'group' => 'module',
'Thoughtco\Reports\FormWidgets\QueryBuilder' => [
'label' => 'lang:thoughtco.reports::default.qb.text_title',
'code' => 'querybuilder',
],
'Thoughtco\Reports\FormWidgets\ReportsTable' => [
'label' => 'lang:thoughtco.reports::default.qb.text_reportstable',
'code' => 'reportstable',
],
];
}

public function registerNavigation()
{
return [
'sales' => [
'reports' => [
'icon' => 'fa-chart-pie',
'title' => lang('lang:thoughtco.reports::default.text_title'),
'priority' => 35,
'child' => [
'reports' => [
'priority' => 10,
'dashboard' => [
'priority' => 5,
'class' => 'pages',
'href' => admin_url('thoughtco/reports/dashboard'),
'title' => lang('lang:thoughtco.reports::default.text_title'),
'title' => lang('lang:thoughtco.reports::default.text_dashboard_title'),
'permission' => 'Thoughtco.Reports.View',
],
'builder' => [
'priority' => 5,
'class' => 'pages',
'href' => admin_url('thoughtco/reports/builder'),
'title' => lang('lang:thoughtco.reports::default.text_builder_title'),
'permission' => 'Thoughtco.Reports.View',
],
],
],
];
}

public function registerPermissions()
{
return [
'Thoughtco.Reports.View' => [
'description' => 'View reports',
'group' => 'module',
],
];
}

}

Expand Down
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,81 @@
## Reports

Adds a report view to the sales menu allowing high level stats on a date range.
Add reporting and data interrogation to your TastyIgniter install.

### Installation

1. Add to extensions/thoughtco/reports inside Tasty Igniter install.
2. Enable extension in Systems -> Extensions

## Usage

After installation a new Reports menu will be added to the admin sidebar, giving links to the Reports Dashboard and Query Builder.

### Dashboard

The Dashboard is a widget based view giving some high level overviews of reports in a configurable date range. Widgets can be changed, moved, and deleted in a similar way to the main admin dashboard.

### Query Builder
The query builder allows you to generate and save custom queries that create an exportable, searchable table of data. Once set up this queries can be accessed at will with data auto-updated.

## Extending

This extension has been designed to allow extending of the Querybuilder queries and fields.

### Adding rule options

Through your extension add rules by listening for the core `admin.form.extendFieldsBefore` event. For example:

```php
Event::listen('admin.form.extendFieldsBefore', function (Form $form) {

if ($form->model instanceof \Thoughtco\Reports\Models\QueryBuilder) {

$form->fields['builderjson']['filters']['\Admin\Models\Customers_model']['filters'][] = [
'id' => 'customers.loyaltypoints',
'label' => 'Loyalty Points',
'type' => 'integer',
'operators' => [
'equal', 'not_equal',
'less', 'less_or_equal',
'greater', 'greater_or_equal',
],
];

}

});
```

### Extending where fields

Through your extension add rules by listening for the core `thoughtco.reports.fieldToQuery` event, and apply logic on the basis of the $field and $controller . For example:

```php
Event::listen('thoughtco.reports.fieldToQuery', function ($controller, $query, $field, $operator, $value, $condition) {

if (!in_array(get_class($query->getModel()), ['Admin\Models\Customers_model']))
return;

if ($field == 'customers.loyaltypoints') {
$query->where('loyalty_points', $operator, $value);
}

});
```

### Extending overall query

Through your extension add rules by listening for the core `extendQuery` event, and apply logic on the basis of the $query, $modelName and $controller . For example:

```php
Event::listen('thoughtco.reports.extendQuery', function($controller, $query, $modelName) {

// we only care about certain models by default - this allows others to extend
if (!in_array($modelName, ['\Admin\Models\Orders_model', '\Admin\Models\Customers_model']))
return;

$query->selectRaw('*, CONCAT(first_name, " ", last_name) as customer_name');

});
```
21 changes: 21 additions & 0 deletions assets/js/editing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

+function ($) {
"use strict"

var $modelEl = $('#form-field-querybuilder-builderjson-type');
var $initialValue = $modelEl.val();

var checkContexts = function() {
var $context = $modelEl.val();
$('.repeater-items select option')
.each(function($idx, $el) {
let $val = JSON.parse($el.value);
if (!$val.contexts.includes($context))
$el.disabled = true;
});
};

$modelEl.on('change', checkContexts);
$(window).on('repeaterItemAdded', checkContexts);

}(window.jQuery)
27 changes: 15 additions & 12 deletions classes/ReportsCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Admin\Models\Locations_model;
use Admin\Models\Menus_model;
use Admin\Models\Orders_model;
use Admin\Models\Payments_model;
use Carbon\Carbon;
use DB;
use Igniter\Flame\Currency;
Expand Down Expand Up @@ -36,7 +37,7 @@ public static function get(String $param, $default = [])
private static function buildCache()
{

$locationModel = AdminLocation::getId() ? Locations_model::find(AdminLocation::getId()) : Locations_model::first();
$locationModel = AdminLocation::getId() ? Locations_model::find(AdminLocation::getId()) : false;

$startDate = Request::get('start_date', strtotime('-1 month'));
$endDate = Request::get('end_date', strtotime('today'));
Expand Down Expand Up @@ -71,9 +72,12 @@ private static function buildCache()

// get order ids for the time period
$orders = Orders_model::whereBetween('order_date', [$startDate->format('Y-m-d'), $endDate->format('Y-m-d')])
->where('location_id', $locationModel->getKey())
->whereIn('status_id', $statusesToQuery)
->get();
->whereIn('status_id', $statusesToQuery);

if ($locationModel)
$orders->where('location_id', $locationModel->getKey());

$orders = $orders->get();

// cancelled order stats
$cancelledOrders = $orders->filter(function($order) {
Expand Down Expand Up @@ -318,14 +322,13 @@ private static function buildCache()
];

// get payment methods for this location
$paymentMethods = $locationModel
->listAvailablePayments()
->map(function($method) {
return (object)[
'name' => $method->name,
'code' => $method->code,
];
});
$paymentMethods = ($locationModel ? $locationModel->listAvailablePayments() : Payments_model::all())
->map(function($method) {
return (object)[
'name' => $method->name,
'code' => $method->code,
];
});

$paymentMethodCount = $paymentMethods->count();
$paymentMethodIndex = 0;
Expand Down
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "thoughtco/reports",
"type": "tastyigniter-extension",
"keywords": ["tastyigniter", "reports"],
"require": {
"league/csv": "^9.0"
}
}
Loading

0 comments on commit 87200b8

Please sign in to comment.