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

$page_security = 'SA_SUBCONTRACTOR_INVOICE';
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 . "/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 . "/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_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_invoices_ui.inc");
include_once($path_to_root . "/includes/banking_custom.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 Invoice"), false, false, "", $js);

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

//$sci_no = get_session_variable('subcontractor_invoice_no'); 
//if(!($sci_no > 0)){
//    set_session_variable('subcontractor_invoice_no', -1);
////    $sci_no = -1;
//}

$sci_no = -1;
 
if (isset($_GET['SCI_No']))
{
    $_POST['SCI_No'] = $_GET['SCI_No'];
}

if (isset($_POST['SCI_No'])){
    $sci_no = $_POST['SCI_No'];
}
   //$sci_no = get_session_variable("subcontractor_invoice_no");

if (isset($_GET['Released_ID']) || isset($_GET['Sent_Back_ID']) || isset($_GET['Rejected_ID'])) 
{
    if (isset($_GET['Released_ID'])) {
        $trans_no = $_GET['Released_ID'];
    } elseif (isset($_GET['Sent_Back_ID'])) {
        $trans_no = $_GET['Sent_Back_ID'];
    } elseif (isset($_GET['Rejected_ID'])) {
        $trans_no = $_GET['Rejected_ID'];
    }
        
    $trans_type = ST_SUBCONTRACTOR_INVOICE;
    $message = get_session_variable('subcontractor_invoice_messages');
    clear_session_variable('subcontractor_invoice_messages');
    
    display_notification_centered($message);

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

    hyperlink_no_params("$path_to_root/projects/subcontractor_invoices.php", _("Create another &Subcontractor Invoice"));

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

    display_footer_exit();
    
}





//function can_release_subcontractor_invoice($sci_no)
//{    
//    
//    if(!($sci_no > 0)){
//        $message = "This Subcontractor Invoice is not saved. Please save it before releasing";
//        $set_focus = '';
//        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
//    }
//        
//    $sci = get_subcontractor_invoice_header_record($sci_no);
//    $sc_invoice_tax_free_total = $sci['total'];
//        
//    $task_lines_total = 0;
//    $sci_result = get_subcontractor_invoice_detail_records($sci_no);
//    while ($row = db_fetch($sci_result)) {
//        if($row['task_id'] > 0){
//            $task_lines_total += $row['amount'];
//        }
//    }
//    
//    if($task_lines_total < $sc_invoice_tax_free_total){
//        $message = "Total value of budget entered lines in the detail section (".number_format($task_lines_total,2)
//                .") can not be less than the tax free total (".number_format($sc_invoice_tax_free_total,2).")";
//        $set_focus = '';
//        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
//    }
//    
//
//      
//    return array("success" => true, "message" => "");    
//}


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


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

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

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


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

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


if(isset($_POST['Save_MPI_Header'])){
        
    $msg = save_subcontractor_invoice_header($sci_no);
    
    if($msg['success'] !== true){
        display_error($msg['message']);
    }
    else{
        $_POST['SCI_No'] = $msg['trans_no'];
        display_notification($msg['message']);
        $Mode = 'RESET';
    }

}


if(isset($_POST['Add_MPI_Item']) || isset($_POST['Update_MPI_Item'])){
        
    $msg = save_subcontractor_invoice($sci_no,$selected_id);
    
    if($msg['success'] !== true){
        display_error($msg['message']);
    }
    else{
        $_POST['SCI_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_invoice_item($selected_id);
        
        if($recs == 1){
                display_notification(_("Manual Project invoice item has been deleted."));
        }
        else{
                display_error(_("Manual Project invoice 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_invoice_header($sci_no);   
//br();

$approval_status = 0;
if(isset($sci_header["approval_status"])){
    $approval_status = $sci_header["approval_status"];
}

if(in_array($approval_status, array(AS_NEW, AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER, AS_BACK_TO_ORIGINATOR))){
    if($sci_no > 0){
        submit_center_first('Save_MPI_Header', _("Save"), '', 'default');
    }

    if($sci_no>0 && !($selected_id > 0)){
        br(2); 
        submit_center_first('Release_MPI', "Release for Approval", '', 'default');
        submit('Back_To_Originator_MPI', 'Back To Originator', true, '', 'default');
        submit_center_last('Reject_MPI', "Reject", '', 'default');	
    }
}

br(2);

//------------------------------------------------------------------------------------------------------------
         
display_subcontractor_invoice_details($sci_no);

//hidden('SCI_No',$sci_no);
//inactive_control_row($th);
end_table(1);
//-----------------------------------------------------------------------------------------------------------

display_subcontractor_invoice_edit_controls($selected_id, $Mode);

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

br();


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


?>
