From 927dbbabb614412a655b0fdcdddbc24c2ddda5a0 Mon Sep 17 00:00:00 2001 From: Martes Erede Date: Sat, 24 Jan 2015 21:40:07 +0100 Subject: [PATCH] Feature #27: Changing cost price while receiving items --- application/models/item.php | 35 ++++++++++++++++++++++ application/models/receiving.php | 14 +++++++-- application/views/receivings/receiving.php | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/application/models/item.php b/application/models/item.php index 134712eb4..7b18032fb 100644 --- a/application/models/item.php +++ b/application/models/item.php @@ -585,6 +585,41 @@ class Item extends CI_Model return $this->db->get(); } + + /* + * changes the cost price of a given item + * calculates the average price between received items and items on stock + * $item_id : the item which price should be changed + * $items_received : the amount of new items received + * $new_price : the cost-price for the newly received items + * $old_price (optional) : the current-cost-price + * + * used in receiving-process to update cost-price if changed + * caution: must be used there before item_quantities gets updated, otherwise average price is wrong! + * + */ + function change_cost_price($item_id, $items_received, $new_price, $old_price = null) + { + if($old_price === null) + { + $item_info = $this->get_info($item['item_id']); + $old_price = $item_info->cost_price; + } + + $this->db->from('item_quantities'); + $this->db->select_sum('quantity'); + $this->db->where('item_id',$item_id); + $this->db->join('stock_locations','stock_locations.location_id=item_quantities.location_id'); + $this->db->where('stock_locations.deleted',0); + $old_total_quantity = $this->db->get()->row()->quantity; + + $total_quantity = $old_total_quantity + $items_received; + $average_price = ($items_received * $new_price + $old_total_quantity * $old_price)/$total_quantity; + + $data = array('cost_price' => $average_price); + + return $this->save($data, $item_id); + } } ?> \ No newline at end of file diff --git a/application/models/receiving.php b/application/models/receiving.php index 62f300ee2..691d56e93 100644 --- a/application/models/receiving.php +++ b/application/models/receiving.php @@ -80,9 +80,19 @@ class Receiving extends CI_Model $this->db->insert('receivings_items',$receivings_items_data); + $items_received = $item['receiving_quantity'] != 0 ? $item['quantity'] * $item['receiving_quantity'] : $item['quantity']; + + // update cost price, if changed + if($cur_item_info->cost_price != $item['price']) + { + $this->Item->change_cost_price($item['item_id'], + $items_received, + $item['price'], + $cur_item_info->cost_price); + } + //Update stock quantity - $item_quantity = $this->Item_quantities->get_item_quantity($item['item_id'], $item['item_location']); - $items_received = $item['receiving_quantity'] != 0 ? $item['quantity'] * $item['receiving_quantity'] : $item['quantity']; + $item_quantity = $this->Item_quantities->get_item_quantity($item['item_id'], $item['item_location']); $this->Item_quantities->save(array('quantity'=>$item_quantity->quantity + $items_received, 'item_id'=>$item['item_id'], 'location_id'=>$item['item_location']), $item['item_id'], $item['item_location']); diff --git a/application/views/receivings/receiving.php b/application/views/receivings/receiving.php index fd60c0775..ec461a3dc 100644 --- a/application/views/receivings/receiving.php +++ b/application/views/receivings/receiving.php @@ -93,7 +93,7 @@ else
[ in ] - 'price','value'=>$item['price'],'size'=>'6'));?>