<?php
$res = run_query("SHOW COLUMNS FROM " . $config['table_prefix'] . "module_asterisk_servers LIKE 'record_field_Trunk'");
if (!sql_num_rows($res)) {
    run_query("ALTER TABLE " . $config['table_prefix'] . "module_asterisk_servers ADD record_field_Trunk INT NOT NULL AFTER record_field_Record_link");

    $ast_server = sql_select_array($config['table_prefix'] . "module_asterisk_servers", "id=1");
    $table_id = $ast_server['record_table'];
    $prev_field = sql_select_array($config['table_prefix'] . "fields", "id=", $ast_server['record_field_Record_link']);
    if ($table_id && $prev_field) {
        run_query("UPDATE " . $config['table_prefix'] . "fields SET field_num = field_num + 1 WHERE field_num > " . $prev_field['field_num']);
        $ins_arr = array(
            'table_id' => $table_id,
            'field_num' => $prev_field['field_num'] + 1,
            'name_field' => 'Аккаунт',
            'type_field' => 3,
            'owner_id' => 1
        );
        $field_id = sql_insert($config['table_prefix'] . "fields", $ins_arr);
        sql_update($config['table_prefix'] . "module_asterisk_servers", array('record_field_Trunk' => $field_id), "id=1");

        run_query("ALTER TABLE " . $config['table_prefix'] . "data" . $table_id . " ADD f" . $field_id . " VARCHAR(255) NOT NULL AFTER f" . $prev_field['id']);

        $ins_arr = array(
            'table_id' => $table_id,
            'field_id' => $field_id,
            'view_tb' => 1,
            'view_add' => 0,
            'view_edit' => 0,
            'view' => 1,
            'read_acc' => 1,
            'write_acc' => 1
        );
        $result = sql_select_field($config['table_prefix'] . "groups", "id");
        while ($row = sql_fetch_assoc($result)) {
            $ins_arr['group_id'] = $row['id'];
            sql_insert($config['table_prefix'] . "acc_fields", $ins_arr);
        }
    }
}