sunat_books 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e08f90e66a2cb035ea0747c2e90ab0717fe01fdd
4
- data.tar.gz: 66e781fabac6fb3126919c07e661f17f4207b42b
3
+ metadata.gz: 45c52f9a2c0415858d8973ce2cd907c4e1852601
4
+ data.tar.gz: 6314b7690b3b93639de8f958c2b6d49c12b551a0
5
5
  SHA512:
6
- metadata.gz: 51e5c9ae81c79fa4176aa6548f4dbbf11f4ee168e653685a79fb2d273436edf34fce09043265bdb2bb87f68bbb4083939777144f959a4b3b42c3e90356b2e544
7
- data.tar.gz: a5bdeb8c98c009f53f7beff00ed0f7b0228087c9437c438f1aba94e2dedd477a3b7a0f97e94540ba99f5e2853e957a0973597b4b00ef03c544694715fadfde44
6
+ metadata.gz: 3d2c0b476c4d8781cdc4c961d2de5eed2462266b092e0747ba91cf8c42626da35666d6578bb3e126e850c7ddb84771703395191cb99f5a6681f241a7e18f605d
7
+ data.tar.gz: 4048c78841241a490d2b43101ca4cc259d7dd9186fb94e7957afef346a945a6c987e8a97fee7d70561359100cee0d09c1b043af91011673ef9c5f9e90be366f5
data/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
1
  *.swp
2
2
  tmp
3
+ *.gem
4
+ *.csv
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Style/Documentation:
4
+ Enabled: false
5
+ Style/StringLiterals:
6
+ EnforcedStyle: double_quotes
7
+ SupportedStyles:
8
+ - single_quotes
9
+ - double_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ EnforcedStyle: single_quotes
13
+ SupportedStyles:
14
+ - single_quotes
15
+ - double_quotes
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ - 2.3.3
5
+ - 2.3.4
6
+ - 2.4.0
7
+ - 2.4.1
8
+ script:
9
+ - make test
data/Gemfile CHANGED
@@ -1,7 +1,5 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
2
 
3
- ruby '2.3.1'
3
+ source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
-
7
- gem 'pry'
data/Gemfile.lock CHANGED
@@ -2,47 +2,82 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  sunat_books (0.0.1)
5
- activesupport
6
- i18n
7
- prawn
5
+ activesupport (> 4.1)
6
+ i18n (~> 0.7)
7
+ prawn (~> 2.0)
8
+ prawn-table (~> 0.2)
8
9
 
9
10
  GEM
10
11
  remote: https://rubygems.org/
11
12
  specs:
12
- activesupport (5.0.0.1)
13
+ Ascii85 (1.0.2)
14
+ activesupport (5.0.2)
13
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
14
16
  i18n (~> 0.7)
15
17
  minitest (~> 5.1)
16
18
  tzinfo (~> 1.1)
19
+ afm (0.2.2)
20
+ ast (2.3.0)
17
21
  clap (1.0.0)
18
22
  coderay (1.1.1)
19
- concurrent-ruby (1.0.2)
23
+ concurrent-ruby (1.0.5)
20
24
  cutest (1.2.3)
21
25
  clap
22
- i18n (0.7.0)
26
+ faker (1.7.3)
27
+ i18n (~> 0.5)
28
+ hashery (2.1.2)
29
+ i18n (0.8.1)
23
30
  method_source (0.8.2)
24
- minitest (5.9.0)
25
- pdf-core (0.6.1)
26
- prawn (2.1.0)
27
- pdf-core (~> 0.6.1)
28
- ttfunk (~> 1.4.0)
31
+ minitest (5.10.1)
32
+ parser (2.4.0.0)
33
+ ast (~> 2.2)
34
+ pdf-core (0.7.0)
35
+ pdf-inspector (1.2.1)
36
+ pdf-reader (~> 1.0)
37
+ pdf-reader (1.4.1)
38
+ Ascii85 (~> 1.0.0)
39
+ afm (~> 0.2.1)
40
+ hashery (~> 2.0)
41
+ ruby-rc4
42
+ ttfunk
43
+ powerpack (0.1.1)
44
+ prawn (2.2.2)
45
+ pdf-core (~> 0.7.0)
46
+ ttfunk (~> 1.5)
47
+ prawn-table (0.2.2)
48
+ prawn (>= 1.3.0, < 3.0.0)
29
49
  pry (0.10.4)
