refactored report listeners

This commit is contained in:
denisdulici 2020-01-22 19:05:49 +03:00
parent c64dfdac1e
commit c208c336eb
12 changed files with 299 additions and 210 deletions

View File

@ -2,10 +2,6 @@
namespace App\Abstracts\Listeners; namespace App\Abstracts\Listeners;
use App\Events\Common\ReportFilterApplying;
use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportGroupApplying;
use App\Events\Common\ReportGroupShowing;
use App\Models\Banking\Account; use App\Models\Banking\Account;
use App\Models\Common\Contact; use App\Models\Common\Contact;
use App\Models\Setting\Category; use App\Models\Setting\Category;
@ -16,7 +12,7 @@ abstract class Report
{ {
use Contacts; use Contacts;
protected $class = ''; protected $classes = [];
protected $events = [ protected $events = [
'App\Events\Common\ReportFilterShowing', 'App\Events\Common\ReportFilterShowing',
@ -25,9 +21,9 @@ abstract class Report
'App\Events\Common\ReportGroupApplying', 'App\Events\Common\ReportGroupApplying',
]; ];
public function checkClass($event) public function skipThisClass($event)
{ {
return (get_class($event->class) == $this->class); return (empty($event->class) || !in_array(get_class($event->class), $this->classes));
} }
public function getYears() public function getYears()
@ -137,70 +133,6 @@ abstract class Report
} }
} }
/**
* Handle filter showing event.
*
* @param $event
* @return void
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
if (!$this->checkClass($event)) {
return;
}
$event->class->filters['years'] = $this->getYears();
}
/**
* Handle filter applying event.
*
* @param $event
* @return void
*/
public function handleReportFilterApplying(ReportFilterApplying $event)
{
if (!$this->checkClass($event)) {
return;
}
// Apply date
$this->applyDateFilter($event);
// Apply search
$this->applySearchStringFilter($event);
}
/**
* Handle group showing event.
*
* @param $event
* @return void
*/
public function handleReportGroupShowing(ReportGroupShowing $event)
{
if (!$this->checkClass($event)) {
return;
}
$event->class->groups['category'] = trans_choice('general.categories', 1);
}
/**
* Handle group applying event.
*
* @param $event
* @return void
*/
public function handleReportGroupApplying(ReportGroupApplying $event)
{
if (!$this->checkClass($event)) {
return;
}
$this->applyAccountGroup($event);
}
/** /**
* Register the listeners for the subscriber. * Register the listeners for the subscriber.
* *

View File

@ -3,14 +3,19 @@
namespace App\Listeners\Common; namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener; use App\Abstracts\Listeners\Report as Listener;
use App\Events\Common\ReportFilterApplying;
use App\Events\Common\ReportFilterShowing; use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportGroupApplying; use App\Events\Common\ReportGroupApplying;
use App\Events\Common\ReportGroupShowing; use App\Events\Common\ReportGroupShowing;
class ExpenseSummaryReport extends Listener class AddAccountsToReports extends Listener
{ {
protected $class = 'App\Reports\ExpenseSummary'; protected $classes = [
'App\Reports\IncomeSummary',
'App\Reports\ExpenseSummary',
'App\Reports\IncomeExpenseSummary',
'App\Reports\ProfitLoss',
'App\Reports\TaxSummary',
];
/** /**
* Handle filter showing event. * Handle filter showing event.
@ -20,14 +25,11 @@ class ExpenseSummaryReport extends Listener
*/ */
public function handleReportFilterShowing(ReportFilterShowing $event) public function handleReportFilterShowing(ReportFilterShowing $event)
{ {
if (!$this->checkClass($event)) { if ($this->skipThisClass($event)) {
return; return;
} }
$event->class->filters['years'] = $this->getYears();
$event->class->filters['accounts'] = $this->getAccounts(); $event->class->filters['accounts'] = $this->getAccounts();
$event->class->filters['categories'] = $this->getExpenseCategories();
$event->class->filters['vendors'] = $this->getVendors();
} }
/** /**
@ -38,28 +40,25 @@ class ExpenseSummaryReport extends Listener
*/ */
public function handleReportGroupShowing(ReportGroupShowing $event) public function handleReportGroupShowing(ReportGroupShowing $event)
{ {
if (!$this->checkClass($event)) { if ($this->skipThisClass($event)) {
return; return;
} }
$event->class->groups['category'] = trans_choice('general.categories', 1);
$event->class->groups['account'] = trans_choice('general.accounts', 1); $event->class->groups['account'] = trans_choice('general.accounts', 1);
$event->class->groups['vendor'] = trans_choice('general.vendors', 1);
} }
/** /**
* Handle group applyinh event. * Handle group applying event.
* *
* @param $event * @param $event
* @return void * @return void
*/ */
public function handleReportGroupApplying(ReportGroupApplying $event) public function handleReportGroupApplying(ReportGroupApplying $event)
{ {
if (!$this->checkClass($event)) { if ($this->skipThisClass($event)) {
return; return;
} }
$this->applyVendorGroup($event);
$this->applyAccountGroup($event); $this->applyAccountGroup($event);
} }
} }

