1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Services/Report/UserSalesReport.php

120 lines
3.1 KiB
PHP
Raw Normal View History

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 03:18:50 +02:00
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 03:18:50 +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 03:18:50 +02:00
2023-04-14 05:04:28 +02:00
class UserSalesReport extends BaseExport
2023-04-14 03:18:50 +02:00
{
use MakesDates;
//Name
//Invoice count
//Amount
//Amount with Tax
public Writer $csv;
public string $date_key = 'created_at';
2023-04-14 05:27:47 +02:00
public array $report_keys = [
'name',
'invoices',
'invoice_amount',
'total_taxes',
];
2023-04-14 03:18:50 +02:00
/**
2023-10-26 04:57:44 +02:00
@param array $input
2023-04-14 03:18:50 +02:00
[
'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();
$query = Invoice::query()
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID]);
$query = $this->addDateRange($query);
$query = $this->filterByClients($query);
2023-04-14 05:15:15 +02:00
$this->csv->insertOne([ctrans('texts.user_sales_report_header', ['client' => $this->client_description, 'start_date' => $this->start_date, 'end_date' => $this->end_date])]);
2023-04-14 05:27:47 +02:00
if (count($this->input['report_keys']) == 0) {
$this->input['report_keys'] = $this->report_keys;
}
2023-04-14 03:18:50 +02:00
$this->csv->insertOne($this->buildHeader());
$users = $this->company->users;
2023-10-26 04:57:44 +02:00
$report = $users->map(function ($user) use ($query) {
2023-04-14 03:18:50 +02:00
$new_query = $query;
$new_query->where('user_id', $user->id);
return [
$user->present()->name(),
$new_query->count(),
Number::formatMoney($new_query->sum('amount'), $this->company),
Number::formatMoney($new_query->sum('total_taxes'), $this->company),
];
})->toArray();
$key_values = array_column($report, 1);
array_multisort($key_values, SORT_DESC, $report);
$this->csv->insertAll($report);
return $this->csv->toString();
}
2023-04-14 06:12:29 +02:00
public function buildHeader() :array
{
$header = [];
foreach ($this->input['report_keys'] as $value) {
2023-04-14 03:18:50 +02:00
2023-04-14 06:12:29 +02:00
$header[] = ctrans("texts.{$value}");
}
return $header;
}
2023-04-14 03:18:50 +02:00
}