Merge branch 'refactor/edit_stock_locations'

Conflicts:
	application/controllers/config.php
	application/models/stock_locations.php
This commit is contained in:
jekkos-t520
2015-01-27 19:24:59 +01:00
5 changed files with 175 additions and 103 deletions

View File

@@ -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');

View File

@@ -1,13 +1,13 @@
<?php
class Stock_locations extends CI_Model
{
function exists($location_name='')
function exists($location_id='')
{
$this->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));
}
}
?>

View File

@@ -259,15 +259,7 @@ echo form_open('config/save/',array('id'=>'config_form'));
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('config_stock_location').':', 'stock_location',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'stock_location',
'id'=>'stock_location',
'value'=>$location_names)); ?>
</div>
</div>
<?php $this->load->view('partial/stock_locations', array('stock_locations' => $stock_locations)); ?>
<div class="field_row clearfix">
<?php echo form_label($this->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 = <?php echo sizeof($stock_locations); ?>;
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("<?php echo $this->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;
}, "<?php echo $this->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('<?php echo site_url("config/stock_locations");?>', 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:"<?php echo $this->lang->line('config_default_tax_rate_number'); ?>"
},
email: "<?php echo $this->lang->line('common_email_invalid_format'); ?>",
return_policy:"<?php echo $this->lang->line('config_return_policy_required'); ?>",
stock_location:"<?php echo $this->lang->line('config_stock_location_required'); ?>"
return_policy:"<?php echo $this->lang->line('config_return_policy_required'); ?>"
}
});
});

View File

@@ -0,0 +1,19 @@
<div id="stock_locations">
<?php $i = 0; ?>
<?php foreach($stock_locations as $location => $location_data ) { ?>
<?php $location_id = $location_data['location_id']; ?>
<?php $location_name = $location_data['location_name']; ?>
<div class="field_row clearfix" style="<? echo $location_data['deleted'] ? ';display:none;' : 'display:block;' ?>">
<?php echo form_label($this->lang->line('config_stock_location').' ' .++$i. ':', 'stock_location_'.$i ,array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'stock_location_'.$location_id,
'id'=>'stock_location_'.$location_id,
'class'=>'stock_location_'.$location_id,
'value'=>$location_name)); ?>
</div>
<img class="add_stock_location" src="<?php echo base_url('images/plus.png'); ?>" />
<img class="remove_stock_location" src="<?php echo base_url('images/minus.png'); ?>" />
</div>
<?php } ?>
</div>

View File

@@ -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`