<?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>.
***********************************************************************/

function display_pre_gl_adjustment_header(&$order) {
    global $Ajax;
//    ($order->trans_type == ST_GL_CREDIT) ? $debit = true : $debit = false;
    
    if($order->trans_type == ST_GL_CREDIT){
        $debit = true;
        $list_codes_in = "'GL_TRANS_TYPE_GL_CREDIT'";
    }
    elseif($order->trans_type == ST_GL_DEBIT){
        $debit = false;
        $list_codes_in = "'GL_TRANS_TYPE_GL_DEBIT'";
    }
    else{
        $list_codes_in = "''";
    }

    $customer_error = false;
    div_start('pmt_header');

	start_outer_table(TABLESTYLE2, "width='90%'"); // outer table
            table_section(1);
                date_row(_("Date:"), 'date_', '', true, 0, 0, 0, null, true);
                ref_row(_("Reference:"), 'ref', '', $order->reference, false, $order->trans_type, get_post('date_'));

            table_section(2, "33%");
                if (! isset($_POST['PayType'])) {
                    if (isset($_GET['PayType']))
                        $_POST['PayType'] = $_GET['PayType'];
                    else
                        $_POST['PayType'] = "";
                }
                
                if (! isset($_POST['person_id'])) {
                    if (isset($_GET['PayPerson']))
                        $_POST['person_id'] = $_GET['PayPerson'];
                    else
                        $_POST['person_id'] = "";
                }
                
                if (isset($_POST['_PayType_update'])) {
                    $_POST['person_id'] = '';
                    $Ajax->activate('pmt_header');
                    $Ajax->activate('code_id');
                    $Ajax->activate('pagehelp');
                    $Ajax->activate('editors');
                    $Ajax->activate('footer');
                }
    
                payment_person_types_list_row( $debit ? _("Crediting Person Type:") :_("Debiting Person Type:"), 'PayType', $_POST['PayType'], true);
                
                switch ($_POST['PayType']) {
                    case PT_MISC :
                        text_row_ex($debit ? _("To the Order of:") : _("Name:"), 'person_id', 40, 50);
                        filtered_value_list_row('GL Trans Type:', 'gl_trans_type_id', $list_codes_in, "'PT_MISC'", null, 'Select a gl trans type', true);
                        break;
                    case PT_SUPPLIER :
                        supplier_list_row(_("Supplier:"), 'person_id', null, false, true, false, true);
                        filtered_value_list_row('GL Trans Type:', 'gl_trans_type_id', $list_codes_in, "'PT_SUPPLIER'", null, 'Select a gl trans type', true);
                        break;
                    case PT_CUSTOMER :
                        customer_list_row(_("Customer:"), 'person_id', null, false, true, false, true);

                        if (db_customer_has_branches($_POST['person_id'])) {
                            customer_branches_list_row(_("Branch:"), $_POST['person_id'], 'PersonDetailID', null, false, true, true, true);
                        } else {
                            $_POST['PersonDetailID'] = ANY_NUMERIC;
                            hidden('PersonDetailID');
                        }
                        
                        $trans = get_customer_habit($_POST['person_id']); // take care of customers on hold
                        if ($trans['dissallow_invoices'] != 0) {
                            if ($debit) {
                                $customer_error = true;
                                display_error(_("This customer account is on hold."));
                            }
                            else
                                display_warning(_("This customer account is on hold."));
                        }
                        filtered_value_list_row('GL Trans Type:', 'gl_trans_type_id', $list_codes_in, "'PT_CUSTOMER'", null, 'Select a gl trans type', true);
                        
                        break;

                    case PT_QUICKENTRY :
                        quick_entries_list_row(_("Type").":", 'person_id', null, ($debit ? ST_GL_CREDIT : ST_GL_DEBIT), true);
                        $qid = get_quick_entry(get_post('person_id'));
                        if (list_updated('person_id')) {
                            unset($_POST['totamount']); // enable default
                            $Ajax->activate('footer');
                            $Ajax->activate('totamount');
                        }
                        
                        if (isset($qid['id']) && $qid['id'] > 0) {
                            amount_row($qid['base_desc'].":", 'totamount', price_format($qid['base_amount']), null, "&nbsp;&nbsp;".submit('go', _("Go"), false, false, true));
                        }
                        filtered_value_list_row('GL Trans Type:', 'gl_trans_type_id', 'GL_TRANS_TYPES', "'PT_QUICKENTRY'", null, 'Select a gl trans type', true);
                        break;	
                        
                    case PT_EMPLOYEE :
                        group_companies_list_row("Company:", 'person_id', null, false, true);
                        if (list_updated('group_company_code')) {
                                $Ajax->activate('person_id');
                        }
                        group_company_employee_ids_simple_list_row(_("Employee:"), 'PersonDetailID', get_post('person_id'), null, false);  
                        filtered_value_list_row('GL Trans Type:', 'gl_trans_type_id', $list_codes_in, "'PT_EMPLOYEE'", null, 'Select a gl trans type', true);

                        break;	
                        
                    case PT_FLOAT :
                        petty_cash_floats_list_row('Float:', 'person_id', null, 'Select a float', true);
                        filtered_value_list_row('GL Trans Type:', 'gl_trans_type_id', $list_codes_in, "'PT_FLOAT'", null, 'Select a gl trans type', true);

                        break;
                }

            table_section(3, "33%");

                switch ($_POST['PayType']) {
                    case PT_MISC :
                    case PT_EMPLOYEE :
                        gl_all_accounts_list_row($debit ? _("Crediting Account:") :_("Debiting Account:"), 'act_code');
                        break;
                    case PT_SUPPLIER :
                        $supp = get_supplier_accounts($_POST['person_id']);
                        if (is_array($supp)) {
                            $sup_act_name = get_gl_account_formatted_name($supp['payable_account']);
                            label_row($debit ? _("Crediting Account:") :_("Debiting Account:"), $sup_act_name);
                            hidden('act_code', $supp['payable_account']);
                        } else {
                            label_row($debit ? _("Crediting Account:") :_("Debiting Account:"), '');
                            hidden('act_code', '');
                        }
                        break;
                    case PT_CUSTOMER :
                        $cus_bra = get_cust_branch($_POST['person_id'], $_POST['PersonDetailID']);
                        if(isset($cus_bra['receivables_account']) && strlen($cus_bra['receivables_account']) > 0){
                            $cus_act_name = get_gl_account_formatted_name($cus_bra['receivables_account']);
                        }
                        else{
                            $cus_act_name = "";
                        }
                        label_row($debit ? _("Crediting Account:") :_("Debiting Account:"), $cus_act_name);
                        hidden('act_code', $cus_bra['receivables_account']);                        
                        break;

                    case PT_QUICKENTRY :
                        quick_entries_list_row($debit ? "From Account:" : "Into Account:", 'person_id', null, ($debit ? ST_GL_CREDIT : ST_GL_DEBIT), true);
                        $qid = get_quick_entry(get_post('person_id'));
                        if (list_updated('person_id')) {
                            unset($_POST['totamount']); // enable default
                            $Ajax->activate('footer');
                            $Ajax->activate('totamount');
                        }
                        
                        if (isset($qid['id']) && $qid['id'] > 0) {
                            amount_row($qid['base_desc'].":", 'totamount', price_format($qid['base_amount']), null, "&nbsp;&nbsp;".submit('go', _("Go"), false, false, true));
                        }
                        break;	
                        
                    case PT_FLOAT :
                        $float = get_petty_cash_float_line($_POST['person_id']);
                        if (is_array($float) && strlen($float['account_code']) > 0) {
                            $float_act_name = get_gl_account_name($float['account_code']);
                            label_row($debit ? _("Crediting Account:") :_("Debiting Account:"), $float_act_name);
                            hidden('act_code', $float['account_code']);
                        } else {
                            label_row($debit ? _("Crediting Account:") :_("Debiting Account:"), '');
                            hidden('act_code', '');
                        }                        
                        break;
                }
                
//                if (isset($_POST['header_total_amount'])) {
//                    label_row('Amount:', $_POST['header_total_amount']);
//                }
 
	end_outer_table(1); // outer table

    div_end();
        
    if ($customer_error) {
        end_form();
        end_page();
        exit;
    }
}

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

