1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Export/CSV/TaskExport.php

213 lines
6.6 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;
2023-09-12 03:55:11 +02:00
use Illuminate\Database\Eloquent\Builder;
2022-04-29 00:47:19 +02:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class TaskExport extends BaseExport
{
private $entity_transformer;
2023-03-08 08:02:04 +01:00
public string $date_key = 'created_at';
2022-04-29 00:47:19 +02:00
private string $date_format = 'YYYY-MM-DD';
2023-03-08 21:49:18 +01:00
public Writer $csv;
2023-09-12 03:55:11 +02:00
private array $storage_array = [];
private array $storage_item_array = [];
2022-04-29 00:47:19 +02:00
public function __construct(Company $company, array $input)
{
$this->company = $company;
$this->input = $input;
$this->entity_transformer = new TaskTransformer();
}
2023-09-12 03:55:11 +02:00
public function init(): Builder
2022-04-29 00:47:19 +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;
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) {
2023-09-12 03:55:11 +02:00
$this->input['report_keys'] = array_values($this->task_report_keys);
}
2022-05-07 09:10:23 +02:00
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);
2023-09-12 03:55:11 +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());
2022-04-29 00:47:19 +02:00
$query->cursor()
->each(function ($entity) {
$this->buildRow($entity);
});
2022-04-29 00:47:19 +02:00
2023-09-12 03:55:11 +02:00
$this->csv->insertAll($this->storage_array);
return $this->csv->toString();
2022-04-29 00:47:19 +02:00
}
2023-09-12 03:55:11 +02:00
public function returnJson()
{
$query = $this->init();
$headerdisplay = $this->buildHeader();
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
2023-10-06 18:43:02 +02:00
return ['identifier' => $key, 'display_value' => $headerdisplay[$value]];
2023-09-12 03:55:11 +02:00
})->toArray();
$query->cursor()
->each(function ($resource) {
$this->buildRow($resource);
foreach($this->storage_array as $row)
{
$this->storage_item_array[] = $this->processMetaData($row, $resource);
}
$this->storage_array = [];
});
2023-09-12 05:55:37 +02:00
nlog($this->storage_item_array);
2023-09-12 03:55:11 +02:00
return array_merge(['columns' => $header], $this->storage_item_array);
}
private function buildRow(Task $task)
2022-04-29 02:23:00 +02:00
{
$entity = [];
$transformed_entity = $this->entity_transformer->transform($task);
2023-07-18 10:21:12 +02:00
foreach (array_values($this->input['report_keys']) as $key) {
2022-04-29 02:23:00 +02:00
2023-09-12 03:55:11 +02:00
$parts = explode('.', $key);
2022-05-10 10:05:15 +02:00
2023-09-12 03:55:11 +02:00
if (is_array($parts) && $parts[0] == 'task' && array_key_exists($parts[1], $transformed_entity)) {
$entity[$key] = $transformed_entity[$parts[1]];
} elseif (array_key_exists($key, $transformed_entity)) {
$entity[$key] = $transformed_entity[$key];
} else {
$entity[$key] = $this->resolveKey($key, $task, $this->entity_transformer);
2023-07-18 10:21:12 +02:00
}
2022-04-29 02:23:00 +02:00
2023-07-18 10:21:12 +02:00
}
2023-09-12 05:55:37 +02:00
$entity['task.start_date'] = '';
$entity['task.end_date'] = '';
$entity['task.duration'] = '';
2023-07-18 10:21:12 +02:00
if (is_null($task->time_log) || (is_array(json_decode($task->time_log, 1)) && count(json_decode($task->time_log, 1)) == 0)) {
2023-09-12 03:55:11 +02:00
$this->storage_array[] = $entity;
2023-07-18 10:21:12 +02:00
} else {
2022-04-29 03:31:19 +02:00
$this->iterateLogs($task, $entity);
2022-04-29 00:47:19 +02:00
}
2023-07-18 10:21:12 +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) {
2023-07-18 10:21:12 +02:00
if (in_array('task.start_date', $this->input['report_keys']) || in_array('start_date', $this->input['report_keys'])) {
2023-09-12 05:02:27 +02:00
$entity['task.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
2023-07-18 10:21:12 +02:00
if ((in_array('task.end_date', $this->input['report_keys']) || in_array('end_date', $this->input['report_keys'])) && $item[1] > 0) {
2023-09-12 05:02:27 +02:00
$entity['task.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
2023-07-18 10:21:12 +02:00
if ((in_array('task.end_date', $this->input['report_keys']) || in_array('end_date', $this->input['report_keys'])) && $item[1] == 0) {
2023-09-12 05:02:27 +02:00
$entity['task.end_date'] = ctrans('texts.is_running');
2022-04-29 00:47:19 +02:00
}
2023-07-18 10:21:12 +02:00
if (in_array('task.duration', $this->input['report_keys']) || in_array('duration', $this->input['report_keys'])) {
2023-09-12 05:02:27 +02:00
$entity['task.duration'] = $task->calcDuration();
2022-04-29 03:31:19 +02:00
}
2023-07-18 10:21:12 +02:00
2022-05-10 10:05:15 +02:00
$entity = $this->decorateAdvancedFields($task, $entity);
2023-07-18 10:21:12 +02:00
2023-09-12 03:55:11 +02:00
$this->storage_array[] = $entity;
2023-07-18 10:21:12 +02:00
2023-09-12 05:55:37 +02:00
unset($entity['task.start_date']);
unset($entity['task.end_date']);
unset($entity['task.duration']);
2022-04-29 03:31:19 +02:00
}
2023-09-12 03:55:11 +02:00
2022-04-29 03:31:19 +02:00
}
2022-04-29 00:47:19 +02:00
private function decorateAdvancedFields(Task $task, array $entity) :array
{
2023-09-12 05:55:37 +02:00
if (in_array('task.status_id', $this->input['report_keys'])) {
$entity['task.status_id'] = $task->status()->exists() ? $task->status->name : '';
}
2022-04-29 00:47:19 +02:00
2023-09-12 05:55:37 +02:00
if (in_array('task.project_id', $this->input['report_keys'])) {
$entity['task.project_id'] = $task->project()->exists() ? $task->project->name : '';
}
2023-09-12 05:55:37 +02:00
2022-04-29 00:47:19 +02:00
return $entity;
}
}