<?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>.
***********************************************************************/
$page_security = 'SA_BANKTRANSVIEW';
$path_to_root = "../..";

include($path_to_root . "/includes/session.inc");

//page(_($help_context = "View Bank Deposit"), true);

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/includes/ui.inc");

include_once($path_to_root . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_pre_journal.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_pre_trans.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_db.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc"); 


$js = '';

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

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

if (isset($_GET["trans_no"]))
{
	$trans_no = $_GET["trans_no"];
}

global $systypes_array;
page(_($help_context = "View ".$systypes_array[ST_CASH_RECEIPT]), true, false, "", $js);




// get the pay-to bank payment info
//$result = get_bank_trans(ST_CASH_RECEIPT, $trans_no);

$journal = get_pre_journal_header(ST_CASH_RECEIPT, $trans_no);
$reimbursement = get_petty_cash_float_trans_record($journal['float_id'], ST_CASH_RECEIPT, $trans_no);
//if (db_num_rows($result) != 1)
//	display_db_error("duplicate payment bank transaction found", "");
//
//$to_trans = db_fetch($result);

$company_currency = get_company_currency();

$show_currencies = false;

global $approval_statuses;

$from_trans_res = get_pre_gl_trans(ST_CASH_RECEIPT, $trans_no);
while ($trans = db_fetch($from_trans_res))
{
    if($trans["amount"] <= 0){
        continue;
    }
    $to_trans = $trans;
}
    
$curr_default = get_company_pref('curr_default');
if ($journal['currency'] != $curr_default)
{
	$show_currencies = true;
}

echo "<center>";

display_heading($systypes_array[ST_CASH_RECEIPT] . " #$trans_no");

echo "<br>";
start_table(TABLESTYLE, "width='80%'");

if ($show_currencies)
{
	$colspan1 = 1;
	$colspan2 = 7;
}
else
{
	$colspan1 = 1;
	$colspan2 = 4;
}

start_row();
    label_cells(_("Received Into Float"), $journal['float_name'], "class='tableheader2'");
    if ($show_currencies){
            label_cells(_("Currency"), $to_trans['bank_curr_code'], "class='tableheader2'");
    }
    label_cells(_("Amount"), number_format2($to_trans['amount'], user_price_dec()), "class='tableheader2'");
    
end_row();
start_row();
    if($journal['person_type_id'] == PT_EMPLOYEE){
        $company_code = $journal['person_id'];
        $employee_id = $journal['person_detail_id'];
        $counterparty_name = get_group_company_employee_formatted_name_by_id($company_code, $employee_id);
    } elseif($journal['person_type_id'] == PT_MISC) {
        $counterparty_name = $journal['person_id'];
    } else{
        $counterparty_name = get_counterparty_name(ST_CASH_RECEIPT, $journal['trans_no']);
    }
        
    label_cells(_("Received Into Account"),  get_gl_account_formatted_name($to_trans['account']), "class='tableheader2'");
    
    if ($show_currencies)
    {
            label_cells(_("Settle currency"), $to_trans['settle_curr'], "class='tableheader2'");
            label_cells(_("Settled amount"),  number_format2($to_trans['settled_amount'], user_price_dec()), "class='tableheader2'");
    }
//    label_cells(_("Deposit Type"), $bank_transfer_types[$to_trans['account_type']], "class='tableheader2'");
//    label_cells(_("Date"), sql2date($journal['tran_date']), "class='tableheader2'");
    
    $reimbursed_amount = 0;
    if (isset($reimbursement['id']) && $reimbursement['id'] > 0) {
        $reimbursed_amount = $reimbursement['reimbursed_amount'];
    }
    
    $link = get_petty_cash_float_reimbursement_details_view_str(ST_CASH_RECEIPT, $journal['trans_no'], $reimbursed_amount);
    label_cells(_("Reimbursed Amount"), $link, "class='tableheader2'");
end_row();
start_row();
    label_cells(_("Paid By"), $counterparty_name, "class='tableheader2'", "colspan=$colspan1");
    label_cells(_("Approval Status"), $journal['approved_by'] > 0 ? $approval_statuses[30] : $approval_statuses[0], "class='tableheader2'", "colspan=$colspan1");
end_row();
start_row();
//    label_cells(_("Approval Status"), $journal['approved_by'] > 0 ? $approval_statuses[30] : $approval_statuses[0], "class='tableheader2'", "colspan=$colspan1");
//    label_cells(_("Approved By"), $journal['approver_name'], "class='tableheader2'");
    label_cells(_("Reference"), $journal['reference'], "class='tableheader2'", "colspan=$colspan1");
    label_cells(_("Approved By"), $journal['approver_name'], "class='tableheader2'");
end_row();

start_row();
    label_cells(_("Date"), sql2date($journal['tran_date']), "class='tableheader2'");
    $gl_trns_type = get_list_value($journal['gl_trans_type_id']);
        if (is_array($gl_trns_type)) {
            label_cells(_("GL Trans Type"), $gl_trns_type['value_name'], "class='tableheader2'");
        } else {
            label_cells(_("GL Trans Type"), '', "class='tableheader2'");
        }
end_row();

comments_display_row(ST_CASH_RECEIPT, $trans_no, $journal['comment']);

end_table(1);

$voided = is_voided_display(ST_CASH_RECEIPT, $trans_no, _("This payment has been voided."));

$items = get_pre_gl_trans(ST_CASH_RECEIPT, $trans_no);

if (db_num_rows($items) == 0)
{
	display_note(_("There are no items for this deposit."));
}
else
{

	display_heading2(_("Items for this Deposit"));
	if ($show_currencies)
		display_heading2(_("Item Amounts are Shown in:") . " " . $company_currency);

    start_table(TABLESTYLE, "width='80%'");
    $dim = get_company_pref('use_dimension');
    if ($dim == 2)
        $th = array(_("Account Code"), _("Account Description"), _("Dimension")." 1", _("Dimension")." 2",
            _("Amount"), _("Memo"));
    elseif ($dim == 1)
        $th = array(_("Account Code"), _("Account Description"), _("Dimension"),
            _("Amount"), _("Memo"));
    else
        $th = array(_("Account Code"), _("Account Description"),
            _("Amount"), _("Memo"));
    table_header($th);

    $k = 0; //row colour counter
	$total_amount = 0;

    while ($item = db_fetch($items))
    {

        if($item["amount"] > 0){
            continue;
        }
        
//        if ($item["account"] != $to_trans["account_code"])
		{
    		alt_table_row_color($k);

        	label_cell($item["account"]);
    		label_cell($item["account_name"]);
            if ($dim >= 1)
                label_cell(get_dimension_string($item['dimension_id'], true));
            if ($dim > 1)
                label_cell(get_dimension_string($item['dimension2_id'], true));
            amount_cell(-$item["amount"]);
    		label_cell($item["memo_"]);
    		end_row();
    		$total_amount += $item["amount"];
		}
	}

	label_row(_("Total"), number_format2(-$total_amount, user_price_dec()),"colspan=".(2+$dim)." align=right", "align=right");

	end_table(1);

        if (!$voided)
		display_allocations_from($journal['person_type_id'], $journal['person_id'], 1, $trans_no, $journal['amount'] * $journal['rate']);
//	display_allocations_from($to_trans['person_type_id'], $to_trans['person_id'], 2, $trans_no, $to_trans['settled_amount']);
}

end_page(true, false, false, ST_CASH_RECEIPT, $trans_no);
