<?php
$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = '$par_table_id = get_table_id({$display_none|default:-1});\r\n$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];\r\n$par_link_id = is_array($line[\'f\' . $link_field_id]) ? $line[\'f\' . $link_field_id][\'raw\'] : $line[\'f\' . $link_field_id];\r\nif ($par_link_id > 0) {\r\n    if ($event[\'type\'] == \'delete\') {\r\n        $result = data_select_field($table_id, \'id, {$receiver_field}\', \'status = 0 AND f\' . $link_field_id . \' = \' . $par_link_id . \' AND id > \' . $line[\'id\']);\r\n        while ($row = sql_fetch_assoc($result)) {\r\n            data_update($table_id, EVENTS_ENABLE, [\'{$receiver_field}\' => $row[\'{$receiver_field}\'] - 1], \'id = \' . $row[\'id\']);\r\n        }\r\n    } else {\r\n        $max_num = sql_fetch_row(data_select_field($table_id, \'MAX({$receiver_field})\', \'status = 0 AND f\' . $link_field_id . \' = \' . $par_link_id))[0];\r\n        $line[\'{$receiver_field}\'] = $max_num + 1;\r\n    }\r\n}',
params = '{\r\n    \"title\": \"Автонумерация в подтаблице\",\r\n    \"help\": \"autonumbering_in_a_subtable\",\r\n    \"parameters\": {\r\n        \"receiver_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"receiver_field\",\r\n            \"description\": \"Поле-получатель\",\r\n            \"help\": \"receiver_field\",\r\n            \"type_field\": 1\r\n        },\r\n        \"display_none\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"display_none\",\r\n            \"description\": \"display_none\",\r\n            \"help\": \"\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 1\r\n        },\r\n        {\r\n            \"type\": 4\r\n        }\r\n    ]\r\n}'
WHERE id = 116
EOT;
run_query($sql_query);