<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_SUBCONTRACTOR_CREDIT_NOTE';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_invoices_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_db.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_credit_note_db.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_purchase_orders_db.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_invoice_line_categories_db.inc");
include_once($path_to_root . "/projects/includes/ui/subcontractor_credit_note_ui.inc");


    
$js = '';
if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

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

page(_($help_context = "Subcontractor Credit Note"), false, false, "", $js);

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


$sccn_no = -1;

if (isset($_GET['SCCN_No']))
{
    $_POST['SCCN_No'] = $_GET['SCCN_No'];
    set_session_variable("ST_SUBCONTRACTOR_CREDIT_NOTE_sccn_no", $_POST['SCCN_No']);
}

if (isset($_GET['NewOrder']))
{
    $_POST['SCCN_No'] = -1;
    set_session_variable("ST_SUBCONTRACTOR_CREDIT_NOTE_sccn_no", $_POST['SCCN_No']);
}        
        
if (isset($_POST['SCCN_No']) && $_POST['SCCN_No'] > 0){
    set_session_variable("ST_SUBCONTRACTOR_CREDIT_NOTE_sccn_no", $_POST['SCCN_No']);
}
else{
    if(!isset($_POST['SCCN_No'])){
        $_POST['SCCN_No'] = -1;
        set_session_variable("ST_SUBCONTRACTOR_CREDIT_NOTE_sccn_no", $_POST['SCCN_No']);
    }
}

$sccn_no = get_session_variable('ST_SUBCONTRACTOR_CREDIT_NOTE_sccn_no'); 



$processed_trans_no = 0;
if (isset($_GET['Released_ID'])){
    $processed_trans_no = $_GET['Released_ID'];
}
if(isset($_GET['Sent_Back_ID'])){
    $processed_trans_no = $_GET['Sent_Back_ID'];
}
if(isset($_GET['Rejected_ID'])){
    $processed_trans_no = $_GET['Rejected_ID'];
}
if($processed_trans_no > 0)
{         
    $trans_type = ST_SUBCONTRACTOR_CREDIT_NOTE;
    $message = get_session_variable('subcontractor_credit_note_messages');
    clear_session_variable('subcontractor_credit_note_messages');
    
    display_notification_centered($message);

    display_note(print_document_link($processed_trans_no, _("&Print this Project Invoice"), true, $trans_type), 0, 0);

    hyperlink_no_params("$path_to_root/projects/subcontractor_credit_note.php", _("Create another &Subcontractor Credit Note"));

//  hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

    display_footer_exit();
    
}




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

if (isset($_POST['Release_SCCN'])){
    
    $release_msg = release_for_approval($sccn_no);
    if($release_msg['success'] !== true){
        display_error($release_msg['message']);
    }
    else{
        new_doc_date($_POST['ord_date']);
        set_session_variable('subcontractor_credit_note_messages', $release_msg['message']);
        meta_forward($_SERVER['PHP_SELF'], "Released_ID=$sccn_no");
    }
    
} 

if (isset($_POST['Back_To_Originator_SCCN'])){

    $sent_back_msg = send_sccn_back_to_originator($sccn_no);
    if($sent_back_msg['success'] !== true){
        display_error($sent_back_msg['message']);
    }
    else{
        new_doc_date($_POST['ord_date']);
        set_session_variable('subcontractor_credit_note_messages', $sent_back_msg['message']);
        meta_forward($_SERVER['PHP_SELF'], "Sent_Back_ID=$sccn_no");
    }
        
} 


if (isset($_POST['Reject_SCCN'])){

    $reject_msg = reject_sccn($sccn_no);
        if($reject_msg['success'] !== true){
        display_error($reject_msg['message']);
    }
    else{
        new_doc_date($_POST['ord_date']);
        set_session_variable('subcontractor_credit_note_messages', $reject_msg['message']);
        meta_forward($_SERVER['PHP_SELF'], "Rejected_ID=$sccn_no");
    }
        
} 


if(isset($_POST['Save_SCCN_Header'])){
        
    begin_transaction();
    $msg = save_subcontractor_credit_note_header($sccn_no);
    
    if($msg['success'] !== true){
        cancel_transaction();
        display_error($msg['message']);
    }
    else{
        commit_transaction();
        $_POST['SCCN_No'] = $msg['trans_no'];
        display_notification($msg['message']);
        $Mode = 'RESET';
    }

}


if(isset($_POST['Add_SCCN']) || isset($_POST['Update_SCCN'])){
     
    $msg = save_subcontractor_credit_note($sccn_no,$selected_id);
    
        
    if($msg['success'] !== true){
        display_error($msg['message']);
    }
    else{
        $_POST['SCCN_No'] = $msg['trans_no'];
        display_notification($msg['message']);
        $Mode = 'RESET';
    }

}


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

if ($Mode == 'Delete')
{
        $recs = delete_subcontractor_credit_note_item($selected_id);
        
        if($recs == 1){
                display_notification(_("Subcontractor credit note item has been deleted."));
        }
        else{
                display_error(_("Subcontractor credit note item could not be deleted."));
        }
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}
    $Ajax->activate('_page_body');
    $Ajax->activate('billing_items');
//--------------------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
start_form();


$sci_header = display_subcontractor_credit_note_header($sccn_no); 


if($sccn_no > 0){
    submit_center_first('Save_SCCN_Header', _("Save"), '', 'default');
}

if($sccn_no>0 && !($selected_id > 0)){
    br(2); 
    submit_center_first('Release_SCCN', "Release for Approval", '', 'default');
    submit('Back_To_Originator_SCCN', 'Back To Originator', true, '', 'default');
    submit_center_last('Reject_SCCN', "Reject", '', 'default');	
}

br(2);

  
display_subcontractor_credit_note_details($sccn_no);
//------------------------------------------------------------------------------------------------------------

end_table(1);
//-----------------------------------------------------------------------------------------------------------


display_subcontractor_credit_note_edit_controls($selected_id, $Mode);



if($selected_id > 0){
    submit_center_first('Update_SCCN', _("Update"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
}
else{        
    submit_center_first('Add_SCCN', _("ADD"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
}

br();


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



?>
