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