Recherche des articles par code, designation, gencode1, gencode2
[bilio:bilio.git] / liste_articles.rb
1 # coding: utf-8
2
3 class ListeArticles < Gtk::VBox
4
5         attr_reader :view
6         
7         def initialize window, research = false
8         
9                 super(false, 3)
10                 
11                 set_border_width 10
12                 
13                 @window = window
14                 @db = window.db
15                 @login = window.login
16                 
17                 @research = research
18                  
19                 agencement
20         
21         end
22         
23         def agencement
24         
25                 vbox = Gtk::VBox.new false, 3
26                 @add_new = Gtk::Button.new
27                 hboxajout = Gtk::HBox.new false, 2
28                 hboxajout.add Gtk::Image.new( "./resources/icons/add.png" )
29                 label_button = Gtk::Label.new "Nouvel article"
30                 hboxajout.add label_button
31                 @add_new.add hboxajout
32                 align_button = Gtk::Alignment.new 1, 0, 0, 0
33                 align_button.add @add_new
34                 frame = Gtk::Frame.new "Articles"
35                 
36                 hbox = Gtk::HBox.new false, 3
37                 @search_gencode = Gtk::Entry.new
38                 @search_gencode.primary_icon_stock = Gtk::Stock::FIND
39                 @search_gencode.secondary_icon_stock = Gtk::Stock::CLEAR
40                 hbox.add @search_gencode
41                 hbox.add align_button
42                 
43                 vbox.pack_start hbox, false, false, 3 if !@research
44                 vbox.pack_start tableau_articles, true, true, 3 
45                 frame.add vbox
46                 
47                 @add_new.signal_connect( "clicked" ) {
48                         @window.article.refresh
49                         @window.affiche @window.article
50                 }
51                 
52                 @search_gencode.signal_connect('activate') {
53                         #open_article @search_gencode.text
54                         refresh @search_gencode.text
55                         @search_gencode.text = ""
56                 }
57                 
58                 add frame
59         
60         end
61         
62         def tableau_articles
63         
64                 # list_store (id, code, designation, gencode,  colisage, id_tva, pu, stock)
65                 @list_store = Gtk::TreeStore.new(Integer, String, String, String, Integer, Float, Float, Integer)
66                 @view = Gtk::TreeView.new(@list_store)
67                 @view.signal_connect ("row-activated") { |view, path, column|
68                         @select = @view.model.get_value( @view.selection.selected, 0 )
69                         if !@research then
70                                 @window.article.refresh @view.model.get_value @view.selection.selected, 0
71                                 @window.affiche @window.article
72                         end
73                 }
74                 
75                 # Create a renderer with the background property set
76                 renderer_left = Gtk::CellRendererText.new
77                 renderer_left.background = "white"
78                 renderer_left.xalign = 0
79                 
80                 # Create a renderer with the background property set
81                 renderer_center = Gtk::CellRendererText.new
82                 renderer_center.background = "white"
83                 renderer_center.xalign = 0.5
84                 
85                 # Create a renderer with the background property set
86                 renderer_right = Gtk::CellRendererText.new
87                 renderer_right.xalign = 1
88
89                 renderer_progress = Gtk::CellRendererProgress.new       
90                 
91                 renderer_pix = Gtk::CellRendererPixbuf.new      
92                 
93                 col = Gtk::TreeViewColumn.new("id", renderer_left, :text => 0)
94                 col.sort_column_id = 0
95                 col.resizable = true
96                 #@view.append_column(col)
97                 
98                 col = Gtk::TreeViewColumn.new("Code", renderer_left, :text => 1)
99                 col.sort_column_id = 1
100                 col.resizable = true
101                 @view.append_column(col)
102                 
103                 col = Gtk::TreeViewColumn.new("D├ęsignation", renderer_left, :text => 2)
104                 col.sort_column_id = 2
105                 col.resizable = true
106                 col.expand = true
107                 @view.append_column(col)
108                 
109                 col = Gtk::TreeViewColumn.new("Code barre", renderer_left, :text => 3)
110                 col.sort_column_id = 3
111                 col.resizable = true
112                 #@view.append_column(col)
113                 
114                 col = Gtk::TreeViewColumn.new("Tarif comptant", renderer_right, :text => 6)
115                 col.sort_column_id = 6
116                 col.resizable = true
117                 @view.append_column(col)
118                 
119                 col = Gtk::TreeViewColumn.new("TVA", renderer_right, :text => 5)
120                 col.sort_column_id = 5
121                 col.resizable = true
122                 @view.append_column(col)
123                 
124                 col = Gtk::TreeViewColumn.new("Colisage", renderer_right, :text => 4)
125                 col.sort_column_id = 4
126                 col.resizable = true
127                 @view.append_column(col)
128                 
129                 col = Gtk::TreeViewColumn.new("Stock", renderer_right, :text => 7)
130                 col.sort_column_id = 7
131                 col.resizable = true
132                 @view.append_column(col)
133                 
134                 scroll = Gtk::ScrolledWindow.new
135         scroll.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC)
136         scroll.add @view
137         
138         scroll
139         
140         end
141         
142         def refresh search=nil
143                 
144                 @search_gencode.text = ""
145                 
146                 req = "SELECT T0.id, T0.code, T0.gencode, T0.designation, T0.colisage, T0.id_tva, T1.tarif, T0.stock "
147                 req += " FROM articles T0 INNER JOIN articles_tarifs T1 ON T0.code=T1.code "
148                 req += " WHERE T1.tarif_id=1 "
149                 req += " AND (T0.designation ILIKE '%#{search}%' OR T0.gencode='#{search}' OR T0.gencode2='#{search}' OR T0.code ILIKE '%#{search}%')" unless search.nil?       
150                 req += " ORDER BY T0.code"
151                 req += " LIMIT 100"
152                 
153                 res = @db.requete req
154                 
155                 if res.count.eql?(1) then
156                         @window.article.refresh res[0]['id'].to_i
157                         @window.affiche @window.article
158                         @window.article.focus
159                 else            
160                         remplir res
161                 end
162         
163         end
164         
165         def remplir res
166
167                 @list_store.clear
168                 
169                 res.each { |h| 
170                         
171                         iter = @list_store.append nil
172                         iter[0] = h['id'].to_i
173                         iter[1] = h['code']
174                         iter[2] = h['designation']
175                         iter[3] = h['gencode']
176                         iter[4] = h['colisage'].to_i
177                         iter[5] = h['id_tva'].to_f
178                         iter[6] = h['tarif'].to_f
179                         iter[7] = h['stock'].to_i
180                 }
181                         
182         end
183         
184         def focus
185                 @search_gencode.grab_focus
186         end
187         
188         def open_article gencode
189         
190                 req = "SELECT id FROM articles WHERE gencode='#{gencode}' OR gencode2='#{gencode}'"
191                 
192                 res = @db.requete req
193                 
194                 if res.count>0 then 
195                         if res.count.eql?(1) then
196                                 @window.article.refresh res[0]['id'].to_i
197                                 @window.affiche @window.article
198                                 @window.article.focus
199                         else
200                                 refresh gencode
201                         end
202                 else
203                         @list_store.clear
204                 end
205         
206         end
207         
208 end