Sha256: 132662310866a94ebcc3212e3e70ea20367c7b7182e57a4ecd7358dec9f99a95

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'csv'
require 'datev/header'
require 'datev/booking'

module Datev
  class Export
    CSV_OPTIONS = { :col_sep => ';' }

    DEFAULT_HEADER_ATTRIBUTES = {
      'DATEV-Format-KZ' => 'EXTF',
      'Versionsnummer'  => 510,
      'Datenkategorie'  => 21,
      'Formatname'      => 'Buchungsstapel',
      'Formatversion'   => 7,
      'Erzeugt am'      => Time.now,
      'Sachkontenlänge' => 4,
      'Bezeichnung'     => 'Buchungen',
      'Buchungstyp'     => 1,
      'WKZ'             => 'EUR'
    }

    def initialize(header_attributes)
      @header = Header.new DEFAULT_HEADER_ATTRIBUTES.merge(header_attributes)
      @rows = []
    end

    def <<(booking)
      raise ArgumentError.new('Param must be a Datev::Booking instance') unless booking.is_a?(Booking)

      @rows << booking
    end

    def to_s
      CSV.generate(CSV_OPTIONS) do |csv|
        write(csv)
      end
    end

    def to_file(filename)
      CSV.open(filename, 'wb', CSV_OPTIONS) do |csv|
        write(csv)
      end
    end

  private

    def write(csv)
      csv << @header.to_a
      csv << Booking.fields.map(&:name)

      @rows.each do |row|
        csv << row.to_a
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datev-0.1.0 lib/datev/export.rb