Class: Magelex::LexwareBill
- Inherits:
-
Object
- Object
- Magelex::LexwareBill
- Defined in:
- lib/magelex/lexware_bill.rb
Constant Summary collapse
- @@EU_CODES =
['BE','BG','CZ','DK','EE','EL','ES','FR', 'IE','IT','CY','LV','LT','LU','HU','MT', 'NL','AT','PL','PT','RO','SI','SK','FI','SE','UK']
Instance Attribute Summary collapse
-
#country_code ⇒ Object
Returns the value of attribute country_code.
-
#customer_name ⇒ Object
Returns the value of attribute customer_name.
-
#date ⇒ Object
Returns the value of attribute date.
-
#discount_19 ⇒ Object
Returns the value of attribute discount_19.
-
#discount_7 ⇒ Object
Returns the value of attribute discount_7.
-
#has_problems ⇒ Object
Returns the value of attribute has_problems.
-
#incorrect_tax ⇒ Object
Returns the value of attribute incorrect_tax.
-
#order_nr ⇒ Object
Returns the value of attribute order_nr.
-
#shipping_cost ⇒ Object
Returns the value of attribute shipping_cost.
-
#status ⇒ Object
Returns the value of attribute status.
-
#tax_19 ⇒ Object
Returns the value of attribute tax_19.
-
#tax_7 ⇒ Object
Returns the value of attribute tax_7.
-
#total ⇒ Object
Returns the value of attribute total.
-
#total_0 ⇒ Object
Returns the value of attribute total_0.
-
#total_19 ⇒ Object
Returns the value of attribute total_19.
-
#total_7 ⇒ Object
Returns the value of attribute total_7.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_item(amount, tax, name, discount = 0, full_amount = 0) ⇒ Object
Add item values to corresponding total_ and tax_ attributes depending on discount, include or exclude taxes.
- #check ⇒ Object
- #check_diff ⇒ Object
- #complete? ⇒ Boolean
- #customer_lastname ⇒ Object
- #in_eu? ⇒ Boolean
-
#initialize(values = {}) ⇒ LexwareBill
constructor
A new instance of LexwareBill.
- #order_and_name ⇒ Object
- #swiss? ⇒ Boolean
Constructor Details
#initialize(values = {}) ⇒ LexwareBill
Returns a new instance of LexwareBill.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 |
Instance Attribute Details
#country_code ⇒ Object
Returns the value of attribute country_code.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def country_code @country_code end |
#customer_name ⇒ Object
Returns the value of attribute customer_name.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def customer_name @customer_name end |
#date ⇒ Object
Returns the value of attribute date.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def date @date end |
#discount_19 ⇒ Object
Returns the value of attribute discount_19.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def discount_19 @discount_19 end |
#discount_7 ⇒ Object
Returns the value of attribute discount_7.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def discount_7 @discount_7 end |
#has_problems ⇒ Object
Returns the value of attribute has_problems.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def has_problems @has_problems end |
#incorrect_tax ⇒ Object
Returns the value of attribute incorrect_tax.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def incorrect_tax @incorrect_tax end |
#order_nr ⇒ Object
Returns the value of attribute order_nr.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def order_nr @order_nr end |
#shipping_cost ⇒ Object
Returns the value of attribute shipping_cost.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def shipping_cost @shipping_cost end |
#status ⇒ Object
Returns the value of attribute status.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def status @status end |
#tax_19 ⇒ Object
Returns the value of attribute tax_19.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def tax_19 @tax_19 end |
#tax_7 ⇒ Object
Returns the value of attribute tax_7.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def tax_7 @tax_7 end |
#total ⇒ Object
Returns the value of attribute total.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def total @total end |
#total_0 ⇒ Object
Returns the value of attribute total_0.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def total_0 @total_0 end |
#total_19 ⇒ Object
Returns the value of attribute total_19.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def total_19 @total_19 end |
#total_7 ⇒ Object
Returns the value of attribute total_7.
9 10 11 |
# File 'lib/magelex/lexware_bill.rb', line 9 def total_7 @total_7 end |
Class Method Details
.floor2(value) ⇒ Object
105 106 107 |
# File 'lib/magelex/lexware_bill.rb', line 105 def self.floor2 value (value * 100).to_i / 100.0 end |
Instance Method Details
#add_item(amount, tax, name, discount = 0, full_amount = 0) ⇒ Object
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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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 ⇒ Object
101 102 103 |
# File 'lib/magelex/lexware_bill.rb', line 101 def check @has_problems == false && @total > 0 && check_diff == 0 end |
#check_diff ⇒ Object
92 93 94 95 96 97 98 99 |
# 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? ⇒ Boolean
109 110 111 |
# File 'lib/magelex/lexware_bill.rb', line 109 def complete? @status == "complete" || @status == "fraud" end |
#customer_lastname ⇒ Object
80 81 82 |
# File 'lib/magelex/lexware_bill.rb', line 80 def customer_lastname @customer_name.to_s.split[-1] end |
#in_eu? ⇒ Boolean
88 89 90 |
# File 'lib/magelex/lexware_bill.rb', line 88 def in_eu? @@EU_CODES.include? @country_code end |
#order_and_name ⇒ Object
84 85 86 |
# File 'lib/magelex/lexware_bill.rb', line 84 def order_and_name "#{@order_nr} #{@customer_name}" end |
#swiss? ⇒ Boolean
40 41 42 |
# File 'lib/magelex/lexware_bill.rb', line 40 def swiss? @country_code == 'CH' end |