<?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 site_material_returns_cart {
    var $trans_no = 0;
    var $trans_type = ST_SITE_MATERIAL_RETURN;
    var $line_items;
    var $order_id;
    var $from_location;
    var $to_location;
    var $to_location_name;
    var $tran_date;
    var $person_id;
    var $reference;
    var $comment;
    var $customer_currency;
    var $from_project_id;
    var $to_project_id;
    var $from_project_description;
    var $to_project_description;
    var $row_state = 0;
    var $current_line_ref = 10000;
    var $id;
    var $company_code;
    var $employee_code;
    var $status;

    var $company_name;
    var $employee_name;
    var $job_reference;
	
	function __construct($type, $order_no = 0){
            if($order_no > 0){
                $this->read_existing_site_material_return($order_no);
            }else{
		$this->trans_type = $type;
		$this->clear_site_material_return_items();
            }
	}

	// --------------- line item functions

        function add_site_material_return_to_cart($line_no, $stock_id, $qty, $standard_cost, $tran_unit=null, $tran_qty=null, $conversion_factor=null, 
                                                  $from_project_site_id=null, $to_project_site_id=null, $from_task_id=null, $to_task_id=null, $user_entered_tran_unit_cost=0, 
                                                  $pri_line_id=null, $submitted_item_array_index=-1){
            
            if(isset($stock_id) && $stock_id != "" && isset($tran_qty) && $tran_qty > 0){
                $this->line_items[$line_no] = new site_material_returns_line_item(0, $submitted_item_array_index, $stock_id, $qty, $standard_cost, $tran_unit, $tran_qty, 
                                                                                  $conversion_factor, $from_project_site_id, $to_project_site_id, $from_task_id, $to_task_id, 
                                                                                  null, null, $user_entered_tran_unit_cost, $pri_line_id);
                return true;
            }else{
                if($stock_id == ""){
                    $message = "New line Item can not be empty. please select an item.";
                }else{
                    $message = "New line Quantity should be greater than zero.";
                }
                display_error($message, "", true);
            }

            return false;
	}
        
        function read_existing_site_material_return($trans_no){
            $sah = get_site_material_return_header($trans_no);
            $this->trans_no = $trans_no;
            $this->tran_date = sql2date($sah['tran_date']);
            $this->reference = $sah['reference'];
            $this->to_location = $sah['to_location'];
            $this->to_location_name = $sah['to_location_name'];
            $this->from_project_id = $sah['from_project_id'];
            $this->to_project_id = $sah['to_project_id'];
            $this->comment = $sah['comments'];
            $this->row_state = $sah['row_state'];
            $this->id = $sah['id'];
            $this->company_code = $sah['company_code'];
            $this->employee_code = $sah['employee_code'];
            $this->status = $sah['status'];
            $company_name = get_group_company_name($sah['company_code']);
            $this->company_name = $company_name;
            $employee_name = get_group_company_employee($sah['company_code'], $sah['employee_code']);
            $this->employee_name = $employee_name['employee_code'].' - '.$employee_name['initials'].' '.$employee_name['last_name'];
            
            $result = get_site_material_return_details($trans_no);
            while($row = db_fetch($result)){

                $line = new site_material_returns_line_item($row['id'], -1, $row['stock_id'], $row['qty'], $row['standard_cost'], $row['tran_unit'], 
                                                            $row['tran_qty'], $row['convertion_factor'], $row['from_project_site_id'], $row['to_project_site_id'], 
                                                            $row['from_task_id'], $row['to_task_id'], null, null, $row['tran_unit_cost'], $row['pri_line_id']);
                                        
                $this->line_items[] = $line;
                $current_element = (count($this->line_items)-1);
                $this->line_items[$current_element]->from_task_description = $row['from_task_description'];
                $this->line_items[$current_element]->to_task_description = $row['to_task_description'];
                $this->line_items[$current_element]->from_project_site_description = $row['from_project_site_description'];                
                $this->line_items[$current_element]->to_project_site_description = $row['to_project_site_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_site_material_return_cart_item($line_no, $qty, $standard_cost, $tran_unit=null, $tran_qty=null, $conversion_factor=null, $from_project_site_id=0, 
                                                       $to_project_site_id=0, $from_task_id=0, $to_task_id=0, $user_entered_tran_unit_cost=0, $pri_line_id=0){
            
            if($tran_unit == null || $tran_unit == ""){
                $item = get_item_edit_info($this->line_items[$line_no]->stock_id);
                $tran_unit = $item['units'];
                $conversion_factor = 1;
                $qty = $tran_qty;
            }
            
            if($qty == 0){
                $this->line_items[$line_no]->quantity = $tran_qty * $conversion_factor;
            }

            $this->line_items[$line_no]->quantity = $qty;
            $this->line_items[$line_no]->standard_cost = $standard_cost;
            $this->line_items[$line_no]->tran_unit = $tran_unit;
            $this->line_items[$line_no]->tran_qty = $tran_qty;
            $this->line_items[$line_no]->conversion_factor = $conversion_factor;
            $this->line_items[$line_no]->from_project_site_id = $from_project_site_id;
            $this->line_items[$line_no]->to_project_site_id = $to_project_site_id;
            $this->line_items[$line_no]->from_task_id = $from_task_id;
            $this->line_items[$line_no]->to_task_id = $to_task_id;
            $this->line_items[$line_no]->user_entered_tran_unit_cost = $user_entered_tran_unit_cost;
            $this->line_items[$line_no]->pri_line_id = $pri_line_id;
            $submitted_item_array_index = find_submit('Edit');
            $this->line_items[$line_no]->submitted_item_array_index = $submitted_item_array_index;
	}
    
	function remove_from_cart($line_no){
            array_splice($this->line_items, $line_no, 1);
	}

	function count_items(){
            if (is_array($this->line_items)) {
                return count($this->line_items);
            } else {
                return 0;
            }
	}

	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_site_material_return_items(){
            unset($this->line_items);
            $this->line_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 site_material_returns_line_item {
    var $row_id = 0;
    var $stock_id;
    var $item_description;
    var $units;
    var $mb_flag;
    var $quantity;
    var $standard_cost;
    var $from_task_id;
    var $to_task_id;
    var $from_task_description;
    var $to_task_description;
    var $customer_currency;
    var $conversion_factor;
    var $alternate_unit_name;
    var $reference_cost;
    var $from_project_site_id;
    var $to_project_site_id;
    var $from_project_site_description;
    var $to_project_site_description;
    var $descr_editable;
    var $stock_control_method;
    var $lot_details;
    var $line_ref=null;
    var $user_entered_unit_cost = 0;
    var $pri_line_id = 0;
    var $submitted_item_array_index = -1;
    var $tran_qty;
    var $tran_unit;
    var $user_entered_tran_unit_cost;

    function __construct($row_id, $submitted_item_array_index, $stock_id, $qty, $standard_cost=null, $tran_unit = null, $tran_qty = null, 
                                             $conversion_factor = null, $from_project_site_id=null, $to_project_site_id=null, $from_task_id=null, $to_task_id=null, 
                                             $stock_control_method=null, $lot_details=null, $user_entered_tran_unit_cost=0, $pri_line_id=null){ 
        $this->row_id = $row_id;
        $item_row = get_item($stock_id);

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

        $this->tran_qty = $tran_qty;
        if($tran_unit == ""){
            $this->conversion_factor = 1;
            $this->tran_unit = $item_row["units"];
            $this->quantity = $tran_qty;
        }else{
            $this->conversion_factor = $conversion_factor;
            $this->tran_unit = $tran_unit;
            $this->quantity = $tran_qty*$conversion_factor;
        }
        
        $alternate_unit = get_unit_name($this->tran_unit);
        $this->alternate_unit_name = $alternate_unit['name'];
        $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->stock_control_method = $stock_control_method;
        $this->lot_details = $lot_details;
        
        $item_info = get_stock_item_edit_info($stock_id);
        $this->reference_cost = $item_info["reference_cost"];
   
        $from_task_description = '';
        if(isset($from_task_id) && $from_task_id > 0 && !($submitted_item_array_index > -1)){
            $from_project_task = get_task_details($from_task_id);
            $from_task_id = $from_project_task['task_id'];
            $from_task_description = $from_project_task['description'];
        }
        $this->from_task_id = $from_task_id;
        $this->from_task_description = $from_task_description;

        $to_task_description = '';
        if(isset($to_task_id) && $to_task_id > 0 && !($submitted_item_array_index > -1)){
            $to_project_task = get_task_details($to_task_id);
            $to_task_id = $to_project_task['task_id'];
            $to_task_description = $to_project_task['description'];
        }
        $this->to_task_id = $to_task_id;
        $this->to_task_description = $to_task_description;

        $from_project_site_description = '';
        if(isset($from_project_site_id) && $from_project_site_id > 0 && !($submitted_item_array_index > -1)){
            $from_project_site = get_project_site_details($from_project_site_id);
            $from_project_site_id = $from_project_site['id'];
            $from_project_site_description = $from_project_site['description'];
        }
        $this->from_project_site_id = $from_project_site_id;
        $this->from_project_site_description = $from_project_site_description;

        $to_project_site_description = '';
        if(isset($to_project_site_id) && $to_project_site_id > 0 && !($submitted_item_array_index > -1)){
            $to_project_site = get_project_site_details($to_project_site_id);
            $to_project_site_id = $to_project_site['id'];
            $to_project_site_description = $to_project_site['description'];
        }
        $this->to_project_site_id = $to_project_site_id;
        $this->to_project_site_description = $to_project_site_description;
        
        $this->user_entered_tran_unit_cost = $user_entered_tran_unit_cost;
        $this->pri_line_id = $pri_line_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;
    }
}

?>