Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. select  pr.id rid, --2388195
  2.     l.id lid,
  3.     pr.import_request_uuid,
  4.     c.full_name      as orgname,
  5.     c.id             as orgid,
  6.     pp.upo_id as org_uo_id,
  7.     gcc.code         as orgcode,
  8.     c_grbs.full_name as grbsname,
  9.     c_grbs.id        as grbsid,
  10.     coalesce(pr.registry_number,join_lots.registry_number) reqnum,    
  11.         pp.order_number plannum,
  12.     substring(pr.title, 0, 2000) reqsubject,
  13.     case when pp_spec.detail_parent_id is not null then 'Особая закупка' when nkp.naumen_name is null then vpt.name else nkp.naumen_name end ot_naumen_name,
  14.     case when pp_spec.detail_parent_id is not null then 101  when nkp.naumen_code is null then vpt.id+100 else nkp.naumen_code end ot_naumen_code,
  15.     plc.name oneex_reason,
  16.     pr.date_added,
  17.     pr.date_published date_published,
  18.     pp.registration_start_date date_demand_start,
  19.     l.date_placed,
  20.     l.date_fulfilled,
  21.     first_notice_try.date first_something,
  22.     nks.naumen_name,
  23.     nks.naumen_id,
  24.     vps.full_name lotstage,
  25.     vps.id lotstageid,
  26.     case when pr.oos_publish_status != 2 and nks.kgntv_name =
  27.         'Ожидание публикации' then 'request' else nks.entity end entity,
  28.     plptp.name changereason,
  29.     plppg.short_name changereason_pg,
  30.     plppz.short_name changereason_pz,
  31.         (CASE WHEN pp.final_decision = 1 then 'Отмена закупки'
  32.         WHEN pp.final_decision = 2 then 'Изменение закупки'
  33.         else '' end) final_decision,
  34.     case when vps.id = 93             then 1           else 0               end ignored,
  35.     case when f_pvtp.name is not null then f_pvtp.name else first_fin.year_ end
  36.     f_year,
  37.     first_fin.year_ first_fin_year,
  38.     l.number lotnum,
  39.     l.plan_position_number lotoosnum,
  40.     substring(l.subject, 0, 2000) lotsubject,
  41.     case when pp.detail_parent_id is not null then l.scheduled_price_total else l.start_price end /* l.start_price*/ start_price,
  42.     case when pp.detail_parent_id is not null then l.scheduled_price_total
  43.         --when  pr.po_schedule_position_id is not null  and sch20.id is not null then l.final_price
  44.         else l.start_price end /* l.start_price*/ final_price,
  45.         --l.final_price,
  46.     vpk."name" production_type,
  47.     ve.description etp,
  48.     substr(pl.min_reqs::text, 0, 2000) min_reqs,
  49.     substring(pl.supply_place, 0, 2000) supply_place,
  50.     pl.advance_pay_amount,
  51.     l.guarantee_contract con_cover,
  52.     case when pl.schedule_id is null then 0 else 1 end planexist,
  53.     coalesce(GREATEST(preferences.smp,participants.smp,restriction.smp), 0) smp,
  54.     coalesce(GREATEST(preferences.forsmp,participants.forsmp,restriction.forsmp), 0) forsmp,
  55.     coalesce(preferences.invalid, 0) invalid,
  56.     coalesce(preferences.criminal, 0) criminal,
  57.     coalesce(preferences.ceoregion, 0) ceoregion,  
  58.  
  59.     coalesce(preferences.smp, 0) smp_preference,
  60.     coalesce(preferences.forsmp, 0) forsmp_preference,
  61.     coalesce(preferences.invalid, 0) invalid_preference,
  62.     coalesce(preferences.criminal, 0) criminal_preference,
  63.     coalesce(preferences.ceoregion, 0) ceoregion_preference,
  64.     coalesce(participants.smp, 0) smp_participant,
  65.     coalesce(participants.forsmp, 0) forsmp_participant,
  66.     coalesce(participants.invalid, 0) invalid_participant,
  67.     coalesce(participants.criminal, 0) criminal_participant,
  68.     coalesce(participants.ceoregion, 0) ceoregion_participant,
  69.     coalesce(restriction.smp, 0) smp_restriction,
  70.     coalesce(restriction.forsmp, 0) forsmp_restriction,
  71.     substr(pl.restriction_fs, 0, 1000) restriction_fs , --ограничение участия в определении поставщика(п4 ст42)
  72.  
  73.     case when (pp.basis_for_single_supplier = 100004 or
  74.         pp.basis_for_single_supplier = 100005 or (pp.basis_for_single_supplier =
  75.         8361009 and l.plan_position_number is null) or pp.basis_for_single_supplier
  76.         = 8361012 or pp.basis_for_single_supplier = 8724068 or
  77.         pp.basis_for_single_supplier = 8395978 or pp.basis_for_single_supplier =
  78.         8724074 or (pp.basis_for_single_supplier = 8724059 and
  79.         l.plan_position_number is null)) then 1 else 0 end ispublic,
  80.     case when l.start_price >= 40000000 or ( l.start_price < 40000000 and l.start_price >= 3000000 and lower(c_grbs.full_name)::text ~~* 'администрация% района%'::text and lower(c.full_name)::text !~~* 'администрация% района%'::text) then 1 else 0 end uoplacing,
  81.     case when l.start_price >= 40000000 AND pr.procedure_type NOT IN (6, 10, 14, 15) then 'Подлежит размещению в УпО' when (l.start_price < 40000000 AND l.start_price >= 3000000 AND vc.org_is_placement_grbs_possible = true AND pr.procedure_type NOT IN (6, 10, 14, 15)) then 'Централизованная закупка' else 'Размещается Заказчиком' end placement,
  82. /*    case when pl.consolidate_lot_id is not null or pp.joint_procedure_id is not null then 1 else 0 end can_be_joint, изменение от 2016-11-15*/
  83.     case when pl.consolidate_lot_id is not null or pp.joint_procedure_id is not null or pp.can_be_joint = true then 1 else 0 end can_be_joint,
  84.     coalesce(pl.consolidate_lot_id, join_lots.lot_id)   joint_procedure_id,
  85.     cc.contract_amount,
  86.     case when pp.detail_parent_id is not null then l.scheduled_price_total else l.start_price end /* l.start_price*/ - cc.contract_amount eco,
  87.     pp.request_order_number, pp.sed_registration_number,pp.sed_registration_date ,
  88.     l.date_end_registration,
  89.     l.date_end_first_parts_review,
  90.     COALESCE(l.date_end_second_parts_review, date_auction.date_end_second_parts_review) date_end_second_parts_review,
  91.     pl.goods_ban,
  92.     pl.public_discussion_num,
  93.     pl.purchase_code ikz,
  94.     coalesce(pl.schedule_id,l_big.schedule_id) schedule_id, -- ID Плана графика
  95.     coalesce(pl.purchase_code,l_big.pg_ikz) pg_ikz,    --  ИКЗ Плана-графика
  96.     pl.purchase_code_in_plan pz_ikz,    --  ИКЗ Плана закупок
  97.     coalesce(l.plan_position_number,l_big.pg_rn) pg_rn, -- РН позиции Плана-графика
  98.     '' pz_rn,
  99.     coalesce(SCH.plan_number,l_big.pg_n) pg_n, -- РН Плана-графика
  100. --    '' pz_n,  -- РН Плана закупок
  101.  
  102.     pl.contract_fulfill_date,  --Срок исполнения контракта
  103.     plsp.special_type,--Тип особых закупок
  104.     plsp.special_type_pz,--наименование типа для планов закупок
  105.     plsp.special_type_pg,--наименование типа для планов-графиков
  106.  
  107.     pr.date_notice_planned_published,--Планируемая дата публикации извещения
  108.     pr.date_notice_published, --Фактическая дата публикации извещения
  109.     pr.date_notice_planned_change, --Планируемая дата публикации изменения в извещение
  110.     pr.date_notice_change, --Фактическая дата публикации изменения в извещение
  111.         CASE WHEN pr.send_to_oos = true then 1 else 0 end send_to_oos ,--Флаг отправки процедуры на ЕИС
  112.     pl.advance_hand_amount,
  113.     substring(pr.basis_proc_type, 0, 500) basis_proc_type, --Обоснование выбранного способа определения поставщика (подрядчика, исполнителя)
  114.     substring(pvbfq.name,0,500)  basis_for_quotation_req --Основание для для проведения запроса предложений
  115.     ,
  116.         Case when pp.type_spec_purch is null then fin.fin_position_all Else fin_detailed.fin_position_all end fin_position_all, -- Финансирование из ПГ (общая сумма по позиции)
  117.         Case when pp.type_spec_purch is null then fin.fin_position_cur Else fin_detailed_2020.fin_position_cur end fin_position_cur,-- Финансирование из ПГ по 2020 году (сумма по позиции)      
  118.    
  119.     case when pr.po_schedule_position_id is null then 'M' else 'D' end MorD_flag --Флаг закупка-"Мать" или закупка-"Дочка"
  120.   from procedures pr
  121.   join po_procedures pp on pp.procedure_id = pr.id
  122.   left join po_list_contract_single_supplier_reasons plc  on plc.code = pp.basis_for_single_supplier
  123.   left join po_list_plan_change_reasons plptp on plptp.code = pp.basis_for_change
  124.  
  125.   left join po_list_tend_plan_2017_change_reasons plppg on plppg.code = pp.basis_schedule_plan
  126.   left join po_list_plan_position_change_reasons plppz  on plppz.code = pp.basis_plan
  127.   left join
  128.     (select code special_type, max(tend_full_name) special_type_pg, max(prch_full_name) special_type_pz
  129.       from po_list_special_purchases
  130.       where actual = true
  131.       group by code
  132.     )plsp on plsp.special_type = pp.type_spec_purch
  133.   left join po_vocab_time_periods f_pvtp  on f_pvtp.id = pp.finance_time_period_id
  134.   left join po_vocab_time_periods pvtp  on pvtp.id = pp.time_period_id
  135.   left join vocab_procedure_types vpt on vpt.id = pr.procedure_type
  136.   left join site.naumen_kgntv_placingtypes nkp  on nkp.kgntv_id = vpt.id
  137.   left join lots l  on l.procedure_id = pr.id    
  138.   left join lot_customers lc  on lc.lot_id = l.id and lc.actual = true
  139.   left join contragents c on c.id = lc.customer_id  --on c.id = pr.organizer_contragent_id
  140.  
  141.   left join view_contragents vc on c.id = vc.id
  142.   left join gpospb_contragents gcc  on gcc.contragent_id = c.id
  143.   left join
  144.     (select  lc.lot_id
  145.       from lot_customers lc
  146.       where lc.actual = true
  147.       group by lc."lot_id"
  148.       having count( * ) > 1
  149.     ) cons_lot  on cons_lot.lot_id = l.id
  150.   left join
  151.     (select procedure_id, min(case when step_id = 'electronic_auction' then date_start else null end) date_end_second_parts_review
  152.       from procedure_steps group by procedure_id
  153.     ) date_auction on date_auction.procedure_id = pr.id
  154.   left join procedure_steps ps  on ps.id = l.current_step --and ps.actual = true
  155.   left join vocab_procedure_steps vps on vps.pseudo::text = ps.step_id::text --and vps.actual = true
  156.   left join site.naumen_kgntv_stages nks  on nks.kgntv_id = vps.id
  157.   left join po_list_org_hierarchy ploh  on ploh.contragent_id = c.id and ploh.actual = true and   ploh.parent_type = 1
  158.   left join contragents c_grbs  on c_grbs.id = ploh.parent_contragent_id
  159.   left join po_lots pl  on pl.lot_id = l.id
  160.   left join
  161.     (select  cc.lot_id,
  162.         max(cc.contract_amount) contract_amount
  163.       from cm_contracts cc
  164.       group by cc.lot_id
  165.     ) cc  on l.id = cc.lot_id
  166.   left join
  167.     (select
  168.         pol.lot_id,
  169.         l.procedure_id,
  170.         p.registry_number registry_number
  171.       from po_lots pol
  172.       inner join lots l on l."id" = pol.consolidate_lot_id
  173.       inner join procedures p on p."id" = l.procedure_id
  174.       where pol.consolidate_lot_id is not null
  175.     ) join_lots on join_lots.lot_id = l.id
  176.   left join
  177.     (select  l.id lot_id,
  178.         min(pvtp.name) year_
  179.       from lots l
  180.       inner join po_finances pf     on pf.lot_id = l.id
  181.       inner join po_account pa      on pa.id = pf.po_account_id
  182.       inner join po_budget pb     on pb.id = pa.po_budget_id
  183.       inner join po_vocab_time_periods pvtp     on pvtp.id = pb.po_period_id
  184.       group by l.id
  185.     ) first_fin on first_fin.lot_id = l.id
  186.   left join
  187.     (select  pp.lot_id,
  188.         max(case when (plpp.short_name)::text in('MP44', 'MP') then 1 else 0 end) smp,
  189.         max(case when (plpp.short_name)::text in('MPSP44') then 1 else 0 end) forsmp,
  190.         max(case when (plpp.short_name)::text in('IN44') then 1 else 0 end) invalid,
  191.         max(case when (plpp.short_name)::text in('UG44') then 1 else 0 end) criminal,
  192.         max(case when (plpp.short_name)::text in('RM', 'RBK44') then 1 else 0 end)ceoregion
  193.       from po_list_purchase_preferences plpp
  194.       join po_preferences pp  on pp.preference_id = plpp.id    
  195.       where plpp.actual = true or (plpp.short_name)::text in('MP44', 'MP')
  196.       group by pp.lot_id
  197.     ) preferences on preferences.lot_id = l.id
  198.   left join
  199.     (select  pp.lot_id,
  200.         max(case when (plpp.short_name)::text in('MP44', 'MP','MB44330') then 1 else 0 end) smp,
  201.         max(case when (plpp.short_name)::text in('MPSP44','TS44530'/*,'NOSP44'*/) then 1 else 0 end) forsmp,
  202.         max(case when (plpp.short_name)::text in('IN44') then 1 else 0 end) invalid,
  203.         max(case when (plpp.short_name)::text in('UG44') then 1 else 0 end) criminal,
  204.         max(case when (plpp.short_name)::text in('RM', 'RBK44') then 1 else 0 end)ceoregion
  205.       from po_list_purchase_preferences plpp
  206.       join po_participant_requirements pp on pp.requirement_id = plpp.id      
  207.       --where plpp.actual = true or (plpp.short_name)::text in('MP44', 'MP')
  208.       group by pp.lot_id
  209.     ) participants on participants.lot_id = l.id
  210.  
  211.   left join
  212.     (select  pp.lot_id,
  213.         max(case when (plpp.short_name)::text in('MP44', 'MP','MB44330') then 1 else 0 end) smp,
  214.         max(case when (plpp.short_name)::text in('MPSP44','TS44530'/*,'NOSP44'*/) then 1 else 0 end) forsmp,
  215.         max(case when (plpp.short_name)::text in('IN44') then 1 else 0 end) invalid,
  216.         max(case when (plpp.short_name)::text in('UG44') then 1 else 0 end) criminal,
  217.         max(case when (plpp.short_name)::text in('RM', 'RBK44') then 1 else 0 end) ceoregion
  218.       from po_list_purchase_preferences plpp
  219.       join po_restrictions pp on pp.restriction_id = plpp.id and pp.actual is true
  220.       --where plpp.actual = true or (plpp.short_name)::text in('MP44', 'MP')
  221.       group by pp.lot_id
  222.     ) restriction on restriction.lot_id = l.id
  223.  
  224.   left join vocab_procedure_kind vpk  on vpk."id" = pp.production_type
  225.   left join vocab_etplist ve  on ve.id::text = pp.etp_place
  226.   left join
  227.     (select min(last_attempt) date, procedure_id
  228.         from oos_messages
  229.         where "type" like 'notification%'
  230.         group by procedure_id
  231.     )first_notice_try on first_notice_try.procedure_id = pr.id
  232.   left join
  233.     (select id, plan_number
  234.         from po_schedules
  235.     )sch on sch.id = pl.schedule_id    
  236.    
  237.   left join (select distinct detail_parent_id from po_procedures pp) pp_spec on pp_spec.detail_parent_id = pr.id
  238.   left join po_vocab_bases_for_quotation pvbfq  on pvbfq.id = pp.basis_for_quotation_req
  239.   left Join (
  240.         select      
  241.               p.id,
  242.               sum(f.amount) AS fin_position_all,
  243.               -- sum( case when f.year=11/*11=2020*/ then f.amount else null end) fin_position_2020
  244.                             sum( case when t.code='CUR' then f.amount else null end) fin_position_cur
  245.         from procedures p
  246.         join po_schedule_2020_position sp ON sp.procedure_id = p.id or sp.id = p.po_schedule_position_id
  247.         join po_schedules_2020 s ON s.id = sp.schedule_id
  248.         join po_schedule_2020_position_finances f ON sp.id = f.po_schedule_position_id
  249.         join po_vocab_time_periods t ON t.id = f.year AND f.actual is true
  250.              group by sp.schedule_id, p.id
  251.         ) fin on fin.id = pr.id
  252.    Left Join (
  253.                             select  s.schedule_number,                                         
  254.                                             pp.type_spec_purch,                        
  255.                                             sum(f.amount) AS fin_position_all,
  256.                                             -- sum( case when f.year=11/*11=2020*/ then f.amount else null end) fin_position_2020
  257.                                             sum( case when t.code='CUR' then f.amount else null end) fin_position_cur
  258.                             from procedures p
  259.                             join po_procedures pp   on pp.procedure_id = p.id
  260.                             join po_schedule_2020_position sp ON sp.procedure_id = p.id --or sp.id = p.po_schedule_position_id
  261.                             join po_schedules_2020 s ON s.id = sp.schedule_id
  262.                             join po_schedule_2020_position_finances f ON sp.id = f.po_schedule_position_id
  263.                             join po_vocab_time_periods t ON t.id = f.year AND f.actual is true
  264.                             where sp.number_reg is not null and pp.type_spec_purch is not null
  265.                             --order by pp.type_spec_purch,f.amount desc
  266.                             group by s.schedule_number,sp.schedule_id, pp.type_spec_purch--p.id,
  267.                          ) fin_detailed on fin_detailed.schedule_number = SCH.plan_number
  268.                                                         and fin_detailed.type_spec_purch=pp.type_spec_purch
  269.      Left join (
  270.                             select  s.schedule_number,         
  271.                                             p.id,
  272.                                             --sum( case when f.year=11/*11=2020*/ then f.amount else null end) fin_position_2020
  273.                                             sum( case when t.code='CUR' then f.amount else null end) fin_position_cur
  274.                             from procedures p
  275.                             join po_procedures pp   on pp.procedure_id = p.id
  276.                             join po_schedule_2020_position sp ON sp.procedure_id = p.id or sp.id = p.po_schedule_position_id
  277.                             join po_schedules_2020 s ON s.id = sp.schedule_id
  278.                             join po_schedule_2020_position_finances f ON sp.id = f.po_schedule_position_id
  279.                             join po_vocab_time_periods t ON t.id = f.year AND f.actual is true
  280.                             where sp.number_reg is not null and pp.type_spec_purch is not null
  281.  
  282.                             group by s.schedule_number,sp.schedule_id,p.id
  283.                          ) fin_detailed_2020 on fin_detailed_2020.id = pr.id
  284.         left join (
  285.                                 select  l.id,
  286.                                                 pl_big.schedule_id,
  287.                                                 sch.plan_id,SCH.plan_number pg_n, -- РН Плана-графика
  288.                         (po_plan.plan_number::numeric) pz_n,
  289.                         pl_big.purchase_code pg_ikz,    --  ИКЗ Плана-графика
  290.                         pl_big.purchase_code_in_plan pz_ikz,    --  ИКЗ Плана закупок
  291.                         l_big.plan_position_number pg_rn -- РН позиции Плана-графика
  292.                                                 ,pp.detail_parent_id procedure_id_parent
  293. FROM contragents c
  294.                 JOIN po_list_org_hierarchy ploh on ploh.contragent_id = c.id and ploh.actual = true and         ploh.parent_type = 1
  295.                 JOIN po_plan on c.id = po_plan.customer_org_id  --
  296.                 JOIN po_schedules sch on po_plan.id = sch.plan_id--
  297.                 JOIN po_lots pl_big     on sch.id = pl_big.schedule_id  --
  298.                 JOIN lots l_big on pl_big.lot_id = l_big.id
  299.                 JOIN po_procedures pp on l_big.procedure_id = pp.detail_parent_id
  300.                 JOIN procedures pr on pp.procedure_id = pr.id
  301.                 JOIN lots l     on l.procedure_id = pr.id
  302.                                 ) l_big on l_big.id=l.id
  303.        
  304.   where /*pr.id='2419059' and */-- sch20.schedule_number='202003722001071001' and
  305.  l.id is not null and cons_lot.lot_id is null and pp.joint_procedure_id is null --and c.actual = true
  306. and vps.actual = true
  307. and pr.id not in (Select p.id from procedures p
  308.                                     join po_schedule_2020_position pos on (p.id=pos.procedure_id and p.po_schedule_position_id is null ) or p.po_schedule_position_id=pos.id
  309.                                     )
  310.                                     --and l.id='2621429'-- '2641226'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement