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