Sha256: 96b90a9a609243933031504cead4fcfac7747e41bcc8e8503df78a42c065e65b

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'bigdecimal'

class ZbrojniNalogGenerator
  attr_accessor :datum, :nalozi
  attr_reader :rows, :iban_platitelj

  def initialize(iban_platitelj)
    @rows = []
    @nalozi = []
    @datum = DateTime.now
    @iban_platitelj = iban_platitelj
  end

  def rows
    @rows << row_300
    @rows << row_301
    @nalozi.each { |nalog| @rows << row_309(nalog) }
    @rows << row_399
  end

  def row_300
    datum.strftime('%Y%m%d') + "1701" + " "*985 + "300"
  end

  def row_301
    "#{@iban_platitelj}HRK" + " "*24 +  "%05d" % nalozi.count + zbroj_to_s + datum.strftime('%Y%m%d') + " "*916 + "301"
  end

  def row_309(nalog)
   nalog[2].ljust(34) + nalog[3].ljust(70) + " "*103 + nalog[1].ljust(140) +("%016.2f" % nalog[4]).gsub(".","") + nalog[0].gsub(" ","").ljust(26) + " "*159 + "0" + " "*449 + "309"
  end

  def zbroj_to_s
   raise 'nema naloga' if nalozi.count == 0
   amount = nalozi.map do |nalog|
      BigDecimal.new(nalog[4].to_s).round(2)
    end.reduce(:+)
    ("%021.2f" % amount).gsub(".","")
  end

  def row_399
    " "*997 + "399"
  end

  def export(file_name)
    File.open(File.expand_path(file_name), 'w:Windows-1250') do |file|
     rows.each { |row| file.write(row + "\r\n") }
   end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fina_files-0.1.8 lib/fina_files/zbrojni_nalog_generator.rb