<?php


function get_items_from_import_item_standard_cost_table(){
    $sql = "SELECT * FROM  import_item_standard_cost";
    return db_query($sql,"Could not get_items_from_import_item_standard_cost_table");
}

function  update_system_item_costs($stock_id, $material_cost, $labour_cost, $overhead_cost)
{
    $sql = "UPDATE ".TB_PREF."stock_master 
            SET material_cost =".db_escape($material_cost). ",
                labour_cost =".db_escape($labour_cost). ", 
                overhead_cost =".db_escape($overhead_cost). " 
            WHERE stock_id=".db_escape($stock_id);
    
    db_query($sql,"Could not update_system_item_costs");
}

function  write_import_item_material_cost_message($id, $message)
{
    $sql = "UPDATE import_item_standard_cost 
            SET message =".db_escape($message);
    
    if($id > 0){
        $sql .= " WHERE id=".db_escape($id);
    }
    
    db_query($sql,"Could not write_import_item_material_cost_message");
}

function import_system_item_material_cost()
{
    write_import_item_material_cost_message(-1, '');

    $costs = get_items_from_import_item_standard_cost_table();
    while ($row = db_fetch($costs)) {
        $sys_itm = get_item($row['stock_id']);
        if(!$sys_itm){
            $message = "Itemcode not found";
            write_import_item_material_cost_message($row['id'], $message);
            continue;
        }
        
        $material_cost = $labour_cost = $overhead_cost = 0;
        
        $unit = get_unit_abbrivation($row['unit_name']);
        if(!(isset($unit['abbr']))){
            $message = "Unknown unit";
            write_import_item_material_cost_message($row['id'], $message);
            continue;
        }
        
        $alternate_unit = get_alternate_unit_info($row['stock_id'], $unit['abbr']);
        if(!(isset($alternate_unit['id']))){
            $message = "Unit not defined for the item";
            write_import_item_material_cost_message($row['id'], $message);
            continue;
        }
        

        $alternate_unit_abbr = $alternate_unit['abbr'];
        $convesion_factor = 1;
        get_convertion_factor2($row['stock_id'], $alternate_unit_abbr, $convesion_factor);
        $material_cost = input_num2($row['material_cost']) / $convesion_factor;
        $labour_cost = input_num2($row['labour_cost']) / $convesion_factor;
        $overhead_cost = input_num2($row['overhead_cost']) / $convesion_factor;
        

        if($sys_itm['material_cost'] == $material_cost && 
                $sys_itm['labour_cost'] == $labour_cost && 
                    $sys_itm['overhead_cost'] == $overhead_cost){
            $message = "No cost change";
            write_import_item_material_cost_message($row['id'], $message);
            continue;
        }
        

        if($sys_itm['mb_flag'] == 'M'){

        }
        else{
            if($labour_cost > 0){
                $message = "Not a Manufactured item. Labour cost should be zero";
                write_import_item_material_cost_message($row['id'], $message);
                continue;
            }
            if($overhead_cost > 0){
                $message = "Not a Manufactured item. Overhead cost should be zero";
                write_import_item_material_cost_message($row['id'], $message);
                continue;
            }
        }
            
        update_system_item_costs($sys_itm['stock_id'], input_num2($row['material_cost']), $labour_cost, $overhead_cost);     
        $num_recs = db_num_affected_rows();
        if($num_recs == 1){
            $message = "Updated";
        }
        else{
            $message = "$num_recs Records updated";
        }
        
        write_import_item_material_cost_message($row['id'], $message);
  
    }
}

?>