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


function display_transactions_for_float_reimbursement($float_id, $from_date, $to_date)
{
    global  $Ajax, $petty_cash_float_transaction_types;
   
    $reimbursement_total = $advances_total = $batch_total = $reimbursement_method = 0;
    
    $float = get_petty_cash_float_line($float_id);
        if (isset($float['id']) && $float['id'] > 0) {
            $reimbursement_method = $float['reimbursement_method'];
        }
    
    $Ajax->activate('leaves');
    div_start('leaves');
    display_heading('Petty Cash Float Reimbursement');
    start_table(TABLESTYLE, "width=90%");

    $th = array(_("Type"), _("Id"), 
            //"IOU Id", _("Batch No"), 
                _("Project"),
                _("Date"), "Comment", 
                _("Advances"), 
                _("Transaction Amount"), 
                _("Reimbursed Amount"), 
                _(""), 
                _("Reimbursing Amount"), 
                _(""));

    table_header($th);

    if($reimbursement_method == 1){ //Payments
        start_row();label_cell("<b>I Owe You Payments</b>", "colspan=9");end_row();
        display_iou_payments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                                $advances_total, $batch_total, $reimbursement_total); 

        start_row();label_cell("<b>Petty cash Payments</b>", "colspan=9");end_row();
        display_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                                $advances_total, $batch_total, $reimbursement_total); 

        start_row();label_cell("<b>I Owe You Advances</b>", "colspan=9");end_row();
        display_iou_advances_for_float_reimbursement($float_id, $from_date, $to_date, 
                                                $advances_total, $batch_total, $reimbursement_total); 

        start_row();label_cell("<b>Project Float Advances</b>", "colspan=9");end_row();
        display_project_float_advances_for_float_reimbursement($float_id, $from_date, $to_date, 
                                                $advances_total, $batch_total, $reimbursement_total); 
    }
    else{ // Expenses
        start_row();label_cell("<b>I Owe You / Petty cash Expenses</b>", "colspan=9");end_row();
        display_iou_expenses_for_float_reimbursement($float_id, $from_date, $to_date, 
                                                $advances_total, $batch_total, $reimbursement_total); 
    }
    
    
    start_row();label_cell("<b>Direct Petty Cash Payments</b>", "colspan=9");end_row();
    display_direct_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                                $advances_total, $batch_total, $reimbursement_total);  
        
    start_row();label_cell("<b>Supplier Payments</b>", "colspan=9");end_row();
    display_supplier_payments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            $advances_total, $batch_total, $reimbursement_total); 
    
    start_row();label_cell(text_bold($petty_cash_float_transaction_types[ST_CASH_PAYMENT]."s"), "colspan=9");end_row();
    display_cash_transactions_for_float_reimbursement(ST_CASH_PAYMENT, $float_id, $from_date, $to_date, 
                                            $advances_total, $batch_total, $reimbursement_total); 
    
    start_row();label_cell(text_bold($petty_cash_float_transaction_types[ST_CASH_RECEIPT]."s"), "colspan=9");end_row();
    display_cash_transactions_for_float_reimbursement(ST_CASH_RECEIPT, $float_id, $from_date, $to_date, 
                                            $advances_total, $batch_total, $reimbursement_total); 

    start_row();label_cell("<b>Petty Cash Float Entries</b>", "colspan=9");end_row();
    display_petty_cash_float_adjustments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            $advances_total, $batch_total, $reimbursement_total); 

    start_row();label_cell("<b>Petty Cash Float Reimbursements (by Other Floats)</b>", "colspan=9");end_row();
    display_petty_cash_float_reimbursements_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            $advances_total, $batch_total, $reimbursement_total); 
    

    
    start_row();
    label_cell("", "colspan=7");
    
    label_cell("Total", "align=center");
    
    $sign = $reimbursement_total > 0 ? "+" : "-";
    label_cell(spaces(5).$sign);

//    label_cell(number_format($advances_total,2), "align=right");
//    label_cell(number_format($batch_total,2), "align=right");
    label_cell(number_format($reimbursement_total,2), "align=right");
    end_row();

    br();


    end_table(2);
    div_end(); 
}


function display_iou_expenses_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types, $systypes_array;
   

    $result = get_iou_expenses_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;
    
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_petty_cash_trans_view_str($myrow['type_id'], $myrow['id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');   
        
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right"); 
//        amount_decimal_cell(($myrow["amount"]),'', "align=right"); 
               
               
//        $reimbursement_amount = $myrow["amount"] - $myrow["advances_total"];
        
        amount_decimal_cell($myrow["amount"],'', "align=right");
        
        $tran_amount = abs($myrow["amount"]);
        
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
       
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);

        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $systypes_array[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
    }
    

}


function display_iou_payments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types, $systypes_array;
   

    $result = get_iou_payments_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;
    
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_petty_cash_trans_view_str($myrow['type_id'], $myrow['id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');          
        
//        amount_decimal_cell(($myrow["amount"]),'', "align=right"); 
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right");        
               
        amount_decimal_cell($myrow["amount"],'', "align=right");
        
        $tran_amount = abs($myrow["amount"]);
        
        
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);

        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        
        
        
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);

        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $petty_cash_float_transaction_types[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
    }
    

}

function display_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types;
   

    $result = get_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_petty_cash_trans_view_str($myrow['type_id'], $myrow['id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');    
        
//        amount_decimal_cell(($myrow["amount"]),'', "align=right");  
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right");        
              
        
        
        amount_decimal_cell($myrow["amount"],'', "align=right");
        
        $tran_amount = abs($myrow["amount"]);
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
       
        
        
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);
        
        global $systypes_array;
        
        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $systypes_array[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
        

    }
    

}


function display_direct_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types, $systypes_array;
   

    $result = get_direct_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_petty_cash_trans_view_str($myrow['type_id'], $myrow['id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');          
               
//        amount_decimal_cell(($myrow["amount"]),'', "align=right");     
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right"); 
        
        amount_decimal_cell($myrow["amount"],'', "align=right");
        
        $tran_amount = abs($myrow["amount"]);
        
        
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
       
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);

        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $systypes_array[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
        
    }
    

}


function display_petty_cash_float_adjustments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types, $systypes_array;
   

    $result = get_petty_cash_float_adjustments_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_petty_cash_trans_view_str($myrow['type_id'], $myrow['id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');          
               
//        amount_decimal_cell(($myrow["amount"]),'', "align=right");     
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right"); 
        
        amount_decimal_cell($myrow["amount"],'', "align=right");
        
        $tran_amount = abs($myrow["amount"]);        
        
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
       
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);

        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $systypes_array[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
    }
    

}


function display_supplier_payments_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types, $systypes_array;
   

    $result = get_supplier_payments_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_supplier_trans_view_str($myrow['type_id'], $myrow['id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');          
               
//        amount_decimal_cell(($myrow["amount"]),'', "align=right");     
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right"); 
        
        $tran_amount = $myrow["amount"];
        amount_decimal_cell($tran_amount,'', "align=right");
        
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
       
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);

        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $systypes_array[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
    }
    

}


function display_cash_transactions_for_float_reimbursement($trans_type, $float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types, $systypes_array;
   

    $result = get_cash_transactions_for_float_reimbursement($trans_type, $float_id, $from_date, $to_date);

    $j = 0;

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_journal_trans_view_str($myrow['type_id'], $myrow['id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');          
               
//        amount_decimal_cell(($myrow["amount"]),'', "align=right");     
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right"); 
        
        $tran_amount = abs($myrow["amount"]);
        amount_decimal_cell($myrow["amount"],'', "align=right");
        
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
       
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);

        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $systypes_array[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
    }
    

}


function display_iou_advances_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types, $systypes_array;
   

    $result = get_iou_advances_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_petty_cash_trans_view_str($myrow['type_id'], $myrow['iou_id'], $myrow['id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');   
        
//        amount_decimal_cell(($myrow["amount"]),'', "align=right"); 
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right");   
        
        $tran_amount = abs($myrow["amount"]);
        amount_decimal_cell($tran_amount,'', "align=right");
        
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
       
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);

        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $systypes_array[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
    }
    

}


function display_petty_cash_float_reimbursements_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types, $systypes_array;
   

    $result = get_petty_cash_float_reimbursements_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_petty_cash_trans_view_str($myrow['type_id'], $myrow['id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');    
        
//        amount_decimal_cell(($myrow["amount"]),'', "align=right"); 
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right");        
          
        amount_decimal_cell($myrow["amount"],'', "align=right");
        
        $tran_amount = abs($myrow["amount"]);
        
        
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
       
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);

        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $systypes_array[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
    }
    

}

