2023-04-14 03:18:50 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @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:27:47 +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;
|
|
|
|
use App\Utils\Number;
|
2023-04-14 05:27:47 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Carbon\Carbon;
|
2023-04-14 05:27:47 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
2023-10-26 04:57:44 +02:00
|
|
|
use League\Csv\Writer;
|
2023-04-14 05:27:47 +02:00
|
|
|
|
|
|
|
class ARDetailReport extends BaseExport
|
2023-04-14 03:18:50 +02:00
|
|
|
{
|
2023-04-14 05:27:47 +02:00
|
|
|
use MakesDates;
|
|
|
|
//Date
|
|
|
|
//Invoice #
|
|
|
|
//Status
|
|
|
|
//Customer
|
|
|
|
//Age - Days
|
|
|
|
//Amount
|
|
|
|
//Balance
|
|
|
|
|
|
|
|
public Writer $csv;
|
|
|
|
|
|
|
|
public string $date_key = 'created_at';
|
|
|
|
|
|
|
|
public array $report_keys = [
|
|
|
|
'date',
|
2023-04-24 05:58:25 +02:00
|
|
|
'due_date',
|
2023-04-14 05:27:47 +02:00
|
|
|
'invoice_number',
|
|
|
|
'status',
|
|
|
|
'client_name',
|
|
|
|
'client_number',
|
|
|
|
'id_number',
|
|
|
|
'age',
|
|
|
|
'amount',
|
|
|
|
'balance',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
@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();
|
|
|
|
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([]);
|
|
|
|
$this->csv->insertOne([ctrans('texts.aged_receivable_detailed_report')]);
|
|
|
|
$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:27:47 +02:00
|
|
|
$this->csv->insertOne($this->buildHeader());
|
|
|
|
|
|
|
|
$query = Invoice::query()
|
|
|
|
->withTrashed()
|
|
|
|
->where('company_id', $this->company->id)
|
|
|
|
->where('is_deleted', 0)
|
|
|
|
->where('balance', '>', 0)
|
|
|
|
->orderBy('due_date', 'ASC')
|
|
|
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]);
|
|
|
|
|
|
|
|
$query = $this->addDateRange($query);
|
|
|
|
|
|
|
|
$query = $this->filterByClients($query);
|
|
|
|
|
|
|
|
$query->cursor()
|
|
|
|
->each(function ($invoice) {
|
2023-10-26 04:57:44 +02:00
|
|
|
$this->csv->insertOne($this->buildRow($invoice));
|
2023-04-14 05:27:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return $this->csv->toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildRow(Invoice $invoice): array
|
|
|
|
{
|
|
|
|
$client = $invoice->client;
|
|
|
|
|
|
|
|
return [
|
2023-04-24 05:58:25 +02:00
|
|
|
$this->translateDate($invoice->date, $this->company->date_format(), $this->company->locale()),
|
2023-04-14 05:27:47 +02:00
|
|
|
$this->translateDate($invoice->due_date, $this->company->date_format(), $this->company->locale()),
|
|
|
|
$invoice->number,
|
|
|
|
$invoice->stringStatus($invoice->status_id),
|
|
|
|
$client->present()->name(),
|
|
|
|
$client->number,
|
|
|
|
$client->id_number,
|
|
|
|
Carbon::parse($invoice->due_date)->diffInDays(now()),
|
|
|
|
Number::formatMoney($invoice->amount, $client),
|
|
|
|
Number::formatMoney($invoice->balance, $client),
|
|
|
|
];
|
|
|
|
}
|
2023-04-14 06:12:29 +02:00
|
|
|
|
|
|
|
public function buildHeader() :array
|
|
|
|
{
|
|
|
|
$header = [];
|
|
|
|
|
|
|
|
foreach ($this->input['report_keys'] as $value) {
|
|
|
|
|
|
|
|
$header[] = ctrans("texts.{$value}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $header;
|
|
|
|
}
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|