<?php



function display_bank_account_reconciliation_filtering_controls()
{
    start_table(TABLESTYLE_NOBORDER);
    start_row();
        bank_accounts_list_cells(_("Account:"), 'bank_account', null, true, '');
    end_row();
    start_row();
    bank_reconciliation_list_cells(_("Reconciled Date:"), get_post('bank_account'),
	'filtering_reconciled_date', null, true, _("New"));
//    date_cells(_("From:"), 'filtering_reconciled_date', '', null, -30);

    submit_cells('Show',_("Show"),'','', 'default');
    end_row();
    end_table();
}

function display_bank_account_reconciliation_header($bank_account, $filtering_reconciled_date)
{
        $act = get_bank_account($bank_account);
        
        $rec = get_bank_reconciliation_header($bank_account, $filtering_reconciled_date);	
        
    $th = array(nl2br("Statement \r\nBeginning \r\nBalance"), nl2br("Statement \r\nEnding \r\nBalance"), nl2br("Account \r\nBalance"), 
                nl2br("Reconciled \r\nAmount"), nl2br("Reconciled \r\nDifference"), 
               nl2br("Unreconciled \r\nDebits"), nl2br("Unreconciled \r\nCredits"), _("User") );
    
    start_table(TABLESTYLE, "width=70%");
    
    table_header($th);
    
    if (isset($rec['id']) && $rec['id'] > 0) {
        start_row();
            label_cell(number_format($rec["statement_beginning_balance"], 2), 'align=right');
            label_cell(number_format($rec["statement_ending_balance"],2), 'align=right');
            label_cell(number_format($rec["account_balance"],2), 'align=right');
            label_cell(number_format($rec["reconciled_amount"],2), 'align=right');
            label_cell(number_format($rec["reconciled_difference"],2), 'align=right');
            label_cell(number_format($rec["unreconciled_debits"],2), 'align=right');
            label_cell(number_format($rec["unreconciled_credits"],2), 'align=right');
            label_cell($rec["user"], 'align=center');

       end_table(2);
    }
}


function display_bank_account_reconciliation_details($bank_account, $filtering_reconciled_date)
{
    global $systypes_array;
    
    $result = get_bank_reconciliation_details($bank_account, $filtering_reconciled_date);	

    


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

    $th = array(_("Type"), _("#"), _("Reference"), _("Date"),
            _("Debit"), _("Credit"), _("Balance"), _("Person/Item"), _("Memo"));
    table_header($th);

//    $bfw = get_balance_before_for_bank_account($_POST['bank_account'], $_POST['TransAfterDate']);
//
//    $credit = $debit = 0;
//    start_row("class='inquirybg' style='font-weight:bold'");
//    label_cell(_("Opening Balance")." - ".$_POST['TransAfterDate'], "colspan=4");
//    display_debit_or_credit_cells($bfw);
//    label_cell("");
//    label_cell("", "colspan=2");
//
//    end_row();
//    $running_total = $bfw;
//    if ($bfw > 0 ) 
//            $debit += $bfw;
//    else 
//            $credit += $bfw;
    $j = 1;
    $running_total = $debit = $credit = 0;
    $k = 0; //row colour counter
    while ($myrow = db_fetch($result))
    {

	alt_table_row_color($k);

	$running_total += $myrow["amount"];

	$trandate = sql2date($myrow["trans_date"]);
	label_cell($systypes_array[$myrow["type"]]);
	label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"]));
	label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"],$myrow['ref']));
	label_cell($trandate);
	display_debit_or_credit_cells($myrow["amount"]);
	amount_cell($running_total);
	label_cell(payment_person_name($myrow["person_type_id"],$myrow["person_id"]));
	label_cell(get_comments_string($myrow["type"], $myrow["trans_no"]));
//	label_cell(get_gl_view_str($myrow["type"], $myrow["trans_no"]));
	end_row();
 	if ($myrow["amount"] > 0 ) 
 		$debit += $myrow["amount"];
 	else 
 		$credit += $myrow["amount"];

	if ($j == 12)
	{
		$j = 1;
		table_header($th);
	}
	$j++;
    }
    //end of while loop

    start_row("class='inquirybg' style='font-weight:bold'");
    label_cell(_("Totals"), "colspan=4");
    amount_cell($debit);
    amount_cell(-$credit);
    //display_debit_or_credit_cells($running_total);
    amount_cell($debit+$credit);
    label_cell("");
    label_cell("", "colspan=2");
    end_row();
    end_table(2);
}

