]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/services/item.js
LP 1779467: Enhance Mark Items Functionality
[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','egCirc','$uibModal','$q','$timeout','$window','egConfirmDialog','egAlertDialog',
8 function(egCore , egCirc , $uibModal , $q , $timeout , $window , 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(copy_list) {
228         if (copy_list.length == 0) return;
229
230         return $uibModal.open({
231             templateUrl: './cat/catalog/t_add_to_bucket',
232             backdrop: 'static',
233             animation: true,
234             size: 'md',
235             controller:
236                    ['$scope','$uibModalInstance',
237             function($scope , $uibModalInstance) {
238
239                 $scope.bucket_id = 0;
240                 $scope.newBucketName = '';
241                 $scope.allBuckets = [];
242
243                 egCore.net.request(
244                     'open-ils.actor',
245                     'open-ils.actor.container.retrieve_by_class.authoritative',
246                     egCore.auth.token(), egCore.auth.user().id(),
247                     'copy', 'staff_client'
248                 ).then(function(buckets) { $scope.allBuckets = buckets; });
249
250                 $scope.add_to_bucket = function() {
251                     var promises = [];
252                     angular.forEach(copy_list, function (cp) {
253                         var item = new egCore.idl.ccbi()
254                         item.bucket($scope.bucket_id);
255                         item.target_copy(cp);
256                         promises.push(
257                             egCore.net.request(
258                                 'open-ils.actor',
259                                 'open-ils.actor.container.item.create',
260                                 egCore.auth.token(), 'copy', item
261                             )
262                         );
263
264                         return $q.all(promises).then(function() {
265                             $uibModalInstance.close();
266                         });
267                     });
268                 }
269
270                 $scope.add_to_new_bucket = function() {
271                     var bucket = new egCore.idl.ccb();
272                     bucket.owner(egCore.auth.user().id());
273                     bucket.name($scope.newBucketName);
274                     bucket.description('');
275                     bucket.btype('staff_client');
276
277                     return egCore.net.request(
278                         'open-ils.actor',
279                         'open-ils.actor.container.create',
280                         egCore.auth.token(), 'copy', bucket
281                     ).then(function(bucket) {
282                         $scope.bucket_id = bucket;
283                         $scope.add_to_bucket();
284                     });
285                 }
286
287                 $scope.cancel = function() {
288                     $uibModalInstance.dismiss();
289                 }
290             }]
291         });
292     }
293
294     service.make_copies_bookable = function(items) {
295
296         var copies_by_record = {};
297         var record_list = [];
298         angular.forEach(
299             items,
300             function (item) {
301                 var record_id = item['call_number.record.id'];
302                 if (typeof copies_by_record[ record_id ] == 'undefined') {
303                     copies_by_record[ record_id ] = [];
304                     record_list.push( record_id );
305                 }
306                 copies_by_record[ record_id ].push(item.id);
307             }
308         );
309
310         var promises = [];
311         var combined_results = [];
312         angular.forEach(record_list, function(record_id) {
313             promises.push(
314                 egCore.net.request(
315                     'open-ils.booking',
316                     'open-ils.booking.resources.create_from_copies',
317                     egCore.auth.token(),
318                     copies_by_record[record_id]
319                 ).then(function(results) {
320                     if (results && results['brsrc']) {
321                         combined_results = combined_results.concat(results['brsrc']);
322                     }
323                 })
324             );
325         });
326
327         $q.all(promises).then(function() {
328             if (combined_results.length > 0) {
329                 $uibModal.open({
330                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
331                     backdrop: 'static',
332                     animation: true,
333                     size: 'md',
334                     controller:
335                            ['$scope','$location','egCore','$uibModalInstance',
336                     function($scope , $location , egCore , $uibModalInstance) {
337
338                         $scope.funcs = {
339                             ses : egCore.auth.token(),
340                             resultant_brsrc : combined_results.map(function(o) { return o[0]; })
341                         }
342
343                         var booking_path = '/eg/conify/global/booking/resource';
344
345                         $scope.booking_admin_url =
346                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
347                     }]
348                 });
349             }
350         });
351     }
352
353     service.book_copies_now = function(items) {
354         var copies_by_record = {};
355         var record_list = [];
356         angular.forEach(
357             items,
358             function (item) {
359                 var record_id = item['call_number.record.id'];
360                 if (typeof copies_by_record[ record_id ] == 'undefined') {
361                     copies_by_record[ record_id ] = [];
362                     record_list.push( record_id );
363                 }
364                 copies_by_record[ record_id ].push(item.id);
365             }
366         );
367
368         var promises = [];
369         var combined_brt = [];
370         var combined_brsrc = [];
371         angular.forEach(record_list, function(record_id) {
372             promises.push(
373                 egCore.net.request(
374                     'open-ils.booking',
375                     'open-ils.booking.resources.create_from_copies',
376                     egCore.auth.token(),
377                     copies_by_record[record_id]
378                 ).then(function(results) {
379                     if (results && results['brt']) {
380                         combined_brt = combined_brt.concat(results['brt']);
381                     }
382                     if (results && results['brsrc']) {
383                         combined_brsrc = combined_brsrc.concat(results['brsrc']);
384                     }
385                 })
386             );
387         });
388
389         $q.all(promises).then(function() {
390             if (combined_brt.length > 0 || combined_brsrc.length > 0) {
391                 $uibModal.open({
392                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
393                     backdrop: 'static',
394                     animation: true,
395                     size: 'md',
396                     controller:
397                            ['$scope','$location','egCore','$uibModalInstance',
398                     function($scope , $location , egCore , $uibModalInstance) {
399
400                         $scope.funcs = {
401                             ses : egCore.auth.token(),
402                             bresv_interface_opts : {
403                                 booking_results : {
404                                      brt : combined_brt
405                                     ,brsrc : combined_brsrc
406                                 }
407                             }
408                         }
409
410                         var booking_path = '/eg/booking/reservation';
411
412                         $scope.booking_admin_url =
413                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
414
415                     }]
416                 });
417             }
418         });
419     }
420
421     service.requestItems = function(copy_list) {
422         if (copy_list.length == 0) return;
423
424         return $uibModal.open({
425             templateUrl: './cat/catalog/t_request_items',
426             backdrop: 'static',
427             animation: true,
428             controller:
429                    ['$scope','$uibModalInstance','egUser',
430             function($scope , $uibModalInstance , egUser) {
431                 $scope.user = null;
432                 $scope.first_user_fetch = true;
433
434                 $scope.hold_data = {
435                     hold_type : 'C',
436                     copy_list : copy_list,
437                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
438                     user      : egCore.auth.user().id()
439                 };
440
441                 egUser.get( $scope.hold_data.user ).then(function(u) {
442                     $scope.user = u;
443                     $scope.barcode = u.card().barcode();
444                     $scope.user_name = egUser.format_name(u);
445                     $scope.hold_data.user = u.id();
446                 });
447
448                 $scope.user_name = '';
449                 $scope.barcode = '';
450                 $scope.$watch('barcode', function (n) {
451                     if (!$scope.first_user_fetch) {
452                         egUser.getByBarcode(n).then(function(u) {
453                             $scope.user = u;
454                             $scope.user_name = egUser.format_name(u);
455                             $scope.hold_data.user = u.id();
456                         }, function() {
457                             $scope.user = null;
458                             $scope.user_name = '';
459                             delete $scope.hold_data.user;
460                         });
461                     }
462                     $scope.first_user_fetch = false;
463                 });
464
465                 $scope.ok = function(h) {
466                     var args = {
467                         patronid  : h.user,
468                         hold_type : h.hold_type,
469                         pickup_lib: h.pickup_lib.id(),
470                         depth     : 0
471                     };
472
473                     egCore.net.request(
474                         'open-ils.circ',
475                         'open-ils.circ.holds.test_and_create.batch.override',
476                         egCore.auth.token(), args, h.copy_list
477                     );
478
479                     $uibModalInstance.close();
480                 }
481
482                 $scope.cancel = function($event) {
483                     $uibModalInstance.dismiss();
484                     $event.preventDefault();
485                 }
486             }]
487         });
488     }
489
490     service.attach_to_peer_bib = function(items) {
491         if (items.length == 0) return;
492
493         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
494             if (!target_record) return;
495
496             return $uibModal.open({
497                 templateUrl: './cat/catalog/t_conjoined_selector',
498                 backdrop: 'static',
499                 animation: true,
500                 controller:
501                        ['$scope','$uibModalInstance',
502                 function($scope , $uibModalInstance) {
503                     $scope.update = false;
504
505                     $scope.peer_type = null;
506                     $scope.peer_type_list = [];
507
508                     get_peer_types = function() {
509                         if (egCore.env.bpt)
510                             return $q.when(egCore.env.bpt.list);
511
512                         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
513                         .then(function(list) {
514                             egCore.env.absorbList(list, 'bpt');
515                             return list;
516                         });
517                     }
518
519                     get_peer_types().then(function(list){
520                         $scope.peer_type_list = list;
521                     });
522
523                     $scope.ok = function(type) {
524                         var promises = [];
525
526                         angular.forEach(items, function (cp) {
527                             var n = new egCore.idl.bpbcm();
528                             n.isnew(true);
529                             n.peer_record(target_record);
530                             n.target_copy(cp.id);
531                             n.peer_type(type);
532                             promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
533                         });
534
535                         return $q.all(promises).then(function(){$uibModalInstance.close()});
536                     }
537
538                     $scope.cancel = function($event) {
539                         $uibModalInstance.dismiss();
540                         $event.preventDefault();
541                     }
542                 }]
543             });
544         });
545     }
546
547     service.selectedHoldingsCopyDelete = function (items) {
548         if (items.length == 0) return;
549
550         var copy_objects = [];
551         egCore.pcrud.search('acp',
552             {deleted : 'f', id : items.map(function(el){return el.id;}) },
553             { flesh : 1, flesh_fields : { acp : ['call_number'] } }
554         ).then(function() {
555
556             var cnHash = {};
557             var perCnCopies = {};
558
559             var cn_count = 0;
560             var cp_count = 0;
561
562             angular.forEach(
563                 copy_objects,
564                 function (cp) {
565                     cp.isdeleted(1);
566                     cp_count++;
567                     var cn_id = cp.call_number().id();
568                     if (!cnHash[cn_id]) {
569                         cnHash[cn_id] = cp.call_number();
570                         perCnCopies[cn_id] = [cp];
571                     } else {
572                         perCnCopies[cn_id].push(cp);
573                     }
574                     cp.call_number(cn_id); // prevent loops in JSON-ification
575                 }
576             );
577
578             angular.forEach(perCnCopies, function (v, k) {
579                 cnHash[k].copies(v);
580             });
581
582             cnList = [];
583             angular.forEach(cnHash, function (v, k) {
584                 cnList.push(v);
585             });
586
587             if (cnList.length == 0) return;
588
589             var flags = {};
590
591             egConfirmDialog.open(
592                 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
593                 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
594                 {copies : cp_count, volumes : cn_count}
595             ).result.then(function() {
596                 egCore.net.request(
597                     'open-ils.cat',
598                     'open-ils.cat.asset.volume.fleshed.batch.update.override',
599                     egCore.auth.token(), cnList, 1, flags
600                 ).then(function(){
601                     angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
602                 });
603             });
604         },
605         null,
606         function(copy) {
607             copy_objects.push(copy);
608         });
609     }
610
611     service.checkin = function (items) {
612         angular.forEach(items, function (cp) {
613             egCirc.checkin({copy_barcode:cp.barcode}).then(
614                 function() { service.add_barcode_to_list(cp.barcode) }
615             );
616         });
617     }
618
619     service.renew = function (items) {
620         angular.forEach(items, function (cp) {
621             egCirc.renew({copy_barcode:cp.barcode}).then(
622                 function() { service.add_barcode_to_list(cp.barcode) }
623             );
624         });
625     }
626
627     service.cancel_transit = function (items) {
628         angular.forEach(items, function(cp) {
629             egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
630                 .then(function(t) { return egCirc.abort_transit(t.id())    })
631                 .then(function()  { return service.add_barcode_to_list(cp.barcode) });
632         });
633     }
634
635     service.selectedHoldingsDamaged = function (items) {
636         angular.forEach(items, function(cp) {
637             if (cp) {
638                 egCirc.mark_damaged({
639                     id: cp.id,
640                     barcode: cp.barcode,
641                     refresh: cp.refresh
642                 });
643             }
644         });
645     }
646
647     service.selectedHoldingsDiscard = function (items) {
648         egCirc.mark_discard(items.map(function(el){return {id : el.id, barcode : el.barcode};})).then(function(){
649             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
650         });
651     }
652
653     service.selectedHoldingsMissing = function (items) {
654         egCirc.mark_missing(items.map(function(el){return {id : el.id, barcode : el.barcode};})).then(function(){
655             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
656         });
657     }
658
659     service.gatherSelectedRecordIds = function (items) {
660         var rid_list = [];
661         angular.forEach(
662             items,
663             function (item) {
664                 if (rid_list.indexOf(item['call_number.record.id']) == -1)
665                     rid_list.push(item['call_number.record.id'])
666             }
667         );
668         return rid_list;
669     }
670
671     service.gatherSelectedVolumeIds = function (items,rid) {
672         var cn_id_list = [];
673         angular.forEach(
674             items,
675             function (item) {
676                 if (rid && item['call_number.record.id'] != rid) return;
677                 if (cn_id_list.indexOf(item['call_number.id']) == -1)
678                     cn_id_list.push(item['call_number.id'])
679             }
680         );
681         return cn_id_list;
682     }
683
684     service.gatherSelectedHoldingsIds = function (items,rid) {
685         var cp_id_list = [];
686         angular.forEach(
687             items,
688             function (item) {
689                 if (rid && item['call_number.record.id'] != rid) return;
690                 cp_id_list.push(item.id)
691             }
692         );
693         return cp_id_list;
694     }
695
696     service.spawnHoldingsAdd = function (items,use_vols,use_copies){
697         angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
698             var raw = [];
699             if (use_copies) { // just a copy on existing volumes
700                 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
701                     raw.push( {callnumber : v} );
702                 });
703             } else if (use_vols) {
704                 angular.forEach(
705                     service.gatherSelectedHoldingsIds(items,r),
706                     function (i) {
707                         angular.forEach(items, function(item) {
708                             if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
709                         });
710                     }
711                 );
712             }
713
714             if (raw.length == 0) raw.push({});
715
716             egCore.net.request(
717                 'open-ils.actor',
718                 'open-ils.actor.anon_cache.set_value',
719                 null, 'edit-these-copies', {
720                     record_id: r,
721                     raw: raw,
722                     hide_vols : false,
723                     hide_copies : false
724                 }
725             ).then(function(key) {
726                 if (key) {
727                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
728                     $timeout(function() { $window.open(url, '_blank') });
729                 } else {
730                     alert('Could not create anonymous cache key!');
731                 }
732             });
733         });
734     }
735
736     service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
737         var item_ids = [];
738         angular.forEach(items, function(i){
739             item_ids.push(i.id);
740         });
741
742         // provide record_id iff one record is selected.
743         // 0 disables record summary
744         var record_ids = service.gatherSelectedRecordIds(items);
745         var record_id  = record_ids.length === 1 ? record_ids[0] : 0;
746         egCore.net.request(
747             'open-ils.actor',
748             'open-ils.actor.anon_cache.set_value',
749             null,
750             'edit-these-copies',
751             {
752                 record_id: record_id,
753                 copies: item_ids,
754                 raw: {},
755                 hide_vols : hide_vols,
756                 hide_copies : hide_copies
757             }).then(function(key) {
758                 if (key) {
759                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
760                     $timeout(function() { $window.open(url, '_blank') });
761                 } else {
762                     alert('Could not create anonymous cache key!');
763                 }
764             });
765     }
766
767     service.replaceBarcodes = function(items) {
768         angular.forEach(items, function (cp) {
769             $uibModal.open({
770                 templateUrl: './cat/share/t_replace_barcode',
771                 backdrop: 'static',
772                 animation: true,
773                 controller:
774                            ['$scope','$uibModalInstance',
775                     function($scope , $uibModalInstance) {
776                         $scope.isModal = true;
777                         $scope.focusBarcode = false;
778                         $scope.focusBarcode2 = true;
779                         $scope.barcode1 = cp.barcode;
780
781                         $scope.updateBarcode = function() {
782                             $scope.copyNotFound = false;
783                             $scope.updateOK = false;
784
785                             egCore.pcrud.search('acp',
786                                 {deleted : 'f', barcode : $scope.barcode1})
787                             .then(function(copy) {
788
789                                 if (!copy) {
790                                     $scope.focusBarcode = true;
791                                     $scope.copyNotFound = true;
792                                     return;
793                                 }
794
795                                 $scope.copyId = copy.id();
796                                 copy.barcode($scope.barcode2);
797
798                                 egCore.pcrud.update(copy).then(function(stat) {
799                                     $scope.updateOK = stat;
800                                     $scope.focusBarcode = true;
801                                     if (stat) service.add_barcode_to_list(copy.barcode());
802                                 });
803
804                             });
805                             $uibModalInstance.close();
806                         }
807
808                         $scope.cancel = function($event) {
809                             $uibModalInstance.dismiss();
810                             $event.preventDefault();
811                         }
812                     }
813                 ]
814             });
815         });
816     }
817
818     // this "transfers" selected copies to a new owning library,
819     // auto-creating volumes and deleting unused volumes as required.
820     service.changeItemOwningLib = function(items) {
821         var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
822         if (!xfer_target || !items.length) {
823             return;
824         }
825         var vols_to_move   = {};
826         var copies_to_move = {};
827         angular.forEach(items, function(item) {
828             if (item['call_number.owning_lib'] != xfer_target) {
829                 if (item['call_number.id'] in vols_to_move) {
830                     copies_to_move[item['call_number.id']].push(item.id);
831                 } else {
832                     vols_to_move[item['call_number.id']] = {
833                         label       : item['call_number.label'],
834                         label_class : item['call_number.label_class'],
835                         record      : item['call_number.record.id'],
836                         prefix      : item['call_number.prefix.id'],
837                         suffix      : item['call_number.suffix.id']
838                     };
839                     copies_to_move[item['call_number.id']] = new Array;
840                     copies_to_move[item['call_number.id']].push(item.id);
841                 }
842             }
843         });
844
845         var promises = [];
846         angular.forEach(vols_to_move, function(vol, vol_id) {
847             promises.push(egCore.net.request(
848                 'open-ils.cat',
849                 'open-ils.cat.call_number.find_or_create',
850                 egCore.auth.token(),
851                 vol.label,
852                 vol.record,
853                 xfer_target,
854                 vol.prefix,
855                 vol.suffix,
856                 vol.label_class
857             ).then(function(resp) {
858                 var evt = egCore.evt.parse(resp);
859                 if (evt) return;
860                 return egCore.net.request(
861                     'open-ils.cat',
862                     'open-ils.cat.transfer_copies_to_volume',
863                     egCore.auth.token(),
864                     resp.acn_id,
865                     copies_to_move[vol_id]
866                 );
867             }));
868         });
869
870         $q.all(promises)
871         .then(
872             function() {
873                 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
874             }
875         );
876     }
877
878     service.transferItems = function (items){
879         var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_vol');
880         var copy_ids = service.gatherSelectedHoldingsIds(items);
881         if (xfer_target && copy_ids.length > 0) {
882             egCore.net.request(
883                 'open-ils.cat',
884                 'open-ils.cat.transfer_copies_to_volume',
885                 egCore.auth.token(),
886                 xfer_target,
887                 copy_ids
888             ).then(
889                 function(resp) { // oncomplete
890                     var evt = egCore.evt.parse(resp);
891                     if (evt) {
892                         egConfirmDialog.open(
893                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
894                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
895                             {'evt_desc': evt}
896                         ).result.then(function() {
897                             egCore.net.request(
898                                 'open-ils.cat',
899                                 'open-ils.cat.transfer_copies_to_volume.override',
900                                 egCore.auth.token(),
901                                 xfer_target,
902                                 copy_ids,
903                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
904                             );
905                         }).then(function() {
906                             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
907                         });
908                     } else {
909                         angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
910                     }
911
912                 },
913                 null, // onerror
914                 null // onprogress
915             );
916         }
917     }
918
919     service.mark_missing_pieces = function(copy,outer_scope) {
920         var b = copy.barcode();
921         var t = egCore.idl.toHash(copy.call_number()).record.title;
922         egConfirmDialog.open(
923             egCore.strings.CONFIRM_MARK_MISSING_TITLE,
924             egCore.strings.CONFIRM_MARK_MISSING_BODY,
925             { barcode : b, title : t }
926         ).result.then(function() {
927
928             // kick off mark missing
929             return egCore.net.request(
930                 'open-ils.circ',
931                 'open-ils.circ.mark_item_missing_pieces',
932                 egCore.auth.token(), copy.id()
933             )
934
935         }).then(function(resp) {
936             var evt = egCore.evt.parse(resp); // should always produce event
937
938             if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
939                 return egAlertDialog.open(
940                     egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
941             }
942
943             var payload = evt.payload;
944
945             // TODO: open copy editor inline?  new tab?
946
947             // print the missing pieces slip
948             var promise = $q.when();
949             if (payload.slip) {
950                 // wait for completion, since it may spawn a confirm dialog
951                 promise = egCore.print.print({
952                     context : 'default',
953                     content_type : 'text/html',
954                     content : payload.slip.template_output().data()
955                 });
956             }
957
958             if (payload.letter) {
959                 outer_scope.letter = payload.letter.template_output().data();
960             }
961
962             // apply patron penalty
963             if (payload.circ) {
964                 promise.then(function() {
965                     egCirc.create_penalty(payload.circ.usr())
966                 });
967             }
968
969         });
970     }
971
972     service.print_spine_labels = function(copy_ids){
973         egCore.net.request(
974             'open-ils.actor',
975             'open-ils.actor.anon_cache.set_value',
976             null, 'print-labels-these-copies', {
977                 copies : copy_ids
978             }
979         ).then(function(key) {
980             if (key) {
981                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
982                 $timeout(function() { $window.open(url, '_blank') });
983             } else {
984                 alert('Could not create anonymous cache key!');
985             }
986         });
987     }
988
989     service.show_in_catalog = function(copy_list){
990         angular.forEach(copy_list, function(copy){
991             window.open('/eg/staff/cat/catalog/record/'+copy['call_number.record.id']+'/catalog', '_blank')
992         });
993     }
994
995     return service;
996 }])
997 .filter('string_pick', function() { return function(i){ return arguments[i] || ''; }; })