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