1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Export/CSV/ActivityExport.php

175 lines
5.5 KiB
PHP
Raw Normal View History

2023-04-19 05:26:22 +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\Export\CSV;
use App\Libraries\MultiDB;
2023-10-26 04:57:44 +02:00
use App\Models\Activity;
use App\Models\Company;
2023-04-19 05:26:22 +02:00
use App\Models\DateFormat;
2023-10-26 04:57:44 +02:00
use App\Models\Task;
use App\Transformers\ActivityTransformer;
use App\Utils\Ninja;
use Illuminate\Database\Eloquent\Builder;
2023-04-19 05:26:22 +02:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
2023-10-26 04:57:44 +02:00
use League\Csv\Writer;
2023-04-19 05:26:22 +02:00
class ActivityExport extends BaseExport
{
2023-04-19 05:26:22 +02:00
private $entity_transformer;
public string $date_key = 'created_at';
private string $date_format = 'YYYY-MM-DD';
public Writer $csv;
public array $entity_keys = [
'date' => 'date',
'activity' => 'activity',
'address' => 'address',
];
public function __construct(Company $company, array $input)
{
$this->company = $company;
$this->input = $input;
$this->entity_transformer = new ActivityTransformer();
}
2023-08-27 14:02:55 +02:00
public function returnJson()
{
$query = $this->init();
$headerdisplay = $this->buildHeader();
2023-10-26 04:57:44 +02:00
$header = collect($this->input['report_keys'])->map(function ($key, $value) use ($headerdisplay) {
return ['identifier' => $key, 'display_value' => $headerdisplay[$value]];
})->toArray();
2023-08-27 14:02:55 +02:00
2023-09-14 07:34:02 +02:00
2023-10-26 04:57:44 +02:00
$report = $query->cursor()
->map(function ($resource) {
$row = $this->buildActivityRow($resource);
return $this->processMetaData($row, $resource);
})->toArray();
2023-08-27 14:02:55 +02:00
return array_merge(['columns' => $header], $report);
}
private function buildActivityRow(Activity $activity): array
{
2023-10-26 04:57:44 +02:00
return [
Carbon::parse($activity->created_at)->format($this->date_format),
ctrans("texts.activity_{$activity->activity_type_id}", [
'payment_amount' => $activity->payment ? $activity->payment->amount : '',
'adjustment' => $activity->payment ? $activity->payment->refunded : '',
'client' => $activity->client ? $activity->client->present()->name() : '',
'contact' => $activity->contact ? $activity->contact->present()->name() : '',
'quote' => $activity->quote ? $activity->quote->number : '',
'user' => $activity->user ? $activity->user->present()->name() : 'System',
'expense' => $activity->expense ? $activity->expense->number : '',
'invoice' => $activity->invoice ? $activity->invoice->number : '',
'recurring_invoice' => $activity->recurring_invoice ? $activity->recurring_invoice->number : '',
'payment' => $activity->payment ? $activity->payment->number : '',
'credit' => $activity->credit ? $activity->credit->number : '',
'task' => $activity->task ? $activity->task->number : '',
'vendor' => $activity->vendor ? $activity->vendor->present()->name() : '',
'purchase_order' => $activity->purchase_order ? $activity->purchase_order->number : '',
'subscription' => $activity->subscription ? $activity->subscription->name : '',
'vendor_contact' => $activity->vendor_contact ? $activity->vendor_contact->present()->name() : '',
'recurring_expense' => $activity->recurring_expense ? $activity->recurring_expense->number : '',
]),
$activity->ip,
2023-08-27 14:02:55 +02:00
];
}
private function init(): Builder
2023-04-19 05:26:22 +02:00
{
MultiDB::setDb($this->company->db);
App::forgetInstance('translator');
App::setLocale($this->company->locale());
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->company->settings));
$this->date_format = DateFormat::find($this->company->settings->date_format_id)->format;
2023-09-14 07:34:02 +02:00
// ksort($this->entity_keys);
2023-04-19 05:26:22 +02:00
if (count($this->input['report_keys']) == 0) {
$this->input['report_keys'] = array_values($this->entity_keys);
}
$query = Activity::query()
->where('company_id', $this->company->id);
$query = $this->addDateRange($query);
2023-08-27 14:02:55 +02:00
return $query;
}
public function run()
{
$query = $this->init();
//load the CSV document from a string
$this->csv = Writer::createFromString();
//insert the header
$this->csv->insertOne($this->buildHeader());
2023-04-19 05:26:22 +02:00
$query->cursor()
->each(function ($entity) {
$this->buildRow($entity);
});
return $this->csv->toString();
}
private function buildRow(Activity $activity)
{
2023-08-27 14:02:55 +02:00
$this->csv->insertOne($this->buildActivityRow($activity));
2023-04-19 05:26:22 +02:00
}
private function decorateAdvancedFields(Task $task, array $entity) :array
{
return $entity;
}
2023-09-14 07:34:02 +02:00
public function processMetaData(array $row, $resource): array
{
$clean_row = [];
foreach (array_values($this->input['report_keys']) as $key => $value) {
2023-09-14 07:34:02 +02:00
$clean_row[$key]['entity'] = 'activity';
$clean_row[$key]['id'] = $key;
$clean_row[$key]['hashed_id'] = null;
$clean_row[$key]['value'] = $row[$key];
$clean_row[$key]['identifier'] = $value;
$clean_row[$key]['display_value'] = $row[$key];
}
return $clean_row;
2023-10-26 04:57:44 +02:00
}
2023-09-14 07:34:02 +02:00
2023-04-19 05:26:22 +02:00
}