diff --git a/api/alembic/versions/sql/0002_functions.sql b/api/alembic/versions/sql/0002_functions.sql index 0670ae1..28f718a 100644 --- a/api/alembic/versions/sql/0002_functions.sql +++ b/api/alembic/versions/sql/0002_functions.sql @@ -4085,23 +4085,35 @@ $function$ -- DROP FUNCTION v3.v_form1_smeta(int4, int4); -CREATE OR REPLACE FUNCTION v3.v_form1_smeta(p_year integer, p_org_unit_id integer) - RETURNS TABLE(row_type character varying, depth integer, section_code character varying, name character varying, supp_plan_q1 numeric, supp_plan_q2 numeric, supp_plan_q3 numeric, supp_plan_q4 numeric, supp_plan_year numeric, dev_plan_q1 numeric, dev_plan_q2 numeric, dev_plan_q3 numeric, dev_plan_q4 numeric, dev_plan_year numeric, total_plan_year numeric, supp_appr_q1 numeric, supp_appr_q2 numeric, supp_appr_q3 numeric, supp_appr_q4 numeric, supp_appr_year numeric, dev_appr_q1 numeric, dev_appr_q2 numeric, dev_appr_q3 numeric, dev_appr_q4 numeric, dev_appr_year numeric, total_appr_year numeric, supp_act_q1 numeric, supp_act_q2 numeric, supp_act_q3 numeric, supp_act_q4 numeric, supp_act_year numeric, dev_act_q1 numeric, dev_act_q2 numeric, dev_act_q3 numeric, dev_act_q4 numeric, dev_act_year numeric, total_act_year numeric, supp_corr_q2 numeric, supp_corr_q3 numeric, supp_corr_q4 numeric, dev_corr_q2 numeric, dev_corr_q3 numeric, dev_corr_q4 numeric) - LANGUAGE plpgsql - STABLE - SET search_path TO 'v3', 'pg_catalog' -AS $function$ + +CREATE FUNCTION v3.v_form1_smeta(p_year INT, p_org_unit_id INT) +RETURNS TABLE ( + row_type VARCHAR, + depth INT, + section_code VARCHAR, + name VARCHAR, + supp_plan_q1 NUMERIC, supp_plan_q2 NUMERIC, supp_plan_q3 NUMERIC, supp_plan_q4 NUMERIC, supp_plan_year NUMERIC, + dev_plan_q1 NUMERIC, dev_plan_q2 NUMERIC, dev_plan_q3 NUMERIC, dev_plan_q4 NUMERIC, dev_plan_year NUMERIC, + total_plan_year NUMERIC, + supp_appr_q1 NUMERIC, supp_appr_q2 NUMERIC, supp_appr_q3 NUMERIC, supp_appr_q4 NUMERIC, supp_appr_year NUMERIC, + dev_appr_q1 NUMERIC, dev_appr_q2 NUMERIC, dev_appr_q3 NUMERIC, dev_appr_q4 NUMERIC, dev_appr_year NUMERIC, + total_appr_year NUMERIC, + supp_act_q1 NUMERIC, supp_act_q2 NUMERIC, supp_act_q3 NUMERIC, supp_act_q4 NUMERIC, supp_act_year NUMERIC, + dev_act_q1 NUMERIC, dev_act_q2 NUMERIC, dev_act_q3 NUMERIC, dev_act_q4 NUMERIC, dev_act_year NUMERIC, + total_act_year NUMERIC, + supp_corr_q2 NUMERIC, supp_corr_q3 NUMERIC, supp_corr_q4 NUMERIC, + dev_corr_q2 NUMERIC, dev_corr_q3 NUMERIC, dev_corr_q4 NUMERIC +) +AS $$ #variable_conflict use_column DECLARE - v_ahr_s INT; v_ahr_d INT; - v_cap_s INT; v_cap_d INT; - v_oper_s INT; v_oper_d INT; + v_ahr INT; v_cap INT; v_oper INT; v_sections TEXT[] := ARRAY['plan','approved','q1','q2','q3','q4']; BEGIN - -- Resolve form_ids: within each (year, ssp, sheet), lower bf.id = Support, higher = Development + -- Одна budget_form на лист содержит ОБА направления (budget_line.direction). + -- Support/Development различаются параметром p_direction, а не отдельной формой. WITH ranked AS ( - SELECT bf.id AS fid, ei.sheet AS sh, - ROW_NUMBER() OVER (PARTITION BY ei.sheet ORDER BY bf.id) AS rn + SELECT bf.id AS fid, ei.sheet AS sh FROM budget_form bf JOIN budget_line bl ON bl.budget_form_id = bf.id JOIN expense_item ei ON ei.id = bl.expense_item_id @@ -4109,18 +4121,14 @@ BEGIN GROUP BY bf.id, ei.sheet ) SELECT - MAX(CASE WHEN sh='AHR' AND rn=1 THEN fid END), - MAX(CASE WHEN sh='AHR' AND rn=2 THEN fid END), - MAX(CASE WHEN sh='CAP' AND rn=1 THEN fid END), - MAX(CASE WHEN sh='CAP' AND rn=2 THEN fid END), - MAX(CASE WHEN sh='OPER' AND rn=1 THEN fid END), - MAX(CASE WHEN sh='OPER' AND rn=2 THEN fid END) - INTO v_ahr_s, v_ahr_d, v_cap_s, v_cap_d, v_oper_s, v_oper_d + MAX(CASE WHEN sh='AHR' THEN fid END), + MAX(CASE WHEN sh='CAP' THEN fid END), + MAX(CASE WHEN sh='OPER' THEN fid END) + INTO v_ahr, v_cap, v_oper FROM ranked; RETURN QUERY WITH - -- 5 источников: row_type, depth, section_code, name + 22 метрик из sections ahr_s AS ( SELECT col_C_section AS sc, col_F_name AS nm, depth, row_type, col_H_plan_q1 AS pq1, col_I_plan_q2 AS pq2, col_J_plan_q3 AS pq3, col_K_plan_q4 AS pq4, col_L_plan_year AS pyr, @@ -4128,7 +4136,7 @@ BEGIN col_CS_q1_actual_quarter AS fq1, col_EB_q2_actual_quarter AS fq2, col_FJ_q3_actual_quarter AS fq3, COALESCE(col_GQ_q4_actual_quarter,0) + COALESCE(col_GR_q4_actual_spod,0) AS fq4, col_DH_q2_new_plan AS np2, col_EP_q3_new_plan AS np3, col_FW_q4_new_plan AS np4 - FROM v_form1_sheet_sections(v_ahr_s, 'AHR', 'Support', v_sections) + FROM v_form1_sheet_sections(v_ahr, 'AHR', 'Support', v_sections) WHERE row_type IN ('ROOT','GROUP','ITEM') ), ahr_d AS ( @@ -4138,7 +4146,7 @@ BEGIN col_CS_q1_actual_quarter AS fq1, col_EB_q2_actual_quarter AS fq2, col_FJ_q3_actual_quarter AS fq3, COALESCE(col_GQ_q4_actual_quarter,0) + COALESCE(col_GR_q4_actual_spod,0) AS fq4, col_DH_q2_new_plan AS np2, col_EP_q3_new_plan AS np3, col_FW_q4_new_plan AS np4 - FROM v_form1_sheet_sections(v_ahr_d, 'AHR', 'Development', v_sections) + FROM v_form1_sheet_sections(v_ahr, 'AHR', 'Development', v_sections) WHERE row_type IN ('ROOT','GROUP','ITEM') ), cap_s AS ( @@ -4148,7 +4156,7 @@ BEGIN col_CS_q1_actual_quarter AS fq1, col_EB_q2_actual_quarter AS fq2, col_FJ_q3_actual_quarter AS fq3, COALESCE(col_GQ_q4_actual_quarter,0) + COALESCE(col_GR_q4_actual_spod,0) AS fq4, col_DH_q2_new_plan AS np2, col_EP_q3_new_plan AS np3, col_FW_q4_new_plan AS np4 - FROM v_form1_sheet_sections(v_cap_s, 'CAP', 'Support', v_sections) + FROM v_form1_sheet_sections(v_cap, 'CAP', 'Support', v_sections) WHERE row_type IN ('ROOT','GROUP','ITEM') ), cap_d AS ( @@ -4158,7 +4166,7 @@ BEGIN col_CS_q1_actual_quarter AS fq1, col_EB_q2_actual_quarter AS fq2, col_FJ_q3_actual_quarter AS fq3, COALESCE(col_GQ_q4_actual_quarter,0) + COALESCE(col_GR_q4_actual_spod,0) AS fq4, col_DH_q2_new_plan AS np2, col_EP_q3_new_plan AS np3, col_FW_q4_new_plan AS np4 - FROM v_form1_sheet_sections(v_cap_d, 'CAP', 'Development', v_sections) + FROM v_form1_sheet_sections(v_cap, 'CAP', 'Development', v_sections) WHERE row_type IN ('ROOT','GROUP','ITEM') ), oper_s AS ( @@ -4168,12 +4176,10 @@ BEGIN col_CS_q1_actual_quarter AS fq1, col_EB_q2_actual_quarter AS fq2, col_FJ_q3_actual_quarter AS fq3, COALESCE(col_GQ_q4_actual_quarter,0) + COALESCE(col_GR_q4_actual_spod,0) AS fq4, col_DH_q2_new_plan AS np2, col_EP_q3_new_plan AS np3, col_FW_q4_new_plan AS np4 - FROM v_form1_sheet_sections(v_oper_s, 'OPER', 'Support', v_sections) + FROM v_form1_sheet_sections(v_oper, 'OPER', 'Support', v_sections) WHERE row_type IN ('ROOT','GROUP','ITEM') ), - -- Пивот по section_code merged AS ( - -- AHR блок SELECT COALESCE(s.row_type, d.row_type) AS row_type, COALESCE(s.depth, d.depth) AS depth, COALESCE(s.sc, d.sc) AS sc, @@ -4188,7 +4194,6 @@ BEGIN d.np2 AS d_np2, d.np3 AS d_np3, d.np4 AS d_np4 FROM ahr_s s FULL OUTER JOIN ahr_d d ON s.sc = d.sc UNION ALL - -- CAP блок SELECT COALESCE(s.row_type, d.row_type), COALESCE(s.depth, d.depth), COALESCE(s.sc, d.sc), COALESCE(s.nm, d.nm), s.pq1, s.pq2, s.pq3, s.pq4, s.pyr, @@ -4201,7 +4206,6 @@ BEGIN d.np2, d.np3, d.np4 FROM cap_s s FULL OUTER JOIN cap_d d ON s.sc = d.sc UNION ALL - -- OPER блок (только Support) SELECT s.row_type, s.depth, s.sc, s.nm, s.pq1, s.pq2, s.pq3, s.pq4, s.pyr, NULL::NUMERIC, NULL::NUMERIC, NULL::NUMERIC, NULL::NUMERIC, NULL::NUMERIC, @@ -4230,8 +4234,7 @@ BEGIN FROM merged m ORDER BY m.sc; END; -$function$ -; +$$ LANGUAGE plpgsql STABLE SET search_path = v3, pg_catalog; -- DROP FUNCTION v3.v_form2_sheet_sections(int4, varchar, _text); @@ -4427,7 +4430,7 @@ BEGIN LEFT JOIN v3.sequestration sg ON sg.line_id = bl.id AND sg.actor='SSP_GO' AND s_need_ap LEFT JOIN v3.reserve r ON r.line_id = bl.id AND s_need_ap LEFT JOIN v3.ckk ck ON ck.line_id = bl.id AND s_book - WHERE bl.budget_form_id = p_form_id AND ei.sheet = p_sheet and (p_direction is null OR bl.direction is null or bl.direction = p_direction) + WHERE bl.budget_form_id = p_form_id AND ei.sheet = p_sheet AND (s_need_ap OR s_book) GROUP BY bl.expense_item_id ), @@ -4453,7 +4456,7 @@ BEGIN FROM v3.budget_line bl JOIN v3.expense_item ei ON ei.id = bl.expense_item_id JOIN v3.budget_line_quarter q ON q.line_id = bl.id - WHERE bl.budget_form_id = p_form_id AND ei.sheet = p_sheet and (p_direction is null OR bl.direction is null or bl.direction = p_direction) + WHERE bl.budget_form_id = p_form_id AND ei.sheet = p_sheet GROUP BY bl.expense_item_id, q.quarter ), -- Tree-rollup