1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 17:31:35 +02:00
invoiceninja/app/Export/CSV/TaskExport.php

274 lines
8.8 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\Models\Task;
use App\Utils\Ninja;
2024-03-21 03:12:25 +01:00
use League\Csv\Writer;
use App\Models\Company;
use App\Models\Timezone;
use App\Libraries\MultiDB;
use App\Models\DateFormat;
use Carbon\CarbonInterval;
2022-04-29 00:47:19 +02:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
2024-03-21 03:12:25 +01:00
use App\Export\Decorators\Decorator;
use App\Transformers\TaskTransformer;
use Illuminate\Database\Eloquent\Builder;
2022-04-29 00:47:19 +02:00
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-12-02 04:09:50 +01:00
private Decorator $decorator;
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-12-02 04:09:50 +01:00
$this->decorator = new Decorator();
2022-04-29 00:47:19 +02:00
}
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
2023-10-18 06:22:38 +02:00
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
2022-11-04 20:54:05 +01:00
$query = Task::query()
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', $this->input['include_deleted'] ?? false);
2022-04-29 00:47:19 +02:00
$query = $this->addDateRange($query);
2024-03-26 04:12:32 +01:00
$clients = &$this->input['client_id'];
2022-04-29 00:47:19 +02:00
2024-03-26 04:12:32 +01:00
if($clients)
$query = $this->addClientFilter($query, $clients);
$document_attachments = &$this->input['document_email_attachment'];
if($document_attachments)
$this->queueDocuments($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();
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-09-12 03:55:11 +02:00
$query->cursor()
->each(function ($resource) {
2024-01-14 05:05:00 +01:00
2023-09-12 03:55:11 +02:00
$this->buildRow($resource);
2024-01-14 05:05:00 +01:00
2023-10-26 04:57:44 +02:00
foreach($this->storage_array as $row) {
2023-09-12 03:55:11 +02:00
$this->storage_item_array[] = $this->processMetaData($row, $resource);
}
$this->storage_array = [];
});
2024-01-16 20:50:13 +01:00
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);
2024-03-27 06:11:22 +01:00
2023-07-18 10:21:12 +02:00
foreach (array_values($this->input['report_keys']) as $key) {
2024-03-27 06:11:22 +01: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];
2023-12-02 04:09:50 +01:00
} elseif (in_array($key, ['task.start_date', 'task.end_date', 'task.duration'])) {
2023-12-02 05:23:49 +01:00
$entity[$key] = '';
2023-09-12 03:55:11 +02:00
} else {
2023-12-02 04:09:50 +01:00
$entity[$key] = $this->decorator->transform($key, $task);
2023-07-18 10:21:12 +02:00
}
2022-04-29 02:23:00 +02:00
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
}
2024-01-14 05:05:00 +01: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'])) {
2024-03-21 03:12:25 +01:00
$carbon_object = Carbon::createFromTimeStamp($item[0])->setTimezone($timezone_name);
$entity['task.start_date'] = $carbon_object->format($date_format_default);
$entity['task.start_time'] = $carbon_object->format('H:i:s');
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) {
2024-03-21 03:12:25 +01:00
$carbon_object = Carbon::createFromTimeStamp($item[1])->setTimezone($timezone_name);
$entity['task.end_date'] = $carbon_object->format($date_format_default);
$entity['task.end_time'] = $carbon_object->format('H:i:s');
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');
2024-03-21 03:12:25 +01:00
$entity['task.end_time'] = 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'])) {
2024-03-21 03:12:25 +01:00
$seconds = $task->calcDuration();
$entity['task.duration'] = $seconds;
2024-03-22 21:53:56 +01:00
$entity['task.duration_words'] = $seconds > 86400 ? CarbonInterval::seconds($seconds)->locale($this->company->locale())->cascade()->forHumans() : now()->startOfDay()->addSeconds($seconds)->format('H:i:s');
2022-04-29 03:31:19 +02:00
}
2024-01-14 05:05:00 +01:00
2023-12-12 00:46:23 +01:00
$entity = $this->decorateAdvancedFields($task, $entity);
2024-01-14 05:05:00 +01:00
2023-09-12 03:55:11 +02:00
$this->storage_array[] = $entity;
2024-01-14 05:05:00 +01:00
2024-01-16 20:50:13 +01:00
$entity['task.start_date'] = '';
2024-03-21 03:12:25 +01:00
$entity['task.start_time'] = '';
2024-01-16 20:50:13 +01:00
$entity['task.end_date'] = '';
2024-03-21 03:12:25 +01:00
$entity['task.end_time'] = '';
2024-01-16 20:50:13 +01:00
$entity['task.duration'] = '';
2024-03-21 03:12:25 +01:00
$entity['task.duration_words'] = '';
2022-04-29 03:31:19 +02:00
}
2023-09-12 03:55:11 +02:00
2022-04-29 03:31:19 +02:00
}
2024-03-05 03:58:26 +01:00
/**
* Add Task Status Filter
*
* @param Builder $query
* @param string $status
* @return Builder
*/
protected function addTaskStatusFilter(Builder $query, string $status): Builder
{
$status_parameters = explode(',', $status);
if (in_array('all', $status_parameters) || count($status_parameters) == 0) {
return $query;
}
if (in_array('invoiced', $status_parameters)) {
$query->whereNotNull('invoice_id');
}
if (in_array('uninvoiced', $status_parameters)) {
$query->whereNull('invoice_id');
}
return $query;
}
2022-04-29 00:47:19 +02:00
2024-01-14 05:05:00 +01:00
private function decorateAdvancedFields(Task $task, array $entity): array
2022-04-29 00:47:19 +02:00
{
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 : '';
}
2024-01-14 05:05:00 +01:00
2023-12-12 00:46:23 +01:00
if (in_array('task.user_id', $this->input['report_keys'])) {
$entity['task.user_id'] = $task->user ? $task->user->present()->name() : '';
}
if (in_array('task.assigned_user_id', $this->input['report_keys'])) {
$entity['task.assigned_user_id'] = $task->assigned_user ? $task->assigned_user->present()->name() : '';
}
2022-04-29 00:47:19 +02:00
return $entity;
}
}