30
50
  coderay (~> 1.1.0)
31
51
  method_source (~> 0.8.1)
32
52
  slop (~> 3.4)
53
+ rainbow (2.2.2)
54
+ rake
55
+ rake (12.0.0)
56
+ rubocop (0.48.1)
57
+ parser (>= 2.3.3.1, < 3.0)
58
+ powerpack (~> 0.1)
59
+ rainbow (>= 1.99.1, < 3.0)
60
+ ruby-progressbar (~> 1.7)
61
+ unicode-display_width (~> 1.0, >= 1.0.1)
62
+ ruby-progressbar (1.8.1)
63
+ ruby-rc4 (0.1.5)
33
64
  slop (3.6.0)
34
- thread_safe (0.3.5)
35
- ttfunk (1.4.0)
65
+ thread_safe (0.3.6)
66
+ ttfunk (1.5.0)
36
67
  tzinfo (1.2.2)
37
68
  thread_safe (~> 0.1)
69
+ unicode-display_width (1.2.1)
38
70
 
39
71
  PLATFORMS
40
72
  ruby
41
73
 
42
74
  DEPENDENCIES
43
- cutest
44
- pry
75
+ cutest (~> 1.2)
76
+ faker (~> 1.7)
77
+ pdf-inspector (~> 1.2.0)
78
+ pry (~> 0.10)
79
+ rubocop (~> 0.48)
45
80
  sunat_books!
46
81
 
47
82
  BUNDLED WITH
48
- 1.12.5
83
+ 1.14.6
data/Makefile CHANGED
@@ -4,4 +4,5 @@ console:
4
4
  irb -Ilib -rsunat_books
5
5
 
6
6
  test:
7
- cutest test/*.rb
7
+ bundle exec cutest test/*.rb test/**/*.rb
8
+ bundle exec rubocop
data/README.mkd CHANGED
@@ -1,5 +1,8 @@
1
1
  # Sunat Books
