-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement sac imports 6_membership_years_report, refs #742
- Loading branch information
Showing
45 changed files
with
423 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of | ||
# hitobito_sac_cas and licensed under the Affero General Public License version 3 | ||
# or later. See the COPYING file at the top-level directory or at | ||
# https://github.com/hitobito/hitobito_sac_cas. | ||
|
||
class SacImports::CsvReport | ||
def initialize(sac_import_name, headers) | ||
@timestamp = Time.zone.now.strftime("%Y-%m-%d-%H:%M") | ||
@sac_import_name = sac_import_name | ||
@headers = headers | ||
csv_init | ||
end | ||
|
||
def add_row(row) | ||
csv_append(row) | ||
end | ||
|
||
private | ||
|
||
def log_dir | ||
@log_dir ||= create_log_dir | ||
end | ||
|
||
def create_log_dir | ||
log_dir = Rails.root.join("log", "sac_imports") | ||
log_dir.mkpath | ||
log_dir | ||
end | ||
|
||
def csv_init | ||
CSV.open(csv_file_path, "wb", col_sep: ";") do |csv| | ||
csv << @headers | ||
end | ||
end | ||
|
||
def csv_append(row_content) | ||
row_content = @headers.map { |header| row_content[header] } | ||
CSV.open(csv_file_path, "ab", col_sep: ";") do |csv| | ||
csv << row_content | ||
end | ||
end | ||
|
||
def csv_file_path | ||
@csv_file_path ||= "#{log_dir}/#{@sac_import_name}_#{@timestamp}.csv" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
class SacImports::CsvSourceFile | ||
SOURCE_HEADERS = | ||
{NAV1: {}, | ||
NAV2: { | ||
navision_id: "Mitgliedernummer", | ||
household_key: "Familien-Nr.", | ||
group_navision_id: "Sektion", | ||
person_name: "Name", | ||
navision_membership_years: "Vereinsmitgliederjahre" | ||
}, | ||
NAV3: {}, | ||
WSO21: {}, | ||
WSO22: {}}.freeze | ||
|
||
AVAILABLE_SOURCES = SOURCE_HEADERS.keys.freeze | ||
|
||
def initialize(source_name) | ||
@source_name = source_name | ||
assert_available_source | ||
end | ||
|
||
def rows | ||
data = [] | ||
CSV.foreach(path, headers: true) do |row| | ||
data << process_row(row) | ||
end | ||
data | ||
end | ||
|
||
private | ||
|
||
def process_row(row) | ||
row = row.to_h | ||
hash = {} | ||
headers.keys.each do |header_key| | ||
hash[header_key] = row[headers[header_key]] | ||
end | ||
hash | ||
end | ||
|
||
def path | ||
files = Dir.glob("#{source_dir}/#{@source_name}_*.csv") | ||
if files.empty? | ||
raise("No source file #{@source_name}_*.csv found in RAILS_CORE_ROOT/tmp/sac_imports_src/.") | ||
end | ||
|
||
source_dir.join(files.first) | ||
end | ||
|
||
def headers | ||
SOURCE_HEADERS[@source_name] | ||
end | ||
|
||
def source_dir | ||
Rails.root.join("tmp", "sac_imports_src") | ||
end | ||
|
||
def assert_available_source | ||
unless AVAILABLE_SOURCES.include?(@source_name) | ||
raise "Invalid source name: #{@source_name}\navailable sources: #{AVAILABLE_SOURCES.map(&:to_s).join(", ")}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2023, Schweizer Alpen-Club. This file is part of | ||
# hitobito_sac_cas and licensed under the Affero General Public License version 3 | ||
# or later. See the COPYING file at the top-level directory or at | ||
# https://github.com/hitobito/hitobito_sac_cas. | ||
|
||
require Rails.root.join("lib", "import", "xlsx_reader.rb") | ||
|
||
module SacImports::Huts | ||
class UnsupportedRow | ||
def self.can_process?(row) | ||
true | ||
end | ||
|
||
def initialize(row) | ||
end | ||
|
||
def import! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of | ||
# hitobito_sac_cas and licensed under the Affero General Public License version 3 | ||
# or later. See the COPYING file at the top-level directory or at | ||
# https://github.com/hitobito/hitobito_sac_cas. | ||
|
||
module SacImports | ||
class MembershipYearsReport | ||
REPORT_HEADERS = [ | ||
:membership_number, :person_name, | ||
:navision_membership_years, :hitobito_membership_years, | ||
:diff, :errors | ||
].freeze | ||
|
||
def initialize(output: $stdout) | ||
@output = output | ||
@source_file = CsvSourceFile.new(:NAV2) | ||
@csv_report = CsvReport.new(:"6_membership_years_report", REPORT_HEADERS) | ||
end | ||
|
||
def create | ||
data = @source_file.rows | ||
fetch_hitobito_people(data) | ||
data.each do |row| | ||
process_row(row) | ||
end | ||
end | ||
|
||
private | ||
|
||
def process_row(row) | ||
person = @hitobito_people[row[:navision_id].to_i] | ||
@csv_report.add_row( | ||
{membership_number: row[:navision_id], | ||
person_name: row[:person_name], | ||
navision_membership_years: row[:navision_membership_years], | ||
hitobito_membership_years: person&.membership_years, | ||
diff: membership_years_diff(row[:navision_membership_years], person&.membership_years), | ||
errors: errors_for(person)} | ||
) | ||
end | ||
|
||
def fetch_hitobito_people(data) | ||
people_ids = data.pluck(:navision_id).compact | ||
@hitobito_people = Person.with_membership_years.where(id: people_ids).index_by(&:id) | ||
end | ||
|
||
def membership_years_diff(navision_years, hitobito_years) | ||
return nil if navision_years.blank? || hitobito_years.blank? | ||
|
||
navision_years.to_i - hitobito_years | ||
end | ||
|
||
def errors_for(person) | ||
[].tap do |errors| | ||
errors << "Person not found in hitobito" unless person | ||
end.join(", ").presence | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.