invoiceninja/app/Transformers/PaymentTransformer.php

110 lines
3.9 KiB
PHP
Raw Normal View History

2019-03-27 15:50:13 +11:00
<?php
2019-05-11 13:32:07 +10:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 13:32:07 +10:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 13:32:07 +10:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 13:32:07 +10:00
*/
2019-03-27 15:50:13 +11:00
2019-03-28 13:36:36 +11:00
namespace App\Transformers;
2019-03-27 15:50:13 +11:00
use App\Models\Client;
2020-06-23 07:56:32 +10:00
use App\Models\Document;
2019-03-27 15:50:13 +11:00
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Paymentable;
2019-04-03 11:09:22 +11:00
use App\Utils\Traits\MakesHash;
2019-03-27 15:50:13 +11:00
class PaymentTransformer extends EntityTransformer
{
2019-04-03 11:09:22 +11:00
use MakesHash;
2019-03-28 13:36:36 +11:00
protected $serializer;
2020-06-24 20:17:42 +10:00
protected $defaultIncludes = [
2020-08-15 10:51:26 +10:00
'paymentables',
2020-08-06 13:04:09 +10:00
'documents',
2020-06-24 20:17:42 +10:00
];
2019-03-27 15:50:13 +11:00
protected $availableIncludes = [
'client',
'invoices',
2020-06-23 08:47:42 +10:00
'paymentables',
'documents',
2019-03-27 15:50:13 +11:00
];
2019-03-28 13:36:36 +11:00
public function __construct($serializer = null)
2019-03-27 15:50:13 +11:00
{
parent::__construct();
2019-03-28 13:36:36 +11:00
$this->serializer = $serializer;
2019-03-27 15:50:13 +11:00
}
public function includeInvoices(Payment $payment)
2019-03-27 15:50:13 +11:00
{
2019-03-28 13:36:36 +11:00
$transformer = new InvoiceTransformer($this->serializer);
2019-03-27 15:50:13 +11:00
return $this->includeCollection($payment->invoices, $transformer, Invoice::class);
2019-03-27 15:50:13 +11:00
}
public function includeClient(Payment $payment)
{
2019-03-28 13:36:36 +11:00
$transformer = new ClientTransformer($this->serializer);
2019-03-27 15:50:13 +11:00
2019-03-28 13:36:36 +11:00
return $this->includeItem($payment->client, $transformer, Client::class);
2019-03-27 15:50:13 +11:00
}
public function includePaymentables(Payment $payment)
{
$transformer = new PaymentableTransformer($this->serializer);
return $this->includeCollection($payment->paymentables, $transformer, Paymentable::class);
}
2020-06-23 07:56:32 +10:00
public function includeDocuments(Payment $payment)
{
$transformer = new DocumentTransformer($this->serializer);
2020-06-23 07:56:32 +10:00
return $this->includeCollection($payment->documents, $transformer, Document::class);
}
2019-03-27 15:50:13 +11:00
public function transform(Payment $payment)
{
2019-03-28 13:36:36 +11:00
return [
2019-04-03 11:09:22 +11:00
'id' => $this->encodePrimaryKey($payment->id),
'user_id' => $this->encodePrimaryKey($payment->user_id),
'assigned_user_id' => $this->encodePrimaryKey($payment->assigned_user_id),
2019-03-27 15:50:13 +11:00
'amount' => (float) $payment->amount,
'refunded' => (float) $payment->refunded,
'applied' => (float) $payment->applied,
2019-03-27 15:50:13 +11:00
'transaction_reference' => $payment->transaction_reference ?: '',
'date' => $payment->date ?: '',
'is_manual' => (bool) $payment->is_manual,
'created_at' => (int) $payment->created_at,
'updated_at' => (int) $payment->updated_at,
'archived_at' => (int) $payment->deleted_at,
2019-03-27 15:50:13 +11:00
'is_deleted' => (bool) $payment->is_deleted,
2020-06-18 08:55:35 +10:00
'type_id' => (string) $payment->type_id ?: '',
2019-10-02 18:50:54 +10:00
'invitation_id' => (string) $payment->invitation_id ?: '',
'private_notes' => (string) $payment->private_notes ?: '',
'number' => (string) $payment->number ?: '',
'custom_value1' => (string) $payment->custom_value1 ?: '',
'custom_value2' => (string) $payment->custom_value2 ?: '',
'custom_value3' => (string) $payment->custom_value3 ?: '',
'custom_value4' => (string) $payment->custom_value4 ?: '',
'client_id' => (string) $this->encodePrimaryKey($payment->client_id),
'client_contact_id' => (string) $this->encodePrimaryKey($payment->client_contact_id),
'company_gateway_id' => (string) $this->encodePrimaryKey($payment->company_gateway_id),
'status_id'=> (string) $payment->status_id,
'project_id' => (string) $this->encodePrimaryKey($payment->project_id),
'vendor_id' => (string) $this->encodePrimaryKey($payment->vendor_id),
'currency_id' => (string) $payment->currency_id ?: '',
2020-03-06 22:21:17 +11:00
'exchange_rate' => (float) $payment->exchange_rate ?: 1,
'exchange_currency_id' => (string) $payment->exchange_currency_id ?: '',
2019-04-20 08:47:10 +10:00
];
2019-03-27 15:50:13 +11:00
}
}