Advertisement
GLASHATAY_007

Untitled

May 3rd, 2024
76
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. with a as
  2. (
  3. Select distinct
  4. pf.fulfilment_sum,
  5. pf.fulfilment_sum_rur,
  6. pff.acceptance_finance_id
  7. From cm_contract_finances pf
  8. LEFT join cm_contract_acceptance_docs_to_payments pff on pf.id=acceptance_finance_id
  9. --WHERE 24090772
  10. --GROUP BY pff.acceptance_finance_id
  11. ),
  12. pf as(
  13. SELECT
  14. pf.lot_id,
  15. pf.amount,
  16. pf.TYPE,
  17. pf.doc_type,
  18. NULL "YEAR",
  19. pf.po_account_id,
  20. pf.stage_id,
  21. pf.supplier_id,
  22. pf.id,
  23. pf.MONTH,
  24. pf.doc_name,
  25. Substring(pf.doc_number FROM 0 FOR 150) doc_number,
  26. pf.doc_date,
  27. pf.pay_doc_name, -- Вид документа
  28. pf.published::text , -- IS 'Опубликован на ООС'
  29. pf.improper_execution_text, -- 'Информация о ненадлежащем исполнении или неисполнении контракта, в том числе в части оплаты'
  30. pf.fulfilment_sum_rur, -- 'Стоимость исполненных поставщиком обязательств, руб'
  31. pf.delivery_accept_date, -- 'Дата подписания заказчиком документа о приемке ТРУ'
  32. pf.product,
  33. NULL payment_description,
  34. NULL sub_type_code,
  35. NULL description,
  36. NULL kosgu,
  37. NULL cvr,
  38. NULL targetexpenseitemcode,
  39. NULL section_code,
  40. NULL fund_code,
  41. NULL req_code,
  42. NULL aip_code,
  43. NULL inn,
  44. NULL aip_name,
  45. NULL receiver_inn,
  46. NULL cvr_name,
  47. NULL kosgu_name,
  48. NULL code,
  49. NULL kosgu_rs
  50. from
  51. cm_contract_finances pf
  52. WHERE 1=1 And pf.type In (1) and pf.actual is TRUE AND COALESCE(doc_type,'0') != 'PAY'
  53. --and lot_id = '4804034'
  54. --AND pf.lot_id=5031694
  55.  
  56. UNION
  57.  
  58. SELECT
  59. cc.lot_id,
  60. pf.amount,
  61. 2 AS TYPE,
  62. 'PAY' AS doc_type,
  63. pf.YEAR,
  64. NULL po_account_id,
  65. null stage_id,
  66. NULL supplier_id,
  67. NULL id,
  68. NULL "MONTH",
  69. NULL doc_name,
  70. Substring(pf.number FROM 0 FOR 150) doc_number,
  71. pf.DATE::DATE doc_date,
  72. pf.pay_doc_name,
  73. NULL published,
  74. NULL improper_execution_text,
  75. NULL fulfilment_sum_rur,
  76. NULL delivery_accept_date,
  77. NULL product,
  78. SUBSTRING(pf.payment_description,0,100)payment_description,
  79. pf.sub_type_code,
  80. vbc.description,
  81. coalesce(gea.kosgu, pf.kosgu_rs) kosgu,
  82. pf.e_code cvr,
  83. CASE WHEN pf.article_code IS NULL THEN '0000000000'
  84. ELSE pf.article_code end targetexpenseitemcode,
  85. pf.section_code,
  86. pf.fund_code,
  87. pf.req_code,
  88. pf.aip_code,
  89. c.inn,
  90. l_aip."NAME" aip_name,
  91. pf.receiver_inn,
  92. gl.name AS cvr_name,
  93. glk.name AS kosgu_name,
  94. ggc.code,
  95. pf.kosgu_rs
  96. FROM cm_contracts cc
  97. INNER JOIN lots l ON (l.id = cc.lot_id AND l.actual = TRUE)
  98. INNER JOIN cm_contract_payments pf ON (pf.cm_contract_id = cc.id)
  99. left join gpo_exp_account gea ON(pf.kosgu_rs = substring(gea.exp_account, 1, 6) and pf.year::varchar = substring(gea.exp_account, 7, 4) and pf.sub_type_code='BUD' and length(gea.exp_account)=10)
  100. LEFT JOIN po_vocab_budget_types vbc ON (vbc.code=pf.sub_type_code)
  101. LEFT JOIN (SELECT aip_code,max(NAME) "NAME",YEAR from gpo_list_aip GROUP BY aip_code,YEAR)l_aip ON (l_aip.aip_code= pf.aip_code AND l_aip.YEAR=pf.YEAR)
  102. LEFT JOIN contragents c ON (c.id = cc.contract_customer)
  103. LEFT join public.po_list_org_hierarchy ploh on ploh.contragent_id = c.id and ploh.actual = true and c.actual= true and ploh.parent_type = 1
  104. left join public.contragents c_grbs on c_grbs.id = ploh.parent_contragent_id
  105. left join public.gpospb_grbs_codes ggc on ggc.inn = c_grbs.inn
  106. LEFT JOIN gpo_list_expense_type gl ON pf.e_code=gl.e_code
  107. LEFT JOIN gpo_list_kosgu glk ON glk.kosgu_code=pf.kosgu_rs
  108. --where cc.lot_id = '4804034'
  109. --WHERE lot_id=5031694
  110. ORDER BY TYPE,doc_type,"YEAR")
  111.  
  112. Select
  113. cc.id contract,
  114. cc.lot_id,
  115. pf.stage_id,
  116. pf.amount finsum, --сумма финансирования
  117. Case
  118. When glb.code = Cast ('72020112' As text)
  119. Then (coalesce(pf.amount, 0))
  120. Else 0
  121. End bsum, --сумма бюджета
  122. Case
  123. When (glb.code)::text != '72020112'
  124. Then (Coalesce(pf.amount, 0))
  125. Else 0 End obsum, --сумма внебюджета,
  126. glb.name finsource,
  127. pf.month,
  128. COALESCE (pvtp.name,pf."YEAR") as year,
  129. pvtp_a.name year_accept,
  130. pb.id budget_id,
  131. c.inn,
  132. pf.supplier_id,
  133. COALESCE (supp.inn,pf.receiver_inn) supplier_inn,
  134. pf.id,
  135. pf.doc_name,
  136. pf.doc_number,
  137. pf.doc_date,
  138. pf.pay_doc_name, -- Вид документа
  139. pf.type,
  140. a.fulfilment_sum, -- Стоимость исполненных поставщиком (подрядчиком, исполнителем) обязательств в валюте исполнения'
  141. Substring(pf.product From 0 For 2000) product,
  142. COALESCE(dea.kosgu,pf.kosgu_rs) economiccode,
  143. COALESCE(dea.article_code,pf.targetexpenseitemcode) targetexpenseitemcode,
  144. Case
  145. When pea.budget_type = 'BUD'
  146. Then pea.po_exp_code
  147. Else Null
  148. End expensesnumeration,
  149. dea.exp_account,
  150. COALESCE(dea.section_code,pf.section_code) subsection,
  151. COALESCE(dea.e_code,pf.cvr) expensetypecode,
  152. COALESCE(glet.name,pf.cvr_name) expensetypename,
  153. COALESCE(dea.fund_code,pf.fund_code) fund_code,
  154. COALESCE(dea.grbs_code,pf.code) grbs_code,
  155. COALESCE(dea.req_code,pf.req_code) req_code, -- код запроса ( добавлено по просьбе Крицкой Л.А. 14.07.2023)
  156. COALESCE(gsa.kosgu,pf.kosgu, dea.kosgu) kosgucode,
  157. COALESCE(glk.name,kosgu_name) kosgutitle,
  158. COALESCE(pea.budget_type,pf.sub_type_code) budget_type,
  159. COALESCE(pvbt.description,pf.description) description,
  160. pf.published::text , -- IS 'Опубликован на ООС'
  161. pf.improper_execution_text, -- 'Информация о ненадлежащем исполнении или неисполнении контракта, в том числе в части оплаты'
  162. a.fulfilment_sum_rur, -- 'Стоимость исполненных поставщиком обязательств, руб'
  163. pf.delivery_accept_date, -- 'Дата подписания заказчиком документа о приемке ТРУ'
  164. ccs.is_done::text -- 'Признак исполненности этапа'
  165. ,cfd.eis_date --отправлен в ЕИС
  166. ,case
  167. When pf.doc_type='ACCEPT' then 'Документ о приёмке'
  168. When pf.doc_type='PAY' then 'Платежный документ'
  169. When pf.doc_type='EXEC' then 'Документ об исполнении'
  170. else null end doc_type,
  171. COALESCE(l_aip.aip_code,pf.aip_code) aip_code,
  172. COALESCE(l_aip.name,pf.aip_name) aip_name
  173. From cm_contracts cc
  174. Inner Join lots l On (l.id = cc.lot_id And l.actual = true)
  175. LEFT Join pf On pf.lot_id = l.id
  176. LEFT Join po_account pa On pa.id = pf.po_account_id
  177. LEFT Join po_exp_account pea On (pea.id = pa.po_exp_account_id And pea.actual = true)
  178. LEFT Join
  179. (Select
  180. Min(gpo_budget_type_code) gpo_budget_type_code,
  181. budget_type
  182. From gpo_vocab_budget_links
  183. Where actual Is true
  184. Group by budget_type
  185. )gvbl On ((gvbl.budget_type)::text = (pea.budget_type)::text) OR pf.sub_type_code=gvbl.budget_type
  186. Left Join gpo_list_budget glb On (glb.code)::text = (gvbl.gpo_budget_type_code)::text
  187. Left Join po_budget pb On (pb.id = pa.po_budget_id And pb.actual = true)
  188. Left Join po_vocab_time_periods pvtp On pvtp.id = pb.po_period_id
  189. Left Join cm_contract_stages ccs On ccs.id = pf.stage_id And ccs.actual = true
  190. Left Join po_vocab_time_periods pvtp_a On (pvtp_a.id = pb.po_accept_period_id)
  191. Left Join gpo_po_exp_account gpea On gpea.po_exp_code_id = pea.id
  192. Left Join gpo_exp_account dea On (dea.id = gpea.gpo_exp_account_id)
  193. Left Join gpo_sub_account gsa On (gsa.id = pea.id)
  194. Left Join gpo_list_kosgu glk On (glk.kosgu_code = gsa.kosgu)
  195. Left Join gpo_list_expense_type glet On (glet.e_code = dea.e_code)
  196. Left Join po_vocab_budget_types pvbt On (pvbt.code::text = gvbl.budget_type::text)
  197. Left Join bp_ek.kf_kf_vocab_fundstype vf On (vf.process_name::text = pvbt.description::text)
  198. Left Join contragents supp On (supp."id" = pf.supplier_id)
  199. Left Join contragents c On (c.id = cc.contract_customer)
  200. Left Join (Select
  201. max(date_file) eis_date,
  202. finance_id
  203. From cm_contract_finances_documents
  204. Group By finance_id
  205. )cfd on cfd.finance_id=pf.id
  206. Left Join cm_contract_aips aip On (aip.lot_id = l.id and aip.cm_contract_finances_id = pf.id and aip.actual is true and pa.id=aip.po_account_id)
  207. Left Join gpo_list_aip l_aip On (l_aip.id = aip.aip_code)
  208. left join a on a.acceptance_finance_id = pf.id
  209. --where cc.lot_id = '4804034'
  210. --WHERE cc.lot_id=5031694
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement