<?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 . "/manufacturing/includes/ui/job_cost_adjustments_inquiry_ui.inc");
include_once($path_to_root . "/manufacturing/includes/db/job_cost_adjustments_inquiry_db.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_JOB_COST_ADDITION] = $systypes_array[ST_JOB_COST_ADDITION];
$values_array[ST_JOB_COST_DEDUCTION] = $systypes_array[ST_JOB_COST_DEDUCTION]; 



//$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);
            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);
            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 _total($row) {
        return number_format($row["total"], 2);
    }

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

    function _comment($row) {
        return $row['comments']; 
    }

    function edit_link($row) {

//        $pre_jor = get_pre_journal($row["type"], $row["trans_no"]);
        if (in_array($row['approval_status'], array(AS_NEW, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR, AS_WITH_THE_APPROVER))){  
            switch ($row["type"]) {
                case ST_JOB_COST_ADDITION:
                    return pager_link(_("Edit"), "/manufacturing/job_cost_additions.php?JCA_No=". $row['trans_no'], ICON_EDIT);
                 
                case ST_JOB_COST_DEDUCTION:
                    return pager_link(_("Edit"), "/manufacturing/job_cost_deductions.php?JCA_No=". $row['trans_no'], ICON_EDIT);
                
                default:
                    return null;
            } 
        }
        else{
            return null;
        }
    }

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

    $sql = get_sql_for_job_cost_adjustments_inquiry(get_post('filterType', -1), get_post('FromDate'), get_post('ToDate'), get_post('Ref'), get_post('tran_id'));

    
    $cols = array(_("Type") => array('fun'=>'_systype_name'),                     
                    _("Trans #") => array('fun'=>'view_link', 'align'=>'center'), 
                    _("Reference") => array('fun' => '_reference'),
                    _("Date") =>array('fun'=>'_tran_date','align'=>'center'), 
                    _("Total") => array('fun' => '_total', 'align'=>'right'), 
                    _("Comment") => array('fun' => '_comment'), 
                    _("Approval Status") => array('fun' => '_approval_status', 'align'=>'center'),
        
                    array('insert'=>true, 'fun'=>'edit_link', 'align'=>'center'));


        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 = "70%";

    br(1);
    display_db_pager($table);

end_form();
end_page();