Skip to content

Commit

Permalink
close #25 Fixed: Fatal error when disable currency used by company/ac…
Browse files Browse the repository at this point in the history
…count/customer
  • Loading branch information
cuneytsenturk committed Sep 26, 2017
1 parent 0f29bda commit 66b67a4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 14 deletions.
54 changes: 46 additions & 8 deletions app/Http/Controllers/Settings/Currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,55 @@ public function edit(Currency $currency)
*/
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());

$message = trans('messages.success.updated', ['type' => trans_choice('general.currencies', 1)]);
// Update default currency setting
if ($request['default_currency']) {
setting()->set('general.default_currency', $request['code']);
setting()->save();
}

flash($message)->success();
$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();
}

return redirect('settings/currencies');
}
Expand Down
51 changes: 45 additions & 6 deletions app/Models/Setting/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions resources/lang/en-GB/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
'warning' => [
'deleted' => 'Warning: You are not delete :type. Because it has :text',
'disabled' => 'Warning: You are not disable :type. Because it has :text',
],

];

0 comments on commit 66b67a4

Please sign in to comment.