diff --git a/app/Abstracts/Listeners/Report.php b/app/Abstracts/Listeners/Report.php index ffcfe8955..90af9521a 100644 --- a/app/Abstracts/Listeners/Report.php +++ b/app/Abstracts/Listeners/Report.php @@ -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) diff --git a/app/Abstracts/Report.php b/app/Abstracts/Report.php index ba63c189d..7a060173d 100644 --- a/app/Abstracts/Report.php +++ b/app/Abstracts/Report.php @@ -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 = '') diff --git a/app/Traits/DateTime.php b/app/Traits/DateTime.php index 6da414121..8a34f319b 100644 --- a/app/Traits/DateTime.php +++ b/app/Traits/DateTime.php @@ -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'; } diff --git a/app/Traits/Permissions.php b/app/Traits/Permissions.php index b5b30520f..f86db8fa5 100644 --- a/app/Traits/Permissions.php +++ b/app/Traits/Permissions.php @@ -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'); diff --git a/app/Traits/SearchString.php b/app/Traits/SearchString.php new file mode 100644 index 000000000..6fb508e7f --- /dev/null +++ b/app/Traits/SearchString.php @@ -0,0 +1,40 @@ +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; + } +}