Lancement de l'assistant quand la base à besoin d'être mise à jour.
[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/logo2.png", :width => 280
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 = ligne["designation"].to_s.gsub("\r", "")
130                         des += "\n" unless des.empty? or ligne["commentaires"].empty?
131                         des += ligne["commentaires"] unless ligne["commentaires"].empty?
132                         des += "\n" unless ligne["lot"].to_s.empty?
133                         des += "N° de lot = #{ligne["lot"]}" unless ligne["lot"].to_s.empty?
134
135                         l << des
136                         l << ligne.tva.designation
137                         l << "%.2f €" % ligne["pu"]
138                         l << (ligne["qtite"]*ligne["colisage"]).to_i
139                         l << (ligne["remise"].to_i>0 ? (ligne["remise"].to_i.eql?(100) ? "Offert !" : "%.2f%" % ligne["remise"]) : "" ) if remise
140                         l << "%.2f €" % ligne["total_ligne"]
141                         res_array << l
142                 end
143                 i = note ? 12 : 10
144                 i.times do text("  ") end
145                 data = [[]]
146                 #data[0] << "Code"
147                 data[0] << "Désignation"
148                 data[0] << "TVA"
149                 data[0] << "P.U. HT"
150                 data[0] << "Qté"
151                 data[0] << "Remise %" if remise
152                 data[0] << "Total HT"
153                 data += res_array
154                 
155                 taille_colonnes = []
156                 taille_colonnes << (remise ? 290 : 340)
157                 taille_colonnes << 50
158                 taille_colonnes << 50
159                 taille_colonnes << 30
160                 taille_colonnes << 50 if remise
161                 taille_colonnes << 70
162                 
163                 t = table(data) do |table|
164                         table.header = true
165                         table.row(0).style :borders => [:top, :bottom, :left, :right]
166                         table.row(0).style :align => :center
167                         table.row(1..data.count).style :borders => [:left, :right]
168                         #table.row(data.count-1).style :borders => [:left, :right, :bottom]
169                         table.cell_style = { :size => 8, :border_color => "aaaaaa"}
170                         table.column(1..5).align = :right
171                         table.row(0).style :align => :center
172                         table.column(0).align = :left
173                         table.column_widths = taille_colonnes
174                 end 
175                 
176                 top = cursor
177                 
178                 left = bounds.left
179                 bottom = 200
180                 if top<bottom then
181                         bottom = 14
182                 end
183                 line [left,top], [left,bottom]
184                 taille_colonnes.each { |i|
185                         left += i
186                         line [left,top], [left,bottom]
187                 }
188                 line [bounds.left,bottom], [left,bottom]
189                 stroke
190                 
191                 if bottom<200 then
192                         start_new_page
193                 end
194                 
195                 pieds @documents_lignes
196         
197         end
198         
199         def pieds res
200
201                 ht = 0.0
202                 tva = 0.0
203                 
204                 tva_tab = []
205                 @tva.each { |t|
206                         tva_tab << {:id=>t['id'],:designation=>t['designation'], :taux=>t['taux'].to_f, :np=>t['np'], :ht=>0.0}
207                 }
208
209                 res.each { |ligne|
210                         ht += ligne["total_ligne"].to_f unless ligne["total_ligne"].nil?
211                         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
212                         tva_tab.each { |t|
213                                 t[:ht] += ligne["total_ligne"].to_f if t[:id].eql?(ligne.tva_id)
214                         }
215                 }
216                 
217                 ht = ht.round(2)
218                 tva = tva.round(2)      
219                 ttc = (ht+tva).round(2) 
220                 
221                 top = 200
222                 text_size = 8
223                 
224                 data = []
225                 
226                 data << ["Total HT", "%.2f €" % ht]
227                 
228                 tva_tab.each { |t|
229                         texte = "Total TVA #{t[:taux]}% "
230                         texte += (t[:np] ? "(Non perçue réc.)" : "")
231                         montant = t[:ht]*t[:taux]/100
232                         data << [texte, "%.2f €" % montant.round(2)] if montant>0
233                 }               
234                 
235                 row_ttc = data.count
236                 data << ["Total TTC", "%.2f €" % ttc]
237                 
238                 if @document.montant_regle>0
239                         data << ["Payé", "%.2f €" % @document.montant_regle]
240                         data << ["Reste à payer", "%.2f €" % (ttc-@document.montant_regle)]
241                 end
242                 
243                 bounding_box([310,top], :width => 300, :height => 200) do
244                         
245                         table(data, :cell_style => { :borders => [], :padding => 1, :size => text_size+2 }) do |table|
246                                 table.column(0).align = :left
247                                 table.column(1).align = :right
248                                 table.row(row_ttc).style :background_color => "eeeeee"
249                                 table.row(data.count-1).style :background_color => "eeeeee"
250                                 table.column(0).width = 150
251                                 table.column(1).width = 80
252                         end 
253                 end
254                 
255                 # Liste des paiements déjà effectués
256                 if @document.montant_regle>0 
257                         paiements = Paiementdocument.where(:document_id => @document.id)
258                         data = [["Date", "Mode", "Numéro", "Montant"]]
259                         paiements.each { |p|
260                                 data << [p.paiement.date_paiement.strftime("%d/%m/%Y"), p.paiement.paiementtype.designation, p.paiement.numero, "%.2f €" % p.montant]
261                         }
262
263                         bounding_box([310,top-80], :width => 300) do    
264                                 text "Règlements déjà effectués :", :size => text_size, :style => :bold
265                                 table(data, :cell_style => { :borders => [], :padding => 1, :size => text_size-2 }) do |table|
266                                         table.row(0).font_style = :bold
267                                         table.column(0).width = 50
268                                         table.column(1).width = 50
269                                         table.column(2).width = 60
270                                         table.column(3).width = 60
271                                 end
272                         end
273                 end
274                 
275                 # Condition et mode de règlement
276                 compte = Compte.first
277                 
278                 formatted_text_box([{ :text => "Condition de règlement: #{@document.conditionpaiement.designation}\n\n",
279                                                           :size =>      text_size,
280                                                           :styles => [:bold]
281                                                         },
282                                             { :text => "Règlement par chèque à l'ordre de #{@societe.nom}\n",
283                                               :size => text_size,
284                                               :styles => [:bold]
285                                             },
286                                             { :text => "envoyé au:\n",
287                                               :size => text_size,
288                                               :styles => [:bold]
289                                             },
290                                             { :text => "#{@societe.adresse}\n\n",
291                                               :size => text_size
292                                             },
293                                             { :text => "Règlement par virement sur le compte bancaire suivant:\n",
294                                               :size => text_size,
295                                               :styles => [:bold]
296                                             },
297                                             { :text => "Banque: #{compte.banque}\n",
298                                               :size => text_size-2
299                                             }],
300                                             {:at => [0,top-5]}
301                                             )
302                 
303                 data = [["Code Banque", "Code guichet", "Numéro compte", "Clé RIB"]]
304                 data << [compte.code_banque, compte.code_guichet, compte.num_compte, compte.cle_rib]
305                 
306                 top = top - 97
307                 
308                 bounding_box([0,top], :width => 250, :height => 50) do
309                         table(data, :cell_style => { :borders => [:left, :right], :padding => 1, :size => text_size-2 }) do |table|
310                                 table.column(0..3).align = :center
311                                 table.column(0..1).width = 50
312                                 table.column(2).width = 60
313                                 table.column(3).width = 30
314                         end
315                 end
316                 
317                 formatted_text_box([{ :text => "Domiciliation: #{compte.domiciliation}\n\n",
318                                                           :size =>      text_size-2
319                                                         },
320                                             { :text => "Code IBAN: #{compte.iban}\n",
321                                               :size => text_size-2
322                                             },
323                                             { :text => "Code BIC/SWIFT: #{compte.bic}\n",
324                                               :size => text_size-2
325                                             }],
326                                             {:at => [0,top-25]}
327                                             )
328                 
329         end
330         
331         def rendu chemin_temp
332         
333                 number_pages "#{@societe['status']} - SIRET: #{@societe['siret']}", {:start_count_at => 1, :at => [0, 10], :align => :center, :size => 8}
334                 number_pages "NAF, ex APE: #{@societe['naf']} - RCS/RM: #{@societe['rcs']}", {:start_count_at => 1, :at => [0, 0], :align => :center, :size => 8}
335                 number_pages "<page>/<total>", {:start_count_at => 1, :at => [bounds.right - 100, 0], :align => :right, :size => 8}
336                 render_file "#{chemin_temp}/#{@document.ref}.pdf"
337         
338         end
339         
340 end