aboutsummaryrefslogtreecommitdiffstats
path: root/lib/carlosgoce/layout/layouts.rb
blob: bb033625fc5a0318e57224c38b38243f22681b76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'rubygems'
require 'prawn'
require 'prawn/table'

module CarlosGoce
  module Layout
    class Simple
      def create(file, data, year)
        tables = []
        Prawn::Document.generate(file) do
          data[:months].each do |k, month|
            days = month[:formatted_days]
            cells = days.to_a.each_slice(7).to_a
            a = []
            %w(L M X J V S D).each {|d| a << "<strong>#{d}</strong>" }
            # cells.unshift ['L', 'M', 'X', 'J', 'V', 'S', 'D']
            cells.unshift a
            cells.unshift [{content: "<color rgb='FF0000'><strong>#{month[:name].upcase}</strong></color>", colspan: 7,
                           inline_format: true}]

            tables << make_table(cells,
              cell_style: {width: 15, height: 12, align: :center, size: 7, border_width: 0, padding: 0, valign: :center})
          end

          table(tables.each_slice(3).to_a, cell_style: {align: :center, padding: [5, 5, 5, 5], border_width: 0,
                                                      valign: :center, margin: [0, 0, 15, 0]})
        end
      end
    end

    class Columns
      attr_reader :columns, :num_cols

      def initialize(cols)
        @columns = cols
        @num_cols = cols.length
      end

      def header
        [["Uke", "Dato"] + @columns]
      end

      def create(file, data, year)
        Prawn::Document.generate(file, :page_layout => :landscape, :page_size => "A4") do |pdf|
          data[:months].each do |k, month|
            pdf.font_size = 16
            pdf.text "#{month[:name].capitalize} #{year}"

            rows = []
            offset = 6 - month[:formatted_days].select { |d| d == "" }.length
            days_in_month = month[:days].length

            month[:days].each_index do |i|
              row = []
              bg = (i - offset) % 7 == 0 ? "ffcccc" : "ffffff"

              if (i - offset) % 7 == 1
                remaining_days = days_in_month - i
                span = remaining_days < 7 ? remaining_days : 7
                row << pdf.make_cell(:content => "#{Date.new(year, k, i + 1).cweek}", :rowspan => span, :align => :center)
              elsif i == 0
                row << pdf.make_cell(:content => "#{Date.new(year, k, 1).cweek}", :rowspan => offset + 1, :align => :center)
              end

              row << pdf.make_cell(:content => "#{month[:days][i]} #{month[:days_names][i]}", :background_color => bg)
              @num_cols.times { row << pdf.make_cell(:content => "", :background_color => bg) }
              rows << row
            end

            pdf.font_size = 10

            t = pdf.make_table(header + rows) do
              cells.align = :center
              cells.padding = 2
              cells.border_width = 1

              column(0).width = 30
              column(1).width = 70
              column(1).align = :left
              columns(2..5).width = 160

              row(0).background_color = "cccccc"
              row(0).font_style = :bold
            end

            t.draw()

            pdf.start_new_page if k < 12
          end
        end
      end
    end
  end
end