SU2-2 | Скрипт интегрирован в sidekiq, создан простой интерфейс на взаимодействие с SK, тест на нескольких ИНН

This commit is contained in:
Dmitry
2025-11-28 17:22:47 +03:00
parent 6758e271fa
commit 7f6e386250
15 changed files with 208 additions and 3 deletions
+14
View File
@@ -0,0 +1,14 @@
class ReportsController < ApplicationController
def index
end
def create
inn = params[:inn]
output_path = Rails.root.join('tmp', 'reports', inn)
FileUtils.mkdir_p(output_path)
FileProcessorJob.perform_async(inn, output_path.to_s)
redirect_to reports_path, notice: 'Отчёт загружается в фоне'
end
end
+2
View File
@@ -0,0 +1,2 @@
module ReportsHelper
end
+53
View File
@@ -0,0 +1,53 @@
require 'zip'
require 'stringio'
class FileProcessorJob
include Sidekiq::Job
def perform(inn, output_path)
Rails.logger.info "=== Начало загрузки для ИНН: #{inn} ==="
download_inn(inn, output_path)
Rails.logger.info "=== Завершено для ИНН: #{inn} ==="
rescue => e
Rails.logger.error "=== ОШИБКА: #{e.message} ==="
Rails.logger.error e.backtrace.first(5).join("\n")
raise
end
private
def get_content(url, json_need = false)
headers = {
'User-Agent' => "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
'Accept-Language' => 'en-US,en;q=0.9',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
}
content = HTTParty.get(url, headers: headers).body
json_need ? JSON.parse(content) : content
end
def download_inn(inn, output)
url = "https://bo.nalog.gov.ru/advanced-search/organizations/search?query=#{inn}&page=0&size=20"
content = get_content(url, true)
id = content['content'][0]['id']
url = "https://bo.nalog.gov.ru/nbo/organizations/#{id}/bfo/"
content = get_content(url, true)
periods_and_details_ids = content.map { |item| [item['period'], item['typeCorrections'][0]['correction']['id']] }
periods_and_details_ids.each do |period, details_id|
url = "https://bo.nalog.gov.ru/download/bfo/#{id}?auditReport=false&balance=true"\
"&capitalChange=true&clarification=false&targetedFundsUsing=false&detailsId=#{details_id}"\
"&financialResult=true&fundsMovement=true&type=XLS&period=#{period}"
content = get_content(url)
zip_file = Zip::File.open_buffer(StringIO.new(content))
zip_file.each do |entry|
output_file = File.join(output, entry.name)
FileUtils.mkdir_p(File.dirname(output_file))
zip_file.extract(entry, output_file) { true } # перезаписывать если существует
end
end
end
end
+4
View File
@@ -0,0 +1,4 @@
<div>
<h1 class="font-bold text-4xl">Reports#create</h1>
<p>Find me in app/views/reports/create.html.erb</p>
</div>
+17
View File
@@ -0,0 +1,17 @@
<!-- app/views/reports/index.html.erb -->
<div style="max-width: 500px; margin: 50px auto; padding: 20px;">
<h1>Загрузка отчётов по ИНН</h1>
<% if notice %>
<p style="color: green;"><%= notice %></p>
<% end %>
<%= form_with url: reports_path, method: :post, local: true do |f| %>
<div style="margin-bottom: 15px;">
<%= f.label :inn, "Введите ИНН:" %>
<%= f.text_field :inn, placeholder: "1234567890", required: true, style: "width: 100%; padding: 8px;" %>
</div>
<%= f.submit "Загрузить отчёт", style: "padding: 10px 20px; cursor: pointer;" %>
<% end %>
</div>