Multi-tarif et suppression de ligne de document possible
[bilio:bilio.git] / article.rb
1 # coding: utf-8
2
3 class Article < Gtk::VBox
4
5         MAX_SPIN = 999999
6
7         def initialize window
8         
9                 super(false, 3)
10                 
11                 set_border_width 10
12                 
13                 @id = 0
14                 
15                 @window = window
16                 @db = window.db
17                 @login = window.login
18                 
19                 @info1 = Gtk::Entry.new
20                 @info2 = Gtk::Entry.new
21                 @description = Gtk::TextView.new
22                 @datecreation = Gtk::Calendar.new
23                 @dateecheance = Gtk::Calendar.new
24                 @dateecheance.select_month Date.today.month+1, Date.today.year
25                 @valider = Gtk::Button.new Gtk::Stock::OK
26                 @annuler = Gtk::Button.new Gtk::Stock::CANCEL
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 = "Nouvel article"
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( en_tete, 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 )
53                 
54                 self.add vbox
55         
56         end
57         
58         def en_tete
59         
60                 # Objets
61                 @code = Gtk::Entry.new
62                 @gencode = Gtk::Entry.new
63                 @designation = Gtk::Entry.new
64                 @colisage = Gtk::SpinButton.new 0, MAX_SPIN, 1
65                 @tva = Gtk::SpinButton.new 1, MAX_SPIN, 1
66                 @stock = Gtk::SpinButton.new -MAX_SPIN, MAX_SPIN, 1
67                 @note = Gtk::TextView.new
68                 
69                 # Propriétés
70                 @tva.digits = 2
71                 @stock.value = 0 if @id.eql?(0)
72                 
73                 # Agencement            
74                 vbox = Gtk::VBox.new false, 3
75                 
76                 table = Gtk::Table.new 3, 5, false
77                 
78                 table.attach( Gtk::Label.new("Code :"), 0, 1, 0, 1, Gtk::FILL, Gtk::FILL, 5, 5 )
79                 table.attach( @code, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL, 5, 5 )
80                 
81                 table.attach( Gtk::Label.new("Code barre :"), 2, 3, 0, 1, Gtk::FILL, Gtk::FILL, 5, 5 )
82                 table.attach( @gencode, 3, 4, 0, 1, Gtk::FILL, Gtk::FILL, 5, 5 )
83                 
84                 table.attach( Gtk::Label.new("Stock :"), 4, 5, 0, 1, Gtk::FILL, Gtk::FILL, 5, 5 )
85                 table.attach( @stock, 5, 6, 0, 1, Gtk::FILL, Gtk::FILL, 5, 5 )
86                 
87                 table.attach( Gtk::Label.new("Désignation :"), 0, 1, 1, 2, Gtk::FILL, Gtk::FILL, 5, 5 )
88                 table.attach( @designation, 1, 6, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 5, 5 )
89                 
90                 table.attach( Gtk::Label.new("TVA :"), 0, 1, 2, 3, Gtk::FILL, Gtk::FILL, 5, 5 )
91                 table.attach( @tva, 1, 2, 2, 3, Gtk::FILL, Gtk::FILL, 5, 5 )            
92                 
93                 table.attach( Gtk::Label.new("Colisage :"), 2, 3, 2, 3, Gtk::FILL, Gtk::FILL, 5, 5 )
94                 table.attach( @colisage, 3, 4, 2, 3, Gtk::FILL, Gtk::FILL, 5, 5 )               
95                 
96                 vbox.pack_start table, false, false, 3
97                 
98                 hbox = Gtk::HBox.new false, 23
99                 vbox_note = Gtk::VBox.new false, 3
100                 vbox_tarifs = Gtk::VBox.new false, 3
101                 hbox.add vbox_tarifs
102                 hbox.add vbox_note
103                 
104                 align_tarifs = Gtk::Alignment.new 0, 0, 0, 0
105                 align_tarifs.add Gtk::Label.new( "Tarifs :" )
106                 vbox_tarifs.pack_start align_tarifs, false, false, 3
107                 vbox_tarifs.pack_start tableau_tarifs, true, true, 3
108                 
109                 scroll_note = Gtk::ScrolledWindow.new
110                 scroll_note.add @note
111                 scroll_note.set_policy Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC
112                 scroll_note.shadow_type = Gtk::SHADOW_IN
113                 
114                 align_note = Gtk::Alignment.new 0, 0, 0, 0
115                 align_note.add Gtk::Label.new( "Note :" )
116                 
117                 vbox_note.pack_start align_note, false, false, 3
118                 vbox_note.pack_start scroll_note, true, true, 3
119                 
120                 vbox.pack_start hbox, true, true, 3
121                 
122                 vbox
123         
124         end
125         
126         def tableau_tarifs
127         
128                 # list_store (tarif_id, designation_tarif, tarif)
129                 @list_store = Gtk::ListStore.new(Integer, String, Float)
130                 @view = Gtk::TreeView.new(@list_store)
131                 
132                 # Create a renderer with the background property set
133                 renderer_left = Gtk::CellRendererText.new
134                 renderer_left.background = "white"
135                 renderer_left.xalign = 0
136                 
137                 col = Gtk::TreeViewColumn.new("id", renderer_left, :text => 0)
138                 col.sort_column_id = 0
139                 col.resizable = true
140                 @view.append_column(col)
141                 
142                 col = Gtk::TreeViewColumn.new("Type de tarif", renderer_left, :text => 1)
143                 col.sort_column_id = 1
144                 col.resizable = true
145                 col.expand = true
146                 @view.append_column(col)
147                 
148                 col = Gtk::TreeViewColumn.new("Tarif", renderer_left, :text => 2)
149                 col.sort_column_id = 2
150                 col.resizable = true
151                 @view.append_column(col)
152                 
153                 scroll = Gtk::ScrolledWindow.new
154         scroll.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC)
155         scroll.add @view
156         
157         scroll
158         
159         end
160         
161         def refresh id=0
162         
163                 @id = id
164                 
165                 if id>0 then
166                         @frame.label = "Article n° " + id.to_s
167                         
168                         req = "SELECT * FROM articles WHERE id=#{id}"
169                         res = @db.requete req           
170                 
171                         res.each { |article|
172                                 @code.text = article['code']
173                                 @gencode.text = article['gencode'] unless article['gencode'].nil?
174                                 @designation.text = article['designation'] unless article['designation'].nil?
175                                 @colisage.value = article['colisage'].to_i
176                                 @tva.value = article['id_tva'].to_f
177                                 #@pu.value = article['pu'].to_f
178                                 @stock.value = article['stock'].to_i
179                                 @note.buffer.text = article['note'].to_s
180                         }
181                         
182                         req = "SELECT T1.id, T1.designation, T0.tarif FROM articles_tarifs T0 INNER JOIN tarifs T1 ON T0.tarif_id=T1.id WHERE T0.code='#{@code.text}' ORDER BY T1.id"
183                         res = @db.requete req   
184                         
185                         @list_store.clear       
186                 
187                         res.each { |h|
188                                 iter = @list_store.append
189                                 iter[0] = h['id'].to_i
190                                 iter[1] = h['designation']
191                                 iter[2] = h['tarif'].to_f
192                         }
193                         
194                 else
195                         @frame.label = "Nouvel article"
196                 end
197         
198         end
199         
200         def validate
201                 
202                 req = ""
203                 
204                 if @id.eql?(0) then
205                         req = "INSERT INTO articles (designation, colisage, id_tva, stock, code, gencode, note) "
206                         req += " VALUES ('#{@designation.text.gsub("'", "''")}', #{@colisage.value.round.to_s}, '#{@tva.value.to_s}', #{@stock.value.round.to_s}, '#{@code.text.gsub("'", "''")}', '#{@gencode.text.gsub("'", "''")}', '#{@note.buffer.text.gsub("'", "''")}' )"
207                 else
208                         req = "UPDATE articles SET designation='#{@designation.text.gsub("'", "''")}', colisage=#{@colisage.value.round.to_s}, id_tva='#{@tva.value.to_s}', "
209                         req += " stock=#{@stock.value.round.to_s}, code='#{@code.text.gsub("'", "''")}', gencode='#{@gencode.text.gsub("'", "''")}', note='#{@note.buffer.text.gsub("'", "''")}' "
210                         req += " WHERE id=#{@id}"
211                 end
212                 
213                 res = @db.requete req
214                 
215                 quit 1
216         
217         end
218         
219         def quit statut=nil
220         
221                 @window.liste_articles.refresh unless statut.nil?
222                 @window.affiche @window.liste_articles
223         
224         end
225         
226 end