2
- A ruby gem for get accounting books for [SUNAT](https://www.sunat.gob.pe)
2
+ [![Gem Version][rubygems-image]][rubygems-url]
3
+ [![Build Status][travis-image]][travis-url]
4
+
5
+ > A ruby gem for get accounting books for [SUNAT](https://www.sunat.gob.pe)
3
6
 
4
7
  ## Install
5
8
  You can install via
@@ -30,8 +33,7 @@ to get the txt file for electronic books
30
33
  ple = PleBooks::Buys.new(ruc, tickets, month, year)
31
34
  ```
32
35
 
33
-
34
- https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/numeric/conversions.rb
35
-
36
- get support for otf font in prawn
37
- https://github.com/prawnpdf/prawn/issues/893
36
+ [rubygems-image]: https://badge.fury.io/rb/sunat_books.svg
37
+ [rubygems-url]: https://badge.fury.io/rb/sunat_books
38
+ [travis-image]: https://travis-ci.org/ccarruitero/sunat_books.svg?branch=master
39
+ [travis-url]: https://travis-ci.org/ccarruitero/sunat_books
data/lib/books/base.rb CHANGED
@@ -1,86 +1,67 @@
1
- require 'prawn'
2
- require 'yaml'
3
- require 'active_support/all'
1
+ # frozen_string_literal: true
2
+
3
+ require "prawn"
4
+ require "prawn/table"
5
+ require "yaml"
6
+ require_relative "utils"
7
+ require_relative "locale"
4
8
 
5
9
  module Books
6
10
  class Base < Prawn::Document
7
- include ActiveSupport::NumberHelper
8
-
9
- MONTHS = { 1 => "Enero", 2 => "Febrero", 3 => "marzo", 4 => "abril",
10
- 5 => "mayo", 6 => "junio", 7 => "julio", 8 => "agosto",
11
- 9 => "setiembre", 10 => "octubre", 11 => "noviembre",
12
- 12 => "diciembre" }
13
-
14
- def formated_number float
15
- number_to_currency(float, unit: '')
16
- end
11
+ include Utils
12
+ include Prawn::Table::Interface
17
13
 
18
- def add_align aligns, options, key
19
- aligns.each do |a|
20
- if a[key] != nil
21
- options[:cell_style] = options[:cell_style].merge({align: a[key][0].to_sym})
22
- end
23
- end
24
- end
25
-
26
- def txt txt
27
- text txt, size: 8
28
- end
29
-
30
- def sub_head hash, book_name, blayout
31
- arr = nil
32
- current_key = nil
33
- column_widths = {}
14
+ def sub_head(hash, book_name, blayout)
15
+ arr, current_key = nil
34
16
  hash.each do |key, value|
35
17
  k = I18n.t("books.#{book_name}.#{key}").mb_chars.upcase.to_s
36
- v = value.collect { |s| I18n.t("books.#{book_name}.#{s}").mb_chars.upcase.to_s}
37
- arr = [[{content: k, colspan: value.length}], v]
18
+ v = value.collect do |s|
19
+ I18n.t("books.#{book_name}.#{s}").mb_chars.upcase.to_s
20
+ end
21
+ arr = [[{ content: k, colspan: value.length }], v]
38
22
  current_key = key
39
23
  end
40
24
 
41
- widths = blayout["widths"]
42
- if !widths.nil?
43
- widths.each do |w|
44
- if w[current_key] != nil
45
- column_widths = w[current_key].flatten
46
- end
47
- end
48
- end
49
- if column_widths.size != 0
50
- multihead = make_table( arr, cell_style: {borders: [], size: 5, align: :center},
51
- column_widths: column_widths) do
52
- cells.padding = 1
53
- end
54
- else
55
- multihead = make_table( arr,
56
- cell_style: {borders: [], size: 5, width: 22, padding: 1, align: :center})
57
- end
25
+ sub_head_table(blayout["widths"], arr, current_key)
26
+ end
27
+
28
+ def sub_head_table(widths, arr, key)
29
+ column_widths = get_column_widths(widths, key)
30
+ options = sub_head_options(column_widths)
31
+ make_table(arr, options)
58
32
  end
59
33
 
60
- def get_period month, year
61
- "#{MONTHS[month.to_i].upcase} #{year}"
34
+ def sub_head_options(column_widths)
35
+ options = { cell_style: {
36
+ borders: [], size: 5, align: :center, padding: 1
37
+ } }
38
+ add_widths(column_widths, options, 22)
39
+ options
62
40
  end
63
41
 
64
- def book_title title
42
+ def book_title(title)
65
43
  text title, align: :center, size: 8
66
44
  end
67
45
 
68
- def book_header period, ruc, name, title=nil
46
+ def book_header(period, ruc, name, title = nil)
69
47
  move_down 5
70
- #txt "PERIODO: #{period}"
71
- #txt "RUC: #{ruc}"
72
- #txt "APELLIDOS Y NOMBRES, DENOMINACIÓN O RAZÓN SOCIAL: #{name.upcase}"
73
- txt "#{name.upcase}"
48
+ txt name.to_s.upcase
74
49
  txt "RUC: #{ruc}"
75
50
  book_title("#{title} - #{period}")
76
51
  move_down 5
77
52
  end
78
53
 
79
- def zero
80
- formated_number(0)
54
+ def prawn_header(title, period, company)
55
+ repeat(:all) do
56
+ canvas do
57
+ bounding_box([bounds.left + 10, bounds.top - 10], width: 800) do
58
+ book_header period, company.ruc, company.name, title
59
+ end
60
+ end
61
+ end
81
62
  end
82
63
 
83
- def table_head fields, book_name, layout
64
+ def table_head(fields, book_name, layout)
84
65
  thead = []
85
66
  fields.each do |h|
86
67
  if h.class == Hash
@@ -93,65 +74,43 @@ module Books
93
74
  thead
94
75
  end
95
76
 
96
- def table_body fields, ticket, widths, aligns
77
+ def table_body(fields, ticket, widths, aligns)
97
78
  tbody = []
98
79
  fields.each do |f|
99
- if f.class == Hash
100
- f.each do |key, value|
101
- v = value.collect do |s|
102
- begin
103
- value = ticket.send(s)
104
- value = formated_number(value) if value.class == BigDecimal
105
- rescue
106
- value = ""
107
- end
108
- value
109
- end
110
- options = {cell_style: {borders: [], size: 5}}
111
- column_widths = nil
112
- if !widths.nil?
113
- widths.each do |w|
114
- if w[key] != nil
115
- column_widths = w[key].flatten
116
- end
117
- end
118
- end
119
- if column_widths != nil
120
- options = options.merge({column_widths: column_widths})
121
- else
122
- options[:cell_style] = options[:cell_style].merge({width: 28})
123
- end
124
- if !aligns.nil?
125
- add_align(aligns, options, key)
126
- end
127
- arr = make_table( [v], options)
128
- tbody << arr
129
- end
80
+ if f.is_a? Hash
81
+ table_hash(f, ticket, tbody, widths, aligns)
130
82
  else
131
- begin
132
- value = ticket.send(f)
133
- rescue
134
- value = ""
135
- end
136
- value = formated_number(value) if value.class == BigDecimal
137
- tbody << value
83
+ tbody << field_value(ticket, f)
138
84
  end
139
85
  end
140
86
  tbody
141
87
  end
142
88
 
89
+ def table_hash(f, ticket, tbody, widths, aligns)
90
+ options = { cell_style: { borders: [], size: 5 } }
91
+
92
+ f.each do |key, value|
93
+ v = value.collect do |s|
94
+ value = field_value(ticket, s)
95
+ end
96
+
97
+ column_widths = get_column_widths(widths, key)
98
+ add_widths(column_widths, options, 28)
99
+ add_align(aligns, options, key) unless aligns.nil?
100
+ tbody << make_table([v], options)
101
+ end
102
+ end
103
+
143
104
  # diary
144
- def get_counts tickets
145
- arr = tickets.map { |t| t.uniq_counts }
146
- arr = arr.flatten.uniq.sort
105
+ def get_counts(tickets)
106
+ tickets.map(&:uniq_counts).flatten.uniq.sort
147
107
  end
148
108
 
149
- def get_mother_counts tickets
150
- arr = tickets.map { |t| t.uniq_mother_counts }
151
- arr = arr.flatten.uniq.sort
109
+ def get_mother_counts(tickets)
110
+ tickets.map(&:uniq_mother_counts).flatten.uniq.sort
152
111
  end
153
112
 
154
- def get_value ticket, count
113
+ def get_value(ticket, count)
155
114
  # active_amount = ticket.get_amount_by_position(count)
156
115
  # pasive_amount = ticket.get_amount_by_position(count, false)
157
116
  active_amount = ticket.get_amount_by_mother_count(count)
@@ -159,54 +118,15 @@ module Books
159
118
  # if count === '401' && ticket.operation_type == 'compras'
160
119
  # amount = amount * (-1)
161
120
  # end
162
- amount = active_amount - pasive_amount
121
+ active_amount - pasive_amount
163
122
  end
164
123
 
165
- def get_row_sums tickets, counts, total_sums
166
- # given an array of counts and tickets get sums by each count
167
- row_counts = get_mother_counts tickets
168
- count_sums = row_counts.map { |count| CountSum.new(count) }
169
-
170
- # get totals
171
- tickets.each do |ticket|
172
- count_sums.each do |count_sum|
173
- count_sum.add get_value(ticket, count_sum.count)
174
- end
175
- end
176
-
177
- # get ordered row
178
- row_data = []
179
- counts.each_with_index do |count, i|
180
- sum_count = nil
181
- count_sums.each do |count_sum|
182
- sum_count = count_sum if count_sum.count == count
183
- end
184
-
185
- if sum_count
186
- value = sum_count.total
187
- else
188
- value = 0
189
- end
190
- total_sums[i].add value
191
- row_data << { content: formated_number(value), align: :right }
192
- end
193
- row_data
194
- end
195
-
196
- def make_sub_table content, width=nil
197
- options = {cell_style: {width: width, size: 5, borders: [], align: :right}}
124
+ def make_sub_table(content, width = nil)
125
+ options = { cell_style: { width: width, size: 5, borders: [],
126
+ align: :right } }
198
127
  content_row = []
199
- content.each {|c| content_row << formated_number(c) }
200
- make_table([content_row],options)
201
- end
202
-
203
- # Utils
204
- def get_date year, month, day
205
- parse_day(Date.new(year.to_i, month.to_i, day))
206
- end
207
-
208
- def parse_day day
209
- day.strftime("%d-%m").to_s
128
+ content.each { |c| content_row << formated_number(c) }
129
+ make_table([content_row], options)
210
130
  end
211
131
  end
212
132
  end
data/lib/books/buys.rb CHANGED
@@ -1,8 +1,11 @@
1
- require 'books/base'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+ require_relative "pages_utils"
2
5
 
3
6
  module Books
4
7
  class Buys < Base
5
- attr_accessor :pages
8
+ include PagesUtils
6
9
 
7
10
  def initialize(company, tickets, view, month, year)
8
11
  # company => an object that respond to ruc and name methods
@@ -15,105 +18,54 @@ module Books
15
18
  @company = company
16
19
  @period = get_period(month, year)
17
20
  @tickets = tickets
18
- @pages = {}
19
21
  @book_name = self.class.name.downcase.sub("books::", "")
20
22
  dir = File.dirname(__FILE__)
21
23
  @blayout = YAML.load_file("#{dir}/layouts/#{@book_name}.yml")
22
24
  @page_max = 27
23
25
 
24
- repeat(:all) do
25
- canvas do
26
- bounding_box([bounds.left + 10, bounds.top - 10], width: 800) do
27
- book_header @period, @company.ruc, @company.name, "REGISTRO DE COMPRAS"
28
- end
29
- end
30
- end
26
+ prawn_book
27
+ end
28
+
29
+ def prawn_book
30
+ prawn_header "REGISTRO DE COMPRAS", @period, @company
31
+ @pages = []
31
32
 
32
- bounding_box([bounds.left + 3, bounds.top - 45], width: 800, height: 530) do
33
+ bounding_box([bounds.left + 3, bounds.top - 45], width: 800,
34
+ height: 530) do
35
+ setup_pages(@pages, @tickets.length, @page_max)
33
36
  book_body
34
37
  end
35
38
  end
36
39
 
37
- def final_row foot_line_text, page
38
- [ {content: foot_line_text, colspan: 5 },
39
- make_sub_table([page[:bi_sum], page[:igv_sum]], 32),
40
- make_sub_table([zero, zero], 25),
41
- make_sub_table([zero, zero], 25),
42
- formated_number(page[:non_taxable]),
43
- zero, zero,
44
- formated_number(page[:total_sum]) ]
40
+ def final_row(foot_line_text, page)
41
+ [{ content: foot_line_text, colspan: 5 },
42
+ make_sub_table([page.bi_sum, page.igv_sum], 32),
43
+ make_sub_table([zero, zero], 25),
44
+ make_sub_table([zero, zero], 25),
45
+ formated_number(page.non_taxable),
46
+ zero, zero,
47
+ formated_number(page.total_sum)]
45
48
  end
46
49
 
47
50
  def book_body
48
- # get total number of tickets
49
- length = @tickets.length
50
- page_num = (length / 27.0).ceil
51
- page_num.times do |i|
52
- pages[i + 1] = {
53
- page_number: i + 1,
54
- length: 0,
55
- bi_sum: BigDecimal(0)
56
- }
57
- end
58
-
59
51
  move_down 5
60
- fields = @blayout["headers"]
61
- widths = @blayout["widths"]
62
- aligns = @blayout["align"]
63
52
  data = []
53
+ fields = @blayout["headers"]
64
54
  data << table_head(fields, @book_name, @blayout)
65
-
66
- n = 1
67
- bi_sum = BigDecimal(0)
68
- igv_sum = BigDecimal(0)
69
- total_sum = BigDecimal(0)
70
- non_taxable = BigDecimal(0)
71
-
72
-
73
- if length > 0
74
- @tickets.each do |ticket|
75
-
76
- if @pages[n][:length] < @page_max
77
- page = @pages[n]
78
- page[:length] += 1
79
- else
80
- data << final_row('VIENEN', @pages[n])
81
-
82
- n += 1
83
- page = @pages[n]
84
- page[:length] += 2
85
- end
86
-
87
- data << table_body(fields, ticket, widths, aligns)
88
-
89
- bi_sum += ticket.taxable_to_taxable_export_bi
90
- igv_sum += ticket.taxable_to_taxable_export_igv
91
- total_sum += ticket.total_operation_buys
92
- non_taxable += ticket.non_taxable unless ticket.non_taxable.nil?
93
- page[:bi_sum] = bi_sum.round(2)
94
- page[:igv_sum] = igv_sum.round(2)
95
- page[:total_sum] = total_sum.round(2)
96
- page[:non_taxable] = non_taxable.round(2)
97
- if page[:length] == @page_max && @tickets.last != ticket
98
- data << final_row('VAN', page)
99
- elsif @tickets.last == ticket
100
- data << final_row('TOTAL', page)
101
- end
102
- end
55
+ if @tickets.length.positive?
56
+ row_data(data, @blayout["widths"], @blayout["align"], fields, "buys")
103
57
  else
104
- data << [content: 'SIN MOVIMIENTO EN EL PERIODO', colspan: 5]
105
- @pages[n] = {}
106
- page = @pages[n]
107
- page[:bi_sum] = zero
108
- page[:igv_sum] = zero
109
- page[:total_sum] = zero
110
- page[:non_taxable] = zero
58
+ not_moviment_page(data)
111
59
  end
60
+ render_prawn_table(data)
61
+ end
112
62
 
113
- table(data, header: true, cell_style: {borders: [], size: 5, align: :right},
114
- column_widths: {0 => 22, 1 => 35, 2 => 30, 8 => 30, 10 => 30,
115
- 9 => 22, 11 => 33, 12 => 33}) do
116
- row(0).borders = [:bottom, :top]
63
+ def render_prawn_table(data)
64
+ table(data, header: true, cell_style: { borders: [], size: 5,
65
+ align: :right },
66
+ column_widths: { 0 => 22, 1 => 35, 2 => 30, 8 => 30,
67
+ 10 => 30, 9 => 22, 11 => 33, 12 => 33 }) do
68
+ row(0).borders = %i[bottom top]
117
69
  end
118
70
  end
119
71
  end
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Books
2
4
  class CountSum
3
- def initialize count_number, initial_value=BigDecimal(0)
5
+ def initialize(count_number, initial_value = BigDecimal(0))
4
6
  @sum = initial_value
5
7
  @count_number = count_number
6
8
  end
7
9
 
8
- def add value
10
+ def add(value)
9
11
  @sum += value
10
12
  end
11
13