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