1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Transformers/ProductTransformer.php

97 lines
2.9 KiB
PHP
Raw Normal View History

2019-04-03 02:09:22 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
2019-04-03 02:09:22 +02:00
namespace App\Transformers;
2019-04-03 03:17:21 +02:00
use App\Models\Company;
2020-06-22 23:56:32 +02:00
use App\Models\Document;
2019-04-03 02:09:22 +02:00
use App\Models\Product;
2019-04-03 03:17:21 +02:00
use App\Models\User;
2019-04-03 02:09:22 +02:00
use App\Utils\Traits\MakesHash;
2020-10-28 11:10:49 +01:00
use League\Fractal\Resource\Collection;
2019-04-03 02:09:22 +02:00
class ProductTransformer extends EntityTransformer
{
use MakesHash;
2019-04-03 03:17:21 +02:00
protected $defaultIncludes = [
2020-08-06 05:04:09 +02:00
'documents',
2019-04-03 03:17:21 +02:00
];
/**
* @var array
*/
protected $availableIncludes = [
'company',
2020-06-22 23:56:32 +02:00
'user',
'documents',
2019-04-03 03:17:21 +02:00
];
/**
* @param Product $product
*
2020-10-28 11:10:49 +01:00
* @return Collection
2019-04-03 03:17:21 +02:00
*/
public function includeUser(Product $product)
{
$transformer = new UserTransformer($this->serializer);
return $this->includeItem($product->user, $transformer, User::class);
}
/**
* @param Product $product
*
2020-10-28 11:10:49 +01:00
* @return Collection
2019-04-03 03:17:21 +02:00
*/
public function includeCompany(Product $product)
{
$transformer = new CompanyTransformer($this->serializer);
return $this->includeItem($product->company, $transformer, Company::class);
}
2020-06-22 23:56:32 +02:00
public function includeDocuments(Product $product)
{
$transformer = new DocumentTransformer($this->serializer);
2020-06-22 23:56:32 +02:00
return $this->includeCollection($product->documents, $transformer, Document::class);
}
2019-04-03 02:09:22 +02:00
public function transform(Product $product)
{
return [
'id' => $this->encodePrimaryKey($product->id),
'user_id' => $this->encodePrimaryKey($product->user_id),
'assigned_user_id' => $this->encodePrimaryKey($product->assigned_user_id),
2019-09-27 00:14:02 +02:00
'product_key' => $product->product_key ?: '',
'notes' => $product->notes ?: '',
2019-10-02 09:41:22 +02:00
'cost' => (float) $product->cost ?: 0,
'price' => (float) $product->price ?: 0,
'quantity' => (float) $product->quantity ?: 1.0,
2019-04-03 02:09:22 +02:00
'tax_name1' => $product->tax_name1 ?: '',
2019-10-02 09:41:22 +02:00
'tax_rate1' => (float) $product->tax_rate1 ?: 0,
2019-04-03 02:09:22 +02:00
'tax_name2' => $product->tax_name2 ?: '',
2019-10-02 09:41:22 +02:00
'tax_rate2' => (float) $product->tax_rate2 ?: 0,
'tax_name3' => $product->tax_name3 ?: '',
'tax_rate3' => (float) $product->tax_rate3 ?: 0,
'created_at' => (int) $product->created_at,
'updated_at' => (int) $product->updated_at,
'archived_at' => (int) $product->deleted_at,
2019-04-03 02:09:22 +02:00
'custom_value1' => $product->custom_value1 ?: '',
'custom_value2' => $product->custom_value2 ?: '',
2019-09-26 01:47:33 +02:00
'custom_value3' => $product->custom_value3 ?: '',
'custom_value4' => $product->custom_value4 ?: '',
2019-04-03 02:09:22 +02:00
'is_deleted' => (bool) $product->is_deleted,
];
}
}