function display_pre_gl_adjustment_items($title, &$order) {
    $dim = get_company_pref('use_dimension');
    $colspan = ($dim == 2 ? 4 : ($dim == 1 ? 3 : 2));
    display_heading($title);

    div_start('items_table');
	start_table(TABLESTYLE, "width='95%'");
            if ($dim == 2)
                $th = array(_("Account Code"), _("Account Description"), _("Dimension")." 1", _("Dimension")." 2", _("Amount"), _("Memo"), "");
            elseif ($dim == 1)
                $th = array(_("Account Code"), _("Account Description"), _("Dimension"), _("Amount"), _("Memo"), "");
            else
                $th = array(_("Account Code"), _("Account Description"), _("Amount"), _("Memo"), "");

            if (count($order->gl_items)) $th[] = '';

            table_header($th);
            $k = 0;  //row colour counter

            $id = find_submit('Edit');
            foreach ($order->gl_items as $line => $item) {
		if ($id != $line) {
                    alt_table_row_color($k);
			label_cell($item->code_id);
			label_cell($item->description);
                        if ($dim >= 1)
                            label_cell(get_dimension_string($item->dimension_id, true));
                        if ($dim > 1)
                            label_cell(get_dimension_string($item->dimension2_id, true));
			if ($order->trans_type == ST_GL_DEBIT)
                            amount_cell(-$item->amount);
			else		
                            amount_cell($item->amount);
			label_cell($item->reference);

			edit_button_cell("Edit$line", _("Edit"), _('Edit document line'));
			delete_button_cell("Delete$line", _("Delete"), _('Remove line from document'));
                    end_row();
		} else {
                    pre_gl_edit_item_controls($order, $dim, $line);
		}
            }

            if ($id == -1)
                pre_gl_edit_item_controls($order, $dim);

            if ($order->count_gl_items())
                label_row(_("Total"), number_format2(abs($order->gl_items_total()), user_price_dec()),"colspan=" . $colspan . " align=right", "align=right",3);

        end_table();
    div_end();
}

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

