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

include($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_pre_trans.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_pre_journal.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();

page(_($help_context = "GL Adjustments Inquiry"), false, false, "", $js);

if (get_post('Search')) {
    $Ajax->activate('journal_tbl');
}

if (isset($_GET['filterType'])) {
    $_POST['filterType'] = $_GET['filterType'];
}
else{
//    $_POST['filterType'] = -1;
}


$values_array[ST_CASH_PAYMENT] = $systypes_array[ST_CASH_PAYMENT];
$values_array[ST_CASH_RECEIPT] = $systypes_array[ST_CASH_RECEIPT];
$values_array[ST_GL_CREDIT] = $systypes_array[ST_GL_CREDIT];
$values_array[ST_GL_DEBIT] = $systypes_array[ST_GL_DEBIT];



$status_array[-1] = "All";
$status_array[AS_NEW] = "Unapproved";
$status_array[AS_APPROVED] = $approval_statuses[AS_APPROVED];


start_form();
    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(_("#:"), 'tran_id');
            ref_cells(_("Reference:"), 'Ref', '',null, _('Enter reference fragment or leave empty'));
//            journal_types_list_cells(_("Type:"), "filterType");
            custom_values_list_cells(_("Type:"), "filterType", $values_array, null, '', true);
        end_row();    
        start_row();
            date_cells(_("From:"), 'FromDate', '', null, -14);
            date_cells(_("To:"), 'ToDate');
//            two_value_list_cells("Status:", 'status', null, "Approved", "Unapproved", AS_APPROVED, 0, true, 'All');
            custom_values_list_cells(_("Status:"), "status", $status_array, null, false, true);
            
        end_row();
        start_row();
            $payment_person_all_types[-1] = '';
            ksort($payment_person_all_types);
            custom_values_list_cells('Person Type:', 'person_type_id', $payment_person_all_types, null, false, true);
            $gl_tr_tp_array = array("'GL_TRANS_TYPE_CASH_PAYMENT'", "'GL_TRANS_TYPE_CASH_RECEIPT'", 
                                    "'GL_TRANS_TYPE_GL_CREDIT'", "'GL_TRANS_TYPE_GL_DEBIT'");
            $list_codes_in = implode(',', $gl_tr_tp_array);
            filtered_value_list_cells('GL Trans Type:', 'gl_trans_type_id', $list_codes_in, '', null, '', true); 
            //        end_row();
            ref_cells(_("Memo:"), 'Memo', '',null, _('Enter memo fragment or leave empty'));
            submit_cells('Search', _("Search"), '', '', 'default');
        end_row();
    end_table();

    function _reference($row) {
        return $row['reference'];
    }
    
    function _amount($row) {
        return number_format($row['amount'],2);
    }
    
    function _tran_date($row) {
        return sql2date($row['tran_date']);
    }

    function _systype_name($row) {
        global $systypes_array;
        return $systypes_array[$row["type"]];
    }

    function _approval_status($row) {
        global $approval_statuses;
        return $approval_statuses[$row["approval_status"]];
    }

    function _person_link($row) {
        return payment_person_name($row["person_type_id"],$row["person_id"], true, $row["person_detail_id"]);
    }

    function view_link($row) {
        return get_trans_view_str($row["type"], $row["trans_no"]);
    }

    function gl_link($row) {
        if ($row['approved_by'] == 0){
            return null;
        }
        return get_gl_view_str($row["type"], $row["trans_no"]);
    }

    function edit_link($row) {

//        $pre_jor = get_pre_journal($row["type"], $row["trans_no"]);
        if ($row['approved_by'] > 0){
            return null;
        }
        else{  
            switch ($row["type"]) {
                case ST_GL_DEBIT:
                    return pager_link(_("Edit"), "/gl/pre_gl_debit.php?EditGLDebitAdjust=". $row['trans_no'], ICON_EDIT);
                 
                case ST_GL_CREDIT:
                    return pager_link(_("Edit"), "/gl/pre_gl_credit.php?EditGLCreditAdjust=". $row['trans_no'], ICON_EDIT);

                case ST_CASH_PAYMENT:
                    return pager_link(_("Edit"), "/gl/gl_cash_payment.php?ModifyPayment=". $row['trans_no'], ICON_EDIT);
                 
                case ST_CASH_RECEIPT:
                    return pager_link(_("Edit"), "/gl/gl_cash_receipt.php?ModifyDeposit=". $row['trans_no'], ICON_EDIT);
                    
                default:
                    break;
            }
            if($row["trans_type"] == ST_GL_DEBIT){
                
            }
            else{
                
            }
        }
    }

    function invoice_supp_reference($row) {
        return $row['supp_reference'];
    }

    function _person_type($row) {
        global $payment_person_all_types;

        return (key_exists($row['person_type_id'], $payment_person_all_types)) ? $payment_person_all_types[$row['person_type_id']] : '';
    }
    
    function _gl_trans_type($row) {
        return $row['gl_trans_type_name'];
    }
    
    function _comment($row) {
        return $row['comment'];
    }
    
    $sql = get_sql_for_pre_gl_adjustments_inquiry(get_post('filterType',-1), get_post('FromDate'), get_post('ToDate'), 
                                                    get_post('Ref'), get_post('tran_id'), get_post('status',-1),
                                                    get_post('person_type_id',-1), get_post('gl_trans_type_id',-1), get_post('Memo', ''));

    
    $cols = array(
                    _("Date") =>array('fun'=>'_tran_date','align'=>'center'), 
                    _("Type") => array('fun'=>'_systype_name'),                     
                    _("Trans #") => array('fun'=>'view_link', 'align'=>'center'), 
                    _("Reference") => array('fun' => '_reference'),
                    _("Person Type") => array('fun' => '_person_type'),
                    _("Person") => array('fun' => '_person_link'),
                    _("GL Trans Type") => array('fun' => '_gl_trans_type'),
                    _("Amount") => array('fun' => '_amount', 'align'=>'right'), 
                    _("Comment") => array('fun' => '_comment'), 
                    _("Approval Status") => array('fun' => '_approval_status', 'align'=>'center'), 
                    _("View") => array('insert'=>true, 'fun'=>'gl_link', 'align'=>'center'),
        
                    array('insert'=>true, 'fun'=>'edit_link', 'align'=>'center'));

        if (! check_value('AlsoClosed')) {
            $cols[_("#")] = 'skip';
        }

        if ($_POST['filterType'] == ST_SUPPINVOICE) { //add the payment column if shown supplier invoices only
            $cols[_("Supplier's Reference")] = array('fun'=>'invoice_supp_reference', 'align'=>'center');
        }

    $table =& new_db_pager('journal_tbl', $sql, $cols);
    $table->width = "95%";

    br(1);
    display_db_pager($table);

end_form();
end_page();