sunat_books 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rvmrc +1 -0
  4. data/.travis.yml +2 -5
  5. data/README.mkd +9 -3
  6. data/lib/sunat_books.rb +6 -6
  7. data/lib/sunat_books/common_utils.rb +9 -0
  8. data/lib/sunat_books/csv/base.rb +63 -0
  9. data/lib/sunat_books/csv/option_error.rb +8 -0
  10. data/lib/sunat_books/pdf/base.rb +134 -0
  11. data/lib/sunat_books/pdf/buys.rb +36 -0
  12. data/lib/sunat_books/pdf/count_sum.rb +24 -0
  13. data/lib/{books → sunat_books/pdf}/diary_entries.rb +1 -3
  14. data/lib/{books → sunat_books/pdf}/layouts/buys.yml +0 -0
  15. data/lib/{books → sunat_books/pdf}/layouts/sales.yml +0 -0
  16. data/lib/{books → sunat_books/pdf}/locale.rb +0 -0
  17. data/lib/{books → sunat_books/pdf}/locales/es.yml +0 -0
  18. data/lib/sunat_books/pdf/page.rb +48 -0
  19. data/lib/{books → sunat_books/pdf}/pages_utils.rb +1 -1
  20. data/lib/sunat_books/pdf/sales.rb +35 -0
  21. data/lib/sunat_books/pdf/simplified_diary.rb +94 -0
  22. data/lib/sunat_books/pdf/trading_book.rb +50 -0
  23. data/lib/{books → sunat_books/pdf}/utils.rb +4 -6
  24. data/lib/sunat_books/ple/base.rb +76 -0
  25. data/lib/sunat_books/ple/buys.rb +38 -0
  26. data/lib/{ple_books → sunat_books/ple}/layouts/buys.yml +0 -0
  27. data/lib/{ple_books → sunat_books/ple}/layouts/sales.yml +0 -0
  28. data/lib/sunat_books/ple/sales.rb +22 -0
  29. data/sunat_books.gemspec +2 -2
  30. data/test/{csv_books → csv}/base_test.rb +8 -5
  31. data/test/{books → pdf}/base_test.rb +0 -0
  32. data/test/pdf/buys_test.rb +27 -0
  33. data/test/{books → pdf}/page_test.rb +4 -4
  34. data/test/{books → pdf}/pages_utils_test.rb +32 -2
  35. data/test/pdf/sales_test.rb +15 -0
  36. data/test/{books → pdf}/utils_test.rb +7 -1
  37. data/test/{ple_books_test.rb → ple/base_test.rb} +2 -2
  38. data/test/{ple_books → ple}/buys_test.rb +8 -8
  39. metadata +38 -35
  40. data/Gemfile.lock +0 -83
  41. data/lib/books/base.rb +0 -132
  42. data/lib/books/buys.rb +0 -72
  43. data/lib/books/count_sum.rb +0 -22
  44. data/lib/books/page.rb +0 -44
  45. data/lib/books/sales.rb +0 -73
  46. data/lib/books/simplified_diary.rb +0 -97
  47. data/lib/csv_books/base.rb +0 -67
  48. data/lib/csv_books/option_error.rb +0 -6
  49. data/lib/ple_books/base.rb +0 -75
  50. data/lib/ple_books/buys.rb +0 -36
  51. data/lib/ple_books/sales.rb +0 -20
  52. data/test/books/buys_test.rb +0 -57
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
- require_relative "pages_utils"
5
-
6
- module Books
7
- class Buys < Base
8
- include PagesUtils
9
-
10
- def initialize(company, tickets, view, month, year)
11
- # company => an object that respond to ruc and name methods
12
- # tickets => an array of objects that respond to a layout's methods
13
- # view => a view context
14
- # month => a number that represent a month
15
- # year => a number that represent a year
16
- super(page_layout: :landscape, margin: [5], page_size: "A4")
17
- @view = view
18
- @company = company
19
- @period = get_period(month, year)
20
- @tickets = tickets
21
- @book_name = self.class.name.downcase.sub("books::", "")
22
- dir = File.dirname(__FILE__)
23
- @blayout = YAML.load_file("#{dir}/layouts/#{@book_name}.yml")
24
- @page_max = 27
25
-
26
- prawn_book
27
- end
28
-
29
- def prawn_book
30
- prawn_header "REGISTRO DE COMPRAS", @period, @company
31
- @pages = []
32
-
33
- bounding_box([bounds.left + 3, bounds.top - 45], width: 800,
34
- height: 530) do
35
- setup_pages(@pages, @tickets.length, @page_max)
36
- book_body
37
- end
38
- end
39
-
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)]
48
- end
49
-
50
- def book_body
51
- move_down 5
52
- data = []
53
- fields = @blayout["headers"]
54
- data << table_head(fields, @book_name, @blayout)
55
- if @tickets.length.positive?
56
- row_data(data, @blayout["widths"], @blayout["align"], fields, "buys")
57
- else
58
- not_moviment_page(data)
59
- end
60
- render_prawn_table(data)
61
- end
62
-
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]
69
- end
70
- end
71
- end
72
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Books
4
- class CountSum
5
- def initialize(count_number, initial_value = BigDecimal(0))
6
- @sum = initial_value
7
- @count_number = count_number
8
- end
9
-
10
- def add(value)
11
- @sum += value
12
- end
13
-
14
- def count
15
- @count_number
16
- end
17
-
18
- def total
19
- @sum
20
- end
21
- end
22
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Books
6
- class Page < Base
7
- attr_accessor :length, :bi_sum, :igv_sum, :total_sum, :non_taxable, :data
8
-
9
- def initialize(page_number, length)
10
- @page_number = page_number
11
- @length = length
12
- @bi_sum = BigDecimal(0)
13
- @igv_sum = BigDecimal(0)
14
- @total_sum = BigDecimal(0)
15
- @non_taxable = BigDecimal(0)
16
- @data = []
17
- end
18
-
19
- def update_data_buys(ticket)
20
- @bi_sum += ticket.taxable_to_taxable_export_bi.round(2)
21
- @igv_sum += ticket.taxable_to_taxable_export_igv.round(2)
22
- @total_sum += ticket.total_operation_buys.round(2)
23
- @non_taxable += ticket.non_taxable unless ticket.non_taxable.nil?
24
- end
25
-
26
- def update_data_sales(ticket)
27
- @bi_sum += ticket.taxable_bi.round(2)
28
- @igv_sum += ticket.igv.round(2)
29
- @total_sum += ticket.total_operation_sales.round(2)
30
- end
31
-
32
- def update_fields(fields = nil, source = nil)
33
- # update fields from a given source
34
- return if source.nil?
35
- fields&.each do |field|
36
- begin
37
- send("#{field}=", source.send(field))
38
- rescue
39
- return nil
40
- end
41
- end
42
- end
43
- end
44
- end
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
- require_relative "pages_utils"
5
-
6
- module Books
7
- class Sales < Base
8
- include PagesUtils
9
-
10
- def initialize(company, tickets, view, month, year)
11
- # company => an object that respond to ruc and name methods
12
- # tickets => an array of objects that respond to a layout's methods
13
- # view => a view context
14
- # month => a number that represent a month
15
- # year => a number that represent a year
16
- super(page_layout: :landscape, margin: [5], page_size: "A4")
17
- @view = view
18
- @company = company
19
- @period = get_period(month, year)
20
- @tickets = tickets
21
- @book_name = self.class.name.downcase.sub("books::", "")
22
- dir = File.dirname(__FILE__)
23
- @blayout = YAML.load_file("#{dir}/layouts/#{@book_name}.yml")
24
- @page_max = 29
25
-
26
- prawn_book
27
- end
28
-
29
- def prawn_book
30
- prawn_header "REGISTRO DE VENTAS", @period, @company
31
- @pages = []
32
-
33
- bounding_box([bounds.left + 3, bounds.top - 45], width: 800,
34
- height: 530) do
35
- setup_pages(@pages, @tickets.length, @page_max)
36
- book_body
37
- end
38
- end
39
-
40
- def book_body
41
- move_down 5
42
- data = []
43
- fields = @blayout["headers"]
44
- data << table_head(fields, @book_name, @blayout)
45
-
46
- if @tickets.length.positive?
47
- row_data(data, @blayout["widths"], @blayout["align"], fields, "sales")
48
- else
49
- not_moviment_page(data)
50
- end
51
-
52
- render_prawn_table(data)
53
- end
54
-
55
- def render_prawn_table(data)
56
- widths_columns = { 0 => 22, 1 => 35, 2 => 30, 5 => 27, 6 => 37, 8 => 20,
57
- 9 => 33, 10 => 27, 11 => 35, 12 => 29 }
58
-
59
- table(data, header: true,
60
- cell_style: { borders: [], size: 5, align: :right },
61
- column_widths: widths_columns) do
62
- row(0).borders = %i[bottom top]
63
- end
64
- end
65
-
66
- def final_row(foot_line_text, page)
67
- [{ content: foot_line_text, colspan: 5 }, zero,
68
- formated_number(page.bi_sum), make_sub_table([zero, zero], 22), zero,
69
- formated_number(page.igv_sum), zero,
70
- formated_number(page.total_sum)]
71
- end
72
- end
73
- end
@@ -1,97 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
- require_relative "count_sum"
5
- require_relative "diary_entries"
6
- require_relative "pages_utils"
7
-
8
- module Books
9
- class SimplifiedDiary < Base
10
- include DiaryEntries
11
- include PagesUtils
12
- def initialize(company, tickets, view, month, year)
13
- super(page_layout: :landscape, margin: [5], page_size: "A4")
14
- @view = view
15
- @company = company
16
- @tickets = tickets
17
- # @book_name = self.class.name.downcase.sub("books::", "")
18
- # dir = File.dirname(__FILE__)
19
- # @blayout = YAML.load_file("#{dir}/layouts/#{@book_name}.yml")
20
- @main_title = "LIBRO DIARIO - FORMATO SIMPLIFICADO"
21
- @counts = get_mother_counts @tickets
22
- @total_sums = @counts.map { |count| CountSum.new(count) }
23
-
24
- prawn_book(month, year)
25
- end
26
-
27
- def prawn_book(month, year)
28
- (month.to_i..12).each do |m|
29
- start_new_page unless m == month.to_i
30
- period = get_period(m, year)
31
-
32
- x = bounds.left + 3
33
- y = bounds.top - 10
34
- bounding_box([x, y], width: 815, height: 510) do
35
- book_body m, year, 20, period
36
- end
37
- end
38
- end
39
-
40
- def book_body(month, year, max_column = nil, period = nil)
41
- tickets = @tickets.where(period_month: month, period_year: year)
42
-
43
- # header
44
- data = []
45
- initial_day = get_date(year.to_i, month.to_i, 1)
46
- draw_table_header(tickets, @counts, @total_sums, data, initial_day)
47
-
48
- period_date = get_date(year, month, -1)
49
- entries_data(tickets, @counts, @total_sums, data, period_date)
50
-
51
- book_header period, @company.ruc, @company.name, @main_title
52
- draw_table_body(data, max_column, period)
53
- end
54
-
55
- def not_moviment_data(data)
56
- data << [{ content: "SIN MOVIMIENTO EN EL PERIODO", colspan: 5 }]
57
- end
58
-
59
- def entries_data(tickets, counts, total_sums, data, period_date)
60
- return not_moviment_data(data) if tickets.empty?
61
- sales_entry(tickets, counts, total_sums, data, period_date)
62
- buys_entry(tickets, counts, total_sums, data, period_date)
63
- other_entry(tickets, counts, total_sums, data)
64
- close_entry(tickets, counts, total_sums, data)
65
- total_entry(total_sums, data)
66
- end
67
-
68
- def draw_table_header(tickets, counts, total_sums, data, date)
69
- data << ["FECHA", "OPERACIÓN", counts].flatten
70
-
71
- # body
72
- initial_data = initial_entry(tickets, counts, total_sums)
73
- data << [date, "ASIENTO INICIAL DEL PERIODO", initial_data].flatten
74
- end
75
-
76
- def draw_table_body(data, max_column, period)
77
- return render_prawn_table(data) unless data.first.count > max_column
78
-
79
- pages = split_data(data, max_column)
80
-
81
- pages.each do |page|
82
- prawn_new_page(period) unless page.page_number.zero?
83
- render_prawn_table(page.data)
84
- end
85
- end
86
-
87
- def prawn_new_page(period)
88
- start_new_page
89
- book_header period, @company.ruc, @company.name, @main_title
90
- end
91
-
92
- def render_prawn_table(data)
93
- table(data, header: true, cell_style: { borders: [], size: 6 },
94
- column_widths: { 1 => 73 })
95
- end
96
- end
97
- end
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "csv"
4
- require_relative "option_error"
5
-
6
- module CsvBooks
7
- class Base
8
- attr_accessor :file
9
-
10
- def initialize(tickets, options = {})
11
- # options
12
- # - layout => Array of strings used to get data for csv
13
- # - filename
14
- if options[:layout].nil?
15
- raise CsvBooks::OptionError.new(msg: "Layout option is required")
16
- end
17
- filename = options[:filename] || "#{tmp_path}book.csv"
18
- fields = options[:layout]
19
- get_file(filename, fields, tickets)
20
- end
21
-
22
- def get_file(filename, fields, tickets)
23
- send("file=", filename)
24
- File.exist?(filename) ? File.delete(filename) : nil
25
- FileUtils.touch(filename)
26
- append_headers(filename, fields)
27
- append_data(tickets, filename, fields)
28
- end
29
-
30
- def tmp_path
31
- dir = File.dirname(__FILE__)
32
- tmp_path = "#{dir}/tmp/"
33
- Dir.mkdir(tmp_path) unless Dir.exist?(tmp_path)
34
- tmp_path
35
- end
36
-
37
- def append_headers(filename, fields)
38
- append_to_csv(filename, fields, "w+")
39
- end
40
-
41
- def append_data(tickets, filename, fields)
42
- tickets&.each do |ticket|
43
- data = []
44
- fields&.each do |field|
45
- data << field_value(ticket, field)
46
- end
47
- append_to_csv(filename, data, "a+")
48
- end
49
- end
50
-
51
- def field_value(ticket, field)
52
- begin
53
- value = ticket.send(field)
54
- rescue
55
- value = ""
56
- end
57
- value
58
- end
59
-
60
- def append_to_csv(filename, data, mode)
61
- return if data.nil?
62
- CSV.open(filename, mode) do |csv|
63
- csv << data
64
- end
65
- end
66
- end
67
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CsvBooks
4
- class OptionError < StandardError
5
- end
6
- end
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: false
2
-
3
- require "csv"
4
-
5
- module PleBooks
6
- class Base
7
- attr_accessor :file
8
-
9
- def ple_book_name(uid, ruc, month, year, *args)
10
- code = book_code(uid)
11
- code_oportunity = "00" # TODO: case for 'inventarios y balances'
12
- operations_state = args[0] || 1 # 0, 1, 2
13
- content = args[1] || 1 # 1 ,0
14
- currency = args[2] || 1 # 1, 2
15
- name = "LE#{ruc}#{year}#{month}00#{code}#{code_oportunity}"
16
- name << "#{operations_state}#{content}#{currency}1"
17
- end
18
-
19
- def book_code(uid)
20
- dir = File.dirname(__FILE__)
21
- path = "#{dir}/book_codes.csv"
22
- code = ""
23
- CSV.foreach(path) do |row|
24
- if row[0] == uid
25
- code = row[2]
26
- break
27
- end
28
- end
29
- code
30
- end
31
-
32
- def path
33
- dir = File.dirname(__FILE__)
34
- tmp_path = "#{dir}/tmp/"
35
- Dir.mkdir(tmp_path) unless Dir.exist?(tmp_path)
36
- tmp_path
37
- end
38
-
39
- def get_file(tickets, fields, filename)
40
- FileUtils.touch(filename.to_s)
41
-
42
- send("file=", filename)
43
-
44
- tickets.each_with_index do |ticket, i|
45
- ticket_data = get_value(fields, ticket)
46
-
47
- mode = (i.zero? ? "w+" : "a+")
48
- File.open(filename.to_s, mode) do |txt|
49
- txt.puts(ticket_data)
50
- end
51
- end
52
- end
53
-
54
- def get_value(fields, ticket)
55
- data = ""
56
- fields.each do |field|
57
- begin
58
- value = ticket.send(field)
59
- rescue
60
- value = ""
61
- end
62
- data << "#{value}|"
63
- end
64
- data
65
- end
66
-
67
- def check_layout(options, fields)
68
- options[:layout]&.each do |key, value|
69
- i = fields.index(key.to_s)
70
- fields.delete(key.to_s)
71
- fields.insert(i, value)
72
- end
73
- end
74
- end
75
- end