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


function display_gl_view_heading($myrow)
{
    global $systypes_array;
    
    $trans_name = $systypes_array[$_GET['type_id']];
    display_heading("GL Transaction - $trans_name #".$_GET['trans_no']);
    
    br(1);
    
    start_table(TABLESTYLE, "width=80%");
//    $th = array(_("General Ledger Transaction Details"), _("Reference"),
//    	_("Date"), _("Person/Item"));
//    table_header($th);	
    
    start_row();	
        label_cells("Transaction Type", $trans_name, "class='tableheader2'");
        label_cells("Transaction Date", sql2date($myrow["tran_date"]), "class='tableheader2'");
    end_row();
    
    start_row();        
        $link = get_trans_view_str($_GET['type_id'], $_GET['trans_no'], $myrow["reference"]);
        label_cells("Reference", $link, "class='tableheader2'");
        if ($myrow["person_type_id"] == PT_EMPLOYEE) {
            $emp_tra = get_emp_trans($myrow["type_no"], $myrow["type"]);
                label_cells("Person/Item", payment_person_name($myrow["person_type_id"], $emp_tra["company_code"], true, $emp_tra["employee_id"]), "class='tableheader2'");
        } else {
            label_cells("Person/Item", payment_person_name($myrow["person_type_id"], $myrow["person_id"]), "class='tableheader2'");
        }
    
    end_row();
    start_row();

    $cheque = get_printed_cheque_details($_GET['type_id'], $_GET['trans_no']);
    if(isset($cheque['id']) && $cheque['id'] > 0){
        start_row();
            label_cells("Cheque No", $cheque["cheque_no"], "class='tableheader2'");
            label_cells("Cheque Date", sql2date($cheque["cheque_date"]), "class='tableheader2'");
        end_row();
    }
    
//    if (in_array($myrow['type'], array(ST_JOURNAL, ST_BANKPAYMENT, ST_BANKDEPOSIT, ST_GL_CREDIT, ST_GL_DEBIT, ST_CASH_PAYMENT, ST_CASH_RECEIPT))) {
    if (in_array($myrow['type'], array(ST_JOURNAL))) {
        start_row();     
            $gl_trns_type = get_list_value($myrow['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($_GET['type_id'], $_GET['trans_no']);

    end_table(1);
}


function display_gl_view_transactions($type_id, $trans_no)
{
    global $systypes_array;
    
    $result = get_gl_trans($type_id, $trans_no);

    if (db_num_rows($result) == 0)
    {
        echo "<p><center>" . _("No general ledger transactions found for") . " " .$systypes_array[$type_id]." " . _("number") . " " . $trans_no . "</center></p><br><br>";
            end_page(true);
            exit;
    }

    /*show a table of the transactions returned by the sql */
    $dim = get_company_pref('use_dimension');

    if ($dim == 2)
            $th = array(_("Account Code"), _("Account Name"), _("Dimension")." 1", _("Dimension")." 2",
                    _("Debit"), _("Credit"), _("Memo"));
    else if ($dim == 1)
            $th = array(_("Account Code"), _("Account Name"), _("Dimension"),
                    _("Debit"), _("Credit"), _("Memo"));
    else		
            $th = array(_("Account Code"), _("Account Name"), _("Project"),
                    _("Debit"), _("Credit"), _("Memo"), _("Line Id"));
    $k = 0; //row colour counter
    $heading_shown = false;

    $credit = $debit = 0;
    while ($myrow = db_fetch($result)) 
    {
            if ($myrow['amount'] == 0) continue;
            if (!$heading_shown)
            {
                    display_gl_view_heading($myrow);
                    start_table(TABLESTYLE, "width=95%");
                    table_header($th);
                    $heading_shown = true;
            }	

            alt_table_row_color($k);

            label_cell($myrow['account']);
            label_cell($myrow['account_name']);
            
            $project_formatted_code = get_project_formatted_code($myrow['dimension_id'], $myrow['project_code']);
            label_cell($project_formatted_code, "align=center");
            if ($dim >= 1)
                    label_cell(get_dimension_string($myrow['dimension_id'], true));
            if ($dim > 1)
                    label_cell(get_dimension_string($myrow['dimension2_id'], true));

            display_debit_or_credit_cells($myrow['amount']);
            label_cell($myrow['memo_']);
            label_cell($myrow['counter'], 'align=center');
            end_row();
        if ($myrow['amount'] > 0 ) 
            $debit += $myrow['amount'];
        else 
            $credit += $myrow['amount'];
    }
    if ($heading_shown)
    {
        start_row("class='inquirybg' style='font-weight:bold'");
        label_cell(_("Total"), "colspan=3");
        if ($dim >= 1)
            label_cell('');
        if ($dim > 1)
            label_cell('');
        amount_cell($debit);
        amount_cell(-$credit);
        label_cell('');
        label_cell('');
        end_row();
            end_table(1);
    }

}