2020-01-20 02:31:58 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Transformers;
|
|
|
|
|
2020-10-11 23:34:02 +02:00
|
|
|
use App\Models\Document;
|
2020-01-20 02:31:58 +01:00
|
|
|
use App\Models\Expense;
|
2022-07-21 16:54:47 +02:00
|
|
|
use App\Models\Vendor;
|
2020-01-20 02:31:58 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-02-27 00:32:44 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2022-07-21 16:50:34 +02:00
|
|
|
use League\Fractal\Resource\Item;
|
2020-01-20 02:31:58 +01:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* class ExpenseTransformer.
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
class ExpenseTransformer extends EntityTransformer
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2020-02-27 00:32:44 +01:00
|
|
|
use SoftDeletes;
|
2020-10-19 12:59:58 +02:00
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
protected $defaultIncludes = [
|
2020-10-11 23:34:02 +02:00
|
|
|
'documents',
|
2020-01-20 02:31:58 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2022-07-21 16:50:34 +02:00
|
|
|
protected $availableIncludes = [
|
|
|
|
'client',
|
2022-07-21 16:54:47 +02:00
|
|
|
'vendor',
|
2022-07-21 16:50:34 +02:00
|
|
|
];
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2020-10-11 23:34:02 +02:00
|
|
|
public function includeDocuments(Expense $expense)
|
|
|
|
{
|
|
|
|
$transformer = new DocumentTransformer($this->serializer);
|
|
|
|
|
|
|
|
return $this->includeCollection($expense->documents, $transformer, Document::class);
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:50:34 +02:00
|
|
|
public function includeClient(Expense $expense): ?Item
|
|
|
|
{
|
|
|
|
$transformer = new ClientTransformer($this->serializer);
|
|
|
|
|
|
|
|
if (!$expense->client) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->includeItem($expense->client, $transformer, Client::class);
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:54:47 +02:00
|
|
|
public function includeVendor(Expense $expense): ?Item
|
|
|
|
{
|
|
|
|
$transformer = new VendorTransformer($this->serializer);
|
|
|
|
|
|
|
|
if (!$expense->vendor) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->includeItem($expense->vendor, $transformer, Vendor::class);
|
|
|
|
}
|
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
/**
|
|
|
|
* @param Expense $expense
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(Expense $expense)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $this->encodePrimaryKey($expense->id),
|
|
|
|
'user_id' => $this->encodePrimaryKey($expense->user_id),
|
|
|
|
'assigned_user_id' => $this->encodePrimaryKey($expense->assigned_user_id),
|
|
|
|
'vendor_id' => $this->encodePrimaryKey($expense->vendor_id),
|
|
|
|
'invoice_id' => $this->encodePrimaryKey($expense->invoice_id),
|
|
|
|
'client_id' => $this->encodePrimaryKey($expense->client_id),
|
2020-09-06 11:38:10 +02:00
|
|
|
'bank_id' => (string) $expense->bank_id ?: '',
|
|
|
|
'invoice_currency_id' => (string) $expense->invoice_currency_id ?: '',
|
2020-10-30 10:17:29 +01:00
|
|
|
'expense_currency_id' => '', //todo remove redundant in 5.0.25
|
2020-12-13 21:58:18 +01:00
|
|
|
'currency_id' => (string) $expense->currency_id ?: '',
|
2020-10-26 20:10:04 +01:00
|
|
|
'category_id' => $this->encodePrimaryKey($expense->category_id),
|
2020-09-06 11:38:10 +02:00
|
|
|
'payment_type_id' => (string) $expense->payment_type_id ?: '',
|
|
|
|
'recurring_expense_id' => (string) $expense->recurring_expense_id ?: '',
|
2020-01-20 02:31:58 +01:00
|
|
|
'is_deleted' => (bool) $expense->is_deleted,
|
|
|
|
'should_be_invoiced' => (bool) $expense->should_be_invoiced,
|
|
|
|
'invoice_documents' => (bool) $expense->invoice_documents,
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => (float) $expense->amount ?: 0,
|
|
|
|
'foreign_amount' => (float) $expense->foreign_amount ?: 0,
|
|
|
|
'exchange_rate' => (float) $expense->exchange_rate ?: 0,
|
2020-01-20 02:31:58 +01:00
|
|
|
'tax_name1' => $expense->tax_name1 ? $expense->tax_name1 : '',
|
|
|
|
'tax_rate1' => (float) $expense->tax_rate1,
|
|
|
|
'tax_name2' => $expense->tax_name2 ? $expense->tax_name2 : '',
|
|
|
|
'tax_rate2' => (float) $expense->tax_rate2,
|
|
|
|
'tax_name3' => $expense->tax_name3 ? $expense->tax_name3 : '',
|
|
|
|
'tax_rate3' => (float) $expense->tax_rate3,
|
|
|
|
'private_notes' => (string) $expense->private_notes ?: '',
|
|
|
|
'public_notes' => (string) $expense->public_notes ?: '',
|
|
|
|
'transaction_reference' => (string) $expense->transaction_reference ?: '',
|
|
|
|
'transaction_id' => (string) $expense->transaction_id ?: '',
|
2020-10-29 01:17:10 +01:00
|
|
|
'date' => $expense->date ?: '',
|
2020-10-22 08:46:02 +02:00
|
|
|
'number' => (string)$expense->number ?: '',
|
2020-01-20 02:31:58 +01:00
|
|
|
'payment_date' => $expense->payment_date ?: '',
|
|
|
|
'custom_value1' => $expense->custom_value1 ?: '',
|
|
|
|
'custom_value2' => $expense->custom_value2 ?: '',
|
|
|
|
'custom_value3' => $expense->custom_value3 ?: '',
|
|
|
|
'custom_value4' => $expense->custom_value4 ?: '',
|
2020-09-06 11:38:10 +02:00
|
|
|
'updated_at' => (int) $expense->updated_at,
|
|
|
|
'archived_at' => (int) $expense->deleted_at,
|
|
|
|
'created_at' => (int) $expense->created_at,
|
2020-10-14 22:58:20 +02:00
|
|
|
'project_id' => $this->encodePrimaryKey($expense->project_id),
|
2021-01-08 11:27:49 +01:00
|
|
|
'tax_amount1' => (float) $expense->tax_amount1,
|
|
|
|
'tax_amount2' => (float) $expense->tax_amount2,
|
|
|
|
'tax_amount3' => (float) $expense->tax_amount3,
|
|
|
|
'uses_inclusive_taxes' => (bool) $expense->uses_inclusive_taxes,
|
2021-01-11 22:57:48 +01:00
|
|
|
'calculate_tax_by_amount' => (bool) $expense->calculate_tax_by_amount,
|
2021-08-27 12:45:09 +02:00
|
|
|
'entity_type' => 'expense',
|
2020-01-20 02:31:58 +01:00
|
|
|
];
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|