Fix Store Config Tax category, Reward, Locations and Tables add and remove of entries

This commit is contained in:
FrancescoUK
2017-07-13 22:43:05 +01:00
parent dc14cbd7ab
commit 47486b980f
9 changed files with 140 additions and 139 deletions

View File

@@ -533,16 +533,15 @@ class Config extends Secure_Controller
public function save_locations()
{
$deleted_locations = $this->Stock_location->get_undeleted_all()->result_array();
$this->db->trans_start();
$not_to_delete = array();
foreach($this->input->post() as $key => $value)
{
if(strstr($key, 'stock_location'))
{
$location_id = preg_replace("/.*?_(\d+)$/", "$1", $key);
unset($deleted_locations[$location_id]);
$not_to_delete[] = $location_id;
// save or update
$location_data = array('location_name' => $value);
if($this->Stock_location->save($location_data, $location_id))
@@ -553,9 +552,14 @@ class Config extends Secure_Controller
}
// all locations not available in post will be deleted now
foreach($deleted_locations as $location_id => $location_name)
$deleted_locations = $this->Stock_location->get_all()->result_array();
foreach($deleted_locations as $location => $location_data)
{
$this->Stock_location->delete($location_id);
if(!in_array($location_data['location_id'], $not_to_delete))
{
$this->Stock_location->delete($location_data['location_id']);
}
}
$this->db->trans_complete();
@@ -570,8 +574,6 @@ class Config extends Secure_Controller
public function save_tables()
{
$deleted_tables = $this->Dinner_table->get_undeleted_all()->result_array();
$this->db->trans_start();
$dinner_table_enable = $this->input->post('dinner_table_enable') != NULL;
@@ -580,12 +582,13 @@ class Config extends Secure_Controller
if($dinner_table_enable)
{
$not_to_delete = array();
foreach($this->input->post() as $key => $value)
{
if(strstr($key, 'dinner_table') && $key != 'dinner_table_enable')
{
$dinner_table_id = preg_replace("/.*?_(\d+)$/", "$1", $key);
unset($deleted_tables[$dinner_table_id]);
$not_to_delete[] = $dinner_table_id;
// save or update
$table_data = array('name' => $value);
if($this->Dinner_table->save($table_data, $dinner_table_id))
@@ -596,9 +599,14 @@ class Config extends Secure_Controller
}
// all tables not available in post will be deleted now
foreach($deleted_tables as $dinner_table_id => $dinner_table_name)
$deleted_tables = $this->Dinner_table->get_all()->result_array();
foreach($deleted_tables as $dinner_tables => $table)
{
$this->Dinner_table->delete($dinner_table_id);
if(!in_array($table['dinner_table_id'], $not_to_delete))
{
$this->Dinner_table->delete($table['dinner_table_id']);
}
}
}
@@ -614,8 +622,6 @@ class Config extends Secure_Controller
public function save_tax()
{
$deleted_categories = $this->Tax->get_all_tax_categories()->result_array();
$this->db->trans_start();
$customer_sales_tax_support = $this->input->post('customer_sales_tax_support') != NULL;
@@ -640,7 +646,6 @@ class Config extends Secure_Controller
if(strstr($key, 'tax_category'))
{
$tax_category_id = preg_replace("/.*?_(\d+)$/", "$1", $key);
unset($deleted_categories[$tax_category_id]);
$array_save[$tax_category_id]['tax_category'] = $value;
}
elseif(strstr($key, 'tax_group_sequence'))
@@ -650,6 +655,7 @@ class Config extends Secure_Controller
}
}
$not_to_delete = array();
if(!empty($array_save))
{
foreach($array_save as $key => $value)
@@ -657,13 +663,19 @@ class Config extends Secure_Controller
// save or update
$category_data = array('tax_category' => $value['tax_category'], 'tax_group_sequence' => $value['tax_group_sequence']);
$this->Tax->save_tax_category($category_data, $key);
$not_to_delete[] = $key;
}
}
// all categories not available in post will be deleted now
foreach($deleted_categories as $tax_category_id => $tax_category)
$deleted_categories = $this->Tax->get_all_tax_categories()->result_array();
foreach($deleted_categories as $tax_category => $category)
{
$this->Tax->delete_tax_category($tax_category_id);
if(!in_array($category['tax_category_id'], $not_to_delete))
{
$this->Tax->delete_tax_category($category['tax_category_id']);
}
}
}
@@ -679,8 +691,6 @@ class Config extends Secure_Controller
public function save_rewards()
{
$deleted_packages = $this->Customer_rewards->get_undeleted_all()->result_array();
$this->db->trans_start();
$customer_reward_enable = $this->input->post('customer_reward_enable') != NULL;
@@ -689,17 +699,14 @@ class Config extends Secure_Controller
if($customer_reward_enable)
{
$not_to_delete = array();
$array_save = array();
foreach($this->input->post() as $key => $value)
{
if(strstr($key, 'customer_reward_enable'))
{
// skip the check box
}
elseif(strstr($key, 'customer_reward'))
if(strstr($key, 'customer_reward') && $key != 'customer_reward_enable')
{
$customer_reward_id = preg_replace("/.*?_(\d+)$/", "$1", $key);
unset($deleted_packages[$customer_reward_id]);
$not_to_delete[] = $customer_reward_id;
$array_save[$customer_reward_id]['package_name'] = $value;
}
elseif(strstr($key, 'reward_points'))
@@ -720,9 +727,14 @@ class Config extends Secure_Controller
}
// all packages not available in post will be deleted now
foreach($deleted_packages as $customer_reward_id => $customer_reward)
$deleted_packages = $this->Customer_rewards->get_all()->result_array();
foreach($deleted_packages as $customer_rewards => $reward_category)
{
$this->Customer_rewards->delete($customer_reward_id);
if(!in_array($reward_category['package_id'], $not_to_delete))
{
$this->Customer_rewards->delete($reward_category['package_id']);
}
}
}

View File

@@ -45,13 +45,6 @@ class Customer_rewards extends CI_Model
}
public function get_all()
{
$this->db->from('customers_packages');
return $this->db->get();
}
public function get_undeleted_all()
{
$this->db->from('customers_packages');
$this->db->where('deleted', 0);
@@ -60,7 +53,7 @@ class Customer_rewards extends CI_Model
}
/**
Deletes one reward
Deletes one reward package
*/
public function delete($package_id)
{

View File

@@ -64,13 +64,6 @@ class Dinner_table extends CI_Model
}
public function get_all()
{
$this->db->from('dinner_tables');
return $this->db->get();
}
public function get_undeleted_all()
{
$this->db->from('dinner_tables');
$this->db->where('deleted', 0);

View File

@@ -6,34 +6,33 @@
class Stock_location extends CI_Model
{
public function exists($location_id = -1)
{
$this->db->from('stock_locations');
$this->db->where('location_id', $location_id);
public function exists($location_id = -1)
{
$this->db->from('stock_locations');
$this->db->where('location_id', $location_id);
return ($this->db->get()->num_rows() >= 1);
}
return ($this->db->get()->num_rows() >= 1);
}
public function get_all($limit = 10000, $offset = 0)
{
$this->db->from('stock_locations');
$this->db->limit($limit);
$this->db->offset($offset);
public function get_all()
{
$this->db->from('stock_locations');
$this->db->where('deleted', 0);
return $this->db->get();
}
return $this->db->get();
}
public function get_undeleted_all($module_id = 'items')
{
$this->db->from('stock_locations');
$this->db->join('permissions', 'permissions.location_id = stock_locations.location_id');
public function get_undeleted_all($module_id = 'items')
{
$this->db->from('stock_locations');
$this->db->join('permissions', 'permissions.location_id = stock_locations.location_id');
$this->db->join('grants', 'grants.permission_id = permissions.permission_id');
$this->db->where('person_id', $this->session->userdata('person_id'));
$this->db->like('permissions.permission_id', $module_id, 'after');
$this->db->where('deleted', 0);
$this->db->where('person_id', $this->session->userdata('person_id'));
$this->db->like('permissions.permission_id', $module_id, 'after');
$this->db->where('deleted', 0);
return $this->db->get();
}
return $this->db->get();
}
public function show_locations($module_id = 'items')
{
@@ -47,17 +46,17 @@ class Stock_location extends CI_Model
return $this->get_all()->num_rows() > 1;
}
public function get_allowed_locations($module_id = 'items')
{
$stock = $this->get_undeleted_all($module_id)->result_array();
$stock_locations = array();
foreach($stock as $location_data)
{
$stock_locations[$location_data['location_id']] = $location_data['location_name'];
}
public function get_allowed_locations($module_id = 'items')
{
$stock = $this->get_undeleted_all($module_id)->result_array();
$stock_locations = array();
foreach($stock as $location_data)
{
$stock_locations[$location_data['location_id']] = $location_data['location_name'];
}
return $stock_locations;
}
return $stock_locations;
}
public function is_allowed_location($location_id, $module_id = 'items')
{
@@ -72,35 +71,35 @@ class Stock_location extends CI_Model
return ($this->db->get()->num_rows() == 1);
}
public function get_default_location_id()
{
$this->db->from('stock_locations');
$this->db->join('permissions', 'permissions.location_id = stock_locations.location_id');
public function get_default_location_id()
{
$this->db->from('stock_locations');
$this->db->join('permissions', 'permissions.location_id = stock_locations.location_id');
$this->db->join('grants', 'grants.permission_id = permissions.permission_id');
$this->db->where('person_id', $this->session->userdata('person_id'));
$this->db->where('deleted', 0);
$this->db->limit(1);
$this->db->where('person_id', $this->session->userdata('person_id'));
$this->db->where('deleted', 0);
$this->db->limit(1);
return $this->db->get()->row()->location_id;
}
return $this->db->get()->row()->location_id;
}
public function get_location_name($location_id)
{
$this->db->from('stock_locations');
$this->db->where('location_id', $location_id);
public function get_location_name($location_id)
{
$this->db->from('stock_locations');
$this->db->where('location_id', $location_id);
return $this->db->get()->row()->location_name;
}
return $this->db->get()->row()->location_name;
}
public function save(&$location_data, $location_id)
{
public function save(&$location_data, $location_id)
{
$location_name = $location_data['location_name'];
$location_data_to_save = array('location_name' => $location_name, 'deleted' => 0);
$location_data_to_save = array('location_name' => $location_name, 'deleted' => 0);
if(!$this->exists($location_id))
{
$this->db->trans_start();
if(!$this->exists($location_id))
{
$this->db->trans_start();
$this->db->insert('stock_locations', $location_data_to_save);
$location_id = $this->db->insert_id();
@@ -125,40 +124,40 @@ class Stock_location extends CI_Model
$this->db->where('location_id', $location_id);
return $this->db->update('stock_locations', $location_data_to_save);
}
}
private 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);
private 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);
}
}
// 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
*/
public function delete($location_id)
{
$this->db->trans_start();
/*
Deletes one item
*/
public function delete($location_id)
{
$this->db->trans_start();
$this->db->where('location_id', $location_id);
$this->db->update('stock_locations', array('deleted' => 1));
$this->db->where('location_id', $location_id);
$this->db->update('stock_locations', array('deleted' => 1));
$this->db->where('location_id', $location_id);
$this->db->delete('permissions');
$this->db->where('location_id', $location_id);
$this->db->delete('permissions');
$this->db->trans_complete();
$this->db->trans_complete();
return $this->db->trans_status();
}
}
}
?>