View File

@ -3,14 +3,16 @@
namespace App\Listeners\Common; namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener; use App\Abstracts\Listeners\Report as Listener;
use App\Events\Common\ReportFilterApplying;
use App\Events\Common\ReportFilterShowing; use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportGroupApplying; use App\Events\Common\ReportGroupApplying;
use App\Events\Common\ReportGroupShowing; use App\Events\Common\ReportGroupShowing;
class IncomeExpenseSummaryReport extends Listener class AddCustomersToReports extends Listener
{ {
protected $class = 'App\Reports\IncomeExpenseSummary'; protected $classes = [
'App\Reports\IncomeSummary',
'App\Reports\IncomeExpenseSummary',
];
/** /**
* Handle filter showing event. * Handle filter showing event.
@ -20,15 +22,11 @@ class IncomeExpenseSummaryReport extends Listener
*/ */
public function handleReportFilterShowing(ReportFilterShowing $event) public function handleReportFilterShowing(ReportFilterShowing $event)
{ {
if (!$this->checkClass($event)) { if ($this->skipThisClass($event)) {
return; return;
} }
$event->class->filters['years'] = $this->getYears();
$event->class->filters['accounts'] = $this->getAccounts();
$event->class->filters['categories'] = $this->getIncomeExpenseCategories();
$event->class->filters['customers'] = $this->getCustomers(); $event->class->filters['customers'] = $this->getCustomers();
$event->class->filters['vendors'] = $this->getVendors();
} }
/** /**
@ -39,14 +37,11 @@ class IncomeExpenseSummaryReport extends Listener
*/ */
public function handleReportGroupShowing(ReportGroupShowing $event) public function handleReportGroupShowing(ReportGroupShowing $event)
{ {
if (!$this->checkClass($event)) { if ($this->skipThisClass($event)) {
return; return;
} }
$event->class->groups['category'] = trans_choice('general.categories', 1);
$event->class->groups['account'] = trans_choice('general.accounts', 1);
$event->class->groups['customer'] = trans_choice('general.customers', 1); $event->class->groups['customer'] = trans_choice('general.customers', 1);
$event->class->groups['vendor'] = trans_choice('general.vendors', 1);
} }
/** /**
@ -57,12 +52,10 @@ class IncomeExpenseSummaryReport extends Listener
*/ */
public function handleReportGroupApplying(ReportGroupApplying $event) public function handleReportGroupApplying(ReportGroupApplying $event)
{ {
if (!$this->checkClass($event)) { if ($this->skipThisClass($event)) {
return; return;
} }
$this->applyCustomerGroup($event); $this->applyCustomerGroup($event);
$this->applyVendorGroup($event);
$this->applyAccountGroup($event);
} }
} }

