Ajour des Fiches d'intervention + corrections de divers bugs
[bilio:bilio.git] / src / liste_documents.rb
1 # coding: utf-8
2
3 class ListeDocuments_box < Gtk::VBox
4
5         def initialize window
6         
7                 super(false, 3)
8                 
9                 set_border_width 10
10                 
11                 @window = window
12                 @login = window.login
13                 
14                 @type_document = 1
15                 
16                 agencement
17         
18         end
19         
20         def agencement
21         
22                 vbox = Gtk::VBox.new false, 3
23                 
24                 add_new = Gtk::Button.new
25                 hboxajout = Gtk::HBox.new false, 2
26                 hboxajout.add Gtk::Image.new( "./resources/icons/add.png" )
27                 @label_button = Gtk::Label.new "Nouveau"
28                 hboxajout.add @label_button
29                 add_new.add hboxajout
30                 
31                 print = Gtk::Button.new
32                 hbox_print = Gtk::HBox.new false, 2
33                 hbox_print.add Gtk::Image.new( "./resources/icons/print.png" )
34                 label_print = Gtk::Label.new "Imprimer"
35                 hbox_print.add label_print
36                 print.add hbox_print
37                 
38                 hb = Gtk::HBox.new false, 2
39                 hb.add print
40                 hb.add add_new
41                 
42                 align_button = Gtk::Alignment.new 1, 0, 0, 0
43                 align_button.add hb
44                 
45                 table_pieds = Gtk::Table.new 1, 3, false
46                 @compteur = Gtk::Label.new
47                 @total_ht = Gtk::Label.new
48                 @total_ttc = Gtk::Label.new
49                 table_pieds.attach( @compteur, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 5, 5 )
50                 table_pieds.attach( @total_ht, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL, 5, 5 )
51                 table_pieds.attach( @total_ttc, 2, 3, 0, 1, Gtk::FILL, Gtk::FILL, 5, 5 )
52                 
53                 hbox = Gtk::HBox.new false, 3
54                 @search = Gtk::Entry.new
55                 @search.primary_icon_stock = Gtk::Stock::FIND
56                 @search.secondary_icon_stock = Gtk::Stock::CLEAR
57                 hbox.pack_start @search, true, true, 3
58                 
59                 @filtre = Gtk::ComboBox.new
60                 hbox.pack_start Gtk::Label.new("Filtres :"), false, false, 3
61                 hbox.pack_start @filtre, false, false, 3
62                 
63                 hbox.add align_button
64                 
65                 vbox.pack_start hbox, false, false, 3
66                 vbox.pack_start tableau_documents, true, true, 3 
67                 vbox.pack_start table_pieds, false, false, 3
68                 @frame = Gtk::Frame.new
69                 @frame.add vbox
70                 
71                 add_new.signal_connect( "clicked" ) {
72                         @window.document.refresh 0, @type_document
73                         @window.affiche @window.document
74                         @window.document.focus
75                 }
76                 
77                 print.signal_connect( "clicked" ) {
78                         imprimer
79                 }
80                 
81                 @filtre.signal_connect('changed') {
82                         changement_filtre
83                 }
84                 
85                 @search.signal_connect('activate') {
86                         if !@search.text.empty? 
87                                 refresh_docs @type_document, @search.text
88                         else
89                                 refresh_docs @type_document, nil
90                         end
91                         @filtre.active = -1
92                 }
93                 
94                 @search.signal_connect('icon-press') { |widget, pos, event|
95                         if !@search.text.empty?
96                                 @search.text = ""
97                                 refresh_docs @type_document, nil
98                         end
99                 }
100                 
101                 add @frame
102         
103         end
104         
105         def tableau_documents
106         
107                 @pix_paye = Gdk::Pixbuf.new("resources/icons/paye.png", 16, 16)
108                 @pix_partiel = Gdk::Pixbuf.new("resources/icons/partiel.png", 16, 16)
109                 @pix_impaye = Gdk::Pixbuf.new("resources/icons/impaye.png", 16, 16)
110                 @pix_vide = Gdk::Pixbuf.new("resources/icons/empty.png", 16, 16)
111                 
112                 # list_store (id, client, date_document, note, type_document, date_livraison, auteur, montant_ht, montant_ttc, ref, date francaise, image, status, httri, ttctri)
113                 @list_store = Gtk::ListStore.new(Integer, String, String, String, String, String, String, String, String, String, String, Gdk::Pixbuf, Integer, Float, Float)
114                 @view = Gtk::TreeView.new(@list_store)
115                 
116                 @view.selection.mode = Gtk::SELECTION_MULTIPLE
117                 
118                 @view.signal_connect ("row-activated") { |view, path, column|
119                         i = nil
120                         @view.selection.selected_each {|model, path, iter| i=iter}
121                         unless i.nil?
122                                 @window.document.refresh @view.model.get_value(i, 0), @type_document
123                                 @window.affiche @window.document                 
124                         end
125                 }
126                 
127                 # Create a renderer with the background property set
128                 renderer_left = Gtk::CellRendererText.new
129                 renderer_left.background = "white"
130                 renderer_left.xalign = 0
131                 renderer_left.yalign = 0
132                 
133                 # Create a renderer with the background property set
134                 renderer_right = Gtk::CellRendererText.new
135                 renderer_right.xalign = 1
136                 renderer_right.yalign = 0
137                 
138                 col = Gtk::TreeViewColumn.new("Référence", renderer_left, :text => 9)
139                 col.sort_column_id = 9
140                 col.resizable = true
141                 @view.append_column(col)
142                 
143                 col = Gtk::TreeViewColumn.new("Tiers", renderer_left, :text => 1)
144                 col.sort_column_id = 1
145                 col.resizable = true
146                 col.expand = true
147                 @view.append_column(col)
148                 
149                 col = Gtk::TreeViewColumn.new("Date du document", renderer_right, :text => 10)
150                 col.sort_column_id = 2
151                 col.resizable = true
152                 @view.append_column(col)
153                 
154                 col = Gtk::TreeViewColumn.new("Montant HT", renderer_right, :text => 7)
155                 col.sort_column_id = 13
156                 col.resizable = true
157                 @view.append_column(col)
158                 
159                 col = Gtk::TreeViewColumn.new("Montant TTC", renderer_right, :text => 8)
160                 col.sort_column_id = 14
161                 col.resizable = true
162                 @view.append_column(col)
163                 
164                 renderer_pix = Gtk::CellRendererPixbuf.new              
165                 col = Gtk::TreeViewColumn.new("", renderer_pix, :pixbuf => 11)
166                 col.sort_column_id = 12
167                 col.resizable = false
168                 @view.append_column(col)
169                 
170                 scroll = Gtk::ScrolledWindow.new
171         scroll.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC)
172         scroll.add @view
173         
174         
175         end
176         
177         def refresh type_document, term=nil, where=nil
178         
179                 @type_document = type_document
180                 
181                 remplir_filtre type_document
182                 
183                 @frame.label = @window.type_doc[@type_document][:nom]
184                 
185                 case type_document
186                         when 1, 3, 6  # Devis et BL
187                                 @label_button.text = "Nouveau "
188                         when 5  # Avoir
189                                 @label_button.text = "Nouvel "
190                         else # Facure et Commande
191                                 @label_button.text = "Nouvelle "
192                 end
193                 
194                 @label_button.text += @window.type_doc[type_document][:nom].downcase 
195                 
196                 refresh_docs type_document, term, where
197                 
198         end
199         
200         def refresh_docs type_document, term=nil, where=nil
201                 docs = Document.includes(:tiers, :documenttype, :utilisateur).where(:documenttype_id => @type_document).order("date_document DESC").order(:ref)
202                 docs = docs.where("documents.id=? OR tiers.nom ILIKE ? OR documents.ref ILIKE ?", term.to_i, "%#{term}%", "%#{term}%") if term
203                 docs = docs.where(where) if where
204                 
205                 remplir docs
206         end
207         
208         def remplir docs
209
210                 totalht = totalttc = 0.0
211                 
212                 @list_store.clear
213                 
214                 docs.each do |d|
215                         iter = @list_store.append
216                         iter[0] = d.id
217                         iter[1] = d.tiers.nom
218                         iter[2] = d.date_document.to_s
219                         iter[3] = d.note
220                         iter[4] = d.documenttype.designation
221                         iter[5] = d.date_livraison.to_s
222                         iter[6] = d.utilisateur.firstname + " " + d.utilisateur.lastname
223                         iter[7] = "%.2f €" % d.montant_ht
224                         totalht += d.montant_ht
225                         iter[8] = "%.2f €" % d.montant_ttc
226                         totalttc += d.montant_ttc
227                         iter[9] = d.ref
228                         iter[10] = d.date_document.strftime("%d/%m/%Y")
229                         if (facture_client?(@type_document) or facture_fournisseur?(@type_document))
230                                 if d.montant_regle.eql?(0.0)
231                                         iter[11] = @pix_impaye
232                                         iter[12] = 0
233                                 elsif d.montant_regle<d.montant_ttc
234                                         iter[11] = @pix_partiel
235                                         iter[12] = 1
236                                 else
237                                         iter[11] = @pix_paye
238                                         iter[12] = 2
239                                 end
240                         else
241                                 iter[11] = @pix_vide
242                         end
243                         iter[13] = d.montant_ht
244                         iter[14] = d.montant_ttc
245                 end
246                 
247                 @compteur.text = "Nombre d'éléments dans la liste : #{docs.count}"
248                 @total_ht.text = "Total HT = #{"%.2f €" % totalht}"
249                 @total_ttc.text = "Total TTC = #{"%.2f €" % totalttc}"
250                         
251         end
252         
253         def remplir_filtre type_doc
254                 @filtre.model.clear
255                 if (facture_client?(type_doc) or facture_fournisseur?(type_doc))
256                         @filtre.append_text "Tout" #0
257                         @filtre.append_text "Impayées" #1
258                         @filtre.append_text "Payées" #2
259                 end
260         end
261         
262         def changement_filtre
263                 @search.text = ""
264                 where = nil
265                 if (facture_client?(@type_document) or facture_fournisseur?(@type_document))
266                         case @filtre.active
267                                 when 0
268                                         where = "2>1"
269                                 when 1
270                                         where = "montant_regle<montant_ttc"
271                                 when 2
272                                         where = "montant_regle>=montant_ttc"
273                         end
274                 end
275                 refresh_docs @type_document, nil, where unless where.nil?
276         end
277         
278         def imprimer
279         
280                 ids = []
281                 @view.selection.selected_each { |model, path, iter|
282                         ids << @view.model.get_value(iter, 0)
283                 }
284                 
285                 if ids.count>0
286                         chemin = @window.config_db.chemin_temp
287                         f = Edition.new ids, chemin
288                         if RUBY_PLATFORM.include?("linux") then 
289                                 system "xdg-open","#{f.fichier}"
290                         else
291                                 if RUBY_PLATFORM.include?("mingw") then 
292                                         system "start","#{f.fichier}"
293                                 else
294                                 
295                                 end
296                         end
297                 end     
298         end
299         
300         def facture_client? type_doc
301                 return type_doc.eql?(4)         
302         end
303         
304         def facture_fournisseur? type_doc
305                 return type_doc.eql?(8)         
306         end
307         
308         def devis_client? type_doc
309                 return type_doc.eql?(1)         
310         end
311         
312         def devis_fournisseur? type_doc
313                 return type_doc.eql?(6)         
314         end
315         
316         def commande_client? type_doc
317                 return type_doc.eql?(2)         
318         end
319         
320         def commande_fournisseur? type_doc
321                 return type_doc.eql?(7)         
322         end
323         
324         def bl_client? type_doc
325                 return type_doc.eql?(3)
326         end
327         
328         def avoir_fournisseur? type_doc
329                 return type_doc.eql?(5)         
330         end
331         
332 end