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

124 lines
4.0 KiB
PHP
Raw Normal View History

2020-01-21 01:32:34 +01:00
<?php
2023-06-15 14:33:48 +02:00
2020-01-21 01:32:34 +01:00
/**
* Invoice Ninja (https://invoiceninja.com).
2020-01-21 01:32:34 +01:00
*
* @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)
2020-01-21 01:32:34 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-01-21 01:32:34 +01:00
*/
namespace App\Transformers;
2024-03-22 05:16:22 +01:00
use App\Models\Task;
use App\Models\Quote;
2023-06-15 14:33:48 +02:00
use App\Models\Client;
2020-01-21 01:32:34 +01:00
use App\Models\Project;
2024-03-22 05:16:22 +01:00
use App\Models\Document;
use App\Models\Expense;
use App\Models\Invoice;
2020-01-21 01:32:34 +01:00
use App\Utils\Traits\MakesHash;
/**
* class ProjectTransformer.
2020-01-21 01:32:34 +01:00
*/
class ProjectTransformer extends EntityTransformer
{
use MakesHash;
2023-08-21 03:29:31 +02:00
protected array $defaultIncludes = [
2020-10-11 23:34:02 +02:00
'documents',
2020-01-21 01:32:34 +01:00
];
/**
* @var array
*/
2023-08-21 03:29:31 +02:00
protected array $availableIncludes = [
2023-06-15 14:33:48 +02:00
'client',
2023-06-15 14:43:51 +02:00
'tasks',
2024-03-22 05:16:22 +01:00
'invoices',
'expenses',
'quotes',
2020-01-21 01:32:34 +01:00
];
2020-10-11 23:34:02 +02:00
public function includeDocuments(Project $project)
{
$transformer = new DocumentTransformer($this->serializer);
2024-02-18 06:19:09 +01:00
if($project->documents)
return $this->includeCollection($project->documents, $transformer, Document::class);
return null;
2020-10-11 23:34:02 +02:00
}
2023-06-15 14:33:48 +02:00
public function includeClient(Project $project): \League\Fractal\Resource\Item
{
2024-03-10 03:05:28 +01:00
if (!$project->client) {
nlog("Project {$project->hashed_id} does not have a client attached - this project is in a bad state");
return null;
}
2023-06-15 14:33:48 +02:00
$transformer = new ClientTransformer($this->serializer);
return $this->includeItem($project->client, $transformer, Client::class);
}
2023-06-15 14:43:51 +02:00
public function includeTasks(Project $project): \League\Fractal\Resource\Collection
{
$transformer = new TaskTransformer($this->serializer);
return $this->includeCollection($project->tasks, $transformer, Task::class);
}
2024-03-22 05:16:22 +01:00
public function includeInvoices(Project $project): \League\Fractal\Resource\Collection
{
$transformer = new InvoiceTransformer($this->serializer);
return $this->includeCollection($project->invoices, $transformer, Invoice::class);
}
public function includeExpenses(Project $project): \League\Fractal\Resource\Collection
{
$transformer = new ExpenseTransformer($this->serializer);
return $this->includeCollection($project->expenses, $transformer, Expense::class);
}
public function includeQuotes(Project $project): \League\Fractal\Resource\Collection
{
$transformer = new QuoteTransformer($this->serializer);
return $this->includeCollection($project->quotes, $transformer, Quote::class);
}
2020-01-21 01:32:34 +01:00
public function transform(Project $project)
{
return [
'id' => (string) $this->encodePrimaryKey($project->id),
2020-10-08 00:25:39 +02:00
'user_id' => (string) $this->encodePrimaryKey($project->user_id),
'assigned_user_id' => (string) $this->encodePrimaryKey($project->assigned_user_id),
2020-01-21 01:32:34 +01:00
'client_id' => (string) $this->encodePrimaryKey($project->client_id),
2020-10-08 00:25:39 +02:00
'name' => $project->name ?: '',
2020-12-17 21:11:31 +01:00
'number' => $project->number ?: '',
'created_at' => (int) $project->created_at,
'updated_at' => (int) $project->updated_at,
'archived_at' => (int) $project->deleted_at,
2020-01-21 01:32:34 +01:00
'is_deleted' => (bool) $project->is_deleted,
'task_rate' => (float) $project->task_rate,
'due_date' => $project->due_date ?: '',
2020-10-08 00:25:39 +02:00
'private_notes' => (string) $project->private_notes ?: '',
'public_notes' => (string) $project->public_notes ?: '',
2020-01-21 01:32:34 +01:00
'budgeted_hours' => (float) $project->budgeted_hours,
2020-10-08 00:25:39 +02:00
'custom_value1' => (string) $project->custom_value1 ?: '',
'custom_value2' => (string) $project->custom_value2 ?: '',
'custom_value3' => (string) $project->custom_value3 ?: '',
'custom_value4' => (string) $project->custom_value4 ?: '',
2021-01-05 05:41:43 +01:00
'color' => (string) $project->color ?: '',
2023-04-04 13:34:01 +02:00
'current_hours' => (int) $project->current_hours ?: 0,
2020-01-21 01:32:34 +01:00
];
}
}