]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
LP#1701001: add search-to-hold to catalog app
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / catalog / app.js
1 /**
2  * TPAC Frame App
3  *
4  * currently, this app doesn't use routes for each sub-ui, because 
5  * reloading the catalog each time is sloooow.  better so far to 
6  * swap out divs w/ ng-if / ng-show / ng-hide as needed.
7  *
8  */
9
10 angular.module('egCatalogApp', ['ui.bootstrap','ngRoute','ngLocationUpdate','egCoreMod','egGridMod', 'egMarcMod', 'egUserMod', 'egHoldingsMod', 'ngToast','egPatronSearchMod'])
11
12 .config(['ngToastProvider', function(ngToastProvider) {
13   ngToastProvider.configure({
14     verticalPosition: 'bottom',
15     animation: 'fade'
16   });
17 }])
18
19 .config(function($routeProvider, $locationProvider, $compileProvider) {
20     $locationProvider.html5Mode(true);
21     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
22
23     var resolver = {delay : ['egCore','egStartup','egUser', function(egCore, egStartup, egUser) {
24         egCore.env.classLoaders.aous = function() {
25             return egCore.org.settings([
26                 'cat.marc_control_number_identifier'
27             ]).then(function(settings) {
28                 // local settings are cached within egOrg.  Caching them
29                 // again in egEnv just simplifies the syntax for access.
30                 egCore.env.aous = settings;
31             });
32         }
33         egCore.env.loadClasses.push('aous');
34         return egStartup.go()
35     }]};
36
37     $routeProvider.when('/cat/catalog/index', {
38         templateUrl: './cat/catalog/t_catalog',
39         controller: 'CatalogCtrl',
40         resolve : resolver
41     });
42
43     // Jump directly to the results page.  Any URL parameter 
44     // supported by the embedded catalog is supported here.
45     $routeProvider.when('/cat/catalog/results', {
46         templateUrl: './cat/catalog/t_catalog',
47         controller: 'CatalogCtrl',
48         resolve : resolver
49     });
50
51     $routeProvider.when('/cat/catalog/retrieve_by_id', {
52         templateUrl: './cat/catalog/t_retrieve_by_id',
53         controller: 'CatalogRecordRetrieve',
54         resolve : resolver
55     });
56
57     $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
58         templateUrl: './cat/catalog/t_retrieve_by_tcn',
59         controller: 'CatalogRecordRetrieve',
60         resolve : resolver
61     });
62
63     $routeProvider.when('/cat/catalog/new_bib', {
64         templateUrl: './cat/catalog/t_new_bib',
65         controller: 'NewBibCtrl',
66         resolve : resolver
67     });
68
69     // create some catalog page-specific mappings
70     $routeProvider.when('/cat/catalog/record/:record_id', {
71         templateUrl: './cat/catalog/t_catalog',
72         controller: 'CatalogCtrl',
73         resolve : resolver
74     });
75
76     // create some catalog page-specific mappings
77     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
78         templateUrl: './cat/catalog/t_catalog',
79         controller: 'CatalogCtrl',
80         resolve : resolver
81     });
82
83     $routeProvider.when('/cat/catalog/batchEdit', {
84         templateUrl: './cat/catalog/t_batchedit',
85         controller: 'BatchEditCtrl',
86         resolve : resolver
87     });
88
89     $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
90         templateUrl: './cat/catalog/t_batchedit',
91         controller: 'BatchEditCtrl',
92         resolve : resolver
93     });
94
95     $routeProvider.when('/cat/catalog/vandelay', {
96         templateUrl: './cat/catalog/t_vandelay',
97         controller: 'VandelayCtrl',
98         resolve : resolver
99     });
100
101     $routeProvider.when('/cat/catalog/verifyURLs', {
102         templateUrl: './cat/catalog/t_verifyurls',
103         controller: 'URLVerifyCtrl',
104         resolve : resolver
105     });
106
107     $routeProvider.when('/cat/catalog/manageAuthorities', {
108         templateUrl: './cat/catalog/t_manageauthorities',
109         controller: 'ManageAuthoritiesCtrl',
110         resolve : resolver
111     });
112
113     $routeProvider.when('/cat/catalog/authority/:authority_id/marc_edit', {
114         templateUrl: './cat/catalog/t_authority',
115         controller: 'AuthorityCtrl',
116         resolve : resolver
117     });
118
119     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
120 })
121
122
123 /**
124  * */
125 .controller('CatalogRecordRetrieve',
126        ['$scope','$routeParams','$location','$q','egCore',
127 function($scope , $routeParams , $location , $q , egCore ) {
128
129     $scope.focusMe = true;
130
131     // jump to the patron checkout UI
132     function loadRecord(record_id) {
133         $location
134         .path('/cat/catalog/record/' + record_id);
135     }
136
137     $scope.submitId = function(args) {
138         $scope.recordNotFound = null;
139         if (!args.record_id) return;
140
141         // blur so next time it's set to true it will re-apply select()
142         $scope.selectMe = false;
143
144         return loadRecord(args.record_id);
145     }
146
147     $scope.submitTCN = function(args) {
148         $scope.recordNotFound = null;
149         $scope.moreRecordsFound = null;
150         if (!args.record_tcn) return;
151
152         // blur so next time it's set to true it will re-apply select()
153         $scope.selectMe = false;
154
155         // lookup TCN
156         egCore.net.request(
157             'open-ils.search',
158             'open-ils.search.biblio.tcn',
159             args.record_tcn)
160
161         .then(function(resp) { // get_barcodes
162
163             if (evt = egCore.evt.parse(resp)) {
164                 alert(evt); // FIXME
165                 return;
166             }
167
168             if (!resp.count) {
169                 $scope.recordNotFound = args.record_tcn;
170                 $scope.selectMe = true;
171                 return;
172             }
173
174             if (resp.count > 1) {
175                 $scope.moreRecordsFound = args.record_tcn;
176                 $scope.selectMe = true;
177                 return;
178             }
179
180             var record_id = resp.ids[0];
181             return loadRecord(record_id);
182         });
183     }
184
185 }])
186
187 .controller('NewBibCtrl',
188        ['$scope','$routeParams','$location','$window','$q','egCore',
189         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
190 function($scope , $routeParams , $location , $window , $q , egCore) {
191
192     $scope.have_template = false;
193     $scope.marc_template = '';
194     $scope.stop_unload = false;
195     $scope.template_list = [];
196     $scope.template_name = '';
197     $scope.new_bib_id = 0;
198
199     egCore.net.request(
200         'open-ils.cat',
201         'open-ils.cat.marc_template.types.retrieve'
202     ).then(function(resp) {
203         angular.forEach(resp, function(name) {
204             $scope.template_list.push(name);
205         });
206         $scope.template_list.sort();
207     });
208     $scope.template_name = egCore.hatch.getSessionItem('eg.cat.last_bib_marc_template');
209     if (!$scope.template_name) {
210         egCore.hatch.getItem('cat.default_bib_marc_template').then(function(template) {
211             $scope.template_name = template;
212         });
213     }
214
215     $scope.loadTemplate = function() {
216         if ($scope.template_name) {
217             egCore.net.request(
218                 'open-ils.cat',
219                 'open-ils.cat.biblio.marc_template.retrieve',
220                 $scope.template_name
221             ).then(function(template) {
222                 $scope.marc_template = template;
223                 $scope.have_template = true;
224                 egCore.hatch.setSessionItem('eg.cat.last_bib_marc_template', $scope.template_name);
225             });
226         }
227     }
228
229     $scope.setDefaultTemplate = function() {
230         var hatch_key = "cat.default_bib_marc_template";
231         if ($scope.template_name) {
232             egCore.hatch.setItem(hatch_key, $scope.template_name);
233         } else {
234             egCore.hatch.removeItem(hatch_key);
235         }
236     }
237
238     $scope.$watch('new_bib_id', function(newVal, oldVal) {
239         if (newVal) {
240             $location.path('/cat/catalog/record/' + $scope.new_bib_id);
241         }
242     });
243     
244
245 }])
246 .controller('CatalogCtrl',
247        ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc','egConfirmDialog','ngToast',
248         'egGridDataProvider','egHoldGridActions','egProgressDialog','$timeout','$uibModal','holdingsSvc','egUser','conjoinedSvc',
249         '$cookies',
250 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc , egConfirmDialog , ngToast ,
251          egGridDataProvider , egHoldGridActions , egProgressDialog , $timeout , $uibModal , holdingsSvc , egUser , conjoinedSvc,
252          $cookies
253 ) {
254
255     var holdingsSvcInst = new holdingsSvc();
256
257     // set record ID on page load if available...
258     $scope.record_id = $routeParams.record_id;
259     $scope.summary_pane_record;
260
261     if ($routeParams.record_id) $scope.from_route = true;
262     else $scope.from_route = false;
263
264     // set search and preferred library cookies
265     egCore.hatch.getItem('eg.search.search_lib').then(function(val) {
266         $cookies.put('eg_search_lib', val, { path : '/' });
267     });
268     egCore.hatch.getItem('eg.search.pref_lib').then(function(val) {
269         $cookies.put('eg_pref_lib', val, { path : '/' });
270     });
271
272     // will hold a ref to the opac iframe
273     $scope.opac_iframe = null;
274     $scope.parts_iframe = null;
275
276     $scope.search_result_index = 1;
277     $scope.search_result_hit_count = 1;
278
279     $scope.$watch(
280         'opac_iframe.dom.contentWindow.search_result_index',
281         function (n,o) {
282             if (!isNaN(parseInt(n)))
283                 $scope.search_result_index = n + 1;
284         }
285     );
286
287     $scope.$watch(
288         'opac_iframe.dom.contentWindow.search_result_hit_count',
289         function (n,o) {
290             if (!isNaN(parseInt(n)))
291                 $scope.search_result_hit_count = n;
292         }
293     );
294
295     $scope.in_opac_call = false;
296     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
297         if ($scope.opac_iframe) {
298             if (force_opac_tab) $scope.record_tab = 'catalog';
299             $scope.in_opac_call = true;
300             $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
301             if (opac_frame_function == 'rdetailBackToResults') {
302                 $location.update_path('/cat/catalog/index');
303             }
304         }
305     }
306
307     $scope.add_to_record_bucket = function() {
308         var recId = $scope.record_id;
309         return $uibModal.open({
310             templateUrl: './cat/catalog/t_add_to_bucket',
311             animation: true,
312             size: 'md',
313             controller:
314                    ['$scope','$uibModalInstance',
315             function($scope , $uibModalInstance) {
316
317                 $scope.bucket_id = 0;
318                 $scope.newBucketName = '';
319                 $scope.allBuckets = [];
320                 egCore.net.request(
321                     'open-ils.actor',
322                     'open-ils.actor.container.retrieve_by_class.authoritative',
323                     egCore.auth.token(), egCore.auth.user().id(),
324                     'biblio', 'staff_client'
325                 ).then(function(buckets) { $scope.allBuckets = buckets; });
326
327                 $scope.add_to_bucket = function() {
328                     var item = new egCore.idl.cbrebi();
329                     item.bucket($scope.bucket_id);
330                     item.target_biblio_record_entry(recId);
331                     egCore.net.request(
332                         'open-ils.actor',
333                         'open-ils.actor.container.item.create',
334                         egCore.auth.token(), 'biblio', item
335                     ).then(function(resp) {
336                         $uibModalInstance.close();
337                     });
338                 }
339
340                 $scope.add_to_new_bucket = function() {
341                     var bucket = new egCore.idl.cbreb();
342                     bucket.owner(egCore.auth.user().id());
343                     bucket.name($scope.newBucketName);
344                     bucket.description('');
345                     bucket.btype('staff_client');
346
347                     egCore.net.request(
348                         'open-ils.actor',
349                         'open-ils.actor.container.create',
350                         egCore.auth.token(), 'biblio', bucket
351                     ).then(function(bucket) {
352                         $scope.bucket_id = bucket;
353                         $scope.add_to_bucket();
354                     });
355                 }
356
357                 $scope.cancel = function() {
358                     $uibModalInstance.dismiss();
359                 }
360             }]
361         });
362     }
363
364     $scope.current_overlay_target     = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record');
365     $scope.current_voltransfer_target = egCore.hatch.getLocalItem('eg.cat.marked_volume_transfer_record');
366     $scope.current_conjoined_target   = egCore.hatch.getLocalItem('eg.cat.marked_conjoined_record');
367
368     $scope.markConjoined = function () {
369         $scope.current_conjoined_target = $scope.record_id;
370         egCore.hatch.setLocalItem('eg.cat.marked_conjoined_record',$scope.record_id);
371         ngToast.create(egCore.strings.MARK_CONJ_TARGET);
372     };
373
374     $scope.markVolTransfer = function () {
375         ngToast.create(egCore.strings.MARK_VOL_TARGET);
376         $scope.current_voltransfer_target = $scope.record_id;
377         egCore.hatch.setLocalItem('eg.cat.marked_volume_transfer_record',$scope.record_id);
378     };
379
380     $scope.markOverlay = function () {
381         $scope.current_overlay_target = $scope.record_id;
382         egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.record_id);
383         ngToast.create(egCore.strings.MARK_OVERLAY_TARGET);
384     };
385
386     $scope.clearRecordMarks = function () {
387         $scope.current_overlay_target     = null;
388         $scope.current_voltransfer_target = null;
389         $scope.current_conjoined_target   = null;
390         $scope.current_hold_transfer_dest = null;
391         egCore.hatch.removeLocalItem('eg.cat.marked_volume_transfer_record');
392         egCore.hatch.removeLocalItem('eg.cat.marked_conjoined_record');
393         egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
394         egCore.hatch.removeLocalItem('eg.circ.hold.title_transfer_target');
395     }
396
397     $scope.stop_unload = false;
398     $scope.$watch('stop_unload',
399         function(newVal, oldVal) {
400             if (newVal && newVal != oldVal && $scope.opac_iframe) {
401                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
402                     return 'There is unsaved data in this record.'
403                 });
404             } else {
405                 if ($scope.opac_iframe)
406                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
407             }
408         }
409     );
410
411     // Set the "last bib" cookie, if we have that
412     if ($scope.record_id)
413         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
414
415     $scope.refresh_record_callback = function (record_id) {
416         egCore.pcrud.retrieve('bre', record_id, {
417             flesh : 1,
418             flesh_fields : {
419                 bre : ['simple_record','creator','editor']
420             }
421         }).then(function(rec) {
422             rec.owner(egCore.org.get(rec.owner()));
423             $scope.summary_pane_record = rec;
424         });
425
426         return record_id;
427     }
428
429     patron_search_dialog = function() {
430         return $uibModal.open({
431             templateUrl: './share/t_patron_selector',
432             size: 'lg',
433             animation: true,
434             controller:
435                    ['$scope','$uibModalInstance','$controller',
436             function($scope , $uibModalInstance , $controller) {
437                 angular.extend(this, $controller('BasePatronSearchCtrl', {$scope : $scope}));
438                 $scope.clearForm();
439                 $scope.need_one_selected = function() {
440                     var items = $scope.gridControls.selectedItems();
441                     return (items.length == 1) ? false : true
442                 }
443                 $scope.ok = function() {
444                     var items = $scope.gridControls.selectedItems();
445                     if (items.length == 1) {
446                         $uibModalInstance.close(items[0].card().barcode());
447                     } else {
448                         $uibModalInstance.close()
449                     }
450                 }
451                 $scope.cancel = function($event) {
452                     $uibModalInstance.dismiss();
453                     $event.preventDefault();
454                 }
455             }]
456         });
457     }
458
459     // also set it when the iframe changes to a new record
460     $scope.handle_page = function(url) {
461
462         if (!url || url == 'about:blank') {
463             // nothing loaded.  If we already have a record ID, leave it.
464             return;
465         }
466
467         var match = url.match(/\/+opac\/+record\/+(\d+)/);
468         if (match) {
469             $scope.record_id = match[1];
470             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
471             $scope.holdings_record_id_changed($scope.record_id);
472             conjoinedSvc.fetch($scope.record_id).then(function(){
473                 $scope.conjoinedGridDataProvider.refresh();
474             });
475             egHolds.fetch_holds(hold_ids).then($scope.hold_grid_data_provider.refresh);
476             init_parts_url();
477             $location.update_path('/cat/catalog/record/' + $scope.record_id);
478         } else {
479             delete $scope.record_id;
480             $scope.from_route = false;
481         }
482
483         // child scope is executing this function, so our digest doesn't fire ... thus,
484         $scope.$apply();
485
486         if (!$scope.in_opac_call) {
487             if ($scope.record_id) {
488                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
489                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
490             } else {
491                 tab = $routeParams.record_tab || 'catalog';
492             }
493             $scope.set_record_tab(tab);
494         } else {
495             $scope.in_opac_call = false;
496         }
497
498         if ($scope.opac_iframe && $location.path().match(/cat\/catalog/)) {
499             var doc = $scope.opac_iframe.dom.contentWindow.document;
500             $(doc).find('#hold_usr_search').show();
501             $(doc).find('#hold_usr_search').on('click', function() {
502                 patron_search_dialog().result.then(function(barc) {
503                     $(doc).find('#hold_usr_input').val(barc);
504                     $(doc).find('#hold_usr_input').change();
505                 });
506             })
507         }
508
509     }
510
511     // xulG catalog handlers
512     $scope.handlers = { }
513
514     // ------------------------------------------------------------------
515     // Conjoined items
516
517     $scope.conjoinedGridControls = {};
518     $scope.conjoinedGridDataProvider = egGridDataProvider.instance({
519         get : function(offset, count) {
520             return this.arrayNotifier(conjoinedSvc.items, offset, count);
521         }
522     });
523
524     $scope.changeConjoinedType = function () {
525         var peers = egCore.idl.Clone($scope.conjoinedGridControls.selectedItems());
526         angular.forEach(peers, function (p) {
527             p.target_copy(p.target_copy().id());
528             p.peer_type(p.peer_type().id());
529         });
530
531         var conjoinedGridDataProviderRef = $scope.conjoinedGridDataProvider;
532
533         return $uibModal.open({
534             templateUrl: './cat/catalog/t_conjoined_selector',
535             animation: true,
536             controller:
537                    ['$scope','$uibModalInstance',
538             function($scope , $uibModalInstance) {
539                 $scope.update = true;
540
541                 $scope.peer_type = null;
542                 $scope.peer_type_list = [];
543                 conjoinedSvc.get_peer_types().then(function(list){
544                     $scope.peer_type_list = list;
545                 });
546     
547                 $scope.ok = function(type) {
548                     var promises = [];
549     
550                     angular.forEach(peers, function (p) {
551                         p.ischanged(1);
552                         p.peer_type(type);
553                         promises.push(egCore.pcrud.update(p));
554                     });
555     
556                     return $q.all(promises)
557                         .then(function(){$uibModalInstance.close()})
558                         .then(function(){return conjoinedSvc.fetch()})
559                         .then(function(){conjoinedGridDataProviderRef.refresh()});
560                 }
561     
562                 $scope.cancel = function($event) {
563                     $uibModalInstance.dismiss();
564                     $event.preventDefault();
565                 }
566             }]
567         });
568         
569     }
570
571     $scope.refreshConjoined = function () {
572         conjoinedSvc.fetch($scope.record_id)
573         .then(function(){$scope.conjoinedGridDataProvider.refresh();});
574     }
575
576     $scope.deleteSelectedConjoined = function () {
577         var peers = $scope.conjoinedGridControls.selectedItems();
578
579         if (peers.length > 0) {
580             egConfirmDialog.open(
581                 egCore.strings.CONFIRM_DELETE_PEERS,
582                 egCore.strings.CONFIRM_DELETE_PEERS_MESSAGE,
583                 {peers : peers.length}
584             ).result.then(function() {
585                 angular.forEach(peers, function (p) {
586                     p.isdeleted(1);
587                 });
588
589                 egCore.pcrud.remove(peers).then(function() {
590                     return conjoinedSvc.fetch();
591                 }).then(function() {
592                     $scope.conjoinedGridDataProvider.refresh();
593                 });
594             });
595         }
596     }
597     if ($scope.record_id)
598         conjoinedSvc.fetch($scope.record_id);
599
600     // ------------------------------------------------------------------
601     // Holdings
602
603     $scope.holdingsGridControls = {
604         activateItem : function (item) {
605             $scope.selectedHoldingsVolCopyEdit();
606         }
607     };
608     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
609         get : function(offset, count) {
610             return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
611         }
612     });
613
614     $scope.add_copies_to_bucket = function() {
615         var copy_list = gatherSelectedHoldingsIds();
616         if (copy_list.length == 0) return;
617
618         return $uibModal.open({
619             templateUrl: './cat/catalog/t_add_to_bucket',
620             animation: true,
621             size: 'md',
622             controller:
623                    ['$scope','$uibModalInstance',
624             function($scope , $uibModalInstance) {
625
626                 $scope.bucket_id = 0;
627                 $scope.newBucketName = '';
628                 $scope.allBuckets = [];
629
630                 egCore.net.request(
631                     'open-ils.actor',
632                     'open-ils.actor.container.retrieve_by_class.authoritative',
633                     egCore.auth.token(), egCore.auth.user().id(),
634                     'copy', 'staff_client'
635                 ).then(function(buckets) { $scope.allBuckets = buckets; });
636
637                 $scope.add_to_bucket = function() {
638                     var promises = [];
639                     angular.forEach(copy_list, function (cp) {
640                         var item = new egCore.idl.ccbi()
641                         item.bucket($scope.bucket_id);
642                         item.target_copy(cp);
643                         promises.push(
644                             egCore.net.request(
645                                 'open-ils.actor',
646                                 'open-ils.actor.container.item.create',
647                                 egCore.auth.token(), 'copy', item
648                             )
649                         );
650
651                         return $q.all(promises).then(function() {
652                             $uibModalInstance.close();
653                         });
654                     });
655                 }
656
657                 $scope.add_to_new_bucket = function() {
658                     var bucket = new egCore.idl.ccb();
659                     bucket.owner(egCore.auth.user().id());
660                     bucket.name($scope.newBucketName);
661                     bucket.description('');
662                     bucket.btype('staff_client');
663
664                     return egCore.net.request(
665                         'open-ils.actor',
666                         'open-ils.actor.container.create',
667                         egCore.auth.token(), 'copy', bucket
668                     ).then(function(bucket) {
669                         $scope.bucket_id = bucket;
670                         $scope.add_to_bucket();
671                     });
672                 }
673
674                 $scope.cancel = function() {
675                     $uibModalInstance.dismiss();
676                 }
677             }]
678         });
679     }
680
681     // TODO: refactor common code between cat/catalog/app.js and cat/item/app.js 
682
683     $scope.need_one_selected = function() {
684         var items = $scope.holdingsGridControls.selectedItems();
685         if (items.length == 1) return false;
686         return true;
687     };
688
689     $scope.make_copies_bookable = function() {
690
691         var copies_by_record = {};
692         var record_list = [];
693         angular.forEach(
694             $scope.holdingsGridControls.selectedItems(),
695             function (item) {
696                 var record_id = item['call_number.record.id'];
697                 if (typeof copies_by_record[ record_id ] == 'undefined') {
698                     copies_by_record[ record_id ] = [];
699                     record_list.push( record_id );
700                 }
701                 copies_by_record[ record_id ].push(item.id);
702             }
703         );
704
705         var promises = [];
706         var combined_results = [];
707         angular.forEach(record_list, function(record_id) {
708             promises.push(
709                 egCore.net.request(
710                     'open-ils.booking',
711                     'open-ils.booking.resources.create_from_copies',
712                     egCore.auth.token(),
713                     copies_by_record[record_id]
714                 ).then(function(results) {
715                     if (results && results['brsrc']) {
716                         combined_results = combined_results.concat(results['brsrc']);
717                     }
718                 })
719             );
720         });
721
722         $q.all(promises).then(function() {
723             if (combined_results.length > 0) {
724                 $uibModal.open({
725                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
726                     animation: true,
727                     size: 'md',
728                     controller:
729                            ['$scope','$location','egCore','$uibModalInstance',
730                     function($scope , $location , egCore , $uibModalInstance) {
731
732                         $scope.funcs = {
733                             ses : egCore.auth.token(),
734                             resultant_brsrc : combined_results.map(function(o) { return o[0]; })
735                         }
736
737                         var booking_path = '/eg/conify/global/booking/resource';
738
739                         $scope.booking_admin_url =
740                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
741                     }]
742                 });
743             }
744         });
745     }
746
747     $scope.book_copies_now = function() {
748         var copies_by_record = {};
749         var record_list = [];
750         angular.forEach(
751             $scope.holdingsGridControls.selectedItems(),
752             function (item) {
753                 var record_id = item['call_number.record.id'];
754                 if (typeof copies_by_record[ record_id ] == 'undefined') {
755                     copies_by_record[ record_id ] = [];
756                     record_list.push( record_id );
757                 }
758                 copies_by_record[ record_id ].push(item.id);
759             }
760         );
761
762         var promises = [];
763         var combined_brt = [];
764         var combined_brsrc = [];
765         angular.forEach(record_list, function(record_id) {
766             promises.push(
767                 egCore.net.request(
768                     'open-ils.booking',
769                     'open-ils.booking.resources.create_from_copies',
770                     egCore.auth.token(),
771                     copies_by_record[record_id]
772                 ).then(function(results) {
773                     if (results && results['brt']) {
774                         combined_brt = combined_brt.concat(results['brt']);
775                     }
776                     if (results && results['brsrc']) {
777                         combined_brsrc = combined_brsrc.concat(results['brsrc']);
778                     }
779                 })
780             );
781         });
782
783         $q.all(promises).then(function() {
784             if (combined_brt.length > 0 || combined_brsrc.length > 0) {
785                 $uibModal.open({
786                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
787                     animation: true,
788                     size: 'md',
789                     controller:
790                            ['$scope','$location','egCore','$uibModalInstance',
791                     function($scope , $location , egCore , $uibModalInstance) {
792
793                         $scope.funcs = {
794                             ses : egCore.auth.token(),
795                             bresv_interface_opts : {
796                                 booking_results : {
797                                      brt : combined_brt
798                                     ,brsrc : combined_brsrc
799                                 }
800                             }
801                         }
802
803                         var booking_path = '/eg/booking/reservation';
804
805                         $scope.booking_admin_url =
806                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
807
808                     }]
809                 });
810             }
811         });
812     }
813
814
815     $scope.requestItems = function() {
816         var copy_list = gatherSelectedHoldingsIds();
817         if (copy_list.length == 0) return;
818
819         return $uibModal.open({
820             templateUrl: './cat/catalog/t_request_items',
821             animation: true,
822             controller:
823                    ['$scope','$uibModalInstance',
824             function($scope , $uibModalInstance) {
825                 $scope.user = null;
826                 $scope.first_user_fetch = true;
827
828                 $scope.hold_data = {
829                     hold_type : 'C',
830                     copy_list : copy_list,
831                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
832                     user      : egCore.auth.user().id()
833                 };
834
835                 egUser.get( $scope.hold_data.user ).then(function(u) {
836                     $scope.user = u;
837                     $scope.barcode = u.card().barcode();
838                     $scope.user_name = egUser.format_name(u);
839                     $scope.hold_data.user = u.id();
840                 });
841
842                 $scope.user_name = '';
843                 $scope.barcode = '';
844                 $scope.$watch('barcode', function (n) {
845                     if (!$scope.first_user_fetch) {
846                         egUser.getByBarcode(n).then(function(u) {
847                             $scope.user = u;
848                             $scope.user_name = egUser.format_name(u);
849                             $scope.hold_data.user = u.id();
850                         }, function() {
851                             $scope.user = null;
852                             $scope.user_name = '';
853                             delete $scope.hold_data.user;
854                         });
855                     }
856                     $scope.first_user_fetch = false;
857                 });
858
859                 $scope.ok = function(h) {
860                     var args = {
861                         patronid  : h.user,
862                         hold_type : h.hold_type,
863                         pickup_lib: h.pickup_lib.id(),
864                         depth     : 0
865                     };
866
867                     egCore.net.request(
868                         'open-ils.circ',
869                         'open-ils.circ.holds.test_and_create.batch.override',
870                         egCore.auth.token(), args, h.copy_list
871                     );
872
873                     $uibModalInstance.close();
874                 }
875
876                 $scope.cancel = function($event) {
877                     $uibModalInstance.dismiss();
878                     $event.preventDefault();
879                 }
880             }]
881         });
882     }
883
884     $scope.view_place_orders = function() {
885         if (!$scope.record_id) return;
886         var url = egCore.env.basePath + 'acq/legacy/lineitem/related/' + $scope.record_id + '?target=bib';
887         $timeout(function() { $window.open(url, '_blank') });
888     }
889
890     $scope.replaceBarcodes = function() {
891         var copy_list = gatherSelectedRawCopies();
892         if (copy_list.length == 0) return;
893
894         var holdingsGridDataProviderRef = $scope.holdingsGridDataProvider;
895
896         angular.forEach(copy_list, function (cp) {
897             $uibModal.open({
898                 templateUrl: './cat/share/t_replace_barcode',
899                 animation: true,
900                 controller:
901                            ['$scope','$uibModalInstance',
902                     function($scope , $uibModalInstance) {
903                         $scope.isModal = true;
904                         $scope.focusBarcode = false;
905                         $scope.focusBarcode2 = true;
906                         $scope.barcode1 = cp.barcode();
907
908                         $scope.updateBarcode = function() {
909                             $scope.copyNotFound = false;
910                             $scope.updateOK = false;
911                 
912                             egCore.pcrud.search('acp',
913                                 {deleted : 'f', barcode : $scope.barcode1})
914                             .then(function(copy) {
915                 
916                                 if (!copy) {
917                                     $scope.focusBarcode = true;
918                                     $scope.copyNotFound = true;
919                                     return;
920                                 }
921                 
922                                 $scope.copyId = copy.id();
923                                 copy.barcode($scope.barcode2);
924                 
925                                 egCore.pcrud.update(copy).then(function(stat) {
926                                     $scope.updateOK = stat;
927                                     $scope.focusBarcode = true;
928                                     holdingsSvc.fetchAgain().then(function (){
929                                         holdingsGridDataProviderRef.refresh();
930                                     });
931                                 });
932
933                             });
934                             $uibModalInstance.close();
935                         }
936
937                         $scope.cancel = function($event) {
938                             $uibModalInstance.dismiss();
939                             $event.preventDefault();
940                         }
941                     }
942                 ]
943             });
944         });
945     }
946
947     // refresh the list of holdings when the record_id is changed.
948     $scope.holdings_record_id_changed = function(id) {
949         if ($scope.record_id != id) $scope.record_id = id;
950         console.log('record id changed to ' + id + ', loading new holdings');
951         holdingsSvcInst.fetch({
952             rid : $scope.record_id,
953             org : $scope.holdings_ou,
954             copy: $scope.holdings_show_copies,
955             vol : $scope.holdings_show_vols,
956             empty: $scope.holdings_show_empty
957         }).then(function() {
958             $scope.holdingsGridDataProvider.refresh();
959         });
960     }
961
962     // refresh the list of holdings when the filter lib is changed.
963     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
964     $scope.holdings_ou_changed = function(org) {
965         $scope.holdings_ou = org;
966         holdingsSvcInst.fetch({
967             rid : $scope.record_id,
968             org : $scope.holdings_ou,
969             copy: $scope.holdings_show_copies,
970             vol : $scope.holdings_show_vols,
971             empty: $scope.holdings_show_empty
972         }).then(function() {
973             $scope.holdingsGridDataProvider.refresh();
974         });
975     }
976
977     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
978         $scope[cb] = newVal;
979         egCore.hatch.setItem('cat.' + cb, newVal);
980         if (!norefresh) holdingsSvcInst.fetch({
981             rid : $scope.record_id,
982             org : $scope.holdings_ou,
983             copy: $scope.holdings_show_copies,
984             vol : $scope.holdings_show_vols,
985             empty: $scope.holdings_show_empty
986         }).then(function() {
987             $scope.holdingsGridDataProvider.refresh();
988         });
989     }
990
991     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
992         if (typeof x ==  'undefined') x = true;
993         $scope.holdings_cb_changed('holdings_show_vols',x,true);
994         $('#holdings_show_vols').prop('checked', x);
995     }).then(function(){
996         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
997             if (typeof x ==  'undefined') x = true;
998             $scope.holdings_cb_changed('holdings_show_copies',x,true);
999             $('#holdings_show_copies').prop('checked', x);
1000         }).then(function(){
1001             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
1002                 if (typeof x ==  'undefined') x = true;
1003                 $scope.holdings_cb_changed('holdings_show_empty',x);
1004                 $('#holdings_show_empty').prop('checked', x);
1005             })
1006         })
1007     });
1008
1009     $scope.vols_not_shown = function () {
1010         return !$scope.holdings_show_vols;
1011     }
1012
1013     $scope.copies_not_shown = function () {
1014         return !$scope.holdings_show_copies;
1015     }
1016
1017     $scope.holdings_checkbox_handler = function (item) {
1018         $scope.holdings_cb_changed(item.checkbox,item.checked);
1019     }
1020
1021     function gatherSelectedHoldingsIds () {
1022         var cp_id_list = [];
1023         angular.forEach(
1024             $scope.holdingsGridControls.selectedItems(),
1025             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
1026         );
1027         return cp_id_list;
1028     }
1029
1030     function gatherSelectedRawCopies () {
1031         var cp_list = [];
1032         angular.forEach(
1033             $scope.holdingsGridControls.selectedItems(),
1034             function (item) { if (item.raw) cp_list = cp_list.concat(item.raw) }
1035         );
1036         return cp_list;
1037     }
1038
1039     function gatherSelectedEmptyVolumeIds () {
1040         var cn_id_list = [];
1041         angular.forEach(
1042             $scope.holdingsGridControls.selectedItems(),
1043             function (item) {
1044                 if (item.copy_count == 0)
1045                     cn_id_list.push(item.call_number.id)
1046             }
1047         );
1048         return cn_id_list;
1049     }
1050
1051     function gatherSelectedVolumeIds () {
1052         var cn_id_list = [];
1053         angular.forEach(
1054             $scope.holdingsGridControls.selectedItems(),
1055             function (item) {
1056                 if (cn_id_list.indexOf(item.call_number.id) == -1)
1057                     cn_id_list.push(item.call_number.id)
1058             }
1059         );
1060         return cn_id_list;
1061     }
1062
1063     $scope.selectedHoldingsDelete = function (vols, copies) {
1064
1065         var cnHash = {};
1066         var perCnCopies = {};
1067
1068         var cn_count = 0;
1069         var cp_count = 0;
1070
1071         angular.forEach(
1072             $scope.holdingsGridControls.selectedItems(),
1073             function (item) {
1074                 if (vols && item.raw_call_number) {
1075                     cnHash[item.call_number.id] = egCore.idl.Clone(item.raw_call_number);
1076                     cnHash[item.call_number.id].isdeleted(1);
1077                     cn_count++;
1078                 } else if (copies) {
1079                     angular.forEach(egCore.idl.Clone(item.raw), function (cp) {
1080                         cp.isdeleted(1);
1081                         cp_count++;
1082                         var cn_id = cp.call_number().id();
1083                         if (!cnHash[cn_id]) {
1084                             cnHash[cn_id] = cp.call_number();
1085                             perCnCopies[cn_id] = [cp];
1086                         } else {
1087                             perCnCopies[cn_id].push(cp);
1088                         }
1089                         cp.call_number(cn_id); // prevent loops in JSON-ification
1090                     });
1091
1092                 }
1093             }
1094         );
1095
1096         angular.forEach(perCnCopies, function (v, k) {
1097             if (vols) {
1098                 cnHash[k].isdeleted(1);
1099                 cn_count++;
1100             }
1101             cnHash[k].copies(v);
1102         });
1103
1104         cnList = [];
1105         angular.forEach(cnHash, function (v, k) {
1106             cnList.push(v);
1107         });
1108
1109         if (cnList.length == 0) return;
1110
1111         var flags = {};
1112         if (vols && copies) flags.force_delete_copies = 1;
1113
1114         egConfirmDialog.open(
1115             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
1116             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
1117             {copies : cp_count, volumes : cn_count}
1118         ).result.then(function() {
1119             egCore.net.request(
1120                 'open-ils.cat',
1121                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
1122                 egCore.auth.token(), cnList, 1, flags
1123             ).then(function(update_count) {
1124                 holdingsSvcInst.fetchAgain().then(function() {
1125                     $scope.holdingsGridDataProvider.refresh();
1126                 });
1127             });
1128         });
1129     }
1130     $scope.selectedHoldingsCopyDelete = function () { $scope.selectedHoldingsDelete(false,true) }
1131     $scope.selectedHoldingsVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,true) }
1132     $scope.selectedHoldingsEmptyVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,false) }
1133
1134     spawnHoldingsAdd = function (vols,copies){
1135         var raw = [];
1136         if (copies) { // just a copy on existing volumes
1137             angular.forEach(gatherSelectedVolumeIds(), function (v) {
1138                 raw.push( {callnumber : v} );
1139             });
1140         } else if (vols) {
1141             if (typeof $scope.holdingsGridControls.selectedItems == "function" &&
1142                 $scope.holdingsGridControls.selectedItems().length > 0) {
1143                 angular.forEach($scope.holdingsGridControls.selectedItems(),
1144                     function (item) {
1145                         raw.push({
1146                             owner : item.owner_id,
1147                             label : item.call_number.label
1148                         });
1149                     });
1150             } else {
1151                 raw.push({
1152                     owner : egCore.auth.user().ws_ou()
1153                 });
1154             }
1155         }
1156
1157         if (raw.length == 0) raw.push({});
1158
1159         egCore.net.request(
1160             'open-ils.actor',
1161             'open-ils.actor.anon_cache.set_value',
1162             null, 'edit-these-copies', {
1163                 record_id: $scope.record_id,
1164                 raw: raw,
1165                 hide_vols : false,
1166                 hide_copies : false
1167             }
1168         ).then(function(key) {
1169             if (key) {
1170                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1171                 $timeout(function() { $window.open(url, '_blank') });
1172             } else {
1173                 alert('Could not create anonymous cache key!');
1174             }
1175         });
1176     }
1177     $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(true,false) }
1178     $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(false,true) }
1179
1180     spawnHoldingsEdit = function (hide_vols,hide_copies){
1181         egCore.net.request(
1182             'open-ils.actor',
1183             'open-ils.actor.anon_cache.set_value',
1184             null, 'edit-these-copies', {
1185                 record_id: $scope.record_id,
1186                 copies: gatherSelectedHoldingsIds(),
1187                 raw: gatherSelectedEmptyVolumeIds().map(
1188                     function(v){ return { callnumber : v } }
1189                 ),
1190                 hide_vols : hide_vols,
1191                 hide_copies : hide_copies
1192             }
1193         ).then(function(key) {
1194             if (key) {
1195                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1196                 $timeout(function() { $window.open(url, '_blank') });
1197             } else {
1198                 alert('Could not create anonymous cache key!');
1199             }
1200         });
1201     }
1202     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
1203     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
1204     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
1205
1206     $scope.selectedHoldingsItemStatus = function (){
1207         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
1208         $timeout(function() { $window.open(url, '_blank') });
1209     }
1210
1211     $scope.markVolAsItemTarget = function() {
1212         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed
1213             egCore.hatch.setLocalItem(
1214                 'eg.cat.item_transfer_target',
1215                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
1216             );
1217             ngToast.create(egCore.strings.MARK_ITEM_TARGET);
1218         }
1219     }
1220
1221     $scope.markLibAsVolTarget = function() {
1222         return $uibModal.open({
1223             templateUrl: './cat/catalog/t_choose_vol_target_lib',
1224             animation: true,
1225             controller:
1226                    ['$scope','$uibModalInstance',
1227             function($scope , $uibModalInstance) {
1228
1229                 var orgId = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target') || 1;
1230                 $scope.org = egCore.org.get(orgId);
1231                 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
1232                 $scope.ok = function(org) {
1233                     egCore.hatch.setLocalItem(
1234                         'eg.cat.volume_transfer_target',
1235                         org.id()
1236                     );
1237                     $uibModalInstance.close();
1238                 }
1239                 $scope.cancel = function($event) {
1240                     $uibModalInstance.dismiss();
1241                     $event.preventDefault();
1242                 }
1243             }]
1244         });
1245     }
1246     $scope.markLibFromSelectedAsVolTarget = function() {
1247         egCore.hatch.setLocalItem(
1248             'eg.cat.volume_transfer_target',
1249             $scope.holdingsGridControls.selectedItems()[0].owner_id
1250         );
1251         ngToast.create(egCore.strings.MARK_VOL_TARGET);
1252     }
1253
1254     $scope.selectedHoldingsItemStatusDetail = function (){
1255         angular.forEach(
1256             gatherSelectedHoldingsIds(),
1257             function (cid) {
1258                 var url = egCore.env.basePath +
1259                           'cat/item/' + cid;
1260                 $timeout(function() { $window.open(url, '_blank') });
1261             }
1262         );
1263     }
1264
1265     $scope.transferVolumesToRecord = function (){
1266         var target_record = egCore.hatch.getLocalItem('eg.cat.marked_volume_transfer_record');
1267         if (!target_record) return;
1268         if ($scope.record_id == target_record) return;
1269         var items = $scope.holdingsGridControls.selectedItems();
1270         if (!items.length) return;
1271
1272         var vols_to_move   = {};
1273         angular.forEach(items, function(item) {
1274             if (!(item.call_number.owning_lib in vols_to_move)) {
1275                 vols_to_move[item.call_number.owning_lib] = new Array;
1276             }
1277             vols_to_move[item.call_number.owning_lib].push(item.call_number.id);
1278         });
1279
1280         var promises = [];        
1281         angular.forEach(vols_to_move, function(vols, owning_lib) {
1282             promises.push(egCore.net.request(
1283                 'open-ils.cat',
1284                 'open-ils.cat.asset.volume.batch.transfer.override',
1285                 egCore.auth.token(), {
1286                     docid   : target_record,
1287                     lib     : owning_lib,
1288                     volumes : vols
1289                 }
1290             ));
1291         });
1292         $q.all(promises).then(function(success) {
1293             if (success) {
1294                 ngToast.create(egCore.strings.VOLS_TRANSFERED);
1295                 holdingsSvcInst.fetchAgain().then(function() {
1296                     $scope.holdingsGridDataProvider.refresh();
1297                 });
1298             } else {
1299                 alert('Could not transfer volumes!');
1300             }
1301         });
1302     }
1303
1304     function transferVolumes(new_record){
1305         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
1306
1307         if (xfer_target) {
1308             egCore.net.request(
1309                 'open-ils.cat',
1310                 'open-ils.cat.asset.volume.batch.transfer.override',
1311                 egCore.auth.token(), {
1312                     docid   : (new_record ? new_record : $scope.record_id),
1313                     lib     : xfer_target,
1314                     volumes : gatherSelectedVolumeIds()
1315                 }
1316             ).then(function(success) {
1317                 if (success) {
1318                     ngToast.create(egCore.strings.VOLS_TRANSFERED);
1319                     holdingsSvcInst.fetchAgain().then(function() {
1320                         $scope.holdingsGridDataProvider.refresh();
1321                     });
1322                 } else {
1323                     alert('Could not transfer volumes!');
1324                 }
1325             });
1326         }
1327         
1328     }
1329
1330     $scope.transferVolumesToLibrary = function() {
1331         transferVolumes();
1332     }
1333
1334     $scope.transferVolumesToRecordAndLibrary = function() {
1335         var target_record = egCore.hatch.getLocalItem('eg.cat.marked_volume_transfer_record');
1336         if (!target_record) return;
1337         transferVolumes(target_record);
1338     }
1339
1340     // this "transfers" selected copies to a new owning library,
1341     // auto-creating volumes and deleting unused volumes as required.
1342     $scope.changeItemOwningLib = function() {
1343         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
1344         var items = $scope.holdingsGridControls.selectedItems();
1345         if (!xfer_target || !items.length) {
1346             return;
1347         }
1348         var vols_to_move   = {};
1349         var copies_to_move = {};
1350         angular.forEach(items, function(item) {
1351             if (item.call_number.owning_lib != xfer_target) {
1352                 if (item.call_number.id in vols_to_move) {
1353                     copies_to_move[item.call_number.id].push(item.id);
1354                 } else {
1355                     vols_to_move[item.call_number.id] = item.call_number;
1356                     copies_to_move[item.call_number.id] = new Array;
1357                     copies_to_move[item.call_number.id].push(item.id);
1358                 }
1359             }
1360         });
1361     
1362         var promises = [];
1363         angular.forEach(vols_to_move, function(vol) {
1364             promises.push(egCore.net.request(
1365                 'open-ils.cat',
1366                 'open-ils.cat.call_number.find_or_create',
1367                 egCore.auth.token(),
1368                 vol.label,
1369                 vol.record,
1370                 xfer_target,
1371                 vol.prefix.id,
1372                 vol.suffix.id,
1373                 vol.label_class
1374             ).then(function(resp) {
1375                 var evt = egCore.evt.parse(resp);
1376                 if (evt) return;
1377                 return egCore.net.request(
1378                     'open-ils.cat',
1379                     'open-ils.cat.transfer_copies_to_volume',
1380                     egCore.auth.token(),
1381                     resp.acn_id,
1382                     copies_to_move[vol.id]
1383                 );
1384             }));
1385         });
1386         $q.all(promises).then(function() {
1387             ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1388             holdingsSvcInst.fetchAgain().then(function() {
1389                 $scope.holdingsGridDataProvider.refresh();
1390             });
1391         });
1392     }
1393
1394     $scope.transferItems = function (){
1395         var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
1396         var copy_ids = gatherSelectedHoldingsIds();
1397         if (xfer_target && copy_ids.length > 0) {
1398             egCore.net.request(
1399                 'open-ils.cat',
1400                 'open-ils.cat.transfer_copies_to_volume',
1401                 egCore.auth.token(),
1402                 xfer_target,
1403                 copy_ids
1404             ).then(
1405                 function(resp) { // oncomplete
1406                     var evt = egCore.evt.parse(resp);
1407                     if (evt) {
1408                         egConfirmDialog.open(
1409                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
1410                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
1411                             {'evt_desc': evt.desc}
1412                         ).result.then(function() {
1413                             egCore.net.request(
1414                                 'open-ils.cat',
1415                                 'open-ils.cat.transfer_copies_to_volume.override',
1416                                 egCore.auth.token(),
1417                                 xfer_target,
1418                                 copy_ids,
1419                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
1420                             ).then(function(resp) {
1421                                 holdingsSvcInst.fetchAgain().then(function() {
1422                                     $scope.holdingsGridDataProvider.refresh();
1423                                 });
1424                             });
1425                         });
1426                     } else {
1427                         ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1428                         holdingsSvcInst.fetchAgain().then(function() {
1429                             $scope.holdingsGridDataProvider.refresh();
1430                         });
1431                     }
1432                 },
1433                 null, // onerror
1434                 null // onprogress
1435             )
1436         }
1437     }
1438
1439     $scope.selectedHoldingsItemStatusTgrEvt = function (){
1440         angular.forEach(
1441             gatherSelectedHoldingsIds(),
1442             function (cid) {
1443                 var url = egCore.env.basePath +
1444                           'cat/item/' + cid + '/triggered_events';
1445                 $timeout(function() { $window.open(url, '_blank') });
1446             }
1447         );
1448     }
1449
1450     $scope.selectedHoldingsItemStatusHolds = function (){
1451         angular.forEach(
1452             gatherSelectedHoldingsIds(),
1453             function (cid) {
1454                 var url = egCore.env.basePath +
1455                           'cat/item/' + cid + '/holds';
1456                 $timeout(function() { $window.open(url, '_blank') });
1457             }
1458         );
1459     }
1460
1461     $scope.selectedHoldingsPrintLabels = function() {
1462         egCore.net.request(
1463             'open-ils.actor',
1464             'open-ils.actor.anon_cache.set_value',
1465             null, 'print-labels-these-copies', {
1466                 copies : gatherSelectedHoldingsIds()
1467             }
1468         ).then(function(key) {
1469             if (key) {
1470                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
1471                 $timeout(function() { $window.open(url, '_blank') });
1472             } else {
1473                 alert('Could not create anonymous cache key!');
1474             }
1475         });
1476     }
1477
1478     $scope.selectedHoldingsDamaged = function () {
1479         egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
1480             holdingsSvcInst.fetchAgain().then(function() {
1481                 $scope.holdingsGridDataProvider.refresh();
1482             });
1483         });
1484     }
1485
1486     $scope.selectedHoldingsMissing = function () {
1487         egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
1488             holdingsSvcInst.fetchAgain().then(function() {
1489                 $scope.holdingsGridDataProvider.refresh();
1490             });
1491         });
1492     }
1493
1494     $scope.attach_to_peer_bib = function() {
1495         var copy_list = gatherSelectedHoldingsIds();
1496         if (copy_list.length == 0) return;
1497
1498         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
1499             if (!target_record) return;
1500
1501             return $uibModal.open({
1502                 templateUrl: './cat/catalog/t_conjoined_selector',
1503                 animation: true,
1504                 controller:
1505                        ['$scope','$uibModalInstance',
1506                 function($scope , $uibModalInstance) {
1507                     $scope.update = false;
1508
1509                     $scope.peer_type = null;
1510                     $scope.peer_type_list = [];
1511                     conjoinedSvc.get_peer_types().then(function(list){
1512                         $scope.peer_type_list = list;
1513                     });
1514     
1515                     $scope.ok = function(type) {
1516                         var promises = [];
1517     
1518                         angular.forEach(copy_list, function (cp) {
1519                             var n = new egCore.idl.bpbcm();
1520                             n.isnew(true);
1521                             n.peer_record(target_record);
1522                             n.target_copy(cp);
1523                             n.peer_type(type);
1524                             promises.push(egCore.pcrud.create(n));
1525                         });
1526     
1527                         return $q.all(promises).then(function(){$uibModalInstance.close()});
1528                     }
1529     
1530                     $scope.cancel = function($event) {
1531                         $uibModalInstance.dismiss();
1532                         $event.preventDefault();
1533                     }
1534                 }]
1535             });
1536         });
1537     }
1538
1539
1540     // ------------------------------------------------------------------
1541     // Holds 
1542     var provider = egGridDataProvider.instance({});
1543     $scope.hold_grid_data_provider = provider;
1544     $scope.grid_actions = egHoldGridActions;
1545     $scope.grid_actions.refresh = function () { provider.refresh() };
1546     $scope.hold_grid_controls = {};
1547
1548     var hold_ids = []; // current list of holds
1549     function fetchHolds(offset, count) {
1550         var ids = hold_ids.slice(offset, offset + count);
1551
1552         return egHolds.fetch_holds(ids).then(null, null,
1553             function(hold_data) { 
1554                 return hold_data;
1555             }
1556         );
1557     }
1558
1559     provider.get = function(offset, count) {
1560         if ($scope.record_tab != 'holds') return $q.when();
1561         var deferred = $q.defer();
1562         hold_ids = []; // no caching ATM
1563
1564         // open a determinate progress dialog, max value set below.
1565         egProgressDialog.open({max : 1, value : 0});
1566
1567         // fetch the IDs
1568         egCore.net.request(
1569             'open-ils.circ',
1570             'open-ils.circ.holds.retrieve_all_from_title',
1571             egCore.auth.token(), $scope.record_id, 
1572             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
1573         ).then(
1574             function(hold_data) {
1575                 hold_ids = []; // clear the list of ids, hack to avoid dups
1576                 // TODO: fix the underlying problem, which is that
1577                 // this gets called twice when switching to the holds
1578                 // tab; once explicitly, and once via the change handler
1579                 // on the OU selector
1580                 angular.forEach(hold_data, function(list, type) {
1581                     hold_ids = hold_ids.concat(list);
1582                 });
1583
1584                 // Set the max value of the progress bar to the lesser of
1585                 // the total number of holds to fetch or the page size
1586                 // of the grid.
1587                 egProgressDialog.update(
1588                     {max : Math.min(hold_ids.length, count)});
1589
1590                 var holds_fetched = 0;
1591                 fetchHolds(offset, count)
1592                 .then(deferred.resolve, null, 
1593                     function(hold_data) {
1594                         holds_fetched++;
1595                         deferred.notify(hold_data);
1596                         egProgressDialog.increment();
1597                     }
1598                 )['finally'](egProgressDialog.close);
1599             }
1600         );
1601
1602         return deferred.promise;
1603     }
1604
1605     $scope.detail_view = function(action, user_data, items) {
1606         if (h = items[0]) {
1607             $scope.detail_hold_id = h.hold.id();
1608         }
1609     }
1610
1611     $scope.list_view = function(items) {
1612          $scope.detail_hold_id = null;
1613     }
1614
1615     // refresh the list of record holds when the pickup lib is changed.
1616     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
1617     $scope.pickup_ou_changed = function(org) {
1618         $scope.pickup_ou = org;
1619         provider.refresh();
1620     }
1621
1622     $scope.print_holds = function() {
1623         var holds = [];
1624         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
1625             holds.push({
1626                 hold : egCore.idl.toHash(item.hold),
1627                 patron_last : item.patron_last,
1628                 patron_alias : item.patron_alias,
1629                 patron_barcode : item.patron_barcode,
1630                 copy : egCore.idl.toHash(item.copy),
1631                 volume : egCore.idl.toHash(item.volume),
1632                 title : item.mvr.title(),
1633                 author : item.mvr.author()
1634             });
1635         });
1636
1637         egCore.print.print({
1638             context : 'receipt', 
1639             template : 'holds_for_bib', 
1640             scope : {holds : holds}
1641         });
1642     }
1643
1644     $scope.current_hold_transfer_dest = egCore.hatch.getLocalItem ('eg.circ.hold.title_transfer_target');
1645
1646     $scope.mark_hold_transfer_dest = function() {
1647         $scope.current_hold_transfer_dest = $scope.record_id;
1648         egCore.hatch.setLocalItem(
1649             'eg.circ.hold.title_transfer_target', $scope.record_id);
1650         ngToast.create(egCore.strings.HOLD_TRANSFER_DEST_MARKED);
1651     }
1652
1653     // UI presents this option as "all holds"
1654     $scope.transfer_holds_to_marked = function() {
1655         var hold_ids = $scope.hold_grid_controls.allItems().map(
1656             function(hold_data) {return hold_data.hold.id()});
1657         egHolds.transfer_to_marked_title(hold_ids);
1658     }
1659
1660     // ------------------------------------------------------------------
1661     // Initialize the selected tab
1662
1663     function init_cat_url() {
1664         // Set the initial catalog URL.  This only happens once.
1665         // The URL is otherwise generated through user navigation.
1666         if ($scope.catalog_url) return; 
1667
1668         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
1669
1670         // A record ID in the path indicates a request for the record-
1671         // specific page.
1672         if ($routeParams.record_id) {
1673             url = url.replace(/advanced/, '/record/' + $scope.record_id);
1674         }
1675
1676         // Jumping directly to the results page by passing a search
1677         // query via the URL.  Copy all URL params to the iframe url.
1678         if ($location.path().match(/catalog\/results/)) {
1679             url = url.replace(/advanced/, '/results?');
1680             var first = true;
1681             angular.forEach($location.search(), function(val, key) {
1682                 if (!first) url += '&';
1683                 first = false;
1684                 url += encodeURIComponent(key) 
1685                     + '=' + encodeURIComponent(val);
1686             });
1687         }
1688
1689         // if we're displaying the advanced search form, select
1690         // whatever default pane the user has chosen via workstation
1691         // preference
1692         if (url.match(/\/opac\/advanced$/)) {
1693             var adv_pane = egCore.hatch.getLocalItem('eg.search.adv_pane');
1694             if (adv_pane) {
1695                 url += '?pane=' + encodeURIComponent(adv_pane);
1696             }
1697         }
1698
1699         $scope.catalog_url = url;
1700     }
1701
1702     function init_parts_url() {
1703         $scope.parts_url = $location
1704             .absUrl()
1705             .replace(
1706                 /\/staff.*/,
1707                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
1708             );
1709     }
1710
1711     $scope.set_record_tab = function(tab) {
1712         $scope.record_tab = tab;
1713
1714         switch(tab) {
1715
1716             case 'monoparts':
1717                 init_parts_url();
1718                 break;
1719
1720             case 'catalog':
1721                 init_cat_url();
1722                 break;
1723
1724             case 'holds':
1725                 $scope.detail_hold_record_id = $scope.record_id; 
1726                 // refresh the holds grid
1727                 provider.refresh();
1728
1729                 break;
1730         }
1731     }
1732
1733     $scope.set_default_record_tab = function() {
1734         egCore.hatch.setLocalItem(
1735             'eg.cat.default_record_tab', $scope.record_tab);
1736         $timeout(function(){$scope.default_tab = $scope.record_tab});
1737     }
1738
1739     var tab;
1740     if ($scope.record_id) {
1741         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
1742         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
1743
1744     } else {
1745         tab = $routeParams.record_tab || 'catalog';
1746     }
1747     $scope.set_record_tab(tab);
1748
1749 }])
1750
1751 .controller('AuthorityCtrl',
1752        ['$scope','$routeParams','$location','$window','$q','egCore',
1753 function($scope , $routeParams , $location , $window , $q , egCore) {
1754
1755     // set record ID on page load if available...
1756     $scope.authority_id = $routeParams.authority_id;
1757
1758     if ($routeParams.authority_id) $scope.from_route = true;
1759     else $scope.from_route = false;
1760
1761     $scope.stop_unload = false;
1762 }])
1763
1764 .controller('URLVerifyCtrl',
1765        ['$scope','$location',
1766 function($scope , $location) {
1767     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
1768 }])
1769
1770 .controller('VandelayCtrl',
1771        ['$scope','$location',
1772 function($scope , $location) {
1773     $scope.vandelay_url = $location.absUrl().replace(/\/staff\/cat\/catalog\/vandelay/, '/vandelay/vandelay');
1774 }])
1775
1776 .controller('ManageAuthoritiesCtrl',
1777        ['$scope','$location',
1778 function($scope , $location) {
1779     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
1780 }])
1781
1782 .controller('BatchEditCtrl',
1783        ['$scope','$location','$routeParams',
1784 function($scope , $location , $routeParams) {
1785     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
1786     if ($routeParams.container_type) {
1787         switch ($routeParams.container_type) {
1788             case 'bucket':
1789                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
1790                 break;
1791             case 'record':
1792                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
1793                 break;
1794         };
1795     }
1796 }])
1797
1798  
1799 .filter('boolText', function(){
1800     return function (v) {
1801         return v == 't';
1802     }
1803 })
1804
1805 .factory('conjoinedSvc', 
1806        ['egCore','$q',
1807 function(egCore , $q) {
1808
1809     var service = {
1810         items : [], // record search results
1811         index : 0, // search grid index
1812         rid : null
1813     };
1814
1815     service.flesh = {   
1816         flesh : 4, 
1817         flesh_fields : {
1818             bpbcm : ['target_copy','peer_type'],
1819             acp : ['call_number'],
1820             acn : ['record'],
1821             bre : ['simple_record']
1822         },
1823         // avoid fetching the MARC blob by specifying which
1824         // fields on the bre to select.  More may be needed.
1825         // note that fleshed fields are explicitly selected.
1826         select : { bre : ['id'] },
1827         order_by : { bpbcm : ['id'] },
1828     }
1829
1830     // resolved with the last received copy
1831     service.fetch = function(rid) {
1832         if (!rid && !service.rid) return $q.when();
1833
1834         if (rid) service.rid = rid;
1835         service.items = [];
1836         service.index = 0;
1837
1838         return egCore.pcrud.search(
1839             'bpbcm',
1840             {peer_record : service.rid},
1841             service.flesh,
1842             {atomic : true}
1843         ).then( function(list) { // finished
1844             service.items = list;
1845             return service.items;
1846         });
1847     }
1848
1849     // returns a promise resolved with the list of peer bib types
1850     service.get_peer_types = function() {
1851         if (egCore.env.bpt)
1852             return $q.when(egCore.env.bpt.list);
1853
1854         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
1855         .then(function(list) {
1856             egCore.env.absorbList(list, 'bpt');
1857             return list;
1858         });
1859     };
1860
1861     return service;
1862 }])
1863
1864