diff --git a/application/models/item_quantities.php b/application/models/item_quantities.php index 1ec21a184..0094fd32e 100644 --- a/application/models/item_quantities.php +++ b/application/models/item_quantities.php @@ -47,5 +47,20 @@ class Item_quantities extends CI_Model } return $result; } + + /* + * changes to quantity of an item according to the given amount. + * if $quantity_change is negative, it will be subtracted, + * if it is positive, it will be added to the current quantity + */ + function change_quantity($item_id, $location_id, $quantity_change) + { + $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); + } } ?> \ No newline at end of file diff --git a/application/models/receiving.php b/application/models/receiving.php index b104b3ec9..62f300ee2 100644 --- a/application/models/receiving.php +++ b/application/models/receiving.php @@ -137,11 +137,17 @@ class Receiving extends CI_Model 'trans_items'=>$item['item_id'], 'trans_user'=>$employee_id, 'trans_comment'=>'Deleting sale ' . $receiving_id, - 'trans_inventory'=>$item['quantity_purchased'] + 'trans_location'=>$item['item_location'], + 'trans_inventory'=>$item['quantity_purchased']*-1 ); // update inventory $this->Inventory->insert($inv_data); + + // update quantities + $this->Item_quantities->change_quantity($item['item_id'], + $item['item_location'], + $item['quantity_purchased']*-1); } } // delete all items diff --git a/application/models/sale.php b/application/models/sale.php index c13b1a8db..361fd587f 100644 --- a/application/models/sale.php +++ b/application/models/sale.php @@ -184,11 +184,17 @@ class Sale extends CI_Model 'trans_items'=>$item['item_id'], 'trans_user'=>$employee_id, 'trans_comment'=>'Deleting sale ' . $sale_id, - 'trans_inventory'=>$item['quantity_purchased'] + 'trans_location'=>$item['item_location'], + 'trans_inventory'=>$item['quantity_purchased']*-1 ); // update inventory $this->Inventory->insert($inv_data); + + // update quantities + $this->Item_quantities->change_quantity($item['item_id'], + $item['item_location'], + $item['quantity_purchased']*-1); } } // delete all items