close #25 Fixed: Fatal error when disable currency used by company/account/customer

This commit is contained in:
cuneytsenturk 2017-09-26 14:59:17 +03:00
parent 0f29bda12b
commit 66b67a4c7f
3 changed files with 93 additions and 15 deletions

View File

@ -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[] = '<b>' . $canDisable['company'] . '</b> ' . trans_choice('general.companies', ($canDisable['company'] > 1) ? 2 : 1);
}
if (isset($canDisable['accounts'])) {
$text[] = '<b>' . $canDisable['accounts'] . '</b> ' . trans_choice('general.accounts', ($canDisable['accounts'] > 1) ? 2 : 1);
}
if (isset($canDisable['customers'])) {
$text[] = '<b>' . $canDisable['customers'] . '</b> ' . trans_choice('general.customers', ($canDisable['customers'] > 1) ? 2 : 1);
}
if (isset($canDisable['invoices'])) {
$text[] = '<b>' . $canDisable['invoices'] . '</b> ' . trans_choice('general.invoices', ($canDisable['invoices'] > 1) ? 2 : 1);
}
if (isset($canDisable['revenues'])) {
$text[] = '<b>' . $canDisable['revenues'] . '</b> ' . trans_choice('general.revenues', ($canDisable['revenues'] > 1) ? 2 : 1);
}
if (isset($canDisable['bills'])) {
$text[] = '<b>' . $canDisable['bills'] . '</b> ' . trans_choice('general.bills', ($canDisable['bills'] > 1) ? 2 : 1);
}
if (isset($canDisable['payments'])) {
$text[] = '<b>' . $canDisable['payments'] . '</b> ' . 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');
}

View File

@ -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;
}
}

View File

@ -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',
],
];