]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/services/item.js
LP#1689325 - require most modals have explicit 'exit' or 'cancel' action inside the...
[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         egCirc.mark_damaged(items.map(function(el){return el.id;})).then(function(){
583             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
584         });
585     }
586
587     service.selectedHoldingsMissing = function (items) {
588         egCirc.mark_missing(items.map(function(el){return el.id;})).then(function(){
589             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
590         });
591     }
592
593     service.gatherSelectedRecordIds = function (items) {
594         var rid_list = [];
595         angular.forEach(
596             items,
597             function (item) {
598                 if (rid_list.indexOf(item['call_number.record.id']) == -1)
599                     rid_list.push(item['call_number.record.id'])
600             }
601         );
602         return rid_list;
603     }
604
605     service.gatherSelectedVolumeIds = function (items,rid) {
606         var cn_id_list = [];
607         angular.forEach(
608             items,
609             function (item) {
610                 if (rid && item['call_number.record.id'] != rid) return;
611                 if (cn_id_list.indexOf(item['call_number.id']) == -1)
612                     cn_id_list.push(item['call_number.id'])
613             }
614         );
615         return cn_id_list;
616     }
617
618     service.gatherSelectedHoldingsIds = function (items,rid) {
619         var cp_id_list = [];
620         angular.forEach(
621             items,
622             function (item) {
623                 if (rid && item['call_number.record.id'] != rid) return;
624                 cp_id_list.push(item.id)
625             }
626         );
627         return cp_id_list;
628     }
629
630     service.spawnHoldingsAdd = function (items,use_vols,use_copies){
631         angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
632             var raw = [];
633             if (use_copies) { // just a copy on existing volumes
634                 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
635                     raw.push( {callnumber : v} );
636                 });
637             } else if (use_vols) {
638                 angular.forEach(
639                     service.gatherSelectedHoldingsIds(items,r),
640                     function (i) {
641                         angular.forEach(items, function(item) {
642                             if (i == item.id) raw.push({owner : item['call_number.owning_lib']});
643                         });
644                     }
645                 );
646             }
647
648             if (raw.length == 0) raw.push({});
649
650             egCore.net.request(
651                 'open-ils.actor',
652                 'open-ils.actor.anon_cache.set_value',
653                 null, 'edit-these-copies', {
654                     record_id: r,
655                     raw: raw,
656                     hide_vols : false,
657                     hide_copies : false
658                 }
659             ).then(function(key) {
660                 if (key) {
661                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
662                     $timeout(function() { $window.open(url, '_blank') });
663                 } else {
664                     alert('Could not create anonymous cache key!');
665                 }
666             });
667         });
668     }
669
670     service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
671         angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
672             egCore.net.request(
673                 'open-ils.actor',
674                 'open-ils.actor.anon_cache.set_value',
675                 null, 'edit-these-copies', {
676                     record_id: r,
677                     copies: service.gatherSelectedHoldingsIds(items,r),
678                     raw: {},
679                     hide_vols : hide_vols,
680                     hide_copies : hide_copies
681                 }
682             ).then(function(key) {
683                 if (key) {
684                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
685                     $timeout(function() { $window.open(url, '_blank') });
686                 } else {
687                     alert('Could not create anonymous cache key!');
688                 }
689             });
690         });
691     }
692
693     service.replaceBarcodes = function(items) {
694         angular.forEach(items, function (cp) {
695             $uibModal.open({
696                 templateUrl: './cat/share/t_replace_barcode',
697                 backdrop: 'static',
698                 animation: true,
699                 controller:
700                            ['$scope','$uibModalInstance',
701                     function($scope , $uibModalInstance) {
702                         $scope.isModal = true;
703                         $scope.focusBarcode = false;
704                         $scope.focusBarcode2 = true;
705                         $scope.barcode1 = cp.barcode;
706
707                         $scope.updateBarcode = function() {
708                             $scope.copyNotFound = false;
709                             $scope.updateOK = false;
710
711                             egCore.pcrud.search('acp',
712                                 {deleted : 'f', barcode : $scope.barcode1})
713                             .then(function(copy) {
714
715                                 if (!copy) {
716                                     $scope.focusBarcode = true;
717                                     $scope.copyNotFound = true;
718                                     return;
719                                 }
720
721                                 $scope.copyId = copy.id();
722                                 copy.barcode($scope.barcode2);
723
724                                 egCore.pcrud.update(copy).then(function(stat) {
725                                     $scope.updateOK = stat;
726                                     $scope.focusBarcode = true;
727                                     if (stat) service.add_barcode_to_list(copy.barcode());
728                                 });
729
730                             });
731                             $uibModalInstance.close();
732                         }
733
734                         $scope.cancel = function($event) {
735                             $uibModalInstance.dismiss();
736                             $event.preventDefault();
737                         }
738                     }
739                 ]
740             });
741         });
742     }
743
744     // this "transfers" selected copies to a new owning library,
745     // auto-creating volumes and deleting unused volumes as required.
746     service.changeItemOwningLib = function(items) {
747         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
748         if (!xfer_target || !items.length) {
749             return;
750         }
751         var vols_to_move   = {};
752         var copies_to_move = {};
753         angular.forEach(items, function(item) {
754             if (item['call_number.owning_lib'] != xfer_target) {
755                 if (item['call_number.id'] in vols_to_move) {
756                     copies_to_move[item['call_number.id']].push(item.id);
757                 } else {
758                     vols_to_move[item['call_number.id']] = {
759                         label       : item['call_number.label'],
760                         label_class : item['call_number.label_class'],
761                         record      : item['call_number.record.id'],
762                         prefix      : item['call_number.prefix.id'],
763                         suffix      : item['call_number.suffix.id']
764                     };
765                     copies_to_move[item['call_number.id']] = new Array;
766                     copies_to_move[item['call_number.id']].push(item.id);
767                 }
768             }
769         });
770
771         var promises = [];
772         angular.forEach(vols_to_move, function(vol) {
773             promises.push(egCore.net.request(
774                 'open-ils.cat',
775                 'open-ils.cat.call_number.find_or_create',
776                 egCore.auth.token(),
777                 vol.label,
778                 vol.record,
779                 xfer_target,
780                 vol.prefix,
781                 vol.suffix,
782                 vol.label_class
783             ).then(function(resp) {
784                 var evt = egCore.evt.parse(resp);
785                 if (evt) return;
786                 return egCore.net.request(
787                     'open-ils.cat',
788                     'open-ils.cat.transfer_copies_to_volume',
789                     egCore.auth.token(),
790                     resp.acn_id,
791                     copies_to_move[vol.id]
792                 );
793             }));
794         });
795
796         angular.forEach(
797             items,
798             function(cp){
799                 promises.push(
800                     function(){ service.add_barcode_to_list(cp.barcode) }
801                 )
802             }
803         );
804         $q.all(promises);
805     }
806
807     service.transferItems = function (items){
808         var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
809         var copy_ids = service.gatherSelectedHoldingsIds(items);
810         if (xfer_target && copy_ids.length > 0) {
811             egCore.net.request(
812                 'open-ils.cat',
813                 'open-ils.cat.transfer_copies_to_volume',
814                 egCore.auth.token(),
815                 xfer_target,
816                 copy_ids
817             ).then(
818                 function(resp) { // oncomplete
819                     var evt = egCore.evt.parse(resp);
820                     if (evt) {
821                         egConfirmDialog.open(
822                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
823                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
824                             {'evt_desc': evt}
825                         ).result.then(function() {
826                             egCore.net.request(
827                                 'open-ils.cat',
828                                 'open-ils.cat.transfer_copies_to_volume.override',
829                                 egCore.auth.token(),
830                                 xfer_target,
831                                 copy_ids,
832                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
833                             );
834                         }).then(function() {
835                             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
836                         });
837                     } else {
838                         angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
839                     }
840
841                 },
842                 null, // onerror
843                 null // onprogress
844             );
845         }
846     }
847
848     service.mark_missing_pieces = function(copy) {
849         var b = copy.barcode();
850         var t = egCore.idl.toHash(copy.call_number()).record.title;
851         egConfirmDialog.open(
852             egCore.strings.CONFIRM_MARK_MISSING_TITLE,
853             egCore.strings.CONFIRM_MARK_MISSING_BODY,
854             { barcode : b, title : t }
855         ).result.then(function() {
856
857             // kick off mark missing
858             return egCore.net.request(
859                 'open-ils.circ',
860                 'open-ils.circ.mark_item_missing_pieces',
861                 egCore.auth.token(), copy.id()
862             )
863
864         }).then(function(resp) {
865             var evt = egCore.evt.parse(resp); // should always produce event
866
867             if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
868                 return egAlertDialog.open(
869                     egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
870             }
871
872             var payload = evt.payload;
873
874             // TODO: open copy editor inline?  new tab?
875
876             // print the missing pieces slip
877             var promise = $q.when();
878             if (payload.slip) {
879                 // wait for completion, since it may spawn a confirm dialog
880                 promise = egCore.print.print({
881                     context : 'default',
882                     content_type : 'text/html',
883                     content : payload.slip.template_output().data()
884                 });
885             }
886
887             if (payload.letter) {
888                 $scope.letter = payload.letter.template_output().data();
889             }
890
891             // apply patron penalty
892             if (payload.circ) {
893                 promise.then(function() {
894                     egCirc.create_penalty(payload.circ.usr())
895                 });
896             }
897
898         });
899     }
900
901     service.print_spine_labels = function(copy_ids){
902         egCore.net.request(
903             'open-ils.actor',
904             'open-ils.actor.anon_cache.set_value',
905             null, 'print-labels-these-copies', {
906                 copies : copy_ids
907             }
908         ).then(function(key) {
909             if (key) {
910                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
911                 $timeout(function() { $window.open(url, '_blank') });
912             } else {
913                 alert('Could not create anonymous cache key!');
914             }
915         });
916     }
917
918     return service;
919 }])