Reset quantities when an Item is deleted (#496)

This commit is contained in:
FrancescoUK
2016-04-22 19:22:04 +01:00
parent 9cfddaf2ab
commit 9aa673448a
3 changed files with 56 additions and 30 deletions

View File

@@ -166,12 +166,12 @@ class Item extends CI_Model
else
{
//Get empty base parent object, as $item_id is NOT an item
$item_obj=new stdClass();
$item_obj = new stdClass();
//Get all the fields from items table
$fields = $this->db->list_fields('items');
foreach ($fields as $field)
foreach($fields as $field)
{
$item_obj->$field='';
}
@@ -197,7 +197,7 @@ class Item extends CI_Model
return $query->row()->item_id;
}
return false;
return FALSE;
}
/*
@@ -216,16 +216,16 @@ class Item extends CI_Model
/*
Inserts or updates a item
*/
public function save(&$item_data, $item_id=false)
public function save(&$item_data, $item_id=FALSE)
{
if(!$item_id or !$this->exists($item_id))
{
if($this->db->insert('items', $item_data))
{
$item_data['item_id'] = $this->db->insert_id();
return true;
return TRUE;
}
return false;
return FALSE;
}
$this->db->where('item_id', $item_id);
@@ -238,9 +238,9 @@ class Item extends CI_Model
*/
public function update_multiple($item_data, $item_ids)
{
$this->db->where_in('item_id',$item_ids);
$this->db->where_in('item_id', $item_ids);
return $this->db->update('items',$item_data);
return $this->db->update('items', $item_data);
}
/*
@@ -249,8 +249,11 @@ class Item extends CI_Model
public function delete($item_id)
{
$this->db->where('item_id', $item_id);
// set to 0 quantities
$this->Item_quantity->reset_quantity($item_id);
return $this->db->update('items', array('deleted' => 1));
return $this->db->update('items', array('deleted'=>1));
}
/*
@@ -260,7 +263,7 @@ class Item extends CI_Model
{
$this->db->where('item_id', $item_id);
return $this->db->update('items', array('deleted' => 0));
return $this->db->update('items', array('deleted'=>0));
}
/*
@@ -268,9 +271,12 @@ class Item extends CI_Model
*/
public function delete_list($item_ids)
{
$this->db->where_in('item_id',$item_ids);
$this->db->where_in('item_id', $item_ids);
return $this->db->update('items', array('deleted' => 1));
// set to 0 quantities
$this->Item_quantity->reset_quantity_list($item_ids);
return $this->db->update('items', array('deleted'=>1));
}
public function get_search_suggestions($search, $filters = array('is_deleted'=>FALSE, 'search_custom'=>FALSE), $unique = FALSE, $limit=25)

View File

@@ -1,11 +1,11 @@
<?php
class Item_quantity extends CI_Model
{
function exists($item_id,$location_id)
function exists($item_id, $location_id)
{
$this->db->from('item_quantities');
$this->db->where('item_id',$item_id);
$this->db->where('location_id',$location_id);
$this->db->where('item_id', $item_id);
$this->db->where('location_id', $location_id);
$query = $this->db->get();
return ($query->num_rows()==1);
@@ -13,9 +13,9 @@ class Item_quantity extends CI_Model
function save($location_detail, $item_id, $location_id)
{
if (!$this->exists($item_id,$location_id))
if (!$this->exists($item_id, $location_id))
{
if($this->db->insert('item_quantities',$location_detail))
if($this->db->insert('item_quantities', $location_detail))
{
return true;
}
@@ -24,27 +24,29 @@ class Item_quantity extends CI_Model
$this->db->where('item_id', $item_id);
$this->db->where('location_id', $location_id);
return $this->db->update('item_quantities',$location_detail);
return $this->db->update('item_quantities', $location_detail);
}
function get_item_quantity($item_id, $location_id)
{
$this->db->from('item_quantities');
$this->db->where('item_id',$item_id);
$this->db->where('location_id',$location_id);
$this->db->where('item_id', $item_id);
$this->db->where('location_id', $location_id);
$result = $this->db->get()->row();
if(empty($result) == true)
{
//Get empty base parent object, as $item_id is NOT an item
$result=new stdClass();
$result = new stdClass();
//Get all the fields from items table (TODO to be reviewed)
$fields = $this->db->list_fields('item_quantities');
foreach ($fields as $field)
foreach($fields as $field)
{
$result->$field='';
$result->$field = '';
}
$result->quantity = 0;
}
}
return $result;
}
@@ -57,10 +59,29 @@ class Item_quantity extends CI_Model
{
$quantity_old = $this->get_item_quantity($item_id, $location_id);
$quantity_new = $quantity_old->quantity + intval($quantity_change);
$location_detail = array('item_id'=>$item_id,
'location_id'=>$location_id,
'quantity'=>$quantity_new);
return $this->save($location_detail,$item_id,$location_id);
$location_detail = array('item_id'=>$item_id, 'location_id'=>$location_id, 'quantity'=>$quantity_new);
return $this->save($location_detail, $item_id, $location_id);
}
/*
* Set to 0 all quantity in the given item
*/
function reset_quantity($item_id)
{
$this->db->where('item_id', $item_id);
return $this->db->update('item_quantities', array('quantity'=>0));
}
/*
* Set to 0 all quantity in the given list of items
*/
function reset_quantity_list($item_ids)
{
$this->db->where_in('item_id', $item_ids);
return $this->db->update('item_quantities', array('quantity'=>0));
}
}
?>

View File

@@ -410,8 +410,7 @@ class Sale extends CI_Model
foreach($items as $item)
{
// create query to update inventory tracking
$inv_data = array
(
$inv_data = array(
'trans_date'=>date('Y-m-d H:i:s'),
'trans_items'=>$item['item_id'],
'trans_user'=>$employee_id,