Advertisement
suburg

Untitled

Jan 8th, 2024 (edited)
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH input_id AS
  2. (
  3.     SELECT
  4.         ce.id AS ce_id,
  5.         ce.sequence_number,
  6.         ROW_NUMBER() OVER (ORDER BY sequence_number DESC) AS rn
  7.     FROM
  8.         administrations.contract con
  9.         INNER JOIN administrations.contract_edition ce ON con.id = ce.contract_id
  10.     WHERE
  11.         -- Вставить идентификатор договора сюда. Выгрузится последняя редакция
  12.         con.id = 'fc3eeb29-4e6a-47fd-b96b-2ae2fe4ef75a'
  13.         AND ce.status = 'Approved')
  14. SELECT
  15.     '<?xml version="1.0" encoding="utf-8"?>' ||
  16.     '<Contract xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"' ||
  17.     ' Number="' || ce_first."number"  || '" Date="' || ce_first.date_conclusion || '"' ||
  18.     CASE WHEN ce.sequence_number > 1 THEN
  19.     ' SupplementaryAgreementNumber="'|| COALESCE(ce."number", 'б/н') || '" SupplementaryAgreementDate="' || ce.date_conclusion || '"' ELSE '' END ||
  20.     ' SchoolNumber="' || eo.code || '" SchoolName="' || REPLACE(o.full_title, '"', '&quot;') || '" ' ||
  21.     ' FeedOrganizationName="' || REPLACE(feed_o.full_title, '"', '&quot;') || '" ' ||
  22.     ' StartDate="' || ce.date_from || '" EndDate="' || ce.date_to || '" xmlns="urn:ekburg.ru:feed:Contract:1.0">' ||
  23.  
  24.     '<School RegistrationName="' || REPLACE(o.full_title, '"', '&quot;') || '" ShortName="' || REPLACE(COALESCE(o.short_title, 'не указано'), '"', '&quot;') ||
  25.     '" Address="' || REPLACE(o.address, '"', '&quot;') || '" INN="' || o.inn || '" KPP="' || o.kpp ||
  26.     '" PayeeName="' || eo.parent_payment_info || '" AccountNumber="03234643657010006200" BIK="016577551" CorrespondenceNumber="40102810645370000054" Bank="Уральское ГУ Банка России//УФК по Свердловской области г. Екатеринбург" />' ||
  27.  
  28.     '<FeedOrganization RegistrationName="' || REPLACE(feed_o.full_title, '"', '&quot;') || '" ShortName="' || REPLACE(COALESCE(feed_o.short_title, 'не указано'), '"', '&quot;') ||
  29.     '" Address="' || REPLACE(feed_o.address, '"', '&quot;') || '" INN="' || feed_o.inn ||
  30.     '" KPP="' || COALESCE(feed_o.kpp, '000000000') ||
  31.     '" PayeeName="' || REPLACE(COALESCE(feed_o.short_title, feed_o.full_title), '"', '&quot;') || '" AccountNumber="' || bank_account.account_number ||
  32.     '" BIK="' || bank_account.bik || '" CorrespondenceNumber="' || bank_account.corr_account_number || '" Bank="' || REPLACE(bank_account.bank_name, '"', '&quot;') || '" />' ||
  33.  
  34.     '' || fo.fo_list ||
  35.     '</Contract>'
  36. FROM
  37.     input_id
  38.     INNER JOIN administrations.contract_edition ce ON ce.id = input_id.ce_id AND input_id.rn = 1
  39.     INNER JOIN administrations.contract con ON con.id = ce.contract_id
  40.     INNER JOIN administrations.bank_account ON bank_account.id = ce.bank_account_id
  41.     INNER JOIN administrations.contract_edition ce_first ON ce_first.contract_id = con.id AND ce_first.sequence_number = 1
  42.     INNER JOIN administrations.organization o ON con.educational_organization_id = o.id
  43.     INNER JOIN administrations.educational_organization eo ON o.educational_id = eo.id
  44.     INNER JOIN administrations.organization feed_o ON feed_o.id = con.catering_organization_id
  45.     INNER JOIN LATERAL (
  46.         SELECT
  47.             STRING_AGG (DISTINCT
  48.                 '<FeedComplex Name="' || cfo.name || '" ' ||
  49.                 ' ParentPrice="' || COALESCE(cfo.parent_money, 0) || '" BudgetPrice="' || COALESCE(cfo.subsidy_money, 0) || '" ' ||
  50.                 CASE WHEN cfo.products_cost IS NOT NULL
  51.                     THEN ' ProductSet="' || cfo.products_cost || '" Markup="' || cfo.margin || '" '
  52.                     ELSE  ''
  53.                 END ||             
  54.                 ' ClassFrom="' || cfo.minimum_class || '" ClassTo="' || cfo.maximum_class || '" ' ||
  55.                 CASE WHEN cfo.subsidy_money IS NOT NULL
  56.                     THEN ' IsCorrection="' || CASE WHEN subsidy_pc.is_two_meals THEN 'true' ELSE 'false' END ||
  57.                         '" IsInvalid="' || CASE WHEN subsidy_pc.invalid = 1 THEN 'true' ELSE 'false' END || '" ' ||
  58.                         ' IsLargeFamily="' || CASE WHEN subsidy_pc.large_family = 1 THEN 'true' ELSE 'false' END || '" ' ||
  59.                         ' IsLowIncome="' ||  CASE WHEN subsidy_pc.swo + subsidy_pc.ukraine_resident + subsidy_pc.low_income + subsidy_pc.military_children >= 1 THEN 'true' ELSE 'false' END || '" ' ||
  60.                         ' IsOrphan="' || CASE WHEN subsidy_pc.orphan = 1 THEN 'true' ELSE 'false' END || '"'
  61.                     ELSE ''
  62.                 END ||
  63.                 ' PortionCount="' || cfo.max_portion_count || '" />',
  64.                 ''
  65.             ) as fo_list
  66.         FROM
  67.             administrations.contract_feeding_option cfo
  68.             LEFT JOIN (
  69.                 SELECT
  70.                     subsidy.id AS subsidy_id,
  71.                     subsidy.is_two_meals,
  72.                     MAX(CASE WHEN privilege_category.code = 'LargeFamily' THEN 1 ELSE 0 END) AS large_family,
  73.                     MAX(CASE WHEN privilege_category.code = 'UkraineResident' THEN 1 ELSE 0 END) AS ukraine_resident,
  74.                     MAX(CASE WHEN privilege_category.code = 'LowIncome' THEN 1 ELSE 0 END) AS low_income,
  75.                     MAX(CASE WHEN privilege_category.code = 'MilitaryChildren' THEN 1 ELSE 0 END) AS military_children,
  76.                     MAX(CASE WHEN privilege_category.code = 'Invalid' THEN 1 ELSE 0 END) AS invalid,
  77.                     MAX(CASE WHEN privilege_category.code = 'Orphan' THEN 1 ELSE 0 END) AS orphan,
  78.                     MAX(CASE WHEN privilege_category.code = 'SWO' THEN 1 ELSE 0 END) AS swo
  79.                 FROM
  80.                     administrations.subsidy
  81.                     INNER JOIN administrations.subsidy_privilege_category subsidy_pc ON subsidy_pc.subsidy_id = subsidy.id
  82.                     INNER JOIN administrations.privilege_category ON privilege_category.id = subsidy_pc.privilege_category_id
  83.                 GROUP BY
  84.                     subsidy.id
  85.             ) subsidy_pc ON subsidy_pc.subsidy_id = cfo.subsidy_id         
  86.         WHERE
  87.             cfo.contract_edition_id = ce.id
  88.         GROUP BY
  89.             cfo.contract_edition_id
  90.     ) fo ON TRUE;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement