magelex 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9375788e021f187ddbad0f169654145ff6d28f7c
4
- data.tar.gz: 1a04c859ed76349806144ab41bb2541af91ee187
3
+ metadata.gz: 032119d67096a3213c374635a710796c3732ea30
4
+ data.tar.gz: 9451eb9eba3fddd6bf7d6a12d0af3e554dc53222
5
5
  SHA512:
6
- metadata.gz: c166ff52c6a1cb1a17c3e4b24821e3250c26cd5da42a8fe9ac5f359d3aa81ee4e508ac1ea399c84709133b102604783945c86862f9ae84f4ca325fb45b30abb5
7
- data.tar.gz: 687c4a026691875b1ce9d9506777b91884b49f7509bfa8ca990e6e6352e52c6d0f5195e35157643e01bedcc6a5b793d0b1800ce666a1f5c0aebbc9a3054fd15d
6
+ metadata.gz: a4e4e62c48ffa8fca02d241e5a953b191cf8fd3e6a7ad29769140aef3a0eae03b681822fa23a251b1aa33aac76afc108a798f97865d44eed6a606fce294e1a0d
7
+ data.tar.gz: cf520013296e47b60ac59790e43b677337673b7bb65bd18c292a3af5b9e95fddf60a0411492af011d0cc4cbaf8587e09a4c53961955cba9e46cc431ad694bbd3
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
  /tmp/
10
10
  *.swp
11
11
  magelex.conf
12
+ /data/
13
+ /lexware/
data/README.md CHANGED
@@ -48,6 +48,10 @@ If no database queries should be done, invoke with `--skip-db`.
48
48
 
49
49
  Call `magelex --help` to get a basic idea.
50
50
 
51
+ ## TODOs
52
+
53
+ - Respect Discounts (item and totals).
54
+
51
55
  ## Development
52
56
 
53
57
  After checking out the repo, run `rake spec` to run the tests. You can also run `bundle console` for an interactive prompt that will allow you to experiment. Run `bundle exec magelex` to use the gem in this directory, ignoring other installed copies of this gem.
data/bin/magelex CHANGED
@@ -52,50 +52,20 @@ Magelex.logger.formatter = proc { |severity, datetime, progname, msg|
52
52
  def main options
53
53
  Magelex.logger.info("Started")
54
54
 
55
- # Import/Read file.
56
- bills_export = []
57
- bills = Magelex::MagentoCSV.read ARGV[0]
58
- bills.each do |bill|
59
- if !bill.complete?
60
- Magelex.logger.info("Skip order #{bill.order_nr} (incomplete: #{bill.status})")
61
- else # complete!
62
- bill.consume_shipping_cost
63
- bill.swissify
64
- if !bill.check
65
- Magelex.logger.info("Skip order #{bill.order_nr} "\
66
- "(totals do not match #{bill.total} != "\
67
- "(0: #{bill.total_0} + 7: #{bill.total_7} "\
68
- "+ 19: #{bill.total_19} "\
69
- "= #{bill.total_0 + bill.total_7 + bill.total_19})")
70
- else
71
- Magelex.logger.debug("Handle #{bill.order_nr}")
72
- bills_export << bill
73
- end
74
- end
75
- end
55
+ Dir.mkdir options[:out_dir] rescue {}
56
+ outdir = Pathname.new(options[:out_dir]).realpath
76
57
 
77
- # Fix dates via database.
78
- if !options[:skipdb]
79
- begin
80
- Magelex.logger.info("Fetching dates from magento mysql.")
81
- Magelex::MagentoMYSQL.update_dates YAML.load_file('magelex.conf'), bills_export
82
- rescue => e
83
- Magelex.logger.error("Could not connect to MySQL database, exiting.")
84
- Magelex.logger.error(e.inspect)
85
- exit 2
58
+ if File.directory?(ARGV[0])
59
+ Dir.entries(ARGV[0]).select{|e| !File.directory? e}.each do |f|
60
+ outfile = outdir.join(File::basename f)
61
+ infile = Pathname.new(ARGV[0]).join f
62
+ Magelex.logger.info("Processing file #{infile}")
63
+ Magelex::process_file infile, outfile, options
86
64
  end
65
+ else
66
+ outfile = outdir.join(File::basename ARGV[0])
67
+ Magelex::process_file ARGV[0], outfile, options
87
68
  end
88
-
89
- # Export/Write to file
90
- Dir.mkdir options[:out_dir] rescue {}
91
- outdir = Pathname.new(options[:out_dir]).realpath
92
- outfile = outdir.join(File::basename ARGV[0])
93
- if File.exist?(outfile)
94
- Magelex.logger.error("Output file #{outfile} exists already, exiting.")
95
- exit 3
96
- end
97
- Magelex.logger.info("Writing to #{outfile}")
98
- Magelex::LexwareCSV.write outfile, bills_export
99
69
  Magelex.logger.info("Finished")
100
70
  end
101
71
 
@@ -21,7 +21,7 @@ module Magelex
21
21
  elsif tax_kind == :total_19
22
22
  return for_19 bill
23
23
  else
24
- raise "unknown tax_kind"
24
+ raise "unknown tax_kind (#{tax_kind})"
25
25
  end
26
26
  end
27
27
 
@@ -7,7 +7,7 @@ module Magelex
7
7
  'NL','AT','PL','PT','RO','SI','SK','FI','SE','UK']
