]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/services/item.js
LP#1932051: serialize adding items to bucket from Item Status
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / services / item.js
1 /**
2  * Shared item services for circulation
3  */
4
5 angular.module('egCoreMod')
6     .factory('egItem',
7        ['egCore','egOrg','egCirc','$uibModal','$q','$timeout','$window','ngToast','egConfirmDialog','egAlertDialog',
8 function(egCore , egOrg , egCirc , $uibModal , $q , $timeout , $window , ngToast , egConfirmDialog , egAlertDialog ) {
9
10     var service = {
11         copies : [], // copy barcode search results
12         index : 0 // search grid index
13     };
14
15     service.flesh = {   
16         flesh : 4,
17         flesh_fields : {
18             acp : ['call_number','location','status','floating','circ_modifier',
19                 'age_protect','circ_lib','copy_alerts', 'creator', 'editor', 'circ_as_type', 'latest_inventory'],
20             acn : ['record','prefix','suffix','label_class'],
21             bre : ['simple_record','creator','editor'],
22             alci : ['inventory_workstation']
23         },
24         select : { 
25             // avoid fleshing MARC on the bre
26             // note: don't add simple_record.. not sure why
27             bre : ['id','tcn_value','creator','editor', 'create_date', 'edit_date'],
28         } 
29     }
30
31     service.circFlesh = {
32         flesh : 2,
33         flesh_fields : {
34             combcirc : [
35                 'usr',
36                 'workstation',
37                 'checkin_workstation',
38                 'checkin_lib',
39                 'duration_rule',
40                 'max_fine_rule',
41                 'recurring_fine_rule'
42             ],
43             au : ['card']
44         },
45         order_by : {combcirc : 'xact_start desc'},
46         limit :  1
47     }
48
49     //Retrieve separate copy, aacs, and accs information
50     service.getCopy = function(barcode, id) {
51         if (barcode) {
52             // handle barcode completion
53             return egCirc.handle_barcode_completion(barcode)
54             .then(function(actual_barcode) {
55                 return egCore.pcrud.search(
56                     'acp', {barcode : actual_barcode, deleted : 'f'},
57                     service.flesh).then(function(copy) {return copy});
58             });
59         }
60
61         return egCore.pcrud.retrieve( 'acp', id, service.flesh)
62             .then(function(copy) {return copy});
63     }
64
65     service.getCirc = function(id) {
66         return egCore.pcrud.search('combcirc', { target_copy : id },
67             service.circFlesh).then(function(circ) {return circ});
68     }
69
70     service.getSummary = function(id) {
71         return circ_summary = egCore.net.request(
72             'open-ils.circ',
73             'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
74             egCore.auth.token(), id).then(
75                 function(circ_summary) {return circ_summary});
76     }
77
78     //Combine copy, circ, and accs information
79     service.retrieveCopyData = function(barcode, id) {
80         var copyData = {};
81
82         var fetchCopy = function(barcode, id) {
83             return service.getCopy(barcode, id)
84                 .then(function(copy) {
85                     copyData.copy = copy;
86                     return copyData;
87                 });
88         }
89         var fetchCirc = function(copy) {
90             return service.getCirc(copy.id())
91                 .then(function(circ) {
92                     copyData.circ = circ;
93                     return copyData;
94                 });
95         }
96         var fetchSummary = function(circ) {
97             return service.getSummary(circ.id())
98                 .then(function(summary) {
99                     copyData.circ_summary = summary;
100                     return copyData;
101                 });
102         }
103
104         return fetchCopy(barcode, id).then(function(res) {
105
106             if(!res.copy) { return $q.when(); }
107             return fetchCirc(copyData.copy).then(function(res) {
108                 if (copyData.circ) {
109                     return fetchSummary(copyData.circ).then(function() {
110                         return copyData;
111                     });
112                 } else {
113                     return copyData;
114                 }
115             });
116         });
117
118     }
119
120     // resolved with the last received copy
121     service.fetch = function(barcode, id, noListDupes) {
122         var copy;
123         var circ;
124         var circ_summary;
125         var lastRes = {};
126
127         return service.retrieveCopyData(barcode, id)
128         .then(function(copyData) {
129             if(!copyData) { return $q.when(); }
130             //Make sure we're getting a completed copyData - no plain acp or circ objects
131             if (copyData.circ) {
132                 // flesh circ_lib locally
133                 copyData.circ.circ_lib(egCore.org.get(copyData.circ.circ_lib()));
134
135             }
136             var flatCopy;
137
138             if (noListDupes) {
139                 // use the existing copy if possible
140                 flatCopy = service.copies.filter(
141                     function(c) {return c.id == copyData.copy.id()})[0];
142             }
143
144             // flesh acn.owning_lib
145             copyData.copy.call_number().owning_lib(egCore.org.get(copyData.copy.call_number().owning_lib()));
146
147             if (!flatCopy) {
148                 flatCopy = egCore.idl.toHash(copyData.copy, true);
149
150                 if (copyData.circ) {
151                     flatCopy._circ = egCore.idl.toHash(copyData.circ, true);
152                     flatCopy._circ_summary = egCore.idl.toHash(copyData.circ_summary, true);
153                     flatCopy._circ_lib = copyData.circ.circ_lib();
154                     flatCopy._duration = copyData.circ.duration();
155                     flatCopy._circ_ws = flatCopy._circ_summary.last_renewal_workstation ?
156                                         flatCopy._circ_summary.last_renewal_workstation :
157                                         flatCopy._circ_summary.checkout_workstation ?
158                                         flatCopy._circ_summary.checkout_workstation :
159                                         '';
160                 }
161                 flatCopy.index = service.index++;
162                 flatCopy.copy_alert_count = copyData.copy.copy_alerts().filter(function(aca) {
163                     return !aca.ack_time();
164                 }).length;
165
166                 service.copies.unshift(flatCopy);
167             }
168
169             //Get in-house use count
170             egCore.pcrud.search('aihu',
171                 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
172             .then(function(uses) {
173                 flatCopy._inHouseUseCount = uses.length;
174                 copyData.copy._inHouseUseCount = uses.length;
175             });
176
177             //Get Monograph Parts
178             egCore.pcrud.search('acpm',
179                 {target_copy: flatCopy.id},
180                 { flesh : 1, flesh_fields : { acpm : ['part'] } },
181                 {atomic :true})
182             .then(function(acpm_array) {
183                 angular.forEach(acpm_array, function(acpm) {
184                     flatCopy.parts = egCore.idl.toHash(acpm.part());
185                     copyData.copy.parts = egCore.idl.toHash(acpm.part());
186                 });
187             });
188
189             return lastRes = {
190                 copy : copyData.copy,
191                 index : flatCopy.index
192             }
193         });
194
195
196     }
197
198     //all_items = selected grid rows, to be updated in place
199     service.updateInventory = function(copy_list, all_items) {
200         if (copy_list.length == 0) return;
201         return egCore.net.request(
202             'open-ils.circ',
203             'open-ils.circ.circulation.update_latest_inventory',
204             egCore.auth.token(), {copy_list: copy_list}
205         ).then(function(res) {
206             if (res) {
207                 if (all_items) angular.forEach(copy_list, function(copy) {
208                     angular.forEach(all_items, function(item) {
209                         if (copy == item.id) {
210                             egCore.pcrud.search('alci', {copy: copy},
211                               {flesh: 1, flesh_fields:
212                                 {alci: ['inventory_workstation']}
213                             }).then(function(alci) {
214                                 //update existing grid rows
215                                 item["latest_inventory.inventory_date"] = alci.inventory_date();
216                                 item["latest_inventory.inventory_workstation.name"] =
217                                     alci.inventory_workstation().name();
218                             });
219                         }
220                     });
221                 });
222                 return all_items || res;
223             }
224         });
225     }
226
227     service.add_copies_to_bucket = function(list, bucket_type) {
228         if (list.length == 0) return;
229         if (!bucket_type) bucket_type = 'copy';
230
231         return $uibModal.open({
232             templateUrl: './cat/catalog/t_add_to_bucket',
233             backdrop: 'static',
234             animation: true,
235             size: 'md',
236             controller:
237                    ['$scope','$uibModalInstance',
238             function($scope , $uibModalInstance) {
239
240                 $scope.bucket_id = 0;
241                 $scope.newBucketName = '';
242                 $scope.allBuckets = [];
243
244                 egCore.net.request(
245                     'open-ils.actor',
246                     'open-ils.actor.container.retrieve_by_class.authoritative',
247                     egCore.auth.token(), egCore.auth.user().id(),
248                     bucket_type, 'staff_client'
249                 ).then(function(buckets) { $scope.allBuckets = buckets; });
250
251                 $scope.add_to_bucket = function() {
252                     var promise = $q.when();
253                     angular.forEach(list, function (entry) {
254                         var item = bucket_type == 'copy' ? new egCore.idl.ccbi() : new egCore.idl.cbrebi();
255                         item.bucket($scope.bucket_id);
256                         if (bucket_type == 'copy') item.target_copy(entry);
257                         if (bucket_type == 'biblio') item.target_biblio_record_entry(entry);
258                         promise = promise.then(function() {
259                             return egCore.net.request(
260                                 'open-ils.actor',
261                                 'open-ils.actor.container.item.create',
262                                 egCore.auth.token(), bucket_type, item
263                             );
264                         });
265                     });
266                     promise.then(function() {
267                         $uibModalInstance.close();
268                     });
269                 }
270
271                 $scope.add_to_new_bucket = function() {
272                     var bucket = bucket_type == 'copy' ? new egCore.idl.ccb() : new egCore.idl.cbreb();
273                     bucket.owner(egCore.auth.user().id());
274                     bucket.name($scope.newBucketName);
275                     bucket.description('');
276                     bucket.btype('staff_client');
277
278                     return egCore.net.request(
279                         'open-ils.actor',
280                         'open-ils.actor.container.create',
281                         egCore.auth.token(), bucket_type, bucket
282                     ).then(function(bucket) {
283                         $scope.bucket_id = bucket;
284                         $scope.add_to_bucket();
285                     });
286                 }
287
288                 $scope.cancel = function() {
289                     $uibModalInstance.dismiss();
290                 }
291             }]
292         });
293     }
294
295     service.make_copies_bookable = function(items) {
296
297         var copies_by_record = {};
298         var record_list = [];
299         angular.forEach(
300             items,
301             function (item) {
302                 var record_id = item['call_number.record.id'];
303                 if (typeof copies_by_record[ record_id ] == 'undefined') {
304                     copies_by_record[ record_id ] = [];
305                     record_list.push( record_id );
306                 }
307                 copies_by_record[ record_id ].push(item.id);
308             }
309         );
310
311         var promises = [];
312         var combined_results = [];
313         angular.forEach(record_list, function(record_id) {
314             promises.push(
315                 egCore.net.request(
316                     'open-ils.booking',
317                     'open-ils.booking.resources.create_from_copies',
318                     egCore.auth.token(),
319                     copies_by_record[record_id]
320                 ).then(function(results) {
321                     if (results && results['brsrc']) {
322                         combined_results = combined_results.concat(results['brsrc']);
323                     }
324                 })
325             );
326         });
327
328         $q.all(promises).then(function() {
329             if (combined_results.length > 0) {
330                 $uibModal.open({
331                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
332                     backdrop: 'static',
333                     animation: true,
334                     size: 'md',
335                     controller:
336                            ['$scope','$location','egCore','$uibModalInstance',
337                     function($scope , $location , egCore , $uibModalInstance) {
338
339                         $scope.funcs = {
340                             ses : egCore.auth.token(),
341                             resultant_brsrc : combined_results.map(function(o) { return o[0]; })
342                         }
343
344                         var booking_path = '/eg/conify/global/booking/resource';
345
346                         $scope.booking_admin_url =
347                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
348                     }]
349                 });
350             }
351         });
352     }
353
354     service.book_copies_now = function(barcode) {
355         location.href = "/eg2/staff/booking/create_reservation/for_resource/" + barcode;
356     }
357
358     service.manage_reservations = function(barcode) {
359         location.href = "/eg2/staff/booking/manage_reservations/by_resource/" + barcode;
360     }
361
362     service.requestItems = function(copy_list,record_list) {
363         if (copy_list.length == 0) return;
364         if (record_list) {
365             record_list = record_list.filter(function(v,i,s){ // dedup
366                 return s.indexOf(v) == i;
367             });
368         }
369
370         return $uibModal.open({
371             templateUrl: './cat/catalog/t_request_items',
372             backdrop: 'static',
373             animation: true,
374             controller:
375                    ['$scope','$uibModalInstance','egUser',
376             function($scope , $uibModalInstance , egUser) {
377                 $scope.user = null;
378                 $scope.first_user_fetch = true;
379
380                 $scope.hold_data = {
381                     hold_type : 'C',
382                     copy_list : copy_list,
383                     record_list : record_list,
384                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
385                     user      : egCore.auth.user().id(),
386                     honor_user_settings : 
387                         egCore.hatch.getLocalItem('eg.cat.request_items.honor_user_settings')
388                 };
389
390                 egUser.get( $scope.hold_data.user ).then(function(u) {
391                     $scope.user = u;
392                     $scope.barcode = u.card().barcode();
393                     $scope.user_name = egUser.format_name(u);
394                     $scope.hold_data.user = u.id();
395                 });
396
397                 $scope.user_name = '';
398                 $scope.barcode = '';
399                 function user_preferred_pickup_lib(u) {
400                     var pickup_lib = u.home_ou();
401                     angular.forEach(u.settings(), function (s) {
402                         if (s.name() == "opac.default_pickup_location") {
403                             pickup_lib = s.value();
404                         }
405                     });
406                     return egOrg.get(pickup_lib);
407                 }
408                 $scope.$watch('barcode', function (n) {
409                     if (!$scope.first_user_fetch) {
410                         egUser.getByBarcode(n).then(function(u) {
411                             $scope.user = u;
412                             $scope.user_name = egUser.format_name(u);
413                             $scope.hold_data.user = u.id();
414                             if ($scope.hold_data.honor_user_settings) {
415                                 $scope.hold_data.pickup_lib = user_preferred_pickup_lib(u);
416                             }
417                         }, function() {
418                             $scope.user = null;
419                             $scope.user_name = '';
420                             delete $scope.hold_data.user;
421                         });
422                     }
423                     $scope.first_user_fetch = false;
424                 });
425                 $scope.$watch('hold_data.honor_user_settings', function (n) {
426                     if (n && $scope.user) {
427                         $scope.hold_data.pickup_lib = user_preferred_pickup_lib($scope.user);
428                     } else {
429                         $scope.hold_data.pickup_lib = egCore.org.get(egCore.auth.user().ws_ou());
430                     }
431                     egCore.hatch.setLocalItem('eg.cat.request_items.honor_user_settings',n);
432                 });
433
434                 $scope.ok = function(h) {
435                     var args = {
436                         patronid  : h.user,
437                         hold_type : h.hold_type,
438                         pickup_lib: h.pickup_lib.id(),
439                         depth     : 0
440                     };
441
442                     egCore.net.request(
443                         'open-ils.circ',
444                         'open-ils.circ.holds.test_and_create.batch.override',
445                         egCore.auth.token(), args,
446                         h.hold_type == 'T' ? h.record_list : h.copy_list,
447                         { 'all' : 1, 'honor_user_settings' : h.honor_user_settings }
448                     ).then(function(r) {
449                         console.log('request result',r);
450                         if (isNaN(r.result)) {
451                             if (typeof r.result.desc != 'undefined') {
452                                 ngToast.danger(r.result.desc);
453                             } else {
454                                 if (typeof r.result.last_event != 'undefined') {
455                                     ngToast.danger(r.result.last_event.desc);
456                                 } else {
457                                     ngToast.danger(egCore.strings.FAILURE_HOLD_REQUEST);
458                                 }
459                             }
460                         } else {
461                             ngToast.success(egCore.strings.SUCCESS_HOLD_REQUEST);
462                         }
463                     });
464
465                     $uibModalInstance.close();
466                 }
467
468                 $scope.cancel = function($event) {
469                     $uibModalInstance.dismiss();
470                     $event.preventDefault();
471                 }
472             }]
473         });
474     }
475
476     service.attach_to_peer_bib = function(items) {
477         if (items.length == 0) return;
478
479         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
480             if (!target_record) return;
481
482             return $uibModal.open({
483                 templateUrl: './cat/catalog/t_conjoined_selector',
484                 backdrop: 'static',
485                 animation: true,
486                 controller:
487                        ['$scope','$uibModalInstance',
488                 function($scope , $uibModalInstance) {
489                     $scope.update = false;
490
491                     $scope.peer_type = null;
492                     $scope.peer_type_list = [];
493
494                     get_peer_types = function() {
495                         if (egCore.env.bpt)
496                             return $q.when(egCore.env.bpt.list);
497
498                         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
499                         .then(function(list) {
500                             egCore.env.absorbList(list, 'bpt');
501                             return list;
502                         });
503                     }
504
505                     get_peer_types().then(function(list){
506                         $scope.peer_type_list = list;
507                     });
508
509                     $scope.ok = function(type) {
510                         var promises = [];
511
512                         angular.forEach(items, function (cp) {
513                             var n = new egCore.idl.bpbcm();
514                             n.isnew(true);
515                             n.peer_record(target_record);
516                             n.target_copy(cp.id);
517                             n.peer_type(type);
518                             promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
519                         });
520
521                         return $q.all(promises).then(function(){$uibModalInstance.close()});
522                     }
523
524                     $scope.cancel = function($event) {
525                         $uibModalInstance.dismiss();
526                         $event.preventDefault();
527                     }
528                 }]
529             });
530         });
531     }
532
533     service.selectedHoldingsCopyDelete = function (items) {
534         if (items.length == 0) return;
535
536         var copy_objects = [];
537         egCore.pcrud.search('acp',
538             {deleted : 'f', id : items.map(function(el){return el.id;}) },
539             { flesh : 1, flesh_fields : { acp : ['call_number'] } }
540         ).then(function() {
541
542             var cnHash = {};
543             var perCnCopies = {};
544
545             var cn_count = 0;
546             var cp_count = 0;
547
548             angular.forEach(
549                 copy_objects,
550                 function (cp) {
551                     cp.isdeleted(1);
552                     cp_count++;
553                     var cn_id = cp.call_number().id();
554                     if (!cnHash[cn_id]) {
555                         cnHash[cn_id] = cp.call_number();
556                         perCnCopies[cn_id] = [cp];
557                     } else {
558                         perCnCopies[cn_id].push(cp);
559                     }
560                     cp.call_number(cn_id); // prevent loops in JSON-ification
561                 }
562             );
563
564             angular.forEach(perCnCopies, function (v, k) {
565                 cnHash[k].copies(v);
566             });
567
568             cnList = [];
569             angular.forEach(cnHash, function (v, k) {
570                 cnList.push(v);
571             });
572
573             if (cnList.length == 0) return;
574
575             var flags = {};
576
577             egConfirmDialog.open(
578                 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
579                 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
580                 {copies : cp_count, volumes : cn_count}
581             ).result.then(function() {
582                 egCore.net.request(
583                     'open-ils.cat',
584                     'open-ils.cat.asset.volume.fleshed.batch.update',
585                     egCore.auth.token(), cnList, 1, flags
586                 ).then(function(resp){
587                     var evt = egCore.evt.parse(resp);
588                     if (evt) {
589                         egConfirmDialog.open(
590                             egCore.strings.OVERRIDE_DELETE_ITEMS_FROM_CATALOG_TITLE,
591                             egCore.strings.OVERRIDE_DELETE_ITEMS_FROM_CATALOG_BODY,
592                             {'evt_desc': evt.desc}
593                         ).result.then(function() {
594                             egCore.net.request(
595                                 'open-ils.cat',
596                                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
597                                 egCore.auth.token(), cnList, 1,
598                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
599                             ).then(function() {
600                                 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
601                             });
602                         });
603                     } else {
604                         angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
605                     }
606                 });
607             });
608         },
609         null,
610         function(copy) {
611             copy_objects.push(copy);
612         });
613     }
614
615     service.checkin = function (items) {
616         // Recursive function that creates a promise for each item. Once the dialog 
617         // window for a given item is closed the next promise is started and a
618         // new dialog is opened. 
619         // This keeps multiple popups from hitting the screen at once.
620         (function checkinLoop(i) {
621             if (i < items.length) new Promise((resolve, reject) => {
622                 egCirc.checkin({copy_barcode: items[i].barcode})
623                 .then(function() {
624                     service.add_barcode_to_list(items[i].barcode);
625                     resolve();
626                 })
627             }).then(checkinLoop.bind(null, i+1));
628         })(0);
629     }
630
631     service.renew = function (items) {
632         angular.forEach(items, function (cp) {
633             egCirc.renew({copy_barcode:cp.barcode}).then(
634                 function() { service.add_barcode_to_list(cp.barcode) }
635             );
636         });
637     }
638
639     service.cancel_transit = function (items) {
640         angular.forEach(items, function(cp) {
641             egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
642                 .then(function(t) { return egCirc.abort_transit(t.id())    })
643                 .then(function()  { return service.add_barcode_to_list(cp.barcode) });
644         });
645     }
646
647     service.selectedHoldingsDamaged = function (items) {
648         angular.forEach(items, function(cp) {
649             if (cp) {
650                 egCirc.mark_damaged({
651                     id: cp.id,
652                     barcode: cp.barcode,
653                     refresh: cp.refresh
654                 });
655             }
656         });
657     }
658
659     service.selectedHoldingsDiscard = function (items) {
660         egCirc.mark_discard(items.map(function(el){return {id : el.id, barcode : el.barcode};})).then(function(){
661             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
662         });
663     }
664
665     service.selectedHoldingsMissing = function (items) {
666         return egCirc.mark_missing(
667             items.map(function(el){return {id : el.id, barcode : el.barcode};})
668         ).then(function(){
669             var promise = $q.when();
670             angular.forEach(items, function(cp){
671                 promise = promise.then(function() {
672                     return service.add_barcode_to_list(cp.barcode, true);
673                 });
674             });
675             return promise;
676         });
677     }
678
679     service.gatherSelectedRecordIds = function (items) {
680         var rid_list = [];
681         angular.forEach(
682             items,
683             function (item) {
684                 if (rid_list.indexOf(item['call_number.record.id']) == -1)
685                     rid_list.push(item['call_number.record.id'])
686             }
687         );
688         return rid_list;
689     }
690
691     service.gatherSelectedVolumeIds = function (items,rid) {
692         var cn_id_list = [];
693         angular.forEach(
694             items,
695             function (item) {
696                 if (rid && item['call_number.record.id'] != rid) return;
697                 if (cn_id_list.indexOf(item['call_number.id']) == -1)
698                     cn_id_list.push(item['call_number.id'])
699             }
700         );
701         return cn_id_list;
702     }
703
704     service.gatherSelectedHoldingsIds = function (items,rid) {
705         var cp_id_list = [];
706         angular.forEach(
707             items,
708             function (item) {
709                 if (rid && item['call_number.record.id'] != rid) return;
710                 cp_id_list.push(item.id)
711             }
712         );
713         return cp_id_list;
714     }
715
716     service.spawnHoldingsAdd = function (items,use_vols,use_copies){
717         angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
718             var raw = [];
719             if (use_copies) { // just a copy on existing volumes
720                 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
721                     raw.push( {callnumber : v} );
722                 });
723             } else if (use_vols) {
724                 angular.forEach(
725                     service.gatherSelectedHoldingsIds(items,r),
726                     function (i) {
727                         angular.forEach(items, function(item) {
728                             if (i == item.id) {
729                                 // owning_lib may be fleshed.
730                                 var owner = item['call_number.owning_lib.id']
731                                     || item['call_number.owning_lib'];
732                                 raw.push({owner : owner});
733                             }
734                         });
735                     }
736                 );
737             }
738
739             if (raw.length == 0) raw.push({});
740
741             egCore.net.request(
742                 'open-ils.actor',
743                 'open-ils.actor.anon_cache.set_value',
744                 null, 'edit-these-copies', {
745                     record_id: r,
746                     raw: raw,
747                     hide_vols : false,
748                     hide_copies : false
749                 }
750             ).then(function(key) {
751                 if (key) {
752                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
753                     $timeout(function() { $window.open(url, '_blank') });
754                 } else {
755                     alert('Could not create anonymous cache key!');
756                 }
757             });
758         });
759     }
760
761     service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
762         var item_ids = [];
763         angular.forEach(items, function(i){
764             item_ids.push(i.id);
765         });
766
767         // provide record_id iff one record is selected.
768         // 0 disables record summary
769         var record_ids = service.gatherSelectedRecordIds(items);
770         var record_id  = record_ids.length === 1 ? record_ids[0] : 0;
771         egCore.net.request(
772             'open-ils.actor',
773             'open-ils.actor.anon_cache.set_value',
774             null,
775             'edit-these-copies',
776             {
777                 record_id: record_id,
778                 copies: item_ids,
779                 raw: {},
780                 hide_vols : hide_vols,
781                 hide_copies : hide_copies
782             }).then(function(key) {
783                 if (key) {
784                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
785                     $timeout(function() { $window.open(url, '_blank') });
786                 } else {
787                     alert('Could not create anonymous cache key!');
788                 }
789             });
790     }
791
792     service.replaceBarcodes = function(items) {
793         angular.forEach(items, function (cp) {
794             $uibModal.open({
795                 templateUrl: './cat/share/t_replace_barcode',
796                 backdrop: 'static',
797                 animation: true,
798                 controller:
799                            ['$scope','$uibModalInstance',
800                     function($scope , $uibModalInstance) {
801                         $scope.isModal = true;
802                         $scope.focusBarcode = false;
803                         $scope.focusBarcode2 = true;
804                         $scope.barcode1 = cp.barcode;
805
806                         $scope.updateBarcode = function() {
807                             $scope.copyNotFound = false;
808                             $scope.updateOK = false;
809
810                             egCore.pcrud.search('acp',
811                                 {deleted : 'f', barcode : $scope.barcode1})
812                             .then(function(copy) {
813
814                                 if (!copy) {
815                                     $scope.focusBarcode = true;
816                                     $scope.copyNotFound = true;
817                                     return;
818                                 }
819
820                                 $scope.copyId = copy.id();
821                                 copy.barcode($scope.barcode2);
822
823                                 egCore.pcrud.update(copy).then(function(stat) {
824                                     $scope.updateOK = stat;
825                                     $scope.focusBarcode = true;
826                                     if (stat) service.add_barcode_to_list(copy.barcode());
827                                 });
828
829                             });
830                             $uibModalInstance.close();
831                         }
832
833                         $scope.cancel = function($event) {
834                             $uibModalInstance.dismiss();
835                             $event.preventDefault();
836                         }
837                     }
838                 ]
839             });
840         });
841     }
842
843     // this "transfers" selected copies to a new owning library,
844     // auto-creating volumes and deleting unused volumes as required.
845     service.changeItemOwningLib = function(items) {
846         var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
847         if (!xfer_target || !items.length) {
848             return;
849         }
850         var vols_to_move   = {};
851         var copies_to_move = {};
852         angular.forEach(items, function(item) {
853             if (item['call_number.owning_lib'] != xfer_target) {
854                 if (item['call_number.id'] in vols_to_move) {
855                     copies_to_move[item['call_number.id']].push(item.id);
856                 } else {
857                     vols_to_move[item['call_number.id']] = {
858                         label       : item['call_number.label'],
859                         label_class : item['call_number.label_class'],
860                         record      : item['call_number.record.id'],
861                         prefix      : item['call_number.prefix.id'],
862                         suffix      : item['call_number.suffix.id']
863                     };
864                     copies_to_move[item['call_number.id']] = new Array;
865                     copies_to_move[item['call_number.id']].push(item.id);
866                 }
867             }
868         });
869
870         var promises = [];
871         angular.forEach(vols_to_move, function(vol, vol_id) {
872             promises.push(egCore.net.request(
873                 'open-ils.cat',
874                 'open-ils.cat.call_number.find_or_create',
875                 egCore.auth.token(),
876                 vol.label,
877                 vol.record,
878                 xfer_target,
879                 vol.prefix,
880                 vol.suffix,
881                 vol.label_class
882             ).then(function(resp) {
883                 var evt = egCore.evt.parse(resp);
884                 if (evt) return;
885                 return egCore.net.request(
886                     'open-ils.cat',
887                     'open-ils.cat.transfer_copies_to_volume',
888                     egCore.auth.token(),
889                     resp.acn_id,
890                     copies_to_move[vol_id]
891                 );
892             }));
893         });
894
895         $q.all(promises)
896         .then(
897             function() {
898                 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
899             }
900         );
901     }
902
903     service.transferItems = function (items){
904         var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_vol');
905         var copy_ids = service.gatherSelectedHoldingsIds(items);
906         if (xfer_target && copy_ids.length > 0) {
907             egCore.net.request(
908                 'open-ils.cat',
909                 'open-ils.cat.transfer_copies_to_volume',
910                 egCore.auth.token(),
911                 xfer_target,
912                 copy_ids
913             ).then(
914                 function(resp) { // oncomplete
915                     var evt = egCore.evt.parse(resp);
916                     if (evt) {
917                         egConfirmDialog.open(
918                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
919                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
920                             {'evt_desc': evt}
921                         ).result.then(function() {
922                             egCore.net.request(
923                                 'open-ils.cat',
924                                 'open-ils.cat.transfer_copies_to_volume.override',
925                                 egCore.auth.token(),
926                                 xfer_target,
927                                 copy_ids,
928                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
929                             );
930                         }).then(function() {
931                             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
932                         });
933                     } else {
934                         angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
935                     }
936
937                 },
938                 null, // onerror
939                 null // onprogress
940             );
941         }
942     }
943
944     service.mark_missing_pieces = function(copy,outer_scope) {
945         var b = copy.barcode();
946         var t = egCore.idl.toHash(copy.call_number()).record.title;
947         egConfirmDialog.open(
948             egCore.strings.CONFIRM_MARK_MISSING_TITLE,
949             egCore.strings.CONFIRM_MARK_MISSING_BODY,
950             { barcode : b, title : t }
951         ).result.then(function() {
952
953             // kick off mark missing
954             return egCore.net.request(
955                 'open-ils.circ',
956                 'open-ils.circ.mark_item_missing_pieces',
957                 egCore.auth.token(), copy.id()
958             )
959
960         }).then(function(resp) {
961             var evt = egCore.evt.parse(resp); // should always produce event
962
963             if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
964                 return egAlertDialog.open(
965                     egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
966             }
967
968             var payload = evt.payload;
969
970             // TODO: open copy editor inline?  new tab?
971
972             // print the missing pieces slip
973             var promise = $q.when();
974             if (payload.slip) {
975                 // wait for completion, since it may spawn a confirm dialog
976                 promise = egCore.print.print({
977                     context : 'receipt',
978                     content_type : 'text/html',
979                     content : payload.slip.template_output().data()
980                 });
981             }
982
983             if (payload.letter) {
984                 outer_scope.letter = payload.letter.template_output().data();
985             }
986
987             // apply patron penalty
988             if (payload.circ) {
989                 promise.then(function() {
990                     egCirc.create_penalty(payload.circ.usr())
991                 });
992             }
993
994         });
995     }
996
997     service.print_spine_labels = function(copy_ids){
998         egCore.net.request(
999             'open-ils.actor',
1000             'open-ils.actor.anon_cache.set_value',
1001             null, 'print-labels-these-copies', {
1002                 copies : copy_ids
1003             }
1004         ).then(function(key) {
1005             if (key) {
1006                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
1007                 $timeout(function() { $window.open(url, '_blank') });
1008             } else {
1009                 alert('Could not create anonymous cache key!');
1010             }
1011         });
1012     }
1013
1014     service.show_in_catalog = function(copy_list){
1015         angular.forEach(copy_list, function(copy){
1016             window.open('/eg2/staff/catalog/record/'+copy['call_number.record.id'], '_blank')
1017         });
1018     }
1019
1020     return service;
1021 }])
1022 .filter('string_pick', function() { return function(i){ return arguments[i] || ''; }; })
1023