diff --git a/app/Controllers/Customers.php b/app/Controllers/Customers.php index 1ef092716..85151de34 100644 --- a/app/Controllers/Customers.php +++ b/app/Controllers/Customers.php @@ -129,7 +129,7 @@ class Customers extends Persons /** * Gives search suggestions based on what is being searched for */ - public function suggest(): void + public function getSuggest(): void { $suggestions = $this->customer->get_search_suggestions($this->request->getVar('term', FILTER_SANITIZE_STRING), 25,TRUE); diff --git a/app/Controllers/Giftcards.php b/app/Controllers/Giftcards.php index d0d40fdb9..b64c63814 100644 --- a/app/Controllers/Giftcards.php +++ b/app/Controllers/Giftcards.php @@ -50,7 +50,7 @@ class Giftcards extends Secure_Controller Gives search suggestions based on what is being searched for */ - public function suggest(): void + public function getSuggest(): void { $suggestions = $this->giftcard->get_search_suggestions($this->request->getVar('term', FILTER_SANITIZE_STRING), TRUE); @@ -84,7 +84,8 @@ class Giftcards extends Secure_Controller } else { - $max_giftnumber = isset($this->giftcard->get_max_number()->giftcard_number) ? $this->Giftcard->get_max_number()->giftcard_number : 0; //TODO: variable does not follow naming standard. + $max_number_obj = $this->giftcard->get_max_number(); + $max_giftnumber = isset($max_number_obj) ? $this->giftcard->get_max_number()->giftcard_number : 0; //TODO: variable does not follow naming standard. $data['giftcard_number'] = $giftcard_id > 0 ? $giftcard_info->giftcard_number : $max_giftnumber + 1; } $data['giftcard_id'] = $giftcard_id; @@ -144,7 +145,7 @@ class Giftcards extends Secure_Controller * * @return void */ - public function ajax_check_number_giftcard(): void + public function postCheckNumberGiftcard(): void { $parsed_value = parse_decimals($this->request->getPost('giftcard_amount', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); echo json_encode (['success' => $parsed_value !== FALSE, 'giftcard_amount' => to_currency_no_money($parsed_value)]); diff --git a/app/Models/Giftcard.php b/app/Models/Giftcard.php index bb4ed3556..6400d4ca0 100644 --- a/app/Models/Giftcard.php +++ b/app/Models/Giftcard.php @@ -36,13 +36,13 @@ class Giftcard extends Model } /** - * Gets max gift card number //TODO: This isn't entirely accurate. It returns the object and the results then pulls the giftcard_number. + * Gets max gift card number //TODO: This isn't entirely accurate. It returns the object and the results then pulls the giftcard_number */ - public function get_max_number(): object + public function get_max_number(): ?object { $builder = $this->db->table('giftcards'); $builder->select('CAST(giftcard_number AS UNSIGNED) AS giftcard_number'); - $builder->where('giftcard_number REGEXP', "'^[0-9]+$'", FALSE); + $builder->where('giftcard_number REGEXP \'^[0-9]+$\' = 0'); $builder->orderBy("giftcard_number","desc"); $builder->limit(1); @@ -78,19 +78,39 @@ class Giftcard extends Model } 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, as $giftcard_id is NOT a giftcard - $giftcard_obj = new stdClass(); - - //Get all the fields from giftcards table - foreach($this->db->getFieldNames('giftcards') as $field) - { - $giftcard_obj->$field = ''; - } - - return $giftcard_obj; + return $this->getEmptyObject('giftcards'); } } + /** + * 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 a giftcard id given a giftcard number */ @@ -254,7 +274,7 @@ class Giftcard extends Model // get_found_rows case if($count_only) //TODO: replace this with `if($count_only)` { - $builder->select('COUNT(giftcards.giftcard_id) as count'); + $builder->select('COUNT(giftcard_id) as count'); } $builder->join('people AS person', 'giftcards.person_id = person.person_id', 'left'); @@ -268,7 +288,7 @@ class Giftcard extends Model $builder->where('giftcards.deleted', 0); // get_found_rows case - if($count_only) //TODO: replace this with `if($count_only)` + if($count_only) { return $builder->get()->getRow()->count; } diff --git a/app/Views/giftcards/form.php b/app/Views/giftcards/form.php index fa5629d52..6a8322b87 100644 --- a/app/Views/giftcards/form.php +++ b/app/Views/giftcards/form.php @@ -129,7 +129,7 @@ $(document).ready(function() required: true, remote: { - url: "", + url: "", type: 'POST', data: { 'amount': $('#giftcard_amount').val()