diff --git a/app/Models/Cashup.php b/app/Models/Cashup.php index e503b6384..44323857f 100644 --- a/app/Models/Cashup.php +++ b/app/Models/Cashup.php @@ -79,7 +79,7 @@ class Cashup extends Model /** * Searches cashups */ - public function search(string $search, array $filters, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'cashup_id', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, array $filters, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'cashup_id', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; @@ -215,9 +215,9 @@ class Cashup extends Model /** * Inserts or updates a cashup */ - public function save_value(array &$cash_up_data, $cashup_id = NEW_ITEM): bool + public function save_value(array &$cash_up_data, $cashup_id = NEW_ENTRY): bool { - if(!$cashup_id == NEW_ITEM || !$this->exists($cashup_id)) + if(!$cashup_id == NEW_ENTRY || !$this->exists($cashup_id)) { $builder = $this->db->table('cash_up'); if($builder->insert($cash_up_data)) diff --git a/app/Models/Employee.php b/app/Models/Employee.php index 8b2336d82..ab95ed6e2 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -123,7 +123,7 @@ class Employee extends Person /** * Inserts or updates an employee */ - public function save_employee(array &$person_data, array &$employee_data, array &$grants_data, bool $employee_id = FALSE): bool + public function save_employee(array &$person_data, array &$employee_data, array &$grants_data, int $employee_id = NEW_ENTRY): bool { $success = FALSE; @@ -133,7 +133,7 @@ class Employee extends Person if(ENVIRONMENT != 'testing' && parent::save_value($person_data, $employee_id)) { $builder = $this->db->table('employees'); - if(!$employee_id || !$this->exists($employee_id)) + if($employee_id == NEW_ENTRY || !$this->exists($employee_id)) { $employee_data['person_id'] = $employee_id = $person_data['person_id']; $success = $builder->insert($employee_data); @@ -335,7 +335,7 @@ class Employee extends Person /** * Performs a search on employees */ - public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'last_name', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'last_name', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; diff --git a/app/Models/Expense.php b/app/Models/Expense.php index d1474d21a..872fc3873 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -87,7 +87,7 @@ class Expense extends Model /** * Searches expenses */ - public function search(string $search, array $filters, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'expense_id', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, array $filters, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'expense_id', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; @@ -227,35 +227,56 @@ class Expense extends Model $query = $builder->get(); - if($query->getNumRows() == 1) //TODO: === + if ($query->getNumRows() == 1) //TODO: === { return $query->getRow(); } - else //TODO: No need for this else statement. Just put it's contents outside of the else since the if has a return in it. - { - //Get empty base parent object - $expenses_obj = new stdClass(); - //Get all the fields from expenses table - foreach($this->db->getFieldNames('expenses') as $field) + $empty_obj = $this->getEmptyObject('expenses'); + $empty_obj->supplier_name = NULL; + $empty_obj->first_name = NULL; + $empty_obj->last_name = NULL; + + return $empty_obj; + } + + /** + * Initializes an empty object based on database definitions + * @param string $table_name + * @return object + */ + private function getEmptyObject(string $table_name): object + { + // Return an empty base parent object, as $item_id is NOT an item + $empty_obj = new stdClass(); + + // Iterate through field definitions to determine how the fields should be initialized + + foreach($this->db->getFieldData($table_name) as $field) { + + $field_name = $field->name; + + if(in_array($field->type, array('int', 'tinyint', 'decimal'))) { - $expenses_obj->$field = ''; + $empty_obj->$field_name = ($field->primary_key == 1) ? NEW_ENTRY : 0; + } + else + { + $empty_obj->$field_name = NULL; } - - $expenses_obj->supplier_name = ''; - - return $expenses_obj; } + + return $empty_obj; } /** * Inserts or updates an expense */ - public function save_value(array &$expense_data, bool $expense_id = FALSE): bool + public function save_value(array &$expense_data, int $expense_id = NEW_ENTRY): bool { $builder = $this->db->table('expenses'); - if(!$expense_id || !$this->exists($expense_id)) + if($expense_id == NEW_ENTRY || !$this->exists($expense_id)) { if($builder->insert($expense_data)) { diff --git a/app/Models/Expense_category.php b/app/Models/Expense_category.php index d753850d3..5bfc8a841 100644 --- a/app/Models/Expense_category.php +++ b/app/Models/Expense_category.php @@ -109,11 +109,11 @@ class Expense_category extends Model /** * Inserts or updates an expense_category */ - public function save_value(array &$expense_category_data, bool $expense_category_id = FALSE): bool + public function save_value(array &$expense_category_data, int $expense_category_id = NEW_ENTRY): bool { $builder = $this->db->table('expense_categories'); - if(!$expense_category_id || !$this->exists($expense_category_id)) + if($expense_category_id == NEW_ENTRY || !$this->exists($expense_category_id)) { if($builder->insert($expense_category_data)) { @@ -152,7 +152,7 @@ class Expense_category extends Model /** * Perform a search on expense_category */ - public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'category_name', ?string $order='asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'category_name', ?string $order='asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; diff --git a/app/Models/Giftcard.php b/app/Models/Giftcard.php index f4e8bbfdf..bb4ed3556 100644 --- a/app/Models/Giftcard.php +++ b/app/Models/Giftcard.php @@ -126,11 +126,11 @@ class Giftcard extends Model /** * Inserts or updates a giftcard */ - public function save_value(array &$giftcard_data, $giftcard_id = FALSE): bool + public function save_value(array &$giftcard_data, int $giftcard_id = NEW_ENTRY): bool { $builder = $this->db->table('giftcards'); - if(!$giftcard_id || !$this->exists($giftcard_id)) + if($giftcard_id == NEW_ENTRY || !$this->exists($giftcard_id)) { if($builder->insert($giftcard_data)) { @@ -233,7 +233,7 @@ class Giftcard extends Model /** * Performs a search on giftcards */ - public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'giftcard_number', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'giftcard_number', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; diff --git a/app/Models/Item_kit.php b/app/Models/Item_kit.php index b02e5bb60..b5a96edee 100644 --- a/app/Models/Item_kit.php +++ b/app/Models/Item_kit.php @@ -167,10 +167,10 @@ class Item_kit extends Model /** * Inserts or updates an item kit */ - public function save_value(array &$item_kit_data, bool $item_kit_id = FALSE): bool + public function save_value(array &$item_kit_data, int $item_kit_id = NEW_ENTRY): bool { $builder = $this->db->table('item_kits'); - if(!$item_kit_id || !$this->exists($item_kit_id)) + if($item_kit_id == NEW_ENTRY || !$this->exists($item_kit_id)) { if($builder->insert($item_kit_data)) { @@ -257,7 +257,7 @@ class Item_kit extends Model /** * Perform a search on items */ - public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'name', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'name', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; @@ -271,7 +271,7 @@ class Item_kit extends Model // get_found_rows case if($count_only) { - $builder->select('COUNT(item_kits.item_kit_id) as count'); + $builder->select('COUNT(item_kit_id) as count'); } $builder->like('name', $search); diff --git a/app/Models/Receiving.php b/app/Models/Receiving.php index af8618673..e626186d7 100644 --- a/app/Models/Receiving.php +++ b/app/Models/Receiving.php @@ -88,7 +88,7 @@ class Receiving extends Model /** * @throws ReflectionException */ - public function save_value(array $items, int $supplier_id, int $employee_id, string $comment, string $reference, string $payment_type, bool $receiving_id = FALSE): int //TODO: $receiving_id gets overwritten before it's evaluated. It doesn't make sense to pass this here. + public function save_value(array $items, int $supplier_id, int $employee_id, string $comment, string $reference, string $payment_type, int $receiving_id = NEW_ENTRY): int //TODO: $receiving_id gets overwritten before it's evaluated. It doesn't make sense to pass this here. { $attribute = model(Attribute::class); $inventory = model('Inventory'); diff --git a/app/Models/Sale.php b/app/Models/Sale.php index f1c240c28..8ced7d308 100644 --- a/app/Models/Sale.php +++ b/app/Models/Sale.php @@ -126,7 +126,7 @@ class Sale extends Model /** * Get the sales data for the takings (sales/manage) view */ - public function search(string $search, array $filters, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'sales.sale_time', ?string $order = 'desc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, array $filters, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'sales.sale_time', ?string $order = 'desc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php index 0eb560338..a88f3159d 100644 --- a/app/Models/Supplier.php +++ b/app/Models/Supplier.php @@ -110,7 +110,7 @@ class Supplier extends Person /** * Inserts or updates a suppliers */ - public function save_supplier(array &$person_data, array &$supplier_data, bool $supplier_id = FALSE): bool + public function save_supplier(array &$person_data, array &$supplier_data, int $supplier_id = NEW_ENTRY): bool { $success = FALSE; @@ -120,7 +120,7 @@ class Supplier extends Person if(parent::save_value($person_data,$supplier_id)) { $builder = $this->db->table('suppliers'); - if(!$supplier_id || !$this->exists($supplier_id)) + if($supplier_id == NEW_ENTRY || !$this->exists($supplier_id)) { $supplier_data['person_id'] = $person_data['person_id']; $success = $builder->insert($supplier_data); @@ -263,7 +263,7 @@ class Supplier extends Person /** * Perform a search on suppliers */ - public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'last_name', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'last_name', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; diff --git a/app/Models/Tax.php b/app/Models/Tax.php index 95d6e737c..012dd32ef 100644 --- a/app/Models/Tax.php +++ b/app/Models/Tax.php @@ -166,7 +166,7 @@ class Tax extends Model public function save_value(array &$tax_rate_data, int $tax_rate_id = NEW_ENTRY): bool { $builder = $this->db->table('tax_rates'); - if(!$this->exists($tax_rate_id)) + if($tax_rate_id == NEW_ENTRY || !$this->exists($tax_rate_id)) { if($builder->insert($tax_rate_data)) { @@ -220,7 +220,7 @@ class Tax extends Model /** * Performs a search on tax_rates */ - public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'tax_code_name', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'tax_code_name', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; diff --git a/app/Models/Tax_category.php b/app/Models/Tax_category.php index 61d2c6d32..f628b3d89 100644 --- a/app/Models/Tax_category.php +++ b/app/Models/Tax_category.php @@ -109,11 +109,11 @@ class Tax_category extends Model /** * Inserts or updates a row */ - public function save_value(array &$tax_category_data, bool $tax_category_id = FALSE): bool + public function save_value(array &$tax_category_data, int $tax_category_id = NEW_ENTRY): bool { $builder = $this->db->table('tax_categories'); - if(!$tax_category_id || !$this->exists($tax_category_id)) + if($tax_category_id == NEW_ENTRY || !$this->exists($tax_category_id)) { if($builder->insert($tax_category_data)) { @@ -208,7 +208,7 @@ class Tax_category extends Model /** * Perform a search for a set of rows */ - public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'tax_category', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'tax_category', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; diff --git a/app/Models/Tax_code.php b/app/Models/Tax_code.php index 03e748420..5a763086b 100644 --- a/app/Models/Tax_code.php +++ b/app/Models/Tax_code.php @@ -202,7 +202,7 @@ class Tax_code extends Model /** * Perform a search for a set of rows */ - public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'tax_code_name', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'tax_code_name', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0; diff --git a/app/Models/Tax_jurisdiction.php b/app/Models/Tax_jurisdiction.php index 5bc4cd8ba..94951b6fc 100644 --- a/app/Models/Tax_jurisdiction.php +++ b/app/Models/Tax_jurisdiction.php @@ -113,10 +113,10 @@ class Tax_jurisdiction extends Model /** * Inserts or updates a row */ - public function save_value(array &$jurisdiction_data, bool $jurisdiction_id = FALSE): bool + public function save_value(array &$jurisdiction_data, int $jurisdiction_id = NEW_ENTRY): bool { $builder = $this->db->table('tax_jurisdictions'); - if(!$jurisdiction_id || !$this->exists($jurisdiction_id)) + if($jurisdiction_id == NEW_ENTRY || !$this->exists($jurisdiction_id)) { if($builder->insert($jurisdiction_data)) //TODO: Replace this with simply a return of the result of insert()... see update() below. { @@ -213,7 +213,7 @@ class Tax_jurisdiction extends Model /** * Perform a search for a set of rows */ - public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'jurisdiction_name', ?string $order = 'asc', ?bool $count_only = FALSE): ResultInterface + public function search(string $search, ?int $rows = 0, ?int $limit_from = 0, ?string $sort = 'jurisdiction_name', ?string $order = 'asc', ?bool $count_only = FALSE) { // Set default values if($rows == null) $rows = 0;