diff --git a/app/Controllers/Items.php b/app/Controllers/Items.php index ef79a7269..6b0079311 100644 --- a/app/Controllers/Items.php +++ b/app/Controllers/Items.php @@ -378,7 +378,7 @@ class Items extends Secure_Controller $data['tax_category'] = ''; } - $data['logo_exists'] = $item_info->pic_filename !== ''; + $data['logo_exists'] = $item_info->pic_filename !== null; $file_extension = pathinfo($item_info->pic_filename, PATHINFO_EXTENSION); if(empty($file_extension)) diff --git a/app/Models/Customer.php b/app/Models/Customer.php index 8ec990181..0a60a94c8 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -102,28 +102,40 @@ class Customer extends Person } else { - //Get empty base parent object, as $customer_id is NOT a customer - $person_obj = parent::get_info(NEW_ENTRY); - - // Initialize empty object - - foreach ($this->db->getFieldData('customers') as $field) { - $field_name = $field->name; - if (in_array($field->type, array('int', 'tinyint', 'decimal'))) - { - $person_obj->$field_name = 0; - } - else - { - $person_obj->$field_name = NULL; - } - } - $person_obj->person_id = NEW_ENTRY; - - return $person_obj; + return $this->getEmptyObject('customers'); } } + /** + * 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 = parent::get_info(NEW_ENTRY); + + // 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'))) + { + $empty_obj->$field_name = ($field->primary_key == 1) ? NEW_ENTRY : 0; + } + else + { + $empty_obj->$field_name = NULL; + } + } + + return $empty_obj; + } + + /** * Gets stats about a particular customer */ diff --git a/app/Models/Item.php b/app/Models/Item.php index 914dc3e77..502934651 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -42,6 +42,7 @@ class Item extends Model 'hsn_code' ]; + /** * Determines if a given item_id is an item */ @@ -325,25 +326,36 @@ class Item extends Model return $query->getRow(); } - //Get empty base parent object, as $item_id is NOT an item - $item_obj = new stdClass(); + return $this->getEmptyObject('items'); + } - // Initialize empty object + /** + * 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) { - foreach ($this->db->getFieldData('items') as $field) { $field_name = $field->name; - if (in_array($field->type, array('int', 'tinyint', 'decimal'))) + + if(in_array($field->type, array('int', 'tinyint', 'decimal'))) { - $item_obj->$field_name = 0; + $empty_obj->$field_name = ($field->primary_key == 1) ? NEW_ENTRY : 0; } else { - $item_obj->$field_name = NULL; + $empty_obj->$field_name = NULL; } } - $item_obj->item_id = NEW_ENTRY; - return $item_obj; + return $empty_obj; } /** diff --git a/app/Models/Person.php b/app/Models/Person.php index 9f2818cba..d1998c85d 100644 --- a/app/Models/Person.php +++ b/app/Models/Person.php @@ -92,24 +92,40 @@ class Person extends Model } else { - $person_obj = new stdClass(); - foreach ($this->db->getFieldData('people') as $field) { - $field_name = $field->name; - if (in_array($field->type, array('int', 'tinyint', 'decimal'))) - { - $person_obj->$field_name = 0; - } - else - { - $person_obj->$field_name = NULL; - } - } - $person_obj->person_id = NEW_ENTRY; - - return $person_obj; + return $this->getEmptyObject('people'); } } + + /** + * 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'))) + { + $empty_obj->$field_name = ($field->primary_key == 1) ? NEW_ENTRY : 0; + } + else + { + $empty_obj->$field_name = NULL; + } + } + + return $empty_obj; + } + /** * Gets information about people as an array of rows *