2023-04-14 05:04:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2023-04-14 05:04:28 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Report;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Export\CSV\BaseExport;
|
|
|
|
use App\Libraries\MultiDB;
|
2023-04-14 05:04:28 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Invoice;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Utils\Ninja;
|
2023-04-14 05:04:28 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
|
|
|
use Illuminate\Support\Facades\App;
|
2023-10-26 04:57:44 +02:00
|
|
|
use League\Csv\Writer;
|
2023-04-14 05:04:28 +02:00
|
|
|
|
|
|
|
class ClientBalanceReport extends BaseExport
|
|
|
|
{
|
|
|
|
use MakesDates;
|
|
|
|
//Name
|
|
|
|
//Invoice count
|
|
|
|
//Amount
|
|
|
|
//Amount with Tax
|
|
|
|
public Writer $csv;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-14 05:04:28 +02:00
|
|
|
public string $date_key = 'created_at';
|
|
|
|
|
|
|
|
public array $report_keys = [
|
|
|
|
'client_name',
|
|
|
|
'client_number',
|
|
|
|
'id_number',
|
2023-04-24 05:58:25 +02:00
|
|
|
'invoices',
|
2023-04-14 05:15:15 +02:00
|
|
|
'invoice_balance',
|
|
|
|
'credit_balance',
|
2024-08-28 03:02:57 +02:00
|
|
|
'payment_balance',
|
2023-04-14 05:04:28 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param array $input
|
|
|
|
[
|
|
|
|
'date_range',
|
|
|
|
'start_date',
|
|
|
|
'end_date',
|
|
|
|
'clients',
|
|
|
|
'client_id',
|
|
|
|
]
|
|
|
|
*/
|
|
|
|
public function __construct(public Company $company, public array $input)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
App::forgetInstance('translator');
|
|
|
|
App::setLocale($this->company->locale());
|
|
|
|
$t = app('translator');
|
|
|
|
$t->replace(Ninja::transformTranslations($this->company->settings));
|
|
|
|
|
|
|
|
$this->csv = Writer::createFromString();
|
2024-05-01 00:32:11 +02:00
|
|
|
\League\Csv\CharsetConverter::addTo($this->csv, 'UTF-8', 'UTF-8');
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-14 05:04:28 +02:00
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
2023-04-24 05:58:25 +02:00
|
|
|
$this->csv->insertOne([ctrans('texts.client_balance_report')]);
|
2023-04-14 05:04:28 +02:00
|
|
|
$this->csv->insertOne([ctrans('texts.created_on'),' ',$this->translateDate(now()->format('Y-m-d'), $this->company->date_format(), $this->company->locale())]);
|
|
|
|
|
2023-04-14 06:12:29 +02:00
|
|
|
if (count($this->input['report_keys']) == 0) {
|
|
|
|
$this->input['report_keys'] = $this->report_keys;
|
|
|
|
}
|
|
|
|
|
2023-04-14 05:04:28 +02:00
|
|
|
$this->csv->insertOne($this->buildHeader());
|
|
|
|
|
|
|
|
Client::query()
|
|
|
|
->where('company_id', $this->company->id)
|
|
|
|
->where('is_deleted', 0)
|
|
|
|
->orderBy('balance', 'desc')
|
|
|
|
->cursor()
|
2023-10-26 04:57:44 +02:00
|
|
|
->each(function ($client) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-14 05:04:28 +02:00
|
|
|
$this->csv->insertOne($this->buildRow($client));
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-14 05:04:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return $this->csv->toString();
|
2023-04-14 05:15:15 +02:00
|
|
|
|
2023-04-14 05:04:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function buildHeader(): array
|
|
|
|
{
|
|
|
|
$headers = [];
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
foreach($this->report_keys as $key) {
|
2023-04-14 05:04:28 +02:00
|
|
|
$headers[] = ctrans("texts.{$key}");
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-04-14 05:04:28 +02:00
|
|
|
|
|
|
|
return $headers;
|
|
|
|
|
|
|
|
}
|
|
|
|
private function buildRow(Client $client): array
|
|
|
|
{
|
2023-08-06 09:35:19 +02:00
|
|
|
$query = Invoice::query()->where('client_id', $client->id)
|
2023-04-14 05:04:28 +02:00
|
|
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-06-23 23:56:13 +02:00
|
|
|
$query = $this->addDateRange($query, 'invoices');
|
2023-04-14 05:04:28 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
$client->present()->name(),
|
|
|
|
$client->number,
|
|
|
|
$client->id_number,
|
|
|
|
$query->count(),
|
|
|
|
$query->sum('balance'),
|
2023-04-14 05:15:15 +02:00
|
|
|
$client->credit_balance,
|
2024-08-28 03:02:57 +02:00
|
|
|
$client->payment_balance,
|
2023-04-14 05:04:28 +02:00
|
|
|
];
|
|
|
|
}
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|