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

page(_($help_context = "Sales Order Approvers"));

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

include_once($path_to_root . "/sales/includes/db/sales_order_approvers_db.inc");

simple_page_mode(false);
//----------------------------------------------------------------------------------

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
	//initialise no input errors assumed initially before we test
	$input_error = 0;
        
        if (!check_num('value_loss', 0))
        {
            $input_error = 1;
            display_error( _("Value cost must be numeric and can not be negative."));
            set_focus('value_loss');

        }
        if (!check_num('percentage_cost', 0))
        {
            $input_error = 1;
            display_error( _("Percentage cost must be numeric and can not be negative."));
            set_focus('percentage_cost');

        }


	if ($input_error !=1 && $selected_id == '') {
            
            $asoa_no = add_sales_order_approvers($_POST['user_id'], input_num('value_loss'), input_num('percentage_cost'));
            
		if($asoa_no > 0)
			display_notification(_('New sales order approver has been added'));

		$Mode = 'RESET';
	}
        elseif ($input_error !=1 && $selected_id != '') {
            
            $usoa_no = update_sales_order_approvers($selected_id, $_POST['user_id'], input_num('value_loss'), input_num('percentage_cost'));
            
		if($usoa_no > 0)
			display_notification(_('Selected sales order approver has been updated'));

		$Mode = 'RESET';
    }
}

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

if ($Mode == 'Delete')
{
    delete_sales_order_approvers($selected_id);
    display_notification(_('Selected sales order approver has been deleted'));
    $Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = '';
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}

//----------------------------------------------------------------------------------
$result = get_sales_order_approvers();

start_form();
start_table(TABLESTYLE, "width=30%");
$th = array(_('User'),_('Value Loss'), _('Percentage Loss'), "", "");
//inactive_control_column($th);

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

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

    label_cell($myrow["real_name"]);
    amount_cell($myrow["value_cost"]);
    amount_cell($myrow["percentage_cost"]);
    
    edit_button_cell("Edit".$myrow["id"], _("Edit"));
    
    delete_button_cell("Delete".$myrow["id"], _("Delete"));
    
    end_row();
}

//inactive_control_row($th);
end_table(1);

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

start_table(TABLESTYLE2);

if ($selected_id != '') 
{
    if ($Mode == 'Edit') {
        //editing an existing item category

        $myrow = get_sales_order_approver($selected_id);
        
        $_POST['user_id'] = $myrow["user_id"];
        $_POST['value_cost'] = price_format($myrow["value_cost"]);
        $_POST['percentage_cost']  = price_format($myrow["percentage_cost"]);
    }
    hidden('selected_id', $myrow["id"]);
    user_list_row(_("User:"), 'user_id', $myrow["user_id"], false, true);
    amount_row(_("Value Loss:"), 'value_loss', $myrow["value_cost"]);
    amount_row(_("Percentage Loss:"), 'percentage_cost', $myrow["percentage_cost"]);
}

if ($Mode != 'Edit') {
    
    user_list_row(_("User:"), 'user_id', null, false, true);
    amount_row(_("Value Loss:"), 'value_loss');
    amount_row(_("Percentage Loss:"), 'percentage_cost');
}
end_table(1);

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

end_form();

end_page();

?>
