From 9aa673448a8175c3c0be34951c73cd8719ea9418 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Fri, 22 Apr 2016 19:22:04 +0100 Subject: [PATCH] Reset quantities when an Item is deleted (#496) --- application/models/Item.php | 30 +++++++++------- application/models/Item_quantity.php | 53 +++++++++++++++++++--------- application/models/Sale.php | 3 +- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/application/models/Item.php b/application/models/Item.php index 4bc026ad0..887080690 100644 --- a/application/models/Item.php +++ b/application/models/Item.php @@ -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) diff --git a/application/models/Item_quantity.php b/application/models/Item_quantity.php index 551731c00..9bebaed2e 100644 --- a/application/models/Item_quantity.php +++ b/application/models/Item_quantity.php @@ -1,11 +1,11 @@ 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)); } } ?> \ No newline at end of file diff --git a/application/models/Sale.php b/application/models/Sale.php index 183449c4b..1304d0f5c 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -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,