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