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

298 lines
10 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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;
2024-08-01 03:15:02 +02:00
public string $date_key = 'calculated_start_date';
2022-04-29 00:47:19 +02:00
2024-06-17 02:14:04 +02:00
private string $date_format = 'Y-m-d';
2022-04-29 00:47:19 +02:00
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);
2024-06-14 09:09:44 +02:00
if(!$this->input['include_deleted'] ?? false) {
$query->where('is_deleted', 0);
}
2022-04-29 00:47:19 +02:00
$query = $this->addDateRange($query, 'tasks');
2024-06-14 09:09:44 +02:00
2024-03-26 04:12:32 +01:00
$clients = &$this->input['client_id'];
2022-04-29 00:47:19 +02:00
2024-06-14 09:09:44 +02:00
if($clients) {
2024-03-26 04:12:32 +01:00
$query = $this->addClientFilter($query, $clients);
2024-06-14 09:09:44 +02:00
}
2024-03-26 04:12:32 +01:00
$document_attachments = &$this->input['document_email_attachment'];
2024-06-14 09:09:44 +02:00
if($document_attachments) {
$this->queueDocuments($query);
2024-06-14 09:09:44 +02:00
}
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();
2024-05-01 00:32:11 +02:00
\League\Csv\CharsetConverter::addTo($this->csv, 'UTF-8', 'UTF-8');
2023-09-12 03:55:11 +02:00
//insert the header
$this->csv->insertOne($this->buildHeader());
2022-04-29 00:47:19 +02:00
$query->cursor()
->each(function ($entity) {
2024-08-22 08:45:06 +02:00
/** @var \App\Models\Task $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
2024-07-14 13:28:54 +02:00
/** @var \App\Models\Task $resource*/
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];
} elseif (in_array($key, ['task.start_date', 'task.end_date', 'task.duration', 'task.billable', 'task.item_notes', 'task.time_log'])) {
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
}
$entity = $this->decorateAdvancedFields($task, $entity);
$entity = $this->convertFloats($entity);
2024-07-11 07:53:18 +02:00
if (is_null($task->time_log) || (is_array(json_decode($task->time_log, true)) && count(json_decode($task->time_log, true)) == 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);
2024-07-29 05:56:54 +02:00
$timezone_name = 'America/New_York';
2022-04-29 00:47:19 +02:00
if ($timezone) {
2022-04-29 03:31:19 +02:00
$timezone_name = $timezone->name;
}
2024-07-11 07:53:18 +02:00
$logs = json_decode($task->time_log, true);
2022-04-29 00:47:19 +02:00
2024-06-17 02:14:04 +02:00
$date_format_default = $this->date_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'])) {
$carbon_object = Carbon::createFromTimeStamp((int)$item[0])->setTimezone($timezone_name);
2024-03-21 03:12:25 +01:00
$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) {
$carbon_object = Carbon::createFromTimeStamp((int)$item[1])->setTimezone($timezone_name);
2024-03-21 03:12:25 +01:00
$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');
$time_log_entry = (isset($item[1]) && $item[1] != 0) ? $item[1] - $item[0] : ctrans('texts.is_running');
$entity['task.time_log'] = $time_log_entry;
$entity['task.time_log_duration_words'] = is_int($time_log_entry) && $time_log_entry > 86400 ? CarbonInterval::seconds($time_log_entry)->locale($this->company->locale())->cascade()->forHumans() : $time_log_entry;
2024-08-22 08:45:06 +02:00
2022-04-29 03:31:19 +02:00
}
2024-01-14 05:05:00 +01:00
2024-07-29 05:56:54 +02:00
if (in_array('task.billable', $this->input['report_keys']) || in_array('billable', $this->input['report_keys'])) {
$entity['task.billable'] = isset($item[3]) && $item[3] == 'true' ? ctrans('texts.yes') : ctrans('texts.no');
}
if (in_array('task.item_notes', $this->input['report_keys']) || in_array('item_notes', $this->input['report_keys'])) {
$entity['task.item_notes'] = isset($item[2]) ? (string)$item[2] : '';
}
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'] = '';
$entity['task.time_log'] = '';
$entity['task.time_log_duration_words'];
2024-07-29 05:56:54 +02:00
$entity['task.billable'] = '';
$entity['task.item_notes'] = '';
2024-03-21 03:12:25 +01:00
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-06-14 09:09:44 +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
{
2024-06-17 02:14:04 +02:00
/** @var array $status_parameters */
2024-03-05 03:58:26 +01:00
$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;
}
}