Module: Magelex::LexwareCSV

Defined in:
lib/magelex/lexware_csv.rb

Class Method Summary collapse

Class Method Details

.render(bills) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/magelex/lexware_csv.rb', line 56

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



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

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



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

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.customer_name,
   bill.total.round(2),
   Magelex::AccountNumber.for_customer(bill),
   Magelex::AccountNumber.for(bill, tax_kind)
   ]]
end

.to_split_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_split_rows bill
  rows = []
  rows << [bill.date.strftime("%d.%m.%Y"),
    bill.order_nr,
    # Replace , by dash
    bill.customer_name,
    bill.total.round(2),
    Magelex::AccountNumber.for_customer(bill),
    0]
  # subs
  [:total_0, :total_7, :total_19, :incorrect_tax, :discount_7, :discount_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