function pre_gl_edit_item_controls(&$order, $dim, $Index=null) {
    global $Ajax;
    ($order->trans_type == ST_GL_CREDIT) ? $debit = true : $debit = false;

    start_row();
    
	$id = find_submit('Edit');
	if ($Index != -1 && $Index == $id) {
            $item = $order->gl_items[$Index];
            $_POST['code_id'] = $item->code_id;
            $_POST['dimension_id'] = $item->dimension_id;
            $_POST['dimension2_id'] = $item->dimension2_id;
            $_POST['amount'] = price_format(abs($item->amount));
            $_POST['description'] = $item->description;
            $_POST['LineMemo'] = $item->reference;

            hidden('Index', $id);
	    $Ajax->activate('items_table');
	} else {
            if (isset($_POST['_code_id_update'])) {
                $Ajax->activate('code_id');
            }
            
            if (list_updated('PayType') || list_updated('person_id')) {
                if ($_POST['PayType'] == PT_CUSTOMER) {
                    $acc = get_branch_accounts($_POST['PersonDetailID']);
                    $_POST['code_id'] = $acc['receivables_account'];
                } elseif ($_POST['PayType'] == PT_SUPPLIER) {
                    $acc = get_supplier($_POST['person_id']);
                    if (is_array($acc)) {
                        $_POST['code_id'] = $acc['payable_account'];
                        $_POST['dimension_id'] = $acc['dimension_id'];
                        $_POST['dimension2_id'] = $acc['dimension2_id'];
                    } else {
                        $_POST['code_id'] = '';
                        $_POST['dimension_id'] = '';
                        $_POST['dimension2_id'] = '';
                    }
                } else {
                    $_POST['code_id'] =
                    get_company_pref($debit ? 'default_cogs_act' : 'default_inv_sales_act');
                }
                
                $Ajax->activate('dimension_id');
                $Ajax->activate('dimension2_id');
            }
        }
        
        echo gl_all_accounts_list('code_id', null, true, true);
        if ($dim >= 1)
            dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
        else
            hidden('dimension_id', 0);
        
        if ($dim > 1)
            dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
        else
            hidden('dimension2_id', 0);

        amount_cells(null, 'amount');
        text_cells_ex(null, 'LineMemo', 35, 255);

        if ($id != -1) {
            button_cell('UpdateItem', _("Update"), _('Confirm changes'), ICON_UPDATE);
            button_cell('CancelItemChanges', _("Cancel"), _('Cancel changes'), ICON_CANCEL);
            set_focus('amount');
        } else {
            submit_cells('AddItem', _("Add Item"), "colspan=2", _('Add new item to document'), true);
        }

    end_row();
}

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

