2019-04-02 07:16:39 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-04-02 07:16:39 +02:00
|
|
|
|
|
|
|
namespace App\Transformers;
|
2019-04-17 02:58:23 +02:00
|
|
|
|
2020-06-01 13:49:11 +02:00
|
|
|
use App\Models\Backup;
|
2020-04-10 07:07:36 +02:00
|
|
|
use App\Models\Document;
|
2019-04-02 07:16:39 +02:00
|
|
|
use App\Models\Invoice;
|
2019-09-23 05:22:24 +02:00
|
|
|
use App\Models\InvoiceInvitation;
|
2020-04-10 07:07:36 +02:00
|
|
|
use App\Transformers\DocumentTransformer;
|
2020-06-01 13:49:11 +02:00
|
|
|
use App\Transformers\InvoiceHistoryTransformer;
|
2019-09-23 05:22:24 +02:00
|
|
|
use App\Transformers\InvoiceInvitationTransformer;
|
2019-04-17 02:58:23 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-04-02 07:16:39 +02:00
|
|
|
|
|
|
|
class InvoiceTransformer extends EntityTransformer
|
|
|
|
{
|
2019-04-03 02:09:22 +02:00
|
|
|
use MakesHash;
|
2019-08-26 10:25:05 +02:00
|
|
|
|
2019-04-02 07:16:39 +02:00
|
|
|
protected $defaultIncludes = [
|
2020-04-10 07:07:36 +02:00
|
|
|
'invitations',
|
|
|
|
'documents',
|
2019-04-02 07:16:39 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $availableIncludes = [
|
2019-09-23 05:22:24 +02:00
|
|
|
'invitations',
|
2020-06-01 13:49:11 +02:00
|
|
|
'history'
|
2019-04-02 07:16:39 +02:00
|
|
|
// 'payments',
|
|
|
|
// 'client',
|
|
|
|
// 'documents',
|
|
|
|
];
|
|
|
|
|
2019-09-23 05:22:24 +02:00
|
|
|
public function includeInvitations(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$transformer = new InvoiceInvitationTransformer($this->serializer);
|
|
|
|
|
|
|
|
return $this->includeCollection($invoice->invitations, $transformer, InvoiceInvitation::class);
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2020-06-01 13:49:11 +02:00
|
|
|
public function includeHistory(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$transformer = new InvoiceHistoryTransformer($this->serializer);
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2020-06-01 13:49:11 +02:00
|
|
|
return $this->includeCollection($invoice->history, $transformer, Backup::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-12-30 22:59:12 +01:00
|
|
|
public function includePayments(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$transformer = new PaymentTransformer($this->account, $this->serializer, $invoice);
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT);
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function includeClient(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$transformer = new ClientTransformer($this->account, $this->serializer);
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->includeItem($invoice->client, $transformer, ENTITY_CLIENT);
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function includeExpenses(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$transformer = new ExpenseTransformer($this->account, $this->serializer);
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->includeCollection($invoice->expenses, $transformer, ENTITY_EXPENSE);
|
|
|
|
}
|
2020-04-10 07:07:36 +02:00
|
|
|
*/
|
|
|
|
public function includeDocuments(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$transformer = new DocumentTransformer($this->serializer);
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2020-04-10 07:07:36 +02:00
|
|
|
// $invoice->documents->each(function ($document) use ($invoice) {
|
|
|
|
// $document->setRelation('invoice', $invoice);
|
|
|
|
// });
|
2020-03-21 06:37:30 +01:00
|
|
|
|
2020-04-10 07:07:36 +02:00
|
|
|
return $this->includeCollection($invoice->documents, $transformer, Document::class);
|
|
|
|
}
|
|
|
|
|
2019-04-02 07:16:39 +02:00
|
|
|
public function transform(Invoice $invoice)
|
|
|
|
{
|
|
|
|
return [
|
2019-04-03 02:09:22 +02:00
|
|
|
'id' => $this->encodePrimaryKey($invoice->id),
|
2019-11-06 23:57:09 +01:00
|
|
|
'user_id' => $this->encodePrimaryKey($invoice->user_id),
|
|
|
|
'assigned_user_id' => $this->encodePrimaryKey($invoice->assigned_user_id),
|
2019-10-27 22:36:09 +01:00
|
|
|
'amount' => (float) $invoice->amount,
|
|
|
|
'balance' => (float) $invoice->balance,
|
2019-10-11 00:11:36 +02:00
|
|
|
'client_id' => (string) $this->encodePrimaryKey($invoice->client_id),
|
2020-02-24 11:15:30 +01:00
|
|
|
'vendor_id' => (string) $this->encodePrimaryKey($invoice->vendor_id),
|
2019-09-12 03:28:41 +02:00
|
|
|
'status_id' => (string) ($invoice->status_id ?: 1),
|
2020-03-07 23:15:11 +01:00
|
|
|
'design_id' => (string) $this->encodePrimaryKey($invoice->design_id),
|
2020-02-27 00:32:44 +01:00
|
|
|
'created_at' => (int)$invoice->created_at,
|
2020-01-27 11:53:08 +01:00
|
|
|
'updated_at' => (int)$invoice->updated_at,
|
|
|
|
'archived_at' => (int)$invoice->deleted_at,
|
2020-02-27 00:32:44 +01:00
|
|
|
'is_deleted' => (bool) $invoice->is_deleted,
|
2019-11-27 11:27:24 +01:00
|
|
|
'number' => $invoice->number ?: '',
|
2019-10-25 04:26:16 +02:00
|
|
|
'discount' => (float) $invoice->discount,
|
2019-09-27 00:14:02 +02:00
|
|
|
'po_number' => $invoice->po_number ?: '',
|
2019-11-27 11:27:24 +01:00
|
|
|
'date' => $invoice->date ?: '',
|
2019-12-22 11:28:41 +01:00
|
|
|
'last_sent_date' => $invoice->last_sent_date ?: '',
|
2019-12-16 12:34:38 +01:00
|
|
|
'next_send_date' => $invoice->date ?: '',
|
2019-04-02 07:16:39 +02:00
|
|
|
'due_date' => $invoice->due_date ?: '',
|
2019-04-23 06:16:41 +02:00
|
|
|
'terms' => $invoice->terms ?: '',
|
2019-04-02 07:16:39 +02:00
|
|
|
'public_notes' => $invoice->public_notes ?: '',
|
|
|
|
'private_notes' => $invoice->private_notes ?: '',
|
2019-11-10 22:12:21 +01:00
|
|
|
'uses_inclusive_taxes' => (bool) $invoice->uses_inclusive_taxes,
|
2019-04-02 07:16:39 +02:00
|
|
|
'tax_name1' => $invoice->tax_name1 ? $invoice->tax_name1 : '',
|
2019-10-02 10:50:54 +02:00
|
|
|
'tax_rate1' => (float) $invoice->tax_rate1,
|
2019-04-02 07:16:39 +02:00
|
|
|
'tax_name2' => $invoice->tax_name2 ? $invoice->tax_name2 : '',
|
2019-10-02 10:50:54 +02:00
|
|
|
'tax_rate2' => (float) $invoice->tax_rate2,
|
2019-10-05 04:28:23 +02:00
|
|
|
'tax_name3' => $invoice->tax_name3 ? $invoice->tax_name3 : '',
|
|
|
|
'tax_rate3' => (float) $invoice->tax_rate3,
|
2019-12-04 02:06:14 +01:00
|
|
|
'total_taxes' => (float) $invoice->total_taxes,
|
2019-04-02 07:16:39 +02:00
|
|
|
'is_amount_discount' => (bool) ($invoice->is_amount_discount ?: false),
|
2019-10-30 07:08:18 +01:00
|
|
|
'footer' => $invoice->footer ?: '',
|
2019-04-02 07:16:39 +02:00
|
|
|
'partial' => (float) ($invoice->partial ?: 0.0),
|
|
|
|
'partial_due_date' => $invoice->partial_due_date ?: '',
|
2019-09-27 00:14:02 +02:00
|
|
|
'custom_value1' => (string) $invoice->custom_value1 ?: '',
|
|
|
|
'custom_value2' => (string) $invoice->custom_value2 ?: '',
|
|
|
|
'custom_value3' => (string) $invoice->custom_value3 ?: '',
|
|
|
|
'custom_value4' => (string) $invoice->custom_value4 ?: '',
|
2019-04-23 06:16:41 +02:00
|
|
|
'has_tasks' => (bool) $invoice->has_tasks,
|
2019-04-02 07:16:39 +02:00
|
|
|
'has_expenses' => (bool) $invoice->has_expenses,
|
2019-10-29 12:12:59 +01:00
|
|
|
'custom_surcharge1' => (float)$invoice->custom_surcharge1,
|
|
|
|
'custom_surcharge2' => (float)$invoice->custom_surcharge2,
|
|
|
|
'custom_surcharge3' => (float)$invoice->custom_surcharge3,
|
|
|
|
'custom_surcharge4' => (float)$invoice->custom_surcharge4,
|
2020-03-06 06:08:44 +01:00
|
|
|
'custom_surcharge_tax1' => (bool) $invoice->custom_surcharge_tax1,
|
|
|
|
'custom_surcharge_tax2' => (bool) $invoice->custom_surcharge_tax2,
|
|
|
|
'custom_surcharge_tax3' => (bool) $invoice->custom_surcharge_tax3,
|
|
|
|
'custom_surcharge_tax4' => (bool) $invoice->custom_surcharge_tax4,
|
2019-10-29 12:12:59 +01:00
|
|
|
'line_items' => $invoice->line_items ?: (array)[],
|
2020-03-02 11:22:37 +01:00
|
|
|
'entity_type' => 'invoice',
|
2019-04-02 07:16:39 +02:00
|
|
|
];
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|