From 8705b91bd357b3c935f3a543711e2477e302fa7b Mon Sep 17 00:00:00 2001 From: jekkos-t520 Date: Wed, 29 Oct 2014 07:02:52 +0100 Subject: [PATCH 1/2] Start refactoring the stock locations configuration screen --- application/controllers/config.php | 15 +++++--- application/models/stock_locations.php | 1 + application/views/config.php | 52 ++++++++++++++++++++++++-- database/database.sql | 2 +- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/application/controllers/config.php b/application/controllers/config.php index 8895bd595..fb063d986 100644 --- a/application/controllers/config.php +++ b/application/controllers/config.php @@ -10,12 +10,8 @@ class Config extends Secure_area function index() { $location_names = array(); - $locations = $this->Stock_locations->get_location_names(); - foreach($locations->result_array() as $array) - { - array_push($location_names, $array['location_name']); - } - $data['location_names'] = implode(',', $location_names); + $allowed_locations = $this->Stock_locations->get_allowed_locations(); + $data['allowed_locations'] = $allowed_locations; $this->load->view("config", $data); } @@ -59,6 +55,13 @@ class Config extends Secure_area array_push($stock_locations_trimmed, trim($location, ' ')); } $current_locations = $this->Stock_locations->concat_location_names()->location_names; + foreach($this->input->post() as $key => $value) + { + if (strstr($key, 'stock_location')) + { + // save or update + } + } if ($this->input->post('stock_locations') != $current_locations) { $this->load->library('sale_lib'); diff --git a/application/models/stock_locations.php b/application/models/stock_locations.php index 96349648d..66d418b7c 100644 --- a/application/models/stock_locations.php +++ b/application/models/stock_locations.php @@ -79,6 +79,7 @@ class Stock_locations extends CI_Model // check for insertion foreach ($stock_locations as $location_name) { + $index=0; if(!$this->exists($location_name)) { $this->db->trans_start(); diff --git a/application/views/config.php b/application/views/config.php index b441f5408..968b12681 100644 --- a/application/views/config.php +++ b/application/views/config.php @@ -258,15 +258,20 @@ echo form_open('config/save/',array('id'=>'config_form')); + + $location_name ) { ?>
-lang->line('config_stock_location').':', 'stock_location',array('class'=>'required wide')); ?> +lang->line('config_stock_location').' ' .++$i. ':', 'stock_location_'.$i ,array('class'=>'required wide')); ?>
'stock_location', - 'id'=>'stock_location', - 'value'=>$location_names)); ?> + 'name'=>'stock_location_'.$location_id, + 'id'=>'stock_location_'.$location_id, + 'value'=>$location_name)); ?>
+ +
+
lang->line('config_recv_invoice_format').':', 'recv_invoice_format',array('class'=>'wide')); ?> @@ -419,6 +424,45 @@ echo form_close(); //validation and submit handling $(document).ready(function() { + var location_count = ; + + var hide_show_remove = function() + { + if ($("input[name*='stock_location']").length > 1) + { + $(".remove_stock_location").show(); + } + else + { + $(".remove_stock_location").hide(); + } + }; + + hide_show_remove(); + + var add_stock_location = function() + { + var id = $(this).parent().find('input').attr('id'); + id = id.replace(/.*?_(\d+)$/g, "$1"); + var block = $(this).parent().clone(true); + var new_block = block.insertAfter($(this).parent()); + var new_block_id = 'stock_location_' + ++id; + $(new_block).find('label').html("lang->line('config_stock_location'); ?> " + ++location_count + ": ").attr('for', new_block_id); + $(new_block).find('input').attr('id', new_block_id).attr('name', new_block_id); + $('.add_stock_location', new_block).click(add_stock_location); + $('.remove_stock_location', new_block).click(remove_stock_location); + hide_show_remove(); + }; + + var remove_stock_location = function() + { + $(this).parent().remove(); + hide_show_remove(); + }; + + $('.add_stock_location').click(add_stock_location); + $('.remove_stock_location').click(remove_stock_location); + $('#config_form').validate({ submitHandler:function(form) { diff --git a/database/database.sql b/database/database.sql index 4501965f1..02deaece4 100644 --- a/database/database.sql +++ b/database/database.sql @@ -644,7 +644,7 @@ CREATE TABLE `ospos_stock_locations` ( `location_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL, `deleted` int(1) NOT NULL DEFAULT '0', PRIMARY KEY (`location_id`) -) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0; +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1; -- -- Dumping data for table `ospos_stock_locations` From 24a6c9c34ff0c77269386f4ff7396c3b9c002a38 Mon Sep 17 00:00:00 2001 From: jekkos-t520 Date: Tue, 27 Jan 2015 19:03:57 +0100 Subject: [PATCH 2/2] Config location editing works now --- application/controllers/config.php | 51 ++++--- application/models/stock_locations.php | 136 +++++++++--------- application/views/config.php | 51 ++++--- application/views/partial/stock_locations.php | 20 +++ 4 files changed, 144 insertions(+), 114 deletions(-) create mode 100644 application/views/partial/stock_locations.php diff --git a/application/controllers/config.php b/application/controllers/config.php index fb063d986..b0c0d28dd 100644 --- a/application/controllers/config.php +++ b/application/controllers/config.php @@ -10,9 +10,8 @@ class Config extends Secure_area function index() { $location_names = array(); - $allowed_locations = $this->Stock_locations->get_allowed_locations(); - $data['allowed_locations'] = $allowed_locations; - $this->load->view("config", $data); + $stock_locations = $this->Stock_locations->get_all()->result_array(); + $this->load->view("config", array('stock_locations' => $stock_locations)); } function save() @@ -48,35 +47,49 @@ class Config extends Secure_area 'custom10_name'=>$this->input->post('custom10_name')/**GARRISON ADDED 4/20/2013**/ ); - $stock_locations = explode( ',', $this->input->post('stock_location')); - $stock_locations_trimmed=array(); - foreach($stock_locations as $location) - { - array_push($stock_locations_trimmed, trim($location, ' ')); - } - $current_locations = $this->Stock_locations->concat_location_names()->location_names; + $deleted_locations = $this->Stock_locations->get_allowed_locations(); foreach($this->input->post() as $key => $value) { if (strstr($key, 'stock_location')) { + $location_id = preg_replace("/.*?_(\d+)$/", "$1", $key); + unset($deleted_locations[$location_id]); // save or update + $location_data = array('location_name' => $value); + if ($this->Stock_locations->save($location_data, $location_id)) + { + $this->_clear_session_state(); + } } } - if ($this->input->post('stock_locations') != $current_locations) + // all locations not available in post will be deleted now + foreach ($deleted_locations as $location_id => $location_name) { - $this->load->library('sale_lib'); - $this->sale_lib->clear_sale_location(); - $this->sale_lib->clear_all(); - $this->load->library('receiving_lib'); - $this->receiving_lib->clear_stock_source(); - $this->receiving_lib->clear_stock_destination(); - $this->receiving_lib->clear_all(); + $this->Stock_locations->delete($location_id); } - if( $this->Appconfig->batch_save( $batch_save_data ) && $this->Stock_locations->array_save($stock_locations_trimmed)) + if( $this->Appconfig->batch_save( $batch_save_data )) { echo json_encode(array('success'=>true,'message'=>$this->lang->line('config_saved_successfully'))); } + $this->_remove_duplicate_cookies(); + } + + function stock_locations() + { + $stock_locations = $this->Stock_locations->get_all()->result_array(); + $this->load->view('partial/stock_locations', array('stock_locations' => $stock_locations)); + } + + function _clear_session_state() + { + $this->load->library('sale_lib'); + $this->sale_lib->clear_sale_location(); + $this->sale_lib->clear_all(); + $this->load->library('receiving_lib'); + $this->receiving_lib->clear_stock_source(); + $this->receiving_lib->clear_stock_destination(); + $this->receiving_lib->clear_all(); } } ?> \ No newline at end of file diff --git a/application/models/stock_locations.php b/application/models/stock_locations.php index 66d418b7c..95a069cf3 100644 --- a/application/models/stock_locations.php +++ b/application/models/stock_locations.php @@ -1,13 +1,13 @@ db->from('stock_locations'); - $this->db->where('location_name',$location_name); + $this->db->where('location_id',$location_id); $query = $this->db->get(); - return ($query->num_rows()==1); + return ($query->num_rows()>=1); } function get_all($limit=10000, $offset=0) @@ -18,22 +18,6 @@ class Stock_locations extends CI_Model return $this->db->get(); } - function get_location_names() - { - $this->db->select('location_name'); - $this->db->from('stock_locations'); - $this->db->where('deleted', 0); - return $this->db->get(); - } - - function concat_location_names() - { - $this->db->select('GROUP_CONCAT(location_name SEPARATOR\',\') AS location_names', FALSE); - $this->db->from('stock_locations'); - $this->db->where('deleted', 0); - return $this->db->get()->row(); - } - function get_undeleted_all() { $this->db->from('stock_locations'); @@ -73,60 +57,68 @@ class Stock_locations extends CI_Model return $this->db->get()->row()->location_name; } - function array_save($stock_locations) + function save(&$location_data,$location_id) { - $allowed_locations = $this->get_allowed_locations(); - // check for insertion - foreach ($stock_locations as $location_name) - { - $index=0; - if(!$this->exists($location_name)) - { - $this->db->trans_start(); - $location_data = array('location_name'=>$location_name,'deleted'=>0); - $this->db->insert('stock_locations',$location_data); - $location_id = $this->db->insert_id(); - - // insert new permission for stock location - $permission_id = 'items_'.$location_name; - $permission_data = array('permission_id'=>$permission_id,'module_id'=>'items','location_id' => $location_id); - $this->db->insert('permissions', $permission_data); - - // insert grants for new permission - $employees = $this->Employee->get_all(); - foreach ($employees->result_array() as $employee) - { - $grants_data = array('permission_id' => $permission_id, 'person_id' => $employee['person_id']); - $this->db->insert('grants', $grants_data); - } - - // insert quantities for existing items - $items = $this->Item->get_all(); - foreach ($items->result_array() as $item) - { - $quantity_data = array('item_id' => $item['item_id'], 'location_id' => $location_id, 'quantity' => 0); - $this->db->insert('item_quantities', $quantity_data); - } - $this->db->trans_complete(); - } - else if (!in_array($location_name, array_values($allowed_locations))) - { - $this->db->where('location_name', $location_name); - $this->db->update('stock_locations', array('deleted' => 0)); - } - } - - // check for deletion - foreach ($allowed_locations as $location_id => $location_name) - { - if (!in_array($location_name, $stock_locations)) - { - $this->db->where('location_id', $location_id); - $this->db->update('stock_locations', array('deleted' => 1)); - } - } - - return true; + if (!$this->exists($location_id)) + { + if($this->db->insert('stock_locations',$location_data)) + { + $location_name = $location_data['location_name']; + $this->db->trans_start(); + $location_data = array('location_name'=>$location_name,'deleted'=>0); + $this->db->insert('stock_locations',$location_data); + $location_id = $this->db->insert_id(); + + $this->_insert_new_permission('items', $location_id, $location_name); + $this->_insert_new_permission('sales', $location_id, $location_name); + $this->_insert_new_permission('receivings', $location_id, $location_name); + + + // insert quantities for existing items + $items = $this->Item->get_all(); + foreach ($items->result_array() as $item) + { + $quantity_data = array('item_id' => $item['item_id'], 'location_id' => $location_id, 'quantity' => 0); + $this->db->insert('item_quantities', $quantity_data); + } + $this->db->trans_complete(); + return TRUE; + } + return FALSE; + } + else + { + $this->db->where('location_id', $location_id); + return $this->db->update('stock_locations',$location_data); + } } + + function _insert_new_permission($module, $location_id, $location_name) + { + // insert new permission for stock location + $permission_id = $module."_".$location_name; + $permission_data = array('permission_id'=>$permission_id,'module_id'=>$module,'location_id' => $location_id); + $this->db->insert('permissions', $permission_data); + + // insert grants for new permission + $employees = $this->Employee->get_all(); + foreach ($employees->result_array() as $employee) + { + $grants_data = array('permission_id' => $permission_id, 'person_id' => $employee['person_id']); + $this->db->insert('grants', $grants_data); + } + + } + + /* + Deletes one item + */ + function delete($location_id) + { + $this->db->where('location_id', $location_id); + return $this->db->update('stock_locations', array('deleted' => 1)); + } + + } ?> diff --git a/application/views/config.php b/application/views/config.php index 968b12681..3568bb75a 100644 --- a/application/views/config.php +++ b/application/views/config.php @@ -258,20 +258,7 @@ echo form_open('config/save/',array('id'=>'config_form'));
- - $location_name ) { ?> -
-lang->line('config_stock_location').' ' .++$i. ':', 'stock_location_'.$i ,array('class'=>'required wide')); ?> -
- 'stock_location_'.$location_id, - 'id'=>'stock_location_'.$location_id, - 'value'=>$location_name)); ?> -
- - -
- +load->view('partial/stock_locations', array('stock_locations' => $stock_locations)); ?>
lang->line('config_recv_invoice_format').':', 'recv_invoice_format',array('class'=>'wide')); ?> @@ -424,7 +411,7 @@ echo form_close(); //validation and submit handling $(document).ready(function() { - var location_count = ; + var location_count = ; var hide_show_remove = function() { @@ -448,7 +435,7 @@ $(document).ready(function() var new_block = block.insertAfter($(this).parent()); var new_block_id = 'stock_location_' + ++id; $(new_block).find('label').html("lang->line('config_stock_location'); ?> " + ++location_count + ": ").attr('for', new_block_id); - $(new_block).find('input').attr('id', new_block_id).attr('name', new_block_id); + $(new_block).find('input').attr('id', new_block_id).attr('name', new_block_id).attr('class', new_block_id).val(''); $('.add_stock_location', new_block).click(add_stock_location); $('.remove_stock_location', new_block).click(remove_stock_location); hide_show_remove(); @@ -459,9 +446,27 @@ $(document).ready(function() $(this).parent().remove(); hide_show_remove(); }; + + var init_add_remove_locations = function() + { + $('.add_stock_location').click(add_stock_location); + $('.remove_stock_location').click(remove_stock_location); + }; + init_add_remove_locations(); - $('.add_stock_location').click(add_stock_location); - $('.remove_stock_location').click(remove_stock_location); + // run validator once for all fields + var validator_name = $("input[name*='stock_location']:first").attr('class'); + $.validator.addMethod(validator_name , function(value, element) + { + var result = true; + var locations = {}; + $("input[name*='stock_location']").each(function() { + var content = $(this).val(); + result &= content && !locations[content]; + locations[content] = content; + }); + return result; + }, "lang->line('config_stock_location_required'); ?>"); $('#config_form').validate({ submitHandler:function(form) @@ -477,6 +482,7 @@ $(document).ready(function() { set_feedback(response.message,'error_message',true); } + $("#stock_locations").load('', init_add_remove_locations); }, dataType:'json' }); @@ -496,8 +502,9 @@ $(document).ready(function() }, email:"email", return_policy: "required", - stock_location:"required" - + stock_location: { + stock_location: true + } }, messages: { @@ -510,9 +517,7 @@ $(document).ready(function() number:"lang->line('config_default_tax_rate_number'); ?>" }, email: "lang->line('common_email_invalid_format'); ?>", - return_policy:"lang->line('config_return_policy_required'); ?>", - stock_location:"lang->line('config_stock_location_required'); ?>" - + return_policy:"lang->line('config_return_policy_required'); ?>" } }); }); diff --git a/application/views/partial/stock_locations.php b/application/views/partial/stock_locations.php new file mode 100644 index 000000000..8706c9ad8 --- /dev/null +++ b/application/views/partial/stock_locations.php @@ -0,0 +1,20 @@ +
+ + $location_data ) { ?> + + + + + +
visibility \ No newline at end of file