From fc8e750bb315330c09a1aa83be780f05fe5d74ba Mon Sep 17 00:00:00 2001 From: Alexander Ross Date: Thu, 5 Nov 2026 17:44:28 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20support=20standard=20and=20?= =?UTF-8?q?custom=20SIE=20dimensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Preserve declared dimension names and voucher object references. Expose all discovered dimensions and allow analysis by standard name or number. Prepare version 1.1.0. --- Gemfile.lock | 4 +- README.md | 12 +++++- lib/sie_analysis/analysis.rb | 33 +++++++++++---- lib/sie_analysis/budgets.rb | 2 +- lib/sie_analysis/dimension_statements.rb | 6 +-- lib/sie_analysis/parser.rb | 19 +++++++-- lib/sie_analysis/version.rb | 2 +- spec/dimensions_spec.rb | 51 ++++++++++++++++++++++++ 8 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 spec/dimensions_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 2e3986f..53866b2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - sie-analysis (1.0.0) + sie-analysis (1.1.0) bigdecimal (>= 3.1, < 5) GEM @@ -42,7 +42,7 @@ CHECKSUMS rspec-expectations (3.13.5) sha256=33a4d3a1d95060aea4c94e9f237030a8f9eae5615e9bd85718fe3a09e4b58836 rspec-mocks (3.13.8) sha256=086ad3d3d17533f4237643de0b5c42f04b66348c28bf6b9c2d3f4a3b01af1d47 rspec-support (3.13.7) sha256=0640e5570872aafefd79867901deeeeb40b0c9875a36b983d85f54fb7381c47c - sie-analysis (1.0.0) + sie-analysis (1.1.0) BUNDLED WITH 4.0.23 diff --git a/README.md b/README.md index 3e00261..5b4f389 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Requires Ruby 3.2 or newer. From a local checkout, build and install the gem: ```sh gem build sie-analysis.gemspec -gem install ./sie-analysis-1.0.0.gem +gem install ./sie-analysis-1.1.0.gem ``` Load an export and create an analyzer: @@ -77,7 +77,15 @@ report[:income_statement][:groups].map { |group| group.slice(:code, :label, :amo Use `dimension: :cost_center` for cost centers. `account_movements` accepts the same dimensions, with `dimension_code: "ABC"` to select an object. -`analysis.available_dimensions` lists the available symbols. The export must contain the relevant object balances; +`analysis.available_dimensions` lists standard names as symbols and other dimensions +as integers. Supported names are `:cost_center` (1), `:cost_bearer` (2), `:project` (6), +`:employee` (7), `:customer` (8), `:supplier` (9), and `:invoice` (10). +Any dimension can also be selected by its SIE number, for example `dimension: 20`. +Declared dimensions and dimensions referenced in objects, balances, budgets or +transactions are included, even without monthly balances. +`document.dimensions` maps declared numbers to names, e.g. `{ 20 => "Department" }`; +`document.vouchers.first.lines.first.objects` contains pairs such as `[[20, "A"]]`. +The export must contain the relevant object balances; balance-sheet totals are omitted for dimension reports. ## Inspect the postings on an account diff --git a/lib/sie_analysis/analysis.rb b/lib/sie_analysis/analysis.rb index 8a4fc93..6817428 100644 --- a/lib/sie_analysis/analysis.rb +++ b/lib/sie_analysis/analysis.rb @@ -15,7 +15,12 @@ class Analysis CURRENCY = "SEK" DIMENSIONS = { cost_center: 1, - project: 6 + cost_bearer: 2, + project: 6, + employee: 7, + customer: 8, + supplier: 9, + invoice: 10 }.freeze # @param document [Parser::Parsed] a decoded and parsed SIE export @@ -26,7 +31,7 @@ def initialize(document) # Summarizes all calendar months touched by the inclusive date range. # @param from [Date] first date # @param to [Date] last date - # @param dimension [Symbol, nil] :project or :cost_center + # @param dimension [Symbol, Integer, nil] standard dimension name or SIE dimension number # @return [Hash] income statement, balance sheet, budget, monthly totals and notes def overview(from:, to:, dimension: nil) start_date, end_date = from, to @@ -57,7 +62,7 @@ def overview(from:, to:, dimension: nil) # @param month [String] YYYY-MM # @param group_code [String, nil] report group, e.g. "revenue" # @param account_number [Integer, nil] BAS account number - # @param dimension [Symbol, nil] :project or :cost_center + # @param dimension [Symbol, Integer, nil] standard dimension name or SIE dimension number # @param dimension_code [String, nil] object code; requires dimension # @return [Array] account_number, account_name, movement and group/object fields def account_movements(month:, group_code: nil, account_number: nil, dimension: nil, dimension_code: nil) @@ -111,12 +116,19 @@ def vouchers_on_account(account_number, from:, to:) end end - # @return [Array] supported dimensions with objects declared in the export + # Includes declared dimensions and dimensions referenced by objects, balances or vouchers. + # @return [Array] standard names and other SIE dimension numbers def available_dimensions - dims = [] - dims << :cost_center if @parsed.objects.keys.any? { |dim, _code| dim == 1 } - dims << :project if @parsed.objects.keys.any? { |dim, _code| dim == 6 } - dims + numbers = @parsed.dimensions.keys + @parsed.objects.keys.map(&:first) + [@parsed.period_changes, @parsed.budgets].each do |periods| + periods.each_value do |rows| + numbers.concat(rows.keys.filter_map { |key| key[1] if key.is_a?(Array) }) + end + end + @parsed.vouchers.each do |voucher| + voucher.lines.each { |line| numbers.concat(line.objects.map(&:first)) } + end + numbers.uniq.sort.map { |number| DIMENSIONS.key(number) || number } end private @@ -200,11 +212,16 @@ def require_dimension!(dimension, available) def normalize_dimension(dimension) return if dimension.nil? + return DIMENSIONS.key(dimension) || dimension if dimension.is_a?(Integer) && dimension.positive? raise ArgumentError, "Unsupported dimension" unless DIMENSIONS.key?(dimension) dimension end + def dimension_number(dimension) + dimension.is_a?(Integer) ? dimension : DIMENSIONS.fetch(dimension) + end + def normalize_year_month(value) text = value.to_s raise ArgumentError, "month must be YYYY-MM" unless text.match?(/\A\d{4}-\d{2}\z/) diff --git a/lib/sie_analysis/budgets.rb b/lib/sie_analysis/budgets.rb index 8ac131e..d0614ef 100644 --- a/lib/sie_analysis/budgets.rb +++ b/lib/sie_analysis/budgets.rb @@ -20,7 +20,7 @@ def budget_overview(months, dimension) def budget_row(month, key, raw, dimension) account, dim, code = dimension_parts(key) - return unless dim == (dimension && Analysis::DIMENSIONS.fetch(dimension)) + return unless dim == (dimension && dimension_number(dimension)) group = Groups.for_account(account) return unless income_group?(group) diff --git a/lib/sie_analysis/dimension_statements.rb b/lib/sie_analysis/dimension_statements.rb index 03bbe0f..015cd13 100644 --- a/lib/sie_analysis/dimension_statements.rb +++ b/lib/sie_analysis/dimension_statements.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true module SieAnalysis - # Cost-center / project income and account-row builders for Analysis. + # Income and account-row builders for any SIE dimension. module DimensionStatements private def dimension_income_groups(months, dimension) - dim_no = Analysis::DIMENSIONS.fetch(dimension) + dim_no = dimension_number(dimension) totals = accumulate_dimension_totals(months, dim_no) groups = dimension_group_rows(totals, dim_no) revenue = totals.values.sum { |values| values[:revenue] } @@ -56,7 +56,7 @@ def dimension_group_rows(totals, dim_no) end def dimension_account_rows(month, dimension) - dim_no = Analysis::DIMENSIONS.fetch(dimension) + dim_no = dimension_number(dimension) rows = [] (@parsed.period_changes[month] || {}).each do |key, raw| account, dim, code = dimension_parts(key) diff --git a/lib/sie_analysis/parser.rb b/lib/sie_analysis/parser.rb index 82fd1f3..6854631 100644 --- a/lib/sie_analysis/parser.rb +++ b/lib/sie_analysis/parser.rb @@ -7,13 +7,15 @@ module SieAnalysis class Parser Record = Data.define(:tag, :fields) Voucher = Data.define(:series, :number, :date, :description, :lines) - Line = Data.define(:account, :amount) + # Voucher line with [dimension number, object code] references. + Line = Data.define(:account, :amount, :objects) # Parsed ledger: monetary values are BigDecimal; voucher dates are Date. # Period keys are YYYYMM strings; object keys are [dimension number, code]. + # Dimensions maps declared dimension numbers to their exported names. Parsed = Data.define( :accounts, :objects, :opening_balances, :closing_balances, :results, :period_changes, - :financial_years, :vouchers, :budgets, :sie_type + :financial_years, :vouchers, :budgets, :sie_type, :dimensions ) # @param text [String] export text already decoded to UTF-8 @@ -28,6 +30,7 @@ def initialize(text) @text = text.to_s @accounts = {} @objects = {} + @dimensions = {} @opening = Hash.new(BigDecimal("0")) @closing = Hash.new(BigDecimal("0")) @results = Hash.new(BigDecimal("0")) @@ -50,7 +53,7 @@ def parse Parsed.new( accounts: @accounts, objects: @objects, opening_balances: @opening, closing_balances: @closing, results: @results, period_changes: @periods, financial_years: @years, vouchers: @vouchers, - budgets: @budgets, sie_type: @sie_type + budgets: @budgets, sie_type: @sie_type, dimensions: @dimensions ) rescue ParseError raise @@ -79,6 +82,7 @@ def apply_record(record) @sie_type = Integer(record.fields.fetch(0)) when "KONTO" then apply_account(record.fields) when "OBJEKT" then apply_object(record.fields) + when "DIM", "UNDERDIM" then apply_dimension(record.fields) when "RAR" then apply_year(record.fields) when "IB" then apply_balance!(@opening, record.fields) when "UB" then apply_balance!(@closing, record.fields) @@ -102,6 +106,13 @@ def apply_object(fields) @objects[[dim, code]] = name if dim && !code.empty? end + def apply_dimension(fields) + number = integer_field(fields[0]) + raise ArgumentError unless number&.positive? + + @dimensions[number] = unquote(fields.fetch(1)) + end + def apply_year(fields) year_no = integer_field(fields[0]) first, last = sie_date(fields[1]), sie_date(fields[2]) @@ -189,7 +200,7 @@ def apply_trans!(fields) raise ArgumentError if object_field.start_with?("{") && !valid_budget_objects?(object_field) - @current_voucher[:lines] << Line.new(account:, amount:) + @current_voucher[:lines] << Line.new(account:, amount:, objects: object_refs_from(object_field)) end def finish_voucher! diff --git a/lib/sie_analysis/version.rb b/lib/sie_analysis/version.rb index 5a0bfc0..6d2aeec 100644 --- a/lib/sie_analysis/version.rb +++ b/lib/sie_analysis/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module SieAnalysis - VERSION = "1.0.0" + VERSION = "1.1.0" end diff --git a/spec/dimensions_spec.rb b/spec/dimensions_spec.rb new file mode 100644 index 0000000..661b904 --- /dev/null +++ b/spec/dimensions_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +RSpec.describe "SIE dimensions" do + def parse_rows(rows) + SieAnalysis::Parser.parse("#SIETYP 4\n#RAR 0 20240101 20241231\n#{rows}") + end + + { cost_center: 1, cost_bearer: 2, project: 6, employee: 7, + customer: 8, supplier: 9, invoice: 10, 20 => 20 }.each do |name, number| + it "reports #{name} without mixing objects from another dimension" do + document = parse_rows(<<~SIE) + #KONTO 3010 "Sales" + #OBJEKT #{number} "A" "Selected" + #PSALDO 0 202401 3010 {#{number} "A"} -100 + #PSALDO 0 202401 3010 {21 "A"} -900 + #PBUDGET 0 202401 3010 {#{number} "A"} -80 + SIE + analysis = SieAnalysis::Analysis.new(document) + expect(analysis.available_dimensions).to include(name, 21) + report = analysis.overview(from: Date.new(2024, 1, 1), to: Date.new(2024, 1, 31), dimension: name) + expect(report.dig(:income_statement, :revenue)).to eq(BigDecimal("100")) + expect(report.dig(:budget, :rows).first).to include(actual: BigDecimal("100"), budget: BigDecimal("80")) + expect(analysis.account_movements(month: "2024-01", dimension: number)).to contain_exactly( + include(dimension_code: "A", dimension_label: "Selected", movement: BigDecimal("100")) + ) + end + end + + it "preserves declared names and discovers dimensions from every supported source" do + document = parse_rows(<<~SIE) + #DIM 20 "Department" + #UNDERDIM 21 "Team" 20 + #OBJEKT 22 "A" "Office" + #PSALDO 0 202401 3010 {23 "A"} -100 + #PBUDGET 0 202401 3010 {24 "A"} -80 + #VER A 1 20240101 "Sale" + { + #TRANS 3010 {25 "A"} -100 + } + SIE + expect(document.dimensions).to eq(20 => "Department", 21 => "Team") + expect(document.vouchers.first.lines.first.objects).to eq([[25, "A"]]) + analysis = SieAnalysis::Analysis.new(document) + expect(analysis.available_dimensions).to eq([20, 21, 22, 23, 24, 25]) + expect(analysis.account_movements(month: "2024-01", dimension: 25)).to eq([]) + expect { analysis.account_movements(month: "2024-01", dimension: 26) }.to raise_error(ArgumentError) + [0, -1, "20", :unknown].each do |dimension| + expect { analysis.account_movements(month: "2024-01", dimension:) }.to raise_error(ArgumentError) + end + end +end