Skip to content

Commit

Permalink
refactored report listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Jan 22, 2020
1 parent c64dfda commit c208c33
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 210 deletions.
74 changes: 3 additions & 71 deletions app/Abstracts/Listeners/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

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\Common\Contact;
use App\Models\Setting\Category;
Expand All @@ -16,7 +12,7 @@ abstract class Report
{
use Contacts;

protected $class = '';
protected $classes = [];

protected $events = [
'App\Events\Common\ReportFilterShowing',
Expand All @@ -25,9 +21,9 @@ abstract class Report
'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()
Expand Down Expand Up @@ -137,70 +133,6 @@ public function applyVendorGroup($event)
}
}

/**
* 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
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 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.
Expand All @@ -20,14 +25,11 @@ class ExpenseSummaryReport extends Listener
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
if (!$this->checkClass($event)) {
if ($this->skipThisClass($event)) {
return;
}

$event->class->filters['years'] = $this->getYears();
$event->class->filters['accounts'] = $this->getAccounts();
$event->class->filters['categories'] = $this->getExpenseCategories();
$event->class->filters['vendors'] = $this->getVendors();
}

/**
Expand All @@ -38,28 +40,25 @@ public function handleReportFilterShowing(ReportFilterShowing $event)
*/
public function handleReportGroupShowing(ReportGroupShowing $event)
{
if (!$this->checkClass($event)) {
if ($this->skipThisClass($event)) {
return;
}

$event->class->groups['category'] = trans_choice('general.categories', 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
* @return void
*/
public function handleReportGroupApplying(ReportGroupApplying $event)
{
if (!$this->checkClass($event)) {
if ($this->skipThisClass($event)) {
return;
}

$this->applyVendorGroup($event);
$this->applyAccountGroup($event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
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 IncomeExpenseSummaryReport extends Listener
class AddCustomersToReports extends Listener
{
protected $class = 'App\Reports\IncomeExpenseSummary';
protected $classes = [
'App\Reports\IncomeSummary',
'App\Reports\IncomeExpenseSummary',
];

/**
* Handle filter showing event.
Expand All @@ -20,15 +22,11 @@ class IncomeExpenseSummaryReport extends Listener
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
if (!$this->checkClass($event)) {
if ($this->skipThisClass($event)) {
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['vendors'] = $this->getVendors();
}

/**
Expand All @@ -39,14 +37,11 @@ public function handleReportFilterShowing(ReportFilterShowing $event)
*/
public function handleReportGroupShowing(ReportGroupShowing $event)
{
if (!$this->checkClass($event)) {
if ($this->skipThisClass($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);
$event->class->groups['vendor'] = trans_choice('general.vendors', 1);
}

/**
Expand All @@ -57,12 +52,10 @@ public function handleReportGroupShowing(ReportGroupShowing $event)
*/
public function handleReportGroupApplying(ReportGroupApplying $event)
{
if (!$this->checkClass($event)) {
if ($this->skipThisClass($event)) {
return;
}

$this->applyCustomerGroup($event);
$this->applyVendorGroup($event);
$this->applyAccountGroup($event);
}
}
}
49 changes: 49 additions & 0 deletions app/Listeners/Common/AddDateToReports.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
54 changes: 54 additions & 0 deletions app/Listeners/Common/AddExpenseCategoriesToReports.php
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c208c33

Please sign in to comment.