From f021215cbb04b1668c4fce621d7b323ee31aa0ba Mon Sep 17 00:00:00 2001 From: jekkos-t520 Date: Wed, 11 Feb 2015 11:32:58 +0100 Subject: [PATCH] Show proper error message on upload misconfiguration Use jquery-validation to do item_number check Fix invoice_number validation in sale edit form --- application/controllers/items.php | 23 ++++----------------- application/language/en/items_lang.php | 2 +- application/language/es/items_lang.php | 2 +- application/language/fr/items_lang.php | 2 +- application/language/id/items_lang.php | 2 +- application/language/nl-BE/items_lang.php | 2 +- application/language/ru/items_lang.php | 2 +- application/language/th/items_lang.php | 2 +- application/language/tr/items_lang.php | 2 +- application/language/zh/items_lang.php | 2 +- application/models/item.php | 9 ++++++++ application/views/items/form.php | 25 ++++++++++++++++++----- application/views/partial/header.php | 2 +- application/views/sales/form.php | 6 ++---- js/common.js | 20 +----------------- translations/items_lang.csv | 2 +- 16 files changed, 47 insertions(+), 58 deletions(-) diff --git a/application/controllers/items.php b/application/controllers/items.php index 8acf33b9b..04a0a344e 100644 --- a/application/controllers/items.php +++ b/application/controllers/items.php @@ -366,8 +366,7 @@ class Items extends Secure_area implements iData_controller $employee_id=$this->Employee->get_logged_in_employee_info()->person_id; $cur_item_info = $this->Item->get_info($item_id); - $validated = $this->validate_item() || $item_id != -1; - if($validated && $this->Item->save($item_data,$item_id)) + if($this->Item->save($item_data,$item_id)) { $success = TRUE; $new_item = FALSE; @@ -428,37 +427,23 @@ class Items extends Secure_area implements iData_controller $this->lang->line('items_error_adding_updating') .' '. $item_data['name'] : $this->upload->display_errors(); echo json_encode(array('success'=>false, - 'error_messages'=>array($this->upload->display_errors()), 'message'=>$error_message,'item_id'=>$item_id)); } } else//failure { - $error_messages = $this->form_validation->get_error_messages(); echo json_encode(array('success'=>false, - 'error_messages'=>$error_messages, 'message'=>$this->lang->line('items_error_adding_updating').' ' .$item_data['name'],'item_id'=>-1)); } } - function validate_item() + function check_item_number() { - $this->load->library('form_validation'); - $this->form_validation->set_rules('item_number', 'lang:items_item_number', 'callback_item_number_check'); - return $this->form_validation->run(); - } - - function item_number_check($item_number) - { - if ($this->Item->get_item_id($item_number) != FALSE) - { - $this->form_validation->set_message('item_number_check', $this->lang->line('items_item_number_exists')); - return FALSE; - } - return TRUE; + $exists = $this->Item->item_number_exists($this->input->post('item_number'),$this->input->post('item_id')); + echo json_encode(array('success'=>!$exists,'message'=>$this->lang->line('items_item_number_duplicate'))); } function _handle_image_upload() diff --git a/application/language/en/items_lang.php b/application/language/en/items_lang.php index 47bd0826e..e9982c3b3 100644 --- a/application/language/en/items_lang.php +++ b/application/language/en/items_lang.php @@ -81,4 +81,4 @@ $lang["items_upc_database"] = "UPC Database"; $lang["items_update"] = "Update Item"; $lang["items_use_inventory_menu"] = "Use Inv. Menu"; $lang["items_image"] = "Avatar"; -$lang["items_item_number_exists"] = "The item number is already present in the database"; +$lang["items_item_number_duplicate"] = "The item number is already present in the database"; diff --git a/application/language/es/items_lang.php b/application/language/es/items_lang.php index da720d1b8..9d62a7d2f 100644 --- a/application/language/es/items_lang.php +++ b/application/language/es/items_lang.php @@ -81,4 +81,4 @@ $lang["items_upc_database"] = "Base de datos UPC"; $lang["items_update"] = "Actualizar Artículo"; $lang["items_use_inventory_menu"] = "Usar Menú de Inventario"; $lang["items_image"] = "Avatar"; -$lang["items_item_number_exists"] = "The item number is already present in the database"; +$lang["items_item_number_duplicate"] = "The item number is already present in the database"; diff --git a/application/language/fr/items_lang.php b/application/language/fr/items_lang.php index 7116149fd..1112c363d 100644 --- a/application/language/fr/items_lang.php +++ b/application/language/fr/items_lang.php @@ -81,4 +81,4 @@ $lang["items_upc_database"] = "Base de Données UPC"; $lang["items_update"] = "Éditer Item"; $lang["items_use_inventory_menu"] = "Utiliser Menu Inv."; $lang["items_image"] = "Avatar"; -$lang["items_item_number_exists"] = "The item number is already present in the database"; +$lang["items_item_number_duplicate"] = "The item number is already present in the database"; diff --git a/application/language/id/items_lang.php b/application/language/id/items_lang.php index 828978df4..01282bdc0 100644 --- a/application/language/id/items_lang.php +++ b/application/language/id/items_lang.php @@ -81,4 +81,4 @@ $lang["items_upc_database"] = "Database UPC"; $lang["items_update"] = "Ubah"; $lang["items_use_inventory_menu"] = "Gunakan Inv. Menu"; $lang["items_image"] = "Avatar"; -$lang["items_item_number_exists"] = "The item number is already present in the database"; +$lang["items_item_number_duplicate"] = "The item number is already present in the database"; diff --git a/application/language/nl-BE/items_lang.php b/application/language/nl-BE/items_lang.php index d3efc59bd..010d8519d 100755 --- a/application/language/nl-BE/items_lang.php +++ b/application/language/nl-BE/items_lang.php @@ -81,4 +81,4 @@ $lang["items_upc_database"] = "UPC Database"; $lang["items_update"] = "Bewerk Product"; $lang["items_use_inventory_menu"] = "Use Inv. Menu"; $lang["items_image"] = "Afbeelding"; -$lang["items_item_number_exists"] = "De barcode nummer is reeds aanwezig in de database"; +$lang["items_item_number_duplicate"] = "De barcode nummer is reeds aanwezig in de database"; diff --git a/application/language/ru/items_lang.php b/application/language/ru/items_lang.php index d12a45142..e242c32fa 100644 --- a/application/language/ru/items_lang.php +++ b/application/language/ru/items_lang.php @@ -81,4 +81,4 @@ $lang["items_upc_database"] = "UPC база данных"; $lang["items_update"] = "Обновить Товар"; $lang["items_use_inventory_menu"] = "Используйте меню инвентаря"; $lang["items_image"] = "Avatar"; -$lang["items_item_number_exists"] = "The item number is already present in the database"; +$lang["items_item_number_duplicate"] = "The item number is already present in the database"; diff --git a/application/language/th/items_lang.php b/application/language/th/items_lang.php index 6fc53a773..e9588197f 100644 --- a/application/language/th/items_lang.php +++ b/application/language/th/items_lang.php @@ -81,4 +81,4 @@ $lang["items_upc_database"] = "UPC ฐานข้อมูล"; $lang["items_update"] = "ปรับแต่งสินค้า"; $lang["items_use_inventory_menu"] = "ใช้เมนูสินค้าคงเหลือ"; $lang["items_image"] = "Avatar"; -$lang["items_item_number_exists"] = "The item number is already present in the database"; +$lang["items_item_number_duplicate"] = "The item number is already present in the database"; diff --git a/application/language/tr/items_lang.php b/application/language/tr/items_lang.php index 0ee376cd5..e5b7d16c3 100644 --- a/application/language/tr/items_lang.php +++ b/application/language/tr/items_lang.php @@ -81,4 +81,4 @@ $lang["items_upc_database"] = "UPC Veritabanı"; $lang["items_update"] = "Ürün Güncelle"; $lang["items_use_inventory_menu"] = "Stok Menüsünü Kullan"; $lang["items_image"] = "Avatar"; -$lang["items_item_number_exists"] = "The item number is already present in the database"; +$lang["items_item_number_duplicate"] = "The item number is already present in the database"; diff --git a/application/language/zh/items_lang.php b/application/language/zh/items_lang.php index c2f2d55d2..442b0e193 100755 --- a/application/language/zh/items_lang.php +++ b/application/language/zh/items_lang.php @@ -81,4 +81,4 @@ $lang["items_upc_database"] = "UPC Database"; $lang["items_update"] = "更新產品"; $lang["items_use_inventory_menu"] = "使用庫存清單"; $lang["items_image"] = "Avatar"; -$lang["items_item_number_exists"] = "The item number is already present in the database"; +$lang["items_item_number_duplicate"] = "The item number is already present in the database"; diff --git a/application/models/item.php b/application/models/item.php index ea3553db0..13241a964 100644 --- a/application/models/item.php +++ b/application/models/item.php @@ -12,6 +12,15 @@ class Item extends CI_Model return ($query->num_rows()==1); } + + function item_number_exists($item_number, $item_id) + { + $this->db->from('items'); + $this->db->where('item_number', $item_number); + $this->db->where('item_id !=', $item_id); + $query=$this->db->get(); + return ($query->num_rows()==1); + } /* Returns all the items diff --git a/application/views/items/form.php b/application/views/items/form.php index b66c84eaa..f981185ea 100644 --- a/application/views/items/form.php +++ b/application/views/items/form.php @@ -11,6 +11,7 @@ echo form_open('items/save/'.$item_info->item_id,array('id'=>'item_form', 'encty
'item_number', + 'class'=>'item_number', 'id'=>'item_number', 'value'=>$item_info->item_number) );?> @@ -398,6 +399,22 @@ $(document).ready(function() } ?> + $.validator.addMethod("item_number", function(value, element) + { + return JSON.parse($.ajax( + { + type: 'POST', + url: '', + data: {'item_id' : 'item_id; ?>', 'item_number' : $(element).val() }, + success: function(response) + { + success=response.success; + }, + async:false, + dataType: 'json' + }).responseText).success; + + }, 'lang->line("items_item_number_duplicate"); ?>'); $('#item_form').validate({ submitHandler:function(form) @@ -405,11 +422,8 @@ $(document).ready(function() $(form).ajaxSubmit({ success:function(response) { - if (handle_validation(response)) - { - tb_remove(); - post_item_form_submit(response); - } + tb_remove(); + post_item_form_submit(response); }, dataType:'json' }); @@ -421,6 +435,7 @@ $(document).ready(function() { name:"required", category:"required", + item_number: { item_number: true }, cost_price: { required:true, diff --git a/application/views/partial/header.php b/application/views/partial/header.php index 32a37006c..6c33b1e3f 100644 --- a/application/views/partial/header.php +++ b/application/views/partial/header.php @@ -63,7 +63,7 @@ html { ?> ', - data: {'sale_id' : id, 'invoice_number' : $(element).val() }, + data: {'sale_id' : , 'invoice_number' : $(element).val() }, success: function(response) { success=response.success; }, async:false, dataType: 'json' - }).response).success; + }).responseText).success; }, 'lang->line("sales_invoice_number_duplicate"); ?>'); $('#date').datePicker({startDate: '01/01/1970'}); diff --git a/js/common.js b/js/common.js index e4b8dfb94..6188eb91d 100644 --- a/js/common.js +++ b/js/common.js @@ -23,7 +23,7 @@ function set_feedback(text, classname, keep_displayed) { if(text) { - $('#feedback_bar').removeClass().addClass(classname).text(text).css('opacity','1'); + $('#feedback_bar').removeClass().addClass(classname).html(text).css('opacity','1'); if(!keep_displayed) { @@ -47,21 +47,3 @@ $.each(['customers', 'items', 'reports', 'receivings', 'sales', 'employees', 'co window.location = BASE_URL + '/' + value + ' /index'; }); }); - -function handle_validation(response) -{ - if (!response.success) - { - var error_message_box = '.error_message_box'; - // server side validation failed.. record won't be saved - $(error_message_box).empty(); - for(var index in response.error_messages) - { - // get validation messages from array and show those to the user - var message = response.error_messages[index]; - $(error_message_box).append("
  • " + message + "
  • ").css("display", ""); - } - return false; - } - return true; -} diff --git a/translations/items_lang.csv b/translations/items_lang.csv index 298b99a6b..058b448cf 100644 --- a/translations/items_lang.csv +++ b/translations/items_lang.csv @@ -80,4 +80,4 @@ items_upc_database,UPC Database,Base de datos UPC,UPC Database,Base de Données items_update,Bewerk Product,Actualizar Artículo,Update Item,Éditer Item,更新產品,Обновить Товар,ปรับแต่งสินค้า,Ürün Güncelle,Ubah items_use_inventory_menu,Use Inv. Menu,Usar Menú de Inventario,Use Inv. Menu,Utiliser Menu Inv.,使用庫存清單,Используйте меню инвентаря,ใช้เมนูสินค้าคงเหลือ,Stok Menüsünü Kullan,Gunakan Inv. Menu items_image,Afbeelding,Avatar,Avatar,Avatar,Avatar,Avatar,Avatar,Avatar,Avatar -items_item_number_exists,De barcode nummer is reeds aanwezig in de database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database +items_item_number_duplicate,De barcode nummer is reeds aanwezig in de database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database,The item number is already present in the database