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