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