<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ITEM';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

//include($path_to_root . "/includes/session.inc");

include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/inventory/includes/db/item_material_groups_db.inc");       
include_once($path_to_root . "/inventory/includes/db/material_groups_db.inc");


$js = '';

if (user_use_date_picker()) {
        $js .= get_js_date_picker();
}

if (!@$_GET['popup'])
        page(_($help_context = "Material Groups"),false,false,"",$js);

simple_page_mode(true);
//----------------------------------------------------------------------------------

if (isset($_GET['stock_id']))
{
    $_POST['stock_id'] = $_GET['stock_id'];
}
 
//----------------------------------------------------------------------------------


function save_item_material_group($stock_id, $material_group_id, $conversion_factor)
{
    $img_rec = get_item_material_group_record($stock_id);
      
    if($img_rec['id'] > 0){     
        update_item_material_group($stock_id, $material_group_id, $conversion_factor);
    }else{
        add_item_material_group($stock_id, $material_group_id, $conversion_factor);
    }
}


function display_material_groups(){
         
    $material_group_id = 0;
    $conversion_factor_to_stock_item_base_unit = 1;

    if(list_updated('stock_id')){
        unset($_POST['material_group_id']);
    }
    
    $img_row = get_item_material_group($_POST['stock_id']);
       
    start_table(TABLESTYLE, "width=80%");
    $th = array(_('Material Group'), _('Material Group Qty'), _('Material Group Unit'), _(""), 
        _("Stock Item Base Unit Conversion Factor"),_("Stock Item Base Unit"));

    table_header($th);
    start_row();
    
    if(isset($img_row['id']) && $img_row['id'] > 0){
        $material_group_id = $img_row['material_group_id'];
        $conversion_factor_to_stock_item_base_unit = $img_row['conversion_factor'];
    }
    
    if(!(isset($_POST['material_group_id']) && $_POST['material_group_id'] > 0)){
        $_POST['material_group_id'] = $material_group_id;
    }
    
    if(!(isset($_POST['conversion_factor_to_stock_item_base_unit']) && $_POST['conversion_factor_to_stock_item_base_unit'] > 0)){
        $_POST['conversion_factor_to_stock_item_base_unit'] = $conversion_factor_to_stock_item_base_unit;
    }
    
    material_groups_list_cells(null, 'material_group_id', null, false, true);

    $mg_row = get_material_group_record($_POST['material_group_id']);    

    $material_group_unit_name = get_unit_descr($mg_row['unit']);

    if(list_updated('material_group_id')){
        $_POST['conversion_factor_to_stock_item_base_unit'] = 1;
    }
        label_cell('1.000', "align=center");
        label_cell($material_group_unit_name, "align=center");
        label_cell('=', "align=center");       
        
//        amount_cells(null, 'stock_item_base_unit_conversion_factor');
        amount_cells(null, 'conversion_factor_to_stock_item_base_unit', null, null, null, 6);
        
        label_cell($img_row['stock_unit_name'], "align=center");
            
    end_row();
    end_table(1);     
}


function display_other_items_in_selected_material_group()
{    
    display_heading2("Other Items in This Material Group");
    
    
    $result = get_material_group_stock_items($_POST['material_group_id']);
    
    start_table(TABLESTYLE, "width=60%");
    
    $th = array(_('Code'), _('Description'), _('Unit'), _('Conversion Factor'));
    
    table_header($th);
    
    $k=0;
    while($row = db_fetch($result)){
        
        alt_table_row_color($k);
        
        label_cell($row['stock_id']);
        label_cell($row['stock_item_description']);
        label_cell($row['stock_item_unit_name'], "align=center");
        label_cell(number_format($row['conversion_factor'],6), "align=right");    
            
        end_row();
    }
    
    end_table(1); 
    
}

if(isset($_POST['Update_Item_Material_Groups'])){
    save_item_material_group($_POST['stock_id'], $_POST['material_group_id'], $_POST['conversion_factor_to_stock_item_base_unit']);
}

//----------------------------------------------------------------------------------

start_form();


global $Ajax;

$Ajax->activate('material_group');

div_start('material_group');

br();

display_material_groups();

submit_center_first('Update_Item_Material_Groups', _("Update"), '', 'default');

br();
br();

display_other_items_in_selected_material_group();

if (!@$_GET['popup'])
{
    end_form();
    end_page(@$_GET['popup'], false, false);
}	



?>
