Merge pull request #2431 from opensourcepos/attributes_csv_import_integration

Attributes csv import integration
This commit is contained in:
FrancescoUK
2019-06-04 21:13:54 +01:00
committed by GitHub
8 changed files with 527 additions and 304 deletions

View File

@@ -17,14 +17,14 @@ class Attribute extends CI_Model
const SHOW_IN_ITEMS = 1;
const SHOW_IN_SALES = 2;
const SHOW_IN_RECEIVINGS = 4;
public static function get_definition_flags()
{
$class = new ReflectionClass(__CLASS__);
return array_flip($class->getConstants());
}
/*
Determines if a given definition_id is an attribute
*/
@@ -33,10 +33,10 @@ class Attribute extends CI_Model
$this->db->from('attribute_definitions');
$this->db->where('definition_id', $definition_id);
$this->db->where('deleted', $deleted);
return ($this->db->get()->num_rows() == 1);
}
public function link_exists($item_id, $definition_id = FALSE)
{
$this->db->where('sale_id');
@@ -50,13 +50,13 @@ class Attribute extends CI_Model
else
{
$this->db->where('definition_id', $definition_id);
}
$this->db->where('item_id', $item_id);
return ($this->db->get()->num_rows() > 0);
}
/*
Determines if a given attribute_value exists in the attribute_values table and returns the attribute_id if it does
*/
@@ -65,10 +65,10 @@ class Attribute extends CI_Model
$this->db->distinct('attribute_id');
$this->db->from('attribute_values');
$this->db->where('attribute_value', $attribute_value);
return $this->db->get()->row()->attribute_id;
}
/*
Gets information about a particular attribute definition
*/
@@ -78,9 +78,9 @@ class Attribute extends CI_Model
$this->db->from('attribute_definitions AS definition');
$this->db->join('attribute_definitions AS parent_definition', 'parent_definition.definition_id = definition.definition_fk', 'left');
$this->db->where('definition.definition_id', $definition_id);
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->row();
@@ -89,17 +89,17 @@ class Attribute extends CI_Model
{
//Get empty base parent object, as $item_id is NOT an item
$item_obj = new stdClass();
//Get all the fields from items table
foreach($this->db->list_fields('attribute_definitions') as $field)
{
$item_obj->$field = '';
}
return $item_obj;
}
}
/*
Performs a search on attribute definitions
*/
@@ -108,22 +108,22 @@ class Attribute extends CI_Model
$this->db->select('definition_group.definition_name AS definition_group, definition.*');
$this->db->from('attribute_definitions AS definition');
$this->db->join('attribute_definitions AS definition_group', 'definition_group.definition_id = definition.definition_fk', 'left');
$this->db->group_start();
$this->db->like('definition.definition_name', $search);
$this->db->or_like('definition.definition_type', $search);
$this->db->group_end();
$this->db->where('definition.deleted', 0);
$this->db->order_by($sort, $order);
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
public function get_attributes_by_item($item_id)
{
$this->db->from('attribute_definitions');
@@ -132,51 +132,51 @@ class Attribute extends CI_Model
$this->db->where('receiving_id');
$this->db->where('sale_id');
$this->db->where('deleted', 0);
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'definition_id');
}
public function get_values_by_definitions($definition_ids)
{
if(count($definition_ids ? : []))
{
$this->db->from('attribute_definitions');
$this->db->group_start();
$this->db->where_in('definition_fk', array_keys($definition_ids));
$this->db->or_where_in('definition_id', array_keys($definition_ids));
$this->db->where('definition_type !=', GROUP);
$this->db->group_end();
$this->db->where('deleted', 0);
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'definition_id');
}
return array();
}
public function get_definitions_by_type($attribute_type, $definition_id = -1)
{
$this->db->from('attribute_definitions');
$this->db->where('definition_type', $attribute_type);
$this->db->where('deleted', 0);
if($definition_id != -1)
{
$this->db->where('definition_id != ', $definition_id);
}
$this->db->where('definition_fk');
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'definition_id', 'definition_name');
}
public function get_definitions_by_flags($definition_flags)
{
$this->db->from('attribute_definitions');
@@ -185,47 +185,60 @@ class Attribute extends CI_Model
$this->db->where('definition_type <>', GROUP);
$this->db->order_by('definition_id');
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'definition_id', 'definition_name');
}
public function get_definition_names()
/**
* Returns an array of attribute definition names and IDs
*
* @param boolean $groups If FALSE does not return GROUP type attributes in the array
* @return array Array containing definition IDs, attribute names and -1 index with the local language '[SELECT]' line.
*/
public function get_definition_names($groups = TRUE)
{
$this->db->from('attribute_definitions');
$this->db->where('deleted', 0);
if($groups === FALSE)
{
$this->db->where_not_in('definition_type',GROUP);
}
$results = $this->db->get()->result_array();
$definition_name = array(-1 => $this->lang->line('common_none_selected_text'));
return $definition_name + $this->_to_array($results, 'definition_id', 'definition_name');
}
public function get_definition_values($definition_id)
{
$attribute_values = [];
if($definition_id > -1)
{
$this->db->from('attribute_links');
$this->db->join('attribute_values', 'attribute_values.attribute_id = attribute_links.attribute_id');
$this->db->where('definition_id', $definition_id);
$this->db->where('item_id');
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'attribute_id', 'attribute_value');
}
return $attribute_values;
}
private function _to_array($results, $key, $value = '')
{
return array_column(array_map(function($result) use ($key, $value) {
return [$result[$key], empty($value) ? $result : $result[$value]];
}, $results), 1, 0);
}
/*
Gets total of rows
*/
@@ -233,10 +246,10 @@ class Attribute extends CI_Model
{
$this->db->from('attribute_definitions');
$this->db->where('deleted', 0);
return $this->db->count_all_results();
}
/*
Get number of rows
*/
@@ -244,11 +257,11 @@ class Attribute extends CI_Model
{
return $this->search($search)->num_rows();
}
private function check_data_validity($definition, $from, $to)
{
$success = FALSE;
if($from === TEXT)
{
$this->db->select('item_id,attribute_value');
@@ -256,7 +269,7 @@ class Attribute extends CI_Model
$this->db->join('attribute_links', 'attribute_values.attribute_id = attribute_links.attribute_id');
$this->db->where('definition_id',$definition);
$success = TRUE;
if($to === DATETIME)
{
foreach($this->db->get()->result_array() as $row)
@@ -282,22 +295,22 @@ class Attribute extends CI_Model
}
return $success;
}
private function convert_definition_type($definition_id, $from_type, $to_type)
{
$success = FALSE;
//From TEXT to DATETIME
if($from_type === TEXT)
{
if($to_type === DATETIME || $to_type === DECIMAL)
{
$field = ($to_type === DATETIME ? 'attribute_datetime' : 'attribute_decimal');
if($this->check_data_validity($definition_id, $from_type, $to_type))
{
$this->db->trans_start();
$query = 'UPDATE ospos_attribute_values ';
$query .= 'INNER JOIN ospos_attribute_links ';
$query .= 'ON ospos_attribute_values.attribute_id = ospos_attribute_links.attribute_id ';
@@ -305,7 +318,7 @@ class Attribute extends CI_Model
$query .= 'attribute_value = NULL ';
$query .= 'WHERE definition_id = ' . $this->db->escape($definition_id);
$success = $this->db->query($query);
$this->db->trans_complete();
}
}
@@ -314,30 +327,30 @@ class Attribute extends CI_Model
$success = TRUE;
}
}
//From DROPDOWN to TEXT
else if($from_type === DROPDOWN)
{
//From DROPDOWN to TEXT
$this->db->trans_start();
$this->db->from('ospos_attribute_links');
$this->db->where('definition_id',$definition_id);
$this->db->where('item_id',NULL);
$success = $this->db->delete();
$this->db->trans_complete();
}
//Any other allowed conversion does not get checked here
else
{
$success = TRUE;
}
return $success;
}
/*
Inserts or updates a definition
*/
@@ -345,23 +358,26 @@ class Attribute extends CI_Model
{
//Run these queries as a transaction, we want to make sure we do all or nothing
$this->db->trans_start();
//Definition doesn't exist
if($definition_id === -1 || !$this->exists($definition_id))
{
$success = $this->db->insert('attribute_definitions', $definition_data);
$definition_data['definition_id'] = $this->db->insert_id();
}
//Definition already exists
else
{
$this->db->select('definition_type');
$this->db->select('definition_type, definition_name');
$this->db->from('attribute_definitions');
$this->db->where('definition_id',$definition_id);
$from_definition_type = $this->db->get()->row()->definition_type;
$row = $this->db->get()->row();
$from_definition_type = $row->definition_type;
$from_definition_name = $row->definition_name;
$to_definition_type = $definition_data['definition_type'];
if($from_definition_type !== $to_definition_type)
{
if(!$this->convert_definition_type($definition_id,$from_definition_type,$to_definition_type))
@@ -369,32 +385,35 @@ class Attribute extends CI_Model
return FALSE;
}
}
$this->db->where('definition_id', $definition_id);
$success = $this->db->update('attribute_definitions', $definition_data);
$definition_data['definition_id'] = $definition_id;
}
$this->db->trans_complete();
$success &= $this->db->trans_status();
return $success;
}
public function get_definition_by_name($definition_name, $definition_type)
public function get_definition_by_name($definition_name, $definition_type = FALSE)
{
$this->db->from('attribute_definitions');
$this->db->where('definition_name', $definition_name);
$this->db->where('definition_type', $definition_type);
return $this->db->get()->row_object();
if($definition_type != FALSE)
{
$this->db->where('definition_type', $definition_type);
}
return $this->db->get()->result_array();
}
public function save_link($item_id, $definition_id, $attribute_id)
{
$this->db->trans_start();
if($this->link_exists($item_id, $definition_id))
{
$this->db->where('definition_id', $definition_id);
@@ -407,30 +426,30 @@ class Attribute extends CI_Model
{
$this->db->insert('attribute_links', array('attribute_id' => $attribute_id, 'item_id' => $item_id, 'definition_id' => $definition_id));
}
$this->db->trans_complete();
return $this->db->trans_status();
}
public function delete_link($item_id)
{
$this->db->where('sale_id');
$this->db->where('receiving_id');
return $this->db->delete('attribute_links', array('item_id' => $item_id));
}
public function get_link_value($item_id, $definition_id)
{
$this->db->where('item_id', $item_id);
$this->db->where('definition_id', $definition_id);
$this->db->where('sale_id');
$this->db->where('receiving_id');
return $this->db->get('attribute_links')->row_object();
}
public function get_link_values($item_id, $sale_receiving_fk, $id, $definition_flags)
{
$this->db->select('GROUP_CONCAT(attribute_value SEPARATOR ", ") AS attribute_values, GROUP_CONCAT(attribute_datetime SEPARATOR ", ") AS attribute_datetimevalues');
@@ -439,7 +458,7 @@ class Attribute extends CI_Model
$this->db->join('attribute_definitions', 'attribute_definitions.definition_id = attribute_links.definition_id');
$this->db->where('definition_type <>', GROUP);
$this->db->where('deleted', 0);
if(!empty($id))
{
$this->db->where($sale_receiving_fk, $id);
@@ -451,26 +470,26 @@ class Attribute extends CI_Model
}
$this->db->where('item_id', (int) $item_id);
$this->db->where('definition_flags & ', $definition_flags);
$results = $this->db->get();
if ($results->num_rows() > 0)
{
$row_object = $results->row_object();
$datetime_values = explode(', ', $row_object->attribute_datetimevalues);
$attribute_values = array();
foreach (array_filter($datetime_values) as $datetime_value)
{
$attribute_values[] = to_datetime(strtotime($datetime_value));
}
return implode(',', $attribute_values) . $row_object->attribute_values;
}
return "";
}
public function get_attribute_value($item_id, $definition_id)
{
$this->db->from('attribute_values');
@@ -479,10 +498,10 @@ class Attribute extends CI_Model
$this->db->where('sale_id');
$this->db->where('receiving_id');
$this->db->where('item_id', (int) $item_id);
return $this->db->get()->row_object();
}
public function copy_attribute_links($item_id, $sale_receiving_fk, $id)
{
$this->db->query(
@@ -492,7 +511,7 @@ class Attribute extends CI_Model
WHERE item_id = ' . $this->db->escape($item_id) . ' AND sale_id IS NULL AND receiving_id IS NULL'
);
}
public function get_suggestions($definition_id, $term)
{
$suggestions = array();
@@ -510,14 +529,14 @@ class Attribute extends CI_Model
$row_array = (array) $row;
$suggestions[] = array('value' => $row_array['attribute_id'], 'label' => $row_array['attribute_value']);
}
return $suggestions;
}
public function save_value($attribute_value, $definition_id, $item_id = FALSE, $attribute_id = FALSE, $definition_type = DROPDOWN)
{
$this->db->trans_start();
if(empty($attribute_id) || empty($item_id))
{
if($definition_type == TEXT || $definition_type == DROPDOWN)
@@ -543,7 +562,7 @@ class Attribute extends CI_Model
$this->db->insert('attribute_values', array('attribute_datetime' => date('Y-m-d H:i:s', strtotime($attribute_value))));
$attribute_id = $this->db->insert_id();
}
$this->db->insert('attribute_links', array(
'attribute_id' => empty($attribute_id) ? NULL : $attribute_id,
'item_id' => empty($item_id) ? NULL : $item_id,
@@ -554,29 +573,35 @@ class Attribute extends CI_Model
$this->db->where('attribute_id', $attribute_id);
$this->db->update('attribute_values', array('attribute_value' => $attribute_value));
}
$this->db->trans_complete();
return $attribute_id;
}
public function delete_value($attribute_value, $definition_id)
{
return $this->db->query("DELETE atrv, atrl FROM " . $this->db->dbprefix('attribute_values') . " atrv, " . $this->db->dbprefix('attribute_links') . " atrl " .
"WHERE atrl.attribute_id = atrv.attribute_id AND atrv.attribute_value = " . $this->db->escape($attribute_value) . " AND atrl.definition_id = " . $this->db->escape($definition_id));
}
/**
* Deletes an Attribute definition from the database and associated column in the items_import.csv
*
* @param unknown $definition_id Attribute definition ID to remove.
* @return boolean TRUE if successful and FALSE if there is a failure
*/
public function delete_definition($definition_id)
{
$this->db->where('definition_id', $definition_id);
return $this->db->update('attribute_definitions', array('deleted' => 1));
}
public function delete_definition_list($definition_ids)
{
$this->db->where_in('definition_id', $definition_ids);
return $this->db->update('attribute_definitions', array('deleted' => 1));
}
}

View File

@@ -109,27 +109,27 @@ class Stock_location extends CI_Model
{
$this->db->trans_start();
$this->db->insert('stock_locations', $location_data_to_save);
$location_id = $this->db->insert_id();
$this->db->insert('stock_locations', $location_data_to_save);
$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);
$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);
}
// 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();
$this->db->trans_complete();
return $this->db->trans_status();
}
}
$original_location_name = $this->get_location_name($location_id);
$original_location_name = $this->get_location_name($location_id);
if($original_location_name != $location_name)
{
@@ -167,7 +167,7 @@ class Stock_location extends CI_Model
/*
Deletes one item
*/
*/
public function delete($location_id)
{
$this->db->trans_start();