8
8
 
9
9
  attr_accessor :order_nr, :customer_name, :country_code,
10
- :date, :status, :shipping_cost, :total, :total_0, :total_7, :total_19
10
+ :date, :status, :shipping_cost, :total, :total_0, :total_7, :total_19, :has_problems
11
11
 
12
12
  def initialize values={}
13
13
  @total_0, @total_7, @total_19, @total = 0, 0, 0, 0
@@ -21,6 +21,7 @@ module Magelex
21
21
  @status = values.delete(:status) || nil
22
22
  @shipping_cost = values.delete(:shipping_cost) || nil
23
23
  @country_code = values.delete(:country_code) || nil
24
+ @has_problems = false
24
25
  if !values.empty?
25
26
  raise "Unknown values for bill: #{values.inspect}"
26
27
  end
@@ -30,16 +31,18 @@ module Magelex
30
31
  @country_code == 'CH'
31
32
  end
32
33
 
33
- def add_item amount, tax, name
34
- case TaxGuess.guess(amount, tax)
35
- when :tax0
36
- @total_0 += amount
37
- when :tax7
38
- @total_7 += amount
39
- when :tax19
40
- @total_19 += amount
41
- else
42
- raise 'Unknown Tax class'
34
+ def add_item amount, tax, name, discount=0
35
+ begin
36
+ case TaxGuess.guess(amount, tax)
37
+ when :tax0
38
+ @total_0 += amount.round(2)# - discount.round(0)
39
+ when :tax7
40
+ @total_7 += amount.round(2)# - discount.round(0)
41
+ when :tax19
42
+ @total_19 += amount.round(2)# - discount.round(0)
43
+ end
44
+ rescue
45
+ @has_problems = true
43
46
  end
44
47
  end
45
48
 
@@ -52,11 +55,19 @@ module Magelex
52
55
  end
53
56
 
54
57
  def check
55
- @total.round(2) == (@total_0 + @total_7 + @total_19).round(2)
58
+ @has_problems == false && @total > 0 && @total.round(2) == (@total_0.round(2) + @total_7.round(2) + @total_19.round(2)).round(2)
59
+ end
60
+
61
+ def self.floor2 value
62
+ (value * 100).to_i / 100.0
56
63
  end
57
64
 
58
65
  def consume_shipping_cost
59
- @total_19 += @shipping_cost * 1.19
66
+ if swiss?
67
+ @total_0 += LexwareBill.floor2(@shipping_cost)
68
+ else
69
+ @total_19 += LexwareBill.floor2(@shipping_cost * 1.19)
70
+ end
60
71
  @shipping_cost = 0
61
72
  end
62
73
 
@@ -7,15 +7,14 @@ module Magelex
7
7
  end
8
8
  end
9
9
 
10
- def self.to_rows bill
11
- # main
10
+ def self.to_split_rows bill
12
11
  rows = []
