]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/standing_penalties.js
re-orient this interface from scaffolding off of csp's to just using ausp's. Need...
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / standing_penalties.js
1 function penalty_init() {
2     try {
3         commonStrings = document.getElementById('commonStrings');
4         patronStrings = document.getElementById('patronStrings');
5
6         if (typeof JSAN == 'undefined') {
7             throw(
8                 commonStrings.getString('common.jsan.missing')
9             );
10         }
11
12         JSAN.errorLevel = "die"; // none, warn, or die
13         JSAN.addRepository('..');
14
15                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
16         XML_HTTP_SERVER = data.server_unadorned;
17
18         JSAN.use('util.error'); var error = new util.error();
19         JSAN.use('util.network'); var net = new util.network();
20         JSAN.use('patron.util'); JSAN.use('util.list'); JSAN.use('util.functional');
21
22         var list = new util.list( 'csp_list' );
23         list.init( 
24             {
25                 'columns' : patron.util.ausp_columns({}),
26                 'map_row_to_columns' : patron.util.std_map_row_to_columns(),
27                 'retrieve_row' : function(params) { 
28                     params.row_node.setAttribute('retrieve_id',params.row.my.ausp.id()); 
29                     params.on_retrieve(params.row); 
30                     return params.row; 
31                 },
32                 'on_select' : function(ev) {
33                     var sel = list.retrieve_selection();
34                     var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
35                     if (ids.length > 0) {
36                         document.getElementById('cmd_remove_penalty').setAttribute('disabled','false');
37                     } else {
38                         document.getElementById('cmd_remove_penalty').setAttribute('disabled','true');
39                     }
40                 }
41             } 
42         );
43
44         var rows = [];
45         for (var i = 0; i < xulG.patron.standing_penalties().length; i++) {
46             var row_params = {
47                 'row' : {
48                     'my' : {
49                         'ausp' : xulG.patron.standing_penalties()[i],
50                         'csp' : xulG.patron.standing_penalties()[i].standing_penalty(),
51                         'au' : xulG.patron,
52                     }
53                 }
54             };
55             rows[ xulG.patron.standing_penalties()[i].id() ] = list.append( row_params );
56         };
57
58         document.getElementById('cmd_apply_penalty').addEventListener(
59             'command',
60             function() {
61                 var sel = list.retrieve_selection();
62                 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
63                 if (ids.length > 0) {
64
65                     var note = window.prompt(patronStrings.getString('staff.patron.standing_penalty.note_prompt'),'',patronStrings.getString('staff.patron.standing_penalty.note_title'));
66
67                     function gen_func(id) {
68                         return function() {
69                             var penalty = new ausp();
70                             penalty.usr( xulG.patron.id() );
71                             penalty.isnew( 1 );
72                             penalty.standing_penalty( id );
73                             penalty.org_unit( ses('ws_ou') );
74                             penalty.note( note );
75                             var req = net.simple_request( 'FM_AUSP_APPLY', [ ses(), penalty ] );
76                             if (typeof req.ilsevent != 'undefined') {
77                                 error.standard_unexpected_error_alert(patronStrings.getFormattedString('staff.patron.standing_penalty.apply_error',[data.hash.csp[id].name()]),req);
78                             }
79                         }; 
80                     }
81
82                     var funcs = [];
83                     for (var i = 0; i < ids.length; i++) {
84                         funcs.push( gen_func(ids[i]) );
85                     } 
86                     funcs.push(
87                         function() {
88                             if (xulG && typeof xulG.refresh == 'function') {
89                                 xulG.refresh();
90                             }
91                             document.getElementById('progress').hidden = true;
92                         }
93                     );
94                     document.getElementById('progress').hidden = false;
95                     JSAN.use('util.exec'); var exec = new util.exec();
96                     exec.chain(funcs);
97                 }
98             },
99             false
100         );
101
102         document.getElementById('cmd_remove_penalty').addEventListener(
103             'command',
104             function() {
105                 var sel = list.retrieve_selection();
106                 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
107                 if (ids.length > 0) {
108                     function gen_func(id) {
109                         return function() {
110                             try {
111                                 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == id; } );
112                                 penalty.isdeleted(1);
113
114                                 var req = net.simple_request( 'FM_AUSP_REMOVE', [ ses(), penalty ] );
115                                 if (typeof req.ilsevent != 'undefined' || String(req) != '1') {
116                                     error.standard_unexpected_error_alert(patronStrings.getFormattedString('staff.patron.standing_penalty.remove_error',[id]),req);
117                                 } else {
118                                     var node = rows[ id ].my_node;
119                                     var parentNode = node.parentNode;
120                                     parentNode.removeChild( node );
121                                     delete(rows[ id ]);
122                                 }
123                             } catch(E) {
124                                 alert(E);
125                             }
126                         }; 
127                     }
128
129                     var funcs = [];
130                     for (var i = 0; i < ids.length; i++) {
131                         funcs.push( gen_func(ids[i]) );
132                     } 
133                     funcs.push(
134                         function() {
135                             if (xulG && typeof xulG.refresh == 'function') {
136                                 xulG.refresh();
137                             }
138                             document.getElementById('progress').hidden = true;
139                         }
140                     );
141                     document.getElementById('progress').hidden = false;
142                     JSAN.use('util.exec'); var exec = new util.exec();
143                     exec.chain(funcs);
144                 }
145             },
146             false
147         );
148
149
150     } catch(E) {
151         alert(E);
152     }
153 }