From e97731eed9c965d39668575ec7f36498989932d9 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Fri, 9 Feb 2018 16:18:56 +0300 Subject: [PATCH] added currency transformer to incomes/expenses api --- app/Models/Expense/BillPayment.php | 5 ++ app/Models/Income/InvoicePayment.php | 5 ++ app/Models/Setting/Currency.php | 10 ++++ app/Transformers/Expense/Bill.php | 56 ++++++++++++--------- app/Transformers/Expense/BillPayments.php | 12 ++++- app/Transformers/Expense/Payment.php | 30 +++++++---- app/Transformers/Income/Invoice.php | 48 +++++++++++------- app/Transformers/Income/InvoicePayments.php | 12 ++++- app/Transformers/Income/Revenue.php | 30 +++++++---- 9 files changed, 144 insertions(+), 64 deletions(-) diff --git a/app/Models/Expense/BillPayment.php b/app/Models/Expense/BillPayment.php index 0f39e3806..4a1ee3f44 100644 --- a/app/Models/Expense/BillPayment.php +++ b/app/Models/Expense/BillPayment.php @@ -27,6 +27,11 @@ class BillPayment extends Model return $this->belongsTo('App\Models\Banking\Account'); } + public function currency() + { + return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code'); + } + public function bill() { return $this->belongsTo('App\Models\Expense\Bill'); diff --git a/app/Models/Income/InvoicePayment.php b/app/Models/Income/InvoicePayment.php index 427661bb2..0a257684a 100644 --- a/app/Models/Income/InvoicePayment.php +++ b/app/Models/Income/InvoicePayment.php @@ -27,6 +27,11 @@ class InvoicePayment extends Model return $this->belongsTo('App\Models\Banking\Account'); } + public function currency() + { + return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code'); + } + public function invoice() { return $this->belongsTo('App\Models\Income\Invoice'); diff --git a/app/Models/Setting/Currency.php b/app/Models/Setting/Currency.php index 96ac2d1aa..792ef4f80 100644 --- a/app/Models/Setting/Currency.php +++ b/app/Models/Setting/Currency.php @@ -38,6 +38,11 @@ class Currency extends Model return $this->hasMany('App\Models\Income\Invoice', 'currency_code', 'code'); } + public function invoice_payments() + { + return $this->hasMany('App\Models\Income\InvoicePayment', 'currency_code', 'code'); + } + public function revenues() { return $this->hasMany('App\Models\Income\Revenue', 'currency_code', 'code'); @@ -48,6 +53,11 @@ class Currency extends Model return $this->hasMany('App\Models\Expense\Bill', 'currency_code', 'code'); } + public function bill_payments() + { + return $this->hasMany('App\Models\Expense\BillPayment', 'currency_code', 'code'); + } + public function payments() { return $this->hasMany('App\Models\Expense\Payment', 'currency_code', 'code'); diff --git a/app/Transformers/Expense/Bill.php b/app/Transformers/Expense/Bill.php index 5b7d2e993..cc69156fe 100644 --- a/app/Transformers/Expense/Bill.php +++ b/app/Transformers/Expense/Bill.php @@ -7,6 +7,7 @@ use App\Transformers\Expense\BillItems; use App\Transformers\Expense\BillPayments; use App\Transformers\Expense\BillStatus; use App\Transformers\Expense\Vendor; +use App\Transformers\Setting\Currency; use App\Models\Expense\Bill as Model; use League\Fractal\TransformerAbstract; @@ -15,7 +16,7 @@ class Bill extends TransformerAbstract /** * @var array */ - protected $defaultIncludes = ['vendor', 'status', 'items', 'payments', 'histories']; + protected $defaultIncludes = ['currency', 'histories', 'items', 'payments', 'status', 'vendor']; /** * @param Model $model @@ -48,30 +49,12 @@ class Bill extends TransformerAbstract } /** - * @param Model $model - * @return \League\Fractal\Resource\Item + * @param Model $model + * @return mixed */ - public function includeVendor(Model $model) + public function includeCurrency(Model $model) { - return $this->item($model->vendor, new Vendor()); - } - - /** - * @param Model $model - * @return \League\Fractal\Resource\Item - */ - public function includeStatus(Model $model) - { - return $this->item($model->status, new BillStatus()); - } - - /** - * @param Model $model - * @return \League\Fractal\Resource\Collection - */ - public function includeItems(Model $model) - { - return $this->collection($model->items, new BillItems()); + return $this->item($model->currency, new Currency()); } /** @@ -83,6 +66,15 @@ class Bill extends TransformerAbstract return $this->collection($model->histories, new BillHistories()); } + /** + * @param Model $model + * @return \League\Fractal\Resource\Collection + */ + public function includeItems(Model $model) + { + return $this->collection($model->items, new BillItems()); + } + /** * @param Model $model * @return \League\Fractal\Resource\Collection @@ -91,4 +83,22 @@ class Bill extends TransformerAbstract { return $this->collection($model->payments, new BillPayments()); } + + /** + * @param Model $model + * @return \League\Fractal\Resource\Item + */ + public function includeStatus(Model $model) + { + return $this->item($model->status, new BillStatus()); + } + + /** + * @param Model $model + * @return \League\Fractal\Resource\Item + */ + public function includeVendor(Model $model) + { + return $this->item($model->vendor, new Vendor()); + } } diff --git a/app/Transformers/Expense/BillPayments.php b/app/Transformers/Expense/BillPayments.php index 9dd2c50ce..d5a736fa1 100644 --- a/app/Transformers/Expense/BillPayments.php +++ b/app/Transformers/Expense/BillPayments.php @@ -3,6 +3,7 @@ namespace App\Transformers\Expense; use App\Transformers\Banking\Account; +use App\Transformers\Setting\Currency; use App\Models\Expense\BillPayment as Model; use League\Fractal\TransformerAbstract; @@ -11,7 +12,7 @@ class BillPayments extends TransformerAbstract /** * @var array */ - protected $defaultIncludes = ['account']; + protected $defaultIncludes = ['account', 'currency']; /** * @param Model $model @@ -45,4 +46,13 @@ class BillPayments extends TransformerAbstract { return $this->item($model->account, new Account()); } + + /** + * @param Model $model + * @return mixed + */ + public function includeCurrency(Model $model) + { + return $this->item($model->currency, new Currency()); + } } diff --git a/app/Transformers/Expense/Payment.php b/app/Transformers/Expense/Payment.php index 0e722dc37..658e47890 100644 --- a/app/Transformers/Expense/Payment.php +++ b/app/Transformers/Expense/Payment.php @@ -5,6 +5,7 @@ namespace App\Transformers\Expense; use App\Transformers\Banking\Account; use App\Transformers\Expense\Vendor; use App\Transformers\Setting\Category; +use App\Transformers\Setting\Currency; use App\Models\Expense\Payment as Model; use League\Fractal\TransformerAbstract; @@ -13,7 +14,7 @@ class Payment extends TransformerAbstract /** * @var array */ - protected $defaultIncludes = ['account', 'vendor', 'category']; + protected $defaultIncludes = ['account', 'category', 'currency', 'vendor']; /** * @param Model $model @@ -49,6 +50,24 @@ class Payment extends TransformerAbstract return $this->item($model->account, new Account()); } + /** + * @param Model $model + * @return \League\Fractal\Resource\Item + */ + public function includeCategory(Model $model) + { + return $this->item($model->category, new Category()); + } + + /** + * @param Model $model + * @return mixed + */ + public function includeCurrency(Model $model) + { + return $this->item($model->currency, new Currency()); + } + /** * @param Model $model * @return mixed @@ -61,13 +80,4 @@ class Payment extends TransformerAbstract return $this->item($model->vendor, new Vendor()); } - - /** - * @param Model $model - * @return \League\Fractal\Resource\Item - */ - public function includeCategory(Model $model) - { - return $this->item($model->category, new Category()); - } } diff --git a/app/Transformers/Income/Invoice.php b/app/Transformers/Income/Invoice.php index d015e785f..c480af921 100644 --- a/app/Transformers/Income/Invoice.php +++ b/app/Transformers/Income/Invoice.php @@ -7,6 +7,7 @@ use App\Transformers\Income\InvoiceHistories; use App\Transformers\Income\InvoiceItems; use App\Transformers\Income\InvoicePayments; use App\Transformers\Income\InvoiceStatus; +use App\Transformers\Setting\Currency; use App\Models\Income\Invoice as Model; use League\Fractal\TransformerAbstract; @@ -15,7 +16,7 @@ class Invoice extends TransformerAbstract /** * @var array */ - protected $defaultIncludes = ['customer', 'status', 'items', 'payments', 'histories']; + protected $defaultIncludes = ['currency', 'customer', 'histories', 'items', 'payments', 'status']; /** * @param Model $model @@ -47,6 +48,15 @@ class Invoice extends TransformerAbstract ]; } + /** + * @param Model $model + * @return mixed + */ + public function includeCurrency(Model $model) + { + return $this->item($model->currency, new Currency()); + } + /** * @param Model $model * @return \League\Fractal\Resource\Item @@ -56,24 +66,6 @@ class Invoice extends TransformerAbstract return $this->item($model->customer, new Customer()); } - /** - * @param Model $model - * @return \League\Fractal\Resource\Item - */ - public function includeStatus(Model $model) - { - return $this->item($model->status, new InvoiceStatus()); - } - - /** - * @param Model $model - * @return \League\Fractal\Resource\Collection - */ - public function includeItems(Model $model) - { - return $this->collection($model->items, new InvoiceItems()); - } - /** * @param Model $model * @return \League\Fractal\Resource\Collection @@ -83,6 +75,15 @@ class Invoice extends TransformerAbstract return $this->collection($model->histories, new InvoiceHistories()); } + /** + * @param Model $model + * @return \League\Fractal\Resource\Collection + */ + public function includeItems(Model $model) + { + return $this->collection($model->items, new InvoiceItems()); + } + /** * @param Model $model * @return \League\Fractal\Resource\Collection @@ -91,4 +92,13 @@ class Invoice extends TransformerAbstract { return $this->collection($model->payments, new InvoicePayments()); } + + /** + * @param Model $model + * @return \League\Fractal\Resource\Item + */ + public function includeStatus(Model $model) + { + return $this->item($model->status, new InvoiceStatus()); + } } diff --git a/app/Transformers/Income/InvoicePayments.php b/app/Transformers/Income/InvoicePayments.php index cc8e5195a..532085e8c 100644 --- a/app/Transformers/Income/InvoicePayments.php +++ b/app/Transformers/Income/InvoicePayments.php @@ -3,6 +3,7 @@ namespace App\Transformers\Income; use App\Transformers\Banking\Account; +use App\Transformers\Setting\Currency; use App\Models\Income\InvoicePayment as Model; use League\Fractal\TransformerAbstract; @@ -11,7 +12,7 @@ class InvoicePayments extends TransformerAbstract /** * @var array */ - protected $defaultIncludes = ['account']; + protected $defaultIncludes = ['account', 'currency']; /** * @param Model $model @@ -45,4 +46,13 @@ class InvoicePayments extends TransformerAbstract { return $this->item($model->account, new Account()); } + + /** + * @param Model $model + * @return mixed + */ + public function includeCurrency(Model $model) + { + return $this->item($model->currency, new Currency()); + } } diff --git a/app/Transformers/Income/Revenue.php b/app/Transformers/Income/Revenue.php index 3aafc5e6f..6d1aaedc7 100644 --- a/app/Transformers/Income/Revenue.php +++ b/app/Transformers/Income/Revenue.php @@ -5,6 +5,7 @@ namespace App\Transformers\Income; use App\Transformers\Banking\Account; use App\Transformers\Income\Customer; use App\Transformers\Setting\Category; +use App\Transformers\Setting\Currency; use App\Models\Income\Revenue as Model; use League\Fractal\TransformerAbstract; @@ -13,7 +14,7 @@ class Revenue extends TransformerAbstract /** * @var array */ - protected $defaultIncludes = ['account', 'customer', 'category']; + protected $defaultIncludes = ['account', 'category', 'currency', 'customer']; /** * @param Model $model @@ -49,6 +50,24 @@ class Revenue extends TransformerAbstract return $this->item($model->account, new Account()); } + /** + * @param Model $model + * @return \League\Fractal\Resource\Item + */ + public function includeCategory(Model $model) + { + return $this->item($model->category, new Category()); + } + + /** + * @param Model $model + * @return mixed + */ + public function includeCurrency(Model $model) + { + return $this->item($model->currency, new Currency()); + } + /** * @param Model $model * @return mixed @@ -61,13 +80,4 @@ class Revenue extends TransformerAbstract return $this->item($model->customer, new Customer()); } - - /** - * @param Model $model - * @return \League\Fractal\Resource\Item - */ - public function includeCategory(Model $model) - { - return $this->item($model->category, new Category()); - } }