<?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>.
***********************************************************************/
$path_to_root = "..";
include_once($path_to_root . "/includes/ui/items_cart.inc");
include_once($path_to_root . "/includes/session.inc");

$page_security = isset($_GET['NewGLDebitAdjust']) || 
                    @($_SESSION['pre_gl_adjustment_items']->trans_type == ST_GL_CREDIT) ? 'SA_GL_CREDIT' : 'SA_GL_DEBIT';


include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/gl/includes/ui/pre_gl_adjustments_ui.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/gl/includes/db/pre_gl_adjustments_db.inc");
include_once($path_to_root . "/gl/includes/gl_ui.inc");
include_once($path_to_root . "/admin/db/attachments_db.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_pre_journal.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_pre_trans.inc");
//include_once($path_to_root . "/gl/includes/ui/gl_adjustments_ui.inc");
//include_once($path_to_root . "/gl/includes/db/gl_adjustments_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/payroll/includes/db/emp_trans_db.inc");
include_once($path_to_root . "/gl/includes/db/gl_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();


if (isset($_GET['NewGLCreditAdjust'])) {
    $_SESSION['page_title'] = _($help_context = "GL Credit Adjustment Entry");
    $name = 'pre_gl_credit_items';
    create_pre_gl_adjustment_cart($name, ST_GL_CREDIT, 0);
} 
else if(isset($_GET['NewGLDebitAdjust'])) {
    $_SESSION['page_title'] = _($help_context = "GL Debit Adjustment Entry");
    $name = 'pre_gl_debit_items';
    create_pre_gl_adjustment_cart($name, ST_GL_DEBIT, 0);
} 
else if (isset($_GET['EditGLCreditAdjust'])) {
    $_SESSION['page_title'] = _($help_context = "Modify GL Credit Adjustment")." #".$_GET['EditGLCreditAdjust'];
    $name = 'pre_gl_credit_items';
    create_pre_gl_adjustment_cart($name, ST_GL_CREDIT, $_GET['EditGLCreditAdjust']);
} 
else if(isset($_GET['EditGLDebitAdjust'])) {
    $_SESSION['page_title'] = _($help_context = "GL Debit Adjustment Edit")." #".$_GET['EditGLDebitAdjust'];
    $name = 'pre_gl_debit_items';
    create_pre_gl_adjustment_cart($name, ST_GL_DEBIT, $_GET['EditGLDebitAdjust']);
}


if(isset($_SESSION['pre_gl_credit_items'])){
    $name = 'pre_gl_credit_items';
}
elseif(isset($_SESSION['pre_gl_debit_items'])){
    $name = 'pre_gl_debit_items';
}

page($_SESSION['page_title'], false, false, '', $js);

//-----------------------------------------------------------------------------------------------

global $Ajax;

if (list_updated('PersonDetailID')) {
    $br = get_branch(get_post('PersonDetailID'));
    $_POST['person_id'] = $br['debtor_no'];
    $Ajax->activate('person_id');
}

//-----------------------------------------------------------------------------------------------

global $systypes_array;

if (isset($_GET['AddedCrdID'])) {
    $trans_no = $_GET['AddedCrdID'];
    $trans_type = ST_GL_CREDIT;

    display_notification_centered(sprintf(_($systypes_array[$trans_type]." %d has been saved"), $trans_no));

//    display_note(get_gl_view_str($trans_type, $trans_no, _("&View the Pre GL Postings for this GL Credit")));

    hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another ".$systypes_array[$trans_type]), "NewGLCreditAdjust=yes");

    hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

    display_footer_exit();
}

if (isset($_GET['AddedDbtID'])) {
    $trans_no = $_GET['AddedDbtID'];
    $trans_type = ST_GL_DEBIT;

    display_notification_centered(sprintf(_($systypes_array[$trans_type]." %d has been saved"), $trans_no));

//    display_note(get_gl_view_str($trans_type, $trans_no, _("View the Pre GL Postings for this GL Debit")));

    hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another ".$systypes_array[$trans_type]), "NewGLDebitAdjust=yes");

    hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

    display_footer_exit();
}

if (isset($_GET['GL_CRD_Approved'])) {
    $trans_no = $_GET['GL_CRD_Approved'];
    $trans_type = ST_GL_CREDIT;
    
    display_notification_centered(sprintf(_($systypes_array[$trans_type]." %d has been approved"), $trans_no));
    display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this ".$systypes_array[$trans_type])));
    hyperlink_params("$path_to_root/gl/inquiry/pre_gl_adjustments_inquiry.php", _("Approve another ".$systypes_array[$trans_type]), "filterType=$trans_type");
    display_footer_exit();
}

if (isset($_GET['GL_DEB_Approved'])) {
    $trans_no = $_GET['GL_DEB_Approved'];
    $trans_type = ST_GL_DEBIT;
    
    display_notification_centered(sprintf(_($systypes_array[$trans_type]." %d has been approved"), $trans_no));
    display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this ".$systypes_array[$trans_type])));
    hyperlink_params("$path_to_root/gl/inquiry/pre_gl_adjustments_inquiry.php", _("Approve another  ".$systypes_array[$trans_type]), "filterType=$trans_type");
    display_footer_exit();
}

//-----------------------------------------------------------------------------------------------

if (isset($_POST['Process'])) {
    if(!check_pre_gl_adjustment_trans($name)){
        $trans_array = process_pre_gl_adjustment_transaction($name, false);

        if (isset($trans_array['message'])) {
            display_error($trans_array['message']);
        } else {
            if ($trans_array[0] == ST_GL_CREDIT) {
                meta_forward($_SERVER['PHP_SELF'], "AddedCrdID=$trans_array[1]");
            } else {
                meta_forward($_SERVER['PHP_SELF'], "AddedDbtID=$trans_array[1]");
            }
        }
    }
}

if (isset($_POST['Post'])){
    if(!check_pre_gl_adjustment_trans($name)) {
        $trans_array = process_pre_gl_adjustment_transaction($name, true);
        if (isset($trans_array['message'])) {
            display_error($trans_array['message']);
        } else {
            if ($trans_array[0] == ST_GL_CREDIT) {
                meta_forward($_SERVER['PHP_SELF'], "GL_CRD_Approved=$trans_array[1]");
            } else {
                meta_forward($_SERVER['PHP_SELF'], "GL_DEB_Approved=$trans_array[1]");
            }
        }
    }
}

//-----------------------------------------------------------------------------------------------

$id = find_submit('Delete');
if ($id != -1)
    handle_delete_pre_gl_adjustment_item($name, $id);

if (isset($_POST['AddItem']))
    handle_new_pre_gl_adjustment_item($name);

if (isset($_POST['UpdateItem']))
    handle_update_pre_gl_adjustment_item($name);

if (isset($_POST['CancelItemChanges']) || isset($_POST['Index']))
    pre_gl_adjustment_line_start_focus();

if (isset($_POST['go'])) {
    display_quick_entries($_SESSION[$name], $_POST['person_id'], input_num('totamount'), 
                          $_SESSION[$name]->trans_type == ST_GL_CREDIT ? QE_PAYMENT : QE_DEPOSIT);
    $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
    pre_gl_adjustment_line_start_focus();
}

//-----------------------------------------------------------------------------------------------

start_form();
    if (isset($_POST['header_total_amount'])) {
        hidden('header_total_amount', $_POST['header_total_amount']);
    }
    display_pre_gl_adjustment_header($_SESSION[$name]);

    start_table(TABLESTYLE2, "width='90%'", 10);
        start_row();
            echo "<td>";
                display_pre_gl_adjustment_items($_SESSION[$name]->trans_type == ST_GL_CREDIT ? _("Debiting Items") : _("Crediting Items"), 
                                                $_SESSION[$name]);
                pre_gl_adjustment_options_controls($_SESSION[$name]);
            echo "</td>";
        end_row();
    end_table(1);
    
    $Ajax->activate('Buttons');
    

    div_start('Buttons');
    submit_center_first('Process',_("Save"), false, false, ICON_SUBMIT);
    br(2);

//$pre_jor = get_pre_journal($_SESSION['cash_items']->trans_type, $_SESSION['cash_items']->order_id);
//if (is_array($pre_jor) && ! ($pre_jor['approved_by'] > 0)) {


    if ($_SESSION[$name]->count_gl_items() >= 1 && $_SESSION["wa_current_user"]->can_access_page('SA_APPROVE_GL_ADJUST')) {
        submit_center_first('Post', 'Approve', false, false, ICON_SUBMIT);  
    }
    div_end();

    br(2);
    end_form();

end_page();