cea2c547aa8bdff9d4a9c6e5b7317d1f0590f94a
[Evergreen.git] / Open-ILS / xul / staff_client / server / circ / util.js
1 dump('entering circ/util.js\n');
2
3 if (typeof circ == 'undefined') var circ = {};
4 circ.util = {};
5
6 circ.util.EXPORT_OK     = [ 
7         'columns', 'checkin_via_barcode', 'std_map_row_to_column', 'hold_capture_by_copy_barcode'
8 ];
9 circ.util.EXPORT_TAGS   = { ':all' : circ.util.EXPORT_OK };
10
11 circ.util.columns = function(modify) {
12         
13         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
14
15         function getString(s) { return data.entities[s]; }
16
17         var c = [
18                 {
19                         'id' : 'acp_id', 'label' : getString('staff.acp_label_id'), 'flex' : 1,
20                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.id()'
21                 },
22                 {
23                         'id' : 'circ_id', 'label' : getString('staff.circ_label_id'), 'flex' : 1,
24                         'primary' : false, 'hidden' : true, 'render' : 'my.circ.id()'
25                 },
26                 {
27                         'id' : 'mvr_doc_id', 'label' : getString('staff.mvr_label_doc_id'), 'flex' : 1,
28                         'primary' : false, 'hidden' : true, 'render' : 'my.mvr.doc_id()'
29                 },
30                 {
31                         'id' : 'barcode', 'label' : getString('staff.acp_label_barcode'), 'flex' : 1,
32                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.barcode()'
33                 },
34                 {
35                         'id' : 'call_number', 'label' : getString('staff.acp_label_call_number'), 'flex' : 1,
36                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.call_number()'
37                 },
38                 {
39                         'id' : 'copy_number', 'label' : getString('staff.acp_label_copy_number'), 'flex' : 1,
40                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.copy_number()'
41                 },
42                 {
43                         'id' : 'location', 'label' : getString('staff.acp_label_location'), 'flex' : 1,
44                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.location()'
45                 },
46                 {
47                         'id' : 'loan_duration', 'label' : getString('staff.acp_label_loan_duration'), 'flex' : 1,
48                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.loan_duration()'
49                 },
50                 {
51                         'id' : 'circ_lib', 'label' : getString('staff.acp_label_circ_lib'), 'flex' : 1,
52                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.circ_lib()'
53                 },
54                 {
55                         'id' : 'fine_level', 'label' : getString('staff.acp_label_fine_level'), 'flex' : 1,
56                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.fine_level()'
57                 },
58                 {
59                         'id' : 'deposit', 'label' : getString('staff.acp_label_deposit'), 'flex' : 1,
60                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.deposit()'
61                 },
62                 {
63                         'id' : 'deposit_amount', 'label' : getString('staff.acp_label_deposit_amount'), 'flex' : 1,
64                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.deposit_amount()'
65                 },
66                 {
67                         'id' : 'price', 'label' : getString('staff.acp_label_price'), 'flex' : 1,
68                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.price()'
69                 },
70                 {
71                         'id' : 'circ_as_type', 'label' : getString('staff.acp_label_circ_as_type'), 'flex' : 1,
72                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.circ_as_type()'
73                 },
74                 {
75                         'id' : 'circ_modifier', 'label' : getString('staff.acp_label_circ_modifier'), 'flex' : 1,
76                         'primary' : false, 'hidden' : true, 'render' : 'my.acp.circ_modifier()'
77                 },
78                 {
79                         'id' : 'xact_start', 'label' : getString('staff.circ_label_xact_start'), 'flex' : 1,
80                         'primary' : false, 'hidden' : true, 'render' : 'my.circ.xact_start()'
81                 },
82                 {
83                         'id' : 'xact_finish', 'label' : getString('staff.circ_label_xact_finish'), 'flex' : 1,
84                         'primary' : false, 'hidden' : true, 'render' : 'my.circ.xact_finish()'
85                 },
86                 {
87                         'id' : 'due_date', 'label' : getString('staff.circ_label_due_date'), 'flex' : 1,
88                         'primary' : false, 'hidden' : true, 'render' : 'my.circ.due_date().substr(0,10)'
89                 },
90                 {
91                         'id' : 'title', 'label' : getString('staff.mvr_label_title'), 'flex' : 2,
92                         'primary' : false, 'hidden' : true, 'render' : 'my.mvr.title()'
93                 },
94                 {
95                         'id' : 'author', 'label' : getString('staff.mvr_label_author'), 'flex' : 1,
96                         'primary' : false, 'hidden' : true, 'render' : 'my.mvr.author()'
97                 },
98                 {
99                         'id' : 'renewal_remaining', 'label' : getString('staff.circ_label_renewal_remaining'), 'flex' : 0,
100                         'primary' : false, 'hidden' : true, 'render' : 'my.circ.renewal_remaining()'
101                 },
102                 {
103                         'id' : 'status', 'label' : getString('staff.acp_label_status'), 'flex' : 1,
104                         'primary' : false, 'hidden' : true, 'render' : 'obj.OpenILS.data.hash.ccs[ my.acp.status() ].name()'
105                 },
106                 {
107                         'id' : 'checkin_status', 'label' : getString('staff.checkin_label_status'), 'flex' : 1,
108                         'primary' : false, 'hidden' : true, 'render' : 'my.status.toString()'
109                 },
110                 {
111                         'id' : 'checkin_route_to', 'label' : getString('staff.checkin_label_route_to'), 'flex' : 1,
112                         'primary' : false, 'hidden' : true, 'render' : 'my.route_to.toString()'
113                 },
114                 {
115                         'id' : 'checkin_text', 'label' : getString('staff.checkin_label_text'), 'flex' : 1,
116                         'primary' : false, 'hidden' : true, 'render' : 'my.text.toString()'
117                 }
118
119         ];
120         for (var i = 0; i < c.length; i++) {
121                 if (modify[ c[i].id ]) {
122                         for (var j in modify[ c[i].id ]) {
123                                 c[i][j] = modify[ c[i].id ][j];
124                         }
125                 }
126         }
127         return c;
128 }
129
130 circ.util.std_map_row_to_column = function() {
131         return function(row,col) {
132                 // row contains { 'my' : { 'acp' : {}, 'circ' : {}, 'mvr' : {} } }
133                 // col contains one of the objects listed above in columns
134                 
135                 // mimicking some of the obj in circ.checkin and circ.checkout where map_row_to_column is usually defined
136                 var obj = {}; obj.OpenILS = {};  // One of our circ columns uses OpenILS.data
137                 JSAN.use('util.error'); obj.error = new util.error();
138                 JSAN.use('OpenILS.data'); obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
139
140                 var my = row.my;
141                 var value;
142                 try { 
143                         value = eval( col.render );
144                 } catch(E) {
145                         obj.error.sdump('D_ERROR','map_row_to_column: ' + E);
146                         value = '???';
147                 }
148                 return value;
149         }
150 }
151
152 circ.util.checkin_via_barcode = function(session,barcode,backdate) {
153         try {
154                 JSAN.use('util.error'); var error = new util.error();
155                 JSAN.use('util.network'); var network = new util.network();
156                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
157                 JSAN.use('util.date');
158                 if (backdate && (backdate == util.date.formatted_date(new Date(),'%Y-%m-%d')) ) backdate = null;
159
160                 var check = network.request(
161                         api.checkin_via_barcode.app,
162                         api.checkin_via_barcode.method,
163                         [ session, barcode, null, backdate ]
164                 );
165
166                 /*
167                 { // REMOVE_ME, forcing a condition for testing
168                         check.status = 1;
169                         check.text = 'This copy is the first that could fulfill a hold.  Do it?';
170                 }
171                 */
172
173                 if (check.status != 0) {
174                         switch(check.status) {
175                                 case '1': case 1: /* possible hold capture */
176                                         var rv = error.yns_alert(
177                                                 check.text,
178                                                 'Alert',
179                                                 "Capture",
180                                                 "Don't Capture",
181                                                 null,
182                                                 "Check here to confirm this message"
183                                         );
184                                         switch(rv) {
185                                                 case 0: /* capture */
186                                                 try {
187                                                         var check2 = this.hold_capture_by_copy_barcode( session, barcode );
188                                                         if (check2) {
189                                                                 check.copy = check2.copy;
190                                                                 check.text = check2.text;
191                                                                 check.route_to = check2.route_to;
192                                                                 JSAN.use('patron.util');
193                                                                 var patron = patron.util.retrieve_au_via_id( check.hold.usr() );
194                                                                 alert('To Printer\n' + check.text + '\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
195                                                                         check.record.title() + '  Author: ' + check.record.author() + 
196                                                                         '\r\n' + 'Route To: ' + check.route_to + '  Patron: ' + 
197                                                                         patron.card().barcode() + ' ' + patron.family_name() + ', ' + 
198                                                                         patron.first_given_name() + '\r\n'); //FIXME
199
200                                                                 /*
201                                                                 sPrint(check.text + '<br />\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
202                                                                         check.record.title() + '  Author: ' + check.record.author() + 
203                                                                         '<br />\r\n' + 'Route To: ' + check.route_to + '  Patron: ' + 
204                                                                         patron.card().barcode() + ' ' + patron.family_name() + ', ' + 
205                                                                         patron.first_given_name() + '<br />\r\n'
206                                                                 );
207                                                                 */
208
209                                                         }
210
211                                                 } catch(E) { 
212                                                         error.sdump('D_ERROR',E + '\n'); 
213                                                         /* 
214                                                         // demo testing 
215                                                         check.text = 'Captured for Hold';
216                                                         check.route_to = 'ARL-ATH';
217                                                         */
218                                                 }
219                                                 break;
220                                                 case 1: /* don't capture */
221
222                                                         check.text = 'Not Captured for Hold';
223                                                 break;
224                                         }
225                                 break;
226                                 case '2': case 2: /* LOST??? */
227                                         JSAN.use('patron.util');
228                                         var patron = patron.util.retrieve_au_via_id( check.circ.usr() );
229                                         var msg = check.text + '\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
230                                                         check.record.title() + '  Author: ' + check.record.author() + '\r\n' +
231                                                         'Patron: ' + patron.card().barcode() + ' ' + patron.family_name() + ', ' +
232                                                         patron.first_given_name();
233                                         var pcheck = error.yns_alert(
234                                                 msg,
235                                                 'Lost Item',
236                                                 'Edit Copy & Patron',
237                                                 "Just Continue",
238                                                 null,
239                                                 "Check here to confirm this message"
240                                         ); 
241                                         if (pcheck == 0) {
242                                                 //FIXME//Re-implement
243                                                 /*
244                                                 var w = mw.spawn_main();
245                                                 setTimeout(
246                                                         function() {
247                                                                 mw.spawn_patron_display(w.document,'new_tab','main_tabbox',{'patron':patron});
248                                                                 mw.spawn_batch_copy_editor(w.document,'new_tab','main_tabbox',
249                                                                         {'copy_ids':[ check.copy.id() ]});
250                                                         }, 0
251                                                 );
252                                                 */
253                                         }
254                                 break;
255                                 case '3': case 3: /* TRANSIT ELSEWHERE */
256                                         if (parseInt(check.route_to)) check.route_to = data.hash.aou[ check.route_to ].shortname();
257                                         var msg = check.text + '\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
258                                                         check.record.title() + '  Author: ' + check.record.author() + 
259                                                         '\r\n' + 'Route To: ' + check.route_to + '\r\n';
260                                         var pcheck = error.yns_alert(
261                                                 msg,
262                                                 'Alert',
263                                                 'Print Receipt',
264                                                 "Don't Print",
265                                                 null,
266                                                 "Check here to confirm this message"
267                                         ); 
268                                         if (pcheck == 0) {
269                                                 alert('To Printer\n' + msg); //FIXME//
270                                                 //sPrint( msg.match( /\n/g, '<br />\r\n'), true );
271                                         }
272
273                                 break;
274                                 case '4': case 4: /* transit for hold is complete */
275                                         if (parseInt(check.route_to)) check.route_to = data.hash.aou[ check.route_to ].shortname();
276                                         var msg = check.text + '\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
277                                                         check.record.title() + '  Author: ' + check.record.author() + 
278                                                         '\r\n' + 'Route To: ' + check.route_to +
279                                                         '\r\n';
280                                         var pcheck = error.yns_alert(
281                                                 msg,
282                                                 'Alert',
283                                                 'Print Receipt',
284                                                 "Don't Print",
285                                                 null,
286                                                 "Check here to confirm this message"
287                                         ); 
288                                         if (pcheck == 0) {
289                                                 alert('To Printer\n' + msg); //FIXME//
290                                                 //sPrint( msg.match( /\n/g, '<br />\r\n'), true );
291                                         }
292
293                                 break;
294
295                                 default: 
296                                         if (parseInt(check.route_to)) check.route_to = data.hash.aou[ check.route_to ].shortname();
297                                         var msg = check.text + '\r\nBarcode: ' + barcode + '  Route To: ' + check.route_to;
298                                         var pcheck = error.yns_alert(
299                                                 msg,
300                                                 'Alert',
301                                                 'Print Receipt',
302                                                 "Don't Print",
303                                                 null,
304                                                 "Check here to confirm this message"
305                                         ); 
306                                         if (pcheck == 0) {
307                                                 alert('To Printer\n' + msg); //FIXME//
308                                                 //sPrint( msg.match( /\n/g, '<br />\r\n'), true );
309                                         }
310                                 break;
311                         }
312                 } else {  // status == 0
313                 }
314                 if (parseInt(check.route_to)) {
315                         if (check.route_to != data.list.au[0].home_ou()) {
316                                 check.route_to = data.hash.aou[ check.route_to ].shortname();
317                         } else {
318                                 check.route_to = data.hash.acpl[ check.copy.location() ].name();
319                         }
320                 }
321                 return check;
322         } catch(E) {
323                 JSAN.use('util.error'); var error = new util.error();
324                 var msg = E + '\n---\n' + js2JSON(E);
325                 error.sdump('D_ERROR',msg);
326                 alert(msg);
327                 return null;
328         }
329 }
330
331 circ.util.hold_capture_by_copy_barcode = function ( session, barcode, retrieve_flag ) {
332         try {
333                 JSAN.use('util.network'); var network = new util.network();
334                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
335                 var check = network.request(
336                         api.capture_copy_for_hold_via_barcode.app,
337                         api.capture_copy_for_hold_via_barcode.method,
338                         [ session, barcode, retrieve_flag ]
339                 )[0];
340                 check.text = 'Captured for Hold';
341                 if (parseInt(check.route_to)) check.route_to = data.hash.aou[ check.route_to ].shortname();
342                 return check;
343         } catch(E) {
344                 JSAN.use('util.error'); var error = new util.error();
345                 var msg = E + '\n---\n' + js2JSON(E);
346                 error.sdump('D_ERROR',msg);
347                 alert(msg);
348                 return null;
349         }
350 }
351
352
353 dump('exiting circ/util.js\n');