1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Import/Transformer/Csv/TaskTransformer.php

176 lines
5.4 KiB
PHP
Raw Normal View History

2023-11-14 03:24:51 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Import\Transformer\Csv;
use App\Import\Transformer\BaseTransformer;
2023-11-26 08:41:42 +01:00
use App\Models\TaskStatus;
2023-11-14 03:24:51 +01:00
/**
* Class TaskTransformer.
*/
class TaskTransformer extends BaseTransformer
{
private int $stubbed_timestamp = 0;
/**
* @param $data
*
* @return bool|array
*/
public function transform($task_items_data)
{
$this->stubbed_timestamp = time();
2023-11-26 08:41:42 +01:00
if(count($task_items_data) == count($task_items_data, COUNT_RECURSIVE)) {
2023-11-20 05:31:40 +01:00
$task_data = $task_items_data;
2023-11-26 08:41:42 +01:00
} else {
2023-11-20 05:31:40 +01:00
$task_data = reset($task_items_data);
2023-11-26 08:41:42 +01:00
}
2024-01-14 05:05:00 +01:00
2023-11-14 03:24:51 +01:00
$clientId = $this->getClient(
$this->getString($task_data, 'client.name'),
$this->getString($task_data, 'client.email')
);
2023-11-14 04:53:25 +01:00
$projectId = $task_data['project.name'] ?? '';
2023-11-14 03:24:51 +01:00
$transformed = [
'company_id' => $this->company->id,
'number' => $this->getString($task_data, 'task.number'),
'user_id' => $this->getString($task_data, 'task.user_id'),
'client_id' => $clientId,
2023-11-14 04:53:25 +01:00
'project_id' => $this->getProjectId($projectId, $clientId),
2023-11-14 03:24:51 +01:00
'description' => $this->getString($task_data, 'task.description'),
'status' => $this->getTaskStatusId($task_data),
'custom_value1' => $this->getString($task_data, 'task.custom_value1'),
'custom_value2' => $this->getString($task_data, 'task.custom_value2'),
'custom_value3' => $this->getString($task_data, 'task.custom_value3'),
'custom_value4' => $this->getString($task_data, 'task.custom_value4'),
];
2023-11-14 04:53:25 +01:00
if(count($task_items_data) == count($task_items_data, COUNT_RECURSIVE)) {
$transformed['time_log'] = json_encode([$this->parseLog($task_items_data)]);
return $transformed;
}
2023-11-14 03:24:51 +01:00
$time_log = collect($task_items_data)
->map(function ($item) {
return $this->parseLog($item);
})->toJson();
$transformed['time_log'] = $time_log;
return $transformed;
}
2023-11-14 04:53:25 +01:00
private function parseLog($item)
2023-11-14 03:24:51 +01:00
{
$start_date = false;
$end_date = false;
$notes = $item['task.notes'] ?? '';
2024-01-14 05:05:00 +01:00
2023-11-26 08:41:42 +01:00
if(isset($item['task.is_billable']) && is_string($item['task.is_billable']) && in_array($item['task.is_billable'], ['yes', 'true', '1'])) {
2023-11-14 04:53:25 +01:00
$is_billable = true;
2023-11-26 08:41:42 +01:00
} elseif(isset($item['task.is_billable']) && is_bool($item['task.is_billable'])) {
2023-11-14 04:53:25 +01:00
$is_billable = $item['task.is_billable'];
2023-11-26 08:41:42 +01:00
} else {
2023-11-14 04:53:25 +01:00
$is_billable = false;
2023-11-26 08:41:42 +01:00
}
2023-11-14 04:53:25 +01:00
if(isset($item['task.start_date']) &&
isset($item['task.end_date'])) {
2023-11-14 03:24:51 +01:00
$start_date = $this->resolveStartDate($item);
$end_date = $this->resolveEndDate($item);
2023-11-14 04:53:25 +01:00
} elseif(isset($item['task.duration'])) {
$duration = strtotime($item['task.duration']) - strtotime('TODAY');
2023-11-14 03:24:51 +01:00
$start_date = $this->stubbed_timestamp;
$end_date = $this->stubbed_timestamp + $duration;
2023-11-14 04:53:25 +01:00
$this->stubbed_timestamp;
2023-11-14 03:24:51 +01:00
} else {
2023-11-14 04:53:25 +01:00
return '';
2023-11-14 03:24:51 +01:00
}
return [$start_date, $end_date, $notes, $is_billable];
}
private function resolveStartDate($item)
{
2023-11-14 04:53:25 +01:00
$stub_start_date = $item['task.start_date'];
$stub_start_date .= isset($item['task.start_time']) ? " ".$item['task.start_time'] : '';
2023-11-14 03:24:51 +01:00
try {
2023-11-14 04:53:25 +01:00
2023-11-14 03:24:51 +01:00
$stub_start_date = \Carbon\Carbon::parse($stub_start_date);
$this->stubbed_timestamp = $stub_start_date->timestamp;
2024-01-14 05:05:00 +01:00
2023-11-14 03:24:51 +01:00
return $stub_start_date->timestamp;
} catch (\Exception $e) {
2023-11-14 04:53:25 +01:00
nlog($e->getMessage());
2023-11-14 03:24:51 +01:00
return $this->stubbed_timestamp;
}
2024-01-14 05:05:00 +01:00
2023-11-14 03:24:51 +01:00
}
private function resolveEndDate($item)
{
2023-11-14 04:53:25 +01:00
$stub_end_date = $item['task.end_date'];
$stub_end_date .= isset($item['task.end_time']) ? " ".$item['task.end_time'] : '';
2023-11-14 03:24:51 +01:00
try {
2023-11-14 04:53:25 +01:00
$stub_end_date = \Carbon\Carbon::parse($stub_end_date);
if($stub_end_date->timestamp == $this->stubbed_timestamp) {
$this->stubbed_timestamp;
2023-11-14 03:24:51 +01:00
return $this->stubbed_timestamp;
}
2023-11-14 04:53:25 +01:00
$this->stubbed_timestamp = $stub_end_date->timestamp;
return $stub_end_date->timestamp;
2023-11-14 03:24:51 +01:00
} catch (\Exception $e) {
2023-11-14 04:53:25 +01:00
nlog($e->getMessage());
return $this->stubbed_timestamp;
2023-11-14 03:24:51 +01:00
}
2024-01-14 05:05:00 +01:00
2023-11-14 03:24:51 +01:00
}
2023-11-14 04:53:25 +01:00
private function getTaskStatusId($item): ?int
{
2023-11-26 08:41:42 +01:00
if(isset($item['task.status'])) {
2023-11-14 04:53:25 +01:00
$name = strtolower(trim($item['task.status']));
$ts = TaskStatus::query()->where('company_id', $this->company->id)
->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->first();
2023-11-26 08:41:42 +01:00
if($ts) {
return $ts->id;
}
}
2023-11-14 04:53:25 +01:00
return TaskStatus::where('company_id', $this->company->id)
->where('is_deleted', false)
->orderBy('status_order', 'asc')
->first()->id ?? null;
}
2023-11-14 03:24:51 +01:00
}