{*
 Шаблон вычисления "Автонумерация в подтаблице"
*}

$par_table_id = get_table_id({$display_none|default:-1});
$link_field_id = sql_fetch_row(sql_select_field(SUBTABLES_TABLE, 'link_field_id', 'table_id = ' . $par_table_id . ' AND link_table_id = ' . $table_id))[0];
$par_link_id = is_array($line['f' . $link_field_id]) ? $line['f' . $link_field_id]['raw'] : $line['f' . $link_field_id];
if ($par_link_id > 0) {
    if ($event['type'] == 'delete') {
        $result = data_select_field($table_id, 'id, {$receiver_field}', 'status = 0 AND f' . $link_field_id . ' = ' . $par_link_id . ' AND id > ' . $line['id']);
        while ($row = sql_fetch_assoc($result)) {
            data_update($table_id, EVENTS_ENABLE, ['{$receiver_field}' => $row['{$receiver_field}'] - 1], 'id = ' . $row['id']);
        }
    } else {
        $max_num = sql_fetch_row(data_select_field($table_id, 'MAX({$receiver_field})', 'status = 0 AND f' . $link_field_id . ' = ' . $par_link_id))[0];
        $line['{$receiver_field}'] = $max_num + 1;
    }
}