<?php

function display_edit_gl_transaction_type_filterings() {
    start_table(TABLESTYLE_NOBORDER);
        start_row();
            custom_values_list_cells(_("Type:"), "type_id", get_edit_gl_transaction_types_array());
            ref_cells(_("Trans. No:"), 'trans_no', '',null, _('Enter reference fragment or leave empty'));
            ref_cells(_("Reference:"), 'reference', '',null, _('Enter reference fragment or leave empty'));
            date_cells(_("From:"), 'from_date', '', null, 0, -1, 0);
            date_cells(_("To:"), 'to_date');

            submit_cells('Search', _("Search"), '', '', 'default');
        end_row();
    end_table(2);
}

function get_edit_gl_transaction_types_array() {
    global $systypes_array;
    
    $gl_tranaction_types = array();
    
        $gl_tranaction_types[-1] = 'Select a gl trans type';
        $gl_tranaction_types[ST_JOURNAL] = $systypes_array[ST_JOURNAL];
        $gl_tranaction_types[ST_BANKPAYMENT] = $systypes_array[ST_BANKPAYMENT];
        $gl_tranaction_types[ST_BANKDEPOSIT] = $systypes_array[ST_BANKDEPOSIT];
        $gl_tranaction_types[ST_CASH_PAYMENT] = $systypes_array[ST_CASH_PAYMENT];
        $gl_tranaction_types[ST_CASH_RECEIPT] = $systypes_array[ST_CASH_RECEIPT];
        $gl_tranaction_types[ST_GL_DEBIT] = $systypes_array[ST_GL_DEBIT];
        $gl_tranaction_types[ST_GL_CREDIT] = $systypes_array[ST_GL_CREDIT];
        
        return $gl_tranaction_types;
}

function get_edit_gl_transaction_type_code($type_id) {
    $type_code = '';
    
        if ($type_id == ST_JOURNAL) {
            $type_code = "'GL_TRANS_TYPE_JOURNAL'";
        } elseif ($type_id == ST_BANKPAYMENT) {
            $type_code = "'GL_TRANS_TYPE_BANKPAYMENT'";
        } elseif ($type_id == ST_BANKDEPOSIT) {
            $type_code = "'GL_TRANS_TYPE_BANKDEPOSIT'";
        } elseif ($type_id == ST_CASH_PAYMENT) {
            $type_code = "'GL_TRANS_TYPE_CASH_PAYMENT'";
        } elseif ($type_id == ST_CASH_RECEIPT) {
            $type_code = "'GL_TRANS_TYPE_CASH_RECEIPT'";
        } elseif ($type_id == ST_GL_DEBIT) {
            $type_code = "'GL_TRANS_TYPE_GL_DEBIT'";
        } elseif ($type_id == ST_GL_CREDIT) {
            $type_code = "'GL_TRANS_TYPE_GL_CREDIT'";
        } 

            return $type_code;
}

