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