first stab at converting old checkin logic to staff client 3
[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'
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         for (var i = 0; i < c.length; i++) {
108                 if (modify[ c[i].id ]) {
109                         for (var j in modify[ c[i].id ]) {
110                                 c[i][j] = modify[ c[i].id ][j];
111                         }
112                 }
113         }
114         return c;
115 }
116
117 circ.util.std_map_row_to_column = function() {
118         return function(row,col) {
119                 // row contains { 'my' : { 'acp' : {}, 'circ' : {}, 'mvr' : {} } }
120                 // col contains one of the objects listed above in columns
121                 
122                 // mimicking some of the obj in circ.checkin and circ.checkout where map_row_to_column is usually defined
123                 var obj = {}; obj.OpenILS = {};  // One of our circ columns uses OpenILS.data
124                 JSAN.use('OpenILS.data'); obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
125
126                 var my = row.my;
127                 var value;
128                 try { 
129                         value = eval( col.render );
130                 } catch(E) {
131                         obj.error.sdump('D_ERROR','map_row_to_column: ' + E);
132                         value = '???';
133                 }
134                 return value;
135         }
136 }
137
138 circ.util.checkin_via_barcode = function(session,barcode,backdate) {
139         try {
140                 JSAN.use('util.error'); var error = new util.error();
141                 JSAN.use('util.network'); var network = new util.network();
142                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
143                 JSAN.use('util.date');
144                 if (backdate && (backdate == util.date.formatted_date(new Date(),'%Y-%m-%d')) ) backdate = null;
145
146                 var check = network.request(
147                         api.checkin_via_barcode.app,
148                         api.checkin_via_barcode.method,
149                         [ session, barcode, null, backdate ]
150                 );
151
152                 /*
153                 { // REMOVE_ME, forcing a condition for testing
154                         check.status = 1;
155                         check.text = 'This copy is the first that could fulfill a hold.  Do it?';
156                 }
157                 */
158
159                 if (check.status != 0) {
160                         switch(check.status) {
161                                 case '1': case 1: /* possible hold capture */
162                                         var rv = error.yns_alert(
163                                                 check.text,
164                                                 'Alert',
165                                                 "Capture",
166                                                 "Don't Capture",
167                                                 null,
168                                                 "Check here to confirm this message"
169                                         );
170                                         switch(rv) {
171                                                 case 0: /* capture */
172                                                 try {
173                                                         var check2 = this.hold_capture_by_copy_barcode( session, barcode );
174                                                         if (check2) {
175                                                                 check.copy = check2.copy;
176                                                                 check.text = check2.text;
177                                                                 check.route_to = check2.route_to;
178                                                                 JSAN.use('patron.util');
179                                                                 var patron = patron.util.retrieve_au_by_id( check.hold.usr() );
180                                                                 alert('To Printer\n' + check.text + '\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
181                                                                         check.record.title() + '  Author: ' + check.record.author() + 
182                                                                         '\r\n' + 'Route To: ' + check.route_to + '  Patron: ' + 
183                                                                         patron.card().barcode() + ' ' + patron.family_name() + ', ' + 
184                                                                         patron.first_given_name() + '\r\n'); //FIXME
185
186                                                                 /*
187                                                                 sPrint(check.text + '<br />\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
188                                                                         check.record.title() + '  Author: ' + check.record.author() + 
189                                                                         '<br />\r\n' + 'Route To: ' + check.route_to + '  Patron: ' + 
190                                                                         patron.card().barcode() + ' ' + patron.family_name() + ', ' + 
191                                                                         patron.first_given_name() + '<br />\r\n'
192                                                                 );
193                                                                 */
194
195                                                         }
196
197                                                 } catch(E) { 
198                                                         error.sdump('D_ERROR',E + '\n'); 
199                                                         /* 
200                                                         // demo testing 
201                                                         check.text = 'Captured for Hold';
202                                                         check.route_to = 'ARL-ATH';
203                                                         */
204                                                 }
205                                                 break;
206                                                 case 1: /* don't capture */
207
208                                                         check.text = 'Not Captured for Hold';
209                                                 break;
210                                         }
211                                 break;
212                                 case '2': case 2: /* LOST??? */
213                                         JSAN.use('patron.util');
214                                         var patron = patron.util.retrieve_au_by_id( check.circ.usr() );
215                                         var msg = check.text + '\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
216                                                         check.record.title() + '  Author: ' + check.record.author() + '\r\n' +
217                                                         'Patron: ' + patron.card().barcode() + ' ' + patron.family_name() + ', ' +
218                                                         patron.first_given_name();
219                                         var pcheck = error.yns_alert(
220                                                 msg,
221                                                 'Lost Item',
222                                                 'Edit Copy & Patron',
223                                                 "Just Continue",
224                                                 null,
225                                                 "Check here to confirm this message"
226                                         ); 
227                                         if (pcheck == 0) {
228                                                 //FIXME//Re-implement
229                                                 /*
230                                                 var w = mw.spawn_main();
231                                                 setTimeout(
232                                                         function() {
233                                                                 mw.spawn_patron_display(w.document,'new_tab','main_tabbox',{'patron':patron});
234                                                                 mw.spawn_batch_copy_editor(w.document,'new_tab','main_tabbox',
235                                                                         {'copy_ids':[ check.copy.id() ]});
236                                                         }, 0
237                                                 );
238                                                 */
239                                         }
240                                 break;
241                                 case '3': case 3: /* TRANSIT ELSEWHERE */
242                                         if (parseInt(check.route_to)) check.route_to = data.hash.aou[ check.route_to ].shortname();
243                                         var msg = check.text + '\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
244                                                         check.record.title() + '  Author: ' + check.record.author() + 
245                                                         '\r\n' + 'Route To: ' + check.route_to + '\r\n';
246                                         var pcheck = error.yns_alert(
247                                                 msg,
248                                                 'Alert',
249                                                 'Print Receipt',
250                                                 "Don't Print",
251                                                 null,
252                                                 "Check here to confirm this message"
253                                         ); 
254                                         if (pcheck == 0) {
255                                                 alert('To Printer\n' + msg); //FIXME//
256                                                 //sPrint( msg.match( /\n/g, '<br />\r\n'), true );
257                                         }
258
259                                 break;
260                                 case '4': case 4: /* transit for hold is complete */
261                                         if (parseInt(check.route_to)) check.route_to = data.hash.aou[ check.route_to ].shortname();
262                                         var msg = check.text + '\r\n' + 'Barcode: ' + barcode + '  Title: ' + 
263                                                         check.record.title() + '  Author: ' + check.record.author() + 
264                                                         '\r\n' + 'Route To: ' + check.route_to +
265                                                         '\r\n';
266                                         var pcheck = error.yns_alert(
267                                                 msg,
268                                                 'Alert',
269                                                 'Print Receipt',
270                                                 "Don't Print",
271                                                 null,
272                                                 "Check here to confirm this message"
273                                         ); 
274                                         if (pcheck == 0) {
275                                                 alert('To Printer\n' + msg); //FIXME//
276                                                 //sPrint( msg.match( /\n/g, '<br />\r\n'), true );
277                                         }
278
279                                 break;
280
281                                 default: 
282                                         if (parseInt(check.route_to)) check.route_to = data.hash.aou[ check.route_to ].shortname();
283                                         var msg = check.text + '\r\nBarcode: ' + barcode + '  Route To: ' + check.route_to;
284                                         var pcheck = error.yns_alert(
285                                                 msg,
286                                                 'Alert',
287                                                 'Print Receipt',
288                                                 "Don't Print",
289                                                 null,
290                                                 "Check here to confirm this message"
291                                         ); 
292                                         if (pcheck == 0) {
293                                                 alert('To Printer\n' + msg); //FIXME//
294                                                 //sPrint( msg.match( /\n/g, '<br />\r\n'), true );
295                                         }
296                                 break;
297                         }
298                 } else {  // status == 0
299                 }
300                 if (parseInt(check.route_to)) {
301                         if (check.route_to != data.list.au[0].home_ou()) {
302                                 check.route_to = data.hash.aou[ check.route_to ].shortname();
303                         } else {
304                                 check.route_to = data.hash.acpl[ check.copy.location() ].name();
305                         }
306                 }
307                 return check;
308         } catch(E) {
309                 JSAN.use('util.error'); var error = new util.error();
310                 var msg = E + '\n---\n' + js2JSON(E);
311                 error.sdump('D_ERROR',msg);
312                 alert(msg);
313                 return null;
314         }
315 }
316
317 circ.util.hold_capture_by_copy_barcode = function ( session, barcode, retrieve_flag ) {
318         try {
319                 JSAN.use('util.network'); var network = new util.network();
320                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
321                 var check = network.request(
322                         api.capture_copy_for_hold_via_barcode.app,
323                         api.capture_copy_for_hold_via_barcode.method,
324                         [ session, barcode, retrieve_flag ]
325                 )[0];
326                 check.text = 'Captured for Hold';
327                 if (parseInt(check.route_to)) check.route_to = data.hash.aou[ check.route_to ].shortname();
328                 return check;
329         } catch(E) {
330                 JSAN.use('util.error'); var error = new util.error();
331                 var msg = E + '\n---\n' + js2JSON(E);
332                 error.sdump('D_ERROR',msg);
333                 alert(msg);
334                 return null;
335         }
336 }
337
338
339 dump('exiting circ/util.js\n');