From 66b67a4c7fd340ef330e2a4d5426bb60e631288c Mon Sep 17 00:00:00 2001 From: cuneytsenturk Date: Tue, 26 Sep 2017 14:59:17 +0300 Subject: [PATCH] close #25 Fixed: Fatal error when disable currency used by company/account/customer --- app/Http/Controllers/Settings/Currencies.php | 56 ++++++++++++++++---- app/Models/Setting/Currency.php | 51 +++++++++++++++--- resources/lang/en-GB/messages.php | 1 + 3 files changed, 93 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Settings/Currencies.php b/app/Http/Controllers/Settings/Currencies.php index d0936c2a3..02c6a37d2 100644 --- a/app/Http/Controllers/Settings/Currencies.php +++ b/app/Http/Controllers/Settings/Currencies.php @@ -95,18 +95,56 @@ class Currencies extends Controller */ public function update(Currency $currency, Request $request) { - $currency->update($request->all()); + $canDisable = $currency->canDisable(); - // Update default currency setting - if ($request['default_currency']) { - setting()->set('general.default_currency', $request['code']); - setting()->save(); + if ($canDisable === true) { + $currency->update($request->all()); + + // Update default currency setting + if ($request['default_currency']) { + setting()->set('general.default_currency', $request['code']); + setting()->save(); + } + + $message = trans('messages.success.updated', ['type' => trans_choice('general.currencies', 1)]); + + flash($message)->success(); + } else { + $text = array(); + + if (isset($canDisable['company'])) { + $text[] = '' . $canDisable['company'] . ' ' . trans_choice('general.companies', ($canDisable['company'] > 1) ? 2 : 1); + } + + if (isset($canDisable['accounts'])) { + $text[] = '' . $canDisable['accounts'] . ' ' . trans_choice('general.accounts', ($canDisable['accounts'] > 1) ? 2 : 1); + } + + if (isset($canDisable['customers'])) { + $text[] = '' . $canDisable['customers'] . ' ' . trans_choice('general.customers', ($canDisable['customers'] > 1) ? 2 : 1); + } + + if (isset($canDisable['invoices'])) { + $text[] = '' . $canDisable['invoices'] . ' ' . trans_choice('general.invoices', ($canDisable['invoices'] > 1) ? 2 : 1); + } + + if (isset($canDisable['revenues'])) { + $text[] = '' . $canDisable['revenues'] . ' ' . trans_choice('general.revenues', ($canDisable['revenues'] > 1) ? 2 : 1); + } + + if (isset($canDisable['bills'])) { + $text[] = '' . $canDisable['bills'] . ' ' . trans_choice('general.bills', ($canDisable['bills'] > 1) ? 2 : 1); + } + + if (isset($canDisable['payments'])) { + $text[] = '' . $canDisable['payments'] . ' ' . trans_choice('general.payments', ($canDisable['payments'] > 1) ? 2 : 1); + } + + $message = trans('messages.warning.disabled', ['type' => trans_choice('general.currencies', 1), 'text' => implode(', ', $text)]); + + flash($message)->warning(); } - $message = trans('messages.success.updated', ['type' => trans_choice('general.currencies', 1)]); - - flash($message)->success(); - return redirect('settings/currencies'); } diff --git a/app/Models/Setting/Currency.php b/app/Models/Setting/Currency.php index b21348a89..42cfc1716 100644 --- a/app/Models/Setting/Currency.php +++ b/app/Models/Setting/Currency.php @@ -25,31 +25,70 @@ class Currency extends Model public function accounts() { - return $this->hasMany('App\Models\Banking\Account'); + return $this->hasMany('App\Models\Banking\Account', 'currency_code', 'code'); } public function customers() { - return $this->hasMany('App\Models\Income\Customer'); + return $this->hasMany('App\Models\Income\Customer', 'currency_code', 'code'); } public function invoices() { - return $this->hasMany('App\Models\Income\Invoice', 'code', 'currency_code'); + return $this->hasMany('App\Models\Income\Invoice', 'currency_code', 'code'); } public function revenues() { - return $this->hasMany('App\Models\Income\Revenue', 'code', 'currency_code'); + return $this->hasMany('App\Models\Income\Revenue', 'currency_code', 'code'); } public function bills() { - return $this->hasMany('App\Models\Expense\Bill', 'code', 'currency_code'); + return $this->hasMany('App\Models\Expense\Bill', 'currency_code', 'code'); } public function payments() { - return $this->hasMany('App\Models\Expense\Payment', 'code', 'currency_code'); + return $this->hasMany('App\Models\Expense\Payment', 'currency_code', 'code'); + } + + public function canDisable() + { + $error = false; + + if ($this->code == setting('general.default_currency')) { + $error['company'] = 1; + } + + if ($accounts = $this->accounts()->count()) { + $error['accounts'] = $accounts; + } + + if ($customers = $this->customers()->count()) { + $error['customers'] = $customers; + } + + if ($invoices = $this->invoices()->count()) { + $error['invoices'] = $invoices; + } + + if ($revenues = $this->revenues()->count()) { + $error['revenues'] = $revenues; + } + + if ($bills = $this->bills()->count()) { + $error['bills'] = $bills; + } + + if ($payments = $this->payments()->count()) { + $error['payments'] = $payments; + } + + if ($error) { + return $error; + } + + return true; } } diff --git a/resources/lang/en-GB/messages.php b/resources/lang/en-GB/messages.php index c65a8511e..36961b225 100644 --- a/resources/lang/en-GB/messages.php +++ b/resources/lang/en-GB/messages.php @@ -12,6 +12,7 @@ return [ ], 'warning' => [ 'deleted' => 'Warning: You are not delete :type. Because it has :text', + 'disabled' => 'Warning: You are not disable :type. Because it has :text', ], ];