Module: Magelex::LexwareCSV

Defined in:
lib/magelex/lexware_csv.rb

Class Method Summary collapse

Class Method Details

.render(bills) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/magelex/lexware_csv.rb', line 35

def self.render bills
  CSV.generate(encoding: 'utf-8') do |csv|
    bills.each do |b|
      to_rows(b).each { |r| csv << r }
    end
  end
end

.to_rows(bill) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/magelex/lexware_csv.rb', line 10

def self.to_rows bill
  # main
  rows = []
  rows << [bill.date.strftime("%d.%m.%Y"),
   bill.order_nr,
   bill.customer_name,
   bill.total.round(2),
   Magelex::AccountNumber.for_customer(bill),
   0]
  # subs
  [:total_0, :total_7, :total_19].each do |part|
    if (amount = bill.send(part)) != 0
      rows << [
              bill.date.strftime("%d.%m.%Y"),
              bill.order_nr,
              bill.customer_name,
              amount.round(2),
              0,
              Magelex::AccountNumber.for(bill, part),
              ]
    end
  end
  rows
end

.write(file, bills) ⇒ Object



3
4
5
6
7
8
# File 'lib/magelex/lexware_csv.rb', line 3

def self.write file, bills
  File.open(file, 'w') do |f|
    f.write render(bills).gsub("\n", "\r\n").encode(
      'windows-1252', invalid: :replace, undef: :replace)
  end
end