function pre_gl_adjustment_options_controls($order) {
    div_start('footer');
	echo "<br><table align='center'>";
            $type = get_post('PayType');
            $company_curr = get_company_pref('curr_default');
            $person_curr = $type == PT_CUSTOMER ? get_customer_currency(get_post('person_id')) : 
                           ($type == PT_SUPPLIER ? get_supplier_currency(get_post('person_id')) : $company_curr);

            if ($person_curr != $company_curr) {
                $_POST['settled_amount'] =
                price_format(abs($order->gl_items_total() / get_exchange_rate_from_to($company_curr, $person_curr, get_post('date_'))));
                amount_row($type == PT_CUSTOMER ? _("Settled AR Amount:") :  _("Settled AP Amount:"), 'settled_amount', null, null, $person_curr, user_price_dec());
            }

            textarea_row(_("Memo"), 'memo_', null, 50, 3);
  	echo "</table>";
    div_end();
}

function check_pre_gl_adjustment_trans($name) {
    $input_error = 0;

    $pay_type = get_post('PayType');
    switch ($pay_type) {
        case PT_CUSTOMER:
            if (!(get_post('person_id') > 0) || !(get_post('PersonDetailID') > 0)) {
                display_error(_("You have to select customer and customer branch."));
                set_focus('person_id');
                $input_error = 1;
            }
            break;

        case PT_SUPPLIER:
            if (!(get_post('person_id') > 0)) {
                display_error(_("You have to select supplier."));
                set_focus('person_id');
                $input_error = 1;
            }
            break;

        case PT_FLOAT:
            if (!(get_post('person_id') > 0)) {
                display_error(_("You have to select float."));
                set_focus('person_id');
                $input_error = 1;
            }
    }
    
    if (! isset_string_post('act_code')) {
        $acc_text = $_SESSION[$name]->trans_type == ST_GL_DEBIT ? _("debiting") : _("crediting");
        display_error(_("No $acc_text account found."));
        $input_error = 1;
    }

    if ($_SESSION[$name]->count_gl_items() < 1) {
        display_error(_("You must enter at least one payment line."));
        set_focus('code_id');
        $input_error = 1;
    }

    if ($_SESSION[$name]->gl_items_total() == 0.0) {
        display_error(_("The total bank amount cannot be 0."));
        set_focus('code_id');
        $input_error = 1;
    }


    $msg = reference_exists($_POST['ref'], $_SESSION[$name]->trans_type, $_SESSION[$name]->order_id);
    if($msg){
        display_error(_($msg));
        set_focus('ref');
        $input_error = 1;
    }
    
    if (! is_date($_POST['date_'])) {
        display_error(_("The entered date for the payment is invalid."));
        set_focus('date_');
        $input_error = 1;
    } elseif (! is_date_in_fiscalyear($_POST['date_'])) {
        display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
        set_focus('date_');
        $input_error = 1;
    } 
    
    $msg = is_accounting_period_open($_POST['date_']);
    if ($msg['success'] !== true)
    {
        display_error($msg['message']);
        set_focus('date_');
        $input_error = 1;
    }

//    if (get_post('PayType') == PT_CUSTOMER && (! get_post('person_id') || ! get_post('PersonDetailID'))) {
//        display_error(_("You have to select customer and customer branch."));
//        set_focus('person_id');
//        $input_error = 1;
//    } elseif (get_post('PayType') == PT_SUPPLIER && (! get_post('person_id'))) {
//        display_error(_("You have to select supplier."));
//        set_focus('person_id');
//        $input_error = 1;
//    }
    
    

    if (get_post('PayType') == PT_CUSTOMER && (! get_post('person_id') || ! get_post('PersonDetailID'))) {
        $currency = get_customer_currency(get_post('person_id'), get_post('PersonDetailID'));
    } elseif (get_post('PayType') == PT_SUPPLIER && (! get_post('person_id'))) {
        $currency = get_supplier_currency(get_post('person_id'));
    } else {
        $currency = get_company_pref('curr_default');
    }  

    if (! db_has_currency_rates($currency, $_POST['date_'], true)) 
        $input_error = 1;
    
    if (! ($_POST['gl_trans_type_id'] > 0)) {
        display_error(_("Please select a gl tranaction type."));
        set_focus('gl_trans_type_id');
        $input_error = 1;
    }

    if (isset($_POST['settled_amount']) && in_array(get_post('PayType'), array(PT_SUPPLIER, PT_CUSTOMER)) && (input_num('settled_amount') <= 0)) {
        display_error(_("Settled amount have to be positive number."));
        set_focus('person_id');
        $input_error = 1;
    }
    
    return $input_error;
}

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

