<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SERVICE_COMPLAIN';
$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc"); 
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/services/includes/ui/service_complains_ui.inc"); 
include_once($path_to_root . "/services/includes/db/service_complains_db.inc"); 
include_once($path_to_root . "/services/includes/db/job_orders_db.inc"); 
include_once($path_to_root . "/admin/db/list_values_db.inc"); 
include_once($path_to_root . "/admin/db/transactions_db.inc");  
include_once($path_to_root . "/assets/includes/db/assets_db.inc");   
include_once($path_to_root . "/services/includes/db/job_activities_db.inc"); 
include_once($path_to_root . "/services/includes/ui/job_activities_ui.inc"); 
include_once($path_to_root . "/includes/ui/db_pager_view.inc"); 
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");  

$js = "";
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

global $Ajax, $systypes_array;

$type = ST_SERVICE_COMPLAIN;

page(_($help_context = $systypes_array[$type]." Entry"), false, false, "", $js);

simple_page_mode(true);

$state = true;

if (isset($_GET['cmp_id'])) {
    $cmp_id = $_GET['cmp_id'];
} elseif(isset($_POST['cmp_id'])) {
    $cmp_id = $_POST['cmp_id'];
} else {
    $cmp_id  = -1;
}

if (isset($_GET['JOID'])) {
    global $systypes_array;
    
    $trans_no = $_GET['JOID'];
    $cmp_id = $_GET['cmp_id'];
    $trans_type = ST_JOB_ORDER;

    $type_name = $systypes_array[$trans_type];

    display_notification("Job Card #$trans_no has been created from Complain #$cmp_id.");
    
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    
    display_note(pager_link( _("Edit $type_name #$trans_no."),"/services/job_order_entry.php?trans_no=$trans_no"), 0, 1);
    display_footer_exit();
}

if (isset($_GET['CLOSED_ID'])) {
    global $systypes_array;
    
    $cmp_id = $_GET['CLOSED_ID'];
    $trans_type = ST_SERVICE_COMPLAIN;

    $type_name = $systypes_array[$trans_type];

    display_notification("$type_name #$cmp_id has been closed.");
    
    display_note(get_trans_view_str($trans_type, $cmp_id, _("&View this $type_name #$cmp_id.")), 0, 1);
    
    display_note(pager_link( _("Create a new $type_name."),"/services/service_complains.php?"), 0, 1);
    display_footer_exit();
}

$row_state = 0;
if (isset($_POST['SAVE_COMPLAIN'])) {
    $msg = handle_save_service_complain_header($cmp_id, $row_state);
        if ($msg['success'] === false) {
            if (isset($msg['focus_to'])) {
                set_focus($msg['focus_to']);
            }
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if (isset($_POST['CLOSE_COMPLAIN'])) {
    $msg = handle_close_service_complain_header($cmp_id);
        if ($msg['success'] === false) {
            if (isset($msg['focus_to'])) {
                set_focus($msg['focus_to']);
            }
            display_error($msg['message']);
        } 
}

if (isset($_POST['SAVE_CREATE_JOB'])) {
    $msg = handle_save_service_complain_header($cmp_id, $row_state, true);
        if ($msg['success'] === false) {
            if (isset($msg['focus_to'])) {
                set_focus($msg['focus_to']);
            }
            display_error($msg['message']);
        } 
}

start_form();

    if ($cmp_id > 0) {
        hidden('cmp_id', $cmp_id);
    }
    
    $Ajax->activate('service_complain_entry');
    div_start('service_complain_entry');
        display_heading($systypes_array[$type].' Entry Details'); br();
        display_service_complain_header($cmp_id);       
        
        start_table();
            submit_center_first('SAVE_COMPLAIN', 'Save Complain/ Request Only', 'Save Complain/ Request Only', false, ICON_SUBMIT);
            if ($cmp_id > 0) {
                submit_center_last('CLOSE_COMPLAIN', 'Close Complain/ Request', 'Close Complain/ Request', false, ICON_ESCAPE);
            }
        end_table(3);
        
        display_heading($systypes_array[ST_JOB_ORDER].' Entry Details'); br();
        display_service_complain_job_header($cmp_id);
    
        start_table();
            submit_center_last('SAVE_CREATE_JOB', 'Save Complain/ Request and Create Job Card', 'Save Complain/ Request and Create Job Card', false, ICON_SUBMIT);
        end_table(1);
    div_end();
    
end_form(2);
end_page();

?>