From 2f9dd707f74e65ae8db94329fdb7b38a45845b59 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 8 Nov 2015 23:12:50 +0200 Subject: [PATCH] Working on the API --- app/Http/Controllers/AccountApiController.php | 2 ++ app/Models/Client.php | 21 ++++++++++++++ app/Ninja/Transformers/AccountTransformer.php | 8 +++--- app/Ninja/Transformers/ClientTransformer.php | 28 +++++++------------ app/Ninja/Transformers/ContactTransformer.php | 6 ++-- app/Ninja/Transformers/EntityTransformer.php | 14 ++++++++++ .../Transformers/InvoiceItemTransformer.php | 6 ++-- app/Ninja/Transformers/InvoiceTransformer.php | 20 +++++++++---- app/Ninja/Transformers/QuoteTransformer.php | 3 +- app/Ninja/Transformers/UserTransformer.php | 4 +-- 10 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 app/Ninja/Transformers/EntityTransformer.php diff --git a/app/Http/Controllers/AccountApiController.php b/app/Http/Controllers/AccountApiController.php index b085874bbfee..b979651457d1 100644 --- a/app/Http/Controllers/AccountApiController.php +++ b/app/Http/Controllers/AccountApiController.php @@ -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); diff --git a/app/Models/Client.php b/app/Models/Client.php index bb8f104dc246..9552b62609d1 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -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'); diff --git a/app/Ninja/Transformers/AccountTransformer.php b/app/Ninja/Transformers/AccountTransformer.php index a9f65a94dff7..ef075967d13d 100644 --- a/app/Ninja/Transformers/AccountTransformer.php +++ b/app/Ninja/Transformers/AccountTransformer.php @@ -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) diff --git a/app/Ninja/Transformers/ClientTransformer.php b/app/Ninja/Transformers/ClientTransformer.php index 2e0664734a83..8e9396981e84 100644 --- a/app/Ninja/Transformers/ClientTransformer.php +++ b/app/Ninja/Transformers/ClientTransformer.php @@ -1,39 +1,31 @@ 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, diff --git a/app/Ninja/Transformers/ContactTransformer.php b/app/Ninja/Transformers/ContactTransformer.php index ec4a8789ead6..0752e504eaf4 100644 --- a/app/Ninja/Transformers/ContactTransformer.php +++ b/app/Ninja/Transformers/ContactTransformer.php @@ -1,10 +1,10 @@ (bool) $contact->is_primary, 'phone' => $contact->phone, 'last_login' => $contact->last_login, - 'account_key' => $contact->account->account_key + 'account_key' => $this->account->account_key ]; } } \ No newline at end of file diff --git a/app/Ninja/Transformers/EntityTransformer.php b/app/Ninja/Transformers/EntityTransformer.php new file mode 100644 index 000000000000..11ba0d1fc356 --- /dev/null +++ b/app/Ninja/Transformers/EntityTransformer.php @@ -0,0 +1,14 @@ +account = $account; + } +} diff --git a/app/Ninja/Transformers/InvoiceItemTransformer.php b/app/Ninja/Transformers/InvoiceItemTransformer.php index 8d007002314c..3f72de9c847f 100644 --- a/app/Ninja/Transformers/InvoiceItemTransformer.php +++ b/app/Ninja/Transformers/InvoiceItemTransformer.php @@ -1,17 +1,17 @@ (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, diff --git a/app/Ninja/Transformers/InvoiceTransformer.php b/app/Ninja/Transformers/InvoiceTransformer.php index 1395ddef303f..d49e592924be 100644 --- a/app/Ninja/Transformers/InvoiceTransformer.php +++ b/app/Ninja/Transformers/InvoiceTransformer.php @@ -1,14 +1,15 @@ 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, diff --git a/app/Ninja/Transformers/QuoteTransformer.php b/app/Ninja/Transformers/QuoteTransformer.php index bfedf7c2513e..dbb2f669fecc 100644 --- a/app/Ninja/Transformers/QuoteTransformer.php +++ b/app/Ninja/Transformers/QuoteTransformer.php @@ -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', diff --git a/app/Ninja/Transformers/UserTransformer.php b/app/Ninja/Transformers/UserTransformer.php index b73cfa70b016..e73ad2bd0450 100644 --- a/app/Ninja/Transformers/UserTransformer.php +++ b/app/Ninja/Transformers/UserTransformer.php @@ -1,10 +1,10 @@