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