penny math
[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.barcode = params['barcode'];
19                 obj.id = params['id'];
20                 if (params['show_name']) {
21                         document.getElementById('patron_name').hidden = false;
22                         document.getElementById('patron_name').setAttribute('hidden','false');
23                 }
24
25                 JSAN.use('OpenILS.data'); this.OpenILS = {}; 
26                 obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
27
28                 JSAN.use('util.controller'); obj.controller = new util.controller();
29                 obj.controller.init(
30                         {
31                                 control_map : {
32                                         'cmd_broken' : [
33                                                 ['command'],
34                                                 function() { alert('Not Yet Implemented'); }
35                                         ],
36                                         'patron_alert' : [
37                                                 ['render'],
38                                                 function(e) {
39                                                         return function() {
40                                                                 JSAN.use('util.widgets');
41                                                                 util.widgets.remove_children( e );
42                                                                 if (obj.patron.alert_message()) {
43                                                                         e.appendChild(
44                                                                                 document.createTextNode(
45                                                                                         obj.patron.alert_message()
46                                                                                 )
47                                                                         );
48                                                                         e.parentNode.hidden = false;
49                                                                 } else {
50                                                                         e.parentNode.hidden = true;
51                                                                 }
52                                                         };
53                                                 }
54                                         ],
55                                         'patron_usrname' : [
56                                                 ['render'],
57                                                 function(e) {
58                                                         return function() {
59                                                                 e.setAttribute('value',obj.patron.usrname());
60                                                         };
61                                                 }
62                                         ],
63                                         'patron_profile' : [
64                                                 ['render'],
65                                                 function(e) {
66                                                         return function() { 
67                                                                 e.setAttribute('value',
68                                                                         obj.OpenILS.data.hash.pgt[
69                                                                                 obj.patron.profile()
70                                                                         ].name()
71                                                                 );
72                                                         };
73                                                 }
74                                         ],
75                                         'patron_net_access' : [
76                                                 ['render'],
77                                                 function(e) {
78                                                         return function() { 
79                                                                 e.setAttribute('value',
80                                                                         'Internet: ' + 
81                                                                         obj.OpenILS.data.hash.cnal[
82                                                                                 obj.patron.net_access_level()
83                                                                         ].name()
84                                                                 );
85                                                         };
86                                                 }
87                                         ],
88                                         'patron_standing' : [
89                                                 ['render'],
90                                                 function(e) {
91                                                         return function() {
92                                                         /*
93                                                                 e.setAttribute('value',
94                                                                         obj.OpenILS.data.hash.cst[
95                                                                                 obj.patron.standing()
96                                                                         ].value()
97                                                                 );
98                                                         */
99                                                                 var e2 = document.getElementById('patron_standing_penalties');
100                                                                 JSAN.use('util.widgets');
101                                                                 util.widgets.remove_children(e2);
102                                                                 var penalties = obj.patron.standing_penalties();
103                                                                 for (var i = 0; i < penalties.length; i++) {
104                                                                         var x = document.createElement('label');
105                                                                         x.setAttribute('value',penalties[i].penalty_type());
106                                                                         e2.appendChild(x);
107                                                                 }
108                                                         };
109                                                 }
110                                         ],
111                                         'patron_credit' : [
112                                                 ['render'],
113                                                 function(e) {
114                                                         return function() { 
115                                                                 JSAN.use('util.money');
116                                                                 e.setAttribute('value',
117                                                                         '$' + 
118                                                                         util.money.sanitize(
119                                                                                 obj.patron.credit_forward_balance()
120                                                                         )
121                                                                 );
122                                                         };
123                                                 }
124                                         ],
125                                         'patron_bill' : [
126                                                 ['render'],
127                                                 function(e) {
128                                                         return function() { 
129                                                                 e.setAttribute('value','...');
130                                                                 obj.network.simple_request(
131                                                                         'FM_MBTS_IDS_RETRIEVE_ALL_HAVING_BALANCE',
132                                                                         [ ses(), obj.patron.id() ],
133                                                                         function(req) {
134                                                                                 JSAN.use('util.money');
135                                                                                 var list = req.getResultObject();
136                                                                                 if (typeof list.ilsevent != 'undefined') {
137                                                                                         e.setAttribute('value', '??? See Bills');
138                                                                                         return;
139                                                                                 }
140                                                                                 var sum = 0;
141                                                                                 for (var i = 0; i < list.length; i++) {
142                                                                                         var robj = typeof list[i] == 'object' ? list[i] : obj.network.simple_request('FM_MBTS_RETRIEVE',[ses(),list[i]]);
143                                                                                         sum += util.money.dollars_float_to_cents_integer( robj.balance_owed() );
144                                                                                 } 
145                                                                                 if (sum > 0) addCSSClass(document.documentElement,'PATRON_HAS_BILLS');
146                                                                                 JSAN.use('util.money');
147                                                                                 e.setAttribute('value', '$' + util.money.sanitize( util.money.cents_as_dollars( sum ) ));
148                                                                         }
149                                                                 );
150                                                         };
151                                                 }
152                                         ],
153                                         'patron_checkouts' : [
154                                                 ['render'],
155                                                 function(e) {
156                                                         return function() { 
157                                                                 e.setAttribute('value','...');
158                                                                 var e2 = document.getElementById( 'patron_overdue' ); if (e2) e2.setAttribute('value','...');
159                                                                 var e3 = document.getElementById( 'patron_claimed_returned' ); if (e3) e3.setAttribute('value','...');
160                                                                 var e4 = document.getElementById( 'patron_long_overdue' ); if (e4) e4.setAttribute('value','...');
161                                                                 var e5 = document.getElementById( 'patron_lost' ); if (e5) e5.setAttribute('value','...');
162                                                                 var e6 = document.getElementById( 'patron_noncat' ); if (e6) e6.setAttribute('value','...');
163                                                                 obj.network.simple_request(
164                                                                         'FM_CIRC_COUNT_RETRIEVE_VIA_USER',
165                                                                         [ ses(), obj.patron.id() ],
166                                                                         function(req) {
167                                                                                 var robj = req.getResultObject();
168                                                                                 e.setAttribute('value', robj.out + robj.overdue + robj.claims_returned + robj.long_overdue );
169                                                                                 if (e2) e2.setAttribute('value', robj.overdue   );
170                                                                                 if (e3) e3.setAttribute('value', robj.claims_returned   );
171                                                                                 if (e4) e4.setAttribute('value', robj.long_overdue      );
172                                                                                 if (e5) e5.setAttribute('value', robj.lost      );
173                                                                         }
174                                                                 );
175                                                                 obj.network.simple_request(
176                                                                         'FM_ANCC_RETRIEVE_VIA_USER',
177                                                                         [ ses(), obj.patron.id() ],
178                                                                         function(req) {
179                                                                                 var robj = req.getResultObject();
180                                                                                 if (e6) e6.setAttribute('value',robj.length);
181                                                                         }
182                                                                 );
183                                                         };
184                                                 }
185                                         ],
186                                         'patron_overdue' : [
187                                                 ['render'],
188                                                 function(e) {
189                                                         return function() { 
190                                                                 /* handled by 'patron_checkouts' */
191                                                         };
192                                                 }
193                                         ],
194                                         'patron_holds' : [
195                                                 ['render'],
196                                                 function(e) {
197                                                         return function() { 
198                                                                 e.setAttribute('value','...');
199                                                                 var e2 = document.getElementById('patron_holds_available');
200                                                                 if (e2) e2.setAttribute('value','...');
201                                                                 obj.network.simple_request(
202                                                                         'FM_AHR_COUNT_RETRIEVE',
203                                                                         [ ses(), obj.patron.id() ],
204                                                                         function(req) {
205                                                                                 e.setAttribute('value',
206                                                                                         req.getResultObject().total
207                                                                                 );
208                                                                                 if (e2) e2.setAttribute('value',
209                                                                                         req.getResultObject().ready
210                                                                                 );
211                                                                         }
212                                                                 );
213                                                         };
214                                                 }
215                                         ],
216                                         'patron_holds_available' : [
217                                                 ['render'],
218                                                 function(e) {
219                                                         return function() { 
220                                                                 /* handled by 'patron_holds' */
221                                                         };
222                                                 }
223                                         ],
224                                         'patron_card' : [
225                                                 ['render'],
226                                                 function(e) {
227                                                         return function() { 
228                                                                 e.setAttribute('value',
229                                                                         obj.patron.card().barcode()
230                                                                 );
231                                                         };
232                                                 }
233                                         ],
234                                         'patron_ident_type_1' : [
235                                                 ['render'],
236                                                 function(e) {
237                                                         return function() { 
238                                                                 var ident_string = '';
239                                                                 var ident = obj.OpenILS.data.hash.cit[
240                                                                         obj.patron.ident_type()
241                                                                 ];
242                                                                 if (ident) ident_string = ident.name()
243                                                                 e.setAttribute('value',
244                                                                         ident_string
245                                                                 );
246                                                         };
247                                                 }
248                                         ],
249                                         'patron_ident_value_1' : [
250                                                 ['render'],
251                                                 function(e) {
252                                                         return function() { 
253                                                                 e.setAttribute('value',
254                                                                         obj.patron.ident_value()
255                                                                 );
256                                                         };
257                                                 }
258                                         ],
259                                         'patron_ident_type_2' : [
260                                                 ['render'],
261                                                 function(e) {
262                                                         return function() { 
263                                                                 var ident_string = '';
264                                                                 var ident = obj.OpenILS.data.hash.cit[
265                                                                         obj.patron.ident_type2()
266                                                                 ];
267                                                                 if (ident) ident_string = ident.name()
268                                                                 e.setAttribute('value',
269                                                                         ident_string
270                                                                 );
271                                                         };
272                                                 }
273                                         ],
274                                         'patron_ident_value_2' : [
275                                                 ['render'],
276                                                 function(e) {
277                                                         return function() { 
278                                                                 e.setAttribute('value',
279                                                                         obj.patron.ident_value2()
280                                                                 );
281                                                         };
282                                                 }
283                                         ],
284                                         'patron_date_of_birth' : [
285                                                 ['render'],
286                                                 function(e) {
287                                                         return function() { 
288                                                                 e.setAttribute('value',
289                                                                         obj.patron.dob() ?
290                                                                         obj.patron.dob().substr(0,10) :
291                                                                         '<Unset>'
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                                                                 if (!get_bool(obj.patron.mailing_address().valid())){e.setAttribute('style','color: red');}
388                                                         };
389                                                 }
390                                         ],
391                                         'patron_mailing_address_street2' : [
392                                                 ['render'],
393                                                 function(e) {
394                                                         return function() { 
395                                                                 e.setAttribute('value',
396                                                                         obj.patron.mailing_address().street2()
397                                                                 );
398                                                                 if (!get_bool(obj.patron.mailing_address().valid())){e.setAttribute('style','color: red');}
399                                                         };
400                                                 }
401                                         ],
402                                         'patron_mailing_address_city' : [
403                                                 ['render'],
404                                                 function(e) {
405                                                         return function() { 
406                                                                 e.setAttribute('value',
407                                                                         obj.patron.mailing_address().city()
408                                                                 );
409                                                                 if (!get_bool(obj.patron.mailing_address().valid())){e.setAttribute('style','color: red');}
410                                                         };
411                                                 }
412                                         ],
413                                         'patron_mailing_address_state' : [
414                                                 ['render'],
415                                                 function(e) {
416                                                         return function() { 
417                                                                 e.setAttribute('value',
418                                                                         obj.patron.mailing_address().state()
419                                                                 );
420                                                                 if (!get_bool(obj.patron.mailing_address().valid())){e.setAttribute('style','color: red');}
421                                                         };
422                                                 }
423                                         ],
424                                         'patron_mailing_address_post_code' : [
425                                                 ['render'],
426                                                 function(e) {
427                                                         return function() { 
428                                                                 e.setAttribute('value',
429                                                                         obj.patron.mailing_address().post_code()
430                                                                 );
431                                                                 if (!get_bool(obj.patron.mailing_address().valid())){e.setAttribute('style','color: red');}
432                                                         };
433                                                 }
434                                         ],
435                                         'patron_physical_address_street1' : [
436                                                 ['render'],
437                                                 function(e) {
438                                                         return function() { 
439                                                                 e.setAttribute('value',
440                                                                         obj.patron.billing_address().street1()
441                                                                 );
442                                                                 if (!get_bool(obj.patron.billing_address().valid())){e.setAttribute('style','color: red');}
443                                                         };
444                                                 }
445                                         ],
446                                         'patron_physical_address_street2' : [
447                                                 ['render'],
448                                                 function(e) {
449                                                         return function() { 
450                                                                 e.setAttribute('value',
451                                                                         obj.patron.billing_address().street2()
452                                                                 );
453                                                                 if (!get_bool(obj.patron.billing_address().valid())){e.setAttribute('style','color: red');}
454                                                         };
455                                                 }
456                                         ],
457                                         'patron_physical_address_city' : [
458                                                 ['render'],
459                                                 function(e) {
460                                                         return function() { 
461                                                                 e.setAttribute('value',
462                                                                         obj.patron.billing_address().city()
463                                                                 );
464                                                                 if (!get_bool(obj.patron.billing_address().valid())){e.setAttribute('style','color: red');}
465                                                         };
466                                                 }
467                                         ],
468                                         'patron_physical_address_state' : [
469                                                 ['render'],
470                                                 function(e) {
471                                                         return function() { 
472                                                                 e.setAttribute('value',
473                                                                         obj.patron.billing_address().state()
474                                                                 );
475                                                                 if (!get_bool(obj.patron.billing_address().valid())){e.setAttribute('style','color: red');}
476                                                         };
477                                                 }
478                                         ],
479                                         'patron_physical_address_post_code' : [
480                                                 ['render'],
481                                                 function(e) {
482                                                         return function() { 
483                                                                 e.setAttribute('value',
484                                                                         obj.patron.billing_address().post_code()
485                                                                 );
486                                                                 if (!get_bool(obj.patron.billing_address().valid())){e.setAttribute('style','color: red');}
487                                                         };
488                                                 }
489                                         ]
490                                 }
491                         }
492                 );
493
494                 obj.retrieve();
495
496                 try {
497                         var caption = document.getElementById("PatronSummaryContact_caption");
498                         var arrow = document.getAnonymousNodes(caption)[0];
499                         var gb_content = document.getAnonymousNodes(caption.parentNode)[1];
500                         arrow.addEventListener(
501                                 'click',
502                                 function() {
503                                         setTimeout(
504                                                 function() {
505                                                         //alert('setting shrink_state to ' + gb_content.hidden);
506                                                         //caption.setAttribute('shrink_state',gb_content.hidden);
507                                                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
508                                                         JSAN.use('util.file'); var file = new util.file('patron_id_shrink');
509                                                         file.set_object(String(gb_content.hidden)); file.close();
510                                                 }, 0
511                                         );
512                                 }, false
513                         );
514                         //var shrink_state = caption.getAttribute('shrink_state');
515                         var shrink_state = false;
516                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
517                         JSAN.use('util.file'); var file = new util.file('patron_id_shrink');
518                         if (file._file.exists()) {
519                                 shrink_state = file.get_object(); file.close();
520                         }
521                         //alert('shrink_state retrieved as ' + shrink_state);
522                         if (shrink_state != 'false' && shrink_state) {
523                                 JSAN.use('util.widgets');
524                                 //alert('clicking the widget');
525                                 util.widgets.click( arrow );
526                         }
527                 } catch(E) {
528                         obj.error.sdump('D_ERROR','with shrink_state in summary.js: ' + E);
529                 }
530         },
531
532         'retrieve' : function() {
533
534                 try {
535
536                         var obj = this;
537
538                         var chain = [];
539
540                         // Retrieve the patron
541                                 function blah_retrieve() {
542                                         try {
543                                                 var robj;
544                                                 if (obj.barcode && obj.barcode != 'null') {
545                                                         robj = obj.network.request(
546                                                                 api.FM_AU_RETRIEVE_VIA_BARCODE.app,
547                                                                 api.FM_AU_RETRIEVE_VIA_BARCODE.method,
548                                                                 [ ses(), obj.barcode ]
549                                                         );
550                                                 } else if (obj.id && obj.id != 'null') {
551                                                         robj = obj.network.simple_request(
552                                                                 'FM_AU_FLESHED_RETRIEVE_VIA_ID',
553                                                                 [ ses(), obj.id ]
554                                                         );
555                                                 } else {
556                                                         throw('summary: No barcode or id');
557                                                 }
558                                                 if (robj) {
559
560                                                         if (instanceOf(robj,au)) {
561
562                                                                 obj.patron = robj;
563                                                                 JSAN.use('patron.util');
564                                                                 document.getElementById('patron_name').setAttribute('value',
565                                                                         obj.patron.family_name() + ', ' + obj.patron.first_given_name() + ' ' +
566                                                                         ( obj.patron.second_given_name() ? obj.patron.second_given_name() : '' )
567                                                                 );
568                                                                 patron.util.set_penalty_css(obj.patron);
569                                                                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
570                                                                 data.last_patron = obj.patron.id(); data.stash('last_patron');
571
572                                                         } else {
573
574                                                                 throw(robj);
575
576                                                         }
577                                                 } else {
578
579                                                         throw(robj);
580
581                                                 }
582
583                                         } catch(E) {
584                                                 throw(E);
585                                         }
586                                 };
587                                 blah_retrieve();
588
589                         /*
590                         // Retrieve the survey responses for required surveys
591                         chain.push(
592                                 function() {
593                                         try {
594                                                 var surveys = obj.OpenILS.data.list.my_asv;
595                                                 var survey_responses = {};
596                                                 for (var i = 0; i < surveys.length; i++) {
597                                                         var s = obj.network.request(
598                                                                 api.FM_ASVR_RETRIEVE.app,
599                                                                 api.FM_ASVR_RETRIEVE.method,
600                                                                 [ ses(), surveys[i].id(), obj.patron.id() ]
601                                                         );
602                                                         survey_responses[ surveys[i].id() ] = s;
603                                                 }
604                                                 obj.patron.survey_responses( survey_responses );
605                                         } catch(E) {
606                                                 var error = ('patron.summary.retrieve : ' + js2JSON(E));
607                                                 obj.error.sdump('D_ERROR',error);
608                                                 throw(error);
609                                         }
610                                 }
611                         );
612                         */
613
614                         // Update the screen
615                         chain.push( function() { obj.controller.render(); } );
616
617                         // On Complete
618
619                         chain.push( function() {
620
621                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_finished == 'function') {
622                                         obj.error.sdump('D_PATRON_SUMMARY',
623                                                 'patron.summary: Calling external .on_finished()\n');
624                                         window.xulG.on_finished(obj.patron);
625                                 } else {
626                                         obj.error.sdump('D_PATRON_SUMMARY','patron.summary: No external .on_finished()\n');
627                                 }
628
629                         } );
630
631                         // Do it
632                         JSAN.use('util.exec'); obj.exec = new util.exec();
633                         obj.exec.on_error = function(E) {
634
635                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_error == 'function') {
636                                         window.xulG.on_error(E);
637                                 } else {
638                                         alert(js2JSON(E));
639                                 }
640
641                         }
642                         this.exec.chain( chain );
643
644                 } catch(E) {
645                         if (typeof window.xulG == 'object' && typeof window.xulG.on_error == 'function') {
646                                 window.xulG.on_error(E);
647                         } else {
648                                 alert(js2JSON(E));
649                         }
650                 }
651         }
652 }
653
654 dump('exiting patron.summary.js\n');