<?php

/**********************************************************************
***********************************************************************/
function can_process_serial_nos($info)
{            
    if($info['stock_control_method'] != 2){
        $message = (_("This item is not a serial number control item."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('serial_no');
        }
        return array("success" => false, "message" => $message);
    }
    
    if (strlen($info['serial_no']) == 0)
    {
        $message = (_("Serial number cannot be empty."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('serial_no');
        }
        return array("success" => false, "message" => $message);
    }

    $existing_serial_no = get_existing_serial_no($info['stock_id'], $info['serial_no']);
    if($info['Mode'] == "ADD_ITEM"){
        if($existing_serial_no){
            $message = ("Entered serial_no [".$info['serial_no']."] is already in the serial numbers for item no ".$info['stock_id'].".");
            if($info['single_item'] == 1){
                display_error($message);
                set_focus('serial_no');
            }
            return array("success" => false, "message" => $message);
        }
    }
    else{
        if($existing_serial_no && $existing_serial_no['id'] != $info['serial_no_id']){
            $message = ("Entered serial_no [".$info['serial_no']."] is already in the serial numbers for item no ".$info['stock_id'].".");
            if($info['single_item'] == 1){
                display_error($message);
                set_focus('serial_no');
            }
            return array("success" => false, "message" => $message);
        }
    }
    
    return array("success" => true, "message" => "");
}

        
function can_process_serial_no_adjustment(&$info)
{
    if($info['stock_control_method'] != SKCM_SERIAL_NOS){
        $message = (_("This item is not a serial no controled item."));
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('stock_id');
        }
        return array("success" => false, "message" => $message);
    }
    
    if(!(isset($info['serial_no_id']) && $info['serial_no_id'] > 0)){
        $message = ("Select a serial no for the item.");
        if($info['single_item'] == 1){
            display_error($message);
            set_focus('serial_no_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($info['type'] == 2){
        if($info['serial_no_qty'] == 1){
            $info['tran_qty'] = -1; 
        }
        else{
            $message = ( _("Serial no [".$info['serial_no']."] is not available in the stocks."));
            if($info['single_item'] == 1){
                display_error($message);
                set_focus('serial_no_id');
            }
            return array("success" => false, "message" => $message);
        }                       
    }
    else{
        if($info['serial_no_qty'] == 0){
            $info['tran_qty'] = 1;
        }
        else{
            $message = ( _("Serial no [".$info['serial_no']."] is already available in the stocks."));
            if($info['single_item'] == 1){
                display_error($message);
                set_focus('serial_no_id');
            }
            return array("success" => false, "message" => $message);
        }
    }
    
    return array("success" => true, "message" => "");
}

function save_serial_no_adjustment($info)
{
    begin_transaction();
    $usn = update_serial_no($info['serial_no_id'], $info['tran_qty'], $info['row_state']);
    if($usn !== 1){
        cancel_transaction();
        $message = ("Serial no [".$info['serial_no']."] has been updated by another user. Could not save the transaction.");
        return array("success" => false, "message" => $message);
    }
    else{       
        add_serial_no_adjustment($info['tran_no'], $info['tran_reference'],
                                $info['serial_no_id'],$info['stock_id'],
                                $info['tran_date'], $info['tran_qty'], 
                                $info['tran_type'], $info['location']);

        if(strlen($info['serial_no_tran_comment']) > 0){
            add_comments($info['tran_type'], $info['tran_no'], $info['tran_date'], $info['serial_no_tran_comment']);
        }
        
        commit_transaction();
        return array("success" => true, "message" => "");
    }
}

function add_serial_no_transactions(&$serial_no_details, $tran_type, $tran_no, $location, 
                            $stock_id, $tran_date, $increase, 
                            $reference = '')
{
    if($serial_no_details == null){
        return array("success" => false, "tran_no" => 0, "message" => "No serial no details found");
    }
    
    //$tran_date = date2sql($tran_date);
    
    foreach ($serial_no_details as &$serial_item)
    {
        if(strlen($serial_item['lot_reference']) == 0){
            continue;
        }
        
        $serial_no_id = $serial_item['lot_id'];
        $serial_no = $serial_item['lot_reference'];
        
        $tran_qty = $serial_item['tran_qty'];
        if(!$increase){
            $tran_qty = -$tran_qty;
        }
        
        $row_state = $serial_item['row_state'];

        $usn = update_serial_no($serial_no_id, $tran_qty, $row_state);
        if($usn == 1){
            add_serial_no_adjustment($tran_no, $reference, $serial_no_id, 
                                    $stock_id, $tran_date, $tran_qty, 
                                    $tran_type, $location);
            
            $serial_item['row_state'] = $row_state;
            
        }
        else{
            cancel_transaction();
            $message = "Serial no [".$serial_no."] has been updated by another user. Please reselect serial numbers for the stock item [".$stock_id."]";
            return array("success" => false, "tran_no" => $tran_no, "message" => $message);
        }
    }
    
    return array("success" => true, "tran_no" => $tran_no, "message" => "");
}
        

function validate_item_line_serial_no_quantities($stock_id, $tran_qty, $lot_details)
{
    $sum = 0;
    
    if($lot_details){
        foreach ($lot_details as $serial_no){
            if(strlen($serial_no['lot_reference']) > 0){
                $sum += 1;
            }
        }
    }
    else{
        return array("success" => false, "message" => "No serial number 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 serial numbers selected.");
    }
    else{
        return array("success" => true, "message" => "");
    }
}

?>        