View File

@@ -182,18 +182,22 @@ class Tax extends CI_Model
}
else
{
return FALSE;
return FALSE;
}
}
/**
* Inserts or updates a tax_category
*/
public function save_tax_category(&$tax_category_data, $tax_category_id)
public function save_tax_category(&$tax_category_data, &$tax_category_id)
{
if(!$this->tax_category_exists($tax_category_id))
{
return $this->db->insert('tax_categories', $tax_category_data);
$result = $this->db->insert('tax_categories', $tax_category_data);
$tax_category_id = $this->db->insert_id();
return $result;
}
$this->db->where('tax_category_id', $tax_category_id);

View File

@@ -1,14 +1,14 @@
<?php
$i = 0;
foreach($customer_rewards as $customer_rewards=>$table)
foreach($customer_rewards as $customer_rewards => $reward_category)
{
$customer_reward_id = $table['package_id'];
$customer_reward_name = $table['package_name'];
$customer_points_percent = $table['points_percent'];
$customer_reward_id = $reward_category['package_id'];
$customer_reward_name = $reward_category['package_name'];
$customer_points_percent = $reward_category['points_percent'];
++$i;
?>
<div class="form-group form-group-sm" style="<?php echo $table['deleted'] ? 'display:none;' : 'display:block;' ?>">
<div class="form-group form-group-sm" style="<?php echo $reward_category['deleted'] ? 'display:none;' : 'display:block;' ?>">
<?php echo form_label($this->lang->line('config_customer_reward') . ' ' . $i, 'customer_reward_' . $i, array('class'=>'required control-label col-xs-2')); ?>
<div class='col-xs-2'>
<?php $form_data = array(
@@ -16,8 +16,8 @@ foreach($customer_rewards as $customer_rewards=>$table)
'id'=>'customer_reward_' . $customer_reward_id,
'class'=>'customer_reward valid_chars form-control input-sm required',
'value'=>$customer_reward_name
);
$table['deleted'] && $form_data['disabled'] = 'disabled';
);
$reward_category['deleted'] && $form_data['disabled'] = 'disabled';
echo form_input($form_data);
?>
</div>
@@ -27,8 +27,8 @@ foreach($customer_rewards as $customer_rewards=>$table)
'id'=>'reward_points_' . $customer_reward_id,
'class'=>'customer_reward valid_chars form-control input-sm required',
'value'=>$customer_points_percent
);
$table['deleted'] && $form_data['disabled'] = 'disabled';
);
$reward_category['deleted'] && $form_data['disabled'] = 'disabled';
echo form_input($form_data);
?>
</div>

