]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/util.js
bugfix for barcode renaming, local code and server code. Needs just a little more...
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / util.js
1 dump('entering cat/util.js\n');
2
3 if (typeof cat == 'undefined') var cat = {};
4 cat.util = {};
5
6 cat.util.EXPORT_OK      = [ 
7         'spawn_copy_editor', 'add_copies_to_bucket', 'show_in_opac', 'spawn_spine_editor', 'transfer_copies', 
8         'mark_item_missing', 'mark_item_damaged', 'replace_barcode',
9 ];
10 cat.util.EXPORT_TAGS    = { ':all' : cat.util.EXPORT_OK };
11
12 cat.util.replace_barcode = function(old_bc) {
13         try {
14                 JSAN.use('util.network');
15                 var network = new util.network();
16
17                 if (!old_bc) old_bc = window.prompt('Enter original barcode for the copy:','','Replace Barcode');
18                 if (!old_bc) return;
19
20                 var copy;
21         try {
22                         copy = network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[ old_bc ]);
23                         if (typeof copy.ilsevent != 'undefined') throw(copy); 
24                         if (!copy) throw(copy);
25                 } catch(E) {
26                         alert('We were unable to retrieve an item with barcode "' + old_bc + '".\n');
27                         return old_bc;
28                 }
29         
30                 // Why did I want to do this twice?  Because this copy is more fleshed?
31                 try {
32                         copy = network.simple_request('FM_ACP_RETRIEVE',[ copy.id() ]);
33                         if (typeof copy.ilsevent != 'undefined') throw(copy);
34                         if (!copy) throw(copy);
35                 } catch(E) {
36                         try { alert('We were unable to retrieve an item with barcode "' + old_bc + '".\n' + (typeof E.ilsevent == 'undefined' ? '' : E.textcode + ' : ' + E.desc)); } catch(F) { alert(E + '\n' + F); }
37                         return old_bc;
38                 }
39         
40                 var new_bc = window.prompt('Enter the replacement barcode for the copy:','','Replace Barcode');
41                 new_bc = String( new_bc ).replace(/\s/g,'');
42                 if (!new_bc) {
43                         alert('Rename aborted.  Blank for barcode not allowed.');
44                         return old_bc;
45                 }
46         
47                 var test = network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[ new_bc ]);
48                 if (typeof test.ilsevent == 'undefined') {
49                         alert('Rename aborted.  Another copy has barcode "' + new_bc + '".');
50                         return old_bc;
51                 } else {
52                         if (test.ilsevent != 1502 /* ASSET_COPY_NOT_FOUND */) {
53                                 obj.error.standard_unexpected_error_alert('Error testing replacement barcode "' . new_bc . '".',test);
54                                 return old_bc;
55                         }       
56                 }
57
58                 copy.barcode(new_bc); copy.ischanged('1');
59                 var r = network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE', [ ses(), [ copy ] ]);
60                 if (typeof r.ilsevent != 'undefined') { 
61                         if (r.ilsevent != 0) {
62                                 if (r.ilsevent == 5000 /* PERM_FAILURE */) {
63                                         alert('Renamed aborted.  Insufficient permission.');
64                                         return old_bc;
65                                 } else {
66                                         obj.error.standard_unexpected_error_alert('Error renaming item.',r);
67                                         return old_bc;
68                                 }
69                         }
70                 }
71
72                 return new_bc;
73         } catch(E) {
74                 JSAN.use('util.error'); var error = new util.error();
75                 error.standard_unexpected_error_alert('Rename did not likely occur.',E);
76                 return old_bc;
77         }
78 }
79
80 cat.util.transfer_copies = function(params) {
81         JSAN.use('util.error'); var error = new util.error();
82         JSAN.use('OpenILS.data'); var data = new OpenILS.data();
83         JSAN.use('util.network'); var network = new util.network();
84         try {
85                 data.stash_retrieve();
86                 if (!data.marked_volume) {
87                         alert('Please mark a volume as the destination from within holdings maintenance and then try this again.');
88                         return;
89                 }
90                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
91                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
92                 if (!params.message) {
93                         params.message = 'Transfer items from their original volumes to ';
94                         params.message += data.hash.aou[ params.owning_lib ].shortname() + "'s volume labelled ";
95                         params.message += '"' + params.volume_label + '" on the following record (and change their circ libs to match)?';
96                 }
97
98                 xml += '<description>' + params.message.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;') + '</description>';
99                 xml += '<hbox><button label="Transfer" name="fancy_submit"/>';
100                 xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
101                 xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + params.docid + '"/>';
102                 xml += '</vbox>';
103                 //data.temp_transfer = xml; data.stash('temp_transfer');
104                 JSAN.use('util.window'); var win = new util.window();
105                 var fancy_prompt_data = win.open(
106                         urls.XUL_FANCY_PROMPT,
107                         //+ '?xml_in_stash=temp_transfer'
108                         //+ '&title=' + window.escape('Item Transfer'),
109                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300',
110                         { 'xml' : xml, 'title' : 'Item Transfer' }
111                 );
112                 if (fancy_prompt_data.fancy_status == 'incomplete') { alert('Transfer Aborted'); return; }
113
114                 JSAN.use('util.functional');
115
116                 var copies = network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [ params.copy_ids ]);
117
118                 for (var i = 0; i < copies.length; i++) {
119                         copies[i].call_number( data.marked_volume );
120                         copies[i].circ_lib( params.owning_lib );
121                         copies[i].ischanged( 1 );
122                 }
123
124                 var robj = network.simple_request(
125                         'FM_ACP_FLESHED_BATCH_UPDATE', 
126                         [ ses(), copies, true ], 
127                         null,
128                         {
129                                 'title' : 'Override Transfer Failure?',
130                                 'overridable_events' : [
131                                         1208 /* TITLE_LAST_COPY */,
132                                         1227 /* COPY_DELETE_WARNING */,
133                                 ]
134                         }
135                 );
136                 
137                 if (typeof robj.ilsevent != 'undefined') {
138                         throw(robj);
139                 } else {
140                         alert('Items transferred.');
141                 }
142
143         } catch(E) {
144                 error.standard_unexpected_error_alert('All items not likely transferred.',E);
145         }
146 }
147
148 cat.util.spawn_spine_editor = function(selection_list) {
149         JSAN.use('util.error'); var error = new util.error();
150         try {
151                 JSAN.use('util.functional');
152                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
153                 data.temp_barcodes_for_labels = util.functional.map_list( selection_list, function(o){return o.barcode;}) ; 
154                 data.stash('temp_barcodes_for_labels');
155                 xulG.new_tab(
156                         xulG.url_prefix( urls.XUL_SPINE_LABEL ),
157                         { 'tab_name' : 'Spine Labels' },
158                         {}
159                 );
160         } catch(E) {
161                 error.standard_unexpected_error_alert('Spine Labels',E);
162         }
163 }
164
165 cat.util.show_in_opac = function(selection_list) {
166         JSAN.use('util.error'); var error = new util.error();
167         var doc_id; var seen = {};
168         try {
169                 for (var i = 0; i < selection_list.length; i++) {
170                         doc_id = selection_list[i].doc_id;
171                         if (!doc_id) {
172                                 alert(selection_list[i].barcode + ' is not cataloged');
173                                 continue;
174                         }
175                         if (typeof seen[doc_id] != 'undefined') {
176                                 continue;
177                         }
178                         seen[doc_id] = true;
179                         var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + doc_id;
180                         var content_params = { 
181                                 'session' : ses(),
182                                 'authtime' : ses('authtime'),
183                                 'opac_url' : opac_url,
184                         };
185                         xulG.new_tab(
186                                 xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
187                                 {'tab_name':'Retrieving title...'}, 
188                                 content_params
189                         );
190                 }
191         } catch(E) {
192                 error.standard_unexpected_error_alert('Error opening catalog for document id = ' + doc_id,E);
193         }
194 }
195
196 cat.util.add_copies_to_bucket = function(selection_list) {
197         JSAN.use('util.functional');
198         JSAN.use('util.window'); var win = new util.window();
199         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
200         data.cb_temp_copy_ids = js2JSON(
201                 util.functional.map_list(
202                         selection_list,
203                         function (o) {
204                                 if (typeof o.copy_id != 'undefined' && o.copy_id != null) {
205                                         return o.copy_id;
206                                 } else {
207                                         return o;
208                                 }
209                         }
210                 )
211         );
212         data.stash('cb_temp_copy_ids');
213         win.open( 
214                 xulG.url_prefix(urls.XUL_COPY_BUCKETS_QUICK),
215                 'sel_bucket_win' + win.window_name_increment(),
216                 'chrome,resizable,center'
217         );
218 }
219
220 cat.util.spawn_copy_editor = function(list,edit) {
221         try {
222                 var obj = {};
223                 JSAN.use('util.network'); obj.network = new util.network();
224                 JSAN.use('util.error'); obj.error = new util.error();
225         
226                 if (list.length == 0) return;
227         
228                 var title = list.length == 1 ? '' : 'Batch '; 
229                 title += edit == 1 ? 'Edit' : 'View';
230                 title += ' Copy Attributes';
231         
232                 JSAN.use('util.window'); var win = new util.window();
233                 //JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
234                 //obj.data.temp_copies = undefined; obj.data.stash('temp_copies');
235                 //obj.data.temp_callnumbers = undefined; obj.data.stash('temp_callnumbers');
236                 //obj.data.temp_copy_ids = js2JSON(list); obj.data.stash('temp_copy_ids');
237                 var my_xulG = win.open(
238                         //window.xulG.url_prefix(urls.XUL_COPY_EDITOR),
239                         (urls.XUL_COPY_EDITOR),
240                         //      +'?handle_update=1&edit='+edit,
241                         title,
242                         'chrome,modal,resizable',
243                         {
244                                 'handle_update' : 1,
245                                 'edit' : edit,
246                                 'copy_ids' : js2JSON(list),
247                         }
248                 );
249                 //obj.data.stash_retrieve();
250                 if (!my_xulG.copies) alert('Copies not modified.');
251                 //if (!obj.data.temp_copies) alert('Copies not modified.');
252                 //obj.data.temp_copies = undefined; obj.data.stash('temp_copies');
253         } catch(E) {
254                 JSAN.use('util.error'); var error = new util.error();
255                 error.standard_unexpected_error_alert('error in cat.util.spawn_copy_editor',E);
256         }
257 }
258
259 cat.util.mark_item_damaged = function(copy_ids) {
260         var error;
261         try {
262                 JSAN.use('util.error'); error = new util.error();
263                 JSAN.use('util.functional');
264                 JSAN.use('util.network'); var network = new util.network();
265                 var copies = network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [ copy_ids ]);
266                 if (typeof copies.ilsevent != 'undefined') throw(copies);
267                 var magic_status = false;
268                 for (var i = 0; i < copies.length; i++) {
269                         var status = copies[i].status(); if (typeof status == 'object') status = status.id();
270                         if (typeof my_constants.magical_statuses[ status ] != 'undefined') 
271                                 if (my_constants.magical_statuses[ status ].block_mark_item_action) magic_status = true;
272                 }
273                 if (magic_status) {
274                 
275                         error.yns_alert('Action failed.  One or more of these items is in a special status (Checked Out, In Transit, etc.) and cannot be changed to the Damaged status.','Action failed.','OK',null,null,'Check here to confirm this message');
276
277                 } else {
278
279                         var r = error.yns_alert('Change the status for these items to Damaged?  You will have to manually retrieve the last circulation if you need to bill a patron.  Barcodes: ' + util.functional.map_list( copies, function(o) { return o.barcode(); } ).join(", "), 'Mark Damaged', 'OK', 'Cancel', null, 'Check here to confirm this action');
280
281                         if (r == 0) {
282                                 var count = 0;
283                                 for (var i = 0; i < copies.length; i++) {
284                                         try {
285                                                 var robj = network.simple_request('MARK_ITEM_DAMAGED',[ses(),copies[i].id()]);
286                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
287                                                 count++;
288                                         } catch(E) {
289                                                 error.standard_unexpected_error_alert('Error marking item ' + copies[i].barcode() + ' damaged.',E);
290                                         }
291                                 }
292                                 alert(count == 1 ? 'Item marked Damaged' : count + ' items marked Damaged.');
293                         }
294                 }
295
296         } catch(E) {
297                 if (error) error.standard_unexpected_error_alert('cat.util.mark_item_damaged',E); else alert('FIXME: ' + E);
298         }
299 }
300
301 cat.util.mark_item_missing = function(copy_ids) {
302         var error;
303         try {
304                 JSAN.use('util.error'); error = new util.error();
305                 JSAN.use('util.functional');
306                 JSAN.use('util.network'); var network = new util.network();
307                 var copies = network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [ copy_ids ]);
308                 if (typeof copies.ilsevent != 'undefined') throw(copies);
309                 var magic_status = false;
310                 for (var i = 0; i < copies.length; i++) {
311                         var status = copies[i].status(); if (typeof status == 'object') status = status.id();
312                         if (typeof my_constants.magical_statuses[ status ] != 'undefined') 
313                                 if (my_constants.magical_statuses[ status ].block_mark_item_action) magic_status = true;
314                 }
315                 if (magic_status) {
316                 
317                         error.yns_alert('Action failed.  One or more of these items is in a special status (Checked Out, In Transit, etc.) and cannot be changed to the Missing status.','Action failed.','OK',null,null,'Check here to confirm this message');
318
319                 } else {
320
321                         var r = error.yns_alert('Change the status for these items to Missing?  Barcodes: ' + util.functional.map_list( copies, function(o) { return o.barcode(); } ).join(", "), 'Mark Missing', 'OK', 'Cancel', null, 'Check here to confirm this action');
322
323                         if (r == 0) {
324                                 var count = 0;
325                                 for (var i = 0; i < copies.length; i++) {
326                                         try {
327                                                 var robj = network.simple_request('MARK_ITEM_MISSING',[ses(),copies[i].id()]);
328                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
329                                                 count++;
330                                         } catch(E) {
331                                                 error.standard_unexpected_error_alert('Error marking item ' + copies[i].barcode() + ' missing.',E);
332                                         }
333                                 }
334                                 alert(count == 1 ? 'Item marked Missing' : count + ' items marked Missing.');
335                         }
336                 }
337
338         } catch(E) {
339                 if (error) error.standard_unexpected_error_alert('cat.util.mark_item_missing',E); else alert('FIXME: ' + E);
340         }
341 }
342
343
344 dump('exiting cat/util.js\n');