Files
opensourcepos/application/libraries/Receiving_lib.php
jekkos 1c6e44af0f Add multi-stock feature
Finished task
1. User can add stock location in config page. Adding format is StockA,StockB,StockC for example
2. Item page, there is no more quantity column show in cert table. The quantity tracking is integrated in inventory detail
3. Receiving page, There is a stock location for user to select before receiving or returning stuff
4. Sale page, There is a stock location which is for user to select a stock location they sell from

Remain task
1. Requisition work flow
2. Language editing
3. Remove unuse code

PS

git-svn-id: svn+ssh://svn.code.sf.net/p/opensourcepos/code/@115 c3eb156b-1dc0-44e1-88ae-e38439141b53
2014-08-19 19:55:24 +00:00

405 lines
12 KiB
PHP

<?php
class Receiving_lib
{
var $CI;
function __construct()
{
$this->CI =& get_instance();
}
function get_cart()
{
if(!$this->CI->session->userdata('cartRecv'))
$this->set_cart(array());
return $this->CI->session->userdata('cartRecv');
}
function set_cart($cart_data)
{
$this->CI->session->set_userdata('cartRecv',$cart_data);
}
function get_supplier()
{
if(!$this->CI->session->userdata('supplier'))
$this->set_supplier(-1);
return $this->CI->session->userdata('supplier');
}
function set_supplier($supplier_id)
{
$this->CI->session->set_userdata('supplier',$supplier_id);
}
function get_mode()
{
if(!$this->CI->session->userdata('recv_mode'))
$this->set_mode('receive');
return $this->CI->session->userdata('recv_mode');
}
function set_mode($mode)
{
$this->CI->session->set_userdata('recv_mode',$mode);
}
function get_stock_source()
{
if(!$this->CI->session->userdata('recv_stock_source'))
{
$stock_locations = $this->CI->Stock_locations->get_undeleted_all()->result_array();
$location_name = 'stock_'.$stock_locations[0]['location_id'];
$this->set_stock_source($location_name);
}
return $this->CI->session->userdata('recv_stock_source');
}
function set_stock_source($stock_source)
{
$this->CI->session->set_userdata('recv_stock_source',$stock_source);
}
function get_stock_destination()
{
if(!$this->CI->session->userdata('recv_stock_destination'))
{
$stock_locations = $this->CI->Stock_locations->get_undeleted_all()->result_array();
$location_name = 'stock_'.$stock_locations[0]['location_id'];
$this->set_stock_destination($location_name);
}
return $this->CI->session->userdata('recv_stock_destination');
}
function set_stock_destination($stock_destination)
{
$this->CI->session->set_userdata('recv_stock_destination',$stock_destination);
}
function get_location_id_from_stock_location($location)
{
return substr($location, 6);
}
function add_item($item_id,$quantity=1,$discount=0,$price=null,$description=null,$serialnumber=null)
{
//make sure item exists in database.
if(!$this->CI->Item->exists($item_id))
{
//try to get item id given an item_number
$item_id = $this->CI->Item->get_item_id($item_id);
if(!$item_id)
return false;
}
//Get items in the receiving so far.
$items = $this->get_cart();
//We need to loop through all items in the cart.
//If the item is already there, get it's key($updatekey).
//We also need to get the next key that we are going to use in case we need to add the
//item to the list. Since items can be deleted, we can't use a count. we use the highest key + 1.
$maxkey=0; //Highest key so far
$itemalreadyinsale=FALSE; //We did not find the item yet.
$insertkey=0; //Key to use for new entry.
$updatekey=0; //Key to use to update(quantity)
foreach ($items as $item)
{
//We primed the loop so maxkey is 0 the first time.
//Also, we have stored the key in the element itself so we can compare.
//There is an array function to get the associated key for an element, but I like it better
//like that!
if($maxkey <= $item['line'])
{
$maxkey = $item['line'];
}
if($item['item_id']==$item_id)
{
$itemalreadyinsale=TRUE;
$updatekey=$item['line'];
}
}
$insertkey=$maxkey+1;
//array records are identified by $insertkey and item_id is just another field.
$item = array(($insertkey)=>
array(
'item_id'=>$item_id,
'line'=>$insertkey,
'name'=>$this->CI->Item->get_info($item_id)->name,
'description'=>$description!=null ? $description: $this->CI->Item->get_info($item_id)->description,
'serialnumber'=>$serialnumber!=null ? $serialnumber: '',
'allow_alt_description'=>$this->CI->Item->get_info($item_id)->allow_alt_description,
'is_serialized'=>$this->CI->Item->get_info($item_id)->is_serialized,
'quantity'=>$quantity,
'discount'=>$discount,
'price'=>$price!=null ? $price: $this->CI->Item->get_info($item_id)->cost_price
)
);
//Item already exists
if($itemalreadyinsale)
{
$items[$updatekey]['quantity']+=$quantity;
}
else
{
//add to existing array
$items+=$item;
}
$this->set_cart($items);
return true;
}
function add_item_unit($item_id,$quantity=1, $description=null)
{
//make sure item exists in database.
if(!$this->CI->Item->exists($item_id))
{
//try to get item id given an item_number
$item_id = $this->CI->Item->get_item_id($item_id);
if(!$item_id)
return false;
}
$related_item_number = $this->CI->Item_unit->get_info($item_id)->related_number;
if($related_item_number!= null && !$this->CI->Item->is_warehouse_item_exist($related_item_number))
{
return false;
}
//Get items in the requisition so far.
$items = $this->get_cart();
//We need to loop through all items in the cart.
//If the item is already there, get it's key($updatekey).
//We also need to get the next key that we are going to use in case we need to add the
//item to the list. Since items can be deleted, we can't use a count. we use the highest key + 1.
$maxkey=0; //Highest key so far
$itemalreadyinsale=FALSE; //We did not find the item yet.
$insertkey=0; //Key to use for new entry.
$updatekey=0; //Key to use to update(quantity)
foreach ($items as $item)
{
//We primed the loop so maxkey is 0 the first time.
//Also, we have stored the key in the element itself so we can compare.
//There is an array function to get the associated key for an element, but I like it better
//like that!
if($maxkey <= $item['line'])
{
$maxkey = $item['line'];
}
if($item['item_id']==$item_id)
{
$itemalreadyinsale=TRUE;
$updatekey=$item['line'];
}
}
$insertkey=$maxkey+1;
//array records are identified by $insertkey and item_id is just another field.
$item = array(($insertkey)=>
array(
'item_id'=>$item_id,
'line'=>$insertkey,
'name'=>$this->CI->Item->get_info($item_id)->name,
'description'=>$description!=null ? $description: $this->CI->Item->get_info($item_id)->description,
'quantity'=>$quantity
)
);
$item[$insertkey]['unit_quantity']= $this->CI->Item_unit->get_info($item_id)->unit_quantity;
$related_item_id = $this->CI->Item->get_item_id($related_item_number,'warehouse');
if($related_item_id == null)
{
$related_item_id = $this->CI->Item->get_item_id($related_item_number,'sale_stock');
if($related_item_id == null)
{
return false;
}
}
$item[$insertkey]['related_item'] = $this->CI->Item->get_info($related_item_id)->name;
//Item already exists
if($itemalreadyinsale)
{
$items[$updatekey]['quantity']+=$quantity;
}
else
{
//add to existing array
$items+=$item;
}
$this->set_cart($items);
return true;
}
function edit_item($line,$description,$serialnumber,$quantity,$discount,$price)
{
$items = $this->get_cart();
if(isset($items[$line]))
{
$items[$line]['description'] = $description;
$items[$line]['serialnumber'] = $serialnumber;
$items[$line]['quantity'] = $quantity;
$items[$line]['discount'] = $discount;
$items[$line]['price'] = $price;
$this->set_cart($items);
}
return false;
}
function edit_item_unit($line,$description,$quantity,$unit_quantity,$related_item)
{
$items = $this->get_cart();
if(isset($items[$line]))
{
$items[$line]['quantity'] = $quantity;
$this->set_cart($items);
}
return false;
}
function is_valid_receipt($receipt_receiving_id)
{
//RECV #
$pieces = explode(' ',$receipt_receiving_id);
if(count($pieces)==2)
{
return $this->CI->Receiving->exists($pieces[1]);
}
return false;
}
function is_valid_item_kit($item_kit_id)
{
//KIT #
$pieces = explode(' ',$item_kit_id);
if(count($pieces)==2)
{
return $this->CI->Item_kit->exists($pieces[1]);
}
return false;
}
function return_entire_receiving($receipt_receiving_id)
{
//POS #
$pieces = explode(' ',$receipt_receiving_id);
$receiving_id = $pieces[1];
$this->empty_cart();
$this->delete_supplier();
foreach($this->CI->Receiving->get_receiving_items($receiving_id)->result() as $row)
{
$this->add_item($row->item_id,-$row->quantity_purchased,$row->discount_percent,$row->item_unit_price,$row->description,$row->serialnumber);
}
$this->set_supplier($this->CI->Receiving->get_supplier($receiving_id)->person_id);
}
function add_item_kit($external_item_kit_id)
{
//KIT #
$pieces = explode(' ',$external_item_kit_id);
$item_kit_id = $pieces[1];
foreach ($this->CI->Item_kit_items->get_info($item_kit_id) as $item_kit_item)
{
$this->add_item($item_kit_item['item_id'], $item_kit_item['quantity']);
}
}
function copy_entire_receiving($receiving_id)
{
$this->empty_cart();
$this->delete_supplier();
foreach($this->CI->Receiving->get_receiving_items($receiving_id)->result() as $row)
{
$this->add_item($row->item_id,$row->quantity_purchased,$row->discount_percent,$row->item_unit_price,$row->description,$row->serialnumber);
}
$this->set_supplier($this->CI->Receiving->get_supplier($receiving_id)->person_id);
}
function copy_entire_requisition($requisition_id)
{
$this->empty_cart();
$this->delete_supplier();
foreach($this->CI->Receiving->get_requisition_items($requisition_id)->result() as $row)
{
$this->add_item_unit($row->item_id,$row->requisition_quantity,$row->description);
}
$this->set_supplier($this->CI->Receiving->get_supplier($requisition_id)->person_id);
}
function delete_item($line)
{
$items=$this->get_cart();
unset($items[$line]);
$this->set_cart($items);
}
function empty_cart()
{
$this->CI->session->unset_userdata('cartRecv');
}
function delete_supplier()
{
$this->CI->session->unset_userdata('supplier');
}
function clear_mode()
{
$this->CI->session->unset_userdata('receiving_mode');
}
function clear_all()
{
$this->clear_mode();
$this->empty_cart();
$this->delete_supplier();
}
function get_total()
{
$total = 0;
foreach($this->get_cart() as $item)
{
$total+=($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100);
}
return $total;
}
}
?>