Merge pull request #52 from denisdulici/relationships

Improved disable/delete with relationships
This commit is contained in:
Denis Duliçi 2017-10-16 10:55:08 +03:00 committed by GitHub
commit 50dac9a952
16 changed files with 131 additions and 311 deletions

View File

@ -108,34 +108,21 @@ class Accounts extends Controller
*/ */
public function destroy(Account $account) public function destroy(Account $account)
{ {
$canDelete = $account->canDelete(); $relationships = $this->countRelationships($account, [
'bill_payments' => 'bills',
'payments' => 'payments',
'invoice_payments' => 'invoices',
'revenues' => 'revenues',
]);
if ($canDelete === true) { if (empty($relationships)) {
$account->delete(); $account->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.accounts', 1)]); $message = trans('messages.success.deleted', ['type' => trans_choice('general.accounts', 1)]);
flash($message)->success(); flash($message)->success();
} else { } else {
$text = array(); $message = trans('messages.warning.deleted', ['name' => $account->name, 'text' => implode(', ', $relationships)]);
if (isset($canDelete['bills'])) {
$text[] = '<b>' . $canDelete['bills'] . '</b> ' . trans_choice('general.bills', ($canDelete['bills'] > 1) ? 2 : 1);
}
if (isset($canDelete['payments'])) {
$text[] = '<b>' . $canDelete['payments'] . '</b> ' . trans_choice('general.payments', ($canDelete['payments'] > 1) ? 2 : 1);
}
if (isset($canDelete['invoices'])) {
$text[] = '<b>' . $canDelete['invoices'] . '</b> ' . trans_choice('general.invoices', ($canDelete['invoices'] > 1) ? 2 : 1);
}
if (isset($canDelete['revenues'])) {
$text[] = '<b>' . $canDelete['revenues'] . '</b> ' . trans_choice('general.revenues', ($canDelete['revenues'] > 1) ? 2 : 1);
}
$message = trans('messages.warning.deleted', ['type' => trans_choice('general.accounts', 1), 'text' => implode(', ', $text)]);
flash($message)->warning(); flash($message)->warning();
} }

View File

@ -44,4 +44,17 @@ class Controller extends BaseController
$this->middleware('permission:update-' . $controller)->only(['update']); $this->middleware('permission:update-' . $controller)->only(['update']);
$this->middleware('permission:delete-' . $controller)->only('destroy'); $this->middleware('permission:delete-' . $controller)->only('destroy');
} }
public function countRelationships($model, $relationships)
{
$counter = array();
foreach ($relationships as $relationship => $text) {
if ($c = $model->$relationship()->count()) {
$counter[] = $c . ' ' . strtolower(trans_choice('general.' . $text, ($c > 1) ? 2 : 1));
}
}
return $counter;
}
} }

View File