function display_bank_account_unreconciled_details($bank_account, $filtering_reconciled_date, $debits)
{
    global $systypes_array;
    
    $rec = get_bank_reconciliation_header($bank_account, $filtering_reconciled_date);
    
    if(isset($rec['unreconciled_ids']) && strlen(($rec['unreconciled_ids']) > 0)){
        $unreconciled_ids = $rec['unreconciled_ids'];
    }
    else{
        $unreconciled_ids = -1;
    }
    $result = get_bank_unreconciled_details($bank_account, $unreconciled_ids, $debits);	

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

    $th = array(_("Type"), _("#"), _("Reference"), _("Date"),
            _("Debit"), _("Credit"), _("Balance"), _("Person/Item"), _("Memo"));
    table_header($th);


    $j = 1;
    $running_total = $debit = $credit = $previous_type = 0;
    $k = 0; //row colour counter
    while ($myrow = db_fetch($result))
    {
        if ($previous_type > 0 && $previous_type != $myrow["type"])
	{
            start_row();
            if($debits){
                label_cell(text_bold($systypes_array[$previous_type]."s Total"), "colspan=4 align=right");
                amount_cell($debit, true);
                label_cell("", "colspan=4");
            }
            else{
                label_cell(text_bold($systypes_array[$previous_type]."s Total"), "colspan=5 align=right");
                amount_cell(abs($credit), true);
                label_cell("", "colspan=3");
            }
            end_row();
            
            $debit = $credit = 0;
	}
	alt_table_row_color($k);

	$running_total += $myrow["amount"];

	$trandate = sql2date($myrow["trans_date"]);
	label_cell($systypes_array[$myrow["type"]]);
	label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"]));
	label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"],$myrow['ref']));
	label_cell($trandate);
	display_debit_or_credit_cells($myrow["amount"]);
	amount_cell($running_total);
	label_cell(payment_person_name($myrow["person_type_id"],$myrow["person_id"]));
	label_cell(get_comments_string($myrow["type"], $myrow["trans_no"]));
//	label_cell(get_gl_view_str($myrow["type"], $myrow["trans_no"]));
	end_row();
 	if ($myrow["amount"] > 0 ) 
 		$debit += $myrow["amount"];
 	else 
 		$credit += $myrow["amount"];

	
        
        
        
        $previous_type = $myrow["type"];
        
	$j++;
    }
    
    if ($previous_type > 0)
    {
        start_row();
        if($debits){
            label_cell(text_bold($systypes_array[$previous_type]."s Total"), "colspan=4 align=right");
            amount_cell($debit, true);
            label_cell("", "colspan=4");
        }
        else{
            label_cell(text_bold($systypes_array[$previous_type]."s Total"), "colspan=5 align=right");
            amount_cell(abs($credit), true);
            label_cell("", "colspan=3");
        }
        end_row();

        $debit = $credit = 0;
    }
    //end of while loop

    start_row("class='inquirybg' style='font-weight:bold'");
    
    if($debits){
        label_cell(_("Debits Totals"), "colspan=6 align=right");
    }
    else{
        label_cell(_("Credits Totals"), "colspan=6 align=right");
    }
//    amount_cell($debit);
//    amount_cell(-$credit);
    //display_debit_or_credit_cells($running_total);
    amount_cell($running_total);
    label_cell("");
    label_cell("", "colspan=2");
    end_row();
    end_table(2);
}