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

$page_security = 'SA_UTILITY_BILL_CREDIT_NOTES';
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/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/sales/includes/db/sales_invoice_db.inc");
include_once($path_to_root . "/sales/includes/db/cust_trans_db.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/projects/includes/db/project_payment_application_db.inc"); 
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/includes/db/approval_functions_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/gl/includes/ui/utility_bill_credit_note_ui.inc");
include_once($path_to_root . "/gl/includes/db/utility_bill_credit_note_db.inc");

include_once($path_to_root . "/transport/includes/db/vehicles_db.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.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 = "Utility Bill Credit Note"), false, false, "", $js);

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


$ubcn_no = -1;
 
if (isset($_GET['UBCN_No']))
{
    $_POST['UBCN_No'] = $_GET['UBCN_No'];
    set_session_variable("ST_UTILITY_BILL_CREDIT_NOTE_ubcn_no", $ubcn_no);
}


if (isset($_GET['NewOrder']))
{
    $_POST['UBCN_No'] = -1;
    set_session_variable("ST_UTILITY_BILL_CREDIT_NOTE_ubcn_no", $_POST['UBCN_No']);
} 

if (isset($_POST['UBCN_No']) && $_POST['UBCN_No'] > 0){
    $ubcn_no = $_POST['UBCN_No'];
}
else{
    if(!isset($_POST['UBCN_No']) > 0){
        $_POST['UBCN_No'] = -1;
        set_session_variable("ST_UTILITY_BILL_CREDIT_NOTE_ubcn_no", $_POST['UBCN_No']);
    }else{
        $ubcn_no = get_session_variable('ST_UTILITY_BILL_CREDIT_NOTE_ubcn_no');
    }
}

if (isset($_GET['Processed_UBCN_No'])) 
{	        
    $trans_no = $_GET['Processed_UBCN_No'];
    $trans_type = ST_UTILITY_BILL_CREDIT_NOTE;
    $message = get_session_variable('ST_UTILITY_BILL_CREDIT_NOTE_messages');
    clear_session_variable('ST_UTILITY_BILL_CREDIT_NOTE_messages');
    
	display_notification_centered($message);
        
	display_note(print_document_link($trans_no, _("&Print this Utility Bills Credit Note"), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/gl/utility_bill_credit_note.php", _("Create another &Utility Bills 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['Update_Release_UBCN']))
{
    release_ub_cn_for_approval($ubcn_no);
}


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

    send_ub_cn_back_to_originator($ubcn_no);
        
} 

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

    reject_ub_cn($ubcn_no);
        
}


if(isset($_POST['Save_Utility_Bill_CN_Header'])){
        
    $h_msg = can_process_ub_cn_header($ubcn_no);   
    
    if($h_msg["success"] !== true){          
        if(isset($h_msg["focus_to"])){
            set_focus($h_msg["focus_to"]);
        }
        display_error($h_msg['message']);
    }
    else{   
        begin_transaction();
        $msg = save_utility_bills_cn_header($ubcn_no);
    
        if($msg['success'] !== true){
            cancel_transaction();
            display_error($msg['message']);
        }
        else{
            update_utility_bill_cn_taxes($ubcn_no, $_POST['tax_group_id'], $_POST['rate_include_taxes']);
            commit_transaction();
            $_POST['UBCN_No'] = $msg['trans_no'];
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
    }

}


if(isset($_POST['Add_Utility_bills_CN_Item']) || isset($_POST['Utility_bills_CN_Item'])){
        
    $msg = save_utility_bills_cn($ubcn_no,$selected_id);
    
    if($msg['success'] !== true){
        display_error($msg['message']);
    }
    else{
        $_POST['UBCN_No'] = $msg['trans_no'];
        display_notification($msg['message']);
        display_notification("Taxes have been recalculated and updated.");
        $Mode = 'RESET';
    }
}


if(isset($_POST['Save_Tax'])){
    
    $subt_msg = save_utility_bill_cn_taxes($ubcn_no);
    if($subt_msg['success'] !== true){
        display_error($subt_msg['message']);
    }
    else{
        $_POST['UBCN_No'] = $ubcn_no;
        display_notification($subt_msg['message']);
        $Mode = 'RESET';
    }
}


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



if ($Mode == 'Delete')
{
    if(can_delete_utility_bill_cn_item($ubcn_no)){
        begin_transaction();
        $recs = delete_utility_bill_cn_item($selected_id);
        if($recs == 1){
            update_utility_bill_cn_taxes($ubcn_no, $_POST['tax_group_id'], $_POST['rate_include_taxes']);
            commit_transaction();
            $_POST['UBCN_No'] = $ubcn_no;

            $Mode = 'RESET';
            display_notification(_("Utility bill credit note item has been deleted."));
        }
        else{
            cancel_transaction();
            display_error(_("Utility bill credit note item could not be deleted."));
        }
    }
}

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

display_utility_bill_cn_header($ubcn_no);   

if($ubcn_no > 0){
//    submit_center_first('Save_Utility_Bill_CN_Header', _("Save"), '', 'default');

    br(1);

    submit_center_first('Update_Release_UBCN', "Release for Approval", '', 'default');
    submit('Back_To_Originator_UBCN', 'Back To Originator', true, '', 'default');
    submit_center_last('Reject_UBCN', "Reject", '', 'default');	
}

br(2);

//------------------------------------------------------------------------------------------------------------
         
display_utility_bill_cn_details($ubcn_no, $selected_id, $Mode);

end_table(1);

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

//display_utility_bill_cn_edit_controls($selected_id, $Mode);


//if($selected_id > 0){
//    submit_center_first('Utility_bills_CN_Item', _("Update"), '', 'default');
//    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
//}
//else{        
//    submit_center_first('Add_Utility_bills_CN_Item', _("ADD"), '', 'default');
//    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
//}
br();



//if($ubcn_no > 0){
//    display_tax_edit_controls($ubcn_no);
//    
//    submit_center_first('Save_Tax', _("Save Taxes"), '', 'default');
//    
//    br();
//}

br();


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


?>
