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