<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_IMPORT_PO_PAYTERMS';
$path_to_root="../..";
include($path_to_root . "/includes/session.inc");
include($path_to_root . "/includes/ui.inc");
include($path_to_root . "/imports/includes/db/import_payment_terms_db.inc");
 

page(_($help_context = "Import Payment Terms"));

simple_page_mode(true);

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

if($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM'){
    $input_error = 0;

    if(!(strlen($_POST['description']) > 0)){
        $input_error = 1;
        display_error( _("Description must be entered."));
        set_focus('description');
    }
    if($selected_id != -1){
        update_import_payment_term($selected_id, $_POST['description']); 
        $note = _("Selected import payment terms has been updated");
    }else{
        add_import_payment_term($_POST['description']);
        $note = _('New import payment term has been added');
    }
    display_notification($note);
    
    $Mode = 'RESET'; 
}

if($Mode == 'Delete'){
    delete_payment_terms($selected_id);
    display_notification(_('Selected import payment terms have been deleted'));

    $Mode = 'RESET';
}

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

$result = get_import_payment_terms(check_value('show_inactive'));

start_form();
    start_table(TABLESTYLE, "width=40%");
        $th = array(_("ID"), _("Description"), "");
        inactive_control_column($th, 1);
        table_header($th);

        $k = 0; //row colour counter
        while($myrow = db_fetch($result)){
            alt_table_row_color($k);
                label_cell($myrow["id"], "align=center");
                label_cell($myrow["description"]);     
                inactive_control_cell($myrow["id"], $myrow["inactive"], 'import_payment_terms', "id");
                edit_button_cell("Edit".$myrow["id"], _("Edit"));
            end_row();
        }

        inactive_control_row($th);
    end_table(1);

        //-------------------------------------------------------------------------------------------------
        if(list_updated('type')) {
            $Ajax->activate('edits');
        }

        div_start('edits');
            start_table(TABLESTYLE2);
                $day_in_following_month = $days_before_due = 0;
                if($selected_id != -1){
                    if($Mode == 'Edit'){
                        $myrow = get_import_payment_term($selected_id);
                        $_POST['id']  = $myrow["id"];
                        $_POST['description']  = $myrow["description"];
                    }
                    hidden('selected_id', $selected_id);
                }

                text_row(_("Terms Description:"), 'description', null, 40, 200);
            end_table(1);
        div_end();

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

end_form();

end_page();

?>