View File

@ -0,0 +1,49 @@
<?php
namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener;
use App\Events\Common\ReportFilterApplying;
use App\Events\Common\ReportFilterShowing;
class AddDateToReports extends Listener
{
protected $classes = [
'App\Reports\IncomeSummary',
'App\Reports\ExpenseSummary',
'App\Reports\IncomeExpenseSummary',
'App\Reports\ProfitLoss',
'App\Reports\TaxSummary',
];
/**
* Handle filter showing event.
*
* @param $event
* @return void
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
if ($this->skipThisClass($event)) {
return;
}
$event->class->filters['years'] = $this->getYears();
}
/**
* Handle filter applying event.
*
* @param $event
* @return void
*/
public function handleReportFilterApplying(ReportFilterApplying $event)
{
if ($this->skipThisClass($event)) {
return;
}
// Apply date
$this->applyDateFilter($event);
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener;
use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportGroupShowing;
class AddExpenseCategoriesToReports extends Listener
{
/**
* Handle filter showing event.
*
* @param $event
* @return void
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
$classes = [
'App\Reports\ExpenseSummary',
'App\Reports\IncomeExpenseSummary',
];
if (empty($event->class) || !in_array(get_class($event->class), $classes)) {
return;
}
$categories = !empty($event->class->filters['categories']) ? $event->class->filters['categories'] : [];
$event->class->filters['categories'] = array_merge($categories, $this->getExpenseCategories());
}
/**
* Handle group showing event.
*
* @param $event
* @return void
*/
public function handleReportGroupShowing(ReportGroupShowing $event)
{
$classes = [
'App\Reports\ExpenseSummary',
'App\Reports\IncomeExpenseSummary',
'App\Reports\ProfitLoss',
'App\Reports\TaxSummary',
];
if (empty($event->class) || !in_array(get_class($event->class), $classes)) {
return;
}
$event->class->groups['category'] = trans_choice('general.categories', 1);
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener;
use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportGroupShowing;
class AddIncomeCategoriesToReports extends Listener
{
/**
* Handle filter showing event.
*
* @param $event
* @return void
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
$classes = [
'App\Reports\IncomeSummary',
'App\Reports\IncomeExpenseSummary',
];
if (empty($event->class) || !in_array(get_class($event->class), $classes)) {
return;
}
$categories = !empty($event->class->filters['categories']) ? $event->class->filters['categories'] : [];
$event->class->filters['categories'] = array_merge($categories, $this->getIncomeCategories());
}
/**
* Handle group showing event.
*
* @param $event
* @return void
*/
public function handleReportGroupShowing(ReportGroupShowing $event)
{
$classes = [
'App\Reports\IncomeSummary',
'App\Reports\IncomeExpenseSummary',
'App\Reports\ProfitLoss',
'App\Reports\TaxSummary',
];
if (empty($event->class) || !in_array(get_class($event->class), $classes)) {
return;
}
$event->class->groups['category'] = trans_choice('general.categories', 1);
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener;
use App\Events\Common\ReportFilterApplying;
use App\Events\Common\ReportFilterShowing;
class AddSearchToReports extends Listener
{
protected $classes = [
'App\Reports\IncomeSummary',
'App\Reports\ExpenseSummary',
'App\Reports\IncomeExpenseSummary',
'App\Reports\ProfitLoss',
'App\Reports\TaxSummary',
];
/**
* Handle filter showing event.
*
* @param $event
* @return void
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
if ($this->skipThisClass($event)) {
return;
}
//$event->class->filters['search'] = $this->getSearch();
}
/**
* Handle filter applying event.
*
* @param $event
* @return void
*/
public function handleReportFilterApplying(ReportFilterApplying $event)
{
if ($this->skipThisClass($event)) {
return;
}
// Apply date
$this->applySearchStringFilter($event);
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener;
use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportGroupApplying;
use App\Events\Common\ReportGroupShowing;
class AddVendorsToReports extends Listener
{
protected $classes = [
'App\Reports\ExpenseSummary',
'App\Reports\IncomeExpenseSummary',
];
/**
* Handle filter showing event.
*
* @param $event
* @return void
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
if ($this->skipThisClass($event)) {
return;
}
$event->class->filters['vendors'] = $this->getVendors();
}
/**
* Handle group showing event.
*
* @param $event
* @return void
*/
public function handleReportGroupShowing(ReportGroupShowing $event)
{
if ($this->skipThisClass($event)) {
return;
}
$event->class->groups['vendor'] = trans_choice('general.vendors', 1);
}
/**
* Handle group applying event.
*
* @param $event
* @return void
*/
public function handleReportGroupApplying(ReportGroupApplying $event)
{
if ($this->skipThisClass($event)) {
return;
}
$this->applyVendorGroup($event);
}
}

View File

@ -1,84 +0,0 @@
<?php
namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener;
use App\Events\Common\ReportFilterApplying;
use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportGroupApplying;
use App\Events\Common\ReportGroupShowing;
class IncomeSummaryReport extends Listener
{
protected $class = 'App\Reports\IncomeSummary';
/**
* Handle filter showing event.
*
* @param $event
* @return void
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
if (!$this->checkClass($event)) {
return;
}
$event->class->filters['years'] = $this->getYears();
$event->class->filters['accounts'] = $this->getAccounts();
$event->class->filters['categories'] = $this->getIncomeCategories();
$event->class->filters['customers'] = $this->getCustomers();
}
/**
* Handle filter applying event.
*
* @param $event
* @return void
*/
public function handleReportFilterApplying(ReportFilterApplying $event)
{
if (!$this->checkClass($event)) {
return;
}
// Apply date
$this->applyDateFilter($event);
// Apply search
$this->applySearchStringFilter($event);
}
/**
* Handle group showing event.
*
* @param $event
* @return void
*/
public function handleReportGroupShowing(ReportGroupShowing $event)
{
if (!$this->checkClass($event)) {
return;
}
$event->class->groups['category'] = trans_choice('general.categories', 1);
$event->class->groups['account'] = trans_choice('general.accounts', 1);
$event->class->groups['customer'] = trans_choice('general.customers', 1);
}
/**
* Handle group applying event.
*
* @param $event
* @return void
*/
public function handleReportGroupApplying(ReportGroupApplying $event)
{
if (!$this->checkClass($event)) {
return;
}
$this->applyCustomerGroup($event);
$this->applyAccountGroup($event);
}
}

View File

@ -1,10 +0,0 @@
<?php
namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener;
class ProfitLossReport extends Listener
{
protected $class = 'App\Reports\ProfitLoss';
}

View File

@ -1,10 +0,0 @@
<?php
namespace App\Listeners\Common;
use App\Abstracts\Listeners\Report as Listener;
class TaxSummaryReport extends Listener
{
protected $class = 'App\Reports\TaxSummary';
}

View File

@ -59,10 +59,12 @@ class Event extends Provider
* @var array * @var array
*/ */
protected $subscribe = [ protected $subscribe = [
'App\Listeners\Common\IncomeSummaryReport', 'App\Listeners\Common\AddDateToReports',
'App\Listeners\Common\ExpenseSummaryReport', 'App\Listeners\Common\AddAccountsToReports',
'App\Listeners\Common\IncomeExpenseSummaryReport', 'App\Listeners\Common\AddCustomersToReports',
'App\Listeners\Common\TaxSummaryReport', 'App\Listeners\Common\AddVendorsToReports',
'App\Listeners\Common\ProfitLossReport', 'App\Listeners\Common\AddExpenseCategoriesToReports',
'App\Listeners\Common\AddIncomeCategoriesToReports',
'App\Listeners\Common\AddSearchToReports',
]; ];
} }