]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/picklist/user_request.js
context filters for acq user requests UI
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / picklist / user_request.js
1 dojo.require('openils.CGI');
2 dojo.require('openils.Util');
3 dojo.require('openils.User');
4 dojo.require('openils.widget.AutoGrid');
5 dojo.require('fieldmapper.OrgUtils');
6 dojo.require('openils.widget.OrgUnitFilteringSelect');
7 dojo.require('openils.widget.EditPane');
8 dojo.require("dijit.layout.StackContainer");
9 dojo.require('openils.PermaCrud');
10 dojo.requireLocalization("openils.acq", "acq");
11 dojo.require('openils.acq.Lineitem');
12
13 var contextOrg;
14 var contextUsr;
15 var contextLI;
16 var contextEg_bib;
17 var aur_obj;
18 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
19 var cgi = new openils.CGI();
20
21 function setup() {
22
23     if (cgi.param('eg_bib')) {
24         changeBib(cgi.param('eg_bib'));
25     }
26
27     if (cgi.param('lineitem')) {
28         changeLI(cgi.param('lineitem'));
29     }
30
31     if (cgi.param('usr')) {
32         var usr_obj = fieldmapper.standardRequest(
33             [
34                 'open-ils.actor',
35                 'open-ils.actor.user.fleshed.retrieve.authoritative'
36             ],
37             {
38                 params: [openils.User.authtoken, cgi.param('usr')]
39             }
40         );
41         if (typeof usr_obj.textcode == 'undefined') {
42             changeUser(usr_obj.id(),usr_obj.card().barcode());
43         } else {
44             alert(usr_obj.textcode + ' : ' + usr_obj.desc);
45         }
46     }
47
48     if(reqId) {
49         drawRequest();
50     } else {
51         drawList();
52     }
53 }
54
55 function drawRequest() {
56     var pcrud = new openils.PermaCrud({ authtoken : openils.User.authtoken });
57     aur_obj = pcrud.retrieve('aur',reqId);
58
59     // hide the grid and the context selector
60     dijit.byId('stackContainer').forward();
61
62     // purge any previous lineitem display
63     // FIXME: I thought it would be cool to have this, but I can't get it 
64     // to look right with our dojo div/contentPanes.  So just testing for
65     // a DOM hook for now.
66     if (dojo.byId('lineitem')) {
67         //openils.Util.hide( 'lineitem_container' );
68         dojo.byId('lineitem').innerHTML = '';
69     }
70
71     // toggle the View Picklist/Add to Picklist button label
72     if (aur_obj.lineitem()) {
73         openils.Util.hide( 'add_to_picklist' );
74         openils.Util.show( 'view_picklist' );
75     } else {
76         openils.Util.hide( 'view_picklist' );
77         openils.Util.show( 'add_to_picklist' );
78     }
79
80     // draw a detail page for a particular request
81     var div = document.getElementById('detail_content_pane');
82     while (div.lastChild) { div.removeChild( div.lastChild ); }
83     var pane = new openils.widget.EditPane({ 
84         fmObject : aur_obj,
85         readOnly : true
86     });
87     pane.domNode = div;
88     pane.hideActionButtons = true;
89     pane.startup();
90
91     // lineitem summary
92     if (dojo.byId('lineitem') && aur_obj.lineitem()) {
93         //openils.Util.show( 'lineitem_container' );
94         openils.acq.Lineitem.fetchAndRender(aur_obj.lineitem(), {},
95             function(li, html) {
96                 dojo.byId('lineitem').innerHTML = html;
97             }
98         );
99     }
100
101     // including ability to add request to a picklist
102     // and to "reject" it (aka apply a cancel reason)
103
104     dojo.byId("acq-ur-cancel-reason").innerHTML = '';
105     var widget = new openils.widget.AutoFieldWidget({
106         "fmField": "cancel_reason",
107         "fmClass": "aur",
108         "parentNode": dojo.byId("acq-ur-cancel-reason"),
109         "orgLimitPerms": ["CREATE_PURCHASE_REQUEST"],
110         "forceSync": true
111     });
112
113     widget.build(
114         function(w, ww) {
115             acqUrCancelReasonSubmit.onClick = function() {
116                 if (w.attr("value")) {
117                     if (confirm( localeStrings.UR_CANCEL_CONFIRM )) {
118                         fieldmapper.standardRequest(
119                             [ 'open-ils.acq', 'open-ils.acq.user_request.cancel.batch' ],
120                             {   async: true,
121                                 params: [openils.User.authtoken, [reqId], w.attr("value")],
122                                 oncomplete: function(r) {
123                                     location.href = location.href; // kludge to reload the interface
124                                 }
125                             }
126                         );
127                     }
128                 }
129             };
130         }
131     );
132 }
133
134 function fooPicklist() {
135     if (aur_obj.lineitem()) {
136         viewPicklist();
137     } else {
138         addToPicklist();
139     }
140 }
141
142 function viewPicklist() {
143     var lineitem = fieldmapper.standardRequest(
144         [ 'open-ils.acq', 'open-ils.acq.lineitem.retrieve.authoritative' ],
145         {
146             params: [openils.User.authtoken, aur_obj.lineitem()]
147         }
148     );
149     location.href = oilsBasePath + "/acq/picklist/view/" + lineitem.picklist();
150 }
151
152 function addToPicklist() {
153     // reqId, from detail view
154     location.href = oilsBasePath + "/acq/picklist/brief_record?ur=" + reqId + "&prepop=" + encodeURIComponent(js2JSON({
155         "1": aur_obj.title() || aur_obj.article_title() || aur_obj.volume(),
156         "2": aur_obj.author(),
157         "5": aur_obj.isxn(),
158         "9": aur_obj.publisher(),
159         "10": aur_obj.pubdate()
160     }));
161 }
162
163 function setNoHold() {
164     // reqId, from detail view
165     fieldmapper.standardRequest(
166         [ 'open-ils.acq', 'open-ils.acq.user_request.set_no_hold.batch' ],
167         {   async: true,
168             params: [openils.User.authtoken, [reqId]],
169             oncomplete: function(r) {
170                 location.href = location.href; // kludge to reload the interface
171             }
172         }
173     );
174 }
175
176 // format the title data as id:title
177 function getTitle(idx, item) {
178     if(item) {
179         return this.grid.store.getValue(item, 'id') + ':' + 
180         this.grid.store.getValue(item, 'title');
181     }
182     return '';
183 }
184
185 // turn id:title into a url
186 function formatTitle(value) {
187     if(value) {
188         var parts = value.split(/:/);
189         return '<a href="' + oilsBasePath + 
190             '/acq/picklist/user_request/' + parts[0] + '">' + parts[1] + '</a>';
191     }
192 }
193
194 function drawList() {
195     buildGrid();
196
197     var connect = function() {
198         dojo.connect(contextOrgSelector, 'onChange',
199             function() {
200                 contextOrg = this.attr('value');
201                 rGrid.resetStore();
202                 buildGrid();
203             }
204         );
205     };
206
207     new openils.User().buildPermOrgSelector(
208         'CREATE_PICKLIST', contextOrgSelector, null, connect);
209 }
210
211 function buildGrid() {
212
213     if(contextOrg == null)
214         contextOrg = openils.User.user.ws_ou();
215
216     var query = {
217         cancel_reason : null,
218         '+au' : {
219             home_ou : fieldmapper.aou.descendantNodeList(contextOrg).map(
220             function(item) { return item.id(); })
221         }
222     };
223
224     if (contextUsr) {
225         delete query['+au']['home_ou'];
226         query['+au']['id'] = contextUsr;
227     }
228
229     if (contextEg_bib) {
230         query['eg_bib'] = contextEg_bib;
231     }
232
233     if (contextLI) {
234         query['lineitem'] = contextLI;
235     }
236
237     rGrid.resetStore();
238     rGrid.loadAll(
239         {   order_by : {aur : 'request_date'},
240             join : 'au' 
241         },
242         query
243     );
244 }
245
246 function changeBib(value) {
247     contextEg_bib = value;
248     rGrid.overrideEditWidgets.eg_bib = new dijit.form.TextBox({"disabled": true});
249     rGrid.overrideEditWidgets.eg_bib.shove = { create : contextEg_bib };
250 }
251
252 function changeLI(value,display_value) {
253     contextLI = value;
254     contextLITextbox.setValue( contextLI );
255     contextLITextbox.setDisplayedValue( display_value || contextLI );
256     rGrid.overrideEditWidgets.lineitem = new dijit.form.TextBox({"disabled": true});
257     rGrid.overrideEditWidgets.lineitem.shove = { create : contextLI };
258 }
259
260 function changeLIPrompt() {
261     var lineitem = window.prompt(localeStrings.UR_FILTER_LINEITEM);
262     if(lineitem != '' && (lineitem == null || Number(lineitem) == NaN)) {
263         return;
264     }
265     changeLI(lineitem);
266     buildGrid();
267 }
268
269 function changeUser(value,display_value) {
270     contextUsr = value;
271     contextUsrTextbox.setValue( contextUsr );
272     contextUsrTextbox.setDisplayedValue( display_value || contextUsr );
273     rGrid.overrideEditWidgets.usr = new dijit.form.TextBox({"disabled": true});
274     rGrid.overrideEditWidgets.usr.shove = { create : contextUsr };
275 }
276
277 function changeUserPrompt() {
278     var barcode = window.prompt(localeStrings.UR_FILTER_USER);
279     if(barcode == null) {
280         return;
281     }
282     if(typeof xulG != 'undefined' && xulG.get_barcode) {
283         // We have a "complete the barcode" function, call it (actor = users only)
284         var new_barcode = xulG.get_barcode(window, 'actor', barcode);
285         // If we got a result (boolean false is "no result") check it
286         if(new_barcode) {
287             // user_false string means they picked "None of the above"
288             // Abort before any other events can fire
289             if(new_barcode == "user_false") return;
290             // No error means we have a (hopefully valid) completed barcode to use.
291             // Otherwise, fall through to other methods of checking
292             if(typeof new_barcode.ilsevent == 'undefined')
293                 barcode = new_barcode.barcode;
294         }
295     }
296     if (barcode == '') {
297         changeUser('','');
298     } else {
299         var usr_obj = fieldmapper.standardRequest(
300             [
301                 'open-ils.actor',
302                 'open-ils.actor.user.fleshed.retrieve_by_barcode.authoritative'
303             ],
304             {
305                 params: [openils.User.authtoken, barcode]
306             }
307         );
308         if (typeof usr_obj.textcode != 'undefined') {
309             alert(usr_obj.textcode + ' : ' + usr_obj.desc);
310             return;
311         } else {
312             changeUser(usr_obj.id(),usr_obj.card().barcode());
313         }
314     }
315     buildGrid();
316 }
317
318 openils.Util.addOnLoad(setup);
319
320