Advertisement
GLASHATAY_007

Untitled

Mar 23rd, 2023
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH expertize AS (
  2. SELECT l.id lid, e.procedure_id,
  3.              MAX(CASE WHEN e.boss_id IS NOT NULL THEN u.last_name|| ' ' ||u.first_name|| ' ' ||u.middle_name ELSE NULL END) boss,
  4.              MAX(CASE WHEN e.boss_id IS NOT NULL THEN e.expertize_date_plan ELSE NULL END) boss_date,
  5.              MAX(CASE WHEN e.department_id = 10564 THEN u.last_name|| ' ' ||u.first_name|| ' ' ||u.middle_name ELSE NULL END) komitet,
  6.              MAX(CASE WHEN e.department_id = 10564 THEN e.expertize_date_plan ELSE NULL END) komitet_date,
  7.              MAX(CASE WHEN e.department_id IN (10565,57725) THEN u.last_name|| ' ' ||u.first_name|| ' ' ||u.middle_name ELSE NULL END) tsmets,
  8.              MAX(CASE WHEN e.department_id IN (10565,57725) THEN e.expertize_date_plan ELSE NULL END) tsmets_date,
  9.              MAX(control_date_End) AS control_date_End,
  10.              MAX(expertize_date_End) AS expertize_date_End,
  11.              MAX(expertize_date_plan) AS expertize_date_plan
  12.   FROM po_procedure_expertize e
  13.     LEFT JOIN lots l ON e.procedure_id = l.procedure_id
  14.   JOIN users u ON COALESCE(e.boss_id,e.user_id) = u.id
  15.      -- where e.procedure_id in (4270356,4182144,4205897)
  16.  GROUP BY e.procedure_id, l.id
  17. HAVING date_part('year', MAX(CASE WHEN e.boss_id IS NOT NULL THEN e.expertize_date_plan ELSE NULL END)) IN (2022, 2023)
  18. ORDER BY procedure_id
  19. ),
  20.  nmck AS (
  21. SELECT l.procedure_id,
  22. SUM(start_price) AS nmсk
  23. FROM procedures pr
  24. JOIN lots l  On l.procedure_id = pr.id
  25. GROUP BY l.procedure_id),
  26. dates as(
  27. SELECT
  28.     pr.id,
  29.     record_id,
  30.      date date_all,
  31.      --request_order_number,
  32.     ROW_NUMBER() OVER (PARTITION BY record_id ORDER BY date)  AS rating,
  33.     Case
  34.         when psfrom.step_id ='edit_request' And psto.step_id='wait_agreement_grbs_request' then 1
  35.         when psfrom.step_id ='wait_agreement_grbs_request' And psto.step_id='edit_request' then 2
  36.         when psfrom.step_id ='wait_agreement_grbs_request' And psto.step_id='wait_pkg_confirm_uo' then 3 else null end
  37.         Alls,
  38.   Case when psfrom.step_id ='edit_request' And psto.step_id='wait_agreement_grbs_request' then 1 else null end sent_to_grbs                
  39.     ,Case when psfrom.step_id ='wait_agreement_grbs_request' And psto.step_id='edit_request' then 2 else null end reject_grbs
  40.     ,Case when psfrom.step_id ='wait_agreement_grbs_request' And psto.step_id='wait_pkg_confirm_uo' then 3 else null end soglas_grbs
  41.         FROM (
  42.         SELECT date,
  43.                      CASE WHEN "from"='NULL' THEN null ELSE "from"::integer END "from",
  44.                      CASE WHEN  "to"='NULL' THEN null ELSE "to"::integer END "to",
  45.                      lh.record_id
  46.         FROM lots_history lh
  47.         WHERE lh.record_id IN (SELECT DISTINCT l.id
  48.             FROM expertize e
  49.             LEFT JOIN lots l ON l.procedure_id=e.procedure_id)
  50.         AND lh.field = 'current_step'
  51.         ORDER BY date
  52.         ) a
  53.         JOIN lots l on l.id = a.record_id
  54.     JOIN procedures pr on l.procedure_id = pr.id
  55.        
  56.         LEFT JOIN procedure_steps psfrom on psfrom.id  = a.from
  57.         LEFT JOIN procedure_steps psto on psto.id  = a.to      
  58.         Where  (psfrom.step_id ='edit_request' And psto.step_id='wait_agreement_grbs_request') OR (psfrom.step_id ='wait_agreement_grbs_request' And psto.step_id='wait_pkg_confirm_uo') OR
  59.         (psfrom.step_id ='wait_agreement_grbs_request' And psto.step_id='edit_request')
  60.         ORDER BY record_id, date)
  61.        
  62. Select
  63.  
  64.     d1.id procedure_id, -- ID_процедуры
  65.     pp.request_order_number, -- Номер заявки на закупку
  66.     vpt.name AS way, -- Способ определения поставщика
  67.     pr.title AS purchase, -- Наименование закупки
  68.   c_grbs.full_name AS grbsname, -- ГРБС
  69.     c.full_name, -- заказчик
  70.   c1.full_name AS customer, -- организатор
  71.     vps.full_name lotstage, -- Статус
  72.     n.nmсk, -- Н(М)ЦК, руб.
  73.     d1.date_all date_sent, -- Дата, время "Отправлено на согласование в ГРБС"
  74.     case when d2.reject_grbs is not null then d2.date_all else null end date_reject, --  Дата, время "Процедура отклонена в ГРБС"
  75.     case when d2.soglas_grbs is not null then d2.date_all else null end date_soglas, --  Дата, время "Процедура отклонена в ГРБС"
  76.     pr.regIstry_number, -- Номер извещени
  77.     cc.contract_rnk-- РНК
  78. From dates d1
  79. join dates d2 on d1.rating+1 =d2.rating and d1.alls=1 and d1.id=d2.id
  80. join procedures pr on d1.id=pr.id
  81. JOIN po_procedures pp ON d1.id = pp.procedure_id
  82. JOIN vocab_procedure_types vpt ON pr.procedure_type = vpt.id
  83. JOIN lots l ON l.procedure_id = d1.id
  84. JOIN lot_customers lc ON lc.lot_id = l.id AND lc.actual = TRUE
  85. JOIN contragents c ON c.id = lc.customer_id
  86. JOIN contragents c1 ON c1.id = pr.organizer_contragent_id
  87. JOIN po_list_org_hierarchy ploh ON ploh.contragent_id = c.id AND ploh.actual = TRUE AND ploh.parent_type = 1
  88. JOIN contragents c_grbs ON c_grbs.id = ploh.parent_contragent_id
  89. JOIN procedure_steps ps ON ps.id = l.current_step
  90. JOIN vocab_procedure_steps vps ON vps.pseudo::text = ps.step_id::text And vps.actual = true
  91. LEFT JOIN nmck n ON n.procedure_id = pr.id
  92. LEFT JOIN cm_contracts cc ON l.id = cc.lot_id
  93. order by d1.id
  94. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement