Module: Magelex::LexwareCSV

Defined in:
lib/magelex/lexware_csv.rb

Class Method Summary collapse

Class Method Details

.render(bills) ⇒ Object

Renders into String



69
70
71
72
73
74
75
# File 'lib/magelex/lexware_csv.rb', line 69

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



59
60
61
62
63
64
65
66
# File 'lib/magelex/lexware_csv.rb', line 59

def self.to_rows bill
  # split-booking needed?
  if [:total_0, :total_7, :total_19, :incorrect_tax, :discount_7, :discount_19].map{|t| bill.send(t)}.count{|i| i > 0} > 1
    to_split_rows bill
  else
    to_single_row bill
  end
end

.to_single_row(bill) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/magelex/lexware_csv.rb', line 47

def self.to_single_row bill
  tax_kind = [:total_0, :total_7, :total_19].detect{|t| bill.send(t) > 0}

  [[bill.date.strftime("%d.%m.%Y"),
   bill.order_nr,
   bill.order_and_name,
   bill.total.round(2),
   Magelex::AccountNumber.for_customer(bill),
   Magelex::AccountNumber.for(bill, tax_kind)
   ]]
end

.to_split_rows(bill) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/magelex/lexware_csv.rb', line 11

def self.to_split_rows bill
  rows = []
  rows << [bill.date.strftime("%d.%m.%Y"),
    bill.order_nr,
    bill.order_and_name,
    bill.total.round(2),
    Magelex::AccountNumber.for_customer(bill),
    0]
  # subs, refactoring needed.
  [:total_0, :total_7, :total_19, :incorrect_tax].each do |part|
    if (amount = bill.send(part)) != 0
      rows << [
              bill.date.strftime("%d.%m.%Y"),
              bill.order_nr,
              bill.order_and_name,
              amount.round(2),
              0,
              Magelex::AccountNumber.for(bill, part),
              ]
    end
  end
  [:discount_7, :discount_19].each do |part|
    if (amount = bill.send(part)) != 0
      rows << [
              bill.date.strftime("%d.%m.%Y"),
              bill.order_nr,
              bill.order_and_name,
              - amount.round(2),
              0,
              Magelex::AccountNumber.for(bill, part),
              ]
    end
  end
  rows
end

.write(file, bills) ⇒ Object

Writes(renders) to file.



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

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