function check_pre_gl_adjustment_item_data() {
    if (! check_num('amount', 0)) {
        display_error( _("The amount entered is not a valid number or is less than zero."));
        set_focus('amount');
        return false;
    }
    
    if (isset($_POST['_ex_rate']) && input_num('_ex_rate') <= 0) {
        display_error( _("The exchange rate cannot be zero or a negative number."));
        set_focus('_ex_rate');
        return false;
    }

    return true;
}

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

function pre_gl_adjustment_line_start_focus() {
    global $Ajax;

    unset($_POST['amount']);
    unset($_POST['dimension_id']);
    unset($_POST['dimension2_id']);
    unset($_POST['LineMemo']);
    $Ajax->activate('items_table');
    $Ajax->activate('footer');
    set_focus('_code_id_edit');
}

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

function handle_update_pre_gl_adjustment_item($name) {
    $amount = ($_SESSION[$name]->trans_type == ST_GL_CREDIT ? 1:-1) * input_num('amount');
    if ($_POST['UpdateItem'] != "" && check_pre_gl_adjustment_item_data()) {
    	$_SESSION[$name]->update_gl_item($_POST['Index'], $_POST['code_id'], $_POST['dimension_id'], $_POST['dimension2_id'], $amount , $_POST['LineMemo']);
    }
    
    pre_gl_adjustment_line_start_focus();
}

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

function handle_delete_pre_gl_adjustment_item($name, $id) {
    $_SESSION[$name]->remove_gl_item($id);
    pre_gl_adjustment_line_start_focus();
}

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

