diff --git a/app/Jobs/Common/DeleteContact.php b/app/Jobs/Common/DeleteContact.php index 3c6a756b0..6b7e96489 100644 --- a/app/Jobs/Common/DeleteContact.php +++ b/app/Jobs/Common/DeleteContact.php @@ -60,7 +60,7 @@ class DeleteContact extends Job 'transactions' => 'transactions', ]; - if (in_array($this->contact->type, $this->getCustomerTypes())) { + if ($this->isCustomer()) { $rels['invoices'] = 'invoices'; } else { $rels['bills'] = 'bills'; diff --git a/app/Models/Common/Contact.php b/app/Models/Common/Contact.php index 982fb4e65..bc8d95132 100644 --- a/app/Models/Common/Contact.php +++ b/app/Models/Common/Contact.php @@ -133,7 +133,7 @@ class Contact extends Model { $amount = 0; - $collection = in_array($this->type, $this->getCustomerTypes()) ? 'invoices' : 'bills'; + $collection = $this->isCustomer() ? 'invoices' : 'bills'; $this->$collection->whereNotIn('status', ['draft', 'cancelled', 'paid'])->each(function ($item) use (&$amount) { $unpaid = $item->amount - $item->paid; diff --git a/app/Traits/Contacts.php b/app/Traits/Contacts.php index b4c836ed6..f0f6368b5 100644 --- a/app/Traits/Contacts.php +++ b/app/Traits/Contacts.php @@ -4,31 +4,44 @@ namespace App\Traits; trait Contacts { + public function isCustomer() + { + return in_array($this->type, $this->getCustomerTypes()); + } + + public function isVendor() + { + return in_array($this->type, $this->getVendorTypes()); + } + public function getCustomerTypes($return = 'array') { - $types = (string) setting('contact.type.customer', 'customer'); - - return ($return == 'array') ? explode(',', $types) : $types; + return $this->getContactTypes('customer', $return); } public function getVendorTypes($return = 'array') { - $types = (string) setting('contact.type.vendor', 'vendor'); + return $this->getContactTypes('vendor', $return); + } + + public function getContactTypes($index, $return = 'array') + { + $types = (string) setting('contact.type.' . $index, $index); return ($return == 'array') ? explode(',', $types) : $types; } public function addCustomerType($new_type) { - $this->addType($new_type, 'customer'); + $this->addContactType($new_type, 'customer'); } public function addVendorType($new_type) { - $this->addType($new_type, 'vendor'); + $this->addContactType($new_type, 'vendor'); } - public function addType($new_type, $index) + public function addContactType($new_type, $index) { $types = explode(',', setting('contact.type.' . $index, $index));