Top Level Namespace

Defined Under Namespace

Modules: Gnucash2Bmd Classes: BMD_LINE, Helpers

Constant Summary collapse

OLD_YEAR =
Date.today.year - 2
AUSGABE =
GNUCASH =
DATE_FORMAT =
'%Y.%m.%d'
KONTEN_GNUCASH_HEADERS =
{
'type' => nil,
'Vollständige_Bezeichnung' => 'Mandats-IDs',
'Name' => nil,
'Kontonummer' => 'bank-kontonr',
'Beschreibung' => '',
'Farbe' => '',
'Bemerkung' => '',
'Devise/Wertpapier M' => '',
'Devise/Wertpapier N' => '',
'Versteckt' => '',
'Steuerrelevant' => '',
'Platzhalter' => '',
}
KONTEN_JOURNAL_HEADERS =
{
  'Datum' => 'buchdatum',
  'Kontobezeichnung' => 'konto',
  'Nummer' => 'belegnr',
  'Beschreibung' => '',
  'Bemerkung' => '',
  'Buchungstext' => 'text',
  'Kategorie' => '',
  'Kontoart' => '',
  'Aktion' => '',
  'Abgleichen' => '',
  'To With Sym' => '',
  'From With Sym' => '',
  'Bis Nr.' => '',
  'Von Nr.' => '',
  'Zu Kurs/Preis' => '',
  'Von Kurs/Preis' => '',
}
Mandant_ID =
1
IDS =
{
:satzart  => 'satzart',
#    :mandant => 'Mandats-IDs',
:account_nr => 'bank-kontonr',
:bank_guid => 'bank-guid',
:konto => 'konto',
:gkonto => 'gkonto',
:buchdatum => 'buchdatum',
:buchungstext => 'buchungstext',
:betrag => 'betrag',
:belegnr => 'belegnr',
:buchcode => 'buchcode',
}

Instance Method Summary collapse

Instance Method Details

#check_cmd(filename) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/gnucash2bmd.rb', line 201

def check_cmd(filename)
  nr_rows = nil
  idx = 0
  CSV.foreach(filename,  :col_sep => ';') do |row|
    idx += 1
    nr_rows ||= row.size
    unless nr_rows == row.size
      puts "Expected #{nr_rows} not #{row.size} in line #{idx}"
      puts "   #{row}"
      exit 2
    end
  end
  puts "Alle #{idx} Zeilen von #{filename} haben #{nr_rows} Elemente"
rescue => error
  puts "got #{error} at line #{idx}"
end

#emit_bmd(filename) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/gnucash2bmd.rb', line 218

def emit_bmd(filename)
  CSV.open(filename, "wb", :encoding => 'UTF-8', :col_sep => ';') do |csv|
    csv << IDS.values
    @contents.uniq.each do |content|
      value_array = []
      IDS.keys.each do |key|
        begin
          value_array << eval("content.#{key} || nil")
        rescue => error
          binding.pry
        end
      end
      csv << value_array
    end
  end
end

#getBuchcode(betrag) ⇒ Object

Journal file start a new Journal entry only when the accout date is given



148
149
150
# File 'lib/gnucash2bmd.rb', line 148

def getBuchcode(betrag)
  betrag.to_f > 0.0 ? 1 : 2
end

#read_accounts(filename) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/gnucash2bmd.rb', line 128

def read_accounts(filename)
  line_nr = 0
  CSV.foreach(filename) do |row|
    line_nr += 1
    if line_nr == 1
      # TODO: Check headers
    else
      name_voll = row[1]
      bezeichung = row[2]
      next unless bezeichung && bezeichung.size > 0
      bmd = BMD_LINE.new
      bmd.bank_guid = Helpers.search_bank_guid(bezeichung)
      bmd. = bezeichung
      @contents << bmd
    end
  end
end

#read_gnucash(filename) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/gnucash2bmd.rb', line 235