function display_project_float_advances_for_float_reimbursement($float_id, $from_date, $to_date, 
                                            &$advances_total, &$batch_total, &$reimbursement_total)
{
    global  $Ajax, $petty_cash_float_transaction_types, $systypes_array;
   

    $result = get_project_float_advances_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

//        label_cells('',sql2date($myrow["transaction_date"]),'', "align=center");
        label_cells('',$petty_cash_float_transaction_types[$myrow["type_id"]],'');
        
        $iou_link = get_petty_cash_trans_view_str(ST_PROJECT_ADVANCE_BATCH, $myrow['id'], $myrow['line_id']);       
        label_cells('',$iou_link,'', "align=center");
        label_cells('',$myrow["project_description"],'');          
        label_cells('',sql2date($myrow["tran_date"]),'', "align=center");        
        label_cells('',$myrow["comment"],'');     
        
//        amount_decimal_cell(($myrow["amount"]),'', "align=right");  
        amount_decimal_cell(($myrow["advances_total"]),'', "align=right");        
              
        amount_decimal_cell($myrow["amount"],'', "align=right");
        
        $tran_amount = abs($myrow["amount"]);
        
        $reimbursed_amount = $myrow["reimbursed_amount"];
        amount_decimal_cell($reimbursed_amount,'', "align=right");
        
        $sign = $myrow["amount"] > 0 ? "+" : "-";
        label_cell(spaces(5).$sign);
        
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        set_post_not_set_numeric($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"], $reimbursible_amount);
        amount_cells(null, $myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
        $reimbursement_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
       
        if(isset($_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]]) && $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1){
            $advances_total += $myrow["advances_total"];
            $batch_total += $myrow["amount"];
            if($myrow["amount"] > 0){
                $reimbursement_total += $reimbursement_amount;
            }
            else{
                $reimbursement_total -= $reimbursement_amount;
            }
        }
      
//        $_POST[$myrow["type_id"]."_".'selected_'.$myrow["id"]] = 1;
        check_cells('', $myrow["type_id"]."_".'selected_'.$myrow["id"]);

        if(isset($_POST['recalculate'])){
            if($tran_amount < $reimbursed_amount + $reimbursement_amount){
                $maximum_reimbursiable_amount = $tran_amount - $reimbursed_amount;
                $msg = $systypes_array[$myrow["type_id"]]." #".$myrow["id"].": maximum reimbursiable amount is ".number_format2($maximum_reimbursiable_amount);
                display_error($msg);
            }
        }
    }
    

}

function reimbursement_i_owe_you_payments()
{
    $result = get_iou_payments_for_float_reimbursement($float_id, $from_date, $to_date);

    $j = 0;
    
    while ($myrow = db_fetch($result))
    {
        $num_recs = update_payment_float_reimbursement_id($line["id"], $reimbursement_id, $line['row_state'] );  
                    if($num_recs != 1){
                        cancel_transaction();
                        display_error("I Owe You expenses batch ".$line["id"]." has been updated by another user. Could not create float reimbursement.");        
                        return;
                    } 
    }
}




