table_name: "FI-0090 \u0421\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043A\u0430\
  \ \u043F\u043E \u0414\u0417/\u041F\u0414\u0417"
main_dttm_col: dt
description: null
default_endpoint: null
offset: 0
cache_timeout: null
schema: dm_view
sql: "-- [JINJA_BLOCK] \u0426\u0435\u043D\u0442\u0440\u0430\u043B\u0438\u0437\u043E\
  \u0432\u0430\u043D\u043D\u043E\u0435 \u043E\u043F\u0440\u0435\u0434\u0435\u043B\u0435\
  \u043D\u0438\u0435 \u0432\u0441\u0435\u0445 Jinja \u043F\u0435\u0440\u0435\u043C\
  \u0435\u043D\u043D\u044B\u0445\r\n{% set raw_to   = filter_values('last_day_of_month_dt_txt')[0]\
  \ \r\n                  if filter_values('last_day_of_month_dt_txt')   else '01.05.2025'\
  \ %}\r\n\r\n{# \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u043C \xABDD.MM.YYYY\xBB\
  \ \u043D\u0430 \u0447\u0430\u0441\u0442\u0438 #}\r\n{% set to_parts   = raw_to.split('.')\
  \ %}\r\n\r\n{# \u0441\u043E\u0431\u0438\u0440\u0430\u0435\u043C ISO\u2011\u0441\u0442\
  \u0440\u043E\u043A\u0443 \xABYYYY-MM-DD\xBB #}\r\n{% set to_dt   = to_parts[2] \
  \  ~ '-' ~ to_parts[1]   ~ '-' ~ to_parts[0] %}\r\n\r\nwith \r\ncp_relations_type\
  \ AS (\r\n   select * from ( SELECT \r\n        ctd.counterparty_code AS counterparty_code,\r\
  \n        min(dt_from) as dt_from,\r\n        max(dt_to) as dt_to,\r\n        crt.relation_type_code\
  \ || ' ' || crt.relation_type_name AS relation_type_code_name\r\n    FROM\r\n  \
  \      dm_view.counterparty_td ctd\r\n    JOIN dm_view.counterparty_relation_type_texts\
  \ crt \r\n        ON ctd.relation_type_code = crt.relation_type_code\r\n    GROUP\
  \ BY\r\n        ctd.counterparty_code, ctd.counterparty_full_name,\r\n        crt.relation_type_code,crt.relation_type_name)\r\
  \n            WHERE \r\n        dt_from <= toDate('{{to_dt }}') AND \r\n       \
  \ dt_to >= toDate('{{to_dt }}')\r\n        ),\r\nt_debt as \r\n(SELECT dt, \r\n\
  counterparty_search_name,\r\ncp_relations_type.relation_type_code_name as relation_type_code_name,\r\
  \nunit_balance_code || ' ' || unit_balance_name as unit_balance_code_name,\r\n'1.\
  \ \u0421\u0443\u043C\u043C\u0430' as attribute,\r\nsum(debt_balance_subposition_no_revaluation_usd_amount)\
  \ as debt_amount,\r\nsumIf(debt_balance_subposition_no_revaluation_usd_amount,dt_overdue\
  \ < dt) as overdue_amount\r\nfrom dm_view.account_debt_for_working_capital t_debt\r\
  \njoin cp_relations_type ON\r\ncp_relations_type.counterparty_code = t_debt.counterparty_code\r\
  \nwhere dt = toLastDayOfMonth(dt)\r\nand match(general_ledger_account_code,'((62)|(60)|(76))')\r\
  \nand debit_or_credit = 'S'\r\nand account_type = 'D'\r\nand dt between addMonths(toDate('{{to_dt\
  \ }}'),-12) and toDate('{{to_dt }}')\r\ngroup by dt, counterparty_search_name,unit_balance_code_name,relation_type_code_name\r\
  \n),\r\n\r\nt_transaction_count_base as \r\n(\r\nselect *,\r\ncp_relations_type.relation_type_code_name\
  \ as relation_type_code_name,\r\nunit_balance_code || ' ' || unit_balance_name as\
  \ unit_balance_code_name,\r\n        case when dt_overdue<dt_clearing then\r\n \
  \       dateDiff(day, dt_overdue, dt_clearing) \r\n        else 0\r\n        end\
  \ as overdue_days\r\nfrom dm_view.accounting_documents_leading_to_debt t_docs\r\n\
  join cp_relations_type ON\r\ncp_relations_type.counterparty_code = t_docs.counterparty_code\r\
  \nwhere 1=1\r\n\r\nand match(general_ledger_account_code,'((62)|(60)|(76))')\r\n\
  and debit_or_credit = 'S'\r\nand account_type = 'D'\r\n)\r\n\r\nselect * from t_debt\r\
  \n\r\nunion all \r\n\r\nselect toLastDayOfMonth(dt_debt) as dt, \r\ncounterparty_search_name,\r\
  \nrelation_type_code_name,\r\nunit_balance_code_name,\r\n'2. \u043A\u043E\u043B\u0438\
  \u0447\u0435\u0441\u0442\u0432\u043E \u0442\u0440\u0430\u043D\u0437\u0430\u043A\u0446\
  \u0438\u0439 \u0432 \u043C\u0435\u0441\u044F\u0446' as attribute,\r\ncount(1)  as\
  \ debt_amount,\r\nnull as overdue_amount\r\nfrom t_transaction_count_base\r\nwhere\
  \ dt_debt between addMonths(toDate('{{to_dt }}'),-12) and toDate('{{to_dt }}')\r\
  \ngroup by toLastDayOfMonth(dt_debt), \r\ncounterparty_search_name,\r\nrelation_type_code_name,\r\
  \nunit_balance_code_name,attribute\r\n\r\nunion all  \r\n\r\nselect toLastDayOfMonth(dt_clearing)\
  \ as dt, \r\ncounterparty_search_name,\r\nrelation_type_code_name,\r\nunit_balance_code_name,\r\
  \n'2. \u043A\u043E\u043B\u0438\u0447\u0435\u0441\u0442\u0432\u043E \u0442\u0440\u0430\
  \u043D\u0437\u0430\u043A\u0446\u0438\u0439 \u0432 \u043C\u0435\u0441\u044F\u0446\
  ' as attribute,\r\nnull  as debt_amount,\r\ncount(1) as overdue_amount\r\nfrom t_transaction_count_base\r\
  \nwhere dt_clearing between addMonths(toDate('{{to_dt }}'),-12) and toDate('{{to_dt\
  \ }}')\r\nand overdue_days > 0\r\ngroup by toLastDayOfMonth(dt_clearing), \r\ncounterparty_search_name,\r\
  \nrelation_type_code_name,\r\nunit_balance_code_name,attribute\r\n\r\nunion all\
  \ \r\n\r\nselect toLastDayOfMonth(dt_clearing) as dt, \r\ncounterparty_search_name,\r\
  \nrelation_type_code_name,\r\nunit_balance_code_name,\r\nmultiIf(\r\noverdue_days\
  \ < 30,'3. \u0434\u043E 30',\r\noverdue_days between 30 and 60, '4. \u043E\u0442\
  \ 30 \u0434\u043E 60',\r\noverdue_days between 61 and 90, '5. \u043E\u0442 61 \u0434\
  \u043E 90',\r\noverdue_days>90,'6. \u0431\u043E\u043B\u0435\u0435 90 \u0434\u043D\
  ',\r\nnull\r\n)\r\n as attribute,\r\nnull  as debt_amount,\r\ncount(1) as overdue_amount\r\
  \nfrom t_transaction_count_base\r\nwhere dt_clearing between addMonths(toDate('{{to_dt\
  \ }}'),-12) and toDate('{{to_dt }}')\r\nand overdue_days > 0\r\ngroup by toLastDayOfMonth(dt_clearing),\
  \ \r\ncounterparty_search_name,\r\nrelation_type_code_name,\r\nattribute,unit_balance_code_name,attribute\r\
  \n"
