1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 14:12:44 +01:00

Working on the API

This commit is contained in:
Hillel Coren 2015-11-08 23:12:50 +02:00
parent 57245211d8
commit 2f9dd707f7
10 changed files with 75 additions and 37 deletions

View File

@ -62,6 +62,8 @@ class AccountApiController extends BaseAPIController
public function show()
{
$account = Auth::user()->account;
$account->load('clients.getInvoices.invoice_items', 'users');
$response = $this->createItem($account, new AccountTransformer);
return $this->response($response);

View File

@ -59,6 +59,27 @@ class Client extends EntityModel
return $this->hasMany('App\Models\Invoice');
}
public function getInvoices()
{
return $this->hasMany('App\Models\Invoice')
->where('is_quote', '=', false)
->where('is_recurring', '=', false);
}
public function getRecurringInvoices()
{
return $this->hasMany('App\Models\Invoice')
->where('is_quote', '=', false)
->where('is_recurring', '=', true);
}
public function getQuotes()
{
return $this->hasMany('App\Models\Invoice')
->where('is_quote', '=', true)
->where('is_recurring', '=', false);
}
public function payments()
{
return $this->hasMany('App\Models\Payment');

View File

@ -12,14 +12,14 @@ class AccountTransformer extends TransformerAbstract
'clients',
];
public function includeUsers($account)
public function includeUsers(Account $account)
{
return $this->collection($account->users, new UserTransformer);
return $this->collection($account->users, new UserTransformer($account));
}
public function includeClients($account)
public function includeClients(Account $account)
{
return $this->collection($account->clients, new ClientTransformer);
return $this->collection($account->clients, new ClientTransformer($account));
}
public function transform(Account $account)

View File

@ -1,39 +1,31 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Account;
use App\Models\Client;
use App\Models\Contact;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class ClientTransformer extends TransformerAbstract
class ClientTransformer extends EntityTransformer
{
protected $defaultIncludes = [
'contacts',
'invoices',
'quotes',
];
public function includeContacts($client)
public function includeContacts(Client $client)
{
return $this->collection($client->contacts, new ContactTransformer);
return $this->collection($client->contacts, new ContactTransformer($this->account));
}
public function includeInvoices($client)
public function includeInvoices(Client $client)
{
$invoices = $client->invoices->filter(function($invoice) {
return !$invoice->is_quote && !$invoice->is_recurring;
});
return $this->collection($invoices, new InvoiceTransformer);
return $this->collection($client->getInvoices, new InvoiceTransformer($this->account, $client));
}
public function includeQuotes($client)
public function includeQuotes(Client $client)
{
$invoices = $client->invoices->filter(function($invoice) {
return $invoice->is_quote && !$invoice->is_recurring;
});
return $this->collection($invoices, new QuoteTransformer);
return $this->collection($client->getQuotes, new QuoteTransformer($this->account, $client));
}
public function transform(Client $client)
@ -44,7 +36,7 @@ class ClientTransformer extends TransformerAbstract
'balance' => (float) $client->balance,
'paid_to_date' => (float) $client->paid_to_date,
'user_id' => (int) $client->user_id,
'account_key' => $client->account->account_key,
'account_key' => $this->account->account_key,
'updated_at' => $client->updated_at,
'deleted_at' => $client->deleted_at,
'address1' => $client->address1,

View File

@ -1,10 +1,10 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Account;
use App\Models\Contact;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class ContactTransformer extends TransformerAbstract
class ContactTransformer extends EntityTransformer
{
public function transform(Contact $contact)
{
@ -19,7 +19,7 @@ class ContactTransformer extends TransformerAbstract
'is_primary' => (bool) $contact->is_primary,
'phone' => $contact->phone,
'last_login' => $contact->last_login,
'account_key' => $contact->account->account_key
'account_key' => $this->account->account_key
];
}
}

View File

@ -0,0 +1,14 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Account;
use League\Fractal\TransformerAbstract;
class EntityTransformer extends TransformerAbstract
{
protected $account;
public function __construct(Account $account)
{
$this->account = $account;
}
}

View File

@ -1,17 +1,17 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Account;
use App\Models\InvoiceItem;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class InvoiceItemTransformer extends TransformerAbstract
class InvoiceItemTransformer extends EntityTransformer
{
public function transform(InvoiceItem $item)
{
return [
'public_id' => (int) $item->public_id,
'product_key' => $item->product_key,
'account_key' => $item->account->account_key,
'account_key' => $this->account->account_key,
'user_id' => (int) $item->user_id,
'invoice_id' => (int) $item->invoice_id,
'product_id' => (int) $item->product_id,

View File

@ -1,14 +1,15 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Account;
use App\Models\Client;
use App\Models\Invoice;
use League\Fractal;
use League\Fractal\TransformerAbstract;
/**
* @SWG\Definition(definition="Invoice",required={"invoice_number"}, @SWG\Xml(name="Invoice"))
*/
class InvoiceTransformer extends TransformerAbstract
class InvoiceTransformer extends EntityTransformer
{
/**
* @SWG\Property(property="id", type="integer", example=1)
@ -19,13 +20,22 @@ class InvoiceTransformer extends TransformerAbstract
* @SWG\Property(property="invoice_status_id", type="integer", example=1)
*/
protected $client;
public function __construct(Account $account, Client $client)
{
parent::__construct($account);
$this->client = $client;
}
protected $defaultIncludes = [
'invoice_items',
];
public function includeInvoiceItems($invoice)
public function includeInvoiceItems(Invoice $invoice)
{
return $this->collection($invoice->invoice_items, new InvoiceItemTransformer);
return $this->collection($invoice->invoice_items, new InvoiceItemTransformer($this->account));
}
public function transform(Invoice $invoice)
@ -35,7 +45,7 @@ class InvoiceTransformer extends TransformerAbstract
'invoice_number' => $invoice->invoice_number,
'amount' => (float) $invoice->amount,
'balance' => (float) $invoice->balance,
'client_id' => (int) $invoice->client->public_id,
'client_id' => (int) $this->client->public_id,
'invoice_status_id' => (int) $invoice->invoice_status_id,
'updated_at' => $invoice->updated_at,
'deleted_at' => $invoice->deleted_at,

View File

@ -2,9 +2,8 @@
use App\Models\Invoice;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class QuoteTransformer extends TransformerAbstract
class QuoteTransformer extends EntityTransformer
{
protected $defaultIncludes = [
'invoice_items',

View File

@ -1,10 +1,10 @@
<?php namespace App\Ninja\Transformers;
use App\Models\Account;
use App\Models\User;
use League\Fractal;
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
class UserTransformer extends EntityTransformer
{
public function transform(User $user)
{