1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Export/CSV/TaskExport.php

227 lines
6.9 KiB
PHP
Raw Normal View History

2022-04-29 00:47:19 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-04-29 00:47:19 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\DateFormat;
use App\Models\Task;
2022-04-29 03:31:19 +02:00
use App\Models\Timezone;
2022-04-29 00:47:19 +02:00
use App\Transformers\TaskTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class TaskExport extends BaseExport
{
private Company $company;
protected array $input;
private $entity_transformer;
protected $date_key = 'created_at';
private string $date_format = 'YYYY-MM-DD';
protected array $entity_keys = [
'start_date' => 'start_date',
'end_date' => 'end_date',
'duration' => 'duration',
'rate' => 'rate',
'number' => 'number',
'description' => 'description',
'custom_value1' => 'custom_value1',
'custom_value2' => 'custom_value2',
'custom_value3' => 'custom_value3',
'custom_value4' => 'custom_value4',
'status' => 'status_id',
'project' => 'project_id',
'invoice' => 'invoice_id',
'client' => 'client_id',
];
private array $decorate_keys = [
'status',
'project',
'client',
'invoice',
'start_date',
'end_date',
'duration',
];
public function __construct(Company $company, array $input)
{
$this->company = $company;
$this->input = $input;
$this->entity_transformer = new TaskTransformer();
}
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->date_format = DateFormat::find($this->company->settings->date_format_id)->format;
2022-04-29 00:47:19 +02:00
//load the CSV document from a string
$this->csv = Writer::createFromString();
2022-05-10 10:05:15 +02:00
ksort($this->entity_keys);
2022-05-07 09:10:23 +02:00
if (count($this->input['report_keys']) == 0) {
2022-05-10 10:05:15 +02:00
$this->input['report_keys'] = array_values($this->entity_keys);
}
2022-05-07 09:10:23 +02:00
2022-04-29 00:47:19 +02:00
//insert the header
$this->csv->insertOne($this->buildHeader());
2022-11-04 20:54:05 +01:00
$query = Task::query()
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0);
2022-04-29 00:47:19 +02:00
$query = $this->addDateRange($query);
$query->cursor()
->each(function ($entity) {
$this->buildRow($entity);
});
2022-04-29 00:47:19 +02:00
return $this->csv->toString();
2022-04-29 00:47:19 +02:00
}
private function buildRow(Task $task)
2022-04-29 02:23:00 +02:00
{
$entity = [];
$transformed_entity = $this->entity_transformer->transform($task);
if (is_null($task->time_log) || (is_array(json_decode($task->time_log, 1)) && count(json_decode($task->time_log, 1)) == 0)) {
foreach (array_values($this->input['report_keys']) as $key) {
2022-05-10 10:05:15 +02:00
$keyval = array_search($key, $this->entity_keys);
if (array_key_exists($key, $transformed_entity)) {
2022-05-10 10:05:15 +02:00
$entity[$keyval] = $transformed_entity[$key];
} else {
$entity[$keyval] = '';
}
2022-04-29 02:23:00 +02:00
}
$entity['start_date'] = '';
$entity['end_date'] = '';
$entity['duration'] = '';
2022-05-10 10:05:15 +02:00
2022-04-29 02:23:00 +02:00
$entity = $this->decorateAdvancedFields($task, $entity);
2022-05-10 10:05:15 +02:00
ksort($entity);
2022-04-29 03:31:19 +02:00
$this->csv->insertOne($entity);
} elseif (is_array(json_decode($task->time_log, 1)) && count(json_decode($task->time_log, 1)) > 0) {
foreach (array_values($this->input['report_keys']) as $key) {
2022-05-10 10:05:15 +02:00
$keyval = array_search($key, $this->entity_keys);
if (array_key_exists($key, $transformed_entity)) {
2022-05-10 10:05:15 +02:00
$entity[$keyval] = $transformed_entity[$key];
} else {
$entity[$keyval] = '';
}
2022-04-29 00:47:19 +02:00
}
2022-04-29 03:31:19 +02:00
$this->iterateLogs($task, $entity);
2022-04-29 00:47:19 +02:00
}
2022-04-29 03:31:19 +02:00
}
2022-04-29 00:47:19 +02:00
2022-04-29 03:31:19 +02:00
private function iterateLogs(Task $task, array $entity)
{
$timezone = Timezone::find($task->company->settings->timezone_id);
$timezone_name = 'US/Eastern';
2022-04-29 00:47:19 +02:00
if ($timezone) {
2022-04-29 03:31:19 +02:00
$timezone_name = $timezone->name;
}
$logs = json_decode($task->time_log, 1);
2022-04-29 00:47:19 +02:00
$date_format_default = 'Y-m-d';
2022-05-10 10:05:15 +02:00
$date_format = DateFormat::find($task->company->settings->date_format_id);
if ($date_format) {
2022-05-10 10:05:15 +02:00
$date_format_default = $date_format->format;
}
2022-04-29 00:47:19 +02:00
foreach ($logs as $key => $item) {
if (in_array('start_date', $this->input['report_keys'])) {
2022-05-10 10:05:15 +02:00
$entity['start_date'] = Carbon::createFromTimeStamp($item[0])->setTimezone($timezone_name)->format($date_format_default);
2022-04-29 03:31:19 +02:00
}
2022-04-29 00:47:19 +02:00
if (in_array('end_date', $this->input['report_keys']) && $item[1] > 0) {
2022-05-10 10:05:15 +02:00
$entity['end_date'] = Carbon::createFromTimeStamp($item[1])->setTimezone($timezone_name)->format($date_format_default);
2022-04-29 03:31:19 +02:00
}
2022-04-29 00:47:19 +02:00
if (in_array('end_date', $this->input['report_keys']) && $item[1] == 0) {
2022-04-29 03:31:19 +02:00
$entity['end_date'] = ctrans('texts.is_running');
2022-04-29 00:47:19 +02:00
}
if (in_array('duration', $this->input['report_keys'])) {
2022-04-29 03:31:19 +02:00
$entity['duration'] = $task->calcDuration();
}
2022-04-29 00:47:19 +02:00
if (! array_key_exists('duration', $entity)) {
$entity['duration'] = '';
}
2022-05-10 10:05:15 +02:00
if (! array_key_exists('start_date', $entity)) {
$entity['start_date'] = '';
}
2022-05-10 10:05:15 +02:00
if (! array_key_exists('end_date', $entity)) {
$entity['end_date'] = '';
}
2022-05-10 10:05:15 +02:00
$entity = $this->decorateAdvancedFields($task, $entity);
2022-05-10 10:05:15 +02:00
ksort($entity);
2022-04-29 03:31:19 +02:00
$this->csv->insertOne($entity);
2022-04-29 00:47:19 +02:00
2022-04-29 03:31:19 +02:00
unset($entity['start_date']);
unset($entity['end_date']);
unset($entity['duration']);
}
}
2022-04-29 00:47:19 +02:00
private function decorateAdvancedFields(Task $task, array $entity) :array
{
if (in_array('status_id', $this->input['report_keys'])) {
2022-05-10 10:05:15 +02:00
$entity['status'] = $task->status()->exists() ? $task->status->name : '';
}
2022-04-29 00:47:19 +02:00
if (in_array('project_id', $this->input['report_keys'])) {
2022-05-10 10:05:15 +02:00
$entity['project'] = $task->project()->exists() ? $task->project->name : '';
}
2022-04-29 00:47:19 +02:00
if (in_array('client_id', $this->input['report_keys'])) {
$entity['client'] = $task->client ? $task->client->present()->name() : '';
}
2022-04-29 00:47:19 +02:00
if (in_array('invoice_id', $this->input['report_keys'])) {
$entity['invoice'] = $task->invoice ? $task->invoice->number : '';
}
2022-04-29 00:47:19 +02:00
return $entity;
}
}