From f66539b0da644ab4c96b186a0fc515e4ff09166b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 5 Jan 2016 23:51:27 +1100 Subject: [PATCH] Integrate Payments into API --- app/Models/Invoice.php | 5 +++++ app/Ninja/Transformers/InvoiceTransformer.php | 7 +++++++ app/Ninja/Transformers/PaymentTransformer.php | 10 ++++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index a80c7cfd2a..10fb6ea1d1 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -176,6 +176,11 @@ class Invoice extends EntityModel implements BalanceAffecting return $this->belongsTo('App\Models\InvoiceDesign'); } + public function payments() + { + return $this->hasMany('App\Models\Payment', 'invoice_id', 'id'); + } + public function recurring_invoice() { return $this->belongsTo('App\Models\Invoice'); diff --git a/app/Ninja/Transformers/InvoiceTransformer.php b/app/Ninja/Transformers/InvoiceTransformer.php index bc6ee6dbbd..9f4560f267 100644 --- a/app/Ninja/Transformers/InvoiceTransformer.php +++ b/app/Ninja/Transformers/InvoiceTransformer.php @@ -22,6 +22,7 @@ class InvoiceTransformer extends EntityTransformer protected $defaultIncludes = [ 'invoice_items', + 'payments' ]; public function includeInvoiceItems(Invoice $invoice) @@ -30,6 +31,12 @@ class InvoiceTransformer extends EntityTransformer return $this->includeCollection($invoice->invoice_items, $transformer, ENTITY_INVOICE_ITEMS); } + public function includePayments(Invoice $invoice) + { + $transformer = new PaymentTransformer($this->account, $this->serializer); + return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT); + } + public function transform(Invoice $invoice) { return [ diff --git a/app/Ninja/Transformers/PaymentTransformer.php b/app/Ninja/Transformers/PaymentTransformer.php index 6ec0b13da7..a2750eaad8 100644 --- a/app/Ninja/Transformers/PaymentTransformer.php +++ b/app/Ninja/Transformers/PaymentTransformer.php @@ -18,10 +18,7 @@ class PaymentTransformer extends EntityTransformer * @SWG\Property(property="amount", type="float", example=10, readOnly=true) * @SWG\Property(property="invoice_id", type="integer", example=1) */ - protected $defaultIncludes = [ - 'invoice', - 'client', - ]; + protected $defaultIncludes = []; public function __construct(Account $account) @@ -50,6 +47,11 @@ class PaymentTransformer extends EntityTransformer 'account_key' => $this->account->account_key, 'user_id' => (int) $payment->user->public_id + 1, 'transaction_reference' => $payment->transaction_reference, + 'payment_date' => $payment->payment_date, + 'updated_at' => $this->getTimestamp($payment->updated_at), + 'archived_at' => $this->getTimestamp($payment->deleted_at), + 'is_deleted' => (bool) $payment->is_deleted, + 'payment_type_id' => (int) $payment->payment_type_id, ]; } } \ No newline at end of file