]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/services/item.js
LP1615805 No inputs after submit in patron search (AngularJS)
[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','egOrg','egCirc','$uibModal','$q','$timeout','$window','ngToast','egConfirmDialog','egAlertDialog',
8 function(egCore , egOrg , egCirc , $uibModal , $q , $timeout , $window , ngToast , 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','floating','circ_modifier',
19                 'age_protect','circ_lib','copy_alerts', 'creator', 'editor', 'circ_as_type', 'latest_inventory', 'total_circ_count'],
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', 'create_date', 'edit_date'],
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, noPrepend) {
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                 if (!noPrepend) {
167                     service.copies.unshift(flatCopy);
168                 }
169             }
170
171             //Get in-house use count
172             egCore.pcrud.search('aihu',
173                 {item : flatCopy.id}, {}, {idlist : true, atomic : true})
174             .then(function(uses) {
175                 flatCopy._inHouseUseCount = uses.length;
176                 copyData.copy._inHouseUseCount = uses.length;
177             });
178
179             //Get Monograph Parts
180             egCore.pcrud.search('acpm',
181                 {target_copy: flatCopy.id},
182                 { flesh : 1, flesh_fields : { acpm : ['part'] } },
183                 {atomic :true})
184             .then(function(acpm_array) {
185                 angular.forEach(acpm_array, function(acpm) {
186                     flatCopy.parts = egCore.idl.toHash(acpm.part());
187                     copyData.copy.parts = egCore.idl.toHash(acpm.part());
188                 });
189             });
190
191             if (noPrepend) {
192                 return flatCopy;
193             }
194             return lastRes = {
195                 copy : copyData.copy,
196                 index : flatCopy.index
197             }
198         });
199
200
201     }
202
203     //all_items = selected grid rows, to be updated in place
204     service.updateInventory = function(copy_list, all_items) {
205         if (copy_list.length == 0) return;
206         return egCore.net.request(
207             'open-ils.circ',
208             'open-ils.circ.circulation.update_copy_inventory',
209             egCore.auth.token(), {copy_list: copy_list}
210         ).then(function(res) {
211             if (res) {
212                 if (all_items) angular.forEach(copy_list, function(copy) {
213                     angular.forEach(all_items, function(item) {
214                         if (copy == item.id) {
215                             egCore.pcrud.search('alci', {copy: copy},
216                               {flesh: 1, flesh_fields:
217                                 {alci: ['inventory_workstation']}
218                             }).then(function(alci) {
219                                 //update existing grid rows
220                                 if (alci) {
221                                     item["latest_inventory.inventory_date"] = alci.inventory_date();
222                                     item["latest_inventory.inventory_workstation.name"] =
223                                         alci.inventory_workstation().name();
224                                 }
225                             });
226                         }
227                     });
228                 });
229                 return res;
230             }
231         });
232     }
233
234     service.add_copies_to_bucket = function(list, bucket_type) {
235         if (list.length == 0) return;
236         if (!bucket_type) bucket_type = 'copy';
237
238         return $uibModal.open({
239             templateUrl: './cat/catalog/t_add_to_bucket',
240             backdrop: 'static',
241             animation: true,
242             size: 'md',
243             controller:
244                    ['$scope','$uibModalInstance',
245             function($scope , $uibModalInstance) {
246
247                 $scope.bucket_id = 0;
248                 $scope.newBucketName = '';
249                 $scope.allBuckets = [];
250
251                 egCore.net.request(
252                     'open-ils.actor',
253                     'open-ils.actor.container.retrieve_by_class.authoritative',
254                     egCore.auth.token(), egCore.auth.user().id(),
255                     bucket_type, 'staff_client'
256                 ).then(function(buckets) { $scope.allBuckets = buckets; });
257
258                 $scope.add_to_bucket = function() {
259                     var promise = $q.when();
260                     angular.forEach(list, function (entry) {
261                         var item = bucket_type == 'copy' ? new egCore.idl.ccbi() : new egCore.idl.cbrebi();
262                         item.bucket($scope.bucket_id);
263                         if (bucket_type == 'copy') item.target_copy(entry);
264                         if (bucket_type == 'biblio') item.target_biblio_record_entry(entry);
265                         promise = promise.then(function() {
266                             return egCore.net.request(
267                                 'open-ils.actor',
268                                 'open-ils.actor.container.item.create',
269                                 egCore.auth.token(), bucket_type, item
270                             );
271                         });
272                     });
273                     promise.then(function() {
274                         $uibModalInstance.close();
275                     });
276                 }
277
278                 $scope.add_to_new_bucket = function() {
279                     var bucket = bucket_type == 'copy' ? new egCore.idl.ccb() : new egCore.idl.cbreb();
280                     bucket.owner(egCore.auth.user().id());
281                     bucket.name($scope.newBucketName);
282                     bucket.description('');
283                     bucket.btype('staff_client');
284
285                     return egCore.net.request(
286                         'open-ils.actor',
287                         'open-ils.actor.container.create',
288                         egCore.auth.token(), bucket_type, bucket
289                     ).then(function(bucket) {
290                         $scope.bucket_id = bucket;
291                         $scope.add_to_bucket();
292                     });
293                 }
294
295                 $scope.cancel = function() {
296                     $uibModalInstance.dismiss();
297                 }
298             }]
299         });
300     }
301
302     service.create_carousel_from_items = function(list) {
303         if (list.length == 0) return;
304
305         return $uibModal.open({
306             templateUrl: './cat/catalog/t_create_carousel',
307             backdrop: 'static',
308             animation: true,
309             size: 'md',
310             controller:
311                    ['$scope','$uibModalInstance',
312             function($scope , $uibModalInstance) {
313
314                 $scope.carousel_name = '';
315
316                 $scope.create_carousel = function() {
317                     return egCore.net.request(
318                         'open-ils.actor',
319                         'open-ils.actor.carousel.create_carousel_from_items',
320                         egCore.auth.token(), $scope.carousel_name, list
321                     ).then(function(response) {
322                         if (response) {
323                             var evt = egCore.evt.parse(response);
324                             if (evt) {
325                                 ngToast.danger(evt);
326                             } else {
327                                 ngToast.success(egCore.strings.SUCCESS_CAROUSEL_CREATE);
328                                 $uibModalInstance.close();
329                             }
330                         }
331                     });
332                 }
333
334                 $scope.cancel = function() {
335                     $uibModalInstance.dismiss();
336                 }
337             }]
338         });
339     }
340
341     service.make_copies_bookable = function(items) {
342
343         var copies_by_record = {};
344         var record_list = [];
345         angular.forEach(
346             items,
347             function (item) {
348                 var record_id = item['call_number.record.id'];
349                 if (typeof copies_by_record[ record_id ] == 'undefined') {
350                     copies_by_record[ record_id ] = [];
351                     record_list.push( record_id );
352                 }
353                 copies_by_record[ record_id ].push(item.id);
354             }
355         );
356
357         var promises = [];
358         var combined_results = [];
359         angular.forEach(record_list, function(record_id) {
360             promises.push(
361                 egCore.net.request(
362                     'open-ils.booking',
363                     'open-ils.booking.resources.create_from_copies',
364                     egCore.auth.token(),
365                     copies_by_record[record_id]
366                 ).then(function(results) {
367                     if (results && results['brsrc']) {
368                         combined_results = combined_results.concat(results['brsrc']);
369                     }
370                 })
371             );
372         });
373
374         $q.all(promises).then(function() {
375             if (combined_results.length > 0) {
376                 $uibModal.open({
377                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
378                     backdrop: 'static',
379                     animation: true,
380                     size: 'md',
381                     controller:
382                            ['$scope','$location','egCore','$uibModalInstance',
383                     function($scope , $location , egCore , $uibModalInstance) {
384
385                         $scope.funcs = {
386                             ses : egCore.auth.token(),
387                             resultant_brsrc : combined_results.map(function(o) { return o[0]; })
388                         }
389
390                         var booking_path = '/eg/conify/global/booking/resource';
391
392                         $scope.booking_admin_url =
393                             $location.absUrl().replace(/\/eg\/staff\/.*/, booking_path);
394                     }]
395                 });
396             }
397         });
398     }
399
400     service.book_copies_now = function(barcode) {
401         location.href = "/eg2/staff/booking/create_reservation/for_resource/" + barcode;
402     }
403
404     service.manage_reservations = function(barcode) {
405         location.href = "/eg2/staff/booking/manage_reservations/by_resource/" + barcode;
406     }
407
408     service.requestItems = function(copy_list,record_list) {
409         if (copy_list.length == 0) return;
410         if (record_list) {
411             record_list = record_list.filter(function(v,i,s){ // dedup
412                 return s.indexOf(v) == i;
413             });
414         }
415
416         return $uibModal.open({
417             templateUrl: './cat/catalog/t_request_items',
418             backdrop: 'static',
419             animation: true,
420             controller:
421                    ['$scope','$uibModalInstance','egUser',
422             function($scope , $uibModalInstance , egUser) {
423                 $scope.user = null;
424                 $scope.first_user_fetch = true;
425
426                 $scope.hold_data = {
427                     hold_type : 'C',
428                     copy_list : copy_list,
429                     record_list : record_list,
430                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
431                     user      : egCore.auth.user().id(),
432                     honor_user_settings : 
433                         egCore.hatch.getLocalItem('eg.cat.request_items.honor_user_settings')
434                 };
435
436                 egUser.get( $scope.hold_data.user ).then(function(u) {
437                     $scope.user = u;
438                     $scope.barcode = u.card().barcode();
439                     $scope.user_name = egUser.format_name(u);
440                     $scope.hold_data.user = u.id();
441                 });
442
443                 $scope.user_name = '';
444                 $scope.barcode = '';
445                 function user_preferred_pickup_lib(u) {
446                     var pickup_lib = u.home_ou();
447                     angular.forEach(u.settings(), function (s) {
448                         if (s.name() == "opac.default_pickup_location") {
449                             pickup_lib = s.value();
450                         }
451                     });
452                     return egOrg.get(pickup_lib);
453                 }
454                 $scope.$watch('barcode', function (n) {
455                     if (!$scope.first_user_fetch) {
456                         egUser.getByBarcode(n).then(function(u) {
457                             $scope.user = u;
458                             $scope.user_name = egUser.format_name(u);
459                             $scope.hold_data.user = u.id();
460                             if ($scope.hold_data.honor_user_settings) {
461                                 $scope.hold_data.pickup_lib = user_preferred_pickup_lib(u);
462                             }
463                         }, function() {
464                             $scope.user = null;
465                             $scope.user_name = '';
466                             delete $scope.hold_data.user;
467                         });
468                     }
469                     $scope.first_user_fetch = false;
470                 });
471                 $scope.$watch('hold_data.honor_user_settings', function (n) {
472                     if (n && $scope.user) {
473                         $scope.hold_data.pickup_lib = user_preferred_pickup_lib($scope.user);
474                     } else {
475                         $scope.hold_data.pickup_lib = egCore.org.get(egCore.auth.user().ws_ou());
476                     }
477                     egCore.hatch.setLocalItem('eg.cat.request_items.honor_user_settings',n);
478                 });
479
480                 $scope.ok = function(h) {
481                     var args = {
482                         patronid  : h.user,
483                         hold_type : h.hold_type,
484                         pickup_lib: h.pickup_lib.id(),
485                         depth     : 0
486                     };
487
488                     egCore.net.request(
489                         'open-ils.circ',
490                         'open-ils.circ.holds.test_and_create.batch.override',
491                         egCore.auth.token(), args,
492                         h.hold_type == 'T' ? h.record_list : h.copy_list,
493                         { 'all' : 1, 'honor_user_settings' : h.honor_user_settings }
494                     ).then(function(r) {
495                         console.log('request result',r);
496                         if (isNaN(r.result)) {
497                             if (typeof r.result.desc != 'undefined') {
498                                 ngToast.danger(r.result.desc);
499                             } else {
500                                 if (typeof r.result.last_event != 'undefined') {
501                                     ngToast.danger(r.result.last_event.desc);
502                                 } else {
503                                     ngToast.danger(egCore.strings.FAILURE_HOLD_REQUEST);
504                                 }
505                             }
506                         } else {
507                             ngToast.success(egCore.strings.SUCCESS_HOLD_REQUEST);
508                         }
509                     });
510
511                     $uibModalInstance.close();
512                 }
513
514                 $scope.cancel = function($event) {
515                     $uibModalInstance.dismiss();
516                     $event.preventDefault();
517                 }
518             }]
519         });
520     }
521
522     service.attach_to_peer_bib = function(items) {
523         if (items.length == 0) return;
524
525         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
526             if (!target_record) return;
527
528             return $uibModal.open({
529                 templateUrl: './cat/catalog/t_conjoined_selector',
530                 backdrop: 'static',
531                 animation: true,
532                 controller:
533                        ['$scope','$uibModalInstance',
534                 function($scope , $uibModalInstance) {
535                     $scope.update = false;
536
537                     $scope.peer_type = null;
538                     $scope.peer_type_list = [];
539
540                     get_peer_types = function() {
541                         if (egCore.env.bpt)
542                             return $q.when(egCore.env.bpt.list);
543
544                         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
545                         .then(function(list) {
546                             egCore.env.absorbList(list, 'bpt');
547                             return list;
548                         });
549                     }
550
551                     get_peer_types().then(function(list){
552                         $scope.peer_type_list = list;
553                     });
554
555                     $scope.ok = function(type) {
556                         var promises = [];
557
558                         angular.forEach(items, function (cp) {
559                             var n = new egCore.idl.bpbcm();
560                             n.isnew(true);
561                             n.peer_record(target_record);
562                             n.target_copy(cp.id);
563                             n.peer_type(type);
564                             promises.push(egCore.pcrud.create(n).then(function(){service.add_barcode_to_list(cp.barcode)}));
565                         });
566
567                         return $q.all(promises).then(function(){$uibModalInstance.close()});
568                     }
569
570                     $scope.cancel = function($event) {
571                         $uibModalInstance.dismiss();
572                         $event.preventDefault();
573                     }
574                 }]
575             });
576         });
577     }
578
579     service.selectedHoldingsCopyDelete = function (items) {
580         if (items.length == 0) return;
581
582         var copy_objects = [];
583         egCore.pcrud.search('acp',
584             {deleted : 'f', id : items.map(function(el){return el.id;}) },
585             { flesh : 1, flesh_fields : { acp : ['call_number'] } }
586         ).then(function() {
587
588             var cnHash = {};
589             var perCnCopies = {};
590
591             var cn_count = 0;
592             var cp_count = 0;
593
594             angular.forEach(
595                 copy_objects,
596                 function (cp) {
597                     cp.isdeleted(1);
598                     cp_count++;
599                     var cn_id = cp.call_number().id();
600                     if (!cnHash[cn_id]) {
601                         cnHash[cn_id] = cp.call_number();
602                         perCnCopies[cn_id] = [cp];
603                     } else {
604                         perCnCopies[cn_id].push(cp);
605                     }
606                     cp.call_number(cn_id); // prevent loops in JSON-ification
607                 }
608             );
609
610             angular.forEach(perCnCopies, function (v, k) {
611                 cnHash[k].copies(v);
612             });
613
614             cnList = [];
615             angular.forEach(cnHash, function (v, k) {
616                 cnList.push(v);
617             });
618
619             if (cnList.length == 0) return;
620
621             var flags = {};
622
623             egConfirmDialog.open(
624                 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
625                 egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
626                 {copies : cp_count, volumes : cn_count}
627             ).result.then(function() {
628                 egCore.net.request(
629                     'open-ils.cat',
630                     'open-ils.cat.asset.volume.fleshed.batch.update',
631                     egCore.auth.token(), cnList, 1, flags
632                 ).then(function(resp){
633                     var evt = egCore.evt.parse(resp);
634                     if (evt) {
635                         egConfirmDialog.open(
636                             egCore.strings.OVERRIDE_DELETE_ITEMS_FROM_CATALOG_TITLE,
637                             egCore.strings.OVERRIDE_DELETE_ITEMS_FROM_CATALOG_BODY,
638                             {'evt_desc': evt.desc}
639                         ).result.then(function() {
640                             egCore.net.request(
641                                 'open-ils.cat',
642                                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
643                                 egCore.auth.token(), cnList, 1,
644                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
645                             ).then(function() {
646                                 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
647                             });
648                         });
649                     } else {
650                         angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
651                     }
652                 });
653             });
654         },
655         null,
656         function(copy) {
657             copy_objects.push(copy);
658         });
659     }
660
661     service.checkin = function (items) {
662         // Recursive function that creates a promise for each item. Once the dialog 
663         // window for a given item is closed the next promise is started and a
664         // new dialog is opened. 
665         // This keeps multiple popups from hitting the screen at once.
666         (function checkinLoop(i) {
667             if (i < items.length) new Promise((resolve, reject) => {
668                 egCirc.checkin({copy_barcode: items[i].barcode})
669                 .then(function() {
670                     service.add_barcode_to_list(items[i].barcode);
671                     resolve();
672                 })
673             }).then(checkinLoop.bind(null, i+1));
674         })(0);
675     }
676
677     service.renew = function (items) {
678         angular.forEach(items, function (cp) {
679             egCirc.renew({copy_barcode:cp.barcode}).then(
680                 function() { service.add_barcode_to_list(cp.barcode) }
681             );
682         });
683     }
684
685     service.cancel_transit = function (items) {
686         angular.forEach(items, function(cp) {
687             egCirc.find_copy_transit(null, {copy_barcode:cp.barcode})
688                 .then(function(t) { return egCirc.abort_transit(t.id())    })
689                 .then(function()  { return service.add_barcode_to_list(cp.barcode) });
690         });
691     }
692
693     service.selectedHoldingsDamaged = function (items) {
694         angular.forEach(items, function(cp) {
695             if (cp) {
696                 egCirc.mark_damaged({
697                     id: cp.id,
698                     barcode: cp.barcode,
699                     refresh: cp.refresh
700                 });
701             }
702         });
703     }
704
705     service.selectedHoldingsDiscard = function (items) {
706         egCirc.mark_discard(items.map(function(el){return {id : el.id, barcode : el.barcode};})).then(function(){
707             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
708         });
709     }
710
711     service.selectedHoldingsMissing = function (items) {
712         return egCirc.mark_missing(
713             items.map(function(el){return {id : el.id, barcode : el.barcode};})
714         ).then(function(modified){
715             var promise = $q.when();
716             angular.forEach(modified, function(barcode){
717                 promise = promise.then(function() {
718                     return service.add_barcode_to_list(barcode, true);
719                 });
720             });
721             return promise;
722         });
723     }
724
725     service.gatherSelectedRecordIds = function (items) {
726         var rid_list = [];
727         angular.forEach(
728             items,
729             function (item) {
730                 if (rid_list.indexOf(item['call_number.record.id']) == -1)
731                     rid_list.push(item['call_number.record.id'])
732             }
733         );
734         return rid_list;
735     }
736
737     service.gatherSelectedVolumeIds = function (items,rid) {
738         var cn_id_list = [];
739         angular.forEach(
740             items,
741             function (item) {
742                 if (rid && item['call_number.record.id'] != rid) return;
743                 if (cn_id_list.indexOf(item['call_number.id']) == -1)
744                     cn_id_list.push(item['call_number.id'])
745             }
746         );
747         return cn_id_list;
748     }
749
750     service.gatherSelectedHoldingsIds = function (items,rid) {
751         var cp_id_list = [];
752         angular.forEach(
753             items,
754             function (item) {
755                 if (rid && item['call_number.record.id'] != rid) return;
756                 cp_id_list.push(item.id)
757             }
758         );
759         return cp_id_list;
760     }
761
762     service.spawnHoldingsAdd = function (items,use_vols,use_copies){
763         angular.forEach(service.gatherSelectedRecordIds(items), function (r) {
764             var raw = [];
765             if (use_copies) { // just a copy on existing volumes
766                 angular.forEach(service.gatherSelectedVolumeIds(items,r), function (v) {
767                     raw.push( {callnumber : v} );
768                 });
769             } else if (use_vols) {
770                 angular.forEach(
771                     service.gatherSelectedHoldingsIds(items,r),
772                     function (i) {
773                         angular.forEach(items, function(item) {
774                             if (i == item.id) {
775                                 // owning_lib may be fleshed.
776                                 var owner = item['call_number.owning_lib.id']
777                                     || item['call_number.owning_lib'];
778                                 raw.push({owner : owner});
779                             }
780                         });
781                     }
782                 );
783             }
784
785             if (raw.length == 0) raw.push({});
786
787             egCore.net.request(
788                 'open-ils.actor',
789                 'open-ils.actor.anon_cache.set_value',
790                 null, 'edit-these-copies', {
791                     record_id: r,
792                     raw: raw,
793                     hide_vols : false,
794                     hide_copies : false
795                 }
796             ).then(function(key) {
797                 if (key) {
798                     var tab = (hide_vols === true) ? 'attrs' : 'holdings';
799                     var url = '/eg2/staff/cat/volcopy/' + tab + '/session/ ' + key;
800                     $timeout(function() { $window.open(url, '_blank') });
801                 } else {
802                     alert('Could not create anonymous cache key!');
803                 }
804             });
805         });
806     }
807
808     service.spawnHoldingsEdit = function (items,hide_vols,hide_copies){
809         var item_ids = [];
810         angular.forEach(items, function(i){
811             item_ids.push(i.id);
812         });
813
814         // provide record_id iff one record is selected.
815         // 0 disables record summary
816         var record_ids = service.gatherSelectedRecordIds(items);
817         var record_id  = record_ids.length === 1 ? record_ids[0] : 0;
818         egCore.net.request(
819             'open-ils.actor',
820             'open-ils.actor.anon_cache.set_value',
821             null,
822             'edit-these-copies',
823             {
824                 record_id: record_id,
825                 copies: item_ids,
826                 raw: {},
827                 hide_vols : hide_vols,
828                 hide_copies : hide_copies
829             }).then(function(key) {
830                 if (key) {
831                     var tab = (hide_vols === true) ? 'attrs' : 'holdings';
832                     var url = '/eg2/staff/cat/volcopy/' + tab + '/session/ ' + key;
833                     $timeout(function() { $window.open(url, '_blank') });
834                 } else {
835                     alert('Could not create anonymous cache key!');
836                 }
837             });
838     }
839
840     service.replaceBarcodes = function(items) {
841         angular.forEach(items, function (cp) {
842             $uibModal.open({
843                 templateUrl: './cat/share/t_replace_barcode',
844                 backdrop: 'static',
845                 animation: true,
846                 controller:
847                            ['$scope','$uibModalInstance',
848                     function($scope , $uibModalInstance) {
849                         $scope.isModal = true;
850                         $scope.focusBarcode = false;
851                         $scope.focusBarcode2 = true;
852                         $scope.barcode1 = cp.barcode;
853
854                         $scope.updateBarcode = function() {
855                             $scope.copyNotFound = false;
856                             $scope.updateOK = false;
857
858                             egCore.pcrud.search('acp',
859                                 {deleted : 'f', barcode : $scope.barcode1})
860                             .then(function(copy) {
861
862                                 if (!copy) {
863                                     $scope.focusBarcode = true;
864                                     $scope.copyNotFound = true;
865                                     return;
866                                 }
867
868                                 egCore.pcrud.search('acp',
869                                     {deleted : 'f', barcode : $scope.barcode2})
870                                 .then(function(newBarcodeCopy) {
871
872                                     if (newBarcodeCopy) {
873                                         $scope.duplicateBarcode = true;
874                                         return;
875                                     }
876
877                                     $scope.copyId = copy.id();
878
879                                     egCore.net.request(
880                                         'open-ils.cat',
881                                         'open-ils.cat.update_copy_barcode',
882                                         egCore.auth.token(), $scope.copyId, $scope.barcode2
883                                     ).then(function(resp) {
884                                         var evt = egCore.evt.parse(resp);
885                                         if (evt) {
886                                             console.log('toast 0 here 2', evt);
887                                         } else {
888                                             $scope.updateOK = true;
889                                             $scope.focusBarcode = true;
890                                             $scope.focusBarcode2 = false;
891                                             service.add_barcode_to_list($scope.barcode2);
892                                             $uibModalInstance.close();
893                                         }
894                                     });
895                                 });
896
897                             },function(E) {
898                                 console.log('toast 1 here 2',E);
899                             },function(E) {
900                                 console.log('toast 2 here 2',E);
901                             });
902                         }
903
904                         $scope.cancel = function($event) {
905                             $uibModalInstance.dismiss();
906                             $event.preventDefault();
907                         }
908                     }
909                 ]
910             });
911         });
912     }
913
914     // this "transfers" selected copies to a new owning library,
915     // auto-creating volumes and deleting unused volumes as required.
916     service.changeItemOwningLib = function(items) {
917         var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
918         if (!xfer_target || !items.length) {
919             return;
920         }
921         var vols_to_move   = {};
922         var copies_to_move = {};
923         angular.forEach(items, function(item) {
924             if (item['call_number.owning_lib'] != xfer_target) {
925                 if (item['call_number.id'] in vols_to_move) {
926                     copies_to_move[item['call_number.id']].push(item.id);
927                 } else {
928                     vols_to_move[item['call_number.id']] = {
929                         label       : item['call_number.label'],
930                         label_class : item['call_number.label_class'],
931                         record      : item['call_number.record.id'],
932                         prefix      : item['call_number.prefix.id'],
933                         suffix      : item['call_number.suffix.id']
934                     };
935                     copies_to_move[item['call_number.id']] = new Array;
936                     copies_to_move[item['call_number.id']].push(item.id);
937                 }
938             }
939         });
940
941         var promises = [];
942         angular.forEach(vols_to_move, function(vol, vol_id) {
943             promises.push(egCore.net.request(
944                 'open-ils.cat',
945                 'open-ils.cat.call_number.find_or_create',
946                 egCore.auth.token(),
947                 vol.label,
948                 vol.record,
949                 xfer_target,
950                 vol.prefix,
951                 vol.suffix,
952                 vol.label_class
953             ).then(function(resp) {
954                 var evt = egCore.evt.parse(resp);
955                 if (evt) return;
956                 return egCore.net.request(
957                     'open-ils.cat',
958                     'open-ils.cat.transfer_copies_to_volume',
959                     egCore.auth.token(),
960                     resp.acn_id,
961                     copies_to_move[vol_id]
962                 );
963             }));
964         });
965
966         $q.all(promises)
967         .then(
968             function() {
969                 angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
970             }
971         );
972     }
973
974     service.transferItems = function (items){
975         var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_vol');
976         var copy_ids = service.gatherSelectedHoldingsIds(items);
977         if (xfer_target && copy_ids.length > 0) {
978             egCore.net.request(
979                 'open-ils.cat',
980                 'open-ils.cat.transfer_copies_to_volume',
981                 egCore.auth.token(),
982                 xfer_target,
983                 copy_ids
984             ).then(
985                 function(resp) { // oncomplete
986                     var evt = egCore.evt.parse(resp);
987                     if (evt) {
988                         egConfirmDialog.open(
989                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
990                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
991                             {'evt_desc': evt}
992                         ).result.then(function() {
993                             egCore.net.request(
994                                 'open-ils.cat',
995                                 'open-ils.cat.transfer_copies_to_volume.override',
996                                 egCore.auth.token(),
997                                 xfer_target,
998                                 copy_ids,
999                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
1000                             );
1001                         }).then(function() {
1002                             angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
1003                         });
1004                     } else {
1005                         angular.forEach(items, function(cp){service.add_barcode_to_list(cp.barcode)});
1006                     }
1007
1008                 },
1009                 null, // onerror
1010                 null // onprogress
1011             );
1012         }
1013     }
1014
1015     service.mark_missing_pieces = function(copy,outer_scope) {
1016         var b = copy.barcode();
1017         var t = egCore.idl.toHash(copy.call_number()).record.title;
1018         egConfirmDialog.open(
1019             egCore.strings.CONFIRM_MARK_MISSING_TITLE,
1020             egCore.strings.CONFIRM_MARK_MISSING_BODY,
1021             { barcode : b, title : t }
1022         ).result.then(function() {
1023
1024             // kick off mark missing
1025             return egCore.net.request(
1026                 'open-ils.circ',
1027                 'open-ils.circ.mark_item_missing_pieces',
1028                 egCore.auth.token(), copy.id()
1029             )
1030
1031         }).then(function(resp) {
1032             var evt = egCore.evt.parse(resp); // should always produce event
1033
1034             if (evt.textcode == 'ACTION_CIRCULATION_NOT_FOUND') {
1035                 return egAlertDialog.open(
1036                     egCore.strings.CIRC_NOT_FOUND, {barcode : copy.barcode()});
1037             }
1038
1039             var payload = evt.payload;
1040
1041             // TODO: open copy editor inline?  new tab?
1042
1043             // print the missing pieces slip
1044             var promise = $q.when();
1045             if (payload.slip) {
1046                 // wait for completion, since it may spawn a confirm dialog
1047                 promise = egCore.print.print({
1048                     context : 'receipt',
1049                     content_type : 'text/html',
1050                     content : payload.slip.template_output().data()
1051                 });
1052             }
1053
1054             if (payload.letter) {
1055                 outer_scope.letter = payload.letter.template_output().data();
1056             }
1057
1058             // apply patron penalty
1059             if (payload.circ) {
1060                 promise.then(function() {
1061                     egCirc.create_penalty(payload.circ.usr())
1062                 });
1063             }
1064
1065         });
1066     }
1067
1068     service.print_spine_labels = function(copy_ids){
1069         egCore.net.request(
1070             'open-ils.actor',
1071             'open-ils.actor.anon_cache.set_value',
1072             null, 'print-labels-these-copies', {
1073                 copies : copy_ids
1074             }
1075         ).then(function(key) {
1076             if (key) {
1077                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
1078                 $timeout(function() { $window.open(url, '_blank') });
1079             } else {
1080                 alert('Could not create anonymous cache key!');
1081             }
1082         });
1083     }
1084
1085     service.show_in_catalog = function(copy_list){
1086         angular.forEach(copy_list, function(copy){
1087             window.open('/eg2/staff/catalog/record/'+copy['call_number.record.id'], '_blank')
1088         });
1089     }
1090
1091     return service;
1092 }])
1093 .filter('string_pick', function() { return function(i){ return arguments[i] || ''; }; })
1094