function create_float_reimbursement($float_id, $from_date, $to_date)
{
    global $Refs, $petty_cash_float_transaction_types;
        
    if(!check_reimbursement_header_data($float_id)){
        return;
    }
    
    $selected_lines = array();
    $reimbursement_total = 0; 
    
    $float = get_petty_cash_float_line($float_id);

    $reimbursement_method = $float['reimbursement_method'];
    
    build_float_reimbursement_array($float_id, $from_date, $to_date, $selected_lines, $reimbursement_total, $reimbursement_method);  
              
    if($reimbursement_total <= 0)
    {
        display_error("Total amount to be reimbursed has to be greater than zero");
        return;
    }
    
    $debit_account = $float['account_code'];
    
    $from_float_id = $bank_payment_id = 0;
    $from_bank_account_id = 0;
    if(get_post('from_float_id') > 0){
        $from_float_id = get_post('from_float_id');
        $from_float = get_petty_cash_float_line($from_float_id);
        $credit_account = $from_float['account_code'];
    }
    else{
        $from_bank_account_id = get_post('bank_account');        
        $credit_account = get_bank_gl_account($from_bank_account_id);
        $bank_payment_id = get_post('bank_payment_id');
    }
    
    if($from_bank_account_id > 0 && $bank_payment_id > 0){
        $bank_trans = get_bank_trans_by_id($bank_payment_id);
        $bank_payment_amount = -$bank_trans['amount'];
        
        if($reimbursement_total !== $bank_payment_amount){
            $amt_message = "Total reimbursement amount Rs. ".number_format($reimbursement_total,2)." does not match with bank payment amount Rs. ".number_format($bank_payment_amount,2);
            display_error($amt_message);
            set_focus("bank_payment_id");
            return;        
        }
    }
    
    if(!check_reimbursement_details($selected_lines)){
        return;
    }
    
//    return;
    
    
    
        
//    return;
    $cheque_number = 0;
    $tran_date = date2sql($_POST['tran_date']);
    
    begin_transaction();
    
    $reimbursement_id = add_i_owe_you_float_reimbursement($float_id, $_POST['ref'], $from_float_id,
                                                            $from_bank_account_id, $credit_account, $debit_account, 
                                                            $cheque_number, $tran_date, $reimbursement_total,
                                                            $bank_payment_id);
    
    if(!($reimbursement_id > 0))
    {
        cancel_transaction();
        display_error("Could not create float reimbursement.");     
        return;
    }
   
    $error_msg = " has been updated by another user. Could not create float reimbursement.";
    
    foreach ($selected_lines as $line) { 
        $reimbursement_amount = input_num($line["type_id"]."_".'reimbursement_amount_'.$line["id"]);
        
        switch ($line['type_id']) {
            case ST_CASH_PAYMENT:
            case ST_CASH_RECEIPT:
                $reimbursement_amount = abs($reimbursement_amount);
                break;

            default:
                if(!($line["amount"] > 0)){
                    $reimbursement_amount *= -1;
                }
                break;
        }
        
        $reimbursement_amount = abs($reimbursement_amount);
        
        $batch_tran_type = $line['type_id'];
        $batch_type_no = $line['id'];
        if($line['type_id'] == ST_I_OWE_YOU_ADVANCES_BATCH){            
            $tran_type = ST_I_OWE_YOU;
            $type_no = $line['iou_id'];
        }
        if($line['type_id'] == ST_PROJECT_ADVANCE){            
            $tran_type = ST_PROJECT_ADVANCE;
            $type_no = $line['line_id'];
        }
        else{
            $tran_type = $tran_type = $line['type_id'];
            $type_no = $trans_no = $line['id'];
        }
        
        add_float_reimbursement_details($reimbursement_id, $batch_tran_type, $batch_type_no, $tran_type, $type_no, $reimbursement_amount);
        
        $total_reimbursed_amount = get_transaction_total_reimbursed_amount($line['type_id'], $line["id"]);
        
        switch ($line['type_id'] ) {
            case ST_I_OWE_YOU_BATCH:
                $num_recs = update_iou_batch_float_reimbursement_amount($line["id"], $total_reimbursed_amount, $line['row_state'] );  
                if($num_recs != 1){
                    cancel_transaction();
                    display_error("I Owe You expenses batch ".$line["id"]." has been updated by another user. Could not create float reimbursement.");        
                    return;
                }  
                break;
                
            case ST_I_OWE_YOU_PAYMENT:
                $num_recs = update_payment_float_reimbursement_amount($line["id"], $total_reimbursed_amount, $line['row_state'] );  
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg);        
                    return;
                } 
                break;

            case ST_PETTY_CASH_PAYMENT:
                $num_recs = update_payment_float_reimbursement_amount($line["id"], $total_reimbursed_amount, $line['row_state'] );
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg);         
                    return;
                }
                break;

            case ST_PETTY_CASH_DIRECT_PAYMENTS:
                $num_recs = update_payment_float_reimbursement_amount($line["id"], $total_reimbursed_amount, $line['row_state'] );
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg);         
                    return;

                }
                break;

            case ST_SUPPAYMENT:
                $num_recs = update_supplier_payment_float_reimbursement_amount($line["id"], $total_reimbursed_amount);
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg);        
                    return;
                }
                break;
                
            case ST_CASH_PAYMENT:
                $num_recs = update_cash_transaction_float_reimbursement_amount(ST_CASH_PAYMENT, $line["id"], $total_reimbursed_amount, $line["row_state"]);
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg);        
                    return;
                }
                break;
                
            case ST_CASH_RECEIPT:
                $num_recs = update_cash_transaction_float_reimbursement_amount(ST_CASH_RECEIPT, $line["id"], $total_reimbursed_amount, $line["row_state"]);
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg);        
                    return;
                }
                break;
                
                
            case ST_PETTY_CASH_FLOAT_ADJ:
                $num_recs = update_adjustment_float_reimbursement_amount($line["id"], $total_reimbursed_amount, $line['row_state'] );
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg);        
                    return;
                }
                break;

            case ST_PETTY_CASH_FLOAT_REIMBUR:
                $num_recs = update_reimbursement_float_reimbursement_amount($line["id"], $total_reimbursed_amount, $line['row_state'] );
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg);      
                    return;
                }
                break;



            case ST_I_OWE_YOU_ADVANCES:
                $num_recs = update_i_owe_you_advance_float_reimbursement_amount($line["line_id"], $total_reimbursed_amount, $line['row_state'] );
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg);        
                    return;
                }
                break;

            case ST_PROJECT_ADVANCE:
                $num_recs = update_project_advance_float_reimbursement_amount($line["line_id"], $total_reimbursed_amount, $line['row_state'] );
                if($num_recs != 1){
                    cancel_transaction();
                    display_error($petty_cash_float_transaction_types[$line['type_id']]." ".$line["id"].$error_msg); 
                    return;
                }
                break;

            default:
                cancel_transaction();
                display_error("Unknown transaction type.".$line['type_id'].". Transaction id ".$line["id"]);        
                return;
        }
        
        
    }  

    if($bank_payment_id > 0){ // reimbursing with a previous payment
        $temp_reimbursement_total = 0;    
    }
    else{
        $temp_reimbursement_total = $reimbursement_total;
    }
    add_to_petty_cash_float(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id,$float_id, $temp_reimbursement_total, $_POST['tran_date']);

    if($from_float_id > 0){
        add_to_petty_cash_float(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id, $from_float_id, -$reimbursement_total, $_POST['tran_date']); 
        // No GL transactions here and will be created by FI
    }

    if($from_bank_account_id > 0){
        if($bank_payment_id > 0){ // reimbursing with a previous payment
            // Related GL entries for the bank payment already created
        }
        else{ // reimbursing with a new payment
            // Create all GL entries ONLY when reimbursing with a new bank payment
            $date_ = $_POST['tran_date'];

            add_bank_trans(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id, $from_bank_account_id, $_POST['ref'],
                         $date_, -$reimbursement_total, 0, "", get_company_currency(),
                         "Cannot insert a destination bank transaction");
            
            $gl_memo = "Reimbursing ".$float['name'];
            // Bank account GL entry
            add_gl_trans(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id, $date_, $credit_account, 0, 0, $gl_memo,
		-$reimbursement_total, null, PT_FLOAT, $float_id); 
            
            // Float GL entry
            add_gl_trans(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id, $date_, $debit_account, 0, 0, $gl_memo,
		$reimbursement_total, null, PT_FLOAT, $float_id); 
            
            add_comments(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id, $date_, $gl_memo);
        
            add_audit_trail(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id, $date_);

        }
    }
