1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Ninja/Transformers/InvoiceTransformer.php

138 lines
5.6 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Transformers;
2015-11-03 15:21:17 +01:00
2015-11-08 22:12:50 +01:00
use App\Models\Account;
use App\Models\Client;
2015-11-03 15:21:17 +01:00
use App\Models\Invoice;
2015-11-08 21:34:26 +01:00
/**
2015-11-08 22:57:28 +01:00
* @SWG\Definition(definition="Invoice", required={"invoice_number"}, @SWG\Xml(name="Invoice"))
2015-11-08 21:34:26 +01:00
*/
2015-11-08 22:12:50 +01:00
class InvoiceTransformer extends EntityTransformer
2015-11-03 15:21:17 +01:00
{
2015-11-08 21:34:26 +01:00
/**
2017-01-30 20:40:43 +01:00
* @SWG\Property(property="id", type="integer", example=1, readOnly=true)
* @SWG\Property(property="amount", type="number", format="float", example=10, readOnly=true)
* @SWG\Property(property="balance", type="number", format="float", example=10, readOnly=true)
2017-01-30 20:40:43 +01:00
* @SWG\Property(property="client_id", type="integer", example=1)
* @SWG\Property(property="invoice_number", type="string", example="0001")
2017-05-16 15:00:56 +02:00
* @SWG\Property(property="private_notes", type="string", example="Notes...")
* @SWG\Property(property="public_notes", type="string", example="Notes...")
2017-01-30 20:40:43 +01:00
*/
2015-11-03 15:21:17 +01:00
protected $defaultIncludes = [
'invoice_items',
];
2016-01-10 11:25:05 +01:00
protected $availableIncludes = [
'invitations',
2016-02-16 16:30:09 +01:00
'payments',
'client',
'documents',
];
2016-05-03 08:46:24 +02:00
public function __construct($account = null, $serializer = null, $client = null)
{
parent::__construct($account, $serializer);
2016-05-29 13:22:25 +02:00
2016-05-03 08:46:24 +02:00
$this->client = $client;
}
2015-11-08 22:12:50 +01:00
public function includeInvoiceItems(Invoice $invoice)
2015-11-03 15:21:17 +01:00
{
2015-11-27 13:55:28 +01:00
$transformer = new InvoiceItemTransformer($this->account, $this->serializer);
2017-01-30 20:40:43 +01:00
2016-08-08 16:45:37 +02:00
return $this->includeCollection($invoice->invoice_items, $transformer, ENTITY_INVOICE_ITEM);
2015-11-03 15:21:17 +01:00
}
public function includeInvitations(Invoice $invoice)
{
2016-01-24 19:55:36 +01:00
$transformer = new InvitationTransformer($this->account, $this->serializer);
2017-01-30 20:40:43 +01:00
return $this->includeCollection($invoice->invitations, $transformer, ENTITY_INVITATION);
}
2016-01-05 13:51:27 +01:00
public function includePayments(Invoice $invoice)
{
2016-05-03 08:46:24 +02:00
$transformer = new PaymentTransformer($this->account, $this->serializer, $invoice);
2017-01-30 20:40:43 +01:00
2016-01-05 13:51:27 +01:00
return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT);
}
2016-02-16 16:30:09 +01:00
public function includeClient(Invoice $invoice)
{
$transformer = new ClientTransformer($this->account, $this->serializer);
2017-01-30 20:40:43 +01:00
2016-02-17 09:10:33 +01:00
return $this->includeItem($invoice->client, $transformer, ENTITY_CLIENT);
2016-02-16 16:30:09 +01:00
}
2016-02-17 09:10:33 +01:00
public function includeExpenses(Invoice $invoice)
{
$transformer = new ExpenseTransformer($this->account, $this->serializer);
2017-01-30 20:40:43 +01:00
2016-02-17 09:10:33 +01:00
return $this->includeCollection($invoice->expenses, $transformer, ENTITY_EXPENSE);
}
public function includeDocuments(Invoice $invoice)
{
$transformer = new DocumentTransformer($this->account, $this->serializer);
2017-01-30 20:40:43 +01:00
2017-07-04 11:04:21 +02:00
$invoice->documents->each(function ($document) use ($invoice) {
$document->setRelation('invoice', $invoice);
});
return $this->includeCollection($invoice->documents, $transformer, ENTITY_DOCUMENT);
}
2015-11-03 15:21:17 +01:00
public function transform(Invoice $invoice)
{
2016-05-03 22:02:29 +02:00
return array_merge($this->getDefaults($invoice), [
2015-11-08 21:34:26 +01:00
'id' => (int) $invoice->public_id,
2015-11-03 15:21:17 +01:00
'amount' => (float) $invoice->amount,
'balance' => (float) $invoice->balance,
2016-05-03 08:46:24 +02:00
'client_id' => (int) ($this->client ? $this->client->public_id : $invoice->client->public_id),
2015-11-07 13:15:37 +01:00
'invoice_status_id' => (int) $invoice->invoice_status_id,
2015-12-27 12:08:58 +01:00
'updated_at' => $this->getTimestamp($invoice->updated_at),
'archived_at' => $this->getTimestamp($invoice->deleted_at),
'invoice_number' => $invoice->is_recurring ? '' : $invoice->invoice_number,
2017-01-30 20:40:43 +01:00
'discount' => (float) $invoice->discount,
2015-11-07 13:15:37 +01:00
'po_number' => $invoice->po_number,
'invoice_date' => $invoice->invoice_date,
'due_date' => $invoice->due_date,
2015-12-17 09:21:31 +01:00
'terms' => $invoice->terms,
2015-11-07 13:15:37 +01:00
'public_notes' => $invoice->public_notes,
2017-05-16 15:00:56 +02:00
'private_notes' => $invoice->private_notes,
2015-11-07 13:15:37 +01:00
'is_deleted' => (bool) $invoice->is_deleted,
2016-05-26 16:56:54 +02:00
'invoice_type_id' => (int) $invoice->invoice_type_id,
2015-11-07 13:15:37 +01:00
'is_recurring' => (bool) $invoice->is_recurring,
'frequency_id' => (int) $invoice->frequency_id,
'start_date' => $invoice->start_date,
'end_date' => $invoice->end_date,
'last_sent_date' => $invoice->last_sent_date,
'recurring_invoice_id' => (int) $invoice->recurring_invoice_id,
2016-04-04 12:10:00 +02:00
'tax_name1' => $invoice->tax_name1 ? $invoice->tax_name1 : '',
2016-03-31 11:29:01 +02:00
'tax_rate1' => (float) $invoice->tax_rate1,
2016-04-04 12:10:00 +02:00
'tax_name2' => $invoice->tax_name2 ? $invoice->tax_name2 : '',
2016-03-31 11:29:01 +02:00
'tax_rate2' => (float) $invoice->tax_rate2,
2015-11-07 13:15:37 +01:00
'amount' => (float) $invoice->amount,
'balance' => (float) $invoice->balance,
'is_amount_discount' => (bool) $invoice->is_amount_discount,
'invoice_footer' => $invoice->invoice_footer,
'partial' => (float) $invoice->partial,
'partial_due_date' => $invoice->partial_due_date,
2015-11-07 13:15:37 +01:00
'has_tasks' => (bool) $invoice->has_tasks,
2015-11-14 09:28:17 +01:00
'auto_bill' => (bool) $invoice->auto_bill,
'custom_value1' => (float) $invoice->custom_value1,
'custom_value2' => (float) $invoice->custom_value2,
'custom_taxes1' => (bool) $invoice->custom_taxes1,
'custom_taxes2' => (bool) $invoice->custom_taxes2,
2016-01-10 11:25:05 +01:00
'has_expenses' => (bool) $invoice->has_expenses,
'quote_invoice_id' => (int) $invoice->quote_invoice_id,
'custom_text_value1' => $invoice->custom_text_value1,
'custom_text_value2' => $invoice->custom_text_value2,
2016-05-29 13:22:25 +02:00
'is_quote' => (bool) $invoice->isType(INVOICE_TYPE_QUOTE), // Temp to support mobile app
'is_public' => (bool) $invoice->is_public,
2016-05-03 22:02:29 +02:00
]);
2015-11-03 15:21:17 +01:00
}
2016-01-10 11:25:05 +01:00
}