View File

@@ -1,7 +1,7 @@
<?php
$i = 0;
foreach($dinner_tables as $dinner_tables=>$table)
foreach($dinner_tables as $dinner_tables => $table)
{
$dinner_table_id = $table['dinner_table_id'];
$dinner_table_name = $table['name'];
@@ -15,7 +15,7 @@ foreach($dinner_tables as $dinner_tables=>$table)
'id'=>'dinner_table_' . $dinner_table_id,
'class'=>'dinner_table valid_chars form-control input-sm required',
'value'=>$dinner_table_name
);
);
$table['deleted'] && $form_data['disabled'] = 'disabled';
echo form_input($form_data);
?>

View File

@@ -1,7 +1,7 @@
<?php
$i = 0;
foreach($stock_locations as $location=>$location_data)
foreach($stock_locations as $location => $location_data)
{
$location_id = $location_data['location_id'];
$location_name = $location_data['location_name'];
@@ -15,7 +15,7 @@ foreach($stock_locations as $location=>$location_data)
'id'=>'stock_location_' . $location_id,
'class'=>'stock_location valid_chars form-control input-sm required',
'value'=>$location_name
);
);
$location_data['deleted'] && $form_data['disabled'] = 'disabled';
echo form_input($form_data);
?>

View File

@@ -1,7 +1,7 @@
<?php
$i = 0;
foreach($tax_categories as $tax_category=>$category)
foreach($tax_categories as $tax_category => $category)
{
$tax_category_id = $category['tax_category_id'];
$tax_category = $category['tax_category'];
@@ -14,7 +14,7 @@ foreach($tax_categories as $tax_category=>$category)
<?php $form_data = array(
'name'=>'tax_category_' . $tax_category_id,
'id'=>'tax_category_' . $tax_category_id,
'class'=>'valid_chars form-control input-sm',
'class'=>'valid_chars form-control input-sm required',
'value'=>$tax_category
);
echo form_input($form_data);
@@ -24,7 +24,7 @@ foreach($tax_categories as $tax_category=>$category)
<?php $form_data = array(
'name'=>'tax_group_sequence_' . $tax_category_id,
'id'=>'tax_group_sequence_' . $tax_category_id,
'class'=>'valid_chars form-control input-sm',
'class'=>'valid_chars form-control input-sm required',
'value'=>$tax_group_sequence
);
echo form_input($form_data);