diff --git a/application/controllers/config.php b/application/controllers/config.php index 2de52dc52..e0359ffa3 100644 --- a/application/controllers/config.php +++ b/application/controllers/config.php @@ -10,13 +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); - $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() @@ -60,24 +55,40 @@ 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) + $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(); + } + } + } + // all locations not available in post will be deleted now + foreach ($deleted_locations as $location_id => $location_name) { - array_push($stock_locations_trimmed, trim($location, ' ')); - } - $current_locations = $this->Stock_locations->concat_location_names()->location_names; - if ($this->input->post('stock_locations') != $current_locations) - { - $this->_clear_session_state(); + $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'); diff --git a/application/models/stock_locations.php b/application/models/stock_locations.php index 5fb4be464..d8021b22f 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,14 +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(); - } - /* * returns all location-ids in a simple array like array (location_id, location_id, ...) * used in items-controller::do_excel_import @@ -45,14 +37,6 @@ class Stock_locations extends CI_Model return $ids_array; } - 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'); @@ -92,61 +76,70 @@ 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) - { - 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)); + 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); $this->db->delete('permissions', array('permission_id' => 'items_'.$location_name)); - } - } - - return true; + } + } + + /* + Deletes one item + */ + function delete($location_id) + { + $this->db->where('location_id', $location_id); + return $this->db->update('stock_locations', array('deleted' => 1)); + } + + } ?> \ No newline at end of file diff --git a/application/views/config.php b/application/views/config.php index c23db69f0..5484a689b 100644 --- a/application/views/config.php +++ b/application/views/config.php @@ -259,15 +259,7 @@ echo form_open('config/save/',array('id'=>'config_form')); -
-lang->line('config_stock_location').':', 'stock_location',array('class'=>'required wide')); ?> -
- 'stock_location', - 'id'=>'stock_location', - 'value'=>$location_names)); ?> -
-
+load->view('partial/stock_locations', array('stock_locations' => $stock_locations)); ?>
lang->line('config_sales_invoice_format').':', 'sales_invoice_format',array('class'=>'wide')); ?> @@ -467,6 +459,63 @@ 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).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(); + }; + + var remove_stock_location = 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(); + + // 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) { @@ -481,6 +530,7 @@ $(document).ready(function() { set_feedback(response.message,'error_message',true); } + $("#stock_locations").load('', init_add_remove_locations); }, dataType:'json' }); @@ -500,8 +550,9 @@ $(document).ready(function() }, email:"email", return_policy: "required", - stock_location:"required" - + stock_location: { + stock_location: true + } }, messages: { @@ -514,9 +565,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..7217edf1d --- /dev/null +++ b/application/views/partial/stock_locations.php @@ -0,0 +1,19 @@ +
+ + $location_data ) { ?> + + + + +
\ No newline at end of file diff --git a/database/database.sql b/database/database.sql index 369421034..4783eaab4 100644 --- a/database/database.sql +++ b/database/database.sql @@ -656,7 +656,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`