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