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


function display_fi_batch_header_view($batch_no)
{
    global $approval_statuses;
    
    $batch_row = view_fi_batch_header($batch_no);
    if(!(isset($batch_row['id']) && $batch_row['id'] > 0)){
        return false;
    }

    display_heading2("FI Batch");
    
    start_table(TABLESTYLE2, "width=50%");

    $th = array(_("FI Batch No"), _("Cutoff Date"), _("Generated By"), _("Generated On"), _("Status"), _("GL Batch No"));

    table_header($th);

    label_cell($batch_no, "align=center");
    label_cell(sql2date($batch_row['cutoff_date']), "align=center");
    label_cell($batch_row['real_name']);
    label_cell($batch_row['created_on'], "align=center");
    label_cell($approval_statuses[$batch_row['status']], "align=center");

    if($batch_row['gl_batch_no'] > 0){
        $link = get_journal_trans_view_str(ST_GL_BATCH, $batch_row['gl_batch_no']);
    }
    else{
        $link = '';
    }
    label_cell($link, "align=center");  


    end_table(1);
    
    hidden('fi_batch_number', $batch_no);
    
    return true;
}


function display_fi_batch_transactions_view($batch_no)
{
 
    $result = view_fi_batches($batch_no);
    
    if(!(db_num_rows($result) > 0)){
        br(2);
        display_note("No entries found for this FI batch number.");
        br(2);
        return false;
    }
    
    display_heading2("Transactions");
    
    start_table(TABLESTYLE2, "width=95%");


    $th = array(_("Type"),_("Trans No"), _('Date'), _('Account'), _("Debit"), _("Credit"), _("Comment"), _("Project Code"), _("FI Line Id"), _("GL Line Id"));                 

    table_header($th);

    $k=0;
    $total_credits = $total_debits = $credit = $debit = 0;
    $previous_trans = $this_trans = '';
    while($myrow = db_fetch($result)){

        global $systypes_array;
        
        $this_trans = $myrow["type"]."_".$myrow["type_no"];
        if($previous_trans != '' && $this_trans != $previous_trans){
            start_row();
                label_cell(_(""), "colspan=4 align=right");
                amount_cell($debit, true);
                amount_cell(-$credit, true);
                
                if(abs($debit+$credit) > 0.0001){
                    $m = "Debits and credits do not match";
                    $m = "<font color='red'>".$m."</font>";
                    label_cell($m, "colspan=3");
                }
                else{
                    label_cell('', "colspan=3");
                }
            end_row();
            
            $credit = $debit = 0;
        }

        alt_table_row_color($k);

        label_cell($systypes_array[$myrow['type']]);    
        
        if($myrow['type'] == ST_PAYROLL){
            $payroll_rec = get_fi_batch_transaction_record($batch_no, $myrow["type"], $myrow["type_no"]);
            $db_name = $payroll_rec['db_name'];
        }
        else{
            $db_name = '';
        }

        if($myrow["type"] == ST_PAYROLL){
            label_cell(get_payroll_trans_view_str($myrow["type"], $myrow["type_no"], "", false, '', '', $db_name), "align=center");
        }
        else{
            label_cell(get_trans_view_str($myrow["type"], $myrow["type_no"], "", false, '', '', $db_name), "align=center");
        }
        
        label_cell(sql2date($myrow['tran_date']), "align=center");
        label_cell($myrow['account_code']." - ".$myrow['account_name']);
        display_debit_or_credit_cells($myrow['amount']);
    
        label_cell(nl2br($myrow['memo_']));
        
        $project_code = "";
        if($myrow['dimension_id'] > 0){
            $project_code = $myrow['project_code'];
        }
        label_cell($project_code, "align=center");
        
        if($myrow['counter'] > 0){
            label_cell($myrow['counter'], "align=center");
        }
        else{
            label_cell('');
        }

        if($myrow['gl_counter'] > 0){
            label_cell($myrow['gl_counter'], "align=center");
        }
        else{
            label_cell('');
        }

    //    label_cell($myrow['amount'], "align=right");
        if ($myrow['amount'] > 0 ){
            $debit += $myrow['amount'];
            $total_debits += $myrow['amount'];
        }
        else{
            $credit += $myrow['amount'];
            $total_credits += $myrow['amount'];
        }
        
        $previous_trans = $this_trans;
    }
    
    if($previous_trans != ''){
        start_row();
            label_cell(_(""), "colspan=4 align=right");
            amount_cell($debit, true);
            amount_cell(-$credit, true);
            
            if(abs($debit+$credit) > 0.0001){
                $m = "Debits and credits do not match";
                $m = "<font color='red'>".$m."</font>";
                label_cell($m, "colspan=3");
            }
            else{
                label_cell('', "colspan=3");
            }
        end_row();

        $credit = $debit = 0;
    }

    start_row("class='inquirybg' style='font-weight:bold'");
    label_cell(_("Total". spaces(10)), "colspan=4 align=right");
    amount_cell($total_debits);
    amount_cell(-$total_credits);
    label_cell('', "colspan=3");
    
    end_row();
	
    end_table(2);
    
    return true;

}


function display_fi_batch_transactions_summary($batch_no)
{
    global $systypes_array;
    
    $result = view_fi_batch_summary($batch_no);
    
    if(!(db_num_rows($result) > 0)){
        br(2);
        display_note("No entries found for this FI batch number.");
        br(2);
        return false;
    }
    
    display_heading2("Summary");
    
    start_table(TABLESTYLE2, "width=60%");


    $th = array(_('Type'), _('Account'), _("Debit"), _("Credit"));                 

    table_header($th);

    $k=0;
    $total_credits = $total_debits = 0;

    while($myrow = db_fetch($result)){
        start_row();
        
        label_cell($systypes_array[$myrow["type"]]);
        label_cell($myrow['account_code']." - ".$myrow['account_name']);
        amount_cell($myrow['debits_sum']);
        amount_cell(-$myrow['credits_sum']);


    //    label_cell($myrow['amount'], "align=right");

            $total_debits += $myrow['debits_sum'];

            $total_credits += $myrow['credits_sum'];
        
    }    

    start_row();
        if(abs($total_debits+$total_credits) > 0.0001){
            $m = "Debits and credits do not match";
            $m = "<font color='red'>".$m."</font>";
            label_cell($m, "colspan=2");
        }
        else{
            label_cell('', "colspan=2");
        }
//        label_cell(_(""), "colspan=2 align=right");
        amount_cell($total_debits, true);
        amount_cell(-$total_credits, true);

        
    end_row();    

//    start_row("class='inquirybg' style='font-weight:bold'");
//    label_cell(_("Total". spaces(10)), "colspan=4 align=right");
//    amount_cell($total_debits);
//    amount_cell(-$total_credits);
//    label_cell('', "colspan=3");
//    
//    end_row();
	
    end_table(1);
    
    return true;

}