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