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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
//include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/projects/includes/db/project_approval_path_db.inc");
$js = '';

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

if (!@$_GET['popup'])
    
        page(_($help_context = "Inventory Item Sales prices"),false,false,"",$js);

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

check_db_has_projects(_("There are no projects defined in the system."));

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;

if (isset($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}


//---------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
	start_form();

	br(2);
//set_global_stock_item($_POST['project_id']);
function clear_site_data()
{
	unset($_POST['approval_path_type']);
	unset($_POST['approval_path_id']);
	
	
}

function get_approval_path_info($project_id,$approval_path_type)
{
     
     $sql = "SELECT pu.id,
                        pu.approval_path_type,
                        pu.approval_path_id                      
                FROM ".TB_PREF."project_approval_paths pu,                        
                        ".TB_PREF."projects_master pm
                    WHERE pu.project_id = ".db_escape($project_id). "
                           AND pu.project_id = pm.project_id AND pu.approval_path_type=".db_escape($approval_path_type);
    return  db_query($sql,"Approval path details could not be retreived");
}

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

if ($Mode=='ADD_ITEM') 
{
    
//    $result = get_approval_path_info($project_id,$_POST['approval_path_type']);  
//   
//    $myrow = db_fetch($result);
    $input_error =0;
    
    if ($_POST['approval_path_id']!='-1' && $_POST['approval_path_type']!='-1')
    {  
        $result = get_approval_path_info($project_id,$_POST['approval_path_type']);  
        $myrow = db_fetch($result);
        if($myrow !='')
        { 
            $input_error = 1;
            display_error( _("The approval path is already entered."));
            set_focus('approval_path_type');
        }
    }
   
    if($_POST['approval_path_type']=='-1')
    {
        $input_error = 1;
        display_error( _("Please select the approval path type."));
        set_focus('approval_path_id');

    }
    if($_POST['approval_path_id']=='-1')
    {
         $input_error = 1;
         display_error( _("Please select the approval path."));
         set_focus('approval_path_id');

    }
  
    if ($input_error == 0){
        add_project_approval_path($project_id, $_POST['approval_path_type'],$_POST['approval_path_id']);            
        $msg = _("New project approval path has been added.");

        clear_site_data();
        $msg = _("Project approval path has been added.");

        display_notification($msg);
        $Mode = 'RESET';      
    }
     
     
   
           

}
if ($Mode=='UPDATE_ITEM') 
{
    $input_error = 0;
    
    if($_POST['approval_path_id']== '-1'){
        $input_error = 1;
        display_error( _("Please select the approval path."));
        set_focus('approval_path_id');
    }
   
   if($input_error == 0){
        update_project_approval_path($selected_id,$_POST['approval_path_id']); 
   }
        clear_site_data();
        $msg = _("Approval path details has been updated.");
        
        display_notification($msg);
        $Mode = 'RESET';
}
//------------------------------------------------------------------------------------------------------

//if ($Mode == 'Delete')
//{
//	//the link to delete a selected record was clicked
//	delete_site($_POST['project_id'], $selected_id);
//	display_notification(_("The selected task has been deleted."));
//	$Mode = 'RESET';
//}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

//--------------------------------------------------------------------------------------------------------
//$tasks = get_task($_POST['project_id']);
$approval_path_list = get_project_approval_path_details($_POST['project_id']);

div_start('price_table');

start_table(TABLESTYLE, "width=40%");


$th = array("Approval Path Type","Approval Path"," ");

table_header($th);

$k = 0; //row colour counter

$calculated = false;

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

//    label_cell($myrow["id"]);
    label_cell($myrow["value_name"]);
    label_cell($myrow["approval_path_name"]);    
    edit_button_cell("Edit".$myrow['id'], _("Edit"));
   
    end_row();
}

end_table();

div_end();
//------------------------------------------------------------------------------------------------

echo "<br>";

if ($Mode == 'Edit')
{
	$myrow = get_approval_paths($selected_id);
                
        $_POST['approval_path_id'] = $myrow["approval_path_id"];
        $_POST['approval_path_type_name'] = $myrow["approval_path_type_name"];
       
}

hidden('selected_id', $selected_id);
if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}
div_start('price_details');

    start_table(TABLESTYLE2);


    if ($Mode == 'Edit')
    {
        label_row("Approval Path Type:", $_POST['approval_path_type_name']);
        approval_path_list_row(_("Approval Path:"), 'approval_path_id', null,_('Select Approval Path'));
    }
    else
    {   
        value_list_code_row(_("Approval Path Type:"), 'approval_path_type','PROJ_APP_PATH_TYPES',null, "Select Approval Path Type");
        approval_path_list_row(_("Approval Path:"), 'approval_path_id', null,_('Select Approval Path'));
    }

    end_table(1);

    submit_add_or_update_center($selected_id == -1, '', 'both');

div_end();

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