diff --git a/app/Http/Controllers/AccountApiController.php b/app/Http/Controllers/AccountApiController.php index b085874bbf..b979651457 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 bb8f104dc2..9552b62609 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 a9f65a94df..ef075967d1 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 2e0664734a..8e9396981e 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 ec4a8789ea..0752e504ea 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 0000000000..11ba0d1fc3 --- /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 8d00700231..3f72de9c84 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 1395ddef30..d49e592924 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 bfedf7c251..dbb2f669fe 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 b73cfa70b0..e73ad2bd04 100644 --- a/app/Ninja/Transformers/UserTransformer.php +++ b/app/Ninja/Transformers/UserTransformer.php @@ -1,10 +1,10 @@