Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sac Membership Years Reporting #750

Merged
merged 17 commits into from
Jul 30, 2024
Merged
48 changes: 48 additions & 0 deletions app/domain/sac_imports/csv_report.rb
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
65 changes: 65 additions & 0 deletions app/domain/sac_imports/csv_source_file.rb
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import::Huts
module SacImports::Huts
class HutChairmanRow
include RemovingPlaceholderContactRole

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import::Huts
module SacImports::Huts
class HutChiefRow
def self.can_process?(row)
row[:verteilercode] == 4006 && role_type_for(row).present?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import::Huts
module SacImports::Huts
class HutCommissionRow
def self.can_process?(row)
row[:verteilercode] == 4000 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import::Huts
module SacImports::Huts
class HutRow
def self.can_process?(row)
row[:verteilercode] == 4000 && hut_type(row).present? &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import::Huts
module SacImports::Huts
class HutWardenPartnerRow
include RemovingPlaceholderContactRole

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import::Huts
module SacImports::Huts
class HutWardenRow
def self.can_process?(row)
row[:verteilercode] == 4005 && role_type_for(row).present?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import::Huts
module SacImports::Huts
class HutsRow
def self.can_process?(row)
row[:verteilercode] == 4000 && group_type(row).present? &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import::Huts
module SacImports::Huts
class KeyDepositRow
include RemovingPlaceholderContactRole

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import::Huts
module SacImports::Huts
module RemovingPlaceholderContactRole
def remove_placeholder_contact_role(person)
Group::ExterneKontakte::Kontakt
Expand Down
22 changes: 22 additions & 0 deletions app/domain/sac_imports/huts/unsupported_row.rb
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
mtnstar marked this conversation as resolved.
Show resolved Hide resolved
# 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
require_relative "huts/hut_chairman_row"
require_relative "huts/key_deposit_row"

module Import
module SacImports
class HutsImporter
HEADERS = {
contact_navision_id: "Kontaktnr.",
Expand All @@ -29,14 +29,14 @@ class HutsImporter
}

IMPORTERS = [
Import::Huts::HutCommissionRow,
Import::Huts::HutsRow,
Import::Huts::HutRow,
Import::Huts::HutChiefRow,
Import::Huts::HutWardenRow,
Import::Huts::HutWardenPartnerRow,
Import::Huts::HutChairmanRow,
Import::Huts::KeyDepositRow
SacImports::Huts::HutCommissionRow,
SacImports::Huts::HutsRow,
SacImports::Huts::HutRow,
SacImports::Huts::HutChiefRow,
SacImports::Huts::HutWardenRow,
SacImports::Huts::HutWardenPartnerRow,
SacImports::Huts::HutChairmanRow,
SacImports::Huts::KeyDepositRow
]

def initialize(path)
Expand Down
61 changes: 61 additions & 0 deletions app/domain/sac_imports/membership_years_report.rb
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

require Rails.root.join("lib", "import", "xlsx_reader.rb")

module Import
module SacImports
class PeopleImporter
class_attribute :headers, default: {
navision_id: "Mitgliedernummer",
Expand Down Expand Up @@ -92,7 +92,7 @@ def skip?(row)
end

def import_row(row)
entry = ::Import::PersonEntry.new(row, group: contact_role_group, emails: existing_emails)
entry = SacImports::PersonEntry.new(row, group: contact_role_group, emails: existing_emails)

if entry.valid?
import_person(entry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import
module SacImports
class PersonEntry
attr_reader :row

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import
module SacImports
module Sektion
class AdditionalMembership < Membership
TARGET_ROLE_TYPE = Group::SektionsMitglieder::MitgliedZusatzsektion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

require Rails.root.join("lib", "import", "xlsx_reader.rb")

module Import
module SacImports
module Sektion
class AdditionalMembershipsImporter < MembershipsImporter
self.headers = {
Expand All @@ -24,7 +24,7 @@ class AdditionalMembershipsImporter < MembershipsImporter
private

def import_row(row)
membership = ::Import::Sektion::AdditionalMembership.new(
membership = SacImports::Sektion::AdditionalMembership.new(
row,
group: membership_group(row),
current_ability: root_ability
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module Import
module SacImports
module Sektion
class Membership
attr_reader :row, :placeholder_contact_group, :current_ability
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

require Rails.root.join("lib", "import", "xlsx_reader.rb")

module Import
module SacImports
module Sektion
class MembershipsImporter < Import::PeopleImporter
class MembershipsImporter < SacImports::PeopleImporter
self.headers = {
navision_id: "Mitgliedernummer",
household_key: "Familien-Nr.",
Expand Down Expand Up @@ -57,7 +57,7 @@ def membership_group(row)
end

def import_row(row)
membership = ::Import::Sektion::Membership.new(
membership = SacImports::Sektion::Membership.new(
row,
group: membership_group(row),
placeholder_contact_group: contact_role_group,
Expand Down
Loading
Loading