class Magelex::LexwareBill

Attributes

country_code[RW]
customer_name[RW]
date[RW]
discount_19[RW]
discount_7[RW]
has_problems[RW]
incorrect_tax[RW]
order_nr[RW]
shipping_cost[RW]
status[RW]
tax_19[RW]
tax_7[RW]
total[RW]
total_0[RW]
total_19[RW]
total_7[RW]

Public Class Methods

floor2(value) click to toggle source
# File lib/magelex/lexware_bill.rb, line 105
def self.floor2 value
  (value * 100).to_i / 100.0
end
new(values={}) click to toggle source
# File lib/magelex/lexware_bill.rb, line 17
def initialize values={}
  @total_0, @total_7, @total_19, @total = 0, 0, 0, 0
  @customer_name = values.delete(:customer_name) || ""
  @order_nr = values.delete(:order_nr) || nil
  @date     = values.delete(:date)  || nil
  @total    = values.delete(:total) || 0
  @total_0  = values.delete(:total_0)  || 0
  @total_7  = values.delete(:total_7)  || 0
  @total_19 = values.delete(:total_19) || 0
  @tax_7    = values.delete(:tax_7)  || 0
  @tax_19   = values.delete(:tax_19) || 0
  @incorrect_tax = values.delete(:incorrect_tax) || 0
  @status   = values.delete(:status) || nil
  @shipping_cost = values.delete(:shipping_cost) || 0
  @country_code  = values.delete(:country_code)  || nil
  @discount_7    = values.delete(:discount_7) || 0
  @discount_19   = values.delete(:discount_19)  || 0
  @has_problems  = false
  if !values.empty?
    raise "Unknown values for bill: #{values.inspect}"
  end
end

Public Instance Methods

add_item(amount, tax, name, discount=0, full_amount=0) click to toggle source

Add item values to corresponding total_ and tax_ attributes depending on discount, include or exclude taxes. TODO full_amount shall not be 0 if discount is not zero

# File lib/magelex/lexware_bill.rb, line 47
def add_item amount, tax, name, discount=0, full_amount=0
  begin
    case TaxGuess.guess(amount, tax)
    when :tax0
      @total_0 += amount.round(2)
    when :tax7
      if discount != 0
        @total_7 += full_amount.round(2)
        @discount_7 += discount
      else
        @total_7 += amount.round(2)
      end
      @tax_7 += tax
    when :tax19
      if discount != 0
        @total_19 += full_amount.round(2)
        @discount_19 += discount
      else
        @total_19 += amount.round(2)
      end
      if swiss?
        Magelex::logger.info("19% Tax Item in swiss order: #{@order_nr}: #{name}")
      end
      @tax_19 += tax
    when :empty_item
      Magelex::logger.debug("Empty item: '#{name}' #{amount}, tax: #{tax}")
    end
  rescue RuntimeError
    Magelex::logger.warn("Unguessable tax (#{@order_nr}: #{name} #{amount}/#{tax})")
    @has_problems = true
  end
end
check() click to toggle source
# File lib/magelex/lexware_bill.rb, line 101
def check
  @has_problems == false && @total > 0 && check_diff == 0
end
check_diff() click to toggle source
# File lib/magelex/lexware_bill.rb, line 92
def check_diff
  @total.round(2) - (@total_0.round(2) + \
                     + @total_7.round(2) + \
                     + @total_19.round(2) + \
                     + @incorrect_tax.round(2) + \
                     - @discount_7.round(2) + \
                     - @discount_19.round(2)).round(2)
end
complete?() click to toggle source
# File lib/magelex/lexware_bill.rb, line 109
def complete?
  @status == "complete" || @status == "fraud"
end
customer_lastname() click to toggle source
# File lib/magelex/lexware_bill.rb, line 80
def customer_lastname
  @customer_name.to_s.split[-1]
end
in_eu?() click to toggle source
# File lib/magelex/lexware_bill.rb, line 88
def in_eu?
  @@EU_CODES.include? @country_code
end
order_and_name() click to toggle source
# File lib/magelex/lexware_bill.rb, line 84
def order_and_name
  "#{@order_nr} #{@customer_name}"
end
swiss?() click to toggle source
# File lib/magelex/lexware_bill.rb, line 40
def swiss?
  @country_code == 'CH'
end