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