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