Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sie-analysis (1.0.0)
sie-analysis (1.1.0)
bigdecimal (>= 3.1, < 5)

GEM
Expand Down Expand Up @@ -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
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
33 changes: 25 additions & 8 deletions lib/sie_analysis/analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<Hash>] account_number, account_name, movement and group/object fields
def account_movements(month:, group_code: nil, account_number: nil, dimension: nil, dimension_code: nil)
Expand Down Expand Up @@ -111,12 +116,19 @@ def vouchers_on_account(account_number, from:, to:)
end
end

# @return [Array<Symbol>] supported dimensions with objects declared in the export
# Includes declared dimensions and dimensions referenced by objects, balances or vouchers.
# @return [Array<Symbol, Integer>] 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
Expand Down Expand Up @@ -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/)
Expand Down
2 changes: 1 addition & 1 deletion lib/sie_analysis/budgets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions lib/sie_analysis/dimension_statements.rb
Original file line number Diff line number Diff line change
@@ -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] }
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 15 additions & 4 deletions lib/sie_analysis/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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])
Expand Down Expand Up @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion lib/sie_analysis/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module SieAnalysis
VERSION = "1.0.0"
VERSION = "1.1.0"
end
51 changes: 51 additions & 0 deletions spec/dimensions_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading