improved disable/delete with relationships

This commit is contained in:
denisdulici
2017-10-16 10:51:43 +03:00
parent ae52ce5a43
commit b1b36d8a64
16 changed files with 131 additions and 311 deletions

View File

@ -81,33 +81,6 @@ class Account extends Model
$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.
*

View File

@ -52,23 +52,4 @@ class Vendor extends Model
{
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');
}
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;
}
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())
{
if (empty($filter_data)) {

View File

@ -37,29 +37,6 @@ class Category extends Model
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.
*

View File

@ -63,43 +63,4 @@ class Currency extends Model
{
$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');
}
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;
}
}