From 5f5a1a6740eb9154ee88bdd5f39d094b811f9fd8 Mon Sep 17 00:00:00 2001 From: Kristian Feldsam Date: Tue, 24 Mar 2020 09:51:11 +0100 Subject: [PATCH 1/2] Fix ExportReportResults (#3506) Signed-off-by: Kristian Feldsam --- app/Jobs/ExportReportResults.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/Jobs/ExportReportResults.php b/app/Jobs/ExportReportResults.php index eacaf6e724a5..3325f95ae763 100644 --- a/app/Jobs/ExportReportResults.php +++ b/app/Jobs/ExportReportResults.php @@ -65,7 +65,17 @@ class ExportReportResults extends Job foreach ($totals as $currencyId => $each) { foreach ($each as $dimension => $val) { $tmp = []; - $tmp[] = Utils::getFromCache($currencyId, 'currencies')->name . (($dimension) ? ' - ' . $dimension : ''); + + $currency = Utils::getFromCache($currencyId, 'currencies'); + if (!$currency) { + $name = $currencyId; + $account = $this->user->account->first(); + $currencyId = $account->currency_id; + } else { + $name = $currency->name; + } + + $tmp[] = $dimension ? $name . ' - ' . $dimension : $name; foreach ($val as $field => $value) { if ($field == 'duration') { $tmp[] = Utils::formatTime($value); From ecc5a00600aa6c093891b0d2babe02a1e3c02bc3 Mon Sep 17 00:00:00 2001 From: Kristian Feldsam Date: Tue, 24 Mar 2020 09:51:37 +0100 Subject: [PATCH 2/2] Reports - Added "Total" of all currencies (#3518) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Reports - Invoices - Added TAX to totals Signed-off-by: Kristián Feldsam * Reports - Add totals calculated from all currencies Signed-off-by: Kristian Feldsam --- app/Console/Commands/SendReminders.php | 2 +- app/Http/Controllers/ReportController.php | 9 ++++--- app/Jobs/RunReport.php | 5 ++-- app/Ninja/Reports/AbstractReport.php | 24 ++++++++++++++++--- app/Ninja/Reports/InvoiceReport.php | 1 + .../views/reports/report_builder.blade.php | 11 +++++++-- 6 files changed, 41 insertions(+), 11 deletions(-) diff --git a/app/Console/Commands/SendReminders.php b/app/Console/Commands/SendReminders.php index bd06bce12355..3d290f3f22a2 100644 --- a/app/Console/Commands/SendReminders.php +++ b/app/Console/Commands/SendReminders.php @@ -229,7 +229,7 @@ class SendReminders extends Command // send email as user auth()->onceUsingId($user->id); - $report = dispatch_now(new RunReport($scheduledReport->user, $reportType, $config, true)); + $report = dispatch_now(new RunReport($scheduledReport->user, $reportType, $config, $account, true)); $file = dispatch_now(new ExportReportResults($scheduledReport->user, $config['export_format'], $reportType, $report->exportParams)); if ($file) { diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index aa1e6bc84f53..60614366f483 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -61,6 +61,8 @@ class ReportController extends BaseController $action = Input::get('action'); $format = Input::get('format'); + $account = Auth::user()->account; + if (Input::get('report_type')) { $reportType = Input::get('report_type'); $dateField = Input::get('date_field'); @@ -96,10 +98,10 @@ class ReportController extends BaseController 'reportTypes' => array_combine($reportTypes, Utils::trans($reportTypes)), 'reportType' => $reportType, 'title' => trans('texts.charts_and_reports'), - 'account' => Auth::user()->account, + 'account' => $account, ]; - if (Auth::user()->account->hasFeature(FEATURE_REPORTS)) { + if ($account->hasFeature(FEATURE_REPORTS)) { $isExport = $action == 'export'; $config = [ 'date_field' => $dateField, @@ -112,7 +114,8 @@ class ReportController extends BaseController 'start_date' => $params['startDate'], 'end_date' => $params['endDate'], ]; - $report = dispatch_now(new RunReport(auth()->user(), $reportType, $config, $isExport)); + + $report = dispatch_now(new RunReport(auth()->user(), $reportType, $config, $account, $isExport)); $params = array_merge($params, $report->exportParams); switch ($action) { case 'export': diff --git a/app/Jobs/RunReport.php b/app/Jobs/RunReport.php index e48cffeee662..50935c8c73db 100644 --- a/app/Jobs/RunReport.php +++ b/app/Jobs/RunReport.php @@ -10,11 +10,12 @@ use App\Jobs\Job; class RunReport extends Job { - public function __construct($user, $reportType, $config, $isExport = false) + public function __construct($user, $reportType, $config, $account, $isExport = false) { $this->user = $user; $this->reportType = $reportType; $this->config = $config; + $this->account = $account; $this->isExport = $isExport; } @@ -71,7 +72,7 @@ class RunReport extends Job $endDate = $config['end_date']; } - $report = new $reportClass($startDate, $endDate, $isExport, $config); + $report = new $reportClass($startDate, $endDate, $isExport, $this->account, $config); $report->run(); $params = [ diff --git a/app/Ninja/Reports/AbstractReport.php b/app/Ninja/Reports/AbstractReport.php index 595ef8daef47..d64a75f30bd6 100644 --- a/app/Ninja/Reports/AbstractReport.php +++ b/app/Ninja/Reports/AbstractReport.php @@ -2,6 +2,8 @@ namespace App\Ninja\Reports; +use App\Libraries\MoneyUtils; +use App\Models\Currency; use Utils; use Auth; use Carbon; @@ -15,17 +17,19 @@ class AbstractReport public $startDate; public $endDate; public $isExport; + private $account; public $options; public $totals = []; public $data = []; public $chartData = []; - public function __construct($startDate, $endDate, $isExport, $options = false) + public function __construct($startDate, $endDate, $isExport, $account, $options = false) { $this->startDate = $startDate; $this->endDate = $endDate; $this->isExport = $isExport; + $this->account = $account; $this->options = $options; } @@ -41,6 +45,8 @@ class AbstractReport public function results() { + asort($this->totals); + return [ 'columns' => $this->getColumns(), 'displayData' => $this->data, @@ -53,14 +59,26 @@ class AbstractReport $currencyId = $currencyId ?: Auth::user()->account->getCurrencyId(); if (! isset($this->totals[$currencyId][$dimension])) { - $this->totals[$currencyId][$dimension] = []; - } + $this->totals[$currencyId][$dimension] = []; + } if (! isset($this->totals[$currencyId][$dimension][$field])) { $this->totals[$currencyId][$dimension][$field] = 0; } $this->totals[$currencyId][$dimension][$field] += $value; + + if($currencyId !== 'Total') $this->addTotalToTotals($currencyId, $field, $value, $dimension); + } + + protected function addTotalToTotals($currencyId, $field, $value, $dimension = false) + { + if ($currencyId != $this->account->getCurrencyId()) { + $currency = Currency::where('id', $currencyId)->first(); + $value = MoneyUtils::convert($value, $currency->code, $this->account->currency->code); + } + + $this->addToTotals('Total', $field, $value, $dimension); } public function tableHeaderArray() { diff --git a/app/Ninja/Reports/InvoiceReport.php b/app/Ninja/Reports/InvoiceReport.php index ddd410521cea..be54bb08a1af 100644 --- a/app/Ninja/Reports/InvoiceReport.php +++ b/app/Ninja/Reports/InvoiceReport.php @@ -146,6 +146,7 @@ class InvoiceReport extends AbstractReport $this->addToTotals($client->currency_id, 'amount', $invoice->amount); $this->addToTotals($client->currency_id, 'balance', $invoice->balance); + $this->addToTotals($client->currency_id, 'tax', $invoice->getTaxTotal()); if ($subgroup == 'status') { $dimension = $invoice->statusLabel(); diff --git a/resources/views/reports/report_builder.blade.php b/resources/views/reports/report_builder.blade.php index 5ce42ab5c93d..744e25b94b39 100644 --- a/resources/views/reports/report_builder.blade.php +++ b/resources/views/reports/report_builder.blade.php @@ -286,7 +286,12 @@ @foreach ($reportTotals as $currencyId => $each) @foreach ($each as $dimension => $val) - {!! Utils::getFromCache($currencyId, 'currencies')->name !!} + + @if ($currencyId == 'Total') + Total + @else + {!! Utils::getFromCache($currencyId, 'currencies')->name !!} + @endif @if ($dimension) - {{ $dimension }} @endif @@ -295,8 +300,10 @@ @if ($field == 'duration') {{ Utils::formatTime($value) }} + @elseif ($currencyId == 'Total') + {{ Utils::formatMoney($value, $account->getCurrencyId()) }} @else - {{ Utils::formatMoney($value, $currencyId) }} + {{ Utils::formatMoney($value, $currencyId) }} @endif @endforeach