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