<?php

function save_approved_transaction_changed_log($type, $log_array) {
    foreach ($log_array as $data) {
        $type = $data['type'];
        $trans_no = $data['trans_no'];
        $field_name = $data['field_name'];
        $previous_value = $data['previous_value'];
        $new_value = $data['new_value'];
            
            $in_id = add_approved_transaction_changed_log($type, $trans_no, $field_name, $previous_value, $new_value);
                if (! ($in_id > 0)) {
                    return array('success' => false, 'message' => 'Could not add approved transaction changed log record.');
                }
    }
    
        return array('success' => true, 'message' => '');
}

function add_approved_transaction_changed_log($type, $trans_no, $field_name, $previous_value, $new_value) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."add_approved_transaction_changed_log 
                      ( type, trans_no,  
                        field_name, previous_value, new_value, 
                        updated_by, updated_on )         
                    VALUES 
                    (".db_escape($type).", ".db_escape($trans_no).", 
                    ".db_escape($field_name).", ".db_escape($previous_value).", ".db_escape($new_value).", 
                    ".db_escape($updated_by).", ".db_escape($updated_on).")";

        db_query($sql, 'add_approved_transaction_changed_log');
        return db_insert_id();        
}

function get_utility_bill_header_record($ub_no) {
    $sql = "SELECT * 
            FROM ".TB_PREF."utility_bills 
            WHERE id = ".db_escape($ub_no);
    
    $result = db_query($sql);
    return db_fetch($result);   
}

function update_approved_transaction_utility_bill_header($ub_no, $entry_type_id, $due_date_) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."utility_bills
                SET entry_type_id = ".db_escape($entry_type_id).",
                    due_date = ".db_escape(date2sql($due_date_)).",
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on)."       
                WHERE id = ".db_escape($ub_no);

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

function update_approved_transaction_supp_trans_due_date($trans_no, $type, $due_date_) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."supp_trans 
                SET due_date = ".db_escape(date2sql($due_date_)).",
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on)."       
                WHERE type = ".db_escape($type)." 
                    AND trans_no = ".db_escape($trans_no);

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

?>