def read_gnucash(filename)
  puts "Lese GnuCash Datei #{filename}"
  book = Gnucash.open(filename)
  book.accounts.each do ||
    bmd = BMD_LINE.new
    # bmd.bank_guid = Helpers.search_bank_guid(account.name)
    bmd.bank_guid = .id
    bmd.name = .name
    bmd.name_voll = .full_name
    bmd. = .description
    $stdout.puts "bmd; #{bmd}" if $VERBOSE
    @contents << bmd
  end
  @first_transaction_date = nil
  @last_transaction_date = nil
  @buchungs_nr = 0
  book.accounts.each do ||
    balance = Gnucash::Value.zero
    .transactions.each do |txn|
      balance += txn.value
      @buchungs_nr += 1
      # $VERBOSE = true if /-408.00/.match(txn.value.to_s)
      $stdout.puts(sprintf("%s  %8s  %8s  %s",
                          txn.date,
                          txn.value,
                          balance,
                          txn.description))  if $VERBOSE
      @buchung              = BMD_LINE.new
      @buchung.buchdatum    = txn.date.strftime(DATE_FORMAT)
      @last_transaction_date  = @buchung.buchdatum if !@last_transaction_date  || @last_transaction_date  < @buchung.buchdatum
      @first_transaction_date = @buchung.buchdatum if !@first_transaction_date || @first_transaction_date > @buchung.buchdatum
      @buchung.buchungstext = txn.description
      @buchung.belegnr      = txn.id # or @buchungs_nr ?? TODO

      if txn.splits.size == 2
        @buchung.konto    = txn.splits.first[:account].id
        @buchung.gkonto   = txn.splits.last[:account].id
        @buchung.betrag   = txn.splits.first[:value]
        @buchung.buchcode = getBuchcode(txn.splits.first[:value])
        @contents << @buchung
      else
        # Splittbuchungen werden vom Programm automatisch erkannt, wenn in mehreren aufeinanderfolgenden Buchungszeilen folgende Felder identisch sind:
        # Konto
        # Belegnr
        # Belegdatum
        gegenbuchungen = []
        txn.splits.each_with_index do |split, idx|
          $stdout.puts(sprintf("idx %d: %s  %8s   %8s",
                               idx,
                              split[:quantity],
                              split[:value],
                              split[:account].id,
                              ))
        end if $VERBOSE
        txn.splits.each_with_index do |split, idx|
          if txn.value.to_s.eql?(split[:value].to_s)
            @buchung.konto    = split[:account].id
            @buchung.betrag   = split[:value]
            @buchung.buchcode = getBuchcode(split[:value])
          else
            gegenbuchung = @buchung.clone
            gegenbuchung.gkonto = split[:account].id
            gegenbuchung.betrag = split[:value]
            gegenbuchung.buchcode = getBuchcode(split[:value])
            gegenbuchungen << gegenbuchung
          end
        end
        gegenbuchungen.each do |info| info.konto = @buchung.konto;  @contents << info; end
      end
    end
  end
  puts "Las #{@buchungs_nr} Buchungen von #{filename} vom #{@first_transaction_date} bis zum #{@last_transaction_date}"
end

#read_journal(filename) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/gnucash2bmd.rb', line 152

def read_journal(filename)
  line_nr = 0
  @bmd = nil
  @mehrteilig = nil
  @buchungs_nr = 0 # Will be added by column Aktion
  puts "reading journal from #{filename}"
  CSV.foreach(filename) do |row|
    line_nr += 1
    if line_nr == 1
      # TODO: Check headers
    else
      row.each_with_index{|val, idx| puts "#{sprintf('%-3d', idx)} #{val}" } if $VERBOSE
      if $VERBOSE && (/100000769/.match( row[3]) || (@bmd && /100000769/.match(@bmd.buchungstext)))
        puts "100000769 in #{filename}:#{line_nr} is #{row}"
        pp @bmd
      end
      if ( buchdatum = row[0]) && buchdatum.size > 0
        @contents << @bmd if @bmd # save terminated
        @buchungs_nr += 1
        @mehrteilig = row[6] && /mehrteilig/i.match(row[6])
        @bmd = BMD_LINE.new
        @bmd.buchdatum = row[0]
        @bmd.konto = Helpers.search_bank_guid(row[1])
        @bmd.buchungstext = row[3]
        @bmd.belegnr = "#{@buchungs_nr} aktion #{row[2]}"
      elsif (betrag = row[12]) && betrag.size > 0
        @bmd.betrag = betrag
        @bmd.buchcode =getBuchcode(betrag)
        @bmd.konto = Helpers.search_bank_guid(row[6])
        @contents << @bmd
        @bmd = @bmd.clone
        @bmd.konto = nil
      elsif (betrag = row[13]) && betrag.size > 0
        @bmd.betrag = betrag
        @bmd.buchcode =getBuchcode(betrag)
        @bmd.gkonto = Helpers.search_bank_guid(row[6])
        @contents << @bmd
        @bmd = @bmd.clone
        @bmd.gkonto = nil
      else
        # probably the last line
        @contents << @bmd if @bmd
        @bmd = nil
      end
    end
  end
  @contents << @bmd if @bmd # save terminated
end