params: null
template_params: null
filter_select_enabled: true
fetch_values_predicate: null
extra: null
normalize_columns: false
uuid: 9e645dc0-da25-4f61-9465-6e649b0bc4b1
metrics:
- metric_name: m_debt_amount
  verbose_name: "\u0414\u0417, $"
  metric_type: count
  expression: sum(debt_amount)
  description: null
  d3format: null
  currency: null
  extra:
    warning_markdown: ''
  warning_text: null
- metric_name: m_overdue_amount
  verbose_name: "\u041F\u0414\u0417, $"
  metric_type: null
  expression: sum(overdue_amount)
  description: null
  d3format: null
  currency: null
  extra:
    warning_markdown: ''
  warning_text: null
columns:
- column_name: debt_amount
  verbose_name: null
  is_dttm: false
  is_active: true
  type: Nullable(Decimal(38, 2))
  advanced_data_type: null
  groupby: true
  filterable: true
  expression: null
  description: null
  python_date_format: null
  extra:
    warning_markdown: null
- column_name: overdue_amount
  verbose_name: null
  is_dttm: false
  is_active: true
  type: Nullable(Decimal(38, 2))
  advanced_data_type: null
  groupby: true
  filterable: true
  expression: null
  description: null
  python_date_format: null
  extra:
    warning_markdown: null
- column_name: dt
  verbose_name: null
  is_dttm: true
  is_active: true
  type: Nullable(Date)
  advanced_data_type: null
  groupby: true
  filterable: true
  expression: null
  description: null
  python_date_format: null
  extra:
    warning_markdown: null
- column_name: unit_balance_code_name
  verbose_name: null
  is_dttm: false
  is_active: true
  type: Nullable(String)
  advanced_data_type: null
  groupby: true
  filterable: true
  expression: null
  description: null
  python_date_format: null
  extra:
    warning_markdown: null
- column_name: relation_type_code_name
  verbose_name: null
  is_dttm: false
  is_active: true
  type: Nullable(String)
  advanced_data_type: null
  groupby: true
  filterable: true
  expression: null
  description: null
  python_date_format: null
  extra:
    warning_markdown: null
- column_name: counterparty_search_name
  verbose_name: null
  is_dttm: false
  is_active: true
  type: Nullable(String)
  advanced_data_type: null
  groupby: true
  filterable: true
  expression: null
  description: null
  python_date_format: null
  extra:
    warning_markdown: null
- column_name: attribute
  verbose_name: null
  is_dttm: false
  is_active: true
  type: Nullable(String)
  advanced_data_type: null
  groupby: true
  filterable: true
  expression: null
  description: null
  python_date_format: null
  extra:
    warning_markdown: null
version: 1.0.0
database_uuid: 97aced68-326a-4094-b381-27980560efa9
