<?php

/**********************************************************************
***********************************************************************/
function can_process_lot($info)
{
    if($info['stock_control_method'] != SKCM_LOTS){
        $message = (_("This item is not a lot controlled item."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('lot_reference');
        }
        return array("success" => false, "message" => $message);
    }
    
    if (strlen($info['lot_reference']) == 0)
    {
        $message = (_("Lot reference cannot be empty."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('lot_reference');
        }
        return array("success" => false, "message" => $message);
    }

    $existing_lot = get_existing_lot_reference($info['stock_id'], $info['lot_reference']);
    if($info['Mode'] == "ADD_ITEM"){
        if($existing_lot){
            $message = ("Entered lot reference [".$info['lot_reference']."] is already in the lots for item no ".$info['stock_id'].".");
            if($info['single_item'] == 1){
                display_error($message);
                set_focus('lot_reference');
            }
            return array("success" => false, "message" => $message);
        }
    }
    else{
        if($existing_lot && $existing_lot['id'] != $info['lot_id']){
            $message = ("Entered lot reference [".$info['lot_reference']."] is already in the lots for item no ".$info['stock_id'].".");
            if($info['single_item'] == 1){
                display_error($message);
                set_focus('lot_reference');
            }
            return array("success" => false, "message" => $message);
        }
    }
    
    return array("success" => true, "message" => "");
}


function can_process_lot_adjustment(&$info)
{
    if($info['stock_control_method'] != SKCM_LOTS){
        $message = (_("This item is not a lot controlled item."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('stock_id');
        }
        return array("success" => false, "message" => $message);
    }
    
    if(!(isset($info['lot_id']) && $info['lot_id'] > 0)){
        $message = ("Select a lot for the item.");
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('lot_id');
        }
        return array("success" => false, "message" => $message);
    }
        
    if ($info['tran_reference'] == "") 
	{
        $message = (_("Enter a reference."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('tran_reference');
        }
        return array("success" => false, "message" => $message);
	}
    
    if ($info['location'] == "") 
	{
        $message = (_("Select a location."));	
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('location');
        }
        return array("success" => false, "message" => $message);
	} 
        
	if (!is_date($info['tran_date'])) 
	{
        $message = (_("The entered date is invalid."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('tran_date');
        }
        return array("success" => false, "message" => $message);
		
	} 
        
    if($info['type'] == '' ){
        $message = ( _("Select a type."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('type');
        }
        return array("success" => false, "message" => $message);

    }
    
    if(!is_numeric($info['tran_qty'])){
        $message = ( _("Entered quantity is not numeric."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('qty');
        }
        return array("success" => false, "message" => $message);
    }

    if (!($info['tran_qty'] >= 0.01))
	{
		$message = (_("The quantity entered must be positive."));
		if($info['single_item'] == 1){
            display_error($message);
            set_focus('type');
        }
        return array("success" => false, "message" => $message);
    }
    
    return array("success" => true, "message" => "");
}



function add_lot_transactions(&$lot_details, $tran_type, $tran_no, $location, 
                            $stock_id, $convertion_factor, $tran_date, $increase, 
                            $reference = '')
{
    if($lot_details == null){
        return array("success" => false, "tran_no" => 0, "message" => "No lot details found");
    }
    
    //$tran_date = date2sql($tran_date);
    
    foreach ($lot_details as &$lot_item)
    {
        if($lot_item['tran_qty'] == 0){
            continue;
        }
        
        $lot_id = $lot_item['lot_id'];
        
        $tran_qty = $lot_item['tran_qty'] * $convertion_factor;
        if(!$increase){
            $tran_qty = -$tran_qty;
        }
        
        $row_state = $lot_item['row_state'];

        update_lot($lot_id, $row_state, $tran_qty);
        
        add_lot_adjustment($tran_no, $reference,$lot_id,
                            $stock_id,$tran_date, $tran_qty, 
                            $tran_type, $location);
            
        $lot_item['row_state'] = $row_state;
    }
    
    return array("success" => true, "tran_no" => $tran_no, "message" => "");
}
        
function validate_transaction_lot_quantities($item_details)
{
    foreach ($item_details as $item){
        if($item['stock_control_method'] == SKCM_LOTS){
            $villq_msg = validate_item_line_lot_quantities($stock_id, $tran_qty, $lot_details);
            if($villq_msg['success'] !== true){
                return $villq_msg;
            }
        }
    }
    
    return array("success" => true, "message" => "");
}

function validate_item_line_lot_quantities($line_ref, $stock_id, $tran_qty, $lot_details)
{
    $sum = 0;
    if($lot_details){
        foreach ($lot_details as $lot){
            if($line_ref ==  $lot['line_ref'] && $stock_id ==  $lot['stock_id']){
                $lot_tran_qty = $lot['tran_qty'];
                if($lot_tran_qty > 0){
                    $sum += $lot_tran_qty;
                }
            }
            else{
                return array("success" => false, "message" => "Lot details line reference [".$line_ref."] of stock item [".$stock_id."] does not match");
            }
        }
    }
    else{
        return array("success" => false, "message" => "No lot details found for Stock item [.$stock_id.]");
    }
    
    if($sum != $tran_qty){
        return array("success" => false, "message" => "Stock item [.$stock_id.] line quantity does not match with the total quantity entered in the lots.");
    }
    else{
        return array("success" => true, "message" => "");
    }
}
?>        