]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons/reciept.rb
24948b55fa0a4fc1e31ccb2a08ea8fa0f33a9253
[gestion-adh.git] / lib / nos_oignons / reciept.rb
1 #-*- coding: utf-8 -*-
2
3 require 'prawn'
4 require 'prawn/measurement_extensions'
5
6 module NosOignons
7   class Reciept
8     attr_reader :member, :amount
9
10     def initialize(member, amount)
11       @member = member
12       unless member.up_to_date?
13         raise ArgumentError.new("membership fee has not been paid for this year")
14       end
15       @amount = amount
16       @now = Time.now
17     end
18
19     def filename
20       dir = ENV['NOS_OIGNONS_RECIEPTS_DIR'] || 'reciepts'
21       "#{dir}/reciept-#{@member.member_id}-#{@member.membership_fee_paid_on.strftime("%Y-%m-%d")}.pdf"
22     end
23
24     WINDOW_LEFT = 110.mm
25     WINDOW_TOP = 50.mm
26     WINDOW_RIGHT = 25.mm
27     WINDOW_BOTTOM = 80.mm
28     WINDOW_WIDTH = 85.mm
29     BODY_LEFT = 25.mm
30     BODY_WIDTH = 210.mm - (BODY_LEFT * 2)
31     FOOTER_HEIGHT = 25.mm
32     DARK_LOGO_COLOR = '57075f'
33     FONT_PATH = File.expand_path("../../../share/fonts/OpenSans/OpenSans.ttf", __FILE__)
34     LOGO_PATH = File.expand_path("../../../share/images/nos-oignons.png", __FILE__)
35
36     def generate_header(pdf)
37       header_height = pdf.bounds.height / 6
38       pdf.bounding_box([0, pdf.cursor], :width => pdf.bounds.width, :height => header_height) do
39         logo_width = 2 * pdf.bounds.width / 3
40         pdf.bounding_box([0, pdf.bounds.top], :width => logo_width, :height => pdf.bounds.height) do
41           pdf.image LOGO_PATH, :width => pdf.bounds.width, :align => :center, :vposition => :center
42         end
43         pdf.bounding_box([logo_width, pdf.bounds.top], :width => pdf.bounds.width / 3, :height => pdf.bounds.height) do
44           pdf.text 'Nœuds de sortie Tor financés par la communauté', :align => :center, :valign => :center
45         end
46       end
47       pdf.bounding_box([WINDOW_LEFT - pdf.bounds.absolute_left, pdf.bounds.absolute_top - WINDOW_TOP], :width => WINDOW_WIDTH, :height => WINDOW_BOTTOM - WINDOW_TOP) do
48         pdf.text member.name + "\n" + member.address, :valign => :bottom
49       end
50     end
51
52     def generate_body(pdf)
53       body_height = pdf.cursor - (pdf.bounds.height / 6)
54       pdf.move_down 30
55       pdf.bounding_box([BODY_LEFT, pdf.cursor], :width => BODY_WIDTH, :height => body_height) do
56         pdf.font_size(16) do
57           pdf.pad(25) do
58             pdf.text "Objet : reçu de cotisation"
59           end
60           margin = pdf.bounds.width / 6
61           pdf.pad_bottom(30) do
62             pdf.bounding_box([WINDOW_LEFT - pdf.bounds.absolute_left, pdf.cursor], :width => WINDOW_WIDTH) do
63               pdf.text "Le #{@now.strftime("%d/%m/%Y")}"
64             end
65           end
66           pdf.text <<-EOT.gsub(/^            /, '').gsub(/\n/, ' ')
67             Nous avons bien enregistré la cotisation annuelle de #{member.name}
68             à l'association Nos oignons reçue à la date du
69             #{member.membership_fee_paid_on.strftime("%d/%m/%Y")} pour un montant
70             de #{amount}€.
71           EOT
72           pdf.pad_top(30) do
73             pdf.bounding_box([margin, pdf.cursor], :width => 5 * margin) do
74               pdf.text "Le Conseil d'Administration"
75             end
76           end
77         end
78       end
79     end
80
81     def generate_footer(pdf)
82       pdf.bounding_box([0, FOOTER_HEIGHT + pdf.bounds.bottom], :width => pdf.bounds.width, :height => FOOTER_HEIGHT) do
83         pdf.pad_bottom(5) do
84           pdf.stroke_color DARK_LOGO_COLOR
85           pdf.dash 5
86           pdf.stroke_horizontal_rule
87         end
88         pdf.font_size(10) do
89           pdf.column_box([0, pdf.cursor], :width => pdf.bounds.width, :columns => 2) do
90             pdf.text NosOignons::CONTACT_INFO, :align => :center, :valign => :center
91             pdf.bounds.move_past_bottom
92             pdf.text NosOignons::POSTAL_ADDRESS, :align => :center, :valign => :center
93           end
94         end
95       end
96     end
97
98     def create!
99       raise "File exists!" if File.exists?(filename)
100       Prawn::Document.generate(filename) do |pdf|
101         pdf.font(FONT_PATH) do
102           generate_header(pdf)
103           generate_body(pdf)
104           generate_footer(pdf)
105         end
106       end
107     end
108   end
109 end