diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 0fc43d99433d..6b2ee1e90f46 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -75,6 +75,7 @@ class ReportController extends BaseController 'activity', 'aging', 'client', + 'credit', 'document', 'expense', 'invoice', diff --git a/app/Ninja/Reports/CreditReport.php b/app/Ninja/Reports/CreditReport.php new file mode 100644 index 000000000000..d4e1711755b9 --- /dev/null +++ b/app/Ninja/Reports/CreditReport.php @@ -0,0 +1,59 @@ + [], + 'amount' => [], + 'balance' => [], + ]; + + return $columns; + } + + public function run() + { + $account = Auth::user()->account; + + $clients = Client::scope() + ->orderBy('name') + ->withArchived() + ->with(['credits' => function ($query) { + $query->where('credit_date', '>=', $this->startDate) + ->where('credit_date', '<=', $this->endDate) + ->withArchived(); + }]); + + foreach ($clients->get() as $client) { + $amount = 0; + $balance = 0; + + foreach ($client->credits as $credit) { + $amount += $credit->amount; + $balance += $credit->balance; + } + + if (! $amount && ! $balance) { + continue; + } + + $row = [ + $this->isExport ? $client->getDisplayName() : $client->present()->link, + $account->formatMoney($amount, $client), + $account->formatMoney($balance, $client), + ]; + + $this->data[] = $row; + + $this->addToTotals($client->currency_id, 'amount', $amount); + $this->addToTotals($client->currency_id, 'balance', $balance); + } + } +}