<?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 . "/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/project_cost_functions.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");

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

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

global $systypes_array;

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

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


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

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

if (isset($_GET['Copied_ID'])) {
    $trans_no = $_GET['Copied_ID'];
    $message = $_GET['message'];
    
	display_notification_centered($message);
        
	hyperlink_no_params("$path_to_root/projects/subcontractor_invoices.php?SCI_No=$trans_no", _("Edit copied &Subcontractor Invoice #$trans_no."));

	display_footer_exit();
}

function display_copy_subcontractor_invoice_header($sci_no) {
    global $approval_statuses, $Refs;
    
    $msci_header =  array();
    
    if($sci_no>0){
        $msci_header = get_subcontractor_invoice_header($sci_no);
        $_POST['project_id'] = $msci_header['project_id'];
        $_POST['supplier_id'] = $msci_header['supplier_id'];
        
        $project_id = $msci_header['project_id'];
        
        if(!isset($_POST['reference'])){
            $_POST['reference'] = $msci_header['reference'];
        }
        if(!isset($_POST['payment_terms'])){
            $_POST['payment_terms'] = $msci_header['payment_terms'];
        }
//        if(!isset($_POST['ord_date'])){
//            $_POST['ord_date'] = sql2date($msci_header['ord_date']);
//        }
        if(!isset($_POST['total'])){
            $_POST['total'] = $msci_header['total'];
        }
        if(!isset($_POST['taxes'])){
            $_POST['taxes'] = $msci_header['taxes'];
        }
        if(!isset($_POST['payment_application_ref'])){
            $_POST['payment_application_ref'] = $msci_header['payment_application_ref'];
        }
        if(!isset($_POST['payment_application_amount'])){
            $_POST['payment_application_amount'] = $msci_header['payment_application_amount'];
        }
        if(!isset($_POST['payment_certificate_ref'])){
            $_POST['payment_certificate_ref'] = $msci_header['payment_certificate_ref'];
        }
        if(!isset($_POST['payment_certificate_amount'])){
            $_POST['payment_certificate_amount'] = $msci_header['payment_certificate_amount'];
        }
        if(!isset($_POST['tax_group_id'])){
            $_POST['tax_group_id'] = $msci_header['tax_group_id'];
        }
        if(!isset($_POST['supp_inv_ref'])){
            $_POST['supp_inv_ref'] = $msci_header['supp_inv_ref'];
        }
        if(!isset($_POST['project_description'])){
            $_POST['project_description'] = $msci_header['project_description'];
        }
        
        $_POST['approval_status'] = $msci_header['approval_status'];
        $_POST['po_no'] = $msci_header['po_no'];
        $_POST['supp_name'] = $msci_header['supp_name'];
        
        if(!(isset($_POST['reference']) && strlen($_POST['reference']) > 0)){
            $_POST['reference'] = $Refs->get_next(ST_SUBCONTRACTOR_INVOICE);
        }
        $_POST['approval_status'] = 0;
    }
    else{        
        $_POST['po_no'] = 0;
        $_POST['supp_name'] = '';
    }
        
    start_outer_table(TABLESTYLE2, "width=85%"); // outer table
        table_section(1, "35%");
            text_row("Reference:", 'reference', null, null, null);      
            
            label_row("Supplier:", $_POST['supp_name']);
            hidden('supplier_id', $_POST['supplier_id']);
            
            if($_POST['po_no'] > 0){
                $s = get_project_trans_view_str(ST_SUBCONTRACTOR_PURCH_ORDER, $_POST['po_no']);
                label_row("PO No:", $s);
            }
            else{
                label_row("PO No:", "(Manual Invoice)");
            }
            
            date_row("Order Date:", 'ord_date');
            start_row();
                sale_payment_list_cells("Payment Terms:", 'payment_terms', null, null);  
            end_row();    
            
            label_row("Project :",$_POST['project_description']);
            hidden('project_id', $_POST['project_id']);

            text_row("Supp. Inv. Ref :", 'supp_inv_ref', null, null, null); 
        
        table_section(2, "35%");
            
            text_row("Payment Application Reference:", 'payment_application_ref', null, null, null); 
            amount_row("Payment Application Amount:", 'payment_application_amount'); 
            
            text_row("Payment Certificate Reference:", 'payment_certificate_ref', null, null, null); 
            amount_row("Payment Certificate Amount:", 'payment_certificate_amount');   
            
            global $approval_statuses;
            $s = get_project_trans_view_str(ST_APPROVAL_LOG, $sci_no."&source_type_id=".ST_SUBCONTRACTOR_INVOICE, "Approval Status");
            label_row($s,$approval_statuses[$_POST['approval_status']]);
            
        table_section(3, "35%");
            table_section_title("Supplier Payable (Accounts Payable) Values");
            amount_row("Total Amount (without taxes):", 'total');   

            if(!(isset($_POST['tax_group_id']) && $_POST['tax_group_id'] > 0)){
                $supplier = get_supplier($_POST['supplier_id']);
                $_POST['tax_group_id'] = $supplier['tax_group_id'];
            }
            
            tax_groups_list_row("Tax Group:", 'tax_group_id', null, "Select a Tax Group", true);
            
            
            $taxes_total = display_selected_tax_types($sci_no, get_post('tax_group_id'));   
            
            $inv_total = input_num('total') + $taxes_total;
            label_row(text_bold("Total"), text_bold(number_format($inv_total, 2)));


//    end_table(1);

    end_outer_table(1); // outer table
    hidden('SCI_No', $sci_no);
    
    
    return $msci_header;

}



if (isset($_POST['COPY_SCI'])) {
    $old_sci_no = $sci_no;
    $sci_no = -1;
    
        begin_transaction();
        $msg = save_subcontractor_invoice_header($sci_no);
            if ($msg['success'] !== true) {
                cancel_transaction();
                $sci_no = $old_sci_no;
                display_error($msg['message']);
            } else {
                $msg2 = copy_and_save_subcontractor_invoice_details($sci_no, $old_sci_no);
                if ($msg2['success'] !== true) {
                    cancel_transaction();
                    $sci_no = $old_sci_no;
                    display_error($msg2['message']);
                } else {
                    commit_transaction();
                    meta_forward($_SERVER['PHP_SELF'], "Copied_ID=$sci_no&message=".$msg2['message']);
                }
            }
            
}

    $Ajax->activate('_page_body');
    $Ajax->activate('billing_items');
    
    
if (!@$_GET['popup'])
start_form();

br(2);

    $sci_header = display_copy_subcontractor_invoice_header($sci_no);   
    br(2);

    start_table(TABLESTYLE_NOBORDER);
    start_row();
    $link = get_project_trans_view_str(ST_PROJECT_INVOICE, $sci_no, "View Copying From ".$systypes_array[ST_SUBCONTRACTOR_INVOICE]);
    label_cell($link);
    end_row();
    end_table(2);

    submit_center_first('COPY_SCI', "Copy".$systypes_array[ST_SUBCONTRACTOR_INVOICE], '', 'default');


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


?>