Correction pour prendre en compte le logo de la société
[bilio:bilio.git] / src / edition.rb
1 # coding: utf-8
2
3 class Edition < Prawn::Document
4         
5         def initialize id, chemin_temp
6         
7                 super(  :page_layout => :portrait,
8                                 :right_margin => 15,
9                                 :page_size => 'A4'
10                          )
11                 
12                 @societe = Societe.find(:all).first 
13                 @document = Document.includes(:tiers).find(id)
14                 @documents_lignes = Documentligne.includes(:tva).where(:document_id => id).order(:id)
15                 @tva = Tva.find(:all)
16         
17                 c = en_tete
18                 corps c
19                 
20                 rendu chemin_temp
21         
22         end
23         
24         def en_tete
25                 
26                 pas = 10
27                 text_size = 10
28                 top = bounds.top-10
29                 
30                 #image "resources/images/logo2.png", :width => 280, :at => [0,top+40]
31                 top -= pas+35
32                 
33                 draw_text "Emetteur :", :at => [0,top], :size => 8, :styles => [:bold]
34                 draw_text "Adressé à :", :at => [250,top], :size => 8, :styles => [:bold]
35                 
36                 top -= pas
37                 
38                 fill_color "eeeeee"
39                 stroke_color "aaaaaa"
40                 rectangle [0,top+5], 230, 110
41                 fill
42                 rectangle [250,top+5], 290, 110
43                 stroke
44                 
45                 fill_color "000000"
46                 
47                 text_size = 10
48                 
49                 repeat :all do
50                         # header
51                         bounding_box [bounds.left, bounds.top+30], :width  => bounds.width do
52                                 image "resources/images/logo_societe.png", :width => 280 if File.exist?("resources/images/logo_societe.png")
53                         end
54                         formatted_text_box([{ :text => "#{@document.documenttype.designation.capitalize}\n",
55                                                           :size =>      text_size+4,
56                                                           :styles => [:bold]
57                                                         },
58                                             { :text => "Réf. : #{@document.ref}\n",
59                                                           :size =>      text_size+4,
60                                                           :styles => [:bold]
61                                                         },
62                                                         { :text => "Date #{@document.documenttype.designation.downcase} : #{@document.date_document.strftime("%d/%m/%Y")}\n",
63                                               :size => text_size
64                                             },
65                                             { :text => "Date échéance : #{@document.date_echeance.strftime("%d/%m/%Y")}\n",
66                                               :size => text_size
67                                             }],
68                                             { :at => [350,bounds.top+20],
69                                               :width => bounds.right-350,
70                                               :align => :right }
71                                             )   
72                                                 
73         end
74                 
75                 formatted_text_box([{ :text => @societe.nom+"\n",
76                                                           :size =>      text_size+1,
77                                                           :styles => [:bold]
78                                                         },
79                                             { :text => "#{@societe.adresse}\n\n",
80                                               :size => text_size,
81                                             },
82                                             { :text => "Téléphone: #{@societe.tel} - Fax: #{@societe.fax}\n",
83                                               :size => text_size,
84                                             },
85                                             { :text => "Email: #{@societe.mail}\n",
86                                               :size => text_size,
87                                             },
88                                             { :text => "Web: #{@societe.site}\n",
89                                               :size => text_size,
90                                             }],
91                                             {:at => [7,top]}
92                                             )
93                 
94                 formatted_text_box([{ :text => "#{@document.tiers.nom}\n\n",
95                                                           :size =>      text_size+1,
96                                                           :styles => [:bold]
97                                                         },
98                                             { :text => "#{@document.tiers.adresse}\n",
99                                               :size => text_size,
100                                             },
101                                             { :text => "#{@document.tiers.cp} #{@document.tiers.ville}",
102                                               :size => text_size,
103                                             }],
104                                             {:at => [257,top]}
105                                             )
106
107                 top = top-120
108                 
109                 if !@document.note.empty? then
110                         rectangle [0,top+4], 540, 15
111                         stroke
112                         draw_text @document.note, :at => [5,top-6], :size => 8
113                         return true
114                 else
115                         return false
116                 end
117         
118         end
119         
120         def corps note=false
121                 
122                 res_array = []
123                 remise = false
124                 @documents_lignes.each do |ligne|
125                         remise = true if ligne["remise"].to_i>0
126                 end
127                 @documents_lignes.each do |ligne|
128                         l = []
129                         des = ""
130                         des = ligne["designation"].to_s.gsub("\r", "") unless ligne["designation"].to_s.eql?("-")
131                         des += "\n" unless des.empty? or ligne["commentaires"].to_s.empty?
132                         des += ligne["commentaires"] unless ligne["commentaires"].to_s.empty?
133                         des += "\n" unless ligne["lot"].to_s.empty?
134                         des += "N° de lot = #{ligne["lot"]}" unless ligne["lot"].to_s.empty?
135
136                         l << des
137                         l << ligne.tva.designation
138                         l << "%.2f €" % ligne["pu"]
139                         l << (ligne["qtite"]*ligne["colisage"]).to_i
140                         l << (ligne["remise"].to_i>0 ? (ligne["remise"].to_i.eql?(100) ? "Offert !" : "%.2f%" % ligne["remise"]) : "" ) if remise
141                         l << "%.2f €" % ligne["total_ligne"]
142                         res_array << l
143                 end
144                 i = note ? 12 : 10
145                 i.times do text("  ") end
146                 data = [[]]
147                 #data[0] << "Code"
148                 data[0] << "Désignation"
149                 data[0] << "TVA"
150                 data[0] << "P.U. HT"
151                 data[0] << "Qté"
152                 data[0] << "Remise %" if remise
153                 data[0] << "Total HT"
154                 data += res_array
155                 
156                 taille_colonnes = []
157                 taille_colonnes << (remise ? 290 : 340)
158                 taille_colonnes << 50
159                 taille_colonnes << 50
160                 taille_colonnes << 30
161                 taille_colonnes << 50 if remise
162                 taille_colonnes << 70
163                 
164                 t = table(data) do |table|
165                         table.header = true
166                         table.row(0).style :borders => [:top, :bottom, :left, :right]
167                         table.row(0).style :align => :center
168                         table.row(1..data.count).style :borders => [:left, :right]
169                         #table.row(data.count-1).style :borders => [:left, :right, :bottom]
170                         table.cell_style = { :size => 8, :border_color => "aaaaaa"}
171                         table.column(1..5).align = :right
172                         table.row(0).style :align => :center
173                         table.column(0).align = :left
174                         table.column_widths = taille_colonnes
175                 end 
176                 
177                 top = cursor
178                 
179                 left = bounds.left
180                 bottom = 200
181                 if top<bottom then
182                         bottom = 14
183                 end
184                 line [left,top], [left,bottom]
185                 taille_colonnes.each { |i|
186                         left += i
187                         line [left,top], [left,bottom]
188                 }
189                 line [bounds.left,bottom], [left,bottom]
190                 stroke
191                 
192                 if bottom<200 then
193                         start_new_page
194                 end
195                 
196                 pieds @documents_lignes
197         
198         end
199         
200         def pieds res
201
202                 ht = 0.0
203                 tva = 0.0
204                 
205                 tva_tab = []
206                 @tva.each { |t|
207                         tva_tab << {:id=>t['id'],:designation=>t['designation'], :taux=>t['taux'].to_f, :np=>t['np'], :ht=>0.0}
208                 }
209
210                 res.each { |ligne|
211                         ht += ligne["total_ligne"].to_f unless ligne["total_ligne"].nil?
212                         tva += ligne["total_ligne"].to_f*(ligne.tva.taux.to_f/100) unless ligne["total_ligne"].nil? or ligne.tva_id.nil? or ligne.tva.np
213                         tva_tab.each { |t|
214                                 t[:ht] += ligne["total_ligne"].to_f if t[:id].eql?(ligne.tva_id)
215                         }
216                 }
217                 
218                 ht = ht.round(2)
219                 tva = tva.round(2)      
220                 ttc = (ht+tva).round(2) 
221                 
222                 top = 200
223                 text_size = 8
224                 
225                 data = []
226                 
227                 data << ["Total HT", "%.2f €" % ht]
228                 
229                 tva_tab.each { |t|
230                         texte = "Total TVA #{t[:taux]}% "
231                         texte += (t[:np] ? "(Non perçue réc.)" : "")
232                         montant = t[:ht]*t[:taux]/100
233                         data << [texte, "%.2f €" % montant.round(2)] if montant>0
234                 }               
235                 
236                 row_ttc = data.count
237                 data << ["Total TTC", "%.2f €" % ttc]
238                 
239                 if @document.montant_regle>0
240                         data << ["Payé", "%.2f €" % @document.montant_regle]
241                         data << ["Reste à payer", "%.2f €" % (ttc-@document.montant_regle)]
242                 end
243                 
244                 bounding_box([310,top], :width => 300, :height => 200) do
245                         
246                         table(data, :cell_style => { :borders => [], :padding => 1, :size => text_size+2 }) do |table|
247                                 table.column(0).align = :left
248                                 table.column(1).align = :right
249                                 table.row(row_ttc).style :background_color => "eeeeee"
250                                 table.row(data.count-1).style :background_color => "eeeeee"
251                                 table.column(0).width = 150
252                                 table.column(1).width = 80
253                         end 
254                 end
255                 
256                 # Liste des paiements déjà effectués
257                 if @document.montant_regle>0 
258                         paiements = Paiementdocument.where(:document_id => @document.id)
259                         data = [["Date", "Mode", "Numéro", "Montant"]]
260                         paiements.each { |p|
261                                 data << [p.paiement.date_paiement.strftime("%d/%m/%Y"), p.paiement.paiementtype.designation, p.paiement.numero, "%.2f €" % p.montant]
262                         }
263
264                         bounding_box([310,top-90], :width => 300) do    
265                                 text "Règlements déjà effectués :", :size => text_size, :style => :bold
266                                 table(data, :cell_style => { :borders => [], :padding => 1, :size => text_size-2 }) do |table|
267                                         table.row(0).font_style = :bold
268                                         table.column(0).width = 50
269                                         table.column(1).width = 50
270                                         table.column(2).width = 60
271                                         table.column(3).width = 60
272                                 end
273                         end
274                 end
275                 
276                 # Condition et mode de règlement
277                 compte = Compte.first
278                 
279                 formatted_text_box([{ :text => "Condition de règlement: #{@document.conditionpaiement.designation}\n\n",
280                                                           :size =>      text_size,
281                                                           :styles => [:bold]
282                                                         },
283                                             { :text => "Règlement par chèque à l'ordre de #{@societe.nom}\n",
284                                               :size => text_size,
285                                               :styles => [:bold]
286                                             },
287                                             { :text => "envoyé au:\n",
288                                               :size => text_size,
289                                               :styles => [:bold]
290                                             },
291                                             { :text => "#{@societe.adresse}\n\n",
292                                               :size => text_size
293                                             },
294                                             { :text => "Règlement par virement sur le compte bancaire suivant:\n",
295                                               :size => text_size,
296                                               :styles => [:bold]
297                                             },
298                                             { :text => "Banque: #{compte.banque}\n",
299                                               :size => text_size-2
300                                             }],
301                                             {:at => [0,top-5]}
302                                             )
303                 
304                 data = [["Code Banque", "Code guichet", "Numéro compte", "Clé RIB"]]
305                 data << [compte.code_banque, compte.code_guichet, compte.num_compte, compte.cle_rib]
306                 
307                 top = top - 97
308                 
309                 bounding_box([0,top], :width => 250, :height => 50) do
310                         table(data, :cell_style => { :borders => [:left, :right], :padding => 1, :size => text_size-2 }) do |table|
311                                 table.column(0..3).align = :center
312                                 table.column(0..1).width = 50
313                                 table.column(2).width = 60
314                                 table.column(3).width = 30
315                         end
316                 end
317                 
318                 formatted_text_box([{ :text => "Domiciliation: #{compte.domiciliation}\n\n",
319                                                           :size =>      text_size-2
320                                                         },
321                                             { :text => "Code IBAN: #{compte.iban}\n",
322                                               :size => text_size-2
323                                             },
324                                             { :text => "Code BIC/SWIFT: #{compte.bic}\n",
325                                               :size => text_size-2
326                                             }],
327                                             {:at => [0,top-25]}
328                                             )
329                 
330         end
331         
332         def rendu chemin_temp
333         
334                 number_pages "#{@societe['status']} - SIRET: #{@societe['siret']}", {:start_count_at => 1, :at => [0, 10], :align => :center, :size => 8}
335                 number_pages "NAF, ex APE: #{@societe['naf']} - RCS/RM: #{@societe['rcs']}", {:start_count_at => 1, :at => [0, 0], :align => :center, :size => 8}
336                 number_pages "<page>/<total>", {:start_count_at => 1, :at => [bounds.right - 100, 0], :align => :right, :size => 8}
337                 render_file "#{chemin_temp}/#{@document.ref}.pdf"
338         
339         end
340         
341 end