//$credit_account
//    $bank_account = get_bank_account($bank_account_id);

//        add_gl_trans(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id, $date_, $credit_account,
//                        0, 0, $_POST['ref'], -$reimbursement_total);
//
//        add_gl_trans(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id, $date_, $debit_account,
//                        0, 0, $_POST['ref'], $reimbursement_total);

    $Refs->save(ST_PETTY_CASH_FLOAT_REIMBUR, $reimbursement_id, $_POST['ref']);
    commit_transaction();
    meta_forward($_SERVER['PHP_SELF'], "Added_ID=$reimbursement_id");
              
    
    
} 
    

function build_float_reimbursement_array($float_id, $from_date, $to_date, &$selected_lines, &$reimbursement_total, $reimbursement_method)
{
    
    if($reimbursement_method == 1){ //Payments
        $result = get_iou_payments_for_float_reimbursement($float_id, $from_date, $to_date);     
        add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);

        $result = get_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date);     
        add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);
        
        $result = get_iou_advances_for_float_reimbursement($float_id, $from_date, $to_date);     
        add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);
    
        $result = get_project_float_advances_for_float_reimbursement($float_id, $from_date, $to_date);     
        add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);
    }
    else{
        $result = get_iou_expenses_for_float_reimbursement($float_id, $from_date, $to_date);
        add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);        
    }
    
    $result = get_direct_petty_cash_payments_for_float_reimbursement($float_id, $from_date, $to_date);     
    add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);
    
    $result = get_supplier_payments_for_float_reimbursement($float_id, $from_date, $to_date);     
    add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);
    
    $result = get_cash_transactions_for_float_reimbursement(ST_CASH_PAYMENT, $float_id, $from_date, $to_date);     
    add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);
    
    $result = get_cash_transactions_for_float_reimbursement(ST_CASH_RECEIPT, $float_id, $from_date, $to_date);     
    add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);
    
    $result = get_petty_cash_float_adjustments_for_float_reimbursement($float_id, $from_date, $to_date);     
    add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);
    
    $result = get_petty_cash_float_reimbursements_for_float_reimbursement($float_id, $from_date, $to_date);     
    add_to_float_reimbursement_array($result, $reimbursement_total, $selected_lines);
    
    
    
 
}

