<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_UOM';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Material Groups"));

include_once($path_to_root . "/includes/ui.inc");

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

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

function can_process_material_group($code, $description, $selected_id)
{    
    $code_msg = check_material_group_code_exists($code, $selected_id);    
    if($code_msg == true){
        $message = "Entered code already exists.";
        $set_focus = 'code';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    $description_msg = check_material_group_description_exists($description, $selected_id);    
    if($description_msg == true){
        $message = "Entered description already exists.";
        $set_focus = 'description';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if($_POST['unit'] == '0' || $_POST['unit'] == ''){
        $message = "Please select an unit.";
        $set_focus = 'unit';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    return array("success" => true, "message" => "");    

}
 
 
function save_material_group($selected_id)
{
    if($selected_id > 0){
        update_material_group($selected_id, $_POST['code'], $_POST['description'],
                                $_POST['long_description'], $_POST['unit'], input_num('cost'));           

    }else{
        add_material_group($_POST['code'], $_POST['description'],
                                $_POST['long_description'], $_POST['unit'], input_num('cost'));
    }
    
} 

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


function display_material_groups(){
         
    $result = get_all_material_groups();
     
    start_table(TABLESTYLE, "width=60%");
    $th = array(_('Code'), _('Description'), _('Long Description'), _('Unit'), _("Cost"), _("Calculated On"), "", "");

    table_header($th);
    $k = 0; //row colour counter

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

        label_cell($myrow['code']);
        label_cell($myrow['description']);
        label_cell($myrow['long_description']);
        label_cell($myrow['unit_name']);
        label_cell($myrow['cost'], "align=right");
        label_cell($myrow['created_on'], "align=right");

        edit_button_cell("Edit".$myrow['id'], _("Edit"));
        delete_button_cell("Delete".$myrow['id'], _("Delete"));
            
    }
    
    end_table(1);     
}



function display_material_groups_edit_controls($selected_id, &$Mode){
 
    if ($selected_id > 0){

            $myrow = get_material_group_item($selected_id);

            $_POST['code'] = $myrow["code"];
            $_POST['description'] = $myrow["description"];
            $_POST['long_description'] = $myrow["long_description"];
            $_POST['unit'] = $myrow["unit"];
            $_POST['cost'] = $myrow["cost"]; 
            
//            hidden('selected_id', $myrow["id"]);
    }     
    else{
        $_POST['code'] = "";
            $_POST['description'] = "";
            $_POST['long_description'] = "";
            $_POST['unit'] = "";
            $_POST['cost'] = 0; 
    }
    
    hidden('selected_id', $selected_id );
     
   
    start_table(TABLESTYLE2);
    
    text_row("Code:", 'code', null, 20, 100);

    text_row("Description:", 'description', null, 50, 100);

    textarea_row("Long Description:", 'long_description', null, 40, 2);

    stock_units_list_row("Unit:", "unit", null, true, 'Please select an Unit', false);

    amount_row("Cost:", "cost");
    
    end_table(1);
   
}


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


if(isset($_POST['Recalculate_costs'])){
    $result = get_all_material_groups();
    
    while($mg_row = db_fetch($result)){
        
        $mg_details = get_material_group_average_cost($mg_row['id']);
      
        if(isset($mg_details['id']) && $mg_details['id'] > 0){
            update_material_group_cost($mg_details['id'], $mg_details['as_material_group_cost']);
        }
        
    }
}



if(isset($_POST['Add_Material_Groups']) || isset($_POST['Update_Material_Groups'])){
    
    $msg = can_process_material_group($_POST['code'], $_POST['description'], $selected_id);
     
    if($msg['success'] !== true){
       display_error($msg['message']);
       return $msg;
    }
    else{
        save_material_group($selected_id);    
        $selected_id = -1;
    }
}


if ($Mode == 'Delete')
{	
//    delete_material_group_item($selected_id);
     $selected_id = -1;
}


if (isset($_POST['cancel']))
{
    $selected_id = -1;
//    unset($_POST);
    $Mode = 'RESET'; 
}


start_form();
  
global $Ajax;

$Ajax->activate('material_group');

div_start('material_group');
display_material_groups();


submit_center_first('Recalculate_costs', _("Recalculate Costs"), '', 'default');
br();
br();
    
display_material_groups_edit_controls($selected_id, $Mode);



if($selected_id > 0){
    submit_center_first('Update_Material_Groups', _("UPDATE"), '', 'default');    
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
}else{
    submit_center_first('Add_Material_Groups', _("ADD"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
}

div_end();
end_form();

end_page();



?>
