<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/


function view_batch_bank_transfer($type_id, $trans_no)
{
    view_display_batch_bank_transfer_header($type_id, $trans_no);
    
    br();
    
    $removed = view_display_batch_bank_transfer_details($type_id, $trans_no);
    
    return $removed;
    
}

function view_display_batch_bank_transfer_details($type_id, $trans_no)
{
    global $systypes_array;
    
    $result = get_batch_bank_transfer_details($type_id, $trans_no);
    div_start('saved_iou_details');

    start_table(TABLESTYLE, "width=90%");

    $th = array("Type","#","Reference", "Date", "Amount", _("Person/Item"),"GL Trans Type", "Comments", "");
    table_header($th);

    $iou_total = $removed = 0;
    $k=0; //row colour counter

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

        //label_cells('',$myrow["id"]);
        label_cell($systypes_array[$row["source_type"]]);
        $link = get_trans_view_str($row["source_type"], $row["source_trans_no"]);
        label_cell($link, "align=center");
        label_cell($row["reference"]);
        label_cell($row["trans_date"]);
        amount_cell(-$row["amount"]);
        label_cell(payment_person_name($row["person_type_id"], $row["person_id"]));
        label_cell($row["gl_trans_type_name"]);
        label_cell(get_comments_string($row["source_type"], $row["source_trans_no"]));
        
        if($row["removed_by"] > 0){
            label_cell("Removed", "style=color:red");
            $removed = 1;
        }else{
            label_cell("");
        }

        
        $iou_total += -$row["amount"];
    }
    
    start_row();
        label_cell("Total", "colspan=4 align=right");
        amount_cell($iou_total, true);
    end_row();
    
    end_table(1);
    div_end();
    
    return $removed;
}


function view_display_batch_bank_transfer_header($type_id, $trans_no)
{
    global $systypes_array;
    
    $batch = view_batch_bank_transfer_header($type_id, $trans_no); 
    
    $company_currency = get_company_currency();

    $show_currencies = false;
    $show_both_amounts = false;

    if (($batch['from_bank_curr_code'] != $company_currency) || ($batch['to_bank_curr_code'] != $company_currency)){
            $show_currencies = true;
    }

    if ($batch['from_bank_curr_code'] != $batch['to_bank_curr_code']) 
    {
            $show_currencies = true;
            $show_both_amounts = true;
    }



    echo "<br>";
    start_table(TABLESTYLE, "width=40%");
    label_row(_("Reference"), $batch['reference'], "class='tableheader2'");
    label_row(_("Date"), sql2date($batch['trans_date']), "class='tableheader2'");

    label_row(_("From Bank Account"), $batch['from_bank_account_name'], "class='tableheader2'");
    if ($show_currencies){
            label_row(_("Currency"), $batch['from_bank_curr_code'], "class='tableheader2'");
    }
    label_row(_("Amount"), number_format2($batch['amount'], user_price_dec()), "class='tableheader2'", "align=right");
	
    label_row(_("To Bank Account"), $batch['to_bank_account_name'], "class='tableheader2'");
    if ($show_currencies){
            label_row(_("Currency"), $batch['to_bank_curr_code'], "class='tableheader2'");
    }
    if ($show_both_amounts){
            label_row(_("Amount"), number_format2($batch['amount'], user_price_dec()), "class='tableheader2'", "align=right");
    }

    if($batch['fund_transfer_id'] > 0){
        $bbtr_link = get_banking_trans_view_str(ST_BANKTRANSFER, $batch["fund_transfer_id"]);
        label_row(_($systypes_array[ST_BANKTRANSFER]." #"), $bbtr_link, "class='tableheader2'");
    }

    comments_display_row(ST_BANKTRANSFER, $trans_no, $batch['comment']);

    end_table(1);

}