<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PETTYCASH_TRANSVIEW';
$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/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_float_reimbursement_inquiry_db.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_bank_accounts.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_floats_db.inc");


$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "View Petty Cash Float Reimbursement"), true, false, "", $js);

//-------------------------------------------------------------------------------------------------
$woid = 0;
if ($_GET['trans_no'] != "")
{
	$id = $_GET['trans_no'];
}

global $approval_statuses;

function display_float_reimbursement_header($ad_batch_no)
{
    $myrow = view_float_reimbursement_header($ad_batch_no);

    start_table(TABLESTYLE2, "width=30%");

    start_row();

    label_cells(_("Reference"), $myrow['reference'], "class='tableheader2'");
    end_row();
    label_cells(_("Float"), $myrow['float_name'], "class='tableheader2'");
    end_row();
    label_cells(_("Date"), sql2date($myrow['tran_date']), "class='tableheader2'");
    end_row();
    
    if($myrow['from_float_id'] > 0){
        $from_float = get_petty_cash_float_line($myrow['from_float_id']);
        label_cells(_("From Float"), $from_float['name'], "class='tableheader2'");
    }
    
    if($myrow['bank_account_id'] > 0){
        $bank_account = get_bank_account($myrow['bank_account_id']);
        label_cells(_("Bank Account"), $bank_account['bank_account_number']." - ".$bank_account['bank_account_name'], "class='tableheader2'");
        end_row();
        if($myrow['bank_payment_id'] > 0){
            $bank_trans = get_bank_trans_by_id($myrow['bank_payment_id']);
            $bank_trans_type = $bank_trans['type'];
            $bank_trans_trans_no = $bank_trans['trans_no'];
            $bank_trans_link = get_banking_trans_view_str($bank_trans_type, $bank_trans_trans_no);
            label_cells(_("Bank Transaction"), $bank_trans_link, "class='tableheader2'");
        }else{
//            $bank_trans_res = get_bank_trans(ST_PETTY_CASH_FLOAT_REIMBUR, $ad_batch_no);
//            $bank_trans = db_fetch($bank_trans_res);
//            $bank_trans_type = $bank_trans['type'];
//            $bank_trans_trans_no = $bank_trans['trans_no'];
        }
        
    }
    end_row();
    

    label_cells(_("Amount"), number_format($myrow['amount'],2), "class='tableheader2'");
    end_row();

//    $link = get_petty_cash_trans_view_str(ST_PETTY_CASH_FLOAT_REIMBUR, $myrow['float_reimbursement_id']);
//    label_cells(_("Reimbursement"), $link, "class='tableheader2'");
    
    $link = get_petty_cash_float_reimbursement_details_view_str(ST_PETTY_CASH_FLOAT_REIMBUR, $myrow['id'], number_format($myrow['reimbursed_amount'],2));
    label_cells(_("Reimbursed Amount"), $link, "class='tableheader2'");

    end_row();
    
    end_table();

}

function display_float_reimbursement_type_details($query, &$advances_total, &$batch_total, &$reimbursement_total)
{
    global $petty_cash_float_transaction_types;
    
    while ($myrow = db_fetch($query)) {
        alt_table_row_color($k);

        label_cell($petty_cash_float_transaction_types[$myrow['tran_type']]);

        $iou_link = get_trans_view_str($myrow['tran_type'], $myrow['type_no']);        
        label_cell($iou_link, 'align=center');
//        label_cell(sql2date($myrow['tran_date']));
//        $advances_total += $myrow['advances_total'];
        
        amount_decimal_cell($myrow['amount']);
        $batch_total += $myrow['amount'];
        
        $reimbursement_total += $myrow['amount'];
        
        end_row();
    }
}

function display_float_reimbursement_details($id)
{
    global  $petty_cash_float_transaction_types;

    $advances_total = $batch_total = $reimbursement_total = 0;
    
    start_table(TABLESTYLE2, "width=40%");
        $th = array(_("Tran. Type"),"Tran. No", _("Amount"));
    table_header($th);

//    $result = view_petty_cash_float_reimbursement_iou_expense_batches($id);
//    display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
//    
//    $result = view_petty_cash_float_reimbursement_iou_payments($id);
//    display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
//    
//    $result = view_petty_cash_float_reimbursement_petty_cash_payments($id);
//    display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
//    
//    $result = view_petty_cash_float_reimbursement_direct_petty_cash_payments($id);
//    display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
//    
//    $result = view_petty_cash_float_reimbursement_float_adjustments($id);
//    display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
//    
//    $result = view_petty_cash_float_reimbursement_float_reimbursements($id);
//    display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
//    
//    $result = view_petty_cash_float_reimbursement_iou_advances($id);
//    display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
//    
//    $result = view_petty_cash_float_reimbursement_project_float_advances($id);
//    display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
//    
//    $result = view_petty_cash_float_reimbursement_supplier_payments($id);
//    $result = display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
    
    $result = view_petty_cash_float_reimbursement_transactions($id);
    $result = display_float_reimbursement_type_details($result, $advances_total, $batch_total, $reimbursement_total);
    
    start_row();
    label_cell("");
    label_cell("Total", "align=center");
//    label_cell(number_format($advances_total,2), "align=right");
    label_cell(text_bold(number_format($batch_total,2)), "align=right");
//    label_cell(number_format($reimbursement_total,2), "align=right");
    end_row();
    
    end_table();
}

br();
display_heading("Petty Cash Float Reimbursement No: " . $id);
br();
display_float_reimbursement_header($id);
br(2);
display_float_reimbursement_details($id);
br(2);
end_page(true, false, false, ST_WORKORDER, $woid);

?>
