invoiceninja/app/Ninja/Reports/ClientReport.php

97 lines
3.3 KiB
PHP
Raw Normal View History

2017-01-22 12:09:29 +02:00
<?php
namespace App\Ninja\Reports;
use App\Models\Client;
2017-01-30 21:40:43 +02:00
use Auth;
2017-01-22 12:09:29 +02:00
class ClientReport extends AbstractReport
{
2018-01-21 15:53:43 +02:00
public function getColumns()
{
$columns = [
2018-01-23 20:32:11 +02:00
'client' => [],
'amount' => [],
'paid' => [],
'balance' => [],
'id_number' => ['columnSelector-false'],
'vat_number' => ['columnSelector-false'],
2018-01-21 15:53:43 +02:00
'public_notes' => ['columnSelector-false'],
'private_notes' => ['columnSelector-false'],
2018-01-26 14:11:23 +02:00
'user' => ['columnSelector-false'],
2018-01-21 15:53:43 +02:00
];
$user = auth()->user();
$account = $user->account;
2018-04-04 16:24:59 +03:00
if ($account->customLabel('client1')) {
$columns[$account->present()->customLabel('client1')] = ['columnSelector-false', 'custom'];
2018-01-21 15:53:43 +02:00
}
2018-04-04 16:24:59 +03:00
if ($account->customLabel('client2')) {
$columns[$account->present()->customLabel('client2')] = ['columnSelector-false', 'custom'];
2018-01-21 15:53:43 +02:00
}
return $columns;
}
2017-01-22 12:09:29 +02:00
public function run()
{
$account = Auth::user()->account;
2018-02-28 14:43:15 +02:00
$subgroup = $this->options['subgroup'];
2017-01-22 12:09:29 +02:00
$clients = Client::scope()
2017-03-31 09:01:07 +03:00
->orderBy('name')
2017-01-22 12:09:29 +02:00
->withArchived()
2018-01-26 14:11:23 +02:00
->with(['contacts', 'user'])
2017-01-30 18:05:31 +02:00
->with(['invoices' => function ($query) {
2017-01-22 12:09:29 +02:00
$query->where('invoice_date', '>=', $this->startDate)
->where('invoice_date', '<=', $this->endDate)
->where('invoice_type_id', '=', INVOICE_TYPE_STANDARD)
->where('is_recurring', '=', false)
->withArchived();
}]);
foreach ($clients->get() as $client) {
$amount = 0;
$paid = 0;
foreach ($client->invoices as $invoice) {
$amount += $invoice->amount;
$paid += $invoice->getAmountPaid();
2018-02-27 09:14:39 +02:00
2018-02-28 16:57:10 +02:00
if ($subgroup == 'country') {
$dimension = $client->present()->country;
} else {
$dimension = $this->getDimension($client);
}
2018-02-28 14:43:15 +02:00
$this->addChartData($dimension, $invoice->invoice_date, $invoice->amount);
2017-01-22 12:09:29 +02:00
}
2018-01-21 15:53:43 +02:00
$row = [
2017-01-22 12:09:29 +02:00
$this->isExport ? $client->getDisplayName() : $client->present()->link,
$account->formatMoney($amount, $client),
$account->formatMoney($paid, $client),
2017-01-30 21:40:43 +02:00
$account->formatMoney($amount - $paid, $client),
$client->id_number,
$client->vat_number,
2018-01-21 15:53:43 +02:00
$client->public_notes,
$client->private_notes,
2018-01-26 14:11:23 +02:00
$client->user->getDisplayName(),
2017-01-22 12:09:29 +02:00
];
2018-04-04 16:24:59 +03:00
if ($account->customLabel('client1')) {
2018-01-21 15:53:43 +02:00
$row[] = $client->custom_value1;
}
2018-04-04 16:24:59 +03:00
if ($account->customLabel('client2')) {
2018-01-21 15:53:43 +02:00
$row[] = $client->custom_value2;
}
$this->data[] = $row;
2017-01-22 12:09:29 +02:00
$this->addToTotals($client->currency_id, 'amount', $amount);
$this->addToTotals($client->currency_id, 'paid', $paid);
$this->addToTotals($client->currency_id, 'balance', $amount - $paid);
}
}
}