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