]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/display.js
toward patron display
[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                 this.session = params['session'];
17                 this.barcode = params['barcode'];
18
19                 JSAN.use('OpenILS.data'); this.OpenILS = {}; 
20                 this.OpenILS.data = new OpenILS.data( { 'session' : params.session } ); this.OpenILS.data.init(true);
21
22                 var obj = this;
23                 obj.view = {}; obj.render_list = [];
24
25                 var control_map = {
26                         'cmd_broken' : [
27                                 ['command'],
28                                 function() { alert('Not Yet Implemented'); }
29                         ],
30                         'patron_name' : [
31                                 ['render'],
32                                 function(e) {
33                                         return function() { 
34                                                 e.setAttribute('value',obj.patron.family_name() + ', ' 
35                                                         + obj.patron.first_given_name());
36                                         };
37                                 }
38                         ],
39                         'patron_profile' : [
40                                 ['render'],
41                                 function(e) {
42                                         return function() { 
43                                                 e.setAttribute('value',
44                                                         obj.OpenILS.data.hash.pgt[
45                                                                 obj.patron.profile()
46                                                         ].name()
47                                                 );
48                                         };
49                                 }
50                         ],
51                         'patron_credit' : [
52                                 ['render'],
53                                 function(e) {
54                                         return function() { 
55                                                 JSAN.use('util.money');
56                                                 e.setAttribute('value',
57                                                         util.money.cents_as_dollars(
58                                                                 obj.patron.credit_forward_balance()
59                                                         )
60                                                 );
61                                         };
62                                 }
63                         ],
64                         'patron_bill' : [
65                                 ['render'],
66                                 function(e) {
67                                         return function() { 
68                                                 JSAN.use('util.money');
69                                                 var total = 0;
70                                                 //FIXME//adjust when .bills becomes a virtual field
71                                                 for (var i = 0; i < obj.patron.bills.length; i++) {
72                                                         total += util.money.dollars_float_to_cents_integer( 
73                                                                 obj.patron.bills[i].balance_owed() 
74                                                         );
75                                                 }
76                                                 e.setAttribute('value',
77                                                         util.money.cents_as_dollars( total )
78                                                 );
79                                         };
80                                 }
81                         ],
82                         'patron_checkouts' : [
83                                 ['render'],
84                                 function(e) {
85                                         return function() { 
86                                                 e.setAttribute('value',
87                                                         obj.patron.checkouts.length     
88                                                 );
89                                         };
90                                 }
91                         ],
92                         'patron_overdue' : [
93                                 ['render'],
94                                 function(e) {
95                                         return function() { 
96                                                 //FIXME//Get Bill to do this correctly on server side
97                                                 JSAN.use('util.date');
98                                                 var total = 0;
99                                                 for (var i = 0; i < obj.patron.checkouts().length; i++) {
100                                                         var item = obj.patron.checkouts()[i];
101                                                         var due_date = item.circ.due_date();
102                                                         due_date = due_date.substr(0,4) 
103                                                                 + due_date.substr(5,2) + due_date.substr(8,2);
104                                                         var today = util.date.formatted_date( new Date() , '%Y%m%d' );
105                                                         if (today > due_date) total++;
106                                                 }
107                                         };
108                                 }
109                         ],
110                         'patron_holds' : [
111                                 ['render'],
112                                 function(e) {
113                                         return function() { 
114                                                 e.setAttribute('value',
115                                                         obj.patron.hold_requests.length
116                                                 );
117                                         };
118                                 }
119                         ],
120                         'patron_holds_available' : [
121                                 ['render'],
122                                 function(e) {
123                                         return function() { };
124                                 }
125                         ],
126                         'patron_card' : [
127                                 ['render'],
128                                 function(e) {
129                                         return function() { };
130                                 }
131                         ],
132                         'patron_ident_type_1' : [
133                                 ['render'],
134                                 function(e) {
135                                         return function() { };
136                                 }
137                         ],
138                         'patron_ident_value_1' : [
139                                 ['render'],
140                                 function(e) {
141                                         return function() { };
142                                 }
143                         ],
144                         'patron_ident_type_2' : [
145                                 ['render'],
146                                 function(e) {
147                                         return function() { };
148                                 }
149                         ],
150                         'patron_date_of_birth' : [
151                                 ['render'],
152                                 function(e) {
153                                         return function() { };
154                                 }
155                         ],
156                         'patron_day_phone' : [
157                                 ['render'],
158                                 function(e) {
159                                         return function() { };
160                                 }
161                         ],
162                         'patron_evening_phone' : [
163                                 ['render'],
164                                 function(e) {
165                                         return function() { };
166                                 }
167                         ],
168                         'patron_other_phone' : [
169                                 ['render'],
170                                 function(e) {
171                                         return function() { };
172                                 }
173                         ],
174                         'patron_email' : [
175                                 ['render'],
176                                 function(e) {
177                                         return function() { };
178                                 }
179                         ],
180                         'patron_photo_url' : [
181                                 ['render'],
182                                 function(e) {
183                                         return function() { };
184                                 }
185                         ],
186                         'patron_library' : [
187                                 ['render'],
188                                 function(e) {
189                                         return function() { };
190                                 }
191                         ],
192                         'patron_mailing_address_street1' : [
193                                 ['render'],
194                                 function(e) {
195                                         return function() { };
196                                 }
197                         ],
198                         'patron_mailing_address_street2' : [
199                                 ['render'],
200                                 function(e) {
201                                         return function() { };
202                                 }
203                         ],
204                         'patron_mailing_address_city' : [
205                                 ['render'],
206                                 function(e) {
207                                         return function() { };
208                                 }
209                         ],
210                         'patron_mailing_address_state' : [
211                                 ['render'],
212                                 function(e) {
213                                         return function() { };
214                                 }
215                         ],
216                         'patron_mailing_address_post_code' : [
217                                 ['render'],
218                                 function(e) {
219                                         return function() { };
220                                 }
221                         ],
222                         'patron_physical_address_street1' : [
223                                 ['render'],
224                                 function(e) {
225                                         return function() { };
226                                 }
227                         ],
228                         'patron_physical_address_street2' : [
229                                 ['render'],
230                                 function(e) {
231                                         return function() { };
232                                 }
233                         ],
234                         'patron_physical_address_city' : [
235                                 ['render'],
236                                 function(e) {
237                                         return function() { };
238                                 }
239                         ],
240                         'patron_physical_address_state' : [
241                                 ['render'],
242                                 function(e) {
243                                         return function() { };
244                                 }
245                         ],
246                         'patron_physical_address_post_code' : [
247                                 ['render'],
248                                 function(e) {
249                                         return function() { };
250                                 }
251                         ]
252                 };
253
254                 for (var i in control_map) {
255                         var cmd = this.w.document.getElementById(i);
256                         if (cmd) {
257                                 for (var j in control_map[i][0]) {
258                                         if (control_map[i][1]) {
259                                                 var ev_type = control_map[i][0][j];
260                                                 switch(ev_type) {
261                                                         case 'render':
262                                                                 obj.render_list.push( control_map[i][1](cmd) ); 
263                                                         break;
264                                                         default: cmd.addEventListener(ev_type,control_map[i][1],false);
265                                                 }
266                                         }
267                                 }
268                         }
269                         obj.view[i] = cmd;
270                 }
271
272                 obj.retrieve();
273
274         },
275
276         'retrieve' : function() {
277
278                 var patron;
279                 try {
280
281                         var obj = this;
282
283                         var chain = [];
284
285                         // Retrieve the patron
286                         chain.push(
287                                 function() {
288                                         try {
289                                                 var patron = obj.network.request(
290                                                         'open-ils.actor',
291                                                         'open-ils.actor.user.fleshed.retrieve_by_barcode',
292                                                         [ obj.session, obj.barcode ]
293                                                 );
294                                                 if (patron) {
295
296                                                         if (instanceOf(patron,au)) {
297
298                                                                 obj.patron = patron;
299
300                                                         } else {
301
302                                                                 throw('patron is not an au fm object');
303                                                         }
304                                                 } else {
305
306                                                         throw('patron == false');
307                                                 }
308
309                                         } catch(E) {
310                                                 var error = ('patron.display.retrieve : ' + js2JSON(E));
311                                                 obj.error.sdump('D_ERROR',error);
312                                                 alert(error);
313                                                 //FIXME// abort the chain
314                                         }
315                                 }
316                         );
317
318                         // Retrieve the bills
319                         chain.push(
320                                 function() {
321                                         try {
322                                                 var bills = obj.network.request(
323                                                         'open-ils.actor',
324                                                         'open-ils.actor.user.transactions.have_balance',
325                                                         [ obj.session, obj.patron.id() ]
326                                                 );
327                                                 //FIXME// obj.patron.bills( bills );
328                                                 obj.patron.bills = bills;
329                                         } catch(E) {
330                                                 var error = ('patron.display.retrieve : ' + js2JSON(E));
331                                                 obj.error.sdump('D_ERROR',error);
332                                                 alert(error);
333                                                 //FIXME// abort the chain
334                                         }
335                                 }
336                         );
337
338                         // Retrieve the checkouts
339                         chain.push(
340                                 function() {
341                                         try {
342                                                 var checkouts = obj.network.request(
343                                                         'open-ils.circ',
344                                                         'open-ils.circ.actor.user.checked_out',
345                                                         [ obj.session, obj.patron.id() ]
346                                                 );
347                                                 obj.patron.checkouts( checkouts );
348                                         } catch(E) {
349                                                 var error = ('patron.display.retrieve : ' + js2JSON(E));
350                                                 obj.error.sdump('D_ERROR',error);
351                                                 alert(error);
352                                                 //FIXME// abort the chain
353                                         }
354                                 }
355                         );
356
357                         // Retrieve the holds
358                         chain.push(
359                                 function() {
360                                         try {
361                                                 var holds = obj.network.request(
362                                                         'open-ils.circ',
363                                                         'open-ils.circ.holds.retrieve',
364                                                         [ obj.session, obj.patron.id() ]
365                                                 );
366                                                 obj.patron.hold_requests( holds );
367                                         } catch(E) {
368                                                 var error = ('patron.display.retrieve : ' + js2JSON(E));
369                                                 obj.error.sdump('D_ERROR',error);
370                                                 alert(error);
371                                                 //FIXME// abort the chain
372                                         }
373                                 }
374                         );
375
376                         // Update the screen
377                         chain.push( function() { obj.render(); } );
378
379                         // Do it
380                         JSAN.use('util.exec');
381                         util.exec.chain( chain );
382
383                 } catch(E) {
384                         var error = ('patron.display.retrieve : ' + js2JSON(E));
385                         this.error.sdump('D_ERROR',error);
386                         alert(error);
387                 }
388         },
389
390         'render' : function() {
391
392                 for (var i in this.render_list) {
393                         try {
394                                 this.render_list[i]();
395                         } catch(E) {
396                                 var error = 'Problem in patron.display.render with\n' + this.render_list[i] + '\n\n' + js2JSON(E);
397                                 this.error.sdump('D_ERROR',error);
398                                 alert(error);
399                         }
400                 }
401         }
402
403 }
404
405 dump('exiting patron/display.js\n');