function add_to_float_reimbursement_array($result, &$reimbursement_total, &$selected_lines)
{
    while($myrow = db_fetch($result))            
    {                  
//        label_cell($myrow["amount"]);
        $amount = $myrow["amount"];
         
        if((isset($_POST[$myrow["type_id"]."_"."selected_".$myrow["id"]])) && ($_POST[$myrow["type_id"]."_"."selected_".$myrow["id"]] == true))
        {           
//            $total_amount += $myrow["amount"];  

//            $reimbursement_amount = $myrow["amount"];
            $reimbursing_amount = input_num($myrow["type_id"]."_".'reimbursement_amount_'.$myrow["id"]);
            
            if($amount > 0){
                $reimbursement_total += $reimbursing_amount;
            }
            else{
                $reimbursement_total -= $reimbursing_amount;
            }
            $myrow['reimbursing_amount'] = $reimbursing_amount;
            $selected_lines[] = $myrow;
            
        }                       
    } 
}

function check_reimbursement_details($selected_lines)
{
    global $systypes_array;
        
    if (!is_date_in_fiscalyear($_POST['tran_date'])) {
        display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
        set_focus('tran_date');
        return false;
    } 

    if(isset_numeric_post('bank_account')){
        $msg = is_accounting_period_open($_POST['tran_date']);
        if ($msg['success'] !== true)
        {
            display_error($msg['message']);
            set_focus('tran_date');
            return false;
        }
    }
    
    
    foreach ($selected_lines as $line) { 
        $tran_amount = abs($line['amount']);
        $reimbursed_amount = abs($line['reimbursed_amount']);
        $reimbursing_amount = $line['reimbursing_amount'];
        
        $total_reimbursible_amount = $reimbursed_amount + $reimbursing_amount;
        $reimbursible_amount = $tran_amount - $reimbursed_amount;
        if($tran_amount < $total_reimbursible_amount){
            $m =  $systypes_array[$line['type_id']]." #".$line['id'].": "." maximum possible reimbursement amount is ".number_format2($reimbursible_amount,2);
            display_error($m);
            $name = $line['type_id']."_"."reimbursement_amount_".$line['id'];
            set_focus($name);
            return false;
        }
    }
    
    return true;
}


