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