2015-11-03 15:21:17 +01:00
|
|
|
<?php namespace App\Ninja\Transformers;
|
|
|
|
|
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
|
|
|
/**
|
2015-11-08 22:53:13 +01:00
|
|
|
* @SWG\Property(property="id", type="integer", example=1, readOnly=true)
|
|
|
|
* @SWG\Property(property="amount", type="float", example=10, readOnly=true)
|
|
|
|
* @SWG\Property(property="balance", type="float", example=10, readOnly=true)
|
2015-11-08 21:34:26 +01:00
|
|
|
* @SWG\Property(property="client_id", type="integer", example=1)
|
2015-11-08 22:53:13 +01:00
|
|
|
* @SWG\Property(property="invoice_number", type="string", example="0001")
|
2015-11-08 21:34:26 +01:00
|
|
|
* @SWG\Property(property="invoice_status_id", type="integer", example=1)
|
|
|
|
*/
|
|
|
|
|
2015-11-03 15:21:17 +01:00
|
|
|
protected $defaultIncludes = [
|
|
|
|
'invoice_items',
|
|
|
|
];
|
2016-01-10 11:25:05 +01:00
|
|
|
|
2016-01-24 19:37:24 +01:00
|
|
|
protected $availableIncludes = [
|
|
|
|
'invitations',
|
2016-02-16 16:30:09 +01:00
|
|
|
'payments',
|
|
|
|
'client',
|
2016-05-19 01:43:03 +02:00
|
|
|
'documents',
|
2016-01-24 19:37:24 +01:00
|
|
|
];
|
|
|
|
|
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);
|
|
|
|
return $this->includeCollection($invoice->invoice_items, $transformer, ENTITY_INVOICE_ITEMS);
|
2015-11-03 15:21:17 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 19:37:24 +01:00
|
|
|
public function includeInvitations(Invoice $invoice)
|
|
|
|
{
|
2016-01-24 19:55:36 +01:00
|
|
|
$transformer = new InvitationTransformer($this->account, $this->serializer);
|
2016-01-24 19:37:24 +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);
|
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);
|
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);
|
|
|
|
return $this->includeCollection($invoice->expenses, $transformer, ENTITY_EXPENSE);
|
|
|
|
}
|
|
|
|
|
2016-05-19 01:43:03 +02:00
|
|
|
public function includeDocuments(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$transformer = new DocumentTransformer($this->account, $this->serializer);
|
|
|
|
return $this->includeCollection($invoice->documents, $transformer, ENTITY_DOCUMENT);
|
|
|
|
}
|
|
|
|
|
2016-02-17 09:10:33 +01:00
|
|
|
|
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),
|
2015-11-07 13:15:37 +01:00
|
|
|
'invoice_number' => $invoice->invoice_number,
|
|
|
|
'discount' => (double) $invoice->discount,
|
|
|
|
'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,
|
|
|
|
'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,
|
|
|
|
'has_tasks' => (bool) $invoice->has_tasks,
|
2015-11-14 09:28:17 +01:00
|
|
|
'auto_bill' => (bool) $invoice->auto_bill,
|
2016-01-06 21:46:11 +01:00
|
|
|
'custom_value1' => (float) $invoice->custom_value1,
|
|
|
|
'custom_value2' => (float) $invoice->custom_value2,
|
2015-12-17 03:33:40 +01:00
|
|
|
'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,
|
2016-01-27 23:19:54 +01:00
|
|
|
'quote_invoice_id' => (int) $invoice->quote_invoice_id,
|
2016-02-15 18:39:26 +01:00
|
|
|
'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
|
2016-05-03 22:02:29 +02:00
|
|
|
]);
|
2015-11-03 15:21:17 +01:00
|
|
|
}
|
2016-01-10 11:25:05 +01:00
|
|
|
}
|