fixed year filter in reports
This commit is contained in:
parent
4c73a19b12
commit
9c0cc9ae87
@ -7,11 +7,12 @@ use App\Models\Common\Contact;
|
||||
use App\Models\Setting\Category;
|
||||
use App\Traits\Contacts;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\SearchString;
|
||||
use Date;
|
||||
|
||||
abstract class Report
|
||||
{
|
||||
use Contacts, DateTime;
|
||||
use Contacts, DateTime, SearchString;
|
||||
|
||||
protected $classes = [];
|
||||
|
||||
@ -102,7 +103,13 @@ abstract class Report
|
||||
|
||||
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)
|
||||
|
@ -10,18 +10,18 @@ use App\Events\Report\GroupApplying;
|
||||
use App\Events\Report\GroupShowing;
|
||||
use App\Events\Report\RowsShowing;
|
||||
use App\Exports\Common\Reports as Export;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Models\Common\Report as Model;
|
||||
use App\Models\Document\Document;
|
||||
use App\Traits\Charts;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\SearchString;
|
||||
use App\Utilities\Chartjs;
|
||||
use Date;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
abstract class Report
|
||||
{
|
||||
use Charts, DateTime;
|
||||
use Charts, DateTime, SearchString;
|
||||
|
||||
public $model;
|
||||
|
||||
@ -234,7 +234,7 @@ abstract class Report
|
||||
|
||||
public function setYear()
|
||||
{
|
||||
$this->year = request('year', Date::now()->year);
|
||||
$this->year = $this->getSearchStringValue('year', Date::now()->year);
|
||||
}
|
||||
|
||||
public function setViews()
|
||||
@ -270,7 +270,7 @@ abstract class Report
|
||||
|
||||
$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++) {
|
||||
switch ($period) {
|
||||
@ -390,13 +390,13 @@ abstract class Report
|
||||
$i = $date->copy()->format($this->getYearlyDateFormat());
|
||||
break;
|
||||
case 'quarterly':
|
||||
$start = $date->copy()->startOfQuarter()->format($this->getQuarterlyDateFormat());
|
||||
$end = $date->copy()->endOfQuarter()->format($this->getQuarterlyDateFormat());
|
||||
$start = $date->copy()->startOfQuarter()->format($this->getQuarterlyDateFormat($this->year));
|
||||
$end = $date->copy()->endOfQuarter()->format($this->getQuarterlyDateFormat($this->year));
|
||||
|
||||
$i = $start . '-' . $end;
|
||||
break;
|
||||
default:
|
||||
$i = $date->copy()->format($this->getMonthlyDateFormat());
|
||||
$i = $date->copy()->format($this->getMonthlyDateFormat($this->year));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -405,21 +405,15 @@ abstract class Report
|
||||
|
||||
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) {
|
||||
$print_url .= '&accounts[]=' . $item;
|
||||
});
|
||||
$search = request('search');
|
||||
|
||||
collect(request('customers'))->each(function($item) use (&$print_url) {
|
||||
$print_url .= '&customers[]=' . $item;
|
||||
});
|
||||
if (!empty($search)) {
|
||||
$url .= '?search=' . $search;
|
||||
}
|
||||
|
||||
collect(request('categories'))->each(function($item) use (&$print_url) {
|
||||
$print_url .= '&categories[]=' . $item;
|
||||
});
|
||||
|
||||
return $print_url;
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function getSetting($name, $default = '')
|
||||
|
@ -2,10 +2,13 @@
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Traits\SearchString;
|
||||
use Date;
|
||||
|
||||
trait DateTime
|
||||
{
|
||||
use SearchString;
|
||||
|
||||
/*
|
||||
* Get the date format based on company settings.
|
||||
* getDateFormat method is used by Eloquent
|
||||
@ -37,19 +40,15 @@ trait DateTime
|
||||
public function scopeMonthsOfYear($query, $field)
|
||||
{
|
||||
$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
|
||||
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');
|
||||
$end = Date::parse($year . '-12-31')->endOfDay()->format('Y-m-d H:i:s');
|
||||
} else {
|
||||
if (!is_null(request('year'))) {
|
||||
$financial_start->year = $year;
|
||||
}
|
||||
|
||||
$start = $financial_start->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;
|
||||
}
|
||||
|
||||
public function getFinancialStart()
|
||||
public function getFinancialStart($year = null)
|
||||
{
|
||||
$now = Date::now();
|
||||
$start = Date::now()->startOfYear();
|
||||
@ -106,7 +105,7 @@ trait DateTime
|
||||
|
||||
$day = !empty($setting[0]) ? $setting[0] : $start->day;
|
||||
$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);
|
||||
|
||||
@ -118,22 +117,22 @@ trait DateTime
|
||||
return $financial_start;
|
||||
}
|
||||
|
||||
public function getMonthlyDateFormat()
|
||||
public function getMonthlyDateFormat($year = null)
|
||||
{
|
||||
$format = 'M';
|
||||
|
||||
if ($this->getFinancialStart()->month != 1) {
|
||||
if ($this->getFinancialStart($year)->month != 1) {
|
||||
$format = 'M Y';
|
||||
}
|
||||
|
||||
return $format;
|
||||
}
|
||||
|
||||
public function getQuarterlyDateFormat()
|
||||
public function getQuarterlyDateFormat($year = null)
|
||||
{
|
||||
$format = 'M';
|
||||
|
||||
if ($this->getFinancialStart()->month != 1) {
|
||||
if ($this->getFinancialStart($year)->month != 1) {
|
||||
$format = 'M Y';
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ namespace App\Traits;
|
||||
|
||||
use App\Models\Auth\Permission;
|
||||
use App\Models\Auth\Role;
|
||||
use App\Traits\SearchString;
|
||||
use App\Utilities\Reports;
|
||||
use App\Utilities\Widgets;
|
||||
use Illuminate\Routing\Route;
|
||||
@ -11,6 +12,8 @@ use Illuminate\Support\Str;
|
||||
|
||||
trait Permissions
|
||||
{
|
||||
use SearchString;
|
||||
|
||||
public function getActionsMap()
|
||||
{
|
||||
return [
|
||||
@ -405,21 +408,10 @@ trait Permissions
|
||||
|
||||
// Find the proper controller for common API endpoints
|
||||
if (in_array($table, ['contacts', 'documents', 'transactions'])) {
|
||||
$controller = $type = '';
|
||||
$controller = '';
|
||||
|
||||
// Look for type in search variable like api/contacts?search=type:customer
|
||||
$queries = explode(' ', request()->get('search'));
|
||||
foreach ($queries as $query) {
|
||||
$tmp = explode(':', $query);
|
||||
|
||||
if (empty($tmp[0]) || ($tmp[0] != 'type') || empty($tmp[1])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$type = $tmp[1];
|
||||
|
||||
break;
|
||||
}
|
||||
$type = $this->getSearchStringValue('type');
|
||||
|
||||
if (!empty($type)) {
|
||||
$alias = config('type.' . $type . '.alias');
|
||||
|
40
app/Traits/SearchString.php
Normal file
40
app/Traits/SearchString.php
Normal 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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user