diff --git a/application/models/Inventory.php b/application/models/Inventory.php index d1f18920c..c01463b0b 100644 --- a/application/models/Inventory.php +++ b/application/models/Inventory.php @@ -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(); + } } ?> \ No newline at end of file diff --git a/application/models/Item.php b/application/models/Item.php index a1c64299a..6fe22dfcc 100644 --- a/application/models/Item.php +++ b/application/models/Item.php @@ -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();