Ajout de l'option compte par défaut pour la liste des comptes bancaires : ce choix...
[bilio:bilio.git] / src / compte.rb
1 # coding: utf-8
2
3 class Compte_box < Gtk::VBox
4
5         def initialize window, dial=false
6         
7                 super(false, 3)
8                 
9                 set_border_width 10
10                 
11                 @id = 0
12                 
13                 @window = window
14                 @login = window.login
15                 @dial = dial
16                 
17                 @valider = Gtk::Button.new
18                 hboxvalider = Gtk::HBox.new false, 2
19                 hboxvalider.add Gtk::Image.new( "./resources/icons/ok.png" )
20                 hboxvalider.add Gtk::Label.new "Valider"
21                 @valider.add hboxvalider
22                 @annuler = Gtk::Button.new
23                 hboxannuler = Gtk::HBox.new false, 2
24                 hboxannuler.add Gtk::Image.new( "./resources/icons/cancel.png" )
25                 hboxannuler.add Gtk::Label.new "Annuler"
26                 @annuler.add hboxannuler
27                 
28                 agencement
29                 
30                 @valider.signal_connect( "clicked" ) { validate }
31                 @annuler.signal_connect( "clicked" ) { quit }
32         
33         end
34         
35         def agencement
36         
37                 vbox = Gtk::VBox.new false, 3
38                 @frame = Gtk::Frame.new
39                 @frame.label = "Nouveau compte"
40                 vbox.pack_start( @frame, true, true, 3 )
41                 
42                 vbox_corps = Gtk::VBox.new false, 3
43                 @frame.add vbox_corps
44                 vbox_corps.pack_start( notebook, true, true, 3 )                
45                 
46                 hbox3 = Gtk::HBox.new false, 2
47                 align1 = Gtk::Alignment.new 1, 1, 0, 0
48                 hbox3.pack_start( @annuler, true, true, 3 )
49                 hbox3.pack_start( @valider, true, true, 3 )
50                 align1.add hbox3
51                 
52                 vbox.pack_start( align1, false, false, 3 ) unless @dial
53                 
54                 self.add vbox
55         
56         end
57         
58         def notebook
59         
60                 @note = Gtk::TextView.new
61                 @ged = Ged_box.new @window, "comptes"
62                 
63                 # Conteneurs
64                 notebook = Gtk::Notebook.new
65                 scroll_note = Gtk::ScrolledWindow.new
66
67                 # Propriétés des objets
68                 @note.wrap_mode = Gtk::TextTag::WRAP_WORD
69                 scroll_note.set_policy Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC
70                 scroll_note.shadow_type = Gtk::SHADOW_NONE
71                 
72                 # Agencement du conteneur Note
73                 scroll_note.add @note
74                 
75                 # Agencement du notebook
76                 notebook.append_page infos, Gtk::Label.new("Informations")
77                 notebook.append_page ecritures, Gtk::Label.new("Ecritures")
78                 notebook.append_page scroll_note, Gtk::Label.new("Notes")
79                 notebook.append_page @ged, Gtk::Label.new("GED")
80
81                 # Renvoie
82                 notebook
83                 
84         end
85         
86         def infos
87                 # Objets        
88                 @designation = Gtk::Entry.new
89                 @banque = Gtk::Entry.new
90                 @code_banque = Gtk::Entry.new
91                 @code_guichet = Gtk::Entry.new
92                 @num_compte = Gtk::Entry.new
93                 @cle_rib = Gtk::Entry.new
94                 @bic = Gtk::Entry.new
95                 @iban = Gtk::Entry.new
96                 @domiciliation = Gtk::Entry.new
97                 @proprietaire = Gtk::Entry.new
98                 @adresse_proprietaire = Gtk::TextView.new
99                 @site = Gtk::Entry.new
100                 @solde_ouverture = Gtk::SpinButton.new 0, 9999999999, 1
101                 visite_site = Gtk::Button.new
102                 debloque_solde = Gtk::Button.new
103                 @defaut = Gtk::CheckButton.new
104                 
105                 # Conteneurs
106                 table_info = Gtk::Table.new 2, 4, false
107                 scroll_info = Gtk::ScrolledWindow.new
108                 hbox_compte = Gtk::HBox.new false, 2
109                 
110                 # Les labels
111                 label_designation = Gtk::Alignment.new 0, 0.5, 0, 0
112                 label_designation.add Gtk::Label.new "Désignation du compte :"
113                 label_banque = Gtk::Alignment.new 0, 0.5, 0, 0
114                 label_banque.add Gtk::Label.new "Nom de la banque :"
115                 label_site = Gtk::Alignment.new 0, 0.5, 0, 0
116                 label_site.add Gtk::Label.new "Site web :"
117                 label_rib = Gtk::Alignment.new 0, 0.5, 0, 0
118                 label_rib.add Gtk::Label.new "RIB :"
119                 label_code_banque = Gtk::Alignment.new 0, 0.5, 0, 0
120                 label_code_banque.add Gtk::Label.new "Code banque :"
121                 label_code_guichet = Gtk::Alignment.new 0, 0.5, 0, 0
122                 label_code_guichet.add Gtk::Label.new "Code guichet :"
123                 label_num_compte = Gtk::Alignment.new 0, 0.5, 0, 0
124                 label_num_compte.add Gtk::Label.new "Numéro de compte :"
125                 label_cle_rib = Gtk::Alignment.new 0, 0.5, 0, 0
126                 label_cle_rib.add Gtk::Label.new "Clé :"
127                 label_bic = Gtk::Alignment.new 0, 0.5, 0, 0
128                 label_bic.add Gtk::Label.new "BIC :"
129                 label_iban = Gtk::Alignment.new 0, 0.5, 0, 0
130                 label_iban.add Gtk::Label.new "IBAN :"
131                 label_domiciliation = Gtk::Alignment.new 0, 0.5, 0, 0
132                 label_domiciliation.add Gtk::Label.new "Domiciliation :"
133                 label_proprietaire = Gtk::Alignment.new 0, 0.5, 0, 0
134                 label_proprietaire.add Gtk::Label.new "Propriétaire du compte:"
135                 label_solde_ouverture = Gtk::Alignment.new 0, 0.5, 0, 0
136                 label_solde_ouverture.add Gtk::Label.new "Solde à l'ouverture du compte:"
137                 label_defaut = Gtk::Alignment.new 0, 0.5, 0, 0
138                 label_defaut.add Gtk::Label.new "Compte par défaut:"
139                 
140                 # Propriétés des objets
141                 scroll_info.set_policy Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC
142                 scroll_info.shadow_type = Gtk::SHADOW_NONE
143                 visite_site.image = Gtk::Image.new "resources/icons/internet.png"
144                 visite_site.signal_connect("clicked") { naviguer unless @site.text.empty? }
145                 visite_site.tooltip_text = "Visiter le site"
146                 debloque_solde.image = Gtk::Image.new "resources/icons/lock.png"
147                 debloque_solde.signal_connect("clicked") { @solde_ouverture.sensitive = !@solde_ouverture.sensitive? }
148                 debloque_solde.tooltip_text = "Dé-vérouiller le solde"
149                 @code_banque.width_chars = 5
150                 @code_guichet.width_chars = 5
151                 @num_compte.width_chars = 11
152                 @cle_rib.width_chars = 2
153                 @solde_ouverture.digits = 2
154                 @solde_ouverture.sensitive = false
155                 
156                 # Agencement du conteneur Informations
157                 i=0
158                 table_info.attach( label_designation , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
159                 table_info.attach( @designation, 1, 3, i, i+1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 5, 5 )
160                 
161                 i+=1
162                 table_info.attach( label_banque , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
163                 table_info.attach( @banque, 1, 3, i, i+1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 5, 5 )
164                 
165                 i+=1
166                 table_info.attach( label_site , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
167                 table_info.attach( @site, 1, 2, i, i+1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 5, 5 )
168                 table_info.attach( visite_site, 2, 3, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
169                 
170                 i+=1
171                 hbox_compte.pack_start label_code_banque, false, false, 2
172                 hbox_compte.pack_start @code_banque, false, false, 2
173                 hbox_compte.pack_start label_code_guichet, false, false, 2
174                 hbox_compte.pack_start @code_guichet, false, false, 2
175                 hbox_compte.pack_start label_num_compte, false, false, 2
176                 hbox_compte.pack_start @num_compte, false, false, 2
177                 hbox_compte.pack_start label_cle_rib, false, false, 2
178                 hbox_compte.pack_start @cle_rib, false, false, 2
179                 table_info.attach( label_rib , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
180                 table_info.attach( hbox_compte , 1, 3, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
181                 
182                 i+=1
183                 table_info.attach( label_bic , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
184                 table_info.attach( @bic, 1, 3, i, i+1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 5, 5 )
185                 
186                 i+=1
187                 table_info.attach( label_iban , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
188                 table_info.attach( @iban, 1, 3, i, i+1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 5, 5 )
189                 
190                 i+=1
191                 table_info.attach( label_domiciliation , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
192                 table_info.attach( @domiciliation, 1, 3, i, i+1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 5, 5 )
193                 
194                 i+=1
195                 table_info.attach( label_proprietaire , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
196                 table_info.attach( @proprietaire, 1, 3, i, i+1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 5, 5 )
197                 
198                 i+=1
199                 table_info.attach( label_solde_ouverture , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
200                 table_info.attach( @solde_ouverture, 1, 2, i, i+1, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 5, 5 )
201                 table_info.attach( debloque_solde, 2, 3, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
202                 
203                 i+=1
204                 table_info.attach( label_defaut , 0, 1, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
205                 table_info.attach( @defaut, 1, 2, i, i+1, Gtk::FILL, Gtk::FILL, 5, 5 )
206                 
207                 scroll_info.add_with_viewport table_info
208                 
209                 scroll_info
210         end
211         
212         def ecritures
213         
214                 # list_store                    (id,     date, , valeur , type,  numero  note    tiers    débit   credit  solde   pointage)
215                 @list_store = Gtk::ListStore.new(Integer, String, String, String, String, String, String, String, String, String, String)
216                 @view = Gtk::TreeView.new(@list_store)
217                 @view.signal_connect ("row-activated") { |view, path, column|
218                                  
219                 }
220                 
221                 # Create a renderer with the background property set
222                 renderer_left = Gtk::CellRendererText.new
223                 renderer_left.xalign = 0
224                 renderer_left.yalign = 0
225                 
226                 # Create a renderer with the background property set
227                 renderer_right = Gtk::CellRendererText.new
228                 renderer_right.xalign = 1
229                 renderer_right.yalign = 0
230                 
231                 col = Gtk::TreeViewColumn.new("Date", renderer_left, :text => 1)
232                 col.resizable = true
233                 @view.append_column(col)
234                 
235                 renderer_date = Gtk::CellRendererText.new
236                 renderer_date.xalign = 0
237                 renderer_date.yalign = 0
238                 renderer_date.mode = Gtk::CellRendererText::MODE_EDITABLE
239                 renderer_date.editable = true
240                 renderer_date.signal_connect('edited') { |combo, data, text|
241                         if (!text.empty? and !text.eql?(@list_store.get_iter(data)[2])) then
242                                 begin
243                                         date = Date.parse(text).strftime("%Y-%m-%d")
244                                         @list_store.get_iter(data)[2] = text
245                                         p = Paiement.find(@list_store.get_iter(data)[0])
246                                         p.date_comptable = date
247                                         p.save
248                                         remplir_ecritures
249                                 rescue
250                                 end
251                         end
252                 }  
253                 col = Gtk::TreeViewColumn.new("Valeur", renderer_date, :text => 2)
254                 col.resizable = true
255                 @view.append_column(col)
256                 
257                 col = Gtk::TreeViewColumn.new("Type", renderer_left, :text => 3)
258                 col.resizable = true
259                 @view.append_column(col)
260                 
261                 col = Gtk::TreeViewColumn.new("Numéro", renderer_left, :text => 4)
262                 col.resizable = true
263                 @view.append_column(col)
264                 
265                 col = Gtk::TreeViewColumn.new("Note", renderer_left, :text => 5)
266                 col.resizable = true
267                 col.expand = true
268                 @view.append_column(col)
269                 
270                 col = Gtk::TreeViewColumn.new("Tiers", renderer_left, :text => 6)
271                 col.resizable = true
272                 @view.append_column(col)
273                 
274                 renderer_red = Gtk::CellRendererText.new
275                 renderer_red.background = "#ffe7e7"
276                 col = Gtk::TreeViewColumn.new("Débit", renderer_red, :text => 7)
277                 col.resizable = true
278                 @view.append_column(col)
279                 
280                 renderer_green = Gtk::CellRendererText.new
281                 renderer_green.background = "#e7ffe7"
282                 col = Gtk::TreeViewColumn.new("Crédit", renderer_green, :text => 8)
283                 col.resizable = true
284                 @view.append_column(col)
285                 
286                 col = Gtk::TreeViewColumn.new("Solde", renderer_right, :text => 9)
287                 col.resizable = true
288                 @view.append_column(col)
289                 
290                 renderer_pointage = Gtk::CellRendererText.new
291                 renderer_pointage.xalign = 0
292                 renderer_pointage.yalign = 0
293                 renderer_pointage.mode = Gtk::CellRendererText::MODE_EDITABLE
294                 renderer_pointage.editable = true
295                 renderer_pointage.signal_connect('edited') { |combo, data, text|
296                         if (!text.empty? and !text.eql?(@list_store.get_iter(data)[10])) then
297                                 @list_store.get_iter(data)[10] = text
298                                 p = Paiement.find(@list_store.get_iter(data)[0])
299                                 p.pointage = text
300                                 p.save
301                         end
302                 }
303                 col = Gtk::TreeViewColumn.new("Pointage", renderer_pointage, :text => 10)
304                 col.resizable = true
305                 @view.append_column(col)
306                 
307                 scroll = Gtk::ScrolledWindow.new
308         scroll.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC)
309         scroll.add @view
310         
311         scroll
312         
313         end
314         
315         def refresh id=0
316         
317                 @id = id
318                 @designation.text = ""
319                 @banque.text = ""
320                 @code_banque.text = ""
321                 @code_guichet.text = ""
322                 @num_compte.text = ""
323                 @cle_rib.text = ""
324                 @bic.text = ""
325                 @iban.text = ""
326                 @domiciliation.text = ""
327                 @proprietaire.text = ""
328                 @adresse_proprietaire.buffer.text = ""
329                 @site.text = ""
330                 
331                 if id>0 then
332                         
333                         @compte = Compte.find(id)
334                         if @compte then
335                                 @frame.label = @compte.designation.to_s
336                                 @designation.text = @compte.designation.to_s
337                                 @banque.text = @compte.banque.to_s
338                                 @code_banque.text = @compte.code_banque.to_s
339                                 @code_guichet.text = @compte.code_guichet.to_s
340                                 @num_compte.text = @compte.num_compte.to_s
341                                 @cle_rib.text = @compte.cle_rib.to_s
342                                 @bic.text = @compte.bic.to_s
343                                 @iban.text = @compte.iban.to_s
344                                 @domiciliation.text = @compte.domiciliation.to_s
345                                 @proprietaire.text = @compte.proprietaire.to_s
346                                 @adresse_proprietaire.buffer.text = @compte.adresse_proprietaire.to_s
347                                 @site.text = @compte.site.to_s
348                                 @solde_ouverture.value = @compte.solde_ouverture.to_f
349                                 @defaut.active = @compte.defaut
350                         else
351                                 @window.message_erreur tiers.error
352                         end
353                         
354                 else
355                         @compte = Compte.new
356                         @frame.label = "Nouveau Compte"
357                         @defaut.active = false
358                 end
359                 
360                 @ged.refresh @id
361                 remplir_ecritures
362         
363         end
364         
365         def remplir_ecritures
366                 # list_store                    (id,     date, , valeur , type,  numero  note    tiers    débit   credit  solde pointage)
367                 @list_store.clear
368                 paiements = Paiement.includes(:tiers, :paiementtype).where(:compte_id => @id, :suppr => false).order(:date_comptable)
369                 solde = @solde_ouverture.value.to_f
370                 paiements.each do |p|
371                         iter = @list_store.append
372                         iter[0] = p.id
373                         iter[1] = p.date_paiement.strftime("%d/%m/%Y")
374                         iter[2] = p.date_comptable.strftime("%d/%m/%Y")
375                         iter[3] = p.paiementtype.designation
376                         iter[4] = p.numero
377                         iter[5] = p.note
378                         iter[6] = p.tiers_id.nil? ? "-" : p.tiers.nom
379                         iter[7] = p.fournisseur ? ("%.2f €" % p.montant) : ""
380                         iter[8] = !p.fournisseur ? ("%.2f €" % p.montant) : ""
381                         solde = p.fournisseur ? solde-p.montant : solde+p.montant
382                         iter[9] = "%.2f €" % solde
383                         iter[10] = p.pointage.to_s
384                 end
385                         
386         end
387         
388         def validate quitter=true
389                 
390                 res = false
391                 if !@designation.text.empty? then
392                         @compte.designation = @designation.text
393                         @compte.banque = @banque.text
394                         @compte.code_banque = @code_banque.text
395                         @compte.code_guichet = @code_guichet.text
396                         @compte.num_compte = @num_compte.text
397                         @compte.cle_rib = @cle_rib.text
398                         @compte.bic = @bic.text
399                         @compte.iban = @iban.text
400                         @compte.domiciliation = @domiciliation.text
401                         @compte.proprietaire = @proprietaire.text
402                         @compte.adresse_proprietaire = @adresse_proprietaire.buffer.text
403                         @compte.site = @site.text
404                         @compte.solde_ouverture = @solde_ouverture.value.to_f
405                         @compte.defaut = @defaut.active?
406                         
407                         Compte.update_all(:defaut => false) if @defaut.active?
408                                                 
409                         res = @compte.save
410                 
411                         if res then
412                                 quit if (!@dial and quitter)
413                         else
414                                 @window.message_erreur res.error
415                         end
416                 else
417                         @window.message_erreur "Vous devez saisir une désignation pour ce compte !"
418                         res = false
419                 end
420                 return res
421         end
422         
423         def focus
424                 @annuler.grab_focus
425         end
426         
427         def naviguer
428                 
429                 if !@site.text.include?("http://")
430                         @site.text = "http://" + @site.text
431                 end
432                 
433                 if RUBY_PLATFORM.include?("linux") then 
434                         system "xdg-open #{@site.text}" 
435                 else
436                         if RUBY_PLATFORM.include?("mingw") then 
437                                 # TODO
438                         else
439                                 
440                         end
441                 end
442         end
443         
444         def quit
445         
446                 @window.liste_compte.refresh
447                 @window.affiche @window.liste_compte
448         
449         end
450         
451 end