Reset stock inventory when deleting items (#1288)

This commit is contained in:
jekkos
2017-05-22 19:30:06 +02:00
parent 5c0c74b037
commit 15bb3ad6a3
2 changed files with 35 additions and 0 deletions

View File

@@ -18,5 +18,34 @@ class Inventory extends CI_Model
return $this->db->get();
}
public function reset_quantity($item_id)
{
$inventory_sums = $this->Inventory->get_inventory_sum($item_id);
foreach($inventory_sums as $inventory_sum)
{
if ($inventory_sum['sum'] > 0)
{
return $this->Inventory->insert(array(
'trans_inventory' => -1 * $inventory_sum['sum'],
'trans_items' => $item_id,
'trans_location' => $inventory_sum['location_id'],
'trans_comment' => $this->lang->line('items_is_deleted'),
'trans_user' => $this->Employee->get_logged_in_employee_info()->person_id)
);
}
}
return TRUE;
}
public function get_inventory_sum($item_id)
{
$this->db->select('SUM(trans_inventory) AS sum, MAX(trans_location) AS location_id');
$this->db->from('inventory');
$this->db->where('trans_items', $item_id);
$this->db->group_by('trans_location');
return $this->db->get()->result_array();
}
}
?>

View File

@@ -358,6 +358,7 @@ class Item extends CI_Model
$this->Item_quantity->reset_quantity($item_id);
$this->db->where('item_id', $item_id);
$success = $this->db->update('items', array('deleted'=>1));
$success &= $this->Inventory->reset_quantity($item_id);
$this->db->trans_complete();
@@ -389,6 +390,11 @@ class Item extends CI_Model
$this->db->where_in('item_id', $item_ids);
$success = $this->db->update('items', array('deleted'=>1));
foreach($item_ids as $item_id)
{
$success &= $this->Inventory->reset_quantity($item_id);
}
$this->db->trans_complete();
$success &= $this->db->trans_status();