<?php

function fi_account_inquiry_controls() {

    global $payment_person_all_types;
    
    $dim = get_company_pref('use_dimension');
    
    start_table(TABLESTYLE_NOBORDER);
	start_row();
            gl_all_accounts_list_cells(_("Account:"), 'account', null, false, false, _("All Accounts"));

	end_row();
//    end_table();
//
//    start_table(TABLESTYLE_NOBORDER);
	start_row();
            journal_types_list_cells(_("Type:"), "filterType");
            text_cells("Trans No.", 'filterTransNo'); 
        end_row();
    end_table();
    
    start_table(TABLESTYLE_NOBORDER);
	start_row();
//            journal_types_list_cells(_("Type:"), "filterType");
            date_cells(_("from:"), 'TransFromDate', '', null, -30);
            date_cells(_("to:"), 'TransToDate');
            
            small_amount_cells(_("Amount min:"), 'amount_min', null, " ");
            small_amount_cells(_("Amount max:"), 'amount_max', null, " ");

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

    
    echo '<hr>';
}

function display_fi_account_transactions() {
    
    global $path_to_root, $systypes_array;

    if (!isset($_POST["account"]))
        $_POST["account"] = null;

    $act_name = $_POST["account"] ? get_gl_account_name($_POST["account"]) : "";
	$dim = get_company_pref('use_dimension');

    /*Now get the transactions  */
    
	$result = get_fi_account_transactions($_POST['TransFromDate'], $_POST['TransToDate'], $_POST['filterTransNo'], $_POST["account"],  
                                       $_POST['filterType'], input_num('amount_min'), input_num('amount_max'));

        $colspan =6;

	if ($_POST["account"] != null){
            display_heading($_POST["account"]. "&nbsp;&nbsp;&nbsp;".$act_name);
        }

	// Only show balances if an account is specified AND we're not filtering by amounts
	$show_balances = $_POST["account"] != null && input_num("amount_min") == 0 && input_num("amount_max") == 0;
		
	start_table(TABLESTYLE, "width=95%");
	
            $first_cols = array(_("Type"), _("#"), _("Date"));
	
            $account_col = array(_("Account"));

            $remaining_cols = array(_("Debit"), _("Credit"), _("Memo"), _("GL Batch"));

            $th = array_merge($first_cols, $account_col, $remaining_cols);

            table_header($th);

            $begin = $_POST['TransFromDate'];

            $bfw = 0;


            $running_total = $bfw;
            $j = 1;
            $k = 0; //row colour counter
            $debits = $credits = 0;

                while ($myrow = db_fetch($result)) {

                    alt_table_row_color($k);

                        $running_total += $myrow["amount"];
                        
                        if($myrow["amount"] > 0){
                            $debits += $myrow["amount"];
                        }
                        if($myrow["amount"] < 0){
                            $credits += $myrow["amount"];
                        }

                        $trandate = sql2date($myrow["tran_date"]);

                        label_cell($systypes_array[$myrow["type"]]);
//                        label_cell(get_gl_view_str($myrow["type"], $myrow["type_no"], $myrow["type_no"], true));
                        label_cell(get_trans_view_str($myrow["type"], $myrow["type_no"], $myrow["type_no"]), "align=center");
                        
                        label_cell($trandate);


                        label_cell($myrow["account"] . ' ' . get_gl_account_name($myrow["account"]));
                
                            display_debit_or_credit_cells($myrow["amount"]);
//                                amount_cell($running_total);
                            if ($myrow['memo_'] == "")
                                    $myrow['memo_'] = get_comments_string($myrow['type'], $myrow['type_no']);
                            label_cell($myrow['memo_']);
                            
                        label_cell(get_trans_view_str(ST_GL_BATCH, $myrow["gl_batch_no"], $myrow["gl_batch_no"]), "align=center");
                        
                    end_row();

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


                start_row("class='inquirybg'");
                    label_cell("<b>" . _("Summary") . "</b>", "colspan=4");
                    amount_cell($debits, true);
                    amount_cell(abs($credits), true); 
                    label_cell("");
                    label_cell("");
                end_row();
                
            

	end_table(2);
        
	if (db_num_rows($result) == 0)
            display_note(_("No general ledger transactions have been created for the specified criteria."), 0, 1);

}

?>