function display_edit_gl_transaction_type_details($type_id, $trans_no, $reference, $from_date, $to_date, $status) {
    global $systypes_array, $payment_person_types, $Ajax;
    
    $Ajax->activate('edit_gl_trans_tp');
    div_start('edit_gl_trans_tp');
    
        start_table(TABLESTYLE2, "width=98%");

            $th = array(_("Date"), _("Trans. Type"), _("Trans. No."), _("Reference"), _("Person Type"), _("Person"), _("GL Trans. Type"), _("Amount"), 
                        _("Memo"), _("User"), _("New GL Trans. Type"), _(""));

                table_header($th);

                $k = 0;
                
                if (! (isset($_POST['Search']) && strlen($_POST['Search']) > 0) && ! isset($_POST['Update'])) {
                    end_table(2);
                    div_end();
                    
                    display_note('Click the search button to display details!', 1, 2);
                    return;
                }
                
                $un_as = get_listvalue_by_code('UNASSIGNED_VALUES', 'UNASSIGNED');
                    $unassigned_id = $un_as['id'];

                $result = get_edit_gl_transaction_type_details($type_id, $from_date, $to_date, $reference, $trans_no);
                    while ($row = db_fetch($result)) {
                        alt_table_row_color($k);

                            label_cell(sql2date($row['tran_date']), 'align=center');
                            label_cell($systypes_array[$row['type']]);
                            $link = get_trans_view_str($row['type'], $row['type_no']);
                            label_cell($link, 'align=center');
                            label_cell($row['reference'], 'align=center');

                            if ($row['person_type_id'] > 0) {
                                label_cell($payment_person_types[$row['person_type_id']]);
                                if ($row['person_type_id'] == PT_EMPLOYEE) {
                                    $emp_tra = get_emp_trans_record($row['type_no'], $row['type']);
                                        if (is_array($emp_tra)) {
                                            label_cell(payment_person_name($row['person_type_id'], $row['person_id'], true, $emp_tra['employee_id']));
                                        } else {
                                            label_cell('');
                                        }
                                } else {
                                    label_cell(payment_person_name($row['person_type_id'], $row['person_id']));
                                }
                            } else{
                                label_cell('');
                                label_cell('');
                            }

                            $key = $row['type'].'_'.$row['type_no'];
                            
                            if (in_array($row['type'], array(ST_BANKPAYMENT, ST_BANKDEPOSIT)) ) {
                                $old_gl_trans_type_id = ($row['bt_gl_trans_type_id'] != $unassigned_id) ? $row['bt_gl_trans_type_id'] : -1;
                                label_cell($row['bt_gl_trans_type_name']);
                                hidden("old_gl_trans_type_id_$key", $row['bt_gl_trans_type_id']);
                            } 
                            elseif (in_array($row['type'], array(ST_CASH_PAYMENT, ST_CASH_RECEIPT, ST_GL_DEBIT, ST_GL_CREDIT))) {
                                $old_gl_trans_type_id = ($row['jo_gl_trans_type_id'] != $unassigned_id) ? $row['jo_gl_trans_type_id'] : -1;
                                label_cell($row['jo_gl_trans_type_name']);
                                hidden("old_gl_trans_type_id_$key", $row['jo_gl_trans_type_id']);
                            } 
                            elseif(in_array($row['type'], array(ST_JOURNAL))){
                                $old_gl_trans_type_id = ($row['gl_trans_type_id'] != $unassigned_id) ? $row['gl_trans_type_id'] : -1;
                                label_cell($row['gl_trans_type_name']);
                                hidden("old_gl_trans_type_id_$key", $row['gl_trans_type_id']);
                            }
                            else{
                                label_cell("");
                                hidden("old_gl_trans_type_id_$key", -1);
                            }
                            
                            label_cell(number_format($row['amount'], 2), 'align=right');
                            $coment_result = get_comments($row['type'], $row['type_no']);
                                $comment = db_fetch($coment_result);
                                    if (is_array($comment)) {
                                        label_cell(nl2br($comment['memo_']));
                                    } else{
                                        label_cell('');
                                    }

                            label_cell($row['real_name']);

                            
                            if ($status === true) {
                                if (! (isset($_POST["gl_trans_type_id_$key"]) && $_POST["gl_trans_type_id_$key"] > 0) || isset($_POST['Search'])) {
                                    $_POST["gl_trans_type_id_$key"] = $old_gl_trans_type_id;
                                } 
                            }

                            if ($row['type'] == ST_JOURNAL) {
                                value_list_cells('', "gl_trans_type_id_$key", 'GL_TRANS_TYPE_JOURNAL', null, "Select a gl trans type");
                            } else {
                                $type_code = get_edit_gl_transaction_type_code($row['type']);
                                
                                switch ($row['person_type_id']) {
                                    case PT_MISC :
                                        filtered_value_list_cells('', "gl_trans_type_id_$key", $type_code, "'PT_MISC'", null, '');
                                        break;
                                    case PT_SUPPLIER :
                                        filtered_value_list_cells('', "gl_trans_type_id_$key", $type_code, "'PT_SUPPLIER'", null, '');
                                        break;
                                    case PT_CUSTOMER :
                                        filtered_value_list_cells('', "gl_trans_type_id_$key", $type_code, "'PT_CUSTOMER'", null, '');
                                        break;

                                    case PT_QUICKENTRY :
                                        filtered_value_list_cells('', "gl_trans_type_id_$key", $type_code, "'PT_QUICKENTRY'", null, '');
                                        break;

                                    case PT_EMPLOYEE :
                                        filtered_value_list_cells('', "gl_trans_type_id_$key", $type_code, "'PT_EMPLOYEE'", null, '');
                                        break;	
                                    
                                    case PT_FLOAT :
                                        filtered_value_list_cells('', "gl_trans_type_id_$key", $type_code, "'PT_FLOAT'", null, '');
                                        break;	
                                    
                                    default:
                                        label_cell("Unsupplorted Person Type");
                                        break;	
                                }
                            }


                            if ($status === true) {
                                if (($old_gl_trans_type_id > 0) && $old_gl_trans_type_id != $unassigned_id) {
                                    $_POST["checked_$key"] = false;
                                } else {
                                    $_POST["checked_$key"] = false;
                                }
                            }

                            check_cells("", "checked_$key", null, false, false, "align=center");

                        end_row();
                    }

        end_table(2);
    
    div_end();
}

