Feature #27: Changing cost price while receiving items

This commit is contained in:
Martes Erede
2015-01-24 21:40:07 +01:00
parent 7544aae394
commit 927dbbabb6
3 changed files with 48 additions and 3 deletions

View File

@@ -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);
}
}
?>

View File

@@ -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']);

View File

@@ -93,7 +93,7 @@ else
<td style="align:center;"><?php echo $item['name']; ?><br /> [<?php echo $item['in_stock']; ?> in <?php echo $item['stock_name']; ?>]
<?php echo form_hidden('location', $item['item_location']); ?></td>
<?php if ($items_module_allowed && !$mode=='requisition')
<?php if ($items_module_allowed && $mode !='requisition')
{
?>
<td><?php echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6'));?></td>