]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/circ/renew.js
Add cointainer() and record_list() filters to QueryParser
[Evergreen.git] / Open-ILS / xul / staff_client / server / circ / renew.js
1 dump('entering circ.renew.js\n');
2
3 if (typeof circ == 'undefined') circ = {};
4 circ.renew = function (params) {
5
6     JSAN.use('util.error'); this.error = new util.error();
7     JSAN.use('util.network'); this.network = new util.network();
8     JSAN.use('util.barcode');
9     JSAN.use('util.date');
10     this.OpenILS = {}; JSAN.use('OpenILS.data'); this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init({'via':'stash'});
11     this.data = this.OpenILS.data;
12 }
13
14 circ.renew.prototype = {
15
16     'selection_list' : [],
17
18     'init' : function( params ) {
19
20         var obj = this;
21
22         JSAN.use('circ.util'); JSAN.use('patron.util');
23         var columns = circ.util.columns( 
24             { 
25                 'barcode' : { 'hidden' : false },
26                 'title' : { 'hidden' : false },
27                 'location' : { 'hidden' : false },
28                 'call_number' : { 'hidden' : false },
29                 'status' : { 'hidden' : false },
30                 'alert_message' : { 'hidden' : false },
31                 'due_date' : { 'hidden' : false },
32                 'due_time' : { 'hidden' : false },
33                 'renewal_remaining' : { 'hidden' : false }
34             },
35             {
36                 'except_these' : [ 'uses', 'checkin_time_full' ]
37             }
38         ).concat(
39             patron.util.columns( { 'family_name' : { 'hidden' : 'false' } } )
40
41         ).concat(
42             patron.util.mbts_columns( {}, { 'except_these' : [ 'total_paid', 'total_owed', 'xact_start', 'xact_finish', 'xact_type' ] } )
43
44         ).sort( function(a,b) { if (a.label < b.label) return -1; if (a.label > b.label) return 1; return 0; } );
45
46         JSAN.use('util.list'); obj.list = new util.list('renew_list');
47         obj.list.init(
48             {
49                 'columns' : columns,
50                 'map_row_to_columns' : circ.util.std_map_row_to_columns(),
51                 'on_select' : function(ev) {
52                     try {
53                         JSAN.use('util.functional');
54                         var sel = obj.list.retrieve_selection();
55                         obj.selection_list = util.functional.map_list(
56                             sel,
57                             function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
58                         );
59                         obj.error.sdump('D_TRACE', 'circ/copy_status: selection list = ' + js2JSON(obj.selection_list) );
60                         if (obj.selection_list.length == 0) {
61                             obj.controller.view.sel_edit.setAttribute('disabled','true');
62                             obj.controller.view.sel_opac.setAttribute('disabled','true');
63                             obj.controller.view.sel_patron.setAttribute('disabled','true');
64                             obj.controller.view.sel_last_patron.setAttribute('disabled','true');
65                             obj.controller.view.sel_copy_details.setAttribute('disabled','true');
66                             obj.controller.view.sel_bucket.setAttribute('disabled','true');
67                             obj.controller.view.sel_spine.setAttribute('disabled','true');
68                             obj.controller.view.sel_transit_abort.setAttribute('disabled','true');
69                             obj.controller.view.sel_clip.setAttribute('disabled','true');
70                             obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','true');
71                         } else {
72                             obj.controller.view.sel_edit.setAttribute('disabled','false');
73                             obj.controller.view.sel_opac.setAttribute('disabled','false');
74                             obj.controller.view.sel_patron.setAttribute('disabled','false');
75                             obj.controller.view.sel_last_patron.setAttribute('disabled','false');
76                             obj.controller.view.sel_copy_details.setAttribute('disabled','false');
77                             obj.controller.view.sel_bucket.setAttribute('disabled','false');
78                             obj.controller.view.sel_spine.setAttribute('disabled','false');
79                             obj.controller.view.sel_transit_abort.setAttribute('disabled','false');
80                             obj.controller.view.sel_clip.setAttribute('disabled','false');
81                             obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','false');
82                         }
83                     } catch(E) {
84                         alert('FIXME: ' + E);
85                     }
86                 }
87             }
88         );
89         
90         JSAN.use('util.controller'); obj.controller = new util.controller();
91         obj.controller.init(
92             {
93                 'control_map' : {
94                     'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
95                     'sel_clip' : [
96                         ['command'],
97                         function() { 
98                             obj.list.clipboard(); 
99                             obj.controller.view.renew_barcode_entry_textbox.focus();
100                         }
101                     ],
102                     'sel_edit' : [
103                         ['command'],
104                         function() {
105                             try {
106                                 obj.spawn_copy_editor();
107                             } catch(E) {
108                                 alert(E);
109                             }
110                         }
111                     ],
112                     'sel_spine' : [
113                         ['command'],
114                         function() {
115                             JSAN.use('cat.util');
116                             cat.util.spawn_spine_editor(obj.selection_list);
117                         }
118                     ],
119                     'sel_opac' : [
120                         ['command'],
121                         function() {
122                             JSAN.use('cat.util');
123                             cat.util.show_in_opac(obj.selection_list);
124                         }
125                     ],
126                     'sel_transit_abort' : [
127                         ['command'],
128                         function() {
129                             JSAN.use('circ.util');
130                             circ.util.abort_transits(obj.selection_list);
131                         }
132                     ],
133                     'sel_patron' : [
134                         ['command'],
135                         function() {
136                             JSAN.use('circ.util');
137                             circ.util.show_last_few_circs(obj.selection_list);
138                         }
139                     ],
140                     'sel_last_patron' : [
141                         ['command'],
142                         function() {
143                             var patrons = {};
144                             for (var i = 0; i < obj.selection_list.length; i++) {
145                                 var circs = obj.network.simple_request('FM_CIRC_RETRIEVE_VIA_COPY',[ses(),obj.selection_list[i].copy_id,1]);
146                                 if (circs.length > 0) {
147                                     patrons[circs[0].usr()] = 1;
148                                 } else {
149                                     alert(document.getElementById('circStrings').getFormattedString('staff.circ.item_no_circs', [obj.selection_list[i].barcode]));
150                                 }
151                             }
152                             for (var i in patrons) {
153                                 xulG.new_patron_tab({},{'id' : i});
154                             }
155                         }
156                     ],
157                     'sel_copy_details' : [
158                         ['command'],
159                         function() {
160                             JSAN.use('circ.util');
161                             circ.util.item_details_new(
162                                 util.functional.map_list(
163                                     obj.selection_list,
164                                     function(o) { return o.barcode; }
165                                 )
166                             );
167                         }
168                     ],
169                     'sel_mark_items_damaged' : [
170                         ['command'],
171                         function() {
172                             var funcs = [];
173                             JSAN.use('cat.util'); JSAN.use('util.functional');
174                             cat.util.mark_item_damaged( util.functional.map_list( obj.selection_list, function(o) { return o.copy_id; } ) );
175                         }
176                     ],
177                     'sel_bucket' : [
178                         ['command'],
179                         function() {
180                             JSAN.use('cat.util');
181                             cat.util.add_copies_to_bucket(obj.selection_list);
182                         }
183                     ],
184                     'renew_barcode_entry_textbox' : [
185                         ['keypress'],
186                         function(ev) {
187                             if (ev.keyCode && ev.keyCode == 13) {
188                                 obj.renew();
189                             }
190                         }
191                     ],
192                     'cmd_broken' : [
193                         ['command'],
194                         function() { alert(document.getElementById('circStrings').getString('staff.circ.unimplemented')); }
195                     ],
196                     'cmd_renew_submit_barcode' : [
197                         ['command'],
198                         function() {
199                             obj.renew();
200                         }
201                     ],
202                     'cmd_renew_print' : [
203                         ['command'],
204                         function() {
205                             var p = { 
206                                 'printer_context' : 'receipt',
207                                 'template' : 'renew'
208                             };
209                             obj.list.print(p);
210                         }
211                     ],
212                     'cmd_csv_to_clipboard' : [ ['command'], function() { 
213                         obj.list.dump_csv_to_clipboard(); 
214                         obj.controller.view.renew_barcode_entry_textbox.focus();
215                     } ],
216                     'cmd_csv_to_printer' : [ ['command'], function() { 
217                         obj.list.dump_csv_to_printer(); 
218                         obj.controller.view.renew_barcode_entry_textbox.focus();
219                     } ],
220                     'cmd_csv_to_file' : [ ['command'], function() { 
221                         obj.list.dump_csv_to_file( { 'defaultFileName' : 'checked_in.txt' } ); 
222                         obj.controller.view.renew_barcode_entry_textbox.focus();
223                     } ],
224                     'renew_duedate_datepicker' : [
225                         ['change'],
226                         function(ev) { 
227                             try {
228                                 if (obj.check_date(ev.target)) {
229                                     ev.target.parentNode.setAttribute('style','');
230                                 } else {
231                                     ev.target.parentNode.setAttribute('style','background-color: red');
232                                 }
233                             } catch(E) {
234                                 alert('Error in renew.js, renew_duedate_datepicker @change: ' + E);
235                             }
236                         }
237                     ]
238
239                 }
240             }
241         );
242         this.controller.render();
243         this.controller.view.renew_barcode_entry_textbox.focus();
244
245     },
246
247     'test_barcode' : function(bc) {
248         var obj = this;
249         var x = document.getElementById('strict_barcode');
250         if (x && x.checked != true) return true;
251         var good = util.barcode.check(bc);
252         if (good) {
253             return true;
254         } else {
255             if ( 1 == obj.error.yns_alert(
256                         document.getElementById('circStrings').getFormattedString('staff.circ.check_digit.bad', [bc]),
257                         document.getElementById('circStrings').getString('staff.circ.barcode.bad'),
258                         document.getElementById('circStrings').getString('staff.circ.cancel'),
259                         document.getElementById('circStrings').getString('staff.circ.barcode.accept'),
260                         null,
261                         document.getElementById('circStrings').getString('staff.circ.confirm'),
262                         '/xul/server/skin/media/images/bad_barcode.png'
263             ) ) {
264                 return true;
265             } else {
266                 return false;
267             }
268         }
269     },
270
271     'renew' : function(params) {
272         var obj = this;
273         try {
274             if (!params) params = {};
275
276             var barcode = obj.controller.view.renew_barcode_entry_textbox.value;
277             if (!barcode) return;
278             if (barcode) {
279                 if ( obj.test_barcode(barcode) ) { /* good */ } else { /* bad */ return; }
280             }
281             params.barcode = barcode;
282             params.return_patron = true;
283
284             var auto_print = document.getElementById('renew_auto');
285             if (auto_print) auto_print = auto_print.checked;
286
287             if (document.getElementById('renew_duedate_checkbox').checked) {
288                 if (! obj.check_date(obj.controller.view.renew_duedate_datepicker)) return;
289                 var tp = document.getElementById('renew_duedate_timepicker');
290                 var dp = obj.controller.view.renew_duedate_datepicker;
291                 var tp_date = tp.dateValue;
292                 var dp_date = dp.dateValue;
293                 dp_date.setHours( tp_date.getHours() );
294                 dp_date.setMinutes( tp_date.getMinutes() );
295
296                 JSAN.use('util.date');
297                 params.due_date = util.date.formatted_date(dp_date,'%{iso8601}');
298             }
299
300
301             JSAN.use('circ.util');
302             var renew = circ.util.renew_via_barcode(
303                 params,
304                 function( r ) {
305                     obj.renew_followup( r, barcode );
306                 }
307             );
308         } catch(E) {
309             obj.error.standard_unexpected_error_alert('Error in circ/renew.js, renew():', E);
310             if (typeof obj.on_failure == 'function') {
311                 obj.on_failure(E);
312             }
313             if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
314                 window.xulG.on_failure(E);
315             }
316         }
317     },
318
319     'renew_followup' : function(r,bc) {
320         var obj = this;
321         try {
322             if (!r) return obj.on_failure(); /* circ.util.renew handles errors and returns null currently */
323             if ( (typeof r[0].ilsevent != 'undefined' && r[0].ilsevent == 0) ) {
324                 // SUCCESS
325                 var x = document.getElementById('no_change_label');
326                 if (x) {
327                     x.hidden = true;
328                     x.setAttribute('value','');
329                 }
330             } else {
331                 // FAILURE
332                 var msg = document.getElementById("patronStrings").getFormattedString('staff.patron.items.items_renew.not_renewed',[bc, r[0].textcode + r[0].desc]);
333                 var x = document.getElementById('no_change_label');
334                 if (x) {
335                     x.hidden = false;
336                     x.setAttribute('value',msg);
337                 }
338                 obj.controller.view.renew_barcode_entry_textbox.focus();
339                 obj.controller.view.renew_barcode_entry_textbox.select();
340                 return;
341             }
342             var renew = r[0].payload;
343             var retrieve_id = js2JSON( { 'copy_id' : renew.copy.id(), 'barcode' : renew.copy.barcode(), 'doc_id' : (renew.record == null ? null : renew.record.doc_id() ) } );
344             if (document.getElementById('trim_list')) {
345                 var x = document.getElementById('trim_list');
346                 if (x.checked) { obj.list.trim_list = 20; } else { obj.list.trim_list = null; }
347             }
348
349             var params = {
350                 'retrieve_id' : retrieve_id,
351                 'row' : {
352                     'my' : {
353                         'circ' : renew.circ,
354                         'mbts' : renew.parent_circ ? renew.parent_circ.billable_transaction().summary() : null,
355                         'mvr' : renew.record,
356                         'acp' : renew.copy,
357                         'acn' : renew.volume,
358                         'au' : renew.patron,
359                         'status' : renew.status,
360                         'route_to' : renew.route_to,
361                         'message' : renew.message
362                     }
363                 },
364                 'to_top' : true
365             };
366             obj.list.append( params );
367
368             if (params.row.my.mbts && ( document.getElementById('no_change_label') || document.getElementById('fine_tally') ) ) {
369                 JSAN.use('util.money');
370                 var bill = params.row.my.mbts;
371                 if (Number(bill.balance_owed()) != 0) {
372                     if (document.getElementById('no_change_label')) {
373                         var m = document.getElementById('no_change_label').getAttribute('value');
374                         document.getElementById('no_change_label').setAttribute(
375                             'value', 
376                             m + document.getElementById('circStrings').getFormattedString('staff.circ.utils.billable.amount', [params.row.my.acp.barcode(), util.money.sanitize(bill.balance_owed())]) + '  '
377                         );
378                         document.getElementById('no_change_label').setAttribute('hidden','false');
379                     }
380                     if (document.getElementById('fine_tally')) {
381                         var amount = Number( document.getElementById('fine_tally').getAttribute('amount') ) + Number( bill.balance_owed() );
382                         document.getElementById('fine_tally').setAttribute('amount',amount);
383                         document.getElementById('fine_tally').setAttribute(
384                             'value',
385                             document.getElementById('circStrings').getFormattedString('staff.circ.utils.fine_tally_text', [ util.money.sanitize( amount ) ])
386                         );
387                         document.getElementById('fine_tally').setAttribute('hidden','false');
388                     }
389                 }
390             }
391
392             obj.list.node.view.selection.select(0);
393
394             JSAN.use('util.sound'); var sound = new util.sound(); sound.circ_good();
395
396             if (typeof obj.on_renew == 'function') {
397                 obj.on_renew(renew);
398             }
399             if (typeof window.xulG == 'object' && typeof window.xulG.on_renew == 'function') {
400                 window.xulG.on_renew(renew);
401             }
402
403             return true;
404
405         } catch(E) {
406             obj.error.standard_unexpected_error_alert('Error in circ/renew.js, renew_followup():', E);
407             if (typeof obj.on_failure == 'function') {
408                 obj.on_failure(E);
409             }
410             if (typeof window.xulG == 'object' && typeof window.xulG.on_failure == 'function') {
411                 window.xulG.on_failure(E);
412             }
413         }
414
415     },
416
417     'on_renew' : function() {
418         try {
419             this.controller.view.renew_barcode_entry_textbox.disabled = false;
420             this.controller.view.renew_barcode_entry_textbox.select();
421             this.controller.view.renew_barcode_entry_textbox.value = '';
422             this.controller.view.renew_barcode_entry_textbox.focus();
423         } catch(E) {
424             alert('Error in renew.js, on_renew(): ' + E);
425         }
426     },
427
428     'on_failure' : function() {
429         try {
430             this.controller.view.renew_barcode_entry_textbox.disabled = false;
431             this.controller.view.renew_barcode_entry_textbox.select();
432             this.controller.view.renew_barcode_entry_textbox.focus();
433         } catch(E) {
434             alert('Error in renew.js, on_failure(): ' + E);
435         }
436     },
437     
438     'spawn_copy_editor' : function() {
439
440         var obj = this;
441
442         JSAN.use('util.functional');
443
444         var list = obj.selection_list;
445
446         list = util.functional.map_list(
447             list,
448             function (o) {
449                 return o.copy_id;
450             }
451         );
452
453         JSAN.use('cat.util'); cat.util.spawn_copy_editor( { 'copy_ids' : list, 'edit' : 1 } );
454
455     },
456
457     'check_date' : function(node) {
458         var obj = this;
459         JSAN.use('util.date');
460         try {
461             obj.controller.view.renew_barcode_entry_textbox.setAttribute('disabled','false');
462             obj.controller.view.renew_barcode_entry_textbox.disabled = false;
463             obj.controller.view.cmd_renew_submit_barcode.setAttribute('disabled','false');
464             obj.controller.view.cmd_renew_submit_barcode.disabled = false;
465             if (util.date.check_past('YYYY-MM-DD',node.value) ) {
466                 obj.controller.view.renew_barcode_entry_textbox.setAttribute('disabled','true');
467                 obj.controller.view.cmd_renew_submit_barcode.setAttribute('disabled','true');
468                 return false;
469             }
470             return true;
471         } catch(E) {
472             throw(E);
473         }
474     }
475
476 }
477
478 dump('exiting circ.renew.js\n');