From aef4658873cbbf061bd78e5d1ba4333eaaf11b62 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 17 Feb 2016 19:10:33 +1100 Subject: [PATCH 1/3] Api work/Bug fixes --- app/Models/Client.php | 4 ++++ app/Models/Invoice.php | 5 +++++ app/Ninja/Transformers/ClientTransformer.php | 8 ++++++++ app/Ninja/Transformers/InvoiceTransformer.php | 10 +++++++++- app/Ninja/Transformers/VendorContactTransformer.php | 1 - 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/Models/Client.php b/app/Models/Client.php index eae0eb1c14..2167af0ddf 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -139,6 +139,10 @@ class Client extends EntityModel return $this->hasMany('App\Models\Credit'); } + public function expenses() + { + return $this->hasMany('App\Models\Expense','client_id','id')->withTrashed(); + } public function addContact($data, $isPrimary = false) { diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index e93c8d3b07..626f4db647 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -197,6 +197,11 @@ class Invoice extends EntityModel implements BalanceAffecting return $this->hasMany('App\Models\Invitation')->orderBy('invitations.contact_id'); } + public function expenses() + { + return $this->hasMany('App\Models\Expense','invoice_id','id')->withTrashed(); + } + public function markInvitationsSent($notify = false) { foreach ($this->invitations as $invitation) { diff --git a/app/Ninja/Transformers/ClientTransformer.php b/app/Ninja/Transformers/ClientTransformer.php index 83ee7e5623..f7d786a2ee 100644 --- a/app/Ninja/Transformers/ClientTransformer.php +++ b/app/Ninja/Transformers/ClientTransformer.php @@ -47,6 +47,7 @@ class ClientTransformer extends EntityTransformer protected $availableIncludes = [ 'invoices', 'credits', + 'expenses', ]; public function includeContacts(Client $client) @@ -67,6 +68,13 @@ class ClientTransformer extends EntityTransformer return $this->includeCollection($client->credits, $transformer, ENTITY_CREDIT); } + public function includeExpenses(Client $client) + { + $transformer = new ExpenseTransformer($this->account, $this->serializer); + return $this->includeCollection($client->expenses, $transformer, ENTITY_EXPENSE); + } + + public function transform(Client $client) { return [ diff --git a/app/Ninja/Transformers/InvoiceTransformer.php b/app/Ninja/Transformers/InvoiceTransformer.php index 464fc5e3fa..b67635386d 100644 --- a/app/Ninja/Transformers/InvoiceTransformer.php +++ b/app/Ninja/Transformers/InvoiceTransformer.php @@ -28,6 +28,7 @@ class InvoiceTransformer extends EntityTransformer 'invitations', 'payments', 'client', + 'expenses', ]; public function includeInvoiceItems(Invoice $invoice) @@ -51,9 +52,16 @@ class InvoiceTransformer extends EntityTransformer public function includeClient(Invoice $invoice) { $transformer = new ClientTransformer($this->account, $this->serializer); - return $this->includeItem($invoice->client, $transformer, 'client'); + return $this->includeItem($invoice->client, $transformer, ENTITY_CLIENT); } + public function includeExpenses(Invoice $invoice) + { + $transformer = new ExpenseTransformer($this->account, $this->serializer); + return $this->includeCollection($invoice->expenses, $transformer, ENTITY_EXPENSE); + } + + public function transform(Invoice $invoice) { return [ diff --git a/app/Ninja/Transformers/VendorContactTransformer.php b/app/Ninja/Transformers/VendorContactTransformer.php index 0166883aba..3b75aee53a 100644 --- a/app/Ninja/Transformers/VendorContactTransformer.php +++ b/app/Ninja/Transformers/VendorContactTransformer.php @@ -17,7 +17,6 @@ class VendorContactTransformer extends EntityTransformer 'archived_at' => $this->getTimestamp($contact->deleted_at), 'is_primary' => (bool) $contact->is_primary, 'phone' => $contact->phone, - 'last_login' => $contact->last_login, 'account_key' => $this->account->account_key, ]; } From 6d09bda6464ad79532c2d674e1302cb1a88b51ff Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 17 Feb 2016 21:16:13 +1100 Subject: [PATCH 2/3] Working on API functionality --- app/Http/Controllers/AccountApiController.php | 1 + app/Http/Controllers/InvoiceApiController.php | 13 +++++++++++++ app/Http/Controllers/PaymentApiController.php | 4 ++-- app/Models/Account.php | 4 ++++ app/Ninja/Transformers/ExpenseTransformer.php | 4 ++++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/AccountApiController.php b/app/Http/Controllers/AccountApiController.php index 193cf2d7c5..f4619892fc 100644 --- a/app/Http/Controllers/AccountApiController.php +++ b/app/Http/Controllers/AccountApiController.php @@ -71,6 +71,7 @@ class AccountApiController extends BaseAPIController 'invoices' => ['invoice_items', 'user', 'client', 'payments'], 'products' => [], 'tax_rates' => [], + 'expenses' => ['client', 'invoice', 'vendor'] ]; foreach ($map as $key => $values) { diff --git a/app/Http/Controllers/InvoiceApiController.php b/app/Http/Controllers/InvoiceApiController.php index 54e527385d..73088f0c87 100644 --- a/app/Http/Controllers/InvoiceApiController.php +++ b/app/Http/Controllers/InvoiceApiController.php @@ -85,6 +85,19 @@ class InvoiceApiController extends BaseAPIController return $this->response($data); } + public function show($publicId) + { + + $invoice = Invoice::scope($publicId)->withTrashed()->first(); + + if(!$invoice) + return $this->errorResponse(['message'=>'Invoice does not exist!'], 404); + + $transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer')); + $data = $this->createItem($invoice, $transformer, 'invoice'); + + return $this->response($data); + } /** * @SWG\Post( diff --git a/app/Http/Controllers/PaymentApiController.php b/app/Http/Controllers/PaymentApiController.php index 5a0f508b98..7d896961de 100644 --- a/app/Http/Controllers/PaymentApiController.php +++ b/app/Http/Controllers/PaymentApiController.php @@ -218,8 +218,8 @@ class PaymentApiController extends BaseAPIController $query->withTrashed(); }])->first(); - $transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer')); - $data = $this->createItem($invoice, $transformer, 'invoice'); + $transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer')); + $data = $this->createItem($payment, $transformer, 'invoice'); return $this->response($data); } diff --git a/app/Models/Account.php b/app/Models/Account.php index 876804bf42..e9947119dc 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -164,6 +164,10 @@ class Account extends Eloquent return $this->belongsTo('App\Models\TaxRate'); } + public function expenses() + { + return $this->hasMany('App\Models\Expense','account_id','id')->withTrashed(); + } public function setIndustryIdAttribute($value) { diff --git a/app/Ninja/Transformers/ExpenseTransformer.php b/app/Ninja/Transformers/ExpenseTransformer.php index 26729f4ddb..a6d358d157 100644 --- a/app/Ninja/Transformers/ExpenseTransformer.php +++ b/app/Ninja/Transformers/ExpenseTransformer.php @@ -8,6 +8,7 @@ class ExpenseTransformer extends EntityTransformer { public function transform(Expense $expense) { + return [ 'id' => (int) $expense->public_id, 'private_notes' => $expense->private_notes, @@ -24,6 +25,9 @@ class ExpenseTransformer extends EntityTransformer 'exchange_rate' => (float) $expense->exchange_rate, 'invoice_currency_id' => (int) $expense->invoice_currency_id, 'is_deleted' => (bool) $expense->is_deleted, + 'client_id' => isset($expense->client->public_id) ? (int) $expense->client->public_id : null, + 'invoice_id' => isset($expense->invoice->public_id) ? (int) $expense->invoice->public_id : null, + 'vendor_id' => isset($expense->vendor->public_id) ? (int) $expense->vendor->public_id : null, ]; } } \ No newline at end of file From 992802c1647dbfb4c8f0293adbf70266d11bd3ad Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 17 Feb 2016 21:50:52 +1100 Subject: [PATCH 3/3] Working on API functionality --- app/Http/Controllers/AccountApiController.php | 3 +- app/Http/Controllers/InvoiceApiController.php | 17 +++++++++++ app/Http/Controllers/PaymentApiController.php | 29 ++++++++++++------- app/Models/Account.php | 5 ++++ app/Ninja/Transformers/AccountTransformer.php | 16 ++++++++-- app/Ninja/Transformers/PaymentTransformer.php | 1 + 6 files changed, 56 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/AccountApiController.php b/app/Http/Controllers/AccountApiController.php index f4619892fc..77e85243d7 100644 --- a/app/Http/Controllers/AccountApiController.php +++ b/app/Http/Controllers/AccountApiController.php @@ -71,7 +71,8 @@ class AccountApiController extends BaseAPIController 'invoices' => ['invoice_items', 'user', 'client', 'payments'], 'products' => [], 'tax_rates' => [], - 'expenses' => ['client', 'invoice', 'vendor'] + 'expenses' => ['client', 'invoice', 'vendor'], + 'payments' => ['invoice'], ]; foreach ($map as $key => $values) { diff --git a/app/Http/Controllers/InvoiceApiController.php b/app/Http/Controllers/InvoiceApiController.php index 73088f0c87..8c4344f608 100644 --- a/app/Http/Controllers/InvoiceApiController.php +++ b/app/Http/Controllers/InvoiceApiController.php @@ -85,6 +85,23 @@ class InvoiceApiController extends BaseAPIController return $this->response($data); } + /** + * @SWG\Get( + * path="/invoices/{invoice_id}", + * summary="Individual Invoice", + * tags={"invoice"}, + * @SWG\Response( + * response=200, + * description="A single invoice", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Invoice")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function show($publicId) { diff --git a/app/Http/Controllers/PaymentApiController.php b/app/Http/Controllers/PaymentApiController.php index 7d896961de..7022f0c3e8 100644 --- a/app/Http/Controllers/PaymentApiController.php +++ b/app/Http/Controllers/PaymentApiController.php @@ -58,8 +58,8 @@ class PaymentApiController extends BaseAPIController $payments = $payments->orderBy('created_at', 'desc')->paginate(); $paginator = $paginator->paginate(); + $transformer = new PaymentTransformer(Auth::user()->account, Input::get('serializer')); - $data = $this->createCollection($payments, $transformer, 'payments', $paginator); return $this->response($data); @@ -98,11 +98,8 @@ class PaymentApiController extends BaseAPIController $payment = Payment::scope($publicId)->withTrashed()->firstOrFail(); $this->paymentRepo->archive($payment); - $invoice = Invoice::scope($data['invoice_id'])->with('client')->with(['payments' => function($query) { - $query->withTrashed(); - }])->first(); - $transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer')); - $data = $this->createItem($invoice, $transformer, 'invoice'); + $transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer')); + $data = $this->createItem($payment, $transformer, 'invoice'); return $this->response($data); } @@ -113,12 +110,17 @@ class PaymentApiController extends BaseAPIController return $error; } + /* $invoice = Invoice::scope($data['invoice_id'])->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) { $query->withTrashed(); }])->withTrashed()->first(); - $transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer')); - $data = $this->createItem($invoice, $transformer, 'invoice'); + */ + + $transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer')); + $data = $this->createItem($payment, $transformer, 'invoice'); + return $this->response($data); + } @@ -175,13 +177,17 @@ class PaymentApiController extends BaseAPIController $this->contactMailer->sendPaymentConfirmation($payment); } + /* $invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) { $query->withTrashed(); }])->first(); - $transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer')); - $data = $this->createItem($invoice, $transformer, 'invoice'); + */ + + $transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer')); + $data = $this->createItem($payment, $transformer, 'invoice'); return $this->response($data); + } /** @@ -214,10 +220,11 @@ class PaymentApiController extends BaseAPIController $this->paymentRepo->delete($payment); + /* $invoice = Invoice::scope($invoiceId)->with('client', 'invoice_items', 'invitations')->with(['payments' => function($query) { $query->withTrashed(); }])->first(); - + */ $transformer = new PaymentTransformer(\Auth::user()->account, Input::get('serializer')); $data = $this->createItem($payment, $transformer, 'invoice'); diff --git a/app/Models/Account.php b/app/Models/Account.php index e9947119dc..d3f64bb2ee 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -169,6 +169,11 @@ class Account extends Eloquent return $this->hasMany('App\Models\Expense','account_id','id')->withTrashed(); } + public function payments() + { + return $this->hasMany('App\Models\Payment','account_id','id')->withTrashed(); + } + public function setIndustryIdAttribute($value) { $this->attributes['industry_id'] = $value ?: null; diff --git a/app/Ninja/Transformers/AccountTransformer.php b/app/Ninja/Transformers/AccountTransformer.php index eed4743465..6a1c32f30e 100644 --- a/app/Ninja/Transformers/AccountTransformer.php +++ b/app/Ninja/Transformers/AccountTransformer.php @@ -12,10 +12,14 @@ class AccountTransformer extends EntityTransformer { protected $defaultIncludes = [ 'users', - // 'clients', - 'invoices', 'products', - 'taxRates' + 'taxRates', + 'payments' + ]; + + protected $availableIncludes = [ + 'clients', + 'invoices', ]; public function includeUsers(Account $account) @@ -48,6 +52,12 @@ class AccountTransformer extends EntityTransformer return $this->includeCollection($account->tax_rates, $transformer, 'taxRates'); } + public function includePayments(Account $account) + { + $transformer = new PaymentTransformer($account, $this->serializer); + return $this->includeCollection($account->payments, $transformer, 'payments'); + } + public function transform(Account $account) { return [ diff --git a/app/Ninja/Transformers/PaymentTransformer.php b/app/Ninja/Transformers/PaymentTransformer.php index 2264225975..a1de09cd6f 100644 --- a/app/Ninja/Transformers/PaymentTransformer.php +++ b/app/Ninja/Transformers/PaymentTransformer.php @@ -57,6 +57,7 @@ class PaymentTransformer extends EntityTransformer 'archived_at' => $this->getTimestamp($payment->deleted_at), 'is_deleted' => (bool) $payment->is_deleted, 'payment_type_id' => (int) $payment->payment_type_id, + 'invoice_id' => (int) $payment->invoice->public_id, ]; } } \ No newline at end of file