Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH cte_granted_geo_tags AS (
  2.     SELECT DISTINCT g.id
  3.     FROM geo_tags g
  4.         JOIN geo_tags gp ON g.lft BETWEEN gp.lft AND gp.rgt
  5.     WHERE gp.id  = ANY(ARRAY[:grantedGeoTagIds]::INT[])
  6. ), cte_requests AS (
  7.     SELECT
  8.         fr.id                                                     AS request_id,
  9.         stage.name::INT                                           AS stage,
  10.         fr.registration_date                                      AS registration_date,
  11.         concat_ws(' ',
  12.             'от "' || extract(DAY FROM fr_first.registration_date) :: INT || '"' ||
  13.             ' ' || initcap(get_month_name((extract(MONTH FROM fr_first.registration_date) :: INT), 'genitive')) ||
  14.             ' ' || extract(YEAR FROM fr_first.registration_date) || ' года',
  15.             '№' || fr_first.registration_number
  16.         )                                                         AS requisites
  17.     FROM fin_requests fr
  18.         JOIN refs stage ON stage.id = fr.stage_id
  19.         LEFT JOIN fin_requests fr_first ON fr_first.id = get_fin_request_branch_id(fr.id)
  20.     WHERE fr.id = ANY (ARRAY[:requestIds] :: INT[])
  21. ), cte_provided AS (
  22.     SELECT
  23.         r.request_id,
  24.         sum(frfmi.reloc_sq_total)           AS mo_reloc_sq_total,
  25.         sum(frfmi.reloc_quarters_total)     AS mo_reloc_quarters_total,
  26.         sum(frfmi.reloc_residents_total)    AS mo_reloc_residents_total,
  27.         sum(frfmi.total_financing)          AS mo_total_financing,
  28.         sum(frfmi.fond_financing)           AS mo_fond_financing,
  29.         sum(frfmi.estimated_savings_budget) AS mo_estimated_savings_budget,
  30.         sum(frfmi.compensation_total)       AS mo_compensation_total
  31.     FROM cte_requests r
  32.         JOIN fin_requests_for_municipal_organizations frfmo ON frfmo.financing_request_id = r.request_id AND frfmo.deleted_at IS NULL
  33.         JOIN fin_requests_for_mo_info frfmi ON frfmi.municipal_organization_id = frfmo.id
  34.     WHERE exists(SELECT 1 FROM cte_granted_geo_tags where id = frfmo.geo_tag_id)
  35.     GROUP BY r.request_id
  36. ), cte_implemented AS (
  37.     WITH cte_contracts AS (
  38.         SELECT
  39.             nrc.id                                                    AS id,
  40.             r.request_id                                              AS request_id,
  41.             nrc.total_amount_of_funds                                 AS total_amount_of_funds,
  42.             nrc.total_amount_of_funds_subject                         AS total_amount_of_funds_subject,
  43.             nrc.total_amount_of_funds_mo                              AS total_amount_of_funds_mo,
  44.             nrc.total_amount_of_extrabudgetary_funds                  AS total_amount_of_extrabudgetary_funds,
  45.             nrcq.house_quarter_id                                     AS quarters_id,
  46.             nrcq.house_resettlement_id                                AS house_resettlement_id,
  47.             nrc.is_acquired                                           AS is_acquired
  48.         FROM cte_requests r
  49.             JOIN new_reloc_contracts_quarters nrcq ON nrcq.fin_request_id = r.request_id
  50.             JOIN new_reloc_contracts nrc ON nrcq.contract_id = nrc.id AND nrc.deleted_at IS NULL
  51.             JOIN statuses contractStatus ON contractStatus.id = nrc.status_id
  52.         WHERE contractStatus.code NOT IN (:statusDraft, :statusTerminated, :statusinArchive)
  53.             AND exists(SELECT 1 FROM cte_granted_geo_tags where id = nrc.mo_geo_tag_id)
  54.     ), cte_contracts_without_quarters AS (
  55.         SELECT
  56.             r.request_id                                  AS request_id,
  57.             sum(nrc.quarters_square)                      AS quarters_square,
  58.             sum(nrc.quarters_amount)                      AS quarters_amount,
  59.             sum(rgkh_summ_nullable(
  60.                 nrc.total_amount_of_funds,
  61.                 nrc.total_amount_of_funds_subject,
  62.                 nrc.total_amount_of_funds_mo
  63.             ))                                            AS total_relocatoion_cost,
  64.             sum(nrc.total_amount_of_funds)                AS fund_cost,
  65.             sum(nrc.total_amount_of_extrabudgetary_funds) AS extrabudgetary_cost
  66.         FROM cte_requests r
  67.             JOIN new_reloc_contracts nrc ON nrc.fin_request_id = r.request_id AND nrc.deleted_at IS NULL
  68.             JOIN statuses contractStatus ON contractStatus.id = nrc.status_id
  69.         WHERE contractStatus.code NOT IN (:statusDraft, :statusTerminated, :statusinArchive)
  70.             AND exists(SELECT 1 FROM cte_granted_geo_tags where id = nrc.mo_geo_tag_id)
  71.         GROUP BY  r.request_id
  72.     ), cte_quarters AS (
  73.         SELECT
  74.             ccd.request_id,
  75.             alarm_hq.square                            AS hq_suare,
  76.             alarm_hq.residents_count                   AS hq_residents_count,
  77.             alarm_hq.id                                AS hq_id,
  78.             CASE WHEN NOT ccd.is_acquired
  79.                 THEN rgkh_summ_nullable(
  80.                     ccd.total_amount_of_funds,
  81.                     ccd.total_amount_of_funds_subject,
  82.                     ccd.total_amount_of_funds_mo
  83.                 )
  84.                 ELSE rgkh_summ_nullable(
  85.                     building_hs.amount_of_funds,
  86.                     building_hs.amount_of_funds_subject,
  87.                     building_hs.amount_of_funds_mo
  88.                 )
  89.             END                                         AS total_relocatoion_cost,
  90.             CASE WHEN NOT ccd.is_acquired
  91.                 THEN ccd.total_amount_of_funds
  92.                 ELSE building_hs.amount_of_funds
  93.             END                                         AS fund_cost,
  94.             CASE WHEN NOT ccd.is_acquired
  95.                 THEN ccd.total_amount_of_extrabudgetary_funds
  96.                 ELSE building_hs.amount_of_extrabudgetary_funds
  97.             END                                         AS extrabudgetary_cost,
  98.             building_hs.compensation_total              AS compensation_total
  99.         FROM cte_contracts ccd
  100.             JOIN house_resettlements alarm_hs ON ccd.house_resettlement_id = alarm_hs.id AND alarm_hs.deleted_at IS NULL
  101.             JOIN house_quarters alarm_hq ON alarm_hq.id = alarm_hs.quarters_id AND alarm_hq.deleted_at IS NULL
  102.             JOIN refs resettlementStatus ON resettlementStatus.id = alarm_hq.resettlement_status_id
  103.             LEFT JOIN house_resettlements building_hs ON ccd.quarters_id = building_hs.quarters_id AND building_hs.deleted_at IS NULL
  104.         WHERE resettlementStatus.code = :statusResettled
  105.            OR exists(SELECT 1 FROM house_quarter_lawsuits WHERE quarters_id = alarm_hq.id AND lawsuit_date_end IS NULL)
  106.     ), cte_prepared_data AS (
  107.         SELECT
  108.             request_id                  AS request_id,
  109.             sum(hq_suare)               AS hq_suare,
  110.             sum(hq_residents_count)     AS hq_residents_count,
  111.             count(hq_id)                AS hq_count,
  112.             sum(total_relocatoion_cost) AS total_relocatoion_cost,
  113.             sum(fund_cost)              AS fund_cost,
  114.             sum(extrabudgetary_cost)    AS extrabudgetary_cost,
  115.             sum(compensation_total)     AS compensation_total
  116.         FROM cte_quarters
  117.         GROUP BY request_id
  118.     )
  119.     SELECT
  120.         request_id,
  121.         rgkh_summ_nullable(
  122.             hq_suare,
  123.             quarters_square
  124.         )                       AS hq_suare,
  125.         rgkh_summ_nullable(
  126.             hq_count,
  127.             quarters_amount
  128.         )                       AS hq_count,
  129.         hq_residents_count      AS hq_residents_count,
  130.         rgkh_summ_nullable(
  131.             pd.total_relocatoion_cost,
  132.             ccwq.total_relocatoion_cost
  133.         )                       AS total_relocatoion_cost,
  134.         rgkh_summ_nullable(
  135.             pd.fund_cost,
  136.             ccwq.fund_cost
  137.         )                       AS fund_cost,
  138.         rgkh_summ_nullable(
  139.             pd.extrabudgetary_cost,
  140.             ccwq.extrabudgetary_cost
  141.         )                       AS extrabudgetary_cost,
  142.         compensation_total      AS compensation_total
  143.     FROM cte_prepared_data pd
  144.         LEFT JOIN cte_contracts_without_quarters ccwq USING (request_id)
  145. )
  146. SELECT
  147.     1                                                     AS total,
  148.     row_number() OVER (ORDER BY stage, registration_date) AS row,
  149.     r.request_id,
  150.     r.stage,
  151.     r.requisites,
  152.     coalesce(p.mo_reloc_sq_total, 0)           AS mo_reloc_sq_total,
  153.     coalesce(p.mo_reloc_quarters_total, 0)     AS mo_reloc_quarters_total,
  154.     coalesce(p.mo_reloc_residents_total, 0)    AS mo_reloc_residents_total,
  155.     coalesce(p.mo_total_financing, 0)          AS mo_total_financing,
  156.     coalesce(p.mo_fond_financing, 0)           AS mo_fond_financing,
  157.     coalesce(p.mo_estimated_savings_budget, 0) AS mo_estimated_savings_budget,
  158.     coalesce(p.mo_compensation_total, 0)       AS mo_compensation_total,
  159.     coalesce(i.hq_suare, 0)                    AS hq_suare,
  160.     coalesce(i.hq_count, 0)                    AS hq_count,
  161.     coalesce(i.hq_residents_count, 0)          AS hq_residents_count,
  162.     coalesce(i.total_relocatoion_cost, 0)      AS total_relocatoion_cost,
  163.     coalesce(i.fund_cost, 0)                   AS fund_cost,
  164.     coalesce(i.extrabudgetary_cost, 0)         AS extrabudgetary_cost,
  165.     coalesce(i.compensation_total, 0)          AS compensation_total
  166. FROM cte_requests r
  167.     LEFT JOIN cte_provided p USING (request_id)
  168.     LEFT JOIN cte_implemented i USING (request_id)
  169. ORDER BY stage, registration_date
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement