From e01b61cef59a7004a7abf4bb0104a177a7d9b379 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Fri, 10 Feb 2017 09:43:46 +0200 Subject: [PATCH] Refactor tax rate code --- app/Http/Controllers/InvoiceController.php | 25 ++++------------------ app/Http/Controllers/QuoteController.php | 23 +++----------------- app/Ninja/Presenters/AccountPresenter.php | 17 +++++++++++++++ 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 91d292a614e0..9f49bc067d2a 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -219,6 +219,8 @@ class InvoiceController extends BaseController private static function getViewModel($invoice) { + $account = Auth::user()->account; + $recurringHelp = ''; $recurringDueDateHelp = ''; $recurringDueDates = []; @@ -282,25 +284,6 @@ class InvoiceController extends BaseController } } - // Tax rate $options - $account = Auth::user()->account; - $rates = TaxRate::scope()->orderBy('name')->get(); - $options = []; - $defaultTax = false; - - foreach ($rates as $rate) { - $name = $rate->name . ' ' . ($rate->rate + 0) . '%'; - if ($rate->is_inclusive) { - $name .= ' - ' . trans('texts.inclusive'); - } - $options[($rate->is_inclusive ? '1 ' : '0 ') . $rate->rate . ' ' . $rate->name] = $name; - - // load default invoice tax - if ($rate->id == $account->default_tax_rate_id) { - $defaultTax = $rate; - } - } - // Check for any taxes which have been deleted if ($invoice->exists) { foreach ($invoice->getTaxes() as $key => $rate) { @@ -315,8 +298,8 @@ class InvoiceController extends BaseController 'data' => Input::old('data'), 'account' => Auth::user()->account->load('country'), 'products' => Product::scope()->with('default_tax_rate')->orderBy('product_key')->get(), - 'taxRateOptions' => $options, - 'defaultTax' => $defaultTax, + 'taxRateOptions' => $account->present()->taxRateOptions, + 'defaultTax' => $account->default_tax_rate, 'currencies' => Cache::get('currencies'), 'sizes' => Cache::get('sizes'), 'paymentTerms' => Cache::get('paymentTerms'), diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index ea6983c36220..c02e54672daa 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -93,31 +93,14 @@ class QuoteController extends BaseController private static function getViewModel() { - // Tax rate $options $account = Auth::user()->account; - $rates = TaxRate::scope()->orderBy('name')->get(); - $options = []; - $defaultTax = false; - - foreach ($rates as $rate) { - $name = $rate->name . ' ' . ($rate->rate + 0) . '%'; - if ($rate->is_inclusive) { - $name .= ' - ' . trans('texts.inclusive'); - } - $options[($rate->is_inclusive ? '1 ' : '0 ') . $rate->rate . ' ' . $rate->name] = $name; - - // load default invoice tax - if ($rate->id == $account->default_tax_rate_id) { - $defaultTax = $rate; - } - } return [ 'entityType' => ENTITY_QUOTE, - 'account' => Auth::user()->account, + 'account' => $account, 'products' => Product::scope()->orderBy('id')->get(['product_key', 'notes', 'cost', 'qty']), - 'taxRateOptions' => $options, - 'defaultTax' => $defaultTax, + 'taxRateOptions' => $account->present()->taxRateOptions, + 'defaultTax' => $account->default_tax_rate, 'countries' => Cache::get('countries'), 'clients' => Client::scope()->with('contacts', 'country')->orderBy('name')->get(), 'taxRates' => TaxRate::scope()->orderBy('name')->get(), diff --git a/app/Ninja/Presenters/AccountPresenter.php b/app/Ninja/Presenters/AccountPresenter.php index 9921b33c4bce..0687cc255dae 100644 --- a/app/Ninja/Presenters/AccountPresenter.php +++ b/app/Ninja/Presenters/AccountPresenter.php @@ -4,6 +4,7 @@ namespace App\Ninja\Presenters; use Carbon; use Domain; +use App\Models\TaxRate; use Laracasts\Presenter\Presenter; use stdClass; use Utils; @@ -132,4 +133,20 @@ class AccountPresenter extends Presenter return $str; } + + public function taxRateOptions() + { + $rates = TaxRate::scope()->orderBy('name')->get(); + $options = []; + + foreach ($rates as $rate) { + $name = $rate->name . ' ' . ($rate->rate + 0) . '%'; + if ($rate->is_inclusive) { + $name .= ' - ' . trans('texts.inclusive'); + } + $options[($rate->is_inclusive ? '1 ' : '0 ') . $rate->rate . ' ' . $rate->name] = $name; + } + + return $options; + } }