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