function can_process_edit_transaction_type($type_id, $from_date, $to_date, $reference, $trans_no) {
    global $systypes_array;
    
    $result = get_edit_gl_transaction_type_details($type_id, $from_date, $to_date, $reference, $trans_no);
        while ($row = db_fetch($result)) {
            $key = $row['type'].'_'.$row['type_no'];
            
                if (isset($_POST["checked_$key"]) && $_POST["checked_$key"] == true) {
                    if (! ($_POST["gl_trans_type_id_$key"] > 0)) {
                        return (array('success' => false, 
                                      'message' => $systypes_array[$row['type']].' #'.$row['type_no'].' : GL trans type cannot be empty.',
                                      'set_focus' => "gl_trans_type_id_$key"));
                    }
                }
        }
        
        return array('success' => true, 'message' => '', 'set_focus' => '');
}

function update_edit_gl_transaction_type() {
    global $systypes_array;
    
    $type_id = $_POST['type_id'];
    $from_date = $_POST['from_date'];
    $to_date = $_POST['to_date'];
    $reference = $_POST['reference'];
    $trans_no = $_POST['trans_no'];
    
    $msg = can_process_edit_transaction_type($type_id, $from_date, $to_date, $reference, $trans_no);
        if ($msg['success'] !== true) {
            return $msg;
        }
        
        $save_recs = false;
        $result = get_edit_gl_transaction_type_details($type_id, $from_date, $to_date, $reference, $trans_no);
            while ($row = db_fetch($result)) {
                $key = $row['type'].'_'.$row['type_no'];
            
                if (isset($_POST["checked_$key"]) && $_POST["checked_$key"] == true) {
                    $new_gl_trans_type_id = $_POST["gl_trans_type_id_$key"];
                    
                    begin_transaction();
                    
                    if (in_array($row['type'], array(ST_BANKPAYMENT, ST_BANKDEPOSIT)) ) {
                        update_bank_trans_gl_trans_type($row['type'], $row['type_no'], $new_gl_trans_type_id);
                        update_gl_trans_gl_trans_type($row['type'], $row['type_no'], $new_gl_trans_type_id);
                    } 
                    elseif (in_array($row['type'], array(ST_CASH_PAYMENT, ST_CASH_RECEIPT, ST_GL_DEBIT, ST_GL_CREDIT))) {
                        update_pre_journal_gl_trans_type($row['type'], $row['type_no'], $new_gl_trans_type_id);
                        update_gl_trans_gl_trans_type($row['type'], $row['type_no'], $new_gl_trans_type_id);
                    }
                    elseif (in_array($row['type'], array(ST_JOURNAL)) ) {
                        update_gl_trans_gl_trans_type($row['type'], $row['type_no'], $new_gl_trans_type_id);
                    }
                    else{
                        cancel_transaction();
                        $msg = ("Unsupported transaction type : ".$systypes_array[$row['type']]." #".$row['type_no']);
                        return array('success' => false, 'message' => $msg, 'set_focus' => '');
                    }
                    
                    add_to_gl_trans_type_updated_log($row['type'], $row['type_no'], $_POST["old_gl_trans_type_id_$key"], $new_gl_trans_type_id);
                    
                    commit_transaction();
                    $save_recs = true;
                    
                    
                }
            }
            
            return array('success' => true, 'message' => 'GL transaction typs have been updated.', 'set_focus' => '');
}

?>