invoiceninja/app/Ninja/Reports/ClientReport.php

85 lines
2.7 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' => [],
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;
if ($account->custom_client_label1) {
2018-02-14 13:29:28 +02:00
$columns[$account->present()->customClientLabel1] = ['columnSelector-false', 'custom'];
2018-01-21 15:53:43 +02:00
}
if ($account->custom_client_label2) {
2018-02-14 13:29:28 +02:00
$columns[$account->present()->customClientLabel2] = ['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;
$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-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),
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-01-21 15:53:43 +02:00
if ($account->custom_client_label1) {
$row[] = $client->custom_value1;
}
if ($account->custom_client_label2) {
$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);
}
}
}