<?php
/**********************************************************************
***********************************************************************/
include_once($path_to_root . "/includes/prefs/sysprefs.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/inventory/includes/db/alternate_units_db.inc");

class items_cart
{
    var $trans_no;
	var $trans_type;
	var $line_items;
	var $gl_items;

	var $order_id;

	var $from_loc;
	var $to_loc;
	var $tran_date;
	var $transfer_type;
	var $increase;
	var $memo_;
	var $person_id;
	var $branch_id;
	var $reference;
    var $movement_type;
    var $linked_field_id;
    var $linked_field_type;
    var $movement_type_name;
    var $original_amount;
    var $issuing_location;
    var $issuing_location_name;
    var $receiving_location;
    var $receiving_location_name;
    var $required_date;
    var $requestor_name;
    var $created_on;
    var $created_by;
    var $mr_complete;

    var $comment;
    var $customer_currency;
    var $price_factor;
    var $sales_type;
    var $plant_code;
    var $job_code;
    var $status_id;
    var $document_date;

    var $project_id;
    var $task_id;
    var $approval_path_id;
    var $project_description;
    //var $task_description;
    var $approval_path_description;
    var $location_name;
    var $required_from_date;
    var $required_to_date;
    var $check;
    var $approval_status;
    var $next_approval_group_id;
    var $sample_items;
    var $debtor_no;
    var $supplier_id;
    var $employee_code;
    var $row_state = 0;
    var $current_line_ref = 10000;
    var $id;
    var $rate = 1;
    var $total = 0;
    var $job_reference = "";
    var $currency;
    var $line_ref;
    var $pr_no;
    var $release;
    var $pr_reference;
    var $requesting_location;
    var $pr_complete;
    var $pr_date;
    var $source_type;
    var $source_order_id;
    var $mtr_complete;
    var $issuing_project_id;
    var $StockLocation;
    var $AdjDate;
    var $location;
    var $reservation_location;
    var $purchase_to_stock;
    var $po_no;
    var $tax_group_id;
    var $tax_included;
    var $doc_date;
    var $event_date;
    var $reverse;
    var $source_ref;
    var $gl_trans_type_id;
    var $pre_gl_debit_items;
	
	function __construct($type, $order_no = 0)
	{
            if($order_no > 0){
                $this->read_existing_adjustment($order_no);
            }
            else{
		$this->trans_type = $type;
		$this->clear_items();
            }
	}

//        	function __construct($type, $trans_no=0)
//	{
//		$this->trans_type = $type;
//		$this->order_id = $trans_no;
//		$this->clear_items();
//		if (in_array($type, array(ST_LOCTRANSFER, ST_INVADJUST, ST_COSTUPDATE, ST_MANUISSUE, ST_MANURECEIVE, ST_JOURNAL)))
//			$this->currency = get_company_pref('curr_default');
//		$this->rate = 1;
//	}
	// --------------- line item functions

        function add_to_cart($line_no, $stock_id, $qty, 
                    $standard_cost, $comment = null, $description=null, $required_date = null, 
                    $line_comment=null, $row_id = 0, $issued_qty = 0, $requested_qty = 0,
                    $stock_category_id = 0, $price = 0, $requesting_location = null, $status_id = 0,
                    $required_from_date = null, $required_to_date = null, $issuing_date=null, $asset_id=null, 
                    $check=null, $tran_unit = null, $tran_qty = null, $convertion_factor = null){
		if (isset($stock_id) && $stock_id != "" && isset($qty))
		{
			$this->line_items[$line_no] = new line_item($stock_id, $qty,
				$standard_cost, $description, $required_date, 
                                $line_comment, $comment, $row_id, $issued_qty, $requested_qty,
                                $stock_category_id, $price, $requesting_location, $status_id,
                                $required_from_date, $required_to_date, $issuing_date, $asset_id, $check, 
                                $tran_unit, $tran_qty, $convertion_factor);
			return true;
		}
		else
		{
			// shouldn't come here under normal circumstances
			display_error("unexpected - adding an invalid item or null quantity", "", true);
		}

		return false;
	}
        
        function read_existing_adjustment($trans_no)
        {
            $sah = get_stock_adjustment_request_header($trans_no);
            $this->trans_no = $trans_no;
            $this->tran_date = sql2date($sah['ai_date']);
            $this->reference = $sah['reference'];
            $this->location_name = $sah['location'];
            $this->project_id = $sah['project_id'];
            $this->trans_type = $sah['type'];
            $this->sample_items = $sah['sample_items'];
            $this->next_approval_group_id = $sah['next_approval_group_id'];
            $this->movement_type = $sah['movement_type'];
            $this->movement_type_name = $sah['movement_type_name'];
            $this->comment = $sah['comments'];
            $this->row_state = $sah['row_state'];
            $this->linked_field_id = $sah['linked_field_id'];
            $this->linked_field_type = $sah['linked_field_type'];
            $this->approval_status = $sah['approval_status'];
            $this->id = $sah['id'];
            $result = get_stock_adjustment_request_items_details($trans_no);
            while($row = db_fetch($result)) {
                $line = new line_item($row['stock_id'], $row['qty'], $row['standard_cost'],  $description = null, $required_date = null, 
                                      $line_comment = null, $comment = null, $row['trans_id'], $issued_qty = 0, $requested_qty = 0,
                                      $stock_category_id = 0, $row['price'], $row['loc_code'], $status_id = 0,
                                      $required_from_date = null, $required_to_date = null, $issuing_date = null, $asset_id = null, 
                                      $check = null, $row['tran_unit'], $row['tran_qty'], $row['convertion_factor'], 
                                      $row['created_by'], $row['created_on'], $row['project_site_id'], $row['task_id']);
                                        
                $this->line_items[] = $line;
                
                $current_element = (count($this->line_items)-1);
                
                $this->line_items[$current_element]->task_description = $row['task_description'];
                $this->line_items[$current_element]->project_site_description = $row['project_description'];                
                
                set_line_ref($this, $current_element);
            }  
        }
        
	function find_cart_item($stock_id)
	{
		foreach($this->line_items as $line_no=>$line) {
			if ($line->stock_id == $stock_id)
				return $this->line_items[$line_no];
		}
		return null;
	}

	function update_cart_item($line_no, $qty, $standard_cost, $comment=null, $line_comment=null, 
                $required_date=null, $requested_qty=null, $estimate_price=null, 
                $tran_qty=null, $tran_unit=null, $convertion_factor=null)
	{
            if($tran_unit == null || $tran_unit == ""){
                $item = get_item_edit_info($this->line_items[$line_no]->stock_id);
                $tran_unit = $item['units'];
                $convertion_factor = 1;
                $qty = $tran_qty;
            }
            if($qty == 0){
                $this->line_items[$line_no]->quantity = $tran_qty * $convertion_factor;
            }

            $this->line_items[$line_no]->quantity = $qty;
            $this->line_items[$line_no]->comment = $comment;
            $this->line_items[$line_no]->standard_cost = $standard_cost;
            $this->line_items[$line_no]->line_comment = $line_comment;
            $this->line_items[$line_no]->required_date = $required_date;
            $this->line_items[$line_no]->requested_qty = $requested_qty;
            $this->line_items[$line_no]->estimate_price = $estimate_price;

            $this->line_items[$line_no]->tran_qty = $tran_qty;
            $this->line_items[$line_no]->tran_unit = $tran_unit;
            $this->line_items[$line_no]->convertion_factor = $convertion_factor;
        }
    
	function remove_from_cart($line_no)
	{
		array_splice($this->line_items, $line_no, 1);
	}

	function count_items()
	{
		return count($this->line_items);
	}

	function check_qoh($location, $date_, $reverse=false)
	{
		foreach ($this->line_items as $line_no => $line_item)
		{
			$item_ret = $line_item->check_qoh($location, $date_, $reverse);
			if ($item_ret != null)
				return $line_no;
		}
		return -1;
	}

	// ----------- GL item functions

	function add_gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
	{
		if (isset($code_id) && $code_id != "" && isset($amount) && isset($dimension_id)  &&
			isset($dimension2_id))
		{
			$this->gl_items[] = new gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description);
			return true;
		}
		else
		{
			// shouldn't come here under normal circumstances
			display_error("unexpected - invalid parameters in add_gl_item($code_id, $dimension_id, $dimension2_id, $amount,...)", "", true);
		}

		return false;
	}

	function update_gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
	{
	    $this->gl_items[$index]->code_id = $code_id;
		$this->gl_items[$index]->dimension_id = $dimension_id;
		$this->gl_items[$index]->dimension2_id = $dimension2_id;
		$this->gl_items[$index]->amount = $amount;
		$this->gl_items[$index]->reference = $reference;
		if ($description == null)
			$this->gl_items[$index]->description = get_gl_account_name($code_id);
		else
			$this->gl_items[$index]->description = $description;
	}

	function remove_gl_item($index)
	{
		array_splice($this->gl_items, $index, 1);
	}

	function count_gl_items()
	{
		return count($this->gl_items);
	}

	function gl_items_total()
	{
		$total = 0;
		foreach ($this->gl_items as $gl_item)
			$total += $gl_item->amount;
		return $total;
	}

	function gl_items_total_debit()
	{
		$total = 0;
		foreach ($this->gl_items as $gl_item)
		{
			if ($gl_item->amount > 0)
				$total += $gl_item->amount;
		}
		return $total;
	}

	function gl_items_total_credit()
	{
		$total = 0;
		foreach ($this->gl_items as $gl_item)
		{
			if ($gl_item->amount < 0)
				$total += $gl_item->amount;
		}
		return $total;
	}

	// ------------ common functions

	function clear_items()
	{
    	unset($this->line_items);
		$this->line_items = array();

    	unset($this->gl_items);
		$this->gl_items = array();

	}
    
    function get_stock_items()
	{
		global $sort_sales_items;

		$items = $this->line_items;
		if (@$sort_sales_items)
			uasort($items, array($this, '_cmp_lines'));

		return $items;
	}
    
    function get_line_ref()
    {
        return $this->current_line_ref;
    }
    
    function set_next_line_ref()
    {
        $this->current_line_ref = $this->current_line_ref + 1;
        return $this->current_line_ref;
    }

}

//--------------------------------------------------------------------------------------------

class line_item
{
	var $stock_id;
	var $item_description;
	var $units;
	var $mb_flag;