@ -94,26 +94,19 @@ class Vendors extends Controller
*/ */
public function destroy(Vendor $vendor) public function destroy(Vendor $vendor)
{ {
$canDelete = $vendor->canDelete(); $relationships = $this->countRelationships($vendor, [
'bills' => 'bills',
'payments' => 'payments',
]);
if ($canDelete === true) { if (empty($relationships)) {
$vendor->delete(); $vendor->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.vendors', 1)]); $message = trans('messages.success.deleted', ['type' => trans_choice('general.vendors', 1)]);
flash($message)->success(); flash($message)->success();
} else { } else {
$text = array(); $message = trans('messages.warning.deleted', ['name' => $vendor->name, 'text' => implode(', ', $relationships)]);
if (isset($canDelete['bills'])) {
$text[] = '<b>' . $canDelete['bills'] . '</b> ' . trans_choice('general.bills', ($canDelete['bills'] > 1) ? 2 : 1);
}
if (isset($canDelete['payments'])) {
$text[] = '<b>' . $canDelete['payments'] . '</b> ' . trans_choice('general.payments', ($canDelete['payments'] > 1) ? 2 : 1);
}
$message = trans('messages.warning.deleted', ['type' => trans_choice('general.vendors', 1), 'text' => implode(', ', $text)]);
flash($message)->warning(); flash($message)->warning();
} }

View File

@ -127,26 +127,19 @@ class Customers extends Controller
*/ */
public function destroy(Customer $customer) public function destroy(Customer $customer)
{ {
$canDelete = $customer->canDelete(); $relationships = $this->countRelationships($customer, [
'invoices' => 'invoices',
'revenues' => 'revenues',
]);
if ($canDelete === true) { if (empty($relationships)) {
$customer->delete(); $customer->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.customers', 1)]); $message = trans('messages.success.deleted', ['type' => trans_choice('general.customers', 1)]);
flash($message)->success(); flash($message)->success();
} else { } else {
$text = array(); $message = trans('messages.warning.deleted', ['name' => $customer->name, 'text' => implode(', ', $relationships)]);
if (isset($canDelete['invoices'])) {
$text[] = '<b>' . $canDelete['invoices'] . '</b> ' . trans_choice('general.invoices', ($canDelete['invoices'] > 1) ? 2 : 1);
}
if (isset($canDelete['revenues'])) {
$text[] = '<b>' . $canDelete['revenues'] . '</b> ' . trans_choice('general.revenues', ($canDelete['revenues'] > 1) ? 2 : 1);
}
$message = trans('messages.warning.deleted', ['type' => trans_choice('general.customers', 1), 'text' => implode(', ', $text)]);
flash($message)->warning(); flash($message)->warning();
} }

View File

@ -117,26 +117,19 @@ class Items extends Controller
*/ */
public function destroy(Item $item) public function destroy(Item $item)
{ {
$canDelete = $item->canDelete(); $relationships = $this->countRelationships($item, [
'invoice_items' => 'invoices',
'bill_items' => 'bills',
]);
if ($canDelete === true) { if (empty($relationships)) {
$item->delete(); $item->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.items', 1)]); $message = trans('messages.success.deleted', ['type' => trans_choice('general.items', 1)]);
flash($message)->success(); flash($message)->success();
} else { } else {
$text = array(); $message = trans('messages.warning.deleted', ['name' => $item->name, 'text' => implode(', ', $relationships)]);
if (isset($canDelete['bills'])) {
$text[] = '<b>' . $canDelete['bills'] . '</b> ' . trans_choice('general.bills', ($canDelete['bills'] > 1) ? 2 : 1);
}
if (isset($canDelete['invoices'])) {
$text[] = '<b>' . $canDelete['invoices'] . '</b> ' . trans_choice('general.invoices', ($canDelete['invoices'] > 1) ? 2 : 1);
}
$message = trans('messages.warning.deleted', ['type' => trans_choice('general.items', 1), 'text' => implode(', ', $text)]);
flash($message)->warning(); flash($message)->warning();
} }

View File

@ -74,13 +74,27 @@ class Categories extends Controller
*/ */
public function update(Category $category, Request $request) public function update(Category $category, Request $request)
{ {
$category->update($request->all()); $relationships = $this->countRelationships($category, [
'items' => 'items',
'revenues' => 'revenues',
'payments' => 'payments',
]);
$message = trans('messages.success.updated', ['type' => trans_choice('general.categories', 1)]); if (empty($relationships) || $request['enabled']) {
$category->update($request->all());
flash($message)->success(); $message = trans('messages.success.updated', ['type' => trans_choice('general.categories', 1)]);
return redirect('settings/categories'); flash($message)->success();
return redirect('settings/categories');
} else {
$message = trans('messages.warning.disabled', ['name' => $category->name, 'text' => implode(', ', $relationships)]);
flash($message)->warning();
return redirect('settings/categories/' . $category->id . '/edit');
}
} }
/** /**
@ -92,30 +106,20 @@ class Categories extends Controller
*/ */
public function destroy(Category $category) public function destroy(Category $category)
{ {
$canDelete = $category->canDelete(); $relationships = $this->countRelationships($category, [
'items' => 'items',
'revenues' => 'revenues',
'payments' => 'payments',
]);
if ($canDelete === true) { if (empty($relationships)) {
$category->delete(); $category->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.categories', 1)]); $message = trans('messages.success.deleted', ['type' => trans_choice('general.categories', 1)]);
flash($message)->success(); flash($message)->success();
} else { } else {
$text = array(); $message = trans('messages.warning.deleted', ['name' => $category->name, 'text' => implode(', ', $relationships)]);
if (isset($canDelete['items'])) {
$text[] = '<b>' . $canDelete['items'] . '</b> ' . trans_choice('general.items', ($canDelete['items'] > 1) ? 2 : 1);
}
if (isset($canDelete['payments'])) {
$text[] = '<b>' . $canDelete['payments'] . '</b> ' . trans_choice('general.payments', ($canDelete['payments'] > 1) ? 2 : 1);
}
if (isset($canDelete['revenues'])) {
$text[] = '<b>' . $canDelete['revenues'] . '</b> ' . trans_choice('general.items', ($canDelete['revenues'] > 1) ? 2 : 1);
}
$message = trans('messages.warning.deleted', ['type' => trans_choice('general.categories', 1), 'text' => implode(', ', $text)]);
flash($message)->warning(); flash($message)->warning();
} }

View File

@ -95,9 +95,20 @@ class Currencies extends Controller
*/ */
public function update(Currency $currency, Request $request) public function update(Currency $currency, Request $request)
{ {
$canDisable = $currency->canDisable(); $relationships = $this->countRelationships($currency, [
'accounts' => 'accounts',
'customers' => 'customers',
'invoices' => 'invoices',
'revenues' => 'revenues',
'bills' => 'bills',
'payments' => 'payments',
]);
if ($canDisable === true || $request['enabled']) { if ($currency->code == setting('general.default_currency')) {
$relationships[] = strtolower(trans_choice('general.companies', 1));
}
if (empty($relationships) || $request['enabled']) {
$currency->update($request->all()); $currency->update($request->all());
// Update default currency setting // Update default currency setting
@ -109,43 +120,15 @@ class Currencies extends Controller
$message = trans('messages.success.updated', ['type' => trans_choice('general.currencies', 1)]); $message = trans('messages.success.updated', ['type' => trans_choice('general.currencies', 1)]);
flash($message)->success(); flash($message)->success();
return redirect('settings/currencies');
} else { } else {
$text = array(); $message = trans('messages.warning.disabled', ['name' => $currency->name, 'text' => implode(', ', $relationships)]);
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(); flash($message)->warning();
}
return redirect('settings/currencies'); return redirect('settings/currencies/' . $currency->id . '/edit');
}
} }
/** /**
@ -157,11 +140,30 @@ class Currencies extends Controller
*/ */
public function destroy(Currency $currency) public function destroy(Currency $currency)
{ {
$currency->delete(); $relationships = $this->countRelationships($currency, [
'accounts' => 'accounts',
'customers' => 'customers',
'invoices' => 'invoices',
'revenues' => 'revenues',
'bills' => 'bills',
'payments' => 'payments',
]);
$message = trans('messages.success.deleted', ['type' => trans_choice('general.currencies', 1)]); if ($currency->code == setting('general.default_currency')) {
$relationships[] = strtolower(trans_choice('general.companies', 1));
}
flash($message)->success(); if (empty($relationships)) {
$currency->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.currencies', 1)]);
flash($message)->success();
} else {
$message = trans('messages.warning.deleted', ['name' => $currency->name, 'text' => implode(', ', $relationships)]);
flash($message)->warning();
}
return redirect('settings/currencies'); return redirect('settings/currencies');
} }

View File

@ -71,13 +71,27 @@ class Taxes extends Controller
*/ */
public function update(Tax $tax, Request $request) public function update(Tax $tax, Request $request)
{ {
$tax->update($request->all()); $relationships = $this->countRelationships($tax, [
'items' => 'items',
'invoice_items' => 'invoices',
'bill_items' => 'bills',
]);
$message = trans('messages.success.updated', ['type' => trans_choice('general.tax_rates', 1)]); if (empty($relationships) || $request['enabled']) {
$tax->update($request->all());
flash($message)->success(); $message = trans('messages.success.updated', ['type' => trans_choice('general.tax_rates', 1)]);
return redirect('settings/taxes'); flash($message)->success();
return redirect('settings/taxes');
} else {
$message = trans('messages.warning.disabled', ['name' => $tax->name, 'text' => implode(', ', $relationships)]);
flash($message)->warning();
return redirect('settings/taxes/' . $tax->id . '/edit');
}
} }
/** /**
@ -89,30 +103,20 @@ class Taxes extends Controller
*/ */
public function destroy(Tax $tax) public function destroy(Tax $tax)
{ {
$canDelete = $tax->canDelete(); $relationships = $this->countRelationships($tax, [
'items' => 'items',
'invoice_items' => 'invoices',
'bill_items' => 'bills',
]);
if ($canDelete === true) { if (empty($relationships)) {
$tax->delete(); $tax->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.taxes', 1)]); $message = trans('messages.success.deleted', ['type' => trans_choice('general.taxes', 1)]);
flash($message)->success(); flash($message)->success();
} else { } else {
$text = array(); $message = trans('messages.warning.deleted', ['name' => $tax->name, 'text' => implode(', ', $relationships)]);
if (isset($canDelete['items'])) {
$text[] = '<b>' . $canDelete['items'] . '</b> ' . trans_choice('general.items', ($canDelete['items'] > 1) ? 2 : 1);
}
if (isset($canDelete['bills'])) {
$text[] = '<b>' . $canDelete['bills'] . '</b> ' . trans_choice('general.bills', ($canDelete['bills'] > 1) ? 2 : 1);
}
if (isset($canDelete['invoices'])) {
$text[] = '<b>' . $canDelete['invoices'] . '</b> ' . trans_choice('general.items', ($canDelete['invoices'] > 1) ? 2 : 1);
}
$message = trans('messages.warning.deleted', ['type' => trans_choice('general.taxes', 1), 'text' => implode(', ', $text)]);
flash($message)->warning(); flash($message)->warning();
} }

View File

@ -81,33 +81,6 @@ class Account extends Model
$this->attributes['opening_balance'] = (float) $value; $this->attributes['opening_balance'] = (float) $value;
} }
public function canDelete()
{
$error = false;
if ($bill_payments = $this->bill_payments()->count()) {
$error['bills'] = $bill_payments;
}
if ($payments = $this->payments()->count()) {
$error['payments'] = $payments;
}
if ($invoice_payments = $this->invoice_payments()->count()) {
$error['invoices'] = $invoice_payments;
}
if ($revenues = $this->revenues()->count()) {
$error['revenues'] = $revenues;
}
if ($error) {
return $error;
}
return true;
}
/** /**
* Get the current balance. * Get the current balance.
* *

View File

@ -52,23 +52,4 @@ class Vendor extends Model
{ {
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code'); return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
} }
public function canDelete()
{
$error = false;
if ($bills = $this->bills()->count()) {
$error['bills'] = $bills;
}
if ($payments = $this->payments()->count()) {
$error['payments'] = $payments;
}
if ($error) {
return $error;
}
return true;
}
} }

View File

@ -59,23 +59,4 @@ class Customer extends Model
{ {
return $this->belongsTo('App\Models\Auth\User', 'customer_id', 'id'); return $this->belongsTo('App\Models\Auth\User', 'customer_id', 'id');
} }
public function canDelete()
{
$error = false;
if ($invoices = $this->invoices()->count()) {
$error['invoices'] = $invoices;
}
if ($revenues = $this->revenues()->count()) {
$error['revenues'] = $revenues;
}
if ($error) {
return $error;
}
return true;
}
} }

View File

@ -81,25 +81,6 @@ class Item extends Model
$this->attributes['purchase_price'] = (float) $value; $this->attributes['purchase_price'] = (float) $value;
} }
public function canDelete()
{
$error = false;
if ($bills = $this->bill_items()->count()) {
$error['bills'] = $bills;
}
if ($invoices = $this->invoice_items()->count()) {
$error['invoices'] = $invoices;
}
if ($error) {
return $error;
}
return true;
}
public static function getItems($filter_data = array()) public static function getItems($filter_data = array())
{ {
if (empty($filter_data)) { if (empty($filter_data)) {

View File

@ -37,29 +37,6 @@ class Category extends Model
return $this->hasMany('App\Models\Item\Item'); return $this->hasMany('App\Models\Item\Item');
} }
public function canDelete()
{
$error = false;
if ($items = $this->items()->count()) {
$error['items'] = $items;
}
if ($payments = $this->payments()->count()) {
$error['payments'] = $payments;
}
if ($revenues = $this->revenues()->count()) {
$error['revenues'] = $revenues;
}
if ($error) {
return $error;
}
return true;
}
/** /**
* Scope to only include categories of a given type. * Scope to only include categories of a given type.
* *

View File

@ -63,43 +63,4 @@ class Currency extends Model
{ {
$this->attributes['rate'] = (float) $value; $this->attributes['rate'] = (float) $value;
} }
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

@ -37,27 +37,4 @@ class Tax extends Model
{ {
return $this->hasMany('App\Models\Income\InvoiceItem'); return $this->hasMany('App\Models\Income\InvoiceItem');
} }
public function canDelete()
{
$error = false;
if ($items = $this->items()->count()) {
$error['items'] = $items;
}
if ($bills = $this->bill_items()->count()) {
$error['bills'] = $bills;
}
if ($invoices = $this->invoice_items()->count()) {
$error['invoices'] = $invoices;
}
if ($error) {
return $error;
}
return true;
}
} }

View File

@ -11,8 +11,8 @@ return [
'not_user_company' => 'Error: You are not allowed to manage this company!', 'not_user_company' => 'Error: You are not allowed to manage this company!',
], ],
'warning' => [ 'warning' => [
'deleted' => 'Warning: You are not delete :type. Because it has :text', 'deleted' => 'Warning: You are not allowed to delete <b>:name</b> because it has :text related.',
'disabled' => 'Warning: You are not disable :type. Because it has :text', 'disabled' => 'Warning: You are not allowed to disable <b>:name</b> because it has :text related.',
], ],
]; ];