mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-01 06:34:35 -04:00
Transformers for Projects and tasks
This commit is contained in:
parent
cc96fe9b38
commit
3f30965d54
@ -237,4 +237,23 @@ class Task extends BaseModel
|
|||||||
|
|
||||||
return $this->company->settings->default_task_rate ?? 0;
|
return $this->company->settings->default_task_rate ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function processLogs()
|
||||||
|
{
|
||||||
|
return
|
||||||
|
collect($this->timelog)->map(function ($log){
|
||||||
|
|
||||||
|
$parent_entity = $this->client ?? $this->company;
|
||||||
|
|
||||||
|
if($log[0])
|
||||||
|
$log[0] = Carbon::createFromTimestamp($log[0])->format($parent_entity->date_format());
|
||||||
|
|
||||||
|
if($log[1] && $log[1] != 0)
|
||||||
|
$log[1] = Carbon::createFromTimestamp($log[1])->format($parent_entity->date_format());
|
||||||
|
else
|
||||||
|
$log[1] = ctrans('texts.running');
|
||||||
|
|
||||||
|
return $log;
|
||||||
|
})->toArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -795,44 +795,86 @@ class TemplateService
|
|||||||
* @param mixed $tasks
|
* @param mixed $tasks
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function processTasks($tasks): array
|
public function processTasks($tasks, bool $nested = false): array
|
||||||
{
|
{
|
||||||
$it = new TaskTransformer();
|
|
||||||
$it->setDefaultIncludes(['client','project','invoice']);
|
|
||||||
$manager = new Manager();
|
|
||||||
$resource = new \League\Fractal\Resource\Collection($tasks, $it, null);
|
|
||||||
$resources = $manager->createData($resource)->toArray();
|
|
||||||
|
|
||||||
foreach($resources['data'] as $key => $resource) {
|
return collect($tasks)->map(function ($task) use ($nested){
|
||||||
|
|
||||||
$resources['data'][$key]['client'] = $resource['client']['data'] ?? [];
|
return [
|
||||||
$resources['data'][$key]['client']['contacts'] = $resource['client']['data']['contacts']['data'] ?? [];
|
'number' => (string) $task->number ?: '',
|
||||||
$resources['data'][$key]['project'] = $resource['project']['data'] ?? [];
|
'description' => (string) $task->description ?: '',
|
||||||
$resources['data'][$key]['invoice'] = $resource['invoice'] ?? [];
|
'duration' => $task->duration ?: 0,
|
||||||
|
'rate' => Number::formatMoney($task->rate ?? 0, $task->client ?? $task->company),
|
||||||
|
'created_at' => $this->translateDate($task->created_at, $task->client ? $task->client->date_format() : $task->company->date_format(), $task->client ? $task->client->locale() : $task->company->locale()),
|
||||||
|
'updated_at' => $this->translateDate($task->updated_at, $task->client ? $task->client->date_format() : $task->company->date_format(), $task->client ? $task->client->locale() : $task->company->locale()),
|
||||||
|
'date' => $task->calculated_start_date ? $this->translateDate($task->calculated_start_date , $task->client ? $task->client->date_format() : $task->company->date_format(), $task->client ? $task->client->locale() : $task->company->locale()) : '',
|
||||||
|
// 'invoice_id' => $this->encodePrimaryKey($task->invoice_id) ?: '',
|
||||||
|
'project' => ($task->project && !$nested) ? $this->transformProject($task->project, true) : [],
|
||||||
|
'time_log' => $task->processLogs(),
|
||||||
|
'custom_value1' => $task->custom_value1 ?: '',
|
||||||
|
'custom_value2' => $task->custom_value2 ?: '',
|
||||||
|
'custom_value3' => $task->custom_value3 ?: '',
|
||||||
|
'custom_value4' => $task->custom_value4 ?: '',
|
||||||
|
'status' => $task->status ? $task->status->name : '',
|
||||||
|
'client' => $task->client ? [
|
||||||
|
'name' => $task->client->present()->name(),
|
||||||
|
'balance' => $task->client->balance,
|
||||||
|
'payment_balance' => $task->client->payment_balance,
|
||||||
|
'credit_balance' => $task->client->credit_balance,
|
||||||
|
] : [],
|
||||||
|
];
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return $resources['data'];
|
|
||||||
|
|
||||||
|
})->toArray();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo refactor
|
* @todo refactor
|
||||||
*
|
*
|
||||||
* @param mixed $projects
|
* @param array | Collection $projects
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function processProjects($projects): array
|
public function processProjects($projects): array
|
||||||
{
|
{
|
||||||
|
|
||||||
$it = new ProjectTransformer();
|
return
|
||||||
$it->setDefaultIncludes(['client','tasks']);
|
collect($projects)->map(function ($project) ){
|
||||||
$manager = new Manager();
|
|
||||||
$manager->setSerializer(new ArraySerializer());
|
return $this->transformProject($project);
|
||||||
$resource = new \League\Fractal\Resource\Collection($projects, $it, Project::class);
|
|
||||||
$i = $manager->createData($resource)->toArray();
|
})->toArray();
|
||||||
return $i[Project::class];
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function transformProject(Project $project, bool $nested = false): array
|
||||||
|
{
|
||||||
|
|
||||||
|
return [
|
||||||
|
'name' => $project->name ?: '',
|
||||||
|
'number' => $project->number ?: '',
|
||||||
|
'created_at' => $this->translateDate($project->created_at, $project->client->date_format(), $project->client->locale()),
|
||||||
|
'updated_at' => $this->translateDate($project->updated_at, $project->client->date_format(), $project->client->locale()),
|
||||||
|
'task_rate' => Number::formatMoney($project->task_rate ?? 0, $project->client),
|
||||||
|
'due_date' => $project->due_date ? $this->translateDate($project->due_date, $project->client->date_format(), $project->client->locale()) : '',
|
||||||
|
'private_notes' => (string) $project->private_notes ?: '',
|
||||||
|
'public_notes' => (string) $project->public_notes ?: '',
|
||||||
|
'budgeted_hours' => (float) $project->budgeted_hours,
|
||||||
|
'custom_value1' => (string) $project->custom_value1 ?: '',
|
||||||
|
'custom_value2' => (string) $project->custom_value2 ?: '',
|
||||||
|
'custom_value3' => (string) $project->custom_value3 ?: '',
|
||||||
|
'custom_value4' => (string) $project->custom_value4 ?: '',
|
||||||
|
'color' => (string) $project->color ?: '',
|
||||||
|
'current_hours' => (int) $project->current_hours ?: 0,
|
||||||
|
'tasks' => ($project->tasks && !$nested) ? $this->processTasks($project->tasks, true) : [],
|
||||||
|
'client' => $project->client ? [
|
||||||
|
'name' => $project->client->present()->name(),
|
||||||
|
'balance' => $project->client->balance,
|
||||||
|
'payment_balance' => $project->client->payment_balance,
|
||||||
|
'credit_balance' => $project->client->credit_balance,
|
||||||
|
] : [],
|
||||||
|
|
||||||
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user