Merge pull request #26 from marteserede/master

Fix for bug #39, error in excel_import
This commit is contained in:
jekkos
2015-01-25 00:22:26 +01:00
7 changed files with 148 additions and 24 deletions

View File

@@ -570,8 +570,19 @@ class Items extends Secure_area implements iData_controller
$this->Item_taxes->save($items_taxes_data, $item_data['item_id']);
}
// quantities
// quantities & inventory Info
$employee_id=$this->Employee->get_logged_in_employee_info()->person_id;
$emp_info=$this->Employee->get_info($employee_id);
$comment ='Qty CSV Imported';
$cols = count($data);
// array to store information if location got a quantity
$quantity_added_to_location = array();
foreach($this->Stock_locations->get_location_ids_as_array() as $loction_id)
{
$quantity_added_to_location[$location_id] = false;
}
for ($col = 24; $col < $cols; $col = $col + 2)
{
$item_quantity_data = array(
@@ -579,26 +590,48 @@ class Items extends Secure_area implements iData_controller
'location_id' => $data[$col],
'quantity' => $data[$col + 1],
);
$this->Item_quantities->save($item_quantity_data, $data[$col], $data[$col + 1]);
$this->Item_quantities->save($item_quantity_data, $item_data['item_id'], $data[$col]);
$excel_data = array
(
'trans_items'=>$item_data['item_id'],
'trans_user'=>$employee_id,
'trans_comment'=>$comment,
'trans_location'=>$data[$col],
'trans_inventory'=>$data[$col + 1]
);
$this->db->insert('inventory',$excel_data);
$quantity_added_to_location[$data[$col]] = true;
}
// Inventory Info
$employee_id=$this->Employee->get_logged_in_employee_info()->person_id;
$emp_info=$this->Employee->get_info($employee_id);
$comment ='Qty CSV Imported';
$excel_data = array
(
'trans_items'=>$item_data['item_id'],
'trans_user'=>$employee_id,
'trans_comment'=>$comment,
'trans_inventory'=>$data[10]
);
$this->db->insert('inventory',$excel_data);
//------------------------------------------------Ramel
/*
* now iterate through the array and check for which location_id no entry into item_quantities was made yet
* those get an entry with quantity as 0.
* unfortunately a bit duplicate code from above...
*/
foreach($quantity_added_to_location as $location_id => $added)
{
if($added === false)
{
$item_quantity_data = array(
'item_id' => $item_data['item_id'],
'location_id' => $location_id,
'quantity' => 0,
);
$this->Item_quantities->save($item_quantity_data, $item_data['item_id'], $data[$col]);
$excel_data = array
(
'trans_items'=>$item_data['item_id'],
'trans_user'=>$employee_id,
'trans_comment'=>$comment,
'trans_location'=>$location_id,
'trans_inventory'=>0
);
$this->db->insert('inventory',$excel_data);
}
}
}
else//insert or update item failure
{

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

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

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']);
@@ -137,11 +147,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

View File

@@ -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

View File

@@ -26,6 +26,25 @@ class Stock_locations extends CI_Model
return $this->db->get();
}
/*
* returns all location-ids in a simple array like array (location_id, location_id, ...)
* used in items-controller::do_excel_import
* @since 22.1.15
*/
function get_location_ids_as_array()
{
$this->db->select('location_id');
$this->db->from('stock_locations');
$this->db->where('deleted', 0);
$query = $this->db->get();
$ids_array = array();
foreach($query->result() as $row)
{
$ids_array[] = $row->location_id;
}
return $ids_array;
}
function concat_location_names()
{
$this->db->select('GROUP_CONCAT(location_name SEPARATOR\',\') AS location_names', FALSE);

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>