function handle_new_pre_gl_adjustment_item($name) {
    if (! check_pre_gl_adjustment_item_data())
        return;
    $amount = ($_SESSION[$name]->trans_type == ST_GL_CREDIT ? 1 : -1) * input_num('amount');

    $_SESSION[$name]->add_gl_item($_POST['code_id'], $_POST['dimension_id'], $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
    pre_gl_adjustment_line_start_focus();
}

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

function create_pre_gl_adjustment_cart($name, $type, $trans_no) {
    global $Refs;

    if (isset($_SESSION[$name])) {
        unset ($_SESSION[$name]);
    }

    $cart = new items_cart($type);
    $cart->order_id = $trans_no;

    if ($trans_no > 0) {
        $pre_journal = get_pre_gl_adjustment_transaction_header($type, $trans_no);
        $_POST['act_code'] = $pre_journal["act_code"];
        $_POST['PayType'] = $pre_journal["person_type_id"];
        $cart->reference = $pre_journal["reference"];
        $_POST['header_total_amount'] = $pre_journal["amount"];
        $_POST['gl_trans_type_id'] = $pre_journal["gl_trans_type_id"];

        if ($pre_journal["person_type_id"] == PT_CUSTOMER) {	
            $_POST['person_id'] = $pre_journal["person_id"]; // person_id
            $_POST['PersonDetailID'] = $pre_journal["person_detail_id"];//person_detail_id
        } elseif ($pre_journal["person_type_id"] == PT_SUPPLIER) {
            $_POST['person_id'] = $pre_journal["person_id"]; // person_id
        }
        elseif ($pre_journal["person_type_id"] == PT_MISC)
            $_POST['person_id'] = $pre_journal["person_id"];
        elseif ($pre_journal["person_type_id"] == PT_QUICKENTRY)
            $_POST['person_id'] = $pre_journal["person_id"];
        else 
            $_POST['person_id'] = $pre_journal["person_id"];

//        $cart->memo_ = get_comments_string($type, $trans_no);
        $cart->memo_ = $pre_journal["comment"];
        $cart->tran_date = sql2date($pre_journal['tran_date']);//tran_date

        $cart->original_amount = $pre_journal['amount'];
        $result = get_pre_gl_trans($type, $trans_no);
        $ex_rate = 1;
        if ($result) {
            while ($row = db_fetch($result)) {
                if ($type == ST_GL_CREDIT && $row['amount'] < 0) {
                    continue;
                } elseif ($type == ST_GL_DEBIT && $row['amount'] > 0) {
                    continue;
                }
                
                if (is_bank_account($row['account'])) {
                    // date exchange rate is currenly not stored in bank transaction,
                    // so we have to restore it from original gl amounts
                    $ex_rate = $pre_journal['amount'] / $row['amount'];
                } else {
                    $cart->add_gl_item($row['account'], $row['dimension_id'], $row['dimension2_id'], $row['amount'], $row['memo_']);
                }
            }
        }

        // apply exchange rate
//        foreach($cart->gl_items as $line_no => $line)
//            $cart->gl_items[$line_no]->amount *= $ex_rate;

    } else {
        $cart->reference = $Refs->get_next($cart->trans_type, null, $cart->tran_date);
        $cart->tran_date = new_doc_date();
        if (! is_date_in_fiscalyear($cart->tran_date))
            $cart->tran_date = end_fiscalyear();
    }

    $_POST['memo_'] = $cart->memo_;
    $_POST['ref'] = $cart->reference;
    $_POST['date_'] = $cart->tran_date;

    $_SESSION[$name] = &$cart;
}

function process_pre_gl_adjustment_transaction($name, $approve=false) 
{
    begin_transaction();

	$_SESSION[$name] = &$_SESSION[$name];
//	$_SESSION[$name]->order_id == 0;

        if (get_post('PayType') == PT_CUSTOMER && (! get_post('person_id') || ! get_post('PersonDetailID'))) {
            $currency = get_customer_currency(get_post('person_id'), get_post('PersonDetailID'));
	} elseif (get_post('PayType') == PT_SUPPLIER && (! get_post('person_id'))) {
            $currency = get_supplier_currency(get_post('person_id'));
	} else {
            $currency = get_company_pref('curr_default');
        } 

	$trans = write_pre_gl_adjustment_transaction($_SESSION[$name]->trans_type, $_SESSION[$name]->order_id, $_POST['act_code'],
                                                     $_SESSION[$name], $_POST['date_'], $_POST['PayType'], $_POST['person_id'], 
                                                     get_post('PersonDetailID', 0), $_POST['ref'], $_POST['memo_'], $currency, input_num('_ex_rate'),
                                                     0, $_POST['gl_trans_type_id']);
        
        if (isset($trans['message'])) {
            return $trans;
        }
        
        $trans_type = $trans[0];
        $trans_no = $trans[1];
            
        if ($approve) {
            $trans = write_gl_transaction($_SESSION[$name]->trans_type, $trans_no, $_POST['act_code'], $_SESSION[$name], $_POST['date_'], 
                                          $_POST['PayType'], $_POST['person_id'], get_post('PersonDetailID', 0), $_POST['ref'], $_POST['memo_'], 
                                          $currency, input_num('_ex_rate'), null, 0, $_POST['gl_trans_type_id']);
            
            
            $n = update_pre_gl_adjustment_approved($trans_type, $trans_no);
            if($n != 1){
                cancel_transaction();
                return array($trans_type, 
                            $trans_no, 
                            'message' => 'This gl adjustment is already approved.');
            }
        }

        commit_transaction();
        
	new_doc_date($_POST['date_']);

	$_SESSION[$name]->clear_items();
	unset($_SESSION[$name]);

    
    
    return $trans;
}