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