fixed year filter in reports

This commit is contained in:
Denis Duliçi 2021-01-23 12:16:58 +03:00
parent 4c73a19b12
commit 9c0cc9ae87
5 changed files with 78 additions and 46 deletions

View File

@ -7,11 +7,12 @@ use App\Models\Common\Contact;
use App\Models\Setting\Category; use App\Models\Setting\Category;
use App\Traits\Contacts; use App\Traits\Contacts;
use App\Traits\DateTime; use App\Traits\DateTime;
use App\Traits\SearchString;
use Date; use Date;
abstract class Report abstract class Report
{ {
use Contacts, DateTime; use Contacts, DateTime, SearchString;
protected $classes = []; protected $classes = [];
@ -102,7 +103,13 @@ abstract class Report
public function applySearchStringFilter($event) public function applySearchStringFilter($event)
{ {
$event->model->usingSearchString(request('search')); $input = request('search');
// Remove year as it's handled based on financial start
$search_year = 'year:' . $this->getSearchStringValue('year', '', $input);
$input = str_replace($search_year, '', $input);
$event->model->usingSearchString($input);
} }
public function applyAccountGroup($event) public function applyAccountGroup($event)

View File

@ -10,18 +10,18 @@ use App\Events\Report\GroupApplying;
use App\Events\Report\GroupShowing; use App\Events\Report\GroupShowing;
use App\Events\Report\RowsShowing; use App\Events\Report\RowsShowing;
use App\Exports\Common\Reports as Export; use App\Exports\Common\Reports as Export;
use App\Models\Banking\Transaction;
use App\Models\Common\Report as Model; use App\Models\Common\Report as Model;
use App\Models\Document\Document; use App\Models\Document\Document;
use App\Traits\Charts; use App\Traits\Charts;
use App\Traits\DateTime; use App\Traits\DateTime;
use App\Traits\SearchString;
use App\Utilities\Chartjs; use App\Utilities\Chartjs;
use Date; use Date;
use Illuminate\Support\Str; use Illuminate\Support\Str;
abstract class Report abstract class Report
{ {
use Charts, DateTime; use Charts, DateTime, SearchString;
public $model; public $model;
@ -234,7 +234,7 @@ abstract class Report
public function setYear() public function setYear()
{ {
$this->year = request('year', Date::now()->year); $this->year = $this->getSearchStringValue('year', Date::now()->year);
} }
public function setViews() public function setViews()
@ -270,7 +270,7 @@ abstract class Report
$function = 'sub' . ucfirst(str_replace('ly', '', $period)); $function = 'sub' . ucfirst(str_replace('ly', '', $period));
$start = $this->getFinancialStart()->copy()->$function(); $start = $this->getFinancialStart($this->year)->copy()->$function();
for ($j = 1; $j <= 12; $j++) { for ($j = 1; $j <= 12; $j++) {
switch ($period) { switch ($period) {
@ -390,13 +390,13 @@ abstract class Report
$i = $date->copy()->format($this->getYearlyDateFormat()); $i = $date->copy()->format($this->getYearlyDateFormat());
break; break;
case 'quarterly': case 'quarterly':
$start = $date->copy()->startOfQuarter()->format($this->getQuarterlyDateFormat()); $start = $date->copy()->startOfQuarter()->format($this->getQuarterlyDateFormat($this->year));
$end = $date->copy()->endOfQuarter()->format($this->getQuarterlyDateFormat()); $end = $date->copy()->endOfQuarter()->format($this->getQuarterlyDateFormat($this->year));
$i = $start . '-' . $end; $i = $start . '-' . $end;
break; break;
default: default:
$i = $date->copy()->format($this->getMonthlyDateFormat()); $i = $date->copy()->format($this->getMonthlyDateFormat($this->year));
break; break;
} }
@ -405,21 +405,15 @@ abstract class Report
public function getUrl($action = 'print') public function getUrl($action = 'print')
{ {
$print_url = 'common/reports/' . $this->model->id . '/' . $action . '?year='. $this->year; $url = 'common/reports/' . $this->model->id . '/' . $action;
collect(request('accounts'))->each(function($item) use (&$print_url) { $search = request('search');
$print_url .= '&accounts[]=' . $item;
});
collect(request('customers'))->each(function($item) use (&$print_url) { if (!empty($search)) {
$print_url .= '&customers[]=' . $item; $url .= '?search=' . $search;
}); }
collect(request('categories'))->each(function($item) use (&$print_url) { return $url;
$print_url .= '&categories[]=' . $item;
});
return $print_url;
} }
public function getSetting($name, $default = '') public function getSetting($name, $default = '')

View File

@ -2,10 +2,13 @@
namespace App\Traits; namespace App\Traits;
use App\Traits\SearchString;
use Date; use Date;
trait DateTime trait DateTime
{ {
use SearchString;
/* /*
* Get the date format based on company settings. * Get the date format based on company settings.
* getDateFormat method is used by Eloquent * getDateFormat method is used by Eloquent
@ -37,19 +40,15 @@ trait DateTime
public function scopeMonthsOfYear($query, $field) public function scopeMonthsOfYear($query, $field)
{ {
$now = Date::now(); $now = Date::now();
$year = request('year', $now->year); $year = $this->getSearchStringValue('year', $now->year);
$financial_start = $this->getFinancialStart(); $financial_start = $this->getFinancialStart($year);
// Check if FS has been customized // Check if FS has been customized
if ($now->startOfYear()->format('Y-m-d') === $financial_start->format('Y-m-d')) { if ($now->startOfYear()->format('Y-m-d') === $financial_start->format('Y-m-d')) {
$start = Date::parse($year . '-01-01')->startOfDay()->format('Y-m-d H:i:s'); $start = Date::parse($year . '-01-01')->startOfDay()->format('Y-m-d H:i:s');
$end = Date::parse($year . '-12-31')->endOfDay()->format('Y-m-d H:i:s'); $end = Date::parse($year . '-12-31')->endOfDay()->format('Y-m-d H:i:s');
} else { } else {
if (!is_null(request('year'))) {
$financial_start->year = $year;
}
$start = $financial_start->format('Y-m-d H:i:s'); $start = $financial_start->format('Y-m-d H:i:s');
$end = $financial_start->addYear(1)->subDays(1)->format('Y-m-d H:i:s'); $end = $financial_start->addYear(1)->subDays(1)->format('Y-m-d H:i:s');
} }
@ -97,7 +96,7 @@ trait DateTime
return $groups; return $groups;
} }
public function getFinancialStart() public function getFinancialStart($year = null)
{ {
$now = Date::now(); $now = Date::now();
$start = Date::now()->startOfYear(); $start = Date::now()->startOfYear();
@ -106,7 +105,7 @@ trait DateTime
$day = !empty($setting[0]) ? $setting[0] : $start->day; $day = !empty($setting[0]) ? $setting[0] : $start->day;
$month = !empty($setting[1]) ? $setting[1] : $start->month; $month = !empty($setting[1]) ? $setting[1] : $start->month;
$year = request('year', $now->year); $year = $year ?? $this->getSearchStringValue('year', $now->year);
$financial_start = Date::create($year, $month, $day); $financial_start = Date::create($year, $month, $day);
@ -118,22 +117,22 @@ trait DateTime
return $financial_start; return $financial_start;
} }
public function getMonthlyDateFormat() public function getMonthlyDateFormat($year = null)
{ {
$format = 'M'; $format = 'M';
if ($this->getFinancialStart()->month != 1) { if ($this->getFinancialStart($year)->month != 1) {
$format = 'M Y'; $format = 'M Y';
} }
return $format; return $format;
} }
public function getQuarterlyDateFormat() public function getQuarterlyDateFormat($year = null)
{ {
$format = 'M'; $format = 'M';
if ($this->getFinancialStart()->month != 1) { if ($this->getFinancialStart($year)->month != 1) {
$format = 'M Y'; $format = 'M Y';
} }

View File

@ -4,6 +4,7 @@ namespace App\Traits;
use App\Models\Auth\Permission; use App\Models\Auth\Permission;
use App\Models\Auth\Role; use App\Models\Auth\Role;
use App\Traits\SearchString;
use App\Utilities\Reports; use App\Utilities\Reports;
use App\Utilities\Widgets; use App\Utilities\Widgets;
use Illuminate\Routing\Route; use Illuminate\Routing\Route;
@ -11,6 +12,8 @@ use Illuminate\Support\Str;
trait Permissions trait Permissions
{ {
use SearchString;
public function getActionsMap() public function getActionsMap()
{ {
return [ return [
@ -405,21 +408,10 @@ trait Permissions
// Find the proper controller for common API endpoints // Find the proper controller for common API endpoints
if (in_array($table, ['contacts', 'documents', 'transactions'])) { if (in_array($table, ['contacts', 'documents', 'transactions'])) {
$controller = $type = ''; $controller = '';
// Look for type in search variable like api/contacts?search=type:customer // Look for type in search variable like api/contacts?search=type:customer
$queries = explode(' ', request()->get('search')); $type = $this->getSearchStringValue('type');
foreach ($queries as $query) {
$tmp = explode(':', $query);
if (empty($tmp[0]) || ($tmp[0] != 'type') || empty($tmp[1])) {
continue;
}
$type = $tmp[1];
break;
}
if (!empty($type)) { if (!empty($type)) {
$alias = config('type.' . $type . '.alias'); $alias = config('type.' . $type . '.alias');

View File

@ -0,0 +1,40 @@
<?php
namespace App\Traits;
trait SearchString
{
/**
* Get the value of a name in search string
* Example: search=type:customer year:2020 account_id:20
*
* @return string
*/
public function getSearchStringValue($name, $default = '', $input = null)
{
$value = $default;
if (is_null($input)) {
$input = request('search');
}
//$manager = $this->getSearchStringManager();
//$parsed = $manager->parse($input);
$columns = explode(' ', $input);
foreach ($columns as $column) {
$variable = explode(':', $column);
if (empty($variable[0]) || ($variable[0] != $name) || empty($variable[1])) {
continue;
}
$value = $variable[1];
break;
}
return $value;
}
}