]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/standing_penalties.js
remove standing penalties
[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.csp_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.csp.id()); 
29                     if (params.row.my.ausp) { params.row_node.setAttribute('retrieve_ausp_id',params.row.my.ausp.id()); }
30                     params.on_retrieve(params.row); 
31                     return params.row; 
32                 },
33                 'on_select' : function(ev) {
34                     var sel = list.retrieve_selection();
35                     var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
36                     var ausp_ids = util.functional.filter_list(
37                         util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_ausp_id') || 'null' ); } ),
38                         function(o) { return o != null; }
39                     );
40                     if (ids.length > 0) {
41                         document.getElementById('cmd_apply_penalty').setAttribute('disabled','false');
42                     } else {
43                         document.getElementById('cmd_apply_penalty').setAttribute('disabled','true');
44                     }
45                     if (ausp_ids.length > 0) {
46                         document.getElementById('cmd_remove_penalty').setAttribute('disabled','false');
47                     } else {
48                         document.getElementById('cmd_remove_penalty').setAttribute('disabled','true');
49                     }
50                 }
51             } 
52         );
53
54         for (var i = 0; i < data.list.csp.length; i++) {
55             if (data.list.csp[i].id() > 100 ) {
56             //if (true) {
57                 list.append(
58                     {
59                         'row' : {
60                             'my' : {
61                                 'csp' : data.list.csp[i],
62                                 'au' : xulG.patron,
63                                 'ausp' : util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.standing_penalty().id() == data.list.csp[i].id(); } )
64                             }
65                         }
66                     }
67                 );
68             }
69         };
70
71         document.getElementById('cmd_apply_penalty').addEventListener(
72             'command',
73             function() {
74                 var sel = list.retrieve_selection();
75                 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
76                 if (ids.length > 0) {
77
78                     var note = window.prompt(patronStrings.getString('staff.patron.standing_penalty.note_prompt'),'',patronStrings.getString('staff.patron.standing_penalty.note_title'));
79
80                     function gen_func(id) {
81                         return function() {
82                             var penalty = new ausp();
83                             penalty.usr( xulG.patron.id() );
84                             penalty.isnew( 1 );
85                             penalty.standing_penalty( id );
86                             penalty.org_unit( ses('ws_ou') );
87                             penalty.note( note );
88                             var req = net.simple_request( 'FM_AUSP_APPLY', [ ses(), penalty ] );
89                             if (typeof req.ilsevent != 'undefined' || String(req) != '1') {
90                                 error.standard_unexpected_error_alert(patronStrings.getFormattedString('staff.patron.standing_penalty.apply_error',[data.hash.csp[id].name()]),req);
91                             }
92                         }; 
93                     }
94
95                     var funcs = [];
96                     for (var i = 0; i < ids.length; i++) {
97                         funcs.push( gen_func(ids[i]) );
98                     } 
99                     funcs.push(
100                         function() {
101                             if (xulG && typeof xulG.refresh == 'function') {
102                                 xulG.refresh();
103                             }
104                             document.getElementById('progress').hidden = true;
105                         }
106                     );
107                     document.getElementById('progress').hidden = false;
108                     JSAN.use('util.exec'); var exec = new util.exec();
109                     exec.chain(funcs);
110                 }
111             },
112             false
113         );
114
115         document.getElementById('cmd_remove_penalty').addEventListener(
116             'command',
117             function() {
118                 var sel = list.retrieve_selection();
119                 var ids = util.functional.filter_list(
120                     util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_ausp_id') || 'null' ); } ),
121                     function(o) { return o != null; }
122                 );
123                 if (ids.length > 0) {
124                     function gen_func(id) {
125                         return function() {
126                             var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == id; } );
127                             penalty.isdeleted(1);
128
129                             var req = net.simple_request( 'FM_AUSP_REMOVE', [ ses(), penalty ] );
130                             if (typeof req.ilsevent != 'undefined' || String(req) != '1') {
131                                 error.standard_unexpected_error_alert(patronStrings.getFormattedString('staff.patron.standing_penalty.remove_error',[data.hash.csp[id].name()]),req);
132                             }
133                         }; 
134                     }
135
136                     var funcs = [];
137                     for (var i = 0; i < ids.length; i++) {
138                         funcs.push( gen_func(ids[i]) );
139                     } 
140                     funcs.push(
141                         function() {
142                             if (xulG && typeof xulG.refresh == 'function') {
143                                 xulG.refresh();
144                             }
145                             document.getElementById('progress').hidden = true;
146                         }
147                     );
148                     document.getElementById('progress').hidden = false;
149                     JSAN.use('util.exec'); var exec = new util.exec();
150                     exec.chain(funcs);
151                 }
152             },
153             false
154         );
155
156
157     } catch(E) {
158         alert(E);
159     }
160 }