<?php
/**********************************************************************
***********************************************************************/


function display_item_material_groups_import_file_data_array(&$file_data, $validate = false, $import = false)
{
    start_table(TABLESTYLE, "width=80%");    
    $th = array("Itemcode", "Material Group Code", "Conversion Factor", "Message");

                                   
    table_header($th);
    
    foreach ($file_data as &$line){    

        if($validate || $import){
            validate_material_group_details_import_data($line );
        }

        if($import && $line['success'] === true){
            save_import_material_group_details($line);
        }

        display_item_material_groups_import_array_line($line);   
    }
    
    end_table();
    
}
    
function save_import_material_group_details(&$line)
{
    
    $stock_id = $line['stock_id'];
    
    $item_material_group = get_material_group_record_by_code($line['material_group_code']);
    $material_group_id = $item_material_group['id'];
    
    $conversion_factor = $line['conversion_factor'];  

    $img = get_item_material_group_record($line['stock_id']);
       
//    return;
    
    if(isset($img['id']) && strlen($img['id']) > 0){
        update_item_material_group($stock_id, $material_group_id, $conversion_factor);
        $line['message'] = "Updated";
    }
    else{
        add_item_material_group($stock_id, $material_group_id, $conversion_factor);
        $line['message'] = "Added";
    }

}


function validate_material_group_details_import_data(&$line)
{
    $success = true;
    
    $stock_item = get_stock_item_edit_info($line['stock_id']);
    if(!(isset($stock_item['stock_id']) && strlen($stock_item['stock_id']) > 0 )){
        $m = "Unknown item code.";
        $line['stock_id'] = $line['stock_id']." <font color='red'>".$m."</font>";
        $success = false;
    }
    
    $material_group = get_material_group_record_by_code($line['material_group_code']);
    if(!(isset($material_group['code']) && strlen($material_group['code']) > 0 )){
        $m = "Unknown material group code.";
        $line['material_group_code'] = $line['material_group_code']." <font color='red'>".$m."</font>";
        $success = false;
    }
    
    
    $conversion_factor = input_num2($line['conversion_factor']);    
    if(!check_num2($conversion_factor, 0.0000000001)){
        $m = "Conversion factor should be numeric and grater than zero.";
        $line['conversion_factor'] = $line['conversion_factor']." <font color='red'>".$m."</font>";
        $success = false;
    }
    
    if($success === true){
        $line['success'] = true;
        $line['message'] = "Can import";
    }else{
        $line['success'] = false;
        $line['message'] .= "<font color='red'>Can not import</font>";
    }
    
}

function display_item_material_groups_import_array_line($row)
{
    $j = $k = 0;
  
    if($row == ''){
        return;
    }

    label_cell($row['stock_id']);
    label_cell($row['material_group_code']);
    label_cell(number_format(input_num2($row['conversion_factor']), 6), "align=right");
    label_cell($row['message']);

    end_row();

}



function read_item_material_groups_import_file_data(&$file_data, $line)
{
        $l = $line;

        $stock_id = str_replace('"', '', $l[0]);
        $material_group_code = str_replace('"', '', $l[1]);
        $conversion_factor = str_replace('"', '', $l[2]);                    
     
        add_to_item_material_groups_array($file_data, $stock_id, $material_group_code, $conversion_factor);                                                             

}

function add_to_item_material_groups_array(&$file_data, $stock_id, $material_group_code, $conversion_factor)
{
    $file_data_line = array("stock_id" => $stock_id, 
                            "material_group_code" => $material_group_code, 
                            "conversion_factor" => $conversion_factor, 
                            "message" => '');
                                              
    $file_data[] = $file_data_line;
}



?>