function check_reimbursement_header_data($float_id)
{
    global $Refs;
    
    if(!($float_id > 0)){ // Can not be the same "reimbursing into" float
        display_error("Reimbursement requesting Float is not selected.");
        set_focus("float_id");
        return false;        
    }
    
    if (strlen($_POST['ref']) == 0){
        $message = "Reference is mandatory.";
        $set_focus = 'ref';
        return false;
    }
    
    $ref_msg = reference_exists($_POST['ref'], ST_PETTY_CASH_FLOAT_REIMBUR);
    if ($ref_msg) 
    {
        display_error($ref_msg);
        set_focus('ref');
        return false;
    }
        
    if(!is_date($_POST['tran_date']))
    { 
        display_error("Date entered is not a valid date");
        set_focus('tran_date');
        return false;        
    }
    
        
    if (!is_date_in_fiscalyear($_POST['tran_date'])) {
        display_error(_("The entered invoice date is not in fiscal year."));
        set_focus('tran_date');
        return false;        
    }
        
    $msg = is_accounting_period_open($_POST['tran_date']);
    if ($msg['success'] !== true)
    {
        display_error($msg['message']);
        set_focus('tran_date');
        return false;        
    }
    
    unset($_POST["account_code"]);
    
    if(get_post('from_float_id') > 0){ //A float is selected
        if(get_post('from_float_id') == $float_id){ // Can not be the same "reimbursing into" float
            display_error("The Float providing the reimbursement has to be different from the Float requesting reimbursement.");
            set_focus("from_float_id");
            return false;        
        }
        if(strlen($_POST["bank_account"]) > 0){ // Can not select an account
            display_error("You can reimburse either from a float or a bank account. Can not select both.");
            set_focus("bank_account");
            return false;        
        }
//        
//        // Validate Float adjustment for the Float providing reimbursement
//        $_POST['adj_reference'] = $Refs->get_next(ST_PETTY_CASH_FLOAT_ADJ);
//        if (strlen($_POST['adj_reference']) == 0){
//            display_error("Reference generated for the Float Reimbursement Adjustment entry is blank.");
//            return false;
//        }
//        if (!is_new_reference($_POST['reference'], ST_PETTY_CASH_FLOAT_ADJ)){
//            display_error( _("Reference generated for the Float Reimbursement Adjustment entry is already in use."));
//            return false;
//        }
    }
    elseif(get_post('bank_account') > 0){//A Bank Account selected
        $float = get_petty_cash_float_line($float_id);
        if(!(isset($float['account_code']) && strlen($float['account_code'])) > 0){
            display_error("No debit account has been set for the reimbursement requesting float.");
            set_focus("debit_account");
            return false;        
        } 
    }
    else{ // Nothing selected
        display_error("Please select either a Float or a Bank Account.");
        set_focus("debit_account");
        return false;             
    }
    
    
    if(get_post('bank_payment_id') > 0){
        $bp_msg = check_bank_payment_already_used(get_post('bank_payment_id'));
        if($bp_msg['success'] !== true){
            display_error($bp_msg['message']);
            set_focus("bank_payment_id");
            return false;        
        }
    }
    
    return true;    
}


function build_bank_float_payments_array(&$payments_array, $float_id, $bank_acc_id)
{
    $bank_trans_res = get_bank_float_payments($float_id, $bank_acc_id);
    
    while ($row = db_fetch($bank_trans_res)) {
        $s = "(".$row['id'].")"." Rs.".number_format(-$row['amount'],2)." on ".sql2date($row['trans_date'])." - ".$row['gl_trans_type_name'];
        $a = array("id"=>$row['id'], "description"=>$s);
        
        $payments_array[$row['id']] = $s;
    }
}


function display_float_reimbursement_controles($float_id)
{
    global $Refs;
    
    div_start('float_reim');
    
    start_table(TABLESTYLE2);

    ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_PETTY_CASH_FLOAT_REIMBUR));
    petty_cash_floats_list_row("From Float:", "from_float_id", null, "Select a Float");
    bank_accounts_list_row("Bank Account", 'bank_account', null, true, "Select a Bank Account");
    //gl_all_accounts_list_row("Account:", 'debit_account', null, true, false,"Select an account");

    if($_POST['bank_account'] > 0){
        $payments_array = array();
        build_bank_float_payments_array($payments_array, $float_id, $_POST['bank_account']);
        custom_values_list_row("Payments To Float:", 'bank_payment_id', $payments_array, null, 'Create new bank payment', true);
        $bank_trans = get_bank_trans_by_id($_POST['bank_payment_id']);
        if(is_array($bank_trans)){
            label_row("Amount:", number_format(-$bank_trans['amount'],2));
        }
    }

    date_row("Date", 'tran_date');

    end_table(TABLESTYLE2); 
    
    div_end();
}