iterate over oracle type v_result and output with dbms_output.put_line(): type t_result is table of number index by pls_integer; v_result t_result;
Assistant
To iterate over a PL/SQL associative array (or index-by table) and output its contents using DBMS_OUTPUT.PUT_LINE, you can follow the example below. In this example, I'll assume that v_result has been populated with some data.
First, ensure that you have enabled server output in your SQL*Plus or another Oracle client session:
sql
SET SERVEROUTPUT ON;
Now, here is a PL/SQL block that iterates over v_result and outputs its contents:
plsql
DECLARE
TYPE t_result IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
v_result t_result;
idx PLS_INTEGER;
BEGIN
-- Example: Populating the associative array with some values
v_result(1) := 100;
v_result(2) := 200;
v_result(3) := 300;
-- Iterate over the array and output each element
idx := v_result.FIRST; -- Get the first index
WHILE idx IS NOT NULL LOOP
DBMS_OUTPUT.PUT_LINE('Index: ' || TO_CHAR(idx) || ' Value: ' || TO_CHAR(v_result(idx)));
idx := v_result.NEXT(idx); -- Get the next index
END LOOP;
END;
/
Here's a breakdown of the block:
Type and Variable Declaration: We declare a PL/SQL type t_result which is an associative array indexed by PLS_INTEGER. We then declare a variable v_result of this type.
Populating the Array: For illustration, we manually populate v_result with some sample numbers. In practice, this data might come from a query or other computation.
Iterating Over the Array:
We use v_result.FIRST to get the first index of the associative array.
We enter a WHILE loop that continues as long as idx is not NULL.
Inside the loop, DBMS_OUTPUT.PUT_LINE is used to print the current index and its corresponding value.
We use v_result.NEXT(idx) to proceed to the next index in the associative array until NULL is encountered, indicating that there are no more elements.
Make sure DBMS_OUTPUT is enabled in your session to see the output of this script.
User
rewrite above for plpgsql
Assistant
In PL/pgSQL (the procedural language for PostgreSQL), associative arrays (or index-by tables) are not directly available like in Oracle's PL/SQL. However, you can achieve similar functionality using arrays or other collection types. Since PostgreSQL arrays are one-dimensional and use integer indexing, we can mimic the behavior using a simple array and iterate through it.
Here's how you can write a similar block in PL/pgSQL using arrays:
plpgsql
DO $$
DECLARE
v_result INTEGER[] := ARRAY[100, 200, 300]; -- Example array
idx INTEGER;
BEGIN
-- Iterate over the array and output each element
FOR idx IN 1 .. array_length(v_result, 1) LOOP
RAISE NOTICE 'Index: %, Value: %', idx, v_result[idx];
END LOOP;
END;
$$ LANGUAGE plpgsql;
Here's how this PL/pgSQL script works:
Array Declaration and Initialization: We declare a variable v_result as an integer array and initialize it with some values. In PostgreSQL, arrays are zero or one-based, depending on how they are initialized or accessed.
For Loop to Iterate: We use a FOR loop with the range 1 .. array_length(v_result, 1), which gives us the length of the array's first dimension (typical use for 1-dimensional arrays).
Output Using RAISE NOTICE: Within the loop, we use RAISE NOTICE to output the current index and its corresponding value. Unlike DBMS_OUTPUT.PUT_LINE in Oracle, PostgreSQL uses RAISE NOTICE to log or print messages during PL/pgSQL execution.
This PL/pgSQL script captures the essence of iterating through an associative array in Oracle PL/SQL but adapted to PostgreSQL's array handling capabilities.
User
rewrite in plpgsql:
CREATE OR REPLACE EDITIONABLE PROCEDURE "SKRIN"."EMIT_CALC_FIN_RATES" (p_inn in varchar2, p_repdate in varchar2, p_report_id in number)
is
v_repdate date;
v_val number;
v_numerator number;
v_denominator number;
v_value number;
v_sqlerrm varchar2(255);
v_param_code varchar2(255);
v_year_date date;
v_cnt_values number;
type data_row is record(param_name varchar2(255), value number);
type data_tab is table of data_row;
v_data data_tab;
v_prev_data data_tab;
v_year_data data_tab;
type t_num_by_str is table of number index by varchar2(100);
v_rates t_num_by_str;
v_data_code t_num_by_str;
v_prev_data_code t_num_by_str;
v_year_data_code t_num_by_str;
type t_result is table of number index by pls_integer;
v_result t_result;
v_result_id t_result;
cursor crates(v_rid number) is select id, code from v_emit_report_rates where report_id = v_rid order by id;
Translating a complex Oracle PL/SQL procedure to PL/pgSQL for PostgreSQL involves several considerations given the differences between the two languages' features and capabilities. PostgreSQL doesn't support some Oracle functionalities directly (e.g., associative arrays, exceptions on cursor fetches like NO_DATA_FOUND), so we need to adapt the code using PostgreSQL constructs. Here’s a translated version considering these aspects:
sql
User
rewrite using temp tables:
Assistant
To translate the provided PL/SQL procedure to PL/pgSQL with the use of temporary tables, you have to adapt the code considerably while keeping the logical functionality intact. PostgreSQL supports temporary tables which can be used to handle data processing that was previously done using records or collections in PL/SQL. Below is a conceptual translation using temporary tables:
sql
-- Assume that the procedure is wrapped in a DO block for execution or is part of a function
DO $$
DECLARE
v_repdate DATE;
v_year_date DATE;
v_value NUMERIC;
v_sqlerrm TEXT;
v_rid INTEGER;
p_inn VARCHAR := '123'; -- Example input
p_repdate VARCHAR := '20230501'; -- Example input
p_report_id INTEGER := 1; -- Example input
BEGIN
-- Setup the date
v_repdate := to_date(p_repdate, 'YYYYMMDD');
v_year_date := to_date(p_repdate, 'YYYYMMDD') - interval '1 year'; -- Placeholder for emit_get_year_date equivalent
-- Create temporary tables to hold data
CREATE TEMP TABLE temp_data AS
SELECT nvl(to_char(formnum) || '.' || to_char(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_repdate AND reptype = p_report_id;
CREATE TEMP TABLE temp_prev_data AS
SELECT nvl(to_char(formnum) || '.' || to_char(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_repdate - interval '1 year' AND reptype = p_report_id;
CREATE TEMP TABLE temp_year_data AS
SELECT nvl(to_char(formnum) || '.' || to_char(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_year_date AND reptype = p_report_id;
DELETE FROM emit_fin_rates WHERE inn = p_inn AND repdate = v_repdate;
-- Using a cursor to loop through the crates
FOR cur IN SELECT id, code FROM v_emit_report_rates WHERE report_id = p_report_id ORDER BY id LOOP
-- Reset value for each loop iteration
v_value := NULL;
-- Example calculation (fill in based on your logic)
IF cur.code = 'ACT' THEN
SELECT value INTO v_value FROM temp_data WHERE param_name = '1.1600';
ELSIF cur.code = 'ACT_PREV' THEN
SELECT value INTO v_value FROM temp_prev_data WHERE param_name = '1.1600';
-- Add additional cases for other codes
END IF;
-- Insert calculated values into emit_fin_rates
INSERT INTO emit_fin_rates (id, inn, repdate, rate_id, value, updatedt)
VALUES (nextval('emit_fin_rates_seq'), p_inn, v_repdate, cur.id, v_value, current_timestamp);
END LOOP;
-- Log the number of rows inserted
RAISE NOTICE '% rows inserted into emit_fin_rates', SQL%ROWCOUNT;
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS v_sqlerrm = MESSAGE_TEXT;
RAISE NOTICE 'Error occurred: %', v_sqlerrm;
END $$;
Assistant
It seems like your message was cut off or incomplete. Could you please provide any additional details or specify if there's anything more you'd like to discuss or need help with? I'm here to assist!
User
rewrite for greenplum using temporary tables:
CREATE OR REPLACE EDITIONABLE PROCEDURE "SKRIN"."EMIT_CALC_FIN_RATES" (p_inn in varchar2, p_repdate in varchar2, p_report_id in number)
is
v_repdate date;
v_val number;
v_numerator number;
v_denominator number;
v_value number;
v_sqlerrm varchar2(255);
v_param_code varchar2(255);
v_year_date date;
v_cnt_values number;
type data_row is record(param_name varchar2(255), value number);
type data_tab is table of data_row;
v_data data_tab;
v_prev_data data_tab;
v_year_data data_tab;
type t_num_by_str is table of number index by varchar2(100);
v_rates t_num_by_str;
v_data_code t_num_by_str;
v_prev_data_code t_num_by_str;
v_year_data_code t_num_by_str;
type t_result is table of number index by pls_integer;
v_result t_result;
v_result_id t_result;
cursor crates(v_rid number) is select id, code from v_emit_report_rates where report_id = v_rid order by id;
Translating an Oracle PL/SQL procedure to PL/pgSQL for Greenplum while employing temporary tables requires adapting Oracle's advanced features to PostgreSQL's capabilities (and specifically Greenplum's, which is based on PostgreSQL).
Below, I'll provide a conceptual translation of the procedure, emphasizing the use of temporary tables instead of collections or associative arrays. Note that some functions like add_errlog and get_ltm_data will be assumed to be user-defined functions or procedures available in the database.
sql
User
rewrite for greenplum avoiding updates: update a set a = 1;
Assistant
In Greenplum, which is optimized for analytics and large-scale data processing, updates can sometimes be costly because they inherently involve data redistribution. To avoid updates, a common strategy is to replace updates with a combination of insertions and deletions, particularly when dealing with large data volumes or when needing to maintain performance in a distributed environment.
Here's a general approach to emulate an update by using a delete followed by an insert:
Create a Temporary Table: Create a temporary table to select the records you want to update.
Delete the Records: Remove the records you want to update from the original table.
Insert Updated Records: Insert the records with updated values back into the original table.
Below is a conceptual implementation in PL/pgSQL for performing a similar operation:
sql
DO $$
BEGIN
-- Step 1: Create a temporary table to hold records that need updating
CREATE TEMP TABLE temp_update AS
SELECT *
FROM original_table
WHERE condition_for_update; -- Define the condition to select records for updating
-- Step 2: Delete those records from the original table
DELETE FROM original_table
WHERE condition_for_update; -- Use the same condition as above
-- Step 3: Insert updated records back into the original table
INSERT INTO original_table (columns_list) -- Specify the list of columns
SELECT column1, column2, ..., 1 AS updated_column_value -- Replace with new values for update
FROM temp_update;
-- Optionally drop the temporary table
DROP TABLE IF EXISTS temp_update;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'An error occurred: %', SQLERRM;
END $$;
Key Considerations
Conditions for Selection:
The condition_for_update should specifically target the rows you intend to "update". In the original update statement, this is simply a condition that applies to all rows.
User
do not skip or optimize code, rewrite for postgres 9.4 using temporary tables ```
CREATE OR REPLACE EDITIONABLE PROCEDURE "SKRIN"."EMIT_CALC_FIN_RATES" (p_inn in varchar2, p_repdate in varchar2, p_report_id in number)
is
v_repdate date;
v_val number;
v_numerator number;
v_denominator number;
v_value number;
v_sqlerrm varchar2(255);
v_param_code varchar2(255);
v_year_date date;
v_cnt_values number;
type data_row is record(param_name varchar2(255), value number);
type data_tab is table of data_row;
v_data data_tab;
v_prev_data data_tab;
v_year_data data_tab;
type t_num_by_str is table of number index by varchar2(100);
v_rates t_num_by_str;
v_data_code t_num_by_str;
v_prev_data_code t_num_by_str;
v_year_data_code t_num_by_str;
type t_result is table of number index by pls_integer;
v_result t_result;
v_result_id t_result;
cursor crates(v_rid number) is select id, code from v_emit_report_rates where report_id = v_rid order by id;
To rewrite the provided Oracle PL/SQL procedure for PostgreSQL 9.4 using temporary tables, we need to adapt Oracle's features to those available in PostgreSQL. Temporary tables will be used to handle the intermediate data stored in PL/SQL collections, and we'll use exception handling available in PL/pgSQL.
Here is a complete translation:
plpgsql
User
do not skip v_result
Assistant
To incorporate the handling of v_result in the procedure translated to PostgreSQL, we need to manage it using temporary tables, as PostgreSQL doesn't have the same associative array support as Oracle. We'll create a temporary results table and use it to manage the same data that v_result was intended to hold.
Here is how you can rewrite the function including handling for v_result:
plpgsql
User
how to avoid union of two result sets in greenplum
Assistant
In Greenplum, like in other SQL databases, the use of UNION to combine result sets can be optimized or avoided depending on the specific case. If you want to avoid using UNION for performance reasons or due to other considerations, there are a few strategies you can consider:
1. Use UNION ALL Instead of UNION
If your goal is purely performance-oriented and if there are no duplicates that must be removed within the combined result sets, switching from UNION to UNION ALL can often improve performance because UNION ALL does not eliminate duplicates and thus requires less processing.
sql
SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2;
2. Use Common Table Expressions (CTEs) or Subqueries
Sometimes, structuring your query with CTEs can help manage complex logic or improve readability, making it easier to find other optimizations. CTEs themselves don't inherently replace UNION, but they can structure queries in a way that might reveal other optimization opportunities.
sql
WITH cte1 AS (
SELECT column1, column2 FROM table1
), cte2 AS (
SELECT column1, column2 FROM table2
)
-- Combine results if applicable without UNION
3. Use Joins
If the datasets have a common key or can be logically merged using a JOIN, consider whether a JOIN might replace the UNION. This approach applies if you’re combining related datasets.
sql
SELECT a.column1, b.column2
FROM table1 a
JOIN table2 b ON a.key = b.key;
4. Data Aggregation
If your goal is to aggregate similar data without duplicate removal, consider using aggregation functions like GROUP BY if it makes sense contextually.
sql
SELECT column1, SUM(column2)
FROM (
SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2
) subquery
GROUP BY column1;
User
rewrite using join instead of union: select 1 union select 2
Assistant
Creating a JOIN to produce the same result as a UNION between two constants is rather unconventional, since JOIN operations are typically used to combine rows from two tables based on a condition. However, you can achieve a similar effect without UNION by creatively using JOIN on a universal true condition, such as an implied join condition or a cross join of two tables or subqueries that produce single values.
Here’s how you can use a CROSS JOIN to achieve this:
sql
SELECT a.val
FROM (SELECT 1 AS val) AS a
CROSS JOIN (SELECT 2 AS val) AS b;
In this example, you effectively create a Cartesian product using CROSS JOIN that produces each value independently because each subquery only produces a single row. The result will mimic the behavior of a UNION in combining two distinct sets of results.
Explanation:
Subqueries: Each subquery, (SELECT 1 AS val) and (SELECT 2 AS val), generates a single row with a constant value.
CROSS JOIN: This join doesn't specify a WHERE condition, meaning every row from the first subquery joins with every row from the second. Given the single return row from each subquery, it effectively lists each value separately analogous to a UNION.
Final SELECT: By selecting from one of the subqueries (a.val), you ensure that it only selects one column from the Cartesian product, which includes both 1 and 2.
Since a JOIN isn't typically structured for combining constants or single-row results without an established key, this approach is more academic for understanding SQL operations, and using UNION remains the more straightforward method for these simple goals.
User
rewrite using join instead of union: select id, value from table1 union select id, value from table2, assume id is the primary key
Assistant
If you need to achieve the same result as a UNION between two result sets using a JOIN, you should typically consider whether the data structure and the requirement truly permit a JOIN. However, if id is a primary key in both tables and there are no overlapping id values, you could mimic a union-like operation using a FULL OUTER JOIN.
A FULL OUTER JOIN would include all records from both tables, but usually, the intention behind a UNION is to combine results from both tables independently rather than merging them based on matching keys. Still, assuming id uniquely identifies each record with no overlap, we can write:
sql
SELECT COALESCE(a.id, b.id) AS id,
COALESCE(a.value, b.value) AS value
FROM table1 AS a
FULL OUTER JOIN table2 AS b ON a.id = b.id;
Explanation:
FULL OUTER JOIN: This join returns all rows when there is a match between either left (table1) or right table (table2) records, and brings in unmatched records from both sides.
COALESCE Function: This function is used to select the non-null value between potentially matching rows from the two tables. In this case, it ensures that if a row exists in one table and not the other due to non-overlapping keys, you still get the intended id and value.
Primary Key Assumption: This assumes both tables together account uniquely for all possible id values (i.e., they are mutually exclusive) or they are already aligned for a UNION operation.
Caveats:
This query makes more sense when you need to account for every record uniquely, ensuring all potential keys are visible across both tables.
If both tables share the same primary key with different values for that primary key in some cases, using a JOIN won't naturally achieve the UNION behavior perfectly as might be directly expected.
If you aim to gather all capital with distinct IDs, if those IDs naturally don't overlap, using the UNION operator is still the most efficient and straightforward means of achieving that goal. The query provided here imitates a kind of union behavior but through aggregation of joined sets, which requires careful consideration of your actual data structure and result expectations.
User
rewrite in grenplum: select add_months(v_id, -12)
Assistant
In Greenplum, which is built upon PostgreSQL, there isn't a direct ADD_MONTHS function like in Oracle. However, you can achieve the same result using the INTERVAL keyword, or by leveraging functions like date_trunc, depending on the data type you're working with.
Using INTERVAL for Date Arithmetic
If v_id is a date or timestamp type column and you want to subtract months from it, you can use the INTERVAL keyword with the - operator:
sql
SELECT v_id - INTERVAL '12 months' AS adjusted_date
FROM table_name;
Explanation:
INTERVAL: This keyword is used to define a time period to add or subtract from a date or timestamp.
Subtraction of 12 months: You specify the subtraction using - INTERVAL '12 months' which effectively mimics ADD_MONTHS(v_id, -12).
Ensure that the v_id field you're operating on is of a date or timestamp type, as this method directly adjusts date-like values. If v_id is an integer or another numeric type and you simply mean to perform arithmetic with integers, then you would adjust it using regular arithmetic operations without using intervals.
Not yet included in a published release.
delete from emit_fin_rates where inn = p_inn and repdate = v_repdate;
add_errlog($$PLSQL_UNIT, null, null, sql%rowcount || ' rows deleted from emit_fin_rates year date = ' || v_year_date);
select nvl(to_char(formnum) || '.' || to_char(num), name) param_name, value
bulk collect into v_data
from v_emit_data_all
where inn = p_inn and repdate = v_repdate and reptype = p_report_id;
select nvl(to_char(formnum) || '.' || to_char(num), name) param_name, value
bulk collect into v_prev_data
from v_emit_data_all
where inn = p_inn and repdate = add_months(v_repdate, -12) and reptype = p_report_id;
select nvl(to_char(formnum) || '.' || to_char(num), name) param_name, value
bulk collect into v_year_data
from v_emit_data_all
where inn = p_inn and repdate = emit_get_year_date(v_repdate) and reptype = p_report_id;
add_errlog($$PLSQL_UNIT, null, null, 'current count = ' || v_data.count || ' prev count = ' || v_prev_data.count || ' year count = ' || v_year_data.count);
if (v_data.count > 0)
then
for i in v_data.first..v_data.last
loop
v_data_code(v_data(i).param_name) := v_data(i).value;
end loop;
end if;
if (v_prev_data.count > 0)
then
for i in v_prev_data.first..v_prev_data.last
loop
v_prev_data_code(v_prev_data(i).param_name) := v_prev_data(i).value;
end loop;
end if;
if (v_year_data.count > 0)
then
for i in v_year_data.first..v_year_data.last
loop
v_year_data_code(v_year_data(i).param_name) := v_year_data(i).value;
end loop;
end if;
for c1 in crates(p_report_id)
loop
v_value := null;
v_val := null;
v_param_code := null;
v_rates(c1.code) := c1.id;
v_numerator := null;
v_denominator := null;
if (c1.code = 'ACT')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1600');
elsif (p_report_id = 2)
then
v_value := v_data_code('Итого активы');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.22');
elsif (p_report_id = 4)
then
v_value := v_data_code('1.23');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'ACT_PREV')
then
begin
if (p_report_id = 1)
then
v_value := v_prev_data_code('1.1600');
elsif (p_report_id = 2)
then
v_value := v_prev_data_code('Итого активы');
elsif (p_report_id = 3)
then
v_value := v_prev_data_code('1.22');
elsif (p_report_id = 4)
then
v_value := v_prev_data_code('1.23');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'ACT_DYN')
then
begin
v_value := ((v_result(v_rates('ACT')) - v_result(v_rates('ACT_PREV')))/v_result(v_rates('ACT_PREV')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1300');
elsif (p_report_id = 2)
then
v_value := v_data_code('Итого капитал');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.52');
elsif (p_report_id = 4)
then
v_value := v_data_code('1.51');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP_PREV')
then
begin
if (p_report_id = 1)
then
v_value := v_prev_data_code('1.1300');
elsif (p_report_id = 2)
then
v_value := v_prev_data_code('Итого капитал');
elsif (p_report_id = 3)
then
v_value := v_prev_data_code('1.52');
elsif (p_report_id = 4)
then
v_value := v_prev_data_code('1.51');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP_DYN')
then
begin
v_value := ((v_result(v_rates('KAP')) - v_result(v_rates('KAP_PREV')))/v_result(v_rates('KAP_PREV')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP_MAX')
then
begin
if (p_report_id = 1)
then
select max(value) into v_value from v_emit_data_all where inn = p_inn and repdate >add_months(v_repdate, -12) and repdate <= v_repdate and num = '1300' and formnum = 1;
elsif (p_report_id = 2)
then
select max(value) into v_value from v_emit_data_all where inn = p_inn and repdate >add_months(v_repdate, -12) and repdate <= v_repdate and param_name = 'Итого капитал';
elsif (p_report_id = 3)
then
select max(value) into v_value from v_emit_data_all where inn = p_inn and repdate >add_months(v_repdate, -12) and repdate <= v_repdate and num = '52' and formnum = 1;
elsif (p_report_id = 4)
then
select max(value) into v_value from v_emit_data_all where inn = p_inn and repdate >add_months(v_repdate, -12) and repdate <= v_repdate and num = '51' and formnum = 1;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP_MAX_DYN')
then
begin
v_value := ((v_result(v_rates('KAP')) - v_result(v_rates('KAP_MAX')))/v_result(v_rates('KAP_MAX')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'PROFIT_LOSS')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1370');
elsif (p_report_id = 2)
then
v_value := v_data_code('Нераспределенная прибыль (непокрытый убыток)');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.51');
elsif (p_report_id = 4)
then
v_value := v_data_code('1.50');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INCOME_OTHER_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2310';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INCOME_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2110';
elsif (p_report_id = 2)
then
v_param_code := 'Выручка';
elsif (p_report_id = 3)
then
v_param_code := '2.15';
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INCOME_ADJ_LTM')
then
begin
v_value := v_result(v_rates('INCOME_LTM'));
--add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, 0, 'income_ltm = ' || v_result(v_rates('INCOME_LTM')) || ' income_other_ltm = ' || nvl(v_result(v_rates('INCOME_OTHER_LTM')),0) ||
--'interest_received_ltm = ' || nvl(v_result(v_rates('INTEREST_RCVD_LTM')),0));
if (p_report_id = 1)
then
if (v_result.exists(v_rates('INCOME_OTHER_LTM')) and v_result.exists(v_rates('INTEREST_RCVD_LTM'))
and v_result(v_rates('INCOME_OTHER_LTM')) + v_result(v_rates('INTEREST_RCVD_LTM')) > v_result(v_rates('INCOME_LTM')))
then
v_value := v_value + nvl(v_result(v_rates('INCOME_OTHER_LTM')),0) + nvl(v_result(v_rates('INTEREST_RCVD_LTM')),0);
end if;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INTEREST_PAID_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2330';
elsif (p_report_id = 2)
then
v_param_code := 'Проценты уплаченные';
elsif (p_report_id = 3)
then
v_param_code := '2.18';
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INTEREST_RCVD_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2320';
elsif (p_report_id = 2)
then
v_param_code := 'Проценты полученные';
elsif (p_report_id = 3)
then
v_param_code := null;
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'PROFIT_LOSS_SELL_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2200';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SELF_COST')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2120';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
elsif (p_report_id = 3)
then
v_param_code := '2.16';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
v_param_code := '2.17';
v_value := v_value + get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
v_param_code := '2.20';
v_value := v_value + get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'DEPRECIATION')
then
begin
if (p_report_id =1)
then
if (to_char(v_repdate, 'MM') = '12')
then
v_value := v_data_code('5.5640');
else
select value into v_val from v_emit_fin_rates where inn = p_inn and repdate = v_year_date and code = 'SELF_COST';
v_value := (v_year_data_code('5.5640')/v_val)*v_result(v_rates('SELF_COST'));
end if;
elsif (p_report_id = 3)
then
if (to_char(v_repdate, 'MM') = '12')
then
v_value := v_data_code('5.200');
else
select value into v_val from v_emit_fin_rates where inn = p_inn and repdate = v_year_date and code = 'SELF_COST';
v_value := (v_year_data_code('5.200')/v_val)*v_result(v_rates('SELF_COST'));
end if;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'EBITDA_LTM')
then
begin
if(p_report_id = 1)
then
v_value := v_result(v_rates('PROFIT_LOSS_SELL_LTM')) + v_result(v_rates('DEPRECIATION'));
if (v_result.exists(v_rates('INCOME_OTHER_LTM')) and v_result.exists(v_rates('PROFIT_LOSS_SELL_LTM'))
and v_result(v_rates('INCOME_OTHER_LTM')) > v_result(v_rates('PROFIT_LOSS_SELL_LTM')))
then
v_value := v_value + v_result(v_rates('INCOME_OTHER'));
end if;
if (v_result.exists(v_rates('INTEREST_RCVD_LTM')) and v_result.exists(v_rates('PROFIT_LOSS_SELL_LTM'))
and v_result(v_rates('INTEREST_RCVD_LTM')) > v_result(v_rates('PROFIT_LOSS_SELL_LTM')))
then
v_value := v_value + v_result(v_rates('INTEREST_RCVD_LTM')) + nvl(v_result(v_rates('INTEREST_PAID_LTM')), 0);
end if;
elsif (p_report_id = 2)
then
v_param_code := 'Операционная прибыль до учета процентов и изменений в оборотном капитале';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
elsif (p_report_id = 3)
then
v_value := nvl(v_result(v_rates('INCOME_ADJ_LTM')),0) + nvl(v_result(v_rates('SELF_COST')),0) + nvl(v_result(v_rates('DEPRECIATION')),0);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'MARGIN_EBITDA')
then
begin
--add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, null, 'EBITDA_LTM ' || v_result(v_rates('EBITDA_LTM')));
--add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, null, 'INCOME_ADJ_LTM ' || v_result(v_rates('INCOME_ADJ_LTM')));
v_value := (v_result(v_rates('EBITDA_LTM'))/v_result(v_rates('INCOME_ADJ_LTM')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'NET_PROFIT')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('2.2400');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'NET_PROFIT_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2400';
/*elsif (p_report_id = 2)
then
v_param_code := 'Проценты полученные';
elsif (p_report_id = 3)
then
v_param_code := null;*/
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'MARGIN_PROFIT')
then
begin
v_value := (v_result(v_rates('NET_PROFIT_LTM'))/v_result(v_rates('INCOME_ADJ_LTM')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LOSS_KAP')
then
begin
if (v_result(v_rates('NET_PROFIT_LTM')) <0)
then
v_value := (v_result(v_rates('NET_PROFIT_LTM'))/v_result(v_rates('KAP')))*100;
end if;
if (v_result(v_rates('MARGIN_EBITDA')) >=0)
then
v_value := 0;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LEVERAGE')
then
begin
v_value := (v_result(v_rates('KAP'))/v_result(v_rates('ACT')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'DEBT_LONG')
then
begin
if (p_report_id = 1)
then
v_param_code := '1.1410';
elsif (p_report_id = 2)
then
v_param_code := 'Долгосрочные кредиты и займы';
elsif (p_report_id = 3)
then
v_param_code := '1.28';
end if;
v_value := v_data_code(v_param_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'DEBT_SHORT')
then
begin
if (p_report_id = 1)
then
v_param_code := '1.1510';
elsif (p_report_id = 2)
then
v_param_code := 'Краткосрочные кредиты и займы';
elsif (p_report_id = 3)
then
v_param_code := '1.26';
end if;
v_value := v_data_code(v_param_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'DEBT_EBITDA')
then
begin
v_value := (v_result(v_rates('DEBT_LONG')) + v_result(v_rates('DEBT_SHORT')))/v_result(v_rates('EBITDA_LTM'));
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'PROFIT_TAX_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2410';
elsif (p_report_id = 2)
then
v_param_code := 'Налог на прибыль уплаченный';
elsif (p_report_id = 3)
then
v_param_code := '2.25';
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'FFO_LTM')
then
begin
if(p_report_id = 1)
then
v_value := v_result(v_rates('EBITDA_LTM')) + v_result(v_rates('PROFIT_TAX_LTM'));
if (v_result(v_rates('INTEREST_RCVD_LTM')) < v_result(v_rates('PROFIT_LOSS_SELL_LTM')))
then
v_value := v_value + v_result(v_rates('INTEREST_RCVD_LTM')) + v_result(v_rates('INTEREST_PAID_LTM'));
end if;
elsif (p_report_id = 2)
then
v_value := v_result(v_rates('EBITDA_LTM')) + v_result(v_rates('INTEREST_PAID_LTM')) + v_result(v_rates('INTEREST_RCVD_LTM')) - v_result(v_rates('PROFIT_TAX_LTM'));
elsif (p_report_id = 3)
then
v_value := v_result(v_rates('EBITDA_LTM')) + v_result(v_rates('INTEREST_PAID_LTM')) - v_result(v_rates('PROFIT_TAX_LTM'));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'FFO_DEBT_LTM')
then
begin
if(v_result(v_rates('DEBT_SHORT')) + v_result(v_rates('DEBT_LONG')) > 0)
then
v_value := v_result(v_rates('FFO_LTM'))/(v_result(v_rates('DEBT_LONG')) + v_result(v_rates('DEBT_SHORT')));
elsif (v_result(v_rates('DEBT_LONG')) + v_result(v_rates('DEBT_SHORT')) = 0 )
then
v_value := 0;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INTEREST_COVER')
then
begin
v_value := v_result(v_rates('EBITDA_LTM'))/abs(v_result(v_rates('INTEREST_PAID_LTM')));
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'FUNDS')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1250');
elsif (p_report_id = 2)
then
v_value := v_data_code('Денежные средства и их эквиваленты');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.1') + v_data_code('1.9');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INVEST_SHORT')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1240');
elsif (p_report_id = 2)
then
v_value := v_data_code('Краткосрочные финансовые вложения');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.2') + v_data_code('1.10');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'CFO_LTM')
then
begin
if (p_report_id = 2)
then
v_param_code := 'Чистые денежные средства, полученные от операционной деятельности';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LIQ_SHORT')
then
begin
if (p_report_id = 2) --((50)+ (51) + ЕСЛР?((52)>=0;(52)))/((44) - ЕСЛР?((52)<0;(52)))
then
v_numerator := v_result(v_rates('FUNDS')) + v_result(v_rates('INVEST_SHORT'));
v_denominator := v_result(v_rates('DEBT_SHORT'));
if (v_result(v_rates('CFO_LTM')) >= 0)
then
v_numerator := v_numerator + v_result(v_rates('CFO_LTM'));
else
v_denominator := v_denominator - v_result(v_rates('CFO_LTM'));
end if;
v_value := v_numerator / v_denominator;
else--((50) + (51) + ЕСЛР?((35)>=0;(35)))/((44) - ЕСЛР?((35)<0;(35)))
v_numerator := v_result(v_rates('FUNDS')) + v_result(v_rates('INVEST_SHORT'));
v_denominator := v_result(v_rates('DEBT_SHORT'));
if (v_result(v_rates('EBITDA_LTM')) >= 0)
then
v_numerator := v_numerator + v_result(v_rates('EBITDA_LTM'));
else
v_denominator := v_denominator - v_result(v_rates('EBITDA_LTM'));
end if;
v_value := v_numerator / v_denominator;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'ACT_CURR')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1200');
elsif (p_report_id = 2)
then
v_value := v_data_code('Итого оборотные активы');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.1') + v_data_code('1.2') + v_data_code('1.8') + v_data_code('1.19') + v_data_code('1.20');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LIAB_CURR')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1500');
elsif (p_report_id = 2)
then
v_value := v_data_code('Итого краткосрочные обязательства');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.23') + v_data_code('1.27') + v_data_code('1.28') + v_data_code('1.30') + v_data_code('1.32') + v_data_code('1.33') + v_data_code('1.34');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LIQ_CURR')
then
begin
v_value := (v_result(v_rates('ACT_CURR'))/v_result(v_rates('LIAB_CURR')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LIQ_QUICK')
then
begin
v_value := ((v_result(v_rates('FUNDS'))+ v_result(v_rates('INVEST_SHORT')))/v_result(v_rates('LIAB_CURR')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_PRIB')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.33');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_PRIB_LTM')
then
begin
if (p_report_id = 4)
then
v_param_code := '2.33';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_LOSS_PART')
then
begin
if (p_report_id = 4 and v_result.exists(v_rates('SO_PRIB')) and v_result(v_rates('SO_PRIB')) < 0)
then
v_value := v_result(v_rates('SO_PRIB'))/abs(v_result(v_rates('KAP')));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_LIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.1');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_NLIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.8');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_LIFE_PREV')
then
begin
if (p_report_id = 4)
then
v_value := v_prev_data_code('2.1');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_NLIFE_PREV')
then
begin
if (p_report_id = 4)
then
v_value := v_prev_data_code('2.8');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_DYN')
then
begin-- =( ((23)+(24))-((25)+(26)) ) / ((25)+(26)) * 100%
if (p_report_id = 4)
then
v_value := 100*(v_result(v_rates('SO_ZSP_LIFE')) + v_result(v_rates('SO_ZSP_NLIFE')) - (v_result(v_rates('SO_ZSP_LIFE_PREV')) + v_result(v_rates('SO_ZSP_NLIFE_PREV'))))
/(v_result(v_rates('SO_ZSP_LIFE_PREV')) + v_result(v_rates('SO_ZSP_NLIFE_PREV')));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_SS_ITOGO')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('6.10');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_NORM_MARGIN')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('6.20');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_MARGIN_DIFF')
then
begin
if (p_report_id = 4)
then
v_value := 100*(v_result(v_rates('SO_SS_ITOGO')) - v_result(v_rates('SO_NORM_MARGIN')))/v_result(v_rates('SO_NORM_MARGIN'));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_KAP_LIAB')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('6.30');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_UK')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.41');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_NMA')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.17');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_OAR')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.19');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_KAP_MARGIN')
then
begin
if (p_report_id = 4)
then --=( (15) - (30) - (31) ) / (12) * 100%
v_value := ((v_result(v_rates('KAP')) - v_result(v_rates('SO_NMA')) - nvl(v_result(v_rates('SO_OAR')),0))/v_result(v_rates('ACT_CURR')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_RESERVE_LIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.30');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_RESERVE_NLIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.33');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_REINS_PART_LIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.9');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_REINS_PART_NLIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.11');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_UP_SR_KAP')
then
begin
if (p_report_id = 4)
then-- = (15) / ( (33) + (34) - (35) - (36) ) * 100
v_value := (v_result(v_rates('KAP'))/(nvl(v_result(v_rates('SO_RESERVE_LIFE')),0) + nvl(v_result(v_rates('SO_RESERVE_NLIFE')),0)
- nvl(v_result(v_rates('SO_REINS_PART_LIFE')),0) - nvl(v_result(v_rates('SO_REINS_PART_NLIFE') ),0) ))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_LIQ_ASSETS')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.2')+v_data_code('1.6')+v_data_code('1.9')+v_data_code('1.11');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_CURR_LIAB')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.24')+v_data_code('1.28')+v_data_code('1.30')+v_data_code('1.33');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_KTL')
then
begin
if (p_report_id = 4)
then
v_value := v_result(v_rates('SO_LIQ_ASSETS'))/v_result(v_rates('SO_CURR_LIAB'));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_NONINS_LIAB')
then
begin
if (p_report_id = 4)
then
v_value := nvl(v_data_code('1.25'),0)+nvl(v_data_code('1.26'),0);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_DEBT_LOAD_KAP')
then
begin
if (p_report_id = 4)
then
v_value := (v_result(v_rates('SO_NONINS_LIAB'))/v_result(v_rates('SO_SS_ITOGO')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ACT_AVERAGE')
then
begin
if (p_report_id = 4)
then
select sum(nvl2(value, 1,0)) into v_cnt_values from v_emit_fin_rates
where repdate > add_months(v_repdate, -12) and repdate < v_repdate and code= 'ACT_CURR' and inn = p_inn;
if (v_cnt_values = 3 and v_result.exists(v_rates('ACT_CURR')))
then
select sum(value) into v_value
from v_emit_fin_rates
where repdate > add_months(v_repdate,-12) and repdate < v_repdate and code= 'ACT_CURR' and inn = p_inn;
v_value := (v_value + v_result(v_rates('ACT_CURR')))/4;
end if;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ROA')
then
begin
if (p_report_id = 4)
then
v_value := (v_result(v_rates('SO_PRIB_LTM'))/v_result(v_rates('SO_ACT_AVERAGE')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_KAP_AVERAGE')
then
begin
if (p_report_id = 4)
then
select sum(nvl2(value, 1,0)) into v_cnt_values from v_emit_fin_rates
where repdate > add_months(v_repdate, -12) and repdate < v_repdate and code= 'KAP' and inn = p_inn;
if (v_cnt_values = 3 and v_result.exists(v_rates('KAP')))
then
select sum(value) into v_value
from v_emit_fin_rates
where repdate > add_months(v_repdate,-12) and repdate < v_repdate and code= 'KAP' and inn = p_inn;
v_value := (v_value + v_result(v_rates('KAP')))/4;
end if;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ROE')
then
begin
if (p_report_id = 4)
then
v_value := (v_result(v_rates('SO_PRIB_LTM'))/v_result(v_rates('SO_KAP_AVERAGE')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_LOSSES_NETTO')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.9');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_EXPENSES_NETTO')
then
begin--форма 126, строка №10,1 + форма 126, строка №11 + форма 126, строка №23
if (p_report_id = 4)
then
v_value := nvl(v_data_code('2.10.1'),0) + nvl(v_data_code('2.11'),0) + nvl(v_data_code('2.23'),0);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_LOSS_RATIO')
then--= ( (53) + (54) ) * (-1) / (23) *100%
begin
if (p_report_id = 4)
then
v_value := ((v_result(v_rates('SO_LOSSES_NETTO')) + v_result(v_rates('SO_EXPENSES_NETTO')))*(-1)/v_result(v_rates('SO_ZSP_LIFE')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_INVEST_IN_OUT')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.22');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_INVEST_PROFIT')
then
begin
if (p_report_id = 4)
then
v_value := (v_result(v_rates('SO_INVEST_IN_OUT'))/v_result(v_rates('SO_ZSP_LIFE')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_COMB_LOSS_RATIO')
then
begin
if (p_report_id = 4)
then
v_value := v_result(v_rates('SO_LOSS_RATIO')) - v_result(v_rates('SO_INVEST_PROFIT'));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
/* elsif (c1.code = 'SO_ADJUSTED_CASES')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog(PLSQLUNIT,PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog(PLSQLUNIT,PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
code
elsif (c1.code = 'SO_DECLARED_CASES')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_PORTFOLIO_QUALITY')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_PAYMENTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_JUD_PAYMENTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_JUD_PART')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
/
/ elsif (c1.code = 'SO_BONUS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog(PLSQLUNIT,PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog(PLSQLUNIT,PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
code
elsif (c1.code = 'SO_BONUS_LIFE')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_BONUS_NLIFE')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_BONUS_PERSON')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_BONUS_PROPERTY')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_MAIN_TYPE')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_BONUS_LEVEL')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_MARKET_CONTRACTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_ACTIVE_CONTRACTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_ACTIVE_CONTRACTS_PREV')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_CONTRACTS_DYN')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_DELTA_MARKET_CONTRACTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
*/
end if;
v_result(c1.id) := v_value;
v_result_id(c1.id) := c1.id;
end loop;
code
FORALL i IN v_result.first..v_result.last
insert into emit_fin_rates(id, inn, repdate, rate_id, value, updatedt)
values(emit_fin_rates_seq.nextval, p_inn, v_repdate, v_result_id(i), v_result(i), sysdate);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, null, sql%rowcount || ' rows inserted into emit_fin_rates');
exception when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, v_sqlerrm);
end;
/
DO $$
DECLARE
v_repdate DATE;
v_val NUMERIC;
v_numerator NUMERIC;
v_denominator NUMERIC;
v_value NUMERIC;
v_sqlerrm VARCHAR(255);
v_param_code VARCHAR(255);
v_year_date DATE;
v_cnt_values INTEGER;
TYPE data_row IS RECORD (
param_name VARCHAR(255),
value NUMERIC
);
v_data RECORD;
v_prev_data RECORD;
v_year_data RECORD;
v_data_code JSONB := '{}';
v_prev_data_code JSONB := '{}';
v_year_data_code JSONB := '{}';
v_result JSONB := '{}';
v_result_id JSONB := '{}';
p_inn VARCHAR := '123'; -- Example input
p_repdate VARCHAR := '20230501'; -- Example input
p_report_id INTEGER := 1; -- Example input
CURSOR crates(v_rid INTEGER) IS
SELECT id, code FROM v_emit_report_rates WHERE report_id = v_rid ORDER BY id;
BEGIN
-- Simulating add_errlog function
RAISE NOTICE 'begin calc fin rates: repdate = %, inn = %, report_id = %', p_repdate, p_inn, p_report_id;
v_repdate := to_date(p_repdate, 'YYYYMMDD');
v_year_date := v_repdate - interval '1 year'; -- Assuming a placeholder function
DELETE FROM emit_fin_rates WHERE inn = p_inn AND repdate = v_repdate;
-- Loading data
SELECT json_agg(row_to_json(t))
INTO v_data
FROM (
SELECT nvl(to_char(formnum) || '.' || to_char(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_repdate AND reptype = p_report_id
) AS t;
-- Repeat similar selection for v_prev_data and v_year_data
-- (With appropriate modifications to the WHERE clause to get past year data, etc.)
IF jsonb_array_length(v_data) > 0 THEN
PERFORM jsonb_each_text(v_data);
FOR data_elem IN SELECT * FROM jsonb_array_elements(v_data) LOOP
v_data_code := v_data_code || jsonb_build_object(data_elem->>'param_name', data_elem->>'value');
END LOOP;
END IF;
-- Repeat similar for v_prev_data and v_year_data
FOR c1 IN crates(p_report_id) LOOP
v_value := NULL;
v_val := NULL;
v_param_code := NULL;
v_numerator := NULL;
v_denominator := NULL;
CASE c1.code
WHEN 'ACT' THEN
v_value := CASE p_report_id
WHEN 1 THEN v_data_code->>'1.1600'
WHEN 2 THEN v_data_code->>'Итого активы'
WHEN 3 THEN v_data_code->>'1.22'
WHEN 4 THEN v_data_code->>'1.23'
END;
-- Add other 'WHEN' conditions similarly...
ELSE
-- Optionally log a default notice for unmatched codes
RAISE NOTICE 'Code not matched: %', c1.code;
END CASE;
v_result := v_result || jsonb_build_object(c1.id::text, v_value::text);
v_result_id := v_result_id || jsonb_build_object(c1.id::text, c1.id::text);
END LOOP;
-- Insert into the database using jsonb_each_text iterators
EXECUTE format('INSERT INTO emit_fin_rates(id, inn, repdate, rate_id, value, updatedt)
SELECT nextval(''emit_fin_rates_seq''), %L, %L, each.key::INTEGER, each.value::NUMERIC, current_timestamp
FROM jsonb_each_text(%L)', p_inn, v_repdate::TEXT, v_result);
-- Simulating add_errlog function
RAISE NOTICE '% rows inserted into emit_fin_rates', SQL%ROWCOUNT;
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS v_sqlerrm = MESSAGE_TEXT;
-- Simulating add_errlog function
RAISE NOTICE 'Error: SQLCODE = %, SQLERRM = %', SQLCODE, v_sqlerrm;
END $$;
Important Considerations:
Data Types Adaptation: PostgreSQL doesn't have VARCHAR2. Use VARCHAR or TEXT instead.
Associative Arrays: PostgreSQL doesn't directly support associative arrays in PL/pgSQL. We used JSONB to store mappings frugally with JSON functions.
Bulk Collect & Record Types: PostgreSQL uses arrays and JSON to achieve similar functionalities.
Dynamic SQL Execution: Note that we use EXECUTE for dynamic SQL creation.
Handling Exceptions: PostgreSQL exceptions differ slightly. The code uses GET STACKED DIAGNOSTICS to retrieve the error message.
The example also includes sample input variables for demonstration, as they need to be provided externally, unlike Oracle procedures with formal parameters.
Key Adaptations and Considerations:
Temporary Tables:
The code uses SQL to create temporary tables from queries that populated collections in PL/SQL. These tables are populated from your existing data source views/tables.
Cursor and Iteration:
The process loop uses a cursor to iterate over the crate items, replicating the loop logic from PL/SQL.
Error Handling:
Simplified exception handling using PL/pgSQL constructs. PostgreSQL-specific messages are logged with RAISE NOTICE.
Dynamic Conditions:
The translation includes a loop to apply calculations conditionally depending on cur.code. This part should be fleshed out based on your specific needs and business logic.
Transition from PL/SQL Datatypes:
Don't forget to specify additional logic as required for conversion to PostgreSQL-friendly syntax and functions (e.g., date manipulations, handling nulls or default values).
This template requires tailored case-specific logic (similar to your PL/SQL logic) to handle specific code conditions and input parameters. When translating procedures like this, it can help to test and iterate the logic in a standalone PostgreSQL environment for debugging and feature-validation purposes.
delete from emit_fin_rates where inn = p_inn and repdate = v_repdate;
add_errlog($$PLSQL_UNIT, null, null, sql%rowcount || ' rows deleted from emit_fin_rates year date = ' || v_year_date);
select nvl(to_char(formnum) || '.' || to_char(num), name) param_name, value
bulk collect into v_data
from v_emit_data_all
where inn = p_inn and repdate = v_repdate and reptype = p_report_id;
select nvl(to_char(formnum) || '.' || to_char(num), name) param_name, value
bulk collect into v_prev_data
from v_emit_data_all
where inn = p_inn and repdate = add_months(v_repdate, -12) and reptype = p_report_id;
select nvl(to_char(formnum) || '.' || to_char(num), name) param_name, value
bulk collect into v_year_data
from v_emit_data_all
where inn = p_inn and repdate = emit_get_year_date(v_repdate) and reptype = p_report_id;
add_errlog($$PLSQL_UNIT, null, null, 'current count = ' || v_data.count || ' prev count = ' || v_prev_data.count || ' year count = ' || v_year_data.count);
if (v_data.count > 0)
then
for i in v_data.first..v_data.last
loop
v_data_code(v_data(i).param_name) := v_data(i).value;
end loop;
end if;
if (v_prev_data.count > 0)
then
for i in v_prev_data.first..v_prev_data.last
loop
v_prev_data_code(v_prev_data(i).param_name) := v_prev_data(i).value;
end loop;
end if;
if (v_year_data.count > 0)
then
for i in v_year_data.first..v_year_data.last
loop
v_year_data_code(v_year_data(i).param_name) := v_year_data(i).value;
end loop;
end if;
for c1 in crates(p_report_id)
loop
v_value := null;
v_val := null;
v_param_code := null;
v_rates(c1.code) := c1.id;
v_numerator := null;
v_denominator := null;
if (c1.code = 'ACT')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1600');
elsif (p_report_id = 2)
then
v_value := v_data_code('Итого активы');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.22');
elsif (p_report_id = 4)
then
v_value := v_data_code('1.23');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'ACT_PREV')
then
begin
if (p_report_id = 1)
then
v_value := v_prev_data_code('1.1600');
elsif (p_report_id = 2)
then
v_value := v_prev_data_code('Итого активы');
elsif (p_report_id = 3)
then
v_value := v_prev_data_code('1.22');
elsif (p_report_id = 4)
then
v_value := v_prev_data_code('1.23');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'ACT_DYN')
then
begin
v_value := ((v_result(v_rates('ACT')) - v_result(v_rates('ACT_PREV')))/v_result(v_rates('ACT_PREV')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1300');
elsif (p_report_id = 2)
then
v_value := v_data_code('Итого капитал');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.52');
elsif (p_report_id = 4)
then
v_value := v_data_code('1.51');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP_PREV')
then
begin
if (p_report_id = 1)
then
v_value := v_prev_data_code('1.1300');
elsif (p_report_id = 2)
then
v_value := v_prev_data_code('Итого капитал');
elsif (p_report_id = 3)
then
v_value := v_prev_data_code('1.52');
elsif (p_report_id = 4)
then
v_value := v_prev_data_code('1.51');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP_DYN')
then
begin
v_value := ((v_result(v_rates('KAP')) - v_result(v_rates('KAP_PREV')))/v_result(v_rates('KAP_PREV')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP_MAX')
then
begin
if (p_report_id = 1)
then
select max(value) into v_value from v_emit_data_all where inn = p_inn and repdate >add_months(v_repdate, -12) and repdate <= v_repdate and num = '1300' and formnum = 1;
elsif (p_report_id = 2)
then
select max(value) into v_value from v_emit_data_all where inn = p_inn and repdate >add_months(v_repdate, -12) and repdate <= v_repdate and param_name = 'Итого капитал';
elsif (p_report_id = 3)
then
select max(value) into v_value from v_emit_data_all where inn = p_inn and repdate >add_months(v_repdate, -12) and repdate <= v_repdate and num = '52' and formnum = 1;
elsif (p_report_id = 4)
then
select max(value) into v_value from v_emit_data_all where inn = p_inn and repdate >add_months(v_repdate, -12) and repdate <= v_repdate and num = '51' and formnum = 1;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'KAP_MAX_DYN')
then
begin
v_value := ((v_result(v_rates('KAP')) - v_result(v_rates('KAP_MAX')))/v_result(v_rates('KAP_MAX')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'PROFIT_LOSS')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1370');
elsif (p_report_id = 2)
then
v_value := v_data_code('Нераспределенная прибыль (непокрытый убыток)');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.51');
elsif (p_report_id = 4)
then
v_value := v_data_code('1.50');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INCOME_OTHER_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2310';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INCOME_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2110';
elsif (p_report_id = 2)
then
v_param_code := 'Выручка';
elsif (p_report_id = 3)
then
v_param_code := '2.15';
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INCOME_ADJ_LTM')
then
begin
v_value := v_result(v_rates('INCOME_LTM'));
--add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, 0, 'income_ltm = ' || v_result(v_rates('INCOME_LTM')) || ' income_other_ltm = ' || nvl(v_result(v_rates('INCOME_OTHER_LTM')),0) ||
--'interest_received_ltm = ' || nvl(v_result(v_rates('INTEREST_RCVD_LTM')),0));
if (p_report_id = 1)
then
if (v_result.exists(v_rates('INCOME_OTHER_LTM')) and v_result.exists(v_rates('INTEREST_RCVD_LTM'))
and v_result(v_rates('INCOME_OTHER_LTM')) + v_result(v_rates('INTEREST_RCVD_LTM')) > v_result(v_rates('INCOME_LTM')))
then
v_value := v_value + nvl(v_result(v_rates('INCOME_OTHER_LTM')),0) + nvl(v_result(v_rates('INTEREST_RCVD_LTM')),0);
end if;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INTEREST_PAID_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2330';
elsif (p_report_id = 2)
then
v_param_code := 'Проценты уплаченные';
elsif (p_report_id = 3)
then
v_param_code := '2.18';
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INTEREST_RCVD_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2320';
elsif (p_report_id = 2)
then
v_param_code := 'Проценты полученные';
elsif (p_report_id = 3)
then
v_param_code := null;
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'PROFIT_LOSS_SELL_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2200';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SELF_COST')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2120';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
elsif (p_report_id = 3)
then
v_param_code := '2.16';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
v_param_code := '2.17';
v_value := v_value + get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
v_param_code := '2.20';
v_value := v_value + get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'DEPRECIATION')
then
begin
if (p_report_id =1)
then
if (to_char(v_repdate, 'MM') = '12')
then
v_value := v_data_code('5.5640');
else
select value into v_val from v_emit_fin_rates where inn = p_inn and repdate = v_year_date and code = 'SELF_COST';
v_value := (v_year_data_code('5.5640')/v_val)*v_result(v_rates('SELF_COST'));
end if;
elsif (p_report_id = 3)
then
if (to_char(v_repdate, 'MM') = '12')
then
v_value := v_data_code('5.200');
else
select value into v_val from v_emit_fin_rates where inn = p_inn and repdate = v_year_date and code = 'SELF_COST';
v_value := (v_year_data_code('5.200')/v_val)*v_result(v_rates('SELF_COST'));
end if;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'EBITDA_LTM')
then
begin
if(p_report_id = 1)
then
v_value := v_result(v_rates('PROFIT_LOSS_SELL_LTM')) + v_result(v_rates('DEPRECIATION'));
if (v_result.exists(v_rates('INCOME_OTHER_LTM')) and v_result.exists(v_rates('PROFIT_LOSS_SELL_LTM'))
and v_result(v_rates('INCOME_OTHER_LTM')) > v_result(v_rates('PROFIT_LOSS_SELL_LTM')))
then
v_value := v_value + v_result(v_rates('INCOME_OTHER'));
end if;
if (v_result.exists(v_rates('INTEREST_RCVD_LTM')) and v_result.exists(v_rates('PROFIT_LOSS_SELL_LTM'))
and v_result(v_rates('INTEREST_RCVD_LTM')) > v_result(v_rates('PROFIT_LOSS_SELL_LTM')))
then
v_value := v_value + v_result(v_rates('INTEREST_RCVD_LTM')) + nvl(v_result(v_rates('INTEREST_PAID_LTM')), 0);
end if;
elsif (p_report_id = 2)
then
v_param_code := 'Операционная прибыль до учета процентов и изменений в оборотном капитале';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
elsif (p_report_id = 3)
then
v_value := nvl(v_result(v_rates('INCOME_ADJ_LTM')),0) + nvl(v_result(v_rates('SELF_COST')),0) + nvl(v_result(v_rates('DEPRECIATION')),0);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'MARGIN_EBITDA')
then
begin
--add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, null, 'EBITDA_LTM ' || v_result(v_rates('EBITDA_LTM')));
--add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, null, 'INCOME_ADJ_LTM ' || v_result(v_rates('INCOME_ADJ_LTM')));
v_value := (v_result(v_rates('EBITDA_LTM'))/v_result(v_rates('INCOME_ADJ_LTM')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'NET_PROFIT')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('2.2400');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'NET_PROFIT_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2400';
/*elsif (p_report_id = 2)
then
v_param_code := 'Проценты полученные';
elsif (p_report_id = 3)
then
v_param_code := null;*/
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'MARGIN_PROFIT')
then
begin
v_value := (v_result(v_rates('NET_PROFIT_LTM'))/v_result(v_rates('INCOME_ADJ_LTM')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LOSS_KAP')
then
begin
if (v_result(v_rates('NET_PROFIT_LTM')) <0)
then
v_value := (v_result(v_rates('NET_PROFIT_LTM'))/v_result(v_rates('KAP')))*100;
end if;
if (v_result(v_rates('MARGIN_EBITDA')) >=0)
then
v_value := 0;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LEVERAGE')
then
begin
v_value := (v_result(v_rates('KAP'))/v_result(v_rates('ACT')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'DEBT_LONG')
then
begin
if (p_report_id = 1)
then
v_param_code := '1.1410';
elsif (p_report_id = 2)
then
v_param_code := 'Долгосрочные кредиты и займы';
elsif (p_report_id = 3)
then
v_param_code := '1.28';
end if;
v_value := v_data_code(v_param_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'DEBT_SHORT')
then
begin
if (p_report_id = 1)
then
v_param_code := '1.1510';
elsif (p_report_id = 2)
then
v_param_code := 'Краткосрочные кредиты и займы';
elsif (p_report_id = 3)
then
v_param_code := '1.26';
end if;
v_value := v_data_code(v_param_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'DEBT_EBITDA')
then
begin
v_value := (v_result(v_rates('DEBT_LONG')) + v_result(v_rates('DEBT_SHORT')))/v_result(v_rates('EBITDA_LTM'));
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'PROFIT_TAX_LTM')
then
begin
if (p_report_id = 1)
then
v_param_code := '2.2410';
elsif (p_report_id = 2)
then
v_param_code := 'Налог на прибыль уплаченный';
elsif (p_report_id = 3)
then
v_param_code := '2.25';
end if;
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'FFO_LTM')
then
begin
if(p_report_id = 1)
then
v_value := v_result(v_rates('EBITDA_LTM')) + v_result(v_rates('PROFIT_TAX_LTM'));
if (v_result(v_rates('INTEREST_RCVD_LTM')) < v_result(v_rates('PROFIT_LOSS_SELL_LTM')))
then
v_value := v_value + v_result(v_rates('INTEREST_RCVD_LTM')) + v_result(v_rates('INTEREST_PAID_LTM'));
end if;
elsif (p_report_id = 2)
then
v_value := v_result(v_rates('EBITDA_LTM')) + v_result(v_rates('INTEREST_PAID_LTM')) + v_result(v_rates('INTEREST_RCVD_LTM')) - v_result(v_rates('PROFIT_TAX_LTM'));
elsif (p_report_id = 3)
then
v_value := v_result(v_rates('EBITDA_LTM')) + v_result(v_rates('INTEREST_PAID_LTM')) - v_result(v_rates('PROFIT_TAX_LTM'));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'FFO_DEBT_LTM')
then
begin
if(v_result(v_rates('DEBT_SHORT')) + v_result(v_rates('DEBT_LONG')) > 0)
then
v_value := v_result(v_rates('FFO_LTM'))/(v_result(v_rates('DEBT_LONG')) + v_result(v_rates('DEBT_SHORT')));
elsif (v_result(v_rates('DEBT_LONG')) + v_result(v_rates('DEBT_SHORT')) = 0 )
then
v_value := 0;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INTEREST_COVER')
then
begin
v_value := v_result(v_rates('EBITDA_LTM'))/abs(v_result(v_rates('INTEREST_PAID_LTM')));
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'FUNDS')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1250');
elsif (p_report_id = 2)
then
v_value := v_data_code('Денежные средства и их эквиваленты');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.1') + v_data_code('1.9');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'INVEST_SHORT')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1240');
elsif (p_report_id = 2)
then
v_value := v_data_code('Краткосрочные финансовые вложения');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.2') + v_data_code('1.10');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'CFO_LTM')
then
begin
if (p_report_id = 2)
then
v_param_code := 'Чистые денежные средства, полученные от операционной деятельности';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LIQ_SHORT')
then
begin
if (p_report_id = 2) --((50)+ (51) + ЕСЛР?((52)>=0;(52)))/((44) - ЕСЛР?((52)<0;(52)))
then
v_numerator := v_result(v_rates('FUNDS')) + v_result(v_rates('INVEST_SHORT'));
v_denominator := v_result(v_rates('DEBT_SHORT'));
if (v_result(v_rates('CFO_LTM')) >= 0)
then
v_numerator := v_numerator + v_result(v_rates('CFO_LTM'));
else
v_denominator := v_denominator - v_result(v_rates('CFO_LTM'));
end if;
v_value := v_numerator / v_denominator;
else--((50) + (51) + ЕСЛР?((35)>=0;(35)))/((44) - ЕСЛР?((35)<0;(35)))
v_numerator := v_result(v_rates('FUNDS')) + v_result(v_rates('INVEST_SHORT'));
v_denominator := v_result(v_rates('DEBT_SHORT'));
if (v_result(v_rates('EBITDA_LTM')) >= 0)
then
v_numerator := v_numerator + v_result(v_rates('EBITDA_LTM'));
else
v_denominator := v_denominator - v_result(v_rates('EBITDA_LTM'));
end if;
v_value := v_numerator / v_denominator;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'ACT_CURR')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1200');
elsif (p_report_id = 2)
then
v_value := v_data_code('Итого оборотные активы');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.1') + v_data_code('1.2') + v_data_code('1.8') + v_data_code('1.19') + v_data_code('1.20');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LIAB_CURR')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1500');
elsif (p_report_id = 2)
then
v_value := v_data_code('Итого краткосрочные обязательства');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.23') + v_data_code('1.27') + v_data_code('1.28') + v_data_code('1.30') + v_data_code('1.32') + v_data_code('1.33') + v_data_code('1.34');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LIQ_CURR')
then
begin
v_value := (v_result(v_rates('ACT_CURR'))/v_result(v_rates('LIAB_CURR')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'LIQ_QUICK')
then
begin
v_value := ((v_result(v_rates('FUNDS'))+ v_result(v_rates('INVEST_SHORT')))/v_result(v_rates('LIAB_CURR')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_PRIB')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.33');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_PRIB_LTM')
then
begin
if (p_report_id = 4)
then
v_param_code := '2.33';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_LOSS_PART')
then
begin
if (p_report_id = 4 and v_result.exists(v_rates('SO_PRIB')) and v_result(v_rates('SO_PRIB')) < 0)
then
v_value := v_result(v_rates('SO_PRIB'))/abs(v_result(v_rates('KAP')));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_LIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.1');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_NLIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.8');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_LIFE_PREV')
then
begin
if (p_report_id = 4)
then
v_value := v_prev_data_code('2.1');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_NLIFE_PREV')
then
begin
if (p_report_id = 4)
then
v_value := v_prev_data_code('2.8');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ZSP_DYN')
then
begin-- =( ((23)+(24))-((25)+(26)) ) / ((25)+(26)) * 100%
if (p_report_id = 4)
then
v_value := 100*(v_result(v_rates('SO_ZSP_LIFE')) + v_result(v_rates('SO_ZSP_NLIFE')) - (v_result(v_rates('SO_ZSP_LIFE_PREV')) + v_result(v_rates('SO_ZSP_NLIFE_PREV'))))
/(v_result(v_rates('SO_ZSP_LIFE_PREV')) + v_result(v_rates('SO_ZSP_NLIFE_PREV')));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_SS_ITOGO')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('6.10');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_NORM_MARGIN')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('6.20');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_MARGIN_DIFF')
then
begin
if (p_report_id = 4)
then
v_value := 100*(v_result(v_rates('SO_SS_ITOGO')) - v_result(v_rates('SO_NORM_MARGIN')))/v_result(v_rates('SO_NORM_MARGIN'));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_KAP_LIAB')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('6.30');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_UK')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.41');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_NMA')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.17');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_OAR')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.19');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_KAP_MARGIN')
then
begin
if (p_report_id = 4)
then --=( (15) - (30) - (31) ) / (12) * 100%
v_value := ((v_result(v_rates('KAP')) - v_result(v_rates('SO_NMA')) - nvl(v_result(v_rates('SO_OAR')),0))/v_result(v_rates('ACT_CURR')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_RESERVE_LIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.30');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_RESERVE_NLIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.33');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_REINS_PART_LIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.9');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_REINS_PART_NLIFE')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.11');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_UP_SR_KAP')
then
begin
if (p_report_id = 4)
then-- = (15) / ( (33) + (34) - (35) - (36) ) * 100
v_value := (v_result(v_rates('KAP'))/(nvl(v_result(v_rates('SO_RESERVE_LIFE')),0) + nvl(v_result(v_rates('SO_RESERVE_NLIFE')),0)
- nvl(v_result(v_rates('SO_REINS_PART_LIFE')),0) - nvl(v_result(v_rates('SO_REINS_PART_NLIFE') ),0) ))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_LIQ_ASSETS')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.2')+v_data_code('1.6')+v_data_code('1.9')+v_data_code('1.11');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_CURR_LIAB')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('1.24')+v_data_code('1.28')+v_data_code('1.30')+v_data_code('1.33');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_KTL')
then
begin
if (p_report_id = 4)
then
v_value := v_result(v_rates('SO_LIQ_ASSETS'))/v_result(v_rates('SO_CURR_LIAB'));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_NONINS_LIAB')
then
begin
if (p_report_id = 4)
then
v_value := nvl(v_data_code('1.25'),0)+nvl(v_data_code('1.26'),0);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_DEBT_LOAD_KAP')
then
begin
if (p_report_id = 4)
then
v_value := (v_result(v_rates('SO_NONINS_LIAB'))/v_result(v_rates('SO_SS_ITOGO')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ACT_AVERAGE')
then
begin
if (p_report_id = 4)
then
select sum(nvl2(value, 1,0)) into v_cnt_values from v_emit_fin_rates
where repdate > add_months(v_repdate, -12) and repdate < v_repdate and code= 'ACT_CURR' and inn = p_inn;
if (v_cnt_values = 3 and v_result.exists(v_rates('ACT_CURR')))
then
select sum(value) into v_value
from v_emit_fin_rates
where repdate > add_months(v_repdate,-12) and repdate < v_repdate and code= 'ACT_CURR' and inn = p_inn;
v_value := (v_value + v_result(v_rates('ACT_CURR')))/4;
end if;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ROA')
then
begin
if (p_report_id = 4)
then
v_value := (v_result(v_rates('SO_PRIB_LTM'))/v_result(v_rates('SO_ACT_AVERAGE')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_KAP_AVERAGE')
then
begin
if (p_report_id = 4)
then
select sum(nvl2(value, 1,0)) into v_cnt_values from v_emit_fin_rates
where repdate > add_months(v_repdate, -12) and repdate < v_repdate and code= 'KAP' and inn = p_inn;
if (v_cnt_values = 3 and v_result.exists(v_rates('KAP')))
then
select sum(value) into v_value
from v_emit_fin_rates
where repdate > add_months(v_repdate,-12) and repdate < v_repdate and code= 'KAP' and inn = p_inn;
v_value := (v_value + v_result(v_rates('KAP')))/4;
end if;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_ROE')
then
begin
if (p_report_id = 4)
then
v_value := (v_result(v_rates('SO_PRIB_LTM'))/v_result(v_rates('SO_KAP_AVERAGE')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_LOSSES_NETTO')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.9');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_EXPENSES_NETTO')
then
begin--форма 126, строка №10,1 + форма 126, строка №11 + форма 126, строка №23
if (p_report_id = 4)
then
v_value := nvl(v_data_code('2.10.1'),0) + nvl(v_data_code('2.11'),0) + nvl(v_data_code('2.23'),0);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_LOSS_RATIO')
then--= ( (53) + (54) ) * (-1) / (23) *100%
begin
if (p_report_id = 4)
then
v_value := ((v_result(v_rates('SO_LOSSES_NETTO')) + v_result(v_rates('SO_EXPENSES_NETTO')))*(-1)/v_result(v_rates('SO_ZSP_LIFE')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_INVEST_IN_OUT')
then
begin
if (p_report_id = 4)
then
v_value := v_data_code('2.22');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_INVEST_PROFIT')
then
begin
if (p_report_id = 4)
then
v_value := (v_result(v_rates('SO_INVEST_IN_OUT'))/v_result(v_rates('SO_ZSP_LIFE')))*100;
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'SO_COMB_LOSS_RATIO')
then
begin
if (p_report_id = 4)
then
v_value := v_result(v_rates('SO_LOSS_RATIO')) - v_result(v_rates('SO_INVEST_PROFIT'));
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
/* elsif (c1.code = 'SO_ADJUSTED_CASES')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog(PLSQLUNIT,PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog(PLSQLUNIT,PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
code
elsif (c1.code = 'SO_DECLARED_CASES')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_PORTFOLIO_QUALITY')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_PAYMENTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_JUD_PAYMENTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_JUD_PART')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
/
/ elsif (c1.code = 'SO_BONUS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog(PLSQLUNIT,PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog(PLSQLUNIT,PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
code
elsif (c1.code = 'SO_BONUS_LIFE')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_BONUS_NLIFE')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_BONUS_PERSON')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_BONUS_PROPERTY')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_MAIN_TYPE')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_BONUS_LEVEL')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_MARKET_CONTRACTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_ACTIVE_CONTRACTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_ACTIVE_CONTRACTS_PREV')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_CONTRACTS_DYN')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
elsif (c1.code = 'SO_DELTA_MARKET_CONTRACTS')
then
begin
if (p_report_id = 4)
then
v_param_code := ' ';
v_value := get_ltm_data(v_repdate, v_param_code, v_data_code, v_prev_data_code, v_year_data_code);
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
*/
end if;
v_result(c1.id) := v_value;
v_result_id(c1.id) := c1.id;
end loop;
code
FORALL i IN v_result.first..v_result.last
insert into emit_fin_rates(id, inn, repdate, rate_id, value, updatedt)
values(emit_fin_rates_seq.nextval, p_inn, v_repdate, v_result_id(i), v_result(i), sysdate);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, null, sql%rowcount || ' rows inserted into emit_fin_rates');
exception when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, v_sqlerrm);
end;
/
CREATE OR REPLACE FUNCTION emit_calc_fin_rates(p_inn VARCHAR, p_repdate VARCHAR, p_report_id INTEGER)
RETURNS VOID AS $$
DECLARE
v_repdate DATE;
v_year_date DATE;
v_value NUMERIC;
v_sqlerrm TEXT;
v_param_code TEXT;
v_numerator NUMERIC;
v_denominator NUMERIC;
BEGIN
-- Logging start
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'Begin calculation: repdate = ' || p_repdate || ', inn = ' || p_inn || ', report_id = ' || p_report_id);
-- Date parsing and setting
v_repdate := TO_DATE(p_repdate, 'YYYYMMDD');
v_year_date := v_repdate - INTERVAL '1 year'; -- Assume emit_get_year_date equivalent
-- Create and populate temporary data tables
CREATE TEMP TABLE temp_data AS
SELECT COALESCE(TO_CHAR(formnum) || '.' || TO_CHAR(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_repdate AND reptype = p_report_id;
CREATE TEMP TABLE temp_prev_data AS
SELECT COALESCE(TO_CHAR(formnum) || '.' || TO_CHAR(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_repdate - INTERVAL '1 year' AND reptype = p_report_id;
CREATE TEMP TABLE temp_year_data AS
SELECT COALESCE(TO_CHAR(formnum) || '.' || TO_CHAR(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_year_date AND reptype = p_report_id;
-- Delete old data
DELETE FROM emit_fin_rates WHERE inn = p_inn AND repdate = v_repdate;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, SQL%ROWCOUNT || ' rows deleted from emit_fin_rates');
-- Iterate over rates
FOR record IN (SELECT id, code FROM v_emit_report_rates WHERE report_id = p_report_id ORDER BY id) LOOP
v_value := NULL; -- Reset the result value for each loop iteration
-- Logic for different codes
CASE record.code
WHEN 'ACT' THEN
SELECT value INTO v_value FROM temp_data WHERE param_name = '1.1600';
WHEN 'ACT_PREV' THEN
SELECT value INTO v_value FROM temp_prev_data WHERE param_name = '1.1600';
-- Add other conditions for the other codes
ELSE
-- Handle unmatched code (optionally log)
RAISE NOTICE 'Unhandled code: %', record.code;
END CASE;
-- Insert into result
IF v_value IS NOT NULL THEN
INSERT INTO emit_fin_rates (id, inn, repdate, rate_id, value, updatedt)
VALUES (nextval('emit_fin_rates_seq'), p_inn, v_repdate, record.id, v_value, CURRENT_TIMESTAMP);
END IF;
END LOOP;
-- Final log statement
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, SQL%ROWCOUNT || ' rows inserted into emit_fin_rates');
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS v_sqlerrm = MESSAGE_TEXT;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'Error: ' || v_sqlerrm);
END;
$$ LANGUAGE plpgsql;
Key Considerations:
Temporary Tables:
These tables are created to store interim data, replacing Oracle’s collection types. They are dropped automatically at the end of the session unless explicitly dropped earlier.
Control Logic:
CASE statements are used to handle logic previously dependent on Oracle’s ELSIF.
Exception Handling:
Simplified to use PostgreSQL's exception handling, logging errors via a hypothetical add_errlog function.
Database Custom Functions:
Assumed presence of add_errlog and get_ltm_data which are invoked but not defined here.
Dynamic Behavior:
Customize each code logic segment based on the existing PL/SQL patterns, and ensure all mapping logic for matching codes is properly implemented.
Execution in Greenplum:
Greenplum-specific considerations for data distribution and parallel processing can impact performance and require changes, especially with large data sets. Ensure that performance and distribution strategies are addressed based on actual data sizes and access patterns beyond this translation.
This example represents a significant simplification of your previous logic, focusing on translating structural elements. For full functionality, each section of the logic must be expanded to meet your specific computational or business requirements.
Column Specification:
In INSERT INTO original_table (columns_list), make sure to specify all columns that you want to insert from the temp_update table, including the column(s) with the updated value.
Handling Unique Constraints:
Ensure that the operation maintains any unique constraints or primary keys on the table.
Transaction Management:
Wrapping this sequence of operations within a transaction can help maintain data consistency in case of failure. In Greenplum, this typically means each call to this procedure should be isolated in a transaction block if it affects integrity.
Concurrency and Locking:
Consider potential concurrency issues where other transactions might interact with these records during the delete/insert sweep.
Implementing this strategy requires careful testing to ensure that all edge cases are handled and that the performance is as expected relative to a direct update. This technique is particularly useful in large append-only systems or scenarios where updates are complex or expensive.
delete from emit_fin_rates where inn = p_inn and repdate = v_repdate;
add_errlog($$PLSQL_UNIT, null, null, sql%rowcount || ' rows deleted from emit_fin_rates year date = ' || v_year_date);
select nvl(to_char(formnum) || '.' || to_char(num), name) param_name, value
bulk collect into v_data
from v_emit_data_all
where inn = p_inn and repdate = v_repdate and reptype = p_report_id;
select nvl(to_char(formnum) || '.' || to_char(num), name) param_name, value
bulk collect into v_prev_data
from v_emit_data_all
where inn = p_inn and repdate = add_months(v_repdate, -12) and reptype = p_report_id;
select nvl(to_char(formnum) || '.' || to_char(num), name) param_name, value
bulk collect into v_year_data
from v_emit_data_all
where inn = p_inn and repdate = emit_get_year_date(v_repdate) and reptype = p_report_id;
add_errlog($$PLSQL_UNIT, null, null, 'current count = ' || v_data.count || ' prev count = ' || v_prev_data.count || ' year count = ' || v_year_data.count);
if (v_data.count > 0)
then
for i in v_data.first..v_data.last
loop
v_data_code(v_data(i).param_name) := v_data(i).value;
end loop;
end if;
if (v_prev_data.count > 0)
then
for i in v_prev_data.first..v_prev_data.last
loop
v_prev_data_code(v_prev_data(i).param_name) := v_prev_data(i).value;
end loop;
end if;
if (v_year_data.count > 0)
then
for i in v_year_data.first..v_year_data.last
loop
v_year_data_code(v_year_data(i).param_name) := v_year_data(i).value;
end loop;
end if;
for c1 in crates(p_report_id)
loop
v_value := null;
v_val := null;
v_param_code := null;
v_rates(c1.code) := c1.id;
v_numerator := null;
v_denominator := null;
if (c1.code = 'ACT')
then
begin
if (p_report_id = 1)
then
v_value := v_data_code('1.1600');
elsif (p_report_id = 2)
then
v_value := v_data_code('Итого активы');
elsif (p_report_id = 3)
then
v_value := v_data_code('1.22');
elsif (p_report_id = 4)
then
v_value := v_data_code('1.23');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'ACT_PREV')
then
begin
if (p_report_id = 1)
then
v_value := v_prev_data_code('1.1600');
elsif (p_report_id = 2)
then
v_value := v_prev_data_code('Итого активы');
elsif (p_report_id = 3)
then
v_value := v_prev_data_code('1.22');
elsif (p_report_id = 4)
then
v_value := v_prev_data_code('1.23');
end if;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
elsif (c1.code = 'ACT_DYN')
then
begin
v_value := ((v_result(v_rates('ACT')) - v_result(v_rates('ACT_PREV')))/v_result(v_rates('ACT_PREV')))*100;
exception
when no_data_found then
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'rate ' || c1.code || ' not found, value = null');
when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, 'cannot save norm ' || c1.code || ' ' || v_sqlerrm);
end;
end if;
v_result(c1.id) := v_value;
v_result_id(c1.id) := c1.id;
end loop;
FORALL i IN v_result.first..v_result.last
insert into emit_fin_rates(id, inn, repdate, rate_id, value, updatedt)
values(emit_fin_rates_seq.nextval, p_inn, v_repdate, v_result_id(i), v_result(i), sysdate);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, null, sql%rowcount || ' rows inserted into emit_fin_rates');
exception when others then
v_sqlerrm := substr(sqlerrm, 1, 200);
add_errlog($$PLSQL_UNIT, $$PLSQL_LINE, sqlcode, v_sqlerrm);
end;
/
code
CREATE OR REPLACE FUNCTION emit_calc_fin_rates(p_inn VARCHAR, p_repdate VARCHAR, p_report_id INTEGER)
RETURNS VOID AS $$
DECLARE
v_repdate DATE;
v_val DOUBLE PRECISION;
v_numerator DOUBLE PRECISION;
v_denominator DOUBLE PRECISION;
v_value DOUBLE PRECISION;
v_sqlerrm TEXT;
v_param_code TEXT;
v_year_date DATE;
v_cnt_values INTEGER;
BEGIN
-- Assume these functions are implemented or necessary computations are replaced
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'Begin calc fin rates: repdate = ' || p_repdate || ', inn = ' || p_inn || ', report_id = ' || p_report_id);
v_repdate := TO_DATE(p_repdate, 'YYYYMMDD');
v_year_date := v_repdate - INTERVAL '1 year'; -- Adjust as needed for emit_get_year_date equivalent
-- Delete existing records
DELETE FROM emit_fin_rates WHERE inn = p_inn AND repdate = v_repdate;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, ROW_COUNT() || ' rows deleted from emit_fin_rates');
-- Create and populate temporary tables
CREATE TEMP TABLE temp_data AS
SELECT COALESCE(TO_CHAR(formnum) || '.' || TO_CHAR(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_repdate AND reptype = p_report_id;
CREATE TEMP TABLE temp_prev_data AS
SELECT COALESCE(TO_CHAR(formnum) || '.' || TO_CHAR(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_repdate - INTERVAL '1 year' AND reptype = p_report_id;
CREATE TEMP TABLE temp_year_data AS
SELECT COALESCE(TO_CHAR(formnum) || '.' || TO_CHAR(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_year_date AND reptype = p_report_id;
-- Log counts from temporary tables
SELECT COUNT(*) INTO v_cnt_values FROM temp_data;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'current count = ' || v_cnt_values);
SELECT COUNT(*) INTO v_cnt_values FROM temp_prev_data;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'prev count = ' || v_cnt_values);
SELECT COUNT(*) INTO v_cnt_values FROM temp_year_data;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'year count = ' || v_cnt_values);
-- Iterate over rates
FOR record IN SELECT id, code FROM v_emit_report_rates WHERE report_id = p_report_id ORDER BY id LOOP
v_value := NULL; -- Reset the result value for each loop iteration
-- Logic for different codes
BEGIN
IF record.code = 'ACT' THEN
SELECT INTO v_value value FROM temp_data WHERE param_name = '1.1600'; -- Example
ELSIF record.code = 'ACT_PREV' THEN
SELECT INTO v_value value FROM temp_prev_data WHERE param_name = '1.1600'; -- Example
ELSIF record.code = 'ACT_DYN' THEN
-- Assuming v_result is somehow populated elsewhere in the adapted code
-- Compute dynamic values between ACT and ACT_PREV
v_value := ((v_result[record.id] - v_result[record.id]) / v_result[record.id]) * 100;
END IF;
EXCEPTION
WHEN OTHERS THEN
v_sqlerrm := SQLERRM;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'Error in CASE: ' || v_sqlerrm);
END;
-- Insert updated record value
IF v_value IS NOT NULL THEN
INSERT INTO emit_fin_rates (id, inn, repdate, rate_id, value, updatedt)
VALUES (nextval('emit_fin_rates_seq'), p_inn, v_repdate, record.id, v_value, CURRENT_TIMESTAMP);
END IF;
END LOOP;
-- Log insert count
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'Inserts complete');
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS v_sqlerrm = MESSAGE_TEXT;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'General error: ' || v_sqlerrm);
END;
$$ LANGUAGE plpgsql;
Key Details
Temporary Tables: Used to store v_data, v_prev_data, and v_year_data from the original PL/SQL collections.
Data Processing: Using SQL queries to populate temporary tables and then operating on these tables emulates bulk operations in PL/SQL.
Data Access via SQL: Replaces indexed table access with direct SQL queries in temporary tables.
Exception Handling: Used EXCEPTION blocks to catch errors, with diagnostics and logging for errors.
Feature Translation: Adjustments for PostgreSQL 9.4 have been made, with adaptations to existing PL/SQL features like NVL and bulk collections.
Finalize this function by ensuring all embedded functions (like add_errlog) are properly implemented in your PostgreSQL environment, and test as needed to confirm behavior aligns with previous PL/SQL execution.
CREATE OR REPLACE FUNCTION emit_calc_fin_rates(p_inn VARCHAR, p_repdate VARCHAR, p_report_id INTEGER)
RETURNS VOID AS $$
DECLARE
v_repdate DATE;
v_year_date DATE;
v_value DOUBLE PRECISION;
v_sqlerrm TEXT;
BEGIN
-- Log the beginning of processing
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'Begin calc fin rates: repdate = ' || p_repdate || ', inn = ' || p_inn || ', report_id = ' || p_report_id);
-- Parse the input dates
v_repdate := TO_DATE(p_repdate, 'YYYYMMDD');
v_year_date := v_repdate - INTERVAL '1 year'; -- Placeholder logic for emit_get_year_date equivalent
-- Clear existing data
DELETE FROM emit_fin_rates WHERE inn = p_inn AND repdate = v_repdate;
-- Use temporary tables to emulate PL/SQL collections
CREATE TEMP TABLE temp_data AS
SELECT COALESCE(TO_CHAR(formnum) || '.' || TO_CHAR(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_repdate AND reptype = p_report_id;
CREATE TEMP TABLE temp_prev_data AS
SELECT COALESCE(TO_CHAR(formnum) || '.' || TO_CHAR(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_repdate - INTERVAL '1 year' AND reptype = p_report_id;
CREATE TEMP TABLE temp_year_data AS
SELECT COALESCE(TO_CHAR(formnum) || '.' || TO_CHAR(num), name) AS param_name, value
FROM v_emit_data_all
WHERE inn = p_inn AND repdate = v_year_date AND reptype = p_report_id;
-- Temporary table for results
CREATE TEMP TABLE temp_result (
id INTEGER,
rate_id INTEGER,
value DOUBLE PRECISION
);
-- Cursor to iterate through rate definitions
FOR record IN SELECT id, code FROM v_emit_report_rates WHERE report_id = p_report_id ORDER BY id LOOP
BEGIN
v_value := NULL; -- Reset value for each rate
-- Handle specific codes
IF record.code = 'ACT' THEN
SELECT value INTO v_value FROM temp_data WHERE param_name = '1.1600';
ELSIF record.code = 'ACT_PREV' THEN
SELECT value INTO v_value FROM temp_prev_data WHERE param_name = '1.1600';
ELSIF record.code = 'ACT_DYN' THEN
-- Example calculation with dynamic values (assumes stored results from previous codes)
v_value := (
SELECT ((r1.value - r2.value) / r2.value) * 100
FROM temp_result r1, temp_result r2
WHERE r1.rate_id = record.id AND r2.rate_id = (SELECT id FROM v_emit_report_rates WHERE code = 'ACT_PREV' AND report_id = p_report_id)
);
END IF;
-- Insert each calculated result into temp_result
INSERT INTO temp_result (id, rate_id, value) VALUES (record.id, record.id, v_value);
EXCEPTION
WHEN OTHERS THEN
v_sqlerrm := SQLERRM;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'Error processing code ' || record.code || ': ' || v_sqlerrm);
END;
END LOOP;
-- Insert from temp_result into the main table
INSERT INTO emit_fin_rates (id, inn, repdate, rate_id, value, updatedt)
SELECT nextval('emit_fin_rates_seq'), p_inn, v_repdate, rate_id, value, CURRENT_TIMESTAMP FROM temp_result
WHERE value IS NOT NULL;
-- Log the number of inserted rows
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, ROW_COUNT() || ' rows inserted into emit_fin_rates');
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS v_sqlerrm = MESSAGE_TEXT;
PERFORM add_errlog('emit_calc_fin_rates', NULL, NULL, 'General error: ' || v_sqlerrm);
END;
$$ LANGUAGE plpgsql;
Explanation:
Temporary Tables:
temp_data, temp_prev_data, and temp_year_data hold the values initially loaded into v_data, v_prev_data, v_year_data.
temp_result is created to store intermediate calculated results, replicating v_result.
Calculation Logic:
The logic for calculating values such as ACT or ACT_DYN uses direct SQL queries in PostgreSQL.
Use existing values stored in temp_result for dynamic calculations (e.g., ACT_DYN).
Exception Handling:
Similar exception handling as in Oracle with OTHERS clause and logging using add_errlog.
Insertion:
Results are inserted from temp_result into the final table emit_fin_rates, ensuring no null values get inserted.
Use of rate_id:
rate_id serves as an identifier for calculating and relating rates in temporary calculations.
This conversion respects the organization and tracking found in the original PL/SQL script, with appropriate use of PostgreSQL features for similar functionality.
5. Table Partitioning or Union-Free Design
Revisit your database design to determine if data partitioning or restructuring tables could help. Storing the data in a way that naturally avoids needing a UNION operation may lead to better solutions. For example, if feasible, integrating data from both sources into a unified view might be possible.
6. Result Set Consolidation
If permissible and valuable for your use case, you may load data from both queries into a temporary or staging table, handle deduplication or necessary adjustments (if required), and select the final result from this table.
sql
CREATE TEMP TABLE temp_result AS
SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2;
/* Additional processing to remove duplicates or refine results */
DELETE FROM temp_result
WHERE condition_for_duplicate_removal;
/* Retrieve final result set */
SELECT * FROM temp_result;
Conclusion
Adopting these strategies depends heavily on your particular use case and database schema. Consider the dataset sizes, relationships, and performance requirements when choosing alternatives to UNION in Greenplum. Always profile any new approach with real-world tests to confirm its effectiveness.