<?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 write_pre_gl_adjustment_transaction($trans_type, $trans_no, $act_code, $items, $date_, $person_type_id, $person_id, $person_detail_id, $ref, $memo_, 
                                             $currency, $exchange_rate=1, $float_id=0, $gl_trans_type_id=0) {
    global $Refs, $systypes_array;
    
    if (!($trans_no > 0)) {
        $trans_no = get_next_trans_no($trans_type);
    }
    
    
    $pre_jor = get_pre_journal($trans_type, $trans_no);
    if (is_array($pre_jor)) {
        if ($pre_jor['approved_by'] > 0) {
            cancel_transaction();
            return array($trans_type, $trans_no, 'message' => 'This '.$systypes_array[$trans_type].' is already approved.'); 
        } else {
            $total_amount = $items->gl_items_total();
            update_pre_journal($trans_type, $trans_no, $act_code, -$total_amount, $date_, $currency, $ref, $ref, $exchange_rate, $date_, $date_, $person_type_id, $person_id, 
                               $person_detail_id, $float_id, $gl_trans_type_id, $memo_);
            delete_pre_gl_adjustment_details($trans_type, $trans_no);
        }
    } else {
        $total_amount = $items->gl_items_total();
        add_pre_journal($trans_type, $trans_no, $act_code, -$total_amount, $date_ , $currency, $ref, $ref, $exchange_rate, $date_, $date_, $person_type_id, $person_id, 
                        $person_detail_id, $float_id, $gl_trans_type_id, $memo_);
    }
    
    $total = 0;
    foreach ($items->gl_items as $gl_item) {
        $total += add_pre_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id, $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
                                   $gl_item->amount, $currency, $person_type_id, $person_id);
    }

    
    add_pre_gl_trans($trans_type, $trans_no, $date_, $act_code, 0, 0, $memo_, -$total, null, $person_type_id, $person_id);

//    add_comments($trans_type, $trans_no, $date_, $memo_);
    $Refs->save($trans_type, $trans_no, $ref);
    add_audit_trail($trans_type, $trans_no, $date_);

    return array($trans_type, $trans_no);
}

function get_pre_gl_adjustment_transaction_header($type, $trans_no) {
//    $sql = "SELECT pj.*,
//                    pg.account,
//                    pg.counter 
//            FROM ".TB_PREF."pre_journal pj 
//                JOIN ".TB_PREF."pre_gl_trans pg  
//                    ON pj.type = pg.type 
//                        AND pj.trans_no = pg.type_no 
//            WHERE pj.type = ".db_escape($type)."  
//                AND pj.trans_no = ".db_escape($trans_no)." 
//                AND pj.amount = pg.amount";
    
    $sql = "SELECT pj.* 
            FROM ".TB_PREF."pre_journal pj  
            WHERE pj.type = ".db_escape($type)."  
                AND pj.trans_no = ".db_escape($trans_no);

    $myrow = db_query($sql, 'get_pre_gl_adjustment_transaction_header');
    return db_fetch($myrow);
}

function get_pre_gl_adjustment_details($type, $trans_no) {
    $sql = "SELECT pj.*,
                    pg.counter,
                    pg.account 
            FROM ".TB_PREF."pre_journal pj 
                JOIN ".TB_PREF."pre_gl_trans pg  
                    ON pj.type = pg.type 
                        AND pj.trans_no = pg.type_no 
            WHERE pj.type = ".db_escape($type)."  
                AND pj.trans_no = ".db_escape($trans_no);

    return db_query($sql, 'get_pre_gl_adjustment_details');
}

function delete_pre_gl_adjustment_details($type, $trans_no) {
    $sql = "DELETE 
            FROM ".TB_PREF."pre_gl_trans   
            WHERE type = ".db_escape($type)."  
                AND type_no = ".db_escape($trans_no);

    db_query($sql, 'delete_pre_gl_adjustment_details');
}

function update_pre_gl_adjustment_approved($type, $trans_no) {
    $approved_by = $_SESSION['wa_current_user']->user;
    $approved_on = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."pre_journal 
            SET approval_status = ".db_escape(AS_APPROVED).", 
                approved_by = ".db_escape($approved_by).",
                approved_on = ".db_escape($approved_on)." 
            WHERE type = ".db_escape($type)."  
                AND trans_no = ".db_escape($trans_no)." 
                AND approved_by = 0 ";

    db_query($sql, 'update_pre_gl_adjustment_approved');
    
    return db_num_affected_rows();
}