717f22170d1b764d08b239f803f41721e4bcd95e
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / display.js
1 dump('entering patron/display.js\n');
2
3 if (typeof patron == 'undefined') patron = {};
4 patron.display = function (params) {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('main.window'); this.window = new main.window();
8         JSAN.use('main.network'); this.network = new main.network();
9         this.w = window;
10 }
11
12 patron.display.prototype = {
13
14         'init' : function( params ) {
15
16                 var obj = this;
17
18                 obj.session = params['session'];
19                 obj.barcode = params['barcode'];
20
21                 JSAN.use('OpenILS.data'); this.OpenILS = {}; 
22                 obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
23
24                 JSAN.use('util.deck');  obj.deck = new util.deck('patron_deck');
25
26                 JSAN.use('main.controller'); obj.controller = new main.controller();
27                 obj.controller.init(
28                         {
29                                 control_map : {
30                                         'cmd_broken' : [
31                                                 ['command'],
32                                                 function() { alert('Not Yet Implemented'); }
33                                         ],
34                                         'cmd_patron_refresh' : [
35                                                 ['command'],
36                                                 function(ev) {
37                                                         obj.controller.view.patron_name.setAttribute(
38                                                                 'value','Retrieving...'
39                                                         );
40                                                         obj.retrieve();
41                                                 }
42                                         ],
43                                         'cmd_patron_checkout' : [
44                                                 ['command'],
45                                                 function(ev) {
46                                                         obj.deck.set_iframe(
47                                                                 urls.remote_checkout
48                                                                 + '?session=' + window.escape( obj.session )
49                                                                 + '&patron_id=' + window.escape( obj.patron.id() ),
50                                                                 {},
51                                                                 { 
52                                                                         'on_checkout' : function(checkout) {
53                                                                                 var c = obj.patron.checkouts();
54                                                                                 c.push( checkout );
55                                                                                 obj.patron.checkouts( c );
56                                                                                 obj.controller.render('patron_checkouts');
57                                                                         }
58                                                                 }
59                                                         );
60                                                         dump('obj.deck.node.childNodes.length = ' + obj.deck.node.childNodes.length + '\n');
61                                                 }
62                                         ],
63                                         'cmd_patron_items' : [
64                                                 ['command'],
65                                                 function(ev) {
66                                                         obj.deck.set_iframe(urls.remote_patron_items);
67                                                         dump('obj.deck.node.childNodes.length = ' + obj.deck.node.childNodes.length + '\n');
68                                                 }
69                                         ],
70                                         'cmd_patron_holds' : [
71                                                 ['command'],
72                                                 function(ev) {
73                                                         obj.deck.set_iframe(urls.remote_patron_holds);
74                                                         dump('obj.deck.node.childNodes.length = ' + obj.deck.node.childNodes.length + '\n');
75                                                 }
76                                         ],
77                                         'cmd_patron_bills' : [
78                                                 ['command'],
79                                                 function(ev) {
80                                                         obj.deck.set_iframe(urls.remote_patron_bills);
81                                                         dump('obj.deck.node.childNodes.length = ' + obj.deck.node.childNodes.length + '\n');
82                                                 }
83                                         ],
84                                         'cmd_patron_edit' : [
85                                                 ['command'],
86                                                 function(ev) {
87                                                         obj.deck.set_iframe(urls.remote_patron_edit);
88                                                         dump('obj.deck.node.childNodes.length = ' + obj.deck.node.childNodes.length + '\n');
89                                                 }
90                                         ],
91                                         'cmd_patron_info' : [
92                                                 ['command'],
93                                                 function(ev) {
94                                                         obj.deck.set_iframe(urls.remote_patron_info);
95                                                         dump('obj.deck.node.childNodes.length = ' + obj.deck.node.childNodes.length + '\n');
96                                                 }
97                                         ],
98                                         'patron_name' : [
99                                                 ['render'],
100                                                 function(e) {
101                                                         return function() { 
102                                                                 e.setAttribute('value',
103                                                                         obj.patron.family_name() + ', ' + obj.patron.first_given_name()
104                                                                 );
105                                                                 e.setAttribute('style','background-color: lime');
106                                                                 //FIXME//bills should become a virtual field
107                                                                 if (obj.patron.bills.length > 0)
108                                                                         e.setAttribute('style','background-color: yellow');
109                                                                 if (obj.patron.standing() == 2)
110                                                                         e.setAttribute('style','background-color: lightred');
111
112                                                         };
113                                                 }
114                                         ],
115                                         'patron_profile' : [
116                                                 ['render'],
117                                                 function(e) {
118                                                         return function() { 
119                                                                 e.setAttribute('value',
120                                                                         obj.OpenILS.data.hash.pgt[
121                                                                                 obj.patron.profile()
122                                                                         ].name()
123                                                                 );
124                                                         };
125                                                 }
126                                         ],
127                                         'patron_standing' : [
128                                                 ['render'],
129                                                 function(e) {
130                                                         return function() {
131                                                                 e.setAttribute('value',
132                                                                         obj.OpenILS.data.hash.cst[
133                                                                                 obj.patron.standing()
134                                                                         ].value()
135                                                                 );
136                                                         };
137                                                 }
138                                         ],
139                                         'patron_credit' : [
140                                                 ['render'],
141                                                 function(e) {
142                                                         return function() { 
143                                                                 JSAN.use('util.money');
144                                                                 e.setAttribute('value',
145                                                                         util.money.cents_as_dollars(
146                                                                                 obj.patron.credit_forward_balance()
147                                                                         )
148                                                                 );
149                                                         };
150                                                 }
151                                         ],
152                                         'patron_bill' : [
153                                                 ['render'],
154                                                 function(e) {
155                                                         return function() { 
156                                                                 JSAN.use('util.money');
157                                                                 var total = 0;
158                                                                 //FIXME//adjust when .bills becomes a virtual field
159                                                                 for (var i = 0; i < obj.patron.bills.length; i++) {
160                                                                         total += util.money.dollars_float_to_cents_integer( 
161                                                                                 obj.patron.bills[i].balance_owed() 
162                                                                         );
163                                                                 }
164                                                                 e.setAttribute('value',
165                                                                         util.money.cents_as_dollars( total )
166                                                                 );
167                                                         };
168                                                 }
169                                         ],
170                                         'patron_checkouts' : [
171                                                 ['render'],
172                                                 function(e) {
173                                                         return function() { 
174                                                                 e.setAttribute('value',
175                                                                         obj.patron.checkouts().length   
176                                                                 );
177                                                         };
178                                                 }
179                                         ],
180                                         'patron_overdue' : [
181                                                 ['render'],
182                                                 function(e) {
183                                                         return function() { 
184                                                                 //FIXME//Get Bill to do this correctly on server side
185                                                                 JSAN.use('util.date');
186                                                                 var total = 0;
187                                                                 for (var i = 0; i < obj.patron.checkouts().length; i++) {
188                                                                         var item = obj.patron.checkouts()[i];
189                                                                         var due_date = item.circ.due_date();
190                                                                         due_date = due_date.substr(0,4) 
191                                                                                 + due_date.substr(5,2) + due_date.substr(8,2);
192                                                                         var today = util.date.formatted_date( new Date() , '%Y%m%d' );
193                                                                         if (today > due_date) total++;
194                                                                 }
195                                                                 e.setAttribute('value',
196                                                                         total
197                                                                 );
198                                                         };
199                                                 }
200                                         ],
201                                         'patron_holds' : [
202                                                 ['render'],
203                                                 function(e) {
204                                                         return function() { 
205                                                                 e.setAttribute('value',
206                                                                         obj.patron.hold_requests.length
207                                                                 );
208                                                         };
209                                                 }
210                                         ],
211                                         'patron_holds_available' : [
212                                                 ['render'],
213                                                 function(e) {
214                                                         return function() { 
215                                                                 var total = 0;
216                                                                 for (var i = 0; i < obj.patron.hold_requests().length; i++) {
217                                                                         var hold = obj.patron.hold_requests()[i];
218                                                                         if (hold.capture_time()) total++;
219                                                                 }
220                                                                 e.setAttribute('value',
221                                                                         total
222                                                                 );
223                                                         };
224                                                 }
225                                         ],
226                                         'patron_card' : [
227                                                 ['render'],
228                                                 function(e) {
229                                                         return function() { 
230                                                                 e.setAttribute('value',
231                                                                         obj.patron.card().barcode()
232                                                                 );
233                                                         };
234                                                 }
235                                         ],
236                                         'patron_ident_type_1' : [
237                                                 ['render'],
238                                                 function(e) {
239                                                         return function() { 
240                                                                 var ident_string = '';
241                                                                 var ident = obj.OpenILS.data.hash.cit[
242                                                                         obj.patron.ident_type()
243                                                                 ];
244                                                                 if (ident) ident_string = ident.name()
245                                                                 e.setAttribute('value',
246                                                                         ident_string
247                                                                 );
248                                                         };
249                                                 }
250                                         ],
251                                         'patron_ident_value_1' : [
252                                                 ['render'],
253                                                 function(e) {
254                                                         return function() { 
255                                                                 e.setAttribute('value',
256                                                                         obj.patron.ident_value()
257                                                                 );
258                                                         };
259                                                 }
260                                         ],
261                                         'patron_ident_type_2' : [
262                                                 ['render'],
263                                                 function(e) {
264                                                         return function() { 
265                                                                 var ident_string = '';
266                                                                 var ident = obj.OpenILS.data.hash.cit[
267                                                                         obj.patron.ident_type2()
268                                                                 ];
269                                                                 if (ident) ident_string = ident.name()
270                                                                 e.setAttribute('value',
271                                                                         ident_string
272                                                                 );
273                                                         };
274                                                 }
275                                         ],
276                                         'patron_ident_value_2' : [
277                                                 ['render'],
278                                                 function(e) {
279                                                         return function() { 
280                                                                 e.setAttribute('value',
281                                                                         obj.patron.ident_value2()
282                                                                 );
283                                                         };
284                                                 }
285                                         ],
286                                         'patron_date_of_birth' : [
287                                                 ['render'],
288                                                 function(e) {
289                                                         return function() { 
290                                                                 e.setAttribute('value',
291                                                                         obj.patron.dob()
292                                                                 );
293                                                         };
294                                                 }
295                                         ],
296                                         'patron_day_phone' : [
297                                                 ['render'],
298                                                 function(e) {
299                                                         return function() { 
300                                                                 e.setAttribute('value',
301                                                                         obj.patron.day_phone()
302                                                                 );
303                                                         };
304                                                 }
305                                         ],
306                                         'patron_evening_phone' : [
307                                                 ['render'],
308                                                 function(e) {
309                                                         return function() { 
310                                                                 e.setAttribute('value',
311                                                                         obj.patron.evening_phone()
312                                                                 );
313                                                         };
314                                                 }
315                                         ],
316                                         'patron_other_phone' : [
317                                                 ['render'],
318                                                 function(e) {
319                                                         return function() { 
320                                                                 e.setAttribute('value',
321                                                                         obj.patron.other_phone()
322                                                                 );
323                                                         };
324                                                 }
325                                         ],
326                                         'patron_email' : [
327                                                 ['render'],
328                                                 function(e) {
329                                                         return function() { 
330                                                                 e.setAttribute('value',
331                                                                         obj.patron.email()
332                                                                 );
333                                                         };
334                                                 }
335                                         ],
336                                         'patron_photo_url' : [
337                                                 ['render'],
338                                                 function(e) {
339                                                         return function() { 
340                                                                 e.setAttribute('src',
341                                                                         obj.patron.photo_url()
342                                                                 );
343                                                         };
344                                                 }
345                                         ],
346                                         'patron_library' : [
347                                                 ['render'],
348                                                 function(e) {
349                                                         return function() { 
350                                                                 e.setAttribute('value',
351                                                                         obj.OpenILS.data.hash.aou[
352                                                                                 obj.patron.home_ou()
353                                                                         ].shortname()
354                                                                 );
355                                                                 e.setAttribute('tooltiptext',
356                                                                         obj.OpenILS.data.hash.aou[
357                                                                                 obj.patron.home_ou()
358                                                                         ].name()
359                                                                 );
360                                                         };
361                                                 }
362                                         ],
363                                         'patron_last_library' : [
364                                                 ['render'],
365                                                 function(e) {
366                                                         return function() { 
367                                                                 e.setAttribute('value',
368                                                                         obj.OpenILS.data.hash.aou[
369                                                                                 obj.patron.home_ou()
370                                                                         ].shortname()
371                                                                 );
372                                                                 e.setAttribute('tooltiptext',
373                                                                         obj.OpenILS.data.hash.aou[
374                                                                                 obj.patron.home_ou()
375                                                                         ].name()
376                                                                 );
377                                                         };
378                                                 }
379                                         ],
380                                         'patron_mailing_address_street1' : [
381                                                 ['render'],
382                                                 function(e) {
383                                                         return function() { 
384                                                                 e.setAttribute('value',
385                                                                         obj.patron.mailing_address().street1()
386                                                                 );
387                                                         };
388                                                 }
389                                         ],
390                                         'patron_mailing_address_street2' : [
391                                                 ['render'],
392                                                 function(e) {
393                                                         return function() { 
394                                                                 e.setAttribute('value',
395                                                                         obj.patron.mailing_address().street2()
396                                                                 );
397                                                         };
398                                                 }
399                                         ],
400                                         'patron_mailing_address_city' : [
401                                                 ['render'],
402                                                 function(e) {
403                                                         return function() { 
404                                                                 e.setAttribute('value',
405                                                                         obj.patron.mailing_address().city()
406                                                                 );
407                                                         };
408                                                 }
409                                         ],
410                                         'patron_mailing_address_state' : [
411                                                 ['render'],
412                                                 function(e) {
413                                                         return function() { 
414                                                                 e.setAttribute('value',
415                                                                         obj.patron.mailing_address().state()
416                                                                 );
417                                                         };
418                                                 }
419                                         ],
420                                         'patron_mailing_address_post_code' : [
421                                                 ['render'],
422                                                 function(e) {
423                                                         return function() { 
424                                                                 e.setAttribute('value',
425                                                                         obj.patron.mailing_address().post_code()
426                                                                 );
427                                                         };
428                                                 }
429                                         ],
430                                         'patron_physical_address_street1' : [
431                                                 ['render'],
432                                                 function(e) {
433                                                         return function() { 
434                                                                 e.setAttribute('value',
435                                                                         obj.patron.billing_address().street1()
436                                                                 );
437                                                         };
438                                                 }
439                                         ],
440                                         'patron_physical_address_street2' : [
441                                                 ['render'],
442                                                 function(e) {
443                                                         return function() { 
444                                                                 e.setAttribute('value',
445                                                                         obj.patron.billing_address().street2()
446                                                                 );
447                                                         };
448                                                 }
449                                         ],
450                                         'patron_physical_address_city' : [
451                                                 ['render'],
452                                                 function(e) {
453                                                         return function() { 
454                                                                 e.setAttribute('value',
455                                                                         obj.patron.billing_address().city()
456                                                                 );
457                                                         };
458                                                 }
459                                         ],
460                                         'patron_physical_address_state' : [
461                                                 ['render'],
462                                                 function(e) {
463                                                         return function() { 
464                                                                 e.setAttribute('value',
465                                                                         obj.patron.billing_address().state()
466                                                                 );
467                                                         };
468                                                 }
469                                         ],
470                                         'patron_physical_address_post_code' : [
471                                                 ['render'],
472                                                 function(e) {
473                                                         return function() { 
474                                                                 e.setAttribute('value',
475                                                                         obj.patron.billing_address().post_code()
476                                                                 );
477                                                         };
478                                                 }
479                                         ]
480                                 }
481                         }
482                 );
483
484                 obj.retrieve();
485
486         },
487
488         'retrieve' : function() {
489
490                 var patron;
491                 try {
492
493                         var obj = this;
494
495                         var chain = [];
496
497                         // Retrieve the patron
498                         chain.push(
499                                 function() {
500                                         try {
501                                                 var patron = obj.network.request(
502                                                         api.fm_au_retrieve_via_barcode.app,
503                                                         api.fm_au_retrieve_via_barcode.method,
504                                                         [ obj.session, obj.barcode ]
505                                                 );
506                                                 if (patron) {
507
508                                                         if (instanceOf(patron,au)) {
509
510                                                                 obj.patron = patron;
511
512                                                         } else {
513
514                                                                 throw('patron is not an au fm object');
515                                                         }
516                                                 } else {
517
518                                                         throw('patron == false');
519                                                 }
520
521                                         } catch(E) {
522                                                 var error = ('patron.display.retrieve : ' + js2JSON(E));
523                                                 obj.error.sdump('D_ERROR',error);
524                                                 throw(error);
525                                         }
526                                 }
527                         );
528
529                         // Retrieve the bills
530                         chain.push(
531                                 function() {
532                                         try {
533                                                 var bills = obj.network.request(
534                                                         api.fm_mobts_having_balance.app,
535                                                         api.fm_mobts_having_balance.method,
536                                                         [ obj.session, obj.patron.id() ]
537                                                 );
538                                                 //FIXME// obj.patron.bills( bills );
539                                                 obj.patron.bills = bills;
540                                         } catch(E) {
541                                                 var error = ('patron.display.retrieve : ' + js2JSON(E));
542                                                 obj.error.sdump('D_ERROR',error);
543                                                 throw(error);
544                                         }
545                                 }
546                         );
547
548                         // Retrieve the checkouts
549                         chain.push(
550                                 function() {
551                                         try {
552                                                 var checkouts = obj.network.request(
553                                                         api.blob_checkouts_retrieve.app,
554                                                         api.blob_checkouts_retrieve.method,
555                                                         [ obj.session, obj.patron.id() ]
556                                                 );
557                                                 obj.patron.checkouts( checkouts );
558                                         } catch(E) {
559                                                 var error = ('patron.display.retrieve : ' + js2JSON(E));
560                                                 obj.error.sdump('D_ERROR',error);
561                                                 throw(error);
562                                         }
563                                 }
564                         );
565
566                         // Retrieve the holds
567                         chain.push(
568                                 function() {
569                                         try {
570                                                 var holds = obj.network.request(
571                                                         api.fm_ahr_retrieve.app,
572                                                         api.fm_ahr_retrieve.method,
573                                                         [ obj.session, obj.patron.id() ]
574                                                 );
575                                                 obj.patron.hold_requests( holds );
576                                         } catch(E) {
577                                                 var error = ('patron.display.retrieve : ' + js2JSON(E));
578                                                 obj.error.sdump('D_ERROR',error);
579                                                 throw(error);
580                                         }
581                                 }
582                         );
583
584                         // Update the screen
585                         chain.push( function() { obj.controller.render(); } );
586
587                         // Do it
588                         JSAN.use('util.exec'); obj.exec = new util.exec();
589                         obj.exec.on_error = function(E) {
590                                 location.href = urls.remote_patron_barcode_entry + '?session=' + window.escape(obj.session);
591                                 alert('FIXME: Need better alert and error handling.\nProblem with barcode.\n' + E);
592                         }
593                         this.exec.chain( chain );
594
595                 } catch(E) {
596                         var error = ('patron.display.retrieve : ' + js2JSON(E));
597                         this.error.sdump('D_ERROR',error);
598                         alert(error);
599                 }
600         }
601 }
602
603 dump('exiting patron/display.js\n');