<?php

function display_change_approved_transaction_selection_parameters() {
    global $systypes_array;
    
    $tran_types_array = array();
    $tran_types_array[ST_UTILITY_BILLS] = $systypes_array[ST_UTILITY_BILLS];
    
    start_table(TABLESTYLE);
        custom_values_list_row("Transaction Type:", 'tran_type', $tran_types_array, null, '', true);
        ref_row("Document No:", 'document_no', null, null); 
        label_row('View Document', get_trans_view_str($_POST['tran_type'], $_POST['document_no']));
    end_table(1);
    
    start_table();
        submit_cells('Refresh', 'Refresh');
    end_table(1);
}

function display_change_approved_transaction_details($tran_type, $document_no) {
    global $systypes_array;
    
    $trn_tp_name = $systypes_array[$tran_type];
    
    $row = get_utility_bill_header_record($document_no);
        if (! (isset($row['id']) && $row['id'] > 0)) {
            display_note("Could not find entered transaction.", 1, 2, "style=color:red");
            return false;
        } elseif ($row['approval_status'] != AS_APPROVED) {
            display_note("$trn_tp_name #$document_no is not approved yet.", 1, 2, "style=color:red");
            return false;
        }
    
        start_table(TABLESTYLE, "width=60%");

            $th = array(_("Value"), _("Current Value"), _("New Value"));
            table_header($th);

            switch ($tran_type) {
                case ST_UTILITY_BILLS:
                    display_change_approved_transaction_utility_bill($row);
                    break;

                default:
                    break;
            }

        end_table(1);
        
        return true;
}

function display_change_approved_transaction_utility_bill($row) {
    start_row();
        label_cell(text_bold('Entry Type'));
        label_cell(get_listvalue_name_by_id($row['entry_type_id']));
        value_list_cells('', 'entry_type_id', 'UTILITY_BILL_ENTRY_TYPES', null, '');
    end_row();

    start_row();
        label_cell(text_bold('Due Date'));
        
        $due_date_ = sql2date($row['due_date']);
        label_cell($due_date_);
        
        if (! isset($_POST['due_date'])) {
            $_POST['due_date'] = $due_date_;
        }
        date_cells('', 'due_date');
    end_row();
}

function can_change_document_approval_group_utility_bill($tran_type, $document_no) {
    global $systypes_array;
    
    $trn_tp_name = $systypes_array[$tran_type];
    
    $row = get_utility_bill_header_record($document_no);
        if (! (isset($row['id']) && $row['id'] > 0)) {
            return array('success' => false, 'message' => _("Could not find entered $trn_tp_name #$document_no."), 'set_focus' => 'document_no');
        } elseif ($row['approval_status'] != AS_APPROVED) {
            return array('success' => false, 'message' => _("$trn_tp_name #$document_no is not approved yet."), 'set_focus' => 'document_no');
        } 
    
    if (! ($_POST['entry_type_id'] > 0)) {
        return array('success' => false, 'message' => _("Please select an entry type."), 'set_focus' => 'entry_type_id');
    }
    
    if (! is_date($_POST['due_date'])) {
        return array('success' => false, 'message' => _("Entered due date is not a valid date."), 'set_focus' => 'due_date');
    }
    
        return array('success' => true, 'message' => '', 'row' => $row);
}

function can_change_document_approval_group() {
    if (! ($_POST['tran_type'] > 0)) {
        return array('success' => false, 'message' => _("Please select a transaction type."), 'set_focus' => 'tran_type');
    }
    
    if (! ($_POST['document_no'] > 0)) {
        return array('success' => false, 'message' => _("The document number should numeric and positive."), 'set_focus' => 'document_no');
    }
    
    $tran_type = $_POST['tran_type'];
    $document_no = $_POST['document_no'];
    
        switch ($tran_type) {
            case ST_UTILITY_BILLS:
                $msg = can_change_document_approval_group_utility_bill($tran_type, $document_no);
                    if ($msg['success'] === false) {
                        return $msg;
                    }
                break;

            default:
                break;
        }
        
	return array('success' => true, 'message' => '', 'row' => $msg['row']);
}

function change_approved_transaction() {  
    global $systypes_array;
    
    $can_up_msg = can_change_document_approval_group();
        if ($can_up_msg['success'] === false) {
            return $can_up_msg;
        }
        
    $previous_row = $can_up_msg['row'];
    
    $tran_type = $_POST['tran_type'];
    $document_no = $_POST['document_no'];
    
    $trn_tp_name = $systypes_array[$tran_type];
    
        begin_transaction();

            $log_array = array();
            switch ($tran_type) {
                case ST_UTILITY_BILLS:
                    $af_row = update_approved_transaction_utility_bill_header($document_no, $_POST['entry_type_id'], $_POST['due_date']);
                        if ($af_row != 1) {
                            cancel_transaction();
                            return array('success' => false, 'message' => _("Could not update $trn_tp_name #$document_no."));
                        } 

                    $af_row_2 = update_approved_transaction_supp_trans_due_date($document_no, $tran_type, $_POST['due_date']);
                        if ($af_row_2 != 1) {
                            cancel_transaction();
                            return array('success' => false, 'message' => _("Could not update $trn_tp_name #$document_no."));
                        }
                        
                    $log_array[] = array('type' => $tran_type, 'trans_no' => $document_no, 'field_name' => 'entry_type_id', 
                                         'previous_value' => $previous_row['entry_type_id'], 'new_value' => $_POST['entry_type_id']);
                    $log_array[] = array('type' => $tran_type, 'trans_no' => $document_no, 'field_name' => 'due_date', 
                                         'previous_value' => $previous_row['due_date'], 'new_value' => date2sql($_POST['due_date']));
                        
                    $log_msg = save_approved_transaction_changed_log($tran_type, $log_array);
                        if ($log_msg['success'] === false) {
                            cancel_transaction();
                            return $log_msg;
                        }

                    break;

                default:
                    break;
            }
            
        commit_transaction();

        return array('success' => true, 'message' => "$trn_tp_name #$document_no has been updated.");
}

?>