Skip to content

Commit

Permalink
refactored report listener
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Jan 27, 2020
1 parent 877d299 commit 9c6e3c2
Show file tree
Hide file tree
Showing 31 changed files with 484 additions and 200 deletions.
53 changes: 50 additions & 3 deletions app/Abstracts/Listeners/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
use App\Models\Common\Contact;
use App\Models\Setting\Category;
use App\Traits\Contacts;
use App\Traits\DateTime;
use Date;

abstract class Report
{
use Contacts;
use Contacts, DateTime;

protected $classes = [];

Expand All @@ -19,13 +20,21 @@ abstract class Report
'App\Events\Common\ReportFilterApplying',
'App\Events\Common\ReportGroupShowing',
'App\Events\Common\ReportGroupApplying',
'App\Events\Common\ReportRowsShowing',
];

public function skipThisClass($event)
{
return (empty($event->class) || !in_array(get_class($event->class), $this->classes));
}

public function skipRowsShowing($event, $group)
{
return $this->skipThisClass($event)
|| empty($event->class->model->settings->group)
|| ($event->class->model->settings->group != $group);
}

public function getYears()
{
$now = Date::now();
Expand Down Expand Up @@ -68,7 +77,7 @@ public function getIncomeExpenseCategories()

public function getCategories($types)
{
return Category::type($types)->enabled()->orderBy('name')->pluck('name', 'id')->toArray();
return Category::type($types)->orderBy('name')->pluck('name', 'id')->toArray();
}

public function getCustomers()
Expand All @@ -83,7 +92,7 @@ public function getVendors()

public function getContacts($types)
{
return Contact::type($types)->enabled()->orderBy('name')->pluck('name', 'id')->toArray();
return Contact::type($types)->orderBy('name')->pluck('name', 'id')->toArray();
}

public function applyDateFilter($event)
Expand Down Expand Up @@ -112,6 +121,8 @@ public function applyAccountGroup($event)
}

$event->model->account_id = $transaction->account_id;

break;
}
}

Expand All @@ -133,6 +144,42 @@ public function applyVendorGroup($event)
}
}

public function setRowNamesAndValues($event, $rows)
{
foreach ($event->class->dates as $date) {
foreach ($event->class->tables as $table) {
foreach ($rows as $id => $name) {
$event->class->row_names[$table][$id] = $name;
$event->class->row_values[$table][$id][$date] = 0;
}
}
}
}

public function getFormattedDate($event, $date)
{
if (empty($event->class->model->settings->period)) {
return $date->copy()->format('Y-m-d');
}

switch ($event->class->model->settings->period) {
case 'yearly':
$d = $date->copy()->format($this->getYearlyDateFormat());
break;
case 'quarterly':
$start = $date->copy()->startOfQuarter()->format($this->getQuarterlyDateFormat());
$end = $date->copy()->endOfQuarter()->format($this->getQuarterlyDateFormat());

$d = $start . '-' . $end;
break;
default:
$d = $date->copy()->format($this->getMonthlyDateFormat());
break;
}

return $d;
}

/**
* Register the listeners for the subscriber.
*
Expand Down
101 changes: 47 additions & 54 deletions app/Abstracts/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Events\Common\ReportFilterApplying;
use App\Events\Common\ReportGroupApplying;
use App\Events\Common\ReportGroupShowing;
use App\Events\Common\ReportRowsShowing;
use App\Exports\Common\Reports as Export;
use App\Models\Banking\Transaction;
use App\Models\Common\Report as Model;
Expand Down Expand Up @@ -36,12 +37,16 @@ abstract class Report

public $dates = [];

public $rows = [];
public $row_names = [];

public $totals = [];
public $row_values = [];

public $footer_totals = [];

public $filters = [];

public $loaded = false;

public $indents = [
'table_header' => '0px',
'table_rows' => '0px',
Expand All @@ -62,7 +67,7 @@ abstract class Report
'datasets' => [],
];

public function __construct(Model $model = null, $get_totals = true)
public function __construct(Model $model = null, $load_data = true)
{
$this->setGroups();

Expand All @@ -72,20 +77,28 @@ public function __construct(Model $model = null, $get_totals = true)

$this->model = $model;

if (!$load_data) {
return;
}

$this->load();
}

abstract public function getTotals();

public function load()
{
$this->setYear();
$this->setViews();
$this->setTables();
$this->setDates();
$this->setFilters();
$this->setRows();
$this->getTotals();

if ($get_totals) {
$this->getTotals();
}
$this->loaded = true;
}

abstract public function getTotals();

public function getDefaultName()
{
if (!empty($this->default_name)) {
Expand All @@ -107,9 +120,13 @@ public function getIcon()

public function getTotal()
{
if (!$this->loaded) {
$this->load();
}

$sum = 0;

foreach ($this->totals as $total) {
foreach ($this->footer_totals as $total) {
$sum += is_array($total) ? array_sum($total) : $total;
}

Expand All @@ -118,21 +135,6 @@ public function getTotal()
return $total;
}

public function getTableRowList()
{
$group_prl = Str::plural($this->model->settings->group);

if ($group_filter = request($group_prl)) {
$rows = collect($this->filters[$group_prl])->filter(function ($value, $key) use ($group_filter) {
return in_array($key, $group_filter);
});
} else {
$rows = $this->filters[$group_prl];
}

return $rows;
}

public function getChart()
{
$chart = new Chartjs();
Expand All @@ -158,7 +160,7 @@ public function getChart()
->fill(false);
}
} else {
foreach ($this->totals as $total) {
foreach ($this->footer_totals as $total) {
$chart->dataset($this->model->name, 'line', array_values($total))
->backgroundColor(isset($config['backgroundColor']) ? $config['backgroundColor'] : '#6da252')
->color(isset($config['color']) ? $config['color'] : '#6da252')
Expand Down Expand Up @@ -214,12 +216,16 @@ public function setViews()
public function setTables()
{
$this->tables = [
'default' => 'default'
'default' => 'default',
];
}

public function setDates()
{
if (empty($this->model->settings->period)) {
return;
}

$function = 'sub' . ucfirst(str_replace('ly', '', $this->model->settings->period));

$start = $this->getFinancialStart()->copy()->$function();
Expand All @@ -234,7 +240,7 @@ public function setDates()
$this->dates[$j] = $date;

foreach ($this->tables as $table) {
$this->totals[$table][$date] = 0;
$this->footer_totals[$table][$date] = 0;
}

$j += 11;
Expand All @@ -248,7 +254,7 @@ public function setDates()
$this->dates[$j] = $date;

foreach ($this->tables as $table) {
$this->totals[$table][$date] = 0;
$this->footer_totals[$table][$date] = 0;
}

$j += 2;
Expand All @@ -262,7 +268,7 @@ public function setDates()
$this->dates[$j] = $date;

foreach ($this->tables as $table) {
$this->totals[$table][$date] = 0;
$this->footer_totals[$table][$date] = 0;
}

break;
Expand All @@ -284,15 +290,7 @@ public function setGroups()

public function setRows()
{
$list = $this->getTableRowList();

foreach ($this->dates as $date) {
foreach ($this->tables as $table) {
foreach ($list as $id => $name) {
$this->rows[$table][$id][$date] = 0;
}
}
}
event(new ReportRowsShowing($this));
}

public function setTotals($items, $date_field, $check_type = false, $table = 'default')
Expand All @@ -305,31 +303,26 @@ public function setTotals($items, $date_field, $check_type = false, $table = 'de

$id_field = $this->model->settings->group . '_id';

if (!isset($this->rows[$table][$item->$id_field]) ||
!isset($this->rows[$table][$item->$id_field][$date]) ||
!isset($this->totals[$table][$date]))
if (
!isset($this->row_values[$table][$item->$id_field])
|| !isset($this->row_values[$table][$item->$id_field][$date])
|| !isset($this->footer_totals[$table][$date]))
{
continue;
}

$amount = $item->getAmountConvertedToDefault();

if (!$check_type) {
$this->rows[$table][$item->$id_field][$date] += $amount;
$type = (($item instanceof Invoice) || (($item instanceof Transaction) && ($item->type == 'income'))) ? 'income' : 'expense';

$this->totals[$table][$date] += $amount;
} else {
$type = (($item instanceof Invoice) || (($item instanceof Transaction) && ($item->type == 'income'))) ? 'income' : 'expense';

if ($type == 'income') {
$this->rows[$table][$item->$id_field][$date] += $amount;
if (($check_type == false) || ($type == 'income')) {
$this->row_values[$table][$item->$id_field][$date] += $amount;

$this->totals[$table][$date] += $amount;
} else {
$this->rows[$table][$item->$id_field][$date] -= $amount;
$this->footer_totals[$table][$date] += $amount;
} else {
$this->row_values[$table][$item->$id_field][$date] -= $amount;

$this->totals[$table][$date] -= $amount;
}
$this->footer_totals[$table][$date] -= $amount;
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions app/Events/Common/ReportRowsShowing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Events\Common;

use Illuminate\Queue\SerializesModels;

class ReportRowsShowing
{
use SerializesModels;

public $class;

/**
* Create a new event instance.
*
* @param $class
*/
public function __construct($class)
{
$this->class = $class;
}
}
26 changes: 24 additions & 2 deletions app/Listeners/Common/AddAccountsToReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportGroupApplying;
use App\Events\Common\ReportGroupShowing;
use App\Events\Common\ReportRowsShowing;

class AddAccountsToReports extends Listener
{
protected $classes = [
'App\Reports\IncomeSummary',
'App\Reports\ExpenseSummary',
'App\Reports\IncomeExpenseSummary',
'App\Reports\ProfitLoss',
'App\Reports\TaxSummary',
];

/**
Expand Down Expand Up @@ -61,4 +60,27 @@ public function handleReportGroupApplying(ReportGroupApplying $event)

$this->applyAccountGroup($event);
}

/**
* Handle rows showing event.
*
* @param $event
* @return void
*/
public function handleReportRowsShowing(ReportRowsShowing $event)
{
if ($this->skipRowsShowing($event, 'account')) {
return;
}

if ($accounts = request('accounts')) {
$rows = collect($event->class->filters['accounts'])->filter(function ($value, $key) use ($accounts) {
return in_array($key, $accounts);
});
} else {
$rows = $event->class->filters['accounts'];
}

$this->setRowNamesAndValues($event, $rows);
}
}

0 comments on commit 9c6e3c2

Please sign in to comment.