	var $quantity;
	var $price;
	var $standard_cost;
    var $required_date;
    var $line_comment;
    var $row_id;
    var $issued_qty;
    var $requested_qty;
    var $stock_category_id;
    var $requesting_location;
    var $line_complete=0;
    var $created_by;
    var $created_on;
    var $comment;
    var $task_id;
    var $task_description;
    var $customer_currency;
    var $price_factor;
    var $sales_type;
    var $plant_code;
    var $job_code;
    var $status_id;
    var $category_id;
    var $po_line_id;
    var $order_quantity;
    var $order_price;
    var $required_from_date;
    var $required_to_date;
    var $issuing_date;
    var $asset_id;
    var $check;
    var $convertion_factor;
    var $alternate_unit_name;
    var $high_value_item;
    var $sample_item;
    var $reference_cost;
    var $lot_details;
    var $stock_control_method;
    var $project_site_id;
    var $project_site_description;
    var $descr_editable;
    var $target_line_id = 0;
    var $tran_qty;
    var $tran_unit;
    var $qty;
    var $estimate_price;
    var $pr_line_id;
    var $check_issue_item;
    var $line_ref;
    var $ordered_qty;
    var $returned_qty;
    var $requested_unit_quantity;
    var $task_name;
    var $quantity_ordered;
    var $requested_unit;
    var $requested_unit_qty;
    var $req_unit_conv_factor;
    var $po_quantity;
    var $po_quantity_received;
    var $requisition_id;
    var $pr_po_line_id;
    var $charging_cost;
    var $cost_source;
    var $item_selected;
    var $tran_unit_conv_factor;

    	
    function __construct($stock_id, $qty, $standard_cost=null, $description=null, $required_date = null, 
                $line_comment=null, $comment = null, $row_id = null, $issued_qty = 0, $requested_qty = null,
                $stock_category_id = 0, $price = 0, $requesting_location = null, $status_id = 0,
                $required_from_date = null, $required_to_date = null, $issuing_date=null, $asset_id=null, 
                $check=null, $tran_unit = null, $tran_qty = null, $convertion_factor = null,
                $created_by=null, $created_on=null, $project_site_id=null, $task_id=0){ 

        $item_row = get_item($stock_id);

        if ($item_row == null){
                display_error("invalid item added to order : $stock_id", "");
        }

        $this->mb_flag = $item_row["mb_flag"];
        $this->units = $item_row["units"];

        $this->tran_qty = $tran_qty;
        if($tran_unit == ""){
            $this->convertion_factor = 1;
            $this->tran_unit = $item_row["units"];
            $this->quantity = $tran_qty;
        }
        else{
            $this->convertion_factor = $convertion_factor;
            $this->tran_unit = $tran_unit;
            $this->quantity = $tran_qty*$convertion_factor;
        }
        $alternate_unit = get_unit_name($this->tran_unit);
        $this->alternate_unit_name = $alternate_unit['name'];

        if (isset($description) && strlen($description) > 0){
            $this->item_description = $description;
        }
	else{
            $this->item_description = $item_row["description"];
        }

        $this->descr_editable = $item_row["editable"];
        
        if ($standard_cost == null){
                $this->standard_cost = $item_row["material_cost"] + $item_row["labour_cost"] + 
                                                $item_row["overhead_cost"];
        }
        else{
                $this->standard_cost = $standard_cost;
        }

        $this->stock_id = $stock_id;

        $this->price = $price;
               
        //$this->price = 0;

        if ($required_date == null){
            $required_date = date('Y-m-d');
        }
        else{
            $this->required_date = $required_date;
        }

        $this->line_comment = $line_comment;

        $this->row_id = $row_id;

        $this->issued_qty = $issued_qty;

        $this->requested_qty = $requested_qty;
        $this->stock_category_id = $stock_category_id;
        $this->requesting_location = $requesting_location;
        $this->comment = $comment;
        $this->row_id = $row_id;
        $this->status_id = $status_id;
        $this->estimate_price = $price;
        $this->qty = $requested_qty;
        $this->required_from_date = $required_from_date;
        $this->required_to_date = $required_to_date;
        $this->issuing_date = $issuing_date;
        $this->asset_id = $asset_id;
        $this->check = $check;
        $this->created_by = $created_by;
        $this->created_on = $created_on;
        $this->project_site_id = $project_site_id;
        $this->task_id = $task_id;
    }

    function check_qoh($location, $date_, $reverse)
    {
		global $SysPrefs;
		
    	if (!$SysPrefs->allow_negative_stock())
    	{
			if (has_stock_holding($this->mb_flag))
			{
				$quantity = $this->quantity;
				if ($reverse)
					$quantity = -$this->quantity;

				if ($quantity >= 0)
					return null;

				$qoh = get_qoh_on_date($this->stock_id, $location, $date_);
        		if ($quantity + $qoh < 0)
        		{
        			return $this;
        		}
    		}
    	}

    	return null;
    }
}

//---------------------------------------------------------------------------------------

class gl_item
{

	var $code_id;
	var $dimension_id;
	var $dimension2_id;
	var $amount;
	var $reference;
	var $description;

	function __construct($code_id, $dimension_id, $dimension2_id, $amount, $reference,
		$description=null)
	{
		//echo "adding $index, $code_id, $dimension_id, $amount, $reference<br>";

		if ($description == null)
			$this->description = get_gl_account_name($code_id);
		else
			$this->description = $description;

		$this->code_id = $code_id;
		$this->dimension_id = $dimension_id;
		$this->dimension2_id = $dimension2_id;
		$this->amount = $amount;
		$this->reference = $reference;
	}
}

//---------------------------------------------------------------------------------------

?>
