From 9c6e3c275184cd2f1c2023bbf7167fbede610b3f Mon Sep 17 00:00:00 2001 From: denisdulici Date: Mon, 27 Jan 2020 22:41:08 +0300 Subject: [PATCH] refactored report listener --- app/Abstracts/Listeners/Report.php | 53 ++++++++- app/Abstracts/Report.php | 101 ++++++++-------- app/Events/Common/ReportRowsShowing.php | 22 ++++ app/Listeners/Common/AddAccountsToReports.php | 26 ++++- .../Common/AddCustomersToReports.php | 24 ++++ .../Common/AddExpenseCategoriesToReports.php | 48 +++++--- .../Common/AddIncomeCategoriesToReports.php | 48 +++++--- .../AddIncomeExpenseCategoriesToReports.php | 108 ++++++++++++++++++ app/Listeners/Common/AddRowsToTaxReport.php | 33 ++++++ app/Listeners/Common/AddVendorsToReports.php | 24 ++++ app/Listeners/Update/V20/Version200.php | 2 +- app/Models/Sale/InvoiceItem.php | 1 - app/Providers/Event.php | 2 + app/Reports/ExpenseSummary.php | 4 +- app/Reports/IncomeExpenseSummary.php | 28 +++-- app/Reports/IncomeSummary.php | 14 ++- app/Reports/ProfitLoss.php | 57 +++------ app/Reports/TaxSummary.php | 26 ++--- app/Traits/DateTime.php | 2 + app/Utilities/Recurring.php | 1 + database/seeds/Reports.php | 2 +- resources/lang/en-GB/demo.php | 2 +- resources/views/common/items/edit.blade.php | 1 + resources/views/common/reports/edit.blade.php | 11 +- .../views/common/reports/index.blade.php | 6 +- .../views/partials/reports/print.blade.php | 2 +- .../views/partials/reports/table.blade.php | 4 +- .../partials/reports/table/footer.blade.php | 8 +- .../partials/reports/table/rows.blade.php | 8 +- .../profit_loss/table/footer.blade.php | 8 +- .../tax_summary/table/footer.blade.php | 8 +- 31 files changed, 484 insertions(+), 200 deletions(-) create mode 100644 app/Events/Common/ReportRowsShowing.php create mode 100644 app/Listeners/Common/AddIncomeExpenseCategoriesToReports.php create mode 100644 app/Listeners/Common/AddRowsToTaxReport.php diff --git a/app/Abstracts/Listeners/Report.php b/app/Abstracts/Listeners/Report.php index d97f43cb9..a226beddc 100644 --- a/app/Abstracts/Listeners/Report.php +++ b/app/Abstracts/Listeners/Report.php @@ -6,11 +6,12 @@ use App\Models\Banking\Account; 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 = []; @@ -19,6 +20,7 @@ abstract class Report 'App\Events\Common\ReportFilterApplying', 'App\Events\Common\ReportGroupShowing', 'App\Events\Common\ReportGroupApplying', + 'App\Events\Common\ReportRowsShowing', ]; public function skipThisClass($event) @@ -26,6 +28,13 @@ abstract class Report 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(); @@ -68,7 +77,7 @@ abstract class Report 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() @@ -83,7 +92,7 @@ abstract class Report 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) @@ -112,6 +121,8 @@ abstract class Report } $event->model->account_id = $transaction->account_id; + + break; } } @@ -133,6 +144,42 @@ abstract class Report } } + 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. * diff --git a/app/Abstracts/Report.php b/app/Abstracts/Report.php index 5bf8617d1..7bd688442 100644 --- a/app/Abstracts/Report.php +++ b/app/Abstracts/Report.php @@ -6,6 +6,7 @@ use App\Events\Common\ReportFilterShowing; 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; @@ -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', @@ -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(); @@ -72,20 +77,28 @@ abstract class Report $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)) { @@ -107,9 +120,13 @@ abstract class Report 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; } @@ -118,21 +135,6 @@ abstract class Report 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(); @@ -158,7 +160,7 @@ abstract class Report ->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') @@ -214,12 +216,16 @@ abstract class Report 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(); @@ -234,7 +240,7 @@ abstract class Report $this->dates[$j] = $date; foreach ($this->tables as $table) { - $this->totals[$table][$date] = 0; + $this->footer_totals[$table][$date] = 0; } $j += 11; @@ -248,7 +254,7 @@ abstract class Report $this->dates[$j] = $date; foreach ($this->tables as $table) { - $this->totals[$table][$date] = 0; + $this->footer_totals[$table][$date] = 0; } $j += 2; @@ -262,7 +268,7 @@ abstract class Report $this->dates[$j] = $date; foreach ($this->tables as $table) { - $this->totals[$table][$date] = 0; + $this->footer_totals[$table][$date] = 0; } break; @@ -284,15 +290,7 @@ abstract class Report 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') @@ -305,31 +303,26 @@ abstract class Report $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; + if (($check_type == false) || ($type == 'income')) { + $this->row_values[$table][$item->$id_field][$date] += $amount; + + $this->footer_totals[$table][$date] += $amount; } else { - $type = (($item instanceof Invoice) || (($item instanceof Transaction) && ($item->type == 'income'))) ? 'income' : 'expense'; + $this->row_values[$table][$item->$id_field][$date] -= $amount; - if ($type == 'income') { - $this->rows[$table][$item->$id_field][$date] += $amount; - - $this->totals[$table][$date] += $amount; - } else { - $this->rows[$table][$item->$id_field][$date] -= $amount; - - $this->totals[$table][$date] -= $amount; - } + $this->footer_totals[$table][$date] -= $amount; } } } diff --git a/app/Events/Common/ReportRowsShowing.php b/app/Events/Common/ReportRowsShowing.php new file mode 100644 index 000000000..98deecf72 --- /dev/null +++ b/app/Events/Common/ReportRowsShowing.php @@ -0,0 +1,22 @@ +class = $class; + } +} diff --git a/app/Listeners/Common/AddAccountsToReports.php b/app/Listeners/Common/AddAccountsToReports.php index 519c47bb5..2dcad4ed9 100644 --- a/app/Listeners/Common/AddAccountsToReports.php +++ b/app/Listeners/Common/AddAccountsToReports.php @@ -6,6 +6,7 @@ use App\Abstracts\Listeners\Report as Listener; use App\Events\Common\ReportFilterShowing; use App\Events\Common\ReportGroupApplying; use App\Events\Common\ReportGroupShowing; +use App\Events\Common\ReportRowsShowing; class AddAccountsToReports extends Listener { @@ -13,8 +14,6 @@ class AddAccountsToReports extends Listener 'App\Reports\IncomeSummary', 'App\Reports\ExpenseSummary', 'App\Reports\IncomeExpenseSummary', - 'App\Reports\ProfitLoss', - 'App\Reports\TaxSummary', ]; /** @@ -61,4 +60,27 @@ class AddAccountsToReports extends Listener $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); + } } diff --git a/app/Listeners/Common/AddCustomersToReports.php b/app/Listeners/Common/AddCustomersToReports.php index 0bb818c11..bc6ec8865 100644 --- a/app/Listeners/Common/AddCustomersToReports.php +++ b/app/Listeners/Common/AddCustomersToReports.php @@ -6,6 +6,7 @@ use App\Abstracts\Listeners\Report as Listener; use App\Events\Common\ReportFilterShowing; use App\Events\Common\ReportGroupApplying; use App\Events\Common\ReportGroupShowing; +use App\Events\Common\ReportRowsShowing; class AddCustomersToReports extends Listener { @@ -58,4 +59,27 @@ class AddCustomersToReports extends Listener $this->applyCustomerGroup($event); } + + /** + * Handle rows showing event. + * + * @param $event + * @return void + */ + public function handleReportRowsShowing(ReportRowsShowing $event) + { + if ($this->skipRowsShowing($event, 'customer')) { + return; + } + + if ($customers = request('customers')) { + $rows = collect($event->class->filters['customers'])->filter(function ($value, $key) use ($customers) { + return in_array($key, $customers); + }); + } else { + $rows = $event->class->filters['customers']; + } + + $this->setRowNamesAndValues($event, $rows); + } } diff --git a/app/Listeners/Common/AddExpenseCategoriesToReports.php b/app/Listeners/Common/AddExpenseCategoriesToReports.php index 54a811245..3ee1e20a9 100644 --- a/app/Listeners/Common/AddExpenseCategoriesToReports.php +++ b/app/Listeners/Common/AddExpenseCategoriesToReports.php @@ -5,9 +5,14 @@ namespace App\Listeners\Common; use App\Abstracts\Listeners\Report as Listener; use App\Events\Common\ReportFilterShowing; use App\Events\Common\ReportGroupShowing; +use App\Events\Common\ReportRowsShowing; class AddExpenseCategoriesToReports extends Listener { + protected $classes = [ + 'App\Reports\ExpenseSummary', + ]; + /** * Handle filter showing event. * @@ -16,18 +21,11 @@ class AddExpenseCategoriesToReports extends Listener */ public function handleReportFilterShowing(ReportFilterShowing $event) { - $classes = [ - 'App\Reports\ExpenseSummary', - 'App\Reports\IncomeExpenseSummary', - ]; - - if (empty($event->class) || !in_array(get_class($event->class), $classes)) { + if ($this->skipThisClass($event)) { return; } - $categories = !empty($event->class->filters['categories']) ? $event->class->filters['categories'] : []; - - $event->class->filters['categories'] = array_merge($categories, $this->getExpenseCategories()); + $event->class->filters['categories'] = $this->getExpenseCategories(); } /** @@ -38,17 +36,33 @@ class AddExpenseCategoriesToReports extends Listener */ 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)) { + if ($this->skipThisClass($event)) { return; } $event->class->groups['category'] = trans_choice('general.categories', 1); } + + /** + * Handle rows showing event. + * + * @param $event + * @return void + */ + public function handleReportRowsShowing(ReportRowsShowing $event) + { + if ($this->skipRowsShowing($event, 'category')) { + return; + } + + if ($categories = request('categories')) { + $rows = collect($event->class->filters['categories'])->filter(function ($value, $key) use ($categories) { + return in_array($key, $categories); + }); + } else { + $rows = $event->class->filters['categories']; + } + + $this->setRowNamesAndValues($event, $rows); + } } diff --git a/app/Listeners/Common/AddIncomeCategoriesToReports.php b/app/Listeners/Common/AddIncomeCategoriesToReports.php index 64b953a60..c66cf5624 100644 --- a/app/Listeners/Common/AddIncomeCategoriesToReports.php +++ b/app/Listeners/Common/AddIncomeCategoriesToReports.php @@ -5,9 +5,14 @@ namespace App\Listeners\Common; use App\Abstracts\Listeners\Report as Listener; use App\Events\Common\ReportFilterShowing; use App\Events\Common\ReportGroupShowing; +use App\Events\Common\ReportRowsShowing; class AddIncomeCategoriesToReports extends Listener { + protected $classes = [ + 'App\Reports\IncomeSummary', + ]; + /** * Handle filter showing event. * @@ -16,18 +21,11 @@ class AddIncomeCategoriesToReports extends Listener */ public function handleReportFilterShowing(ReportFilterShowing $event) { - $classes = [ - 'App\Reports\IncomeSummary', - 'App\Reports\IncomeExpenseSummary', - ]; - - if (empty($event->class) || !in_array(get_class($event->class), $classes)) { + if ($this->skipThisClass($event)) { return; } - $categories = !empty($event->class->filters['categories']) ? $event->class->filters['categories'] : []; - - $event->class->filters['categories'] = array_merge($categories, $this->getIncomeCategories()); + $event->class->filters['categories'] = $this->getIncomeCategories(); } /** @@ -38,17 +36,33 @@ class AddIncomeCategoriesToReports extends Listener */ 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)) { + if ($this->skipThisClass($event)) { return; } $event->class->groups['category'] = trans_choice('general.categories', 1); } + + /** + * Handle rows showing event. + * + * @param $event + * @return void + */ + public function handleReportRowsShowing(ReportRowsShowing $event) + { + if ($this->skipRowsShowing($event, 'category')) { + return; + } + + if ($categories = request('categories')) { + $rows = collect($event->class->filters['categories'])->filter(function ($value, $key) use ($categories) { + return in_array($key, $categories); + }); + } else { + $rows = $event->class->filters['categories']; + } + + $this->setRowNamesAndValues($event, $rows); + } } diff --git a/app/Listeners/Common/AddIncomeExpenseCategoriesToReports.php b/app/Listeners/Common/AddIncomeExpenseCategoriesToReports.php new file mode 100644 index 000000000..b80388f4f --- /dev/null +++ b/app/Listeners/Common/AddIncomeExpenseCategoriesToReports.php @@ -0,0 +1,108 @@ +class) || !in_array(get_class($event->class), $classes)) { + return; + } + + $event->class->filters['categories'] = $this->getIncomeExpenseCategories(); + } + + /** + * Handle group showing event. + * + * @param $event + * @return void + */ + public function handleReportGroupShowing(ReportGroupShowing $event) + { + $classes = [ + 'App\Reports\IncomeExpenseSummary', + 'App\Reports\ProfitLoss', + ]; + + if (empty($event->class) || !in_array(get_class($event->class), $classes)) { + return; + } + + $event->class->groups['category'] = trans_choice('general.categories', 1); + } + + /** + * Handle records showing event. + * + * @param $event + * @return void + */ + public function handleReportRowsShowing(ReportRowsShowing $event) + { + if ( + empty($event->class) + || empty($event->class->model->settings->group) + || ($event->class->model->settings->group != 'category') + ) { + return; + } + + switch (get_class($event->class)) { + case 'App\Reports\ProfitLoss': + $categories = Category::type(['income', 'expense'])->orderBy('name')->get(); + $rows = $categories->pluck('name', 'id')->toArray(); + + $this->setRowNamesAndValuesForProfitLoss($event, $rows, $categories); + + break; + case 'App\Reports\IncomeExpenseSummary': + if ($categories = request('categories')) { + $rows = collect($event->class->filters['categories'])->filter(function ($value, $key) use ($categories) { + return in_array($key, $categories); + }); + } else { + $rows = $event->class->filters['categories']; + } + + $this->setRowNamesAndValues($event, $rows); + + break; + } + } + + public function setRowNamesAndValuesForProfitLoss($event, $rows, $categories) + { + foreach ($event->class->dates as $date) { + foreach ($event->class->tables as $type_id => $type_name) { + foreach ($rows as $id => $name) { + $category = $categories->where('id', $id)->first(); + + if ($category->type != $type_id) { + continue; + } + + $event->class->row_names[$type_name][$id] = $name; + $event->class->row_values[$type_name][$id][$date] = 0; + } + } + } + } +} diff --git a/app/Listeners/Common/AddRowsToTaxReport.php b/app/Listeners/Common/AddRowsToTaxReport.php new file mode 100644 index 000000000..52b694bac --- /dev/null +++ b/app/Listeners/Common/AddRowsToTaxReport.php @@ -0,0 +1,33 @@ +skipThisClass($event)) { + return; + } + + $rows = [ + 'income' => trans_choice('general.sales', 2), + 'expense' => trans_choice('general.purchases', 2), + ]; + + $this->setRowNamesAndValues($event, $rows); + } +} diff --git a/app/Listeners/Common/AddVendorsToReports.php b/app/Listeners/Common/AddVendorsToReports.php index b25027368..4d978aa92 100644 --- a/app/Listeners/Common/AddVendorsToReports.php +++ b/app/Listeners/Common/AddVendorsToReports.php @@ -6,6 +6,7 @@ use App\Abstracts\Listeners\Report as Listener; use App\Events\Common\ReportFilterShowing; use App\Events\Common\ReportGroupApplying; use App\Events\Common\ReportGroupShowing; +use App\Events\Common\ReportRowsShowing; class AddVendorsToReports extends Listener { @@ -58,4 +59,27 @@ class AddVendorsToReports extends Listener $this->applyVendorGroup($event); } + + /** + * Handle rows showing event. + * + * @param $event + * @return void + */ + public function handleReportRowsShowing(ReportRowsShowing $event) + { + if ($this->skipRowsShowing($event, 'vendor')) { + return; + } + + if ($vendors = request('vendors')) { + $rows = collect($event->class->filters['vendors'])->filter(function ($value, $key) use ($vendors) { + return in_array($key, $vendors); + }); + } else { + $rows = $event->class->filters['vendors']; + } + + $this->setRowNamesAndValues($event, $rows); + } } diff --git a/app/Listeners/Update/V20/Version200.php b/app/Listeners/Update/V20/Version200.php index 12167db8a..6c08712b0 100644 --- a/app/Listeners/Update/V20/Version200.php +++ b/app/Listeners/Update/V20/Version200.php @@ -290,7 +290,7 @@ class Version200 extends Listener 'class' => 'App\Reports\TaxSummary', 'name' => trans('reports.summary.tax'), 'description' => trans('demo.reports.tax'), - 'settings' => ['group' => 'category', 'period' => 'quarterly', 'basis' => 'accrual'], + 'settings' => ['period' => 'quarterly', 'basis' => 'accrual'], ], ]; diff --git a/app/Models/Sale/InvoiceItem.php b/app/Models/Sale/InvoiceItem.php index 4fee1a529..b9bd13a43 100644 --- a/app/Models/Sale/InvoiceItem.php +++ b/app/Models/Sale/InvoiceItem.php @@ -8,7 +8,6 @@ use Bkwld\Cloner\Cloneable; class InvoiceItem extends Model { - use Cloneable, Currencies; protected $table = 'invoice_items'; diff --git a/app/Providers/Event.php b/app/Providers/Event.php index 9f15efc33..8499390b7 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -68,6 +68,8 @@ class Event extends Provider 'App\Listeners\Common\AddVendorsToReports', 'App\Listeners\Common\AddExpenseCategoriesToReports', 'App\Listeners\Common\AddIncomeCategoriesToReports', + 'App\Listeners\Common\AddIncomeExpenseCategoriesToReports', 'App\Listeners\Common\AddSearchToReports', + 'App\Listeners\Common\AddRowsToTaxReport', ]; } diff --git a/app/Reports/ExpenseSummary.php b/app/Reports/ExpenseSummary.php index 9d756284b..cc1cea2e7 100644 --- a/app/Reports/ExpenseSummary.php +++ b/app/Reports/ExpenseSummary.php @@ -30,11 +30,12 @@ class ExpenseSummary extends Report public function getTotals() { - $payments = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at'])->get(); + $transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at']); switch ($this->model->settings->basis) { case 'cash': // Payments + $payments = $transactions->get(); $this->setTotals($payments, 'paid_at'); break; @@ -45,6 +46,7 @@ class ExpenseSummary extends Report $this->setTotals($bills, 'billed_at'); // Payments + $payments = $transactions->isNotDocument()->get(); Recurring::reflect($payments, 'paid_at'); $this->setTotals($payments, 'paid_at'); diff --git a/app/Reports/IncomeExpenseSummary.php b/app/Reports/IncomeExpenseSummary.php index ace5fb31f..3f0136873 100644 --- a/app/Reports/IncomeExpenseSummary.php +++ b/app/Reports/IncomeExpenseSummary.php @@ -16,16 +16,18 @@ class IncomeExpenseSummary extends Report public function getTotals() { - $income_transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at'])->get(); - $expense_transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at'])->get(); + $income_transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at']); + $expense_transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at']); switch ($this->model->settings->basis) { case 'cash': - // Income Transactions - $this->setTotals($income_transactions, 'paid_at', true); + // Revenues + $revenues = $income_transactions->get(); + $this->setTotals($revenues, 'paid_at', true); - // Expense Transactions - $this->setTotals($expense_transactions, 'paid_at', true); + // Payments + $payments = $expense_transactions->get(); + $this->setTotals($payments, 'paid_at', true); break; default: @@ -34,18 +36,20 @@ class IncomeExpenseSummary extends Report Recurring::reflect($invoices, 'invoiced_at'); $this->setTotals($invoices, 'invoiced_at', true); - // Income Transactions - Recurring::reflect($income_transactions, 'paid_at'); - $this->setTotals($income_transactions, 'paid_at', true); + // Revenues + $revenues = $income_transactions->isNotDocument()->get(); + Recurring::reflect($revenues, 'paid_at'); + $this->setTotals($revenues, 'paid_at', true); // Bills $bills = $this->applyFilters(Bill::accrued(), ['date_field' => 'billed_at'])->get(); Recurring::reflect($bills, 'bill', 'billed_at'); $this->setTotals($bills, 'billed_at', true); - // Expense Transactions - Recurring::reflect($expense_transactions, 'paid_at'); - $this->setTotals($expense_transactions, 'paid_at', true); + // Payments + $payments = $expense_transactions->isNotDocument()->get(); + Recurring::reflect($payments, 'paid_at'); + $this->setTotals($payments, 'paid_at', true); break; } diff --git a/app/Reports/IncomeSummary.php b/app/Reports/IncomeSummary.php index 8cfa4c899..a109ba49e 100644 --- a/app/Reports/IncomeSummary.php +++ b/app/Reports/IncomeSummary.php @@ -30,12 +30,13 @@ class IncomeSummary extends Report public function getTotals() { - $transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at'])->get(); + $transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at']); switch ($this->model->settings->basis) { case 'cash': - // Transactions - $this->setTotals($transactions, 'paid_at'); + // Revenues + $revenues = $transactions->get(); + $this->setTotals($revenues, 'paid_at'); break; default: @@ -44,9 +45,10 @@ class IncomeSummary extends Report Recurring::reflect($invoices, 'invoiced_at'); $this->setTotals($invoices, 'invoiced_at'); - // Transactions - Recurring::reflect($transactions, 'paid_at'); - $this->setTotals($transactions, 'paid_at'); + // Revenues + $revenues = $transactions->isNotDocument()->get(); + Recurring::reflect($revenues, 'paid_at'); + $this->setTotals($revenues, 'paid_at'); break; } diff --git a/app/Reports/ProfitLoss.php b/app/Reports/ProfitLoss.php index db373efed..6fefe2dd3 100644 --- a/app/Reports/ProfitLoss.php +++ b/app/Reports/ProfitLoss.php @@ -6,7 +6,6 @@ use App\Abstracts\Report; use App\Models\Banking\Transaction; use App\Models\Purchase\Bill; use App\Models\Sale\Invoice; -use App\Models\Setting\Category; use App\Utilities\Recurring; class ProfitLoss extends Report @@ -36,44 +35,20 @@ class ProfitLoss extends Report ]; } - public function getTableRowList() - { - $this->cat_list = Category::type(['income', 'expense'])->enabled()->orderBy('name')->get(); - - return collect($this->cat_list)->pluck('name', 'id')->toArray(); - } - - public function setRows() - { - $list = $this->getTableRowList(); - - foreach ($this->dates as $date) { - foreach ($this->tables as $t_id => $t_name) { - foreach ($list as $id => $name) { - $cat = $this->cat_list->where('id', $id)->first(); - - if ($cat->type != $t_id) { - continue; - } - - $this->rows[$t_name][$id][$date] = 0; - } - } - } - } - public function getTotals() { - $income_transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at'])->get(); - $expense_transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at'])->get(); + $income_transactions = $this->applyFilters(Transaction::type('income')->isNotTransfer(), ['date_field' => 'paid_at']); + $expense_transactions = $this->applyFilters(Transaction::type('expense')->isNotTransfer(), ['date_field' => 'paid_at']); switch ($this->model->settings->basis) { case 'cash': - // Income Transactions - $this->setTotals($income_transactions, 'paid_at', true, $this->tables['income']); + // Revenues + $revenues = $income_transactions->get(); + $this->setTotals($revenues, 'paid_at', true, $this->tables['income']); - // Expense Transactions - $this->setTotals($expense_transactions, 'paid_at', true, $this->tables['expense']); + // Payments + $payments = $expense_transactions->get(); + $this->setTotals($payments, 'paid_at', true, $this->tables['expense']); break; default: @@ -82,24 +57,26 @@ class ProfitLoss extends Report Recurring::reflect($invoices, 'invoiced_at'); $this->setTotals($invoices, 'invoiced_at', true, $this->tables['income']); - // Income Transactions - Recurring::reflect($income_transactions, 'paid_at'); - $this->setTotals($income_transactions, 'paid_at', true, $this->tables['income']); + // Revenues + $revenues = $income_transactions->isNotDocument()->get(); + Recurring::reflect($revenues, 'paid_at'); + $this->setTotals($revenues, 'paid_at', true, $this->tables['income']); // Bills $bills = $this->applyFilters(Bill::accrued(), ['date_field' => 'billed_at'])->get(); Recurring::reflect($bills, 'bill', 'billed_at'); $this->setTotals($bills, 'billed_at', true, $this->tables['expense']); - // Expense Transactions - Recurring::reflect($expense_transactions, 'paid_at'); - $this->setTotals($expense_transactions, 'paid_at', true, $this->tables['expense']); + // Payments + $payments = $expense_transactions->isNotDocument()->get(); + Recurring::reflect($payments, 'paid_at'); + $this->setTotals($payments, 'paid_at', true, $this->tables['expense']); break; } // TODO: move to views - foreach ($this->totals as $table => $dates) { + foreach ($this->footer_totals as $table => $dates) { foreach ($dates as $date => $total) { if (!isset($this->net_profit[$date])) { $this->net_profit[$date] = 0; diff --git a/app/Reports/TaxSummary.php b/app/Reports/TaxSummary.php index 66c054f0b..e282b1ad8 100644 --- a/app/Reports/TaxSummary.php +++ b/app/Reports/TaxSummary.php @@ -38,24 +38,16 @@ class TaxSummary extends Report $this->tables = array_combine($taxes, $taxes); } - public function getTableRowList() - { - return [ - 'income' => trans_choice('general.incomes', 2), - 'expense' => trans_choice('general.expenses', 2), - ]; - } - public function getTotals() { switch ($this->model->settings->basis) { case 'cash': // Invoice Payments - $invoices = $this->applyFilters(Transaction::type('income')->isDocument()->with(['invoice', 'invoice.totals'])->isNotTransfer(), ['date_field' => 'paid_at'])->get(); + $invoices = $this->applyFilters(Transaction::with(['invoice', 'invoice.totals'])->type('income')->isDocument()->isNotTransfer(), ['date_field' => 'paid_at'])->get(); $this->setTotals($invoices, 'paid_at'); // Bill Payments - $bills = $this->applyFilters(Transaction::type('expense')->isDocument()->with(['bill', 'bill.totals'])->isNotTransfer(), ['date_field' => 'paid_at'])->get(); + $bills = $this->applyFilters(Transaction::with(['bill', 'bill.totals'])->type('expense')->isDocument()->isNotTransfer(), ['date_field' => 'paid_at'])->get(); $this->setTotals($bills, 'paid_at'); break; @@ -97,8 +89,9 @@ class TaxSummary extends Report continue; } - if (!isset($this->rows[$item_total->name][$type][$date]) || - !isset($this->totals[$item_total->name][$date])) + if ( + !isset($this->row_values[$item_total->name][$type][$date]) + || !isset($this->footer_totals[$item_total->name][$date])) { continue; } @@ -113,13 +106,13 @@ class TaxSummary extends Report $amount = $this->convertToDefault($item_amount, $item->currency_code, $item->currency_rate); if ($type == 'income') { - $this->rows[$item_total->name][$type][$date] += $amount; + $this->row_values[$item_total->name][$type][$date] += $amount; - $this->totals[$item_total->name][$date] += $amount; + $this->footer_totals[$item_total->name][$date] += $amount; } else { - $this->rows[$item_total->name][$type][$date] -= $amount; + $this->row_values[$item_total->name][$type][$date] -= $amount; - $this->totals[$item_total->name][$date] -= $amount; + $this->footer_totals[$item_total->name][$date] -= $amount; } } } @@ -128,7 +121,6 @@ class TaxSummary extends Report public function getFields() { return [ - $this->getGroupField(), $this->getPeriodField(), $this->getBasisField(), ]; diff --git a/app/Traits/DateTime.php b/app/Traits/DateTime.php index ecbc4abab..f3a81838d 100644 --- a/app/Traits/DateTime.php +++ b/app/Traits/DateTime.php @@ -59,6 +59,8 @@ trait DateTime public function getTimezones() { + $groups = []; + // The list of available timezone groups to use. $use_zones = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific'); diff --git a/app/Utilities/Recurring.php b/app/Utilities/Recurring.php index 1ddd772d3..776690988 100644 --- a/app/Utilities/Recurring.php +++ b/app/Utilities/Recurring.php @@ -11,6 +11,7 @@ class Recurring public static function reflect(&$items, $issued_date_field) { foreach ($items as $key => $item) { + // @todo cache recurrings if (!$item->recurring || !empty($item->parent_id)) { continue; } diff --git a/database/seeds/Reports.php b/database/seeds/Reports.php index 0acf1ee75..7e8a28102 100644 --- a/database/seeds/Reports.php +++ b/database/seeds/Reports.php @@ -60,7 +60,7 @@ class Reports extends Seeder 'class' => 'App\Reports\TaxSummary', 'name' => trans('reports.summary.tax'), 'description' => trans('demo.reports.tax'), - 'settings' => ['group' => 'category', 'period' => 'quarterly', 'basis' => 'accrual'], + 'settings' => ['period' => 'quarterly', 'basis' => 'accrual'], ], ]; diff --git a/resources/lang/en-GB/demo.php b/resources/lang/en-GB/demo.php index abeb2357f..ef1472d27 100644 --- a/resources/lang/en-GB/demo.php +++ b/resources/lang/en-GB/demo.php @@ -27,7 +27,7 @@ return [ 'income' => 'Monthly income summary by category.', 'expense' => 'Monthly expense summary by category.', 'income_expense' => 'Monthly income vs expense by category.', - 'tax' => 'Quarterly tax summary by category.', + 'tax' => 'Quarterly tax summary.', 'profit_loss' => 'Quarterly profit & loss by category.', ], diff --git a/resources/views/common/items/edit.blade.php b/resources/views/common/items/edit.blade.php index 2fa9e5c0c..f62701d75 100644 --- a/resources/views/common/items/edit.blade.php +++ b/resources/views/common/items/edit.blade.php @@ -43,6 +43,7 @@ @endpermission + {!! Form::close() !!} @endsection diff --git a/resources/views/common/reports/edit.blade.php b/resources/views/common/reports/edit.blade.php index 487148b92..bf9e4aa00 100644 --- a/resources/views/common/reports/edit.blade.php +++ b/resources/views/common/reports/edit.blade.php @@ -60,11 +60,14 @@ - @endsection diff --git a/resources/views/common/reports/index.blade.php b/resources/views/common/reports/index.blade.php index a33a1e9f6..cb7f7ecf7 100644 --- a/resources/views/common/reports/index.blade.php +++ b/resources/views/common/reports/index.blade.php @@ -25,15 +25,13 @@ diff --git a/resources/views/partials/reports/print.blade.php b/resources/views/partials/reports/print.blade.php index af4381ae4..6324a0e57 100644 --- a/resources/views/partials/reports/print.blade.php +++ b/resources/views/partials/reports/print.blade.php @@ -6,7 +6,7 @@ {{ setting('company.name') }} - @if($class->model->settings->chart) + @if(!empty($class->model->settings->chart)) @include($class->views['chart']) @endif diff --git a/resources/views/partials/reports/table.blade.php b/resources/views/partials/reports/table.blade.php index 5726f91c8..631bfbbe9 100644 --- a/resources/views/partials/reports/table.blade.php +++ b/resources/views/partials/reports/table.blade.php @@ -2,8 +2,8 @@ @include($class->views['table.header']) - @if (!empty($class->rows[$table])) - @foreach($class->rows[$table] as $id => $items) + @if (!empty($class->row_values[$table])) + @foreach($class->row_values[$table] as $id => $rows) @include($class->views['table.rows']) @endforeach @else diff --git a/resources/views/partials/reports/table/footer.blade.php b/resources/views/partials/reports/table/footer.blade.php index 971a3f890..9c987344d 100644 --- a/resources/views/partials/reports/table/footer.blade.php +++ b/resources/views/partials/reports/table/footer.blade.php @@ -1,11 +1,11 @@ - @php $total_total = 0; @endphp - @foreach($class->totals[$table] as $total) - @php $total_total += $total; @endphp + @php $grand_total = 0; @endphp + @foreach($class->footer_totals[$table] as $total) + @php $grand_total += $total; @endphp @endforeach - + diff --git a/resources/views/partials/reports/table/rows.blade.php b/resources/views/partials/reports/table/rows.blade.php index eaffc1ea0..e91d696c7 100644 --- a/resources/views/partials/reports/table/rows.blade.php +++ b/resources/views/partials/reports/table/rows.blade.php @@ -1,9 +1,9 @@ @php $row_total = 0; @endphp - - @foreach($items as $item) - @php $row_total += $item; @endphp - + + @foreach($rows as $row) + @php $row_total += $row; @endphp + @endforeach diff --git a/resources/views/reports/profit_loss/table/footer.blade.php b/resources/views/reports/profit_loss/table/footer.blade.php index e92f826ad..42262c86e 100644 --- a/resources/views/reports/profit_loss/table/footer.blade.php +++ b/resources/views/reports/profit_loss/table/footer.blade.php @@ -1,11 +1,11 @@ - @php $total_total = 0; @endphp - @foreach($class->totals[$table] as $date => $total) - @php $total_total += $total; @endphp + @php $grand_total = 0; @endphp + @foreach($class->footer_totals[$table] as $date => $total) + @php $grand_total += $total; @endphp @endforeach - + diff --git a/resources/views/reports/tax_summary/table/footer.blade.php b/resources/views/reports/tax_summary/table/footer.blade.php index 7dbb0dc2b..9e280f413 100644 --- a/resources/views/reports/tax_summary/table/footer.blade.php +++ b/resources/views/reports/tax_summary/table/footer.blade.php @@ -1,11 +1,11 @@ - @php $total_total = 0; @endphp - @foreach($class->totals[$table] as $total) - @php $total_total += $total; @endphp + @php $grand_total = 0; @endphp + @foreach($class->footer_totals[$table] as $total) + @php $grand_total += $total; @endphp @endforeach - +
{{ trans_choice('general.totals', 1) }}@money($total, setting('default.currency'), true)@money($total_total, setting('default.currency'), true)@money($grand_total, setting('default.currency'), true)
{{ $class->getTableRowList()[$id] }}@money($item, setting('default.currency'), true){{ $class->row_names[$table][$id] }}@money($row, setting('default.currency'), true)@money($row_total, setting('default.currency'), true)
{{ trans_choice('general.totals', 1) }}@money($total, setting('default.currency'), true)@money($total_total, setting('default.currency'), true)@money($grand_total, setting('default.currency'), true)
{{ trans('reports.net') }}@money($total, setting('default.currency'), true)@money($total_total, setting('default.currency'), true)@money($grand_total, setting('default.currency'), true)