13
12
  rows << [bill.date.strftime("%d.%m.%Y"),
14
- bill.order_nr,
15
- bill.customer_name,
16
- bill.total.round(2),
17
- Magelex::AccountNumber.for_customer(bill),
18
- 0]
13
+ bill.order_nr,
14
+ bill.customer_name,
15
+ bill.total.round(2),
16
+ Magelex::AccountNumber.for_customer(bill),
17
+ 0]
19
18
  # subs
20
19
  [:total_0, :total_7, :total_19].each do |part|
21
20
  if (amount = bill.send(part)) != 0
@@ -32,6 +31,27 @@ module Magelex
32
31
  rows
33
32
  end
34
33
 
34
+ def self.to_single_row bill
35
+ tax_kind = [:total_0, :total_7, :total_19].detect{|t| bill.send(t) > 0}
36
+
37
+ [[bill.date.strftime("%d.%m.%Y"),
38
+ bill.order_nr,
39
+ bill.customer_name,
40
+ bill.total.round(2),
41
+ Magelex::AccountNumber.for_customer(bill),
42
+ Magelex::AccountNumber.for(bill, tax_kind)
43
+ ]]
44
+ end
45
+
46
+ def self.to_rows bill
47
+ # split-booking needed?
48
+ if [:total_0, :total_7, :total_19].map{|t| bill.send(t)}.count{|i| i > 0} > 1
49
+ to_split_rows bill
50
+ else
51
+ to_single_row bill
52
+ end
53
+ end
54
+
35
55
  def self.render bills
36
56
  CSV.generate(encoding: 'utf-8') do |csv|
37
57
  bills.each do |b|
@@ -1,3 +1,3 @@
1
1
  module Magelex
2
- VERSION = "0.1.1".freeze
2
+ VERSION = "0.1.2".freeze
3
3
  end
data/lib/magelex.rb CHANGED
@@ -15,4 +15,50 @@ module Magelex
15
15
  def self.logger= logger
16
16
  @logger = logger
17
17
  end
18
+
19
+ def self.process_file in_file, out_file, options
20
+ bills_export = []
21
+ # Import/Read file.
22
+ bills = Magelex::MagentoCSV.read in_file
23
+ bills.each do |bill|
24
+ if !bill.complete?
25
+ Magelex.logger.info("Skip order #{bill.order_nr} (incomplete: #{bill.status})")
26
+ else # complete!
27
+ bill.consume_shipping_cost
28
+ bill.swissify
29
+ if !bill.check
30
+ Magelex.logger.info("Skip order #{bill.order_nr}#{bill.swiss? ? ' (swiss)' : ''}")
31
+ Magelex.logger.info(" (totals do not match #{bill.total} != "\
32
+ "(0: #{bill.total_0} + 7: #{bill.total_7} "\
33
+ "+ 19: #{bill.total_19} "\
34
+ "= #{bill.total_0 + bill.total_7 + bill.total_19})")
35
+ else
36
+ Magelex.logger.debug("Handle #{bill.order_nr}")
37
+ bills_export << bill
38
+ end
39
+ end
40
+ end
41
+
42
+ # Fix dates via database.
43
+ if !options[:skipdb]
44
+ begin
45
+ Magelex.logger.info("Fetching dates from magento mysql.")
46
+ Magelex::MagentoMYSQL.update_dates YAML.load_file('magelex.conf'), bills_export
47
+ rescue => e
48
+ Magelex.logger.error("Could not connect to MySQL database, exiting.")
49
+ Magelex.logger.error(e.inspect)
50
+ exit 2
51
+ end
52
+ else
53
+ Magelex.logger.debug("Skip fetching dates from magento mysql.")
54
+ end
55
+
56
+ # Export/Write to file
57
+ if File.exist?(out_file)
58
+ Magelex.logger.error("Output file #{out_file} exists already, exiting.")
59
+ exit 3
60
+ end
61
+ Magelex.logger.info("Writing to #{out_file}")
62
+ Magelex::LexwareCSV.write out_file, bills_export
63
+ end
18
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magelex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Wolfsteller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-08 00:00:00.000000000 Z
11
+ date: 2016-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2