]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/volcopy/app.js
webstaff: Add a save-and-continue mode
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / volcopy / app.js
1 /**
2  * Vol/Copy Editor
3  */
4
5 angular.module('egVolCopy',
6     ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
7
8 .filter('boolText', function(){
9     return function (v) {
10         return v == 't';
11     }
12 })
13
14 .config(function($routeProvider, $locationProvider, $compileProvider) {
15     $locationProvider.html5Mode(true);
16     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
17
18     var resolver = {
19         delay : ['egStartup', function(egStartup) { return egStartup.go(); }]
20     };
21
22     $routeProvider.when('/cat/volcopy/:dataKey', {
23         templateUrl: './cat/volcopy/t_view',
24         controller: 'EditCtrl',
25         resolve : resolver
26     });
27
28 })
29
30 .factory('itemSvc', 
31        ['egCore','$q',
32 function(egCore , $q) {
33
34     var service = {
35         new_cp_id : 0,
36         new_cn_id : 0,
37         tree : {}, // holds lib->cn->copy hash stack
38         copies : [] // raw copy list
39     };
40
41     // returns a promise resolved with the list of circ mods
42     service.get_classifications = function() {
43         if (egCore.env.acnc)
44             return $q.when(egCore.env.acnc.list);
45
46         return egCore.pcrud.retrieveAll('acnc', null, {atomic : true})
47         .then(function(list) {
48             egCore.env.absorbList(list, 'acnc');
49             return list;
50         });
51     };
52
53     service.get_prefixes = function(org) {
54         return egCore.pcrud.search('acnp',
55             {owning_lib : egCore.org.fullPath(org, true)},
56             null, {atomic : true}
57         );
58
59     };
60
61     service.get_statcats = function(orgs) {
62         return egCore.pcrud.search('asc',
63             {owner : orgs},
64             { flesh : 1,
65               flesh_fields : {
66                 asc : ['owner','entries']
67               }
68             },
69             { atomic : true }
70         );
71     };
72
73     service.get_locations = function(orgs) {
74         return egCore.pcrud.search('acpl',
75             {owning_lib : orgs},
76             null, {atomic : true}
77         );
78     };
79
80     service.get_suffixes = function(org) {
81         return egCore.pcrud.search('acns',
82             {owning_lib : egCore.org.fullPath(org, true)},
83             null, {atomic : true}
84         );
85
86     };
87
88     service.get_statuses = function() {
89         if (egCore.env.ccs)
90             return $q.when(egCore.env.ccs.list);
91
92         return egCore.pcrud.retrieveAll('ccs', {}, {atomic : true}).then(
93             function(list) {
94                 egCore.env.absorbList(list, 'ccs');
95                 return list;
96             }
97         );
98
99     };
100
101     service.get_circ_mods = function() {
102         if (egCore.env.ccm)
103             return $q.when(egCore.env.ccm.list);
104
105         return egCore.pcrud.retrieveAll('ccm', {}, {atomic : true}).then(
106             function(list) {
107                 egCore.env.absorbList(list, 'ccm');
108                 return list;
109             }
110         );
111
112     };
113
114     service.get_circ_types = function() {
115         if (egCore.env.citm)
116             return $q.when(egCore.env.citm.list);
117
118         return egCore.pcrud.retrieveAll('citm', {}, {atomic : true}).then(
119             function(list) {
120                 egCore.env.absorbList(list, 'citm');
121                 return list;
122             }
123         );
124
125     };
126
127     service.get_age_protects = function() {
128         if (egCore.env.crahp)
129             return $q.when(egCore.env.crahp.list);
130
131         return egCore.pcrud.retrieveAll('crahp', {}, {atomic : true}).then(
132             function(list) {
133                 egCore.env.absorbList(list, 'crahp');
134                 return list;
135             }
136         );
137
138     };
139
140     service.bmp_parts = {};
141     service.get_parts = function(rec) {
142         if (service.bmp_parts[rec])
143             return $q.when(service.bmp_parts[rec]);
144
145         return egCore.pcrud.search('bmp',
146             {record : rec},
147             null, {atomic : true}
148         ).then(function(list) {
149             service.bmp_parts[rec] = list;
150             return list;
151         });
152
153     };
154
155     service.flesh = {   
156         flesh : 3, 
157         flesh_fields : {
158             acp : ['call_number','parts','stat_cat_entries', 'notes'],
159             acn : ['label_class','prefix','suffix']
160         }
161     }
162
163     service.addCopy = function (cp) {
164
165         if (!cp.parts()) cp.parts([]); // just in case...
166
167         var lib = cp.call_number().owning_lib();
168         var cn = cp.call_number().id();
169
170         if (!service.tree[lib]) service.tree[lib] = {};
171         if (!service.tree[lib][cn]) service.tree[lib][cn] = [];
172
173         service.tree[lib][cn].push(cp);
174         service.copies.push(cp);
175     }
176
177     service.fetchIds = function(idList) {
178         service.tree = {}; // clear the tree on fetch
179         service.copies = []; // clear the copy list on fetch
180         return egCore.pcrud.search('acp', { 'id' : idList }, service.flesh).then(null,null,
181             function(copy) {
182                 service.addCopy(copy);
183             }
184         );
185     }
186
187     return service;
188 }])
189
190 .directive("egVolCopyEdit", function () {
191     return {
192         restrict: 'E',
193         replace: true,
194         template:
195             '<div class="row">'+
196                 '<div class="col-xs-5">'+
197                     '<input id="{{callNumber.id()}}_{{copy.id()}}"'+
198                     ' eg-enter="nextBarcode(copy.id())" class="form-control"'+
199                     ' type="text" ng-model="barcode" ng-change="updateBarcode()"/>'+
200                 '</div>'+
201                 '<div class="col-xs-3"><input class="form-control" type="number" ng-model="copy_number" ng-change="updateCopyNo()"/></div>'+
202                 '<div class="col-xs-4"><eg-basic-combo-box list="parts" selected="part"></eg-basic-combo-box></div>'+
203             '</div>',
204
205         scope: { focusNext: "=", copy: "=", callNumber: "=", index: "@" },
206         controller : ['$scope','itemSvc','egCore',
207             function ( $scope , itemSvc , egCore ) {
208                 $scope.new_part_id = 0;
209
210                 $scope.nextBarcode = function (i) {
211                     $scope.focusNext(i);
212                 }
213
214                 $scope.updateBarcode = function () { $scope.copy.barcode($scope.barcode); $scope.copy.ischanged(1); };
215                 $scope.updateCopyNo = function () { $scope.copy.copy_number($scope.copy_number); $scope.copy.ischanged(1); };
216                 $scope.updatePart = function () {
217                     if ($scope.part) {
218                         var p = $scope.part_list.filter(function (x) {
219                             return x.label() == $scope.part
220                         });
221                         if (p.length > 0) { // preexisting part
222                             $scope.copy.parts(p)
223                         } else { // create one...
224                             var part = new egCore.idl.bmp();
225                             part.id( --$scope.new_part_id );
226                             part.isnew( true );
227                             part.label( $scope.part );
228                             part.record( $scope.callNumber.owning_lib() );
229                             $scope.copy.parts([part]);
230                             $scope.copy.ischanged(1);
231                         }
232                     } else {
233                         $scope.copy.parts([]);
234                     }
235                 }
236                 $scope.$watch('part', $scope.updatePart);
237
238                 $scope.barcode = $scope.copy.barcode();
239                 $scope.copy_number = $scope.copy.copy_number();
240
241                 if ($scope.copy.parts()) {
242                     $scope.part = $scope.copy.parts()[0];
243                     if ($scope.part) $scope.part = $scope.part.label();
244                 };
245
246                 $scope.parts = [];
247                 $scope.part_list = [];
248
249                 itemSvc.get_parts($scope.callNumber.record()).then(function(list){
250                     $scope.part_list = list;
251                     angular.forEach(list, function(p){ $scope.parts.push(p.label()) });
252                     $scope.parts = angular.copy($scope.parts);
253                 });
254
255             }
256         ]
257
258     }
259 })
260
261 .directive("egVolRow", function () {
262     return {
263         restrict: 'E',
264         replace: true,
265         transclude: true,
266         template:
267             '<div class="row">'+
268                 '<div class="col-xs-2">'+
269                     '<select class="form-control" ng-model="classification" ng-options="cl.name() for cl in classification_list track by idTracker(cl)"/>'+
270                 '</div>'+
271                 '<div class="col-xs-1">'+
272                     '<select class="form-control" ng-model="prefix" ng-change="updatePrefix()" ng-options="p.label() for p in prefix_list track by idTracker(p)"/>'+
273                 '</div>'+
274                 '<div class="col-xs-2"><input class="form-control" type="text" ng-change="updateLabel()" ng-model="label"/></div>'+
275                 '<div class="col-xs-1">'+
276                     '<select class="form-control" ng-model="suffix" ng-change="updateSuffix()" ng-options="s.label() for s in suffix_list track by idTracker(s)"/>'+
277                 '</div>'+
278                 '<div class="col-xs-1"><input class="form-control" type="number" ng-model="copy_count" min="{{orig_copy_count}}" ng-change="changeCPCount()"></div>'+
279                 '<div class="col-xs-5">'+
280                     '<eg-vol-copy-edit ng-repeat="cp in copies track by idTracker(cp)" focus-next="focusNextBarcode" copy="cp" call-number="callNumber"></eg-vol-copy-edit>'+
281                 '</div>'+
282             '</div>',
283
284         scope: {focusNext: "=", allcopies: "=", copies: "=" },
285         controller : ['$scope','itemSvc','egCore',
286             function ( $scope , itemSvc , egCore ) {
287                 $scope.callNumber =  $scope.copies[0].call_number();
288
289                 $scope.idTracker = function (x) { if (x) return x.id() };
290
291                 // XXX $() is not working! arg
292                 $scope.focusNextBarcode = function (i) {
293                     var n;
294                     var yep = false;
295                     angular.forEach($scope.copies, function (cp) {
296                         if (n) return;
297
298                         if (cp.id() == i) {
299                             yep = true;
300                             return;
301                         }
302
303                         if (yep) n = cp.id();
304                     });
305
306                     if (n) {
307                         var next = '#' + $scope.callNumber.id() + '_' + n;
308                         var el = $(next);
309                         if (el) el.focus()
310                     } else {
311                         $scope.focusNext($scope.callNumber.id())
312                     }
313                 }
314
315                 $scope.suffix_list = [];
316                 itemSvc.get_suffixes($scope.callNumber.owning_lib()).then(function(list){
317                     $scope.suffix_list = list;
318                 });
319                 $scope.updateSuffix = function () {
320                     angular.forEach($scope.copies, function(cp) {
321                         cp.call_number().suffix($scope.suffix);
322                         cp.call_number().ischanged(1);
323                     });
324                 }
325
326                 $scope.prefix_list = [];
327                 itemSvc.get_prefixes($scope.callNumber.owning_lib()).then(function(list){
328                     $scope.prefix_list = list;
329                 });
330                 $scope.updatePrefix = function () {
331                     angular.forEach($scope.copies, function(cp) {
332                         cp.call_number().prefix($scope.prefix);
333                         cp.call_number().ischanged(1);
334                     });
335                 }
336
337                 $scope.classification_list = [];
338                 itemSvc.get_classifications().then(function(list){
339                     $scope.classification_list = list;
340                 });
341                 $scope.updateClassification = function () {
342                     angular.forEach($scope.copies, function(cp) {
343                         cp.call_number().label_class($scope.classification);
344                         cp.call_number().ischanged(1);
345                     });
346                 }
347
348                 $scope.updateLabel = function () {
349                     angular.forEach($scope.copies, function(cp) {
350                         cp.call_number().label($scope.label);
351                         cp.call_number().ischanged(1);
352                     });
353                 }
354
355                 $scope.$watch('callNumber.prefix()', function (v) {
356                     if (typeof v != 'object') {
357                         $scope.prefix = $scope.prefix_list.filter(function (p) {
358                             return p.id() == v;
359                         })[0];
360                         $scope.callNumber.prefix($scope.prefix);
361                     }
362                 });
363
364                 $scope.$watch('callNumber.suffix()', function (v) {
365                     if (typeof v != 'object') {
366                         $scope.suffix = $scope.suffix_list.filter( function (s) {
367                             return s.id() == v;
368                         })[0];
369                         $scope.callNumber.suffix($scope.suffix);
370                     }
371                 });
372
373                 $scope.$watch('callNumber.label_class()', function (v) {
374                     if (typeof v != 'object') {
375                         $scope.classification = $scope.classification_list.filter(function (c) {
376                             return c.id() == v;
377                         })[0];
378                         $scope.callNumber.label_class($scope.classification);
379                     }
380                 });
381
382                 $scope.$watch('callNumber.label()', function (v) {
383                     $scope.label = v;
384                 });
385
386                 $scope.prefix = $scope.callNumber.prefix();
387                 $scope.suffix = $scope.callNumber.suffix();
388                 $scope.classification = $scope.callNumber.label_class();
389                 $scope.label = $scope.callNumber.label();
390
391                 $scope.copy_count = $scope.copies.length;
392                 $scope.orig_copy_count = $scope.copy_count;
393
394                 $scope.changeCPCount = function () {
395                     while ($scope.copy_count > $scope.copies.length) {
396                         var cp = new egCore.idl.acp();
397                         cp.id( --itemSvc.new_cp_id );
398                         cp.isnew( true );
399                         cp.circ_lib( $scope.lib );
400                         cp.call_number( $scope.callNumber );
401                         $scope.copies.push( cp );
402                         $scope.allcopies.push( cp );
403                     }
404
405                     if ($scope.copy_count >= $scope.orig_copy_count) {
406                         var how_many = $scope.copies.length - $scope.copy_count;
407                         if (how_many > 0) {
408                             var dead = $scope.copies.splice($scope.copy_count,how_many);
409                             $scope.callNumber.copies($scope.copies);
410
411                             // Trimming the global list is a bit more tricky
412                             angular.forEach( dead, function (d) {
413                                 angular.forEach( $scope.allcopies, function (l, i) { 
414                                     if (l === d) $scope.allcopies.splice(i,1);
415                                 });
416                             });
417                         }
418                     }
419                 }
420
421             }
422         ]
423
424     }
425 })
426
427 .directive("egVolEdit", function () {
428     return {
429         restrict: 'E',
430         replace: true,
431         template:
432             '<div class="row">'+
433                 '<div class="col-xs-1"><eg-org-selector selected="owning_lib" disableTest="cant_have_vols"></eg-org-selector></div>'+
434                 '<div class="col-xs-1"><input class="form-control" type="number" min="{{orig_cn_count}}" ng-model="cn_count" ng-change="changeCNCount()"/></div>'+
435                 '<div class="col-xs-10">'+
436                     '<eg-vol-row ng-repeat="(cn,copies) in struct | orderBy:cn track by cn" focus-next="focusNextFirst" copies="copies" allcopies="allcopies"></eg-vol-row>'+
437                 '</div>'+
438             '</div>',
439
440         scope: { focusNext: "=", allcopies: "=", struct: "=", lib: "@", record: "@" },
441         controller : ['$scope','itemSvc','egCore',
442             function ( $scope , itemSvc , egCore ) {
443                 $scope.first_cn = Object.keys($scope.struct)[0];
444                 $scope.full_cn = $scope.struct[$scope.first_cn][0].call_number();
445
446                 $scope.focusNextFirst = function(prev_cn) {
447                     var n;
448                     var yep = false;
449                     angular.forEach(Object.keys($scope.struct).sort(), function (cn) {
450                         console.log('checking '+cn);
451                         if (n) return;
452
453                         if (cn == prev_cn) {
454                             console.log('prev is '+cn);
455                             yep = true;
456                             return;
457                         }
458                         console.log('prev is not '+cn);
459
460                         if (yep) n = cn;
461                     });
462
463                     console.log('found '+n);
464                     if (n) {
465                         var next = '#' + n + '_' + $scope.struct[n][0].id();
466                         var el = $(next);
467                         if (el) el.focus()
468                     } else {
469                         $scope.focusNext($scope.lib);
470                     }
471                 }
472
473                 $scope.cn_count = Object.keys($scope.struct).length;
474                 $scope.orig_cn_count = $scope.cn_count;
475
476                 $scope.owning_lib = egCore.org.get($scope.lib);
477                 $scope.$watch('owning_lib', function (l) {
478                     angular.forEach( $scope.struct[$scope.first_cn], function (cp) {
479                         cp.call_number().owning_lib( $scope.owning_lib.id() );
480                     });
481                 });
482
483                 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
484
485                 $scope.$watch('cn_count', function (n) {
486                     var o = Object.keys($scope.struct).length;
487                     if (n > o) { // adding
488                         for (var i = o; o < n; o++) {
489                             var cn = new egCore.idl.acn();
490                             cn.id( --itemSvc.new_cn_id );
491                             cn.isnew( true );
492                             cn.owning_lib( $scope.owning_lib.id() );
493                             cn.record( $scope.full_cn.record() );
494
495                             var cp = new egCore.idl.acp();
496                             cp.id( --itemSvc.new_cp_id );
497                             cp.isnew( true );
498                             cp.circ_lib( $scope.owning_lib.id() );
499                             cp.call_number( cn );
500
501                             $scope.struct[cn.id()] = [cp];
502                             $scope.allcopies.push(cp);
503                         }
504                     } else if (n < o && n >= $scope.orig_cn_count) { // removing
505                         var how_many = o - n;
506                         var list = Object
507                                 .keys($scope.struct)
508                                 .sort(function(a, b){return parseInt(a)-parseInt(b)})
509                                 .filter(function(x){ return parseInt(x) <= 0 });
510                         for (var i = 0; i < how_many; i++) {
511                             // Trimming the global list is a bit more tricky
512                             console.log('trying to trim ' + i);
513                             angular.forEach($scope.struct[list[i]], function (d) {
514                                 angular.forEach( $scope.allcopies, function (l, j) { 
515                                     if (l === d) $scope.allcopies.splice(j,1);
516                                 });
517                             });
518                             delete $scope.struct[list[i]];
519                         }
520                     }
521                 });
522             }
523         ]
524
525     }
526 })
527
528 /**
529  * Edit controller!
530  */
531 .controller('EditCtrl', 
532        ['$scope','$q','$window','$routeParams','$location','$timeout','egCore','egNet','egGridDataProvider','itemSvc','$modal',
533 function($scope , $q , $window , $routeParams , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc , $modal) {
534
535     $scope.defaults = { // If defaults are not set at all, allow everything
536         statcats : true,
537         copy_notes : true,
538         attributes : {
539             status : true,
540             loan_duration : true,
541             fine_level : true,
542             cost : true,
543             alerts : true,
544             deposit : true,
545             deposit_amount : true,
546             opac_visible : true,
547             price : true,
548             circulate : true,
549             mint_condition : true,
550             circ_lib : true,
551             ref : true,
552             circ_modifier : true,
553             circ_as_type : true,
554             location : true,
555             holdable : true,
556             age_protect : true
557         }
558     };
559
560     $scope.saveDefaults = function () {
561         egCore.hatch.setItem('cat.copy.defaults', $scope.defaults);
562     }
563
564     $scope.fetchDefaults = function () {
565         egCore.hatch.getItem('cat.copy.defaults').then(function(t) {
566             if (t) {
567                 $scope.defaults = t;
568                 if (!$scope.batch) $scope.batch = {};
569                 $scope.batch.classification = $scope.defaults.classification;
570                 $scope.batch.prefix = $scope.defaults.prefix;
571                 $scope.batch.suffix = $scope.defaults.suffix;
572                 $scope.working.statcat_filter = $scope.defaults.statcat_filter;
573                 if ($scope.defaults.always_vols) $scope.show_vols = true;
574             }
575         });
576     }
577     $scope.fetchDefaults();
578
579     $scope.dirty = false;
580     $scope.$watch('dirty',
581         function(newVal, oldVal) {
582             if (newVal && newVal != oldVal) {
583                 $($window).on('beforeunload.edit', function(){
584                     return 'There is unsaved data!'
585                 });
586             } else {
587                 $($window).off('beforeunload.edit');
588             }
589         }
590     );
591
592     $scope.show_vols = true;
593     $scope.show_copies = true;
594
595     $scope.tracker = function (x,f) { if (x) return x[f]() };
596     $scope.idTracker = function (x) { if (x) return $scope.tracker(x,'id') };
597     $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
598
599     $scope.orgById = function (id) { return egCore.org.get(id) }
600     $scope.statusById = function (id) {
601         return $scope.status_list.filter( function (s) { return s.id() == id } )[0];
602     }
603     $scope.locationById = function (id) {
604         return $scope.location_cache[''+id];
605     }
606
607     $scope.workingToComplete = function () {
608         angular.forEach( $scope.workingGridControls.selectedItems(), function (c) {
609             angular.forEach( itemSvc.copies, function (w, i) {
610                 if (c === w)
611                     $scope.completed_copies = $scope.completed_copies.concat(itemSvc.copies.splice(i,1));
612             });
613         });
614     }
615
616     $scope.completeToWorking = function () {
617         angular.forEach( $scope.completedGridControls.selectedItems(), function (c) {
618             angular.forEach( $scope.completed_copies, function (w, i) {
619                 if (c === w)
620                     itemSvc.copies = itemSvc.copies.concat($scope.completed_copies.splice(i,1));
621             });
622         });
623     }
624
625     createSimpleUpdateWatcher = function (field) {
626         return $scope.$watch('working.' + field, function () {
627             var newval = $scope.working[field];
628
629             if (typeof newval != 'undefined') {
630                 if (angular.isObject(newval)) { // we'll use the pkey
631                     if (newval.id) newval = newval.id();
632                     else if (newval.code) newval = newval.code();
633                 }
634
635                 if (""+newval == "" || newval == null) {
636                     $scope.working[field] = undefined;
637                     newval = null;
638                 }
639
640                 if ($scope.workingGridControls && $scope.workingGridControls.selectedItems) {
641                     angular.forEach(
642                         $scope.workingGridControls.selectedItems(),
643                         function (cp) {
644                             if (cp[field]() !== newval) {
645                                 cp[field](newval);
646                                 cp.ischanged(1);
647                                 $scope.dirty = true;
648                             }
649                         }
650                     );
651                 }
652             }
653         });
654     }
655
656     $scope.working = {
657         statcats: {},
658         statcat_filter: undefined
659     };
660
661     $scope.statcatUpdate = function (id) {
662         var newval = $scope.working.statcats[id];
663
664         if (typeof newval != 'undefined') {
665             if (angular.isObject(newval)) { // we'll use the pkey
666                 newval = newval.id();
667             }
668     
669             if (""+newval == "" || newval == null) {
670                 $scope.working.statcats[id] = undefined;
671                 newval = null;
672             }
673     
674             if (!$scope.in_item_select && $scope.workingGridControls && $scope.workingGridControls.selectedItems) {
675                 angular.forEach(
676                     $scope.workingGridControls.selectedItems(),
677                     function (cp) {
678                         $scope.dirty = true;
679
680                         cp.stat_cat_entries(
681                             angular.forEach( cp.stat_cat_entries(), function (e) {
682                                 if (e.stat_cat() == id) { // mark deleted
683                                     e.isdeleted(1);
684                                 }
685                             })
686                         );
687     
688                         if (newval) {
689                             var e = new egCore.idl.ascecm();
690                             e.isnew( 1 );
691                             e.owning_copy( cp.id() );
692                             e.stat_cat( id );
693                             e.stat_cat_entry( newval );
694
695                             cp.stat_cat_entries(
696                                 cp.stat_cat_entries().concat([ e ])
697                             );
698
699                         }
700
701                         cp.stat_cat_entries( // trim out ephemeral deleted ones
702                             cp.stat_cat_entries().filter(function (e) {
703                                 if (Boolean(e.isnew())) {
704                                     if (Boolean(e.isdeleted())) {
705                                         return false;
706                                     }
707                                 }
708                                 return true;
709                             })
710                         );
711    
712                         cp.ischanged(1);
713                     }
714                 );
715             }
716         }
717     }
718
719     var dataKey = $routeParams.dataKey;
720     console.debug('dataKey: ' + dataKey);
721
722     if (dataKey && dataKey.length > 0) {
723
724         $scope.templates = {};
725         $scope.template_name = '';
726         $scope.template_name_list = [];
727
728         $scope.fetchTemplates = function () {
729             egCore.hatch.getItem('cat.copy.templates').then(function(t) {
730                 if (t) {
731                     $scope.templates = t;
732                     $scope.template_name_list = Object.keys(t);
733                 }
734             });
735         }
736         $scope.fetchTemplates();
737
738          $scope.applyTemplate = function (n) {
739             angular.forEach($scope.templates[n], function (v,k) {
740                 if (!angular.isObject(v)) {
741                     $scope.working[k] = angular.copy(v);
742                 } else {
743                     angular.forEach(v, function (sv,sk) {
744                         if (k == 'callnumber') {
745                             angular.forEach(v, function (cnv,cnk) {
746                                 $scope.batch[cnk] = cnv;
747                             });
748                             $scope.applyBatchCNValues();
749                         } else {
750                             $scope.working[k][sk] = angular.copy(sv);
751                             if (k == 'statcats') $scope.statcatUpdate(sk);
752                         }
753                     });
754                 }
755             });
756             $scope.template_name = '';
757         }
758
759         $scope.copytab = 'working';
760         $scope.tab = 'edit';
761         $scope.summaryRecord = null;
762         $scope.record_id = null;
763         $scope.data = {};
764         $scope.completed_copies = [];
765         $scope.location_orgs = [];
766         $scope.location_cache = {};
767         $scope.statcats = [];
768         if (!$scope.batch) $scope.batch = {};
769
770         $scope.applyBatchCNValues = function () {
771             if ($scope.data.tree) {
772                 angular.forEach($scope.data.tree, function(cn_hash) {
773                     angular.forEach(cn_hash, function(copies) {
774                         angular.forEach(copies, function(cp) {
775                             if (typeof $scope.batch.classification != 'undefined' && $scope.batch.classification != '')
776                                 cp.call_number().label_class($scope.batch.classification);
777                                 $scope.dirty = true;
778                             if (typeof $scope.batch.prefix != 'undefined' && $scope.batch.prefix != '')
779                                 cp.call_number().prefix($scope.batch.prefix);
780                                 $scope.dirty = true;
781                             if (typeof $scope.batch.label != 'undefined' && $scope.batch.label != '')
782                                 cp.call_number().label($scope.batch.label);
783                                 $scope.dirty = true;
784                             if (typeof $scope.batch.suffix != 'undefined' && $scope.batch.suffix != '')
785                                 cp.call_number().suffix($scope.batch.suffix);
786                                 $scope.dirty = true;
787                         });
788                     });
789                 });
790             }
791         }
792
793         $scope.clearWorking = function () {
794             angular.forEach($scope.working, function (v,k,o) {
795                 if (!angular.isObject(v)) {
796                     if (typeof v != 'undefined')
797                         $scope.working[k] = undefined;
798                 } else if (k != 'circ_lib') {
799                     angular.forEach(v, function (sv,sk) {
800                         if (typeof v != 'undefined')
801                             $scope.working[k][sk] = undefined;
802                     });
803                 }
804             });
805             $scope.working.circ_lib = undefined; // special
806         }
807
808         $scope.completedGridDataProvider = egGridDataProvider.instance({
809             get : function(offset, count) {
810                 //return provider.arrayNotifier(itemSvc.copies, offset, count);
811                 return this.arrayNotifier($scope.completed_copies, offset, count);
812             }
813         });
814
815         $scope.completedGridControls = {};
816
817         $scope.workingGridDataProvider = egGridDataProvider.instance({
818             get : function(offset, count) {
819                 //return provider.arrayNotifier(itemSvc.copies, offset, count);
820                 return this.arrayNotifier(itemSvc.copies, offset, count);
821             }
822         });
823
824         $scope.workingGridControls = {};
825
826         egNet.request(
827             'open-ils.actor',
828             'open-ils.actor.anon_cache.get_value',
829             dataKey, 'edit-these-copies'
830         ).then(function (data) {
831
832             if (data) {
833                 if (data.hide_vols && !$scope.defaults.always_vols) $scope.show_vols = false;
834                 if (data.hide_copies) $scope.show_copies = false;
835
836                 $scope.record_id = data.record_id;
837
838                 if (data.copies && data.copies.length)
839                     return itemSvc.fetchIds(data.copies);
840
841                 if (data.raw && data.raw.length) {
842                     $scope.dirty = true;
843
844                     /* data.raw data structure looks like this:
845                      * [{
846                      *      callnumber : $cn_id, // optional, to add a copy to a cn
847                      *      owner      : $org, // optional, defaults to ws_ou
848                      *      label      : $cn_label, // optional, to supply a label on a new cn
849                      *      barcode    : $cp_barcode // optional, to supply a barcode on a new cp
850                      * },...]
851                      * 
852                      * All can be left out and a completely empty vol/copy combo will be vivicated.
853                      */
854
855                     angular.forEach(
856                         data.raw,
857                         function (proto) {
858                             if (proto.callnumber) {
859                                 return egCore.pcrud.retrieve('acn', proto.callnumber)
860                                 .then(function(cn) {
861                                     var cp = new egCore.idl.acp();
862                                     cp.call_number( cn );
863                                     cp.id( --itemSvc.new_cp_id );
864                                     cp.isnew( true );
865                                     cp.circ_lib( $scope.record_id );
866                                     if (proto.barcode) cp.barcode( proto.barcode );
867
868                                     itemSvc.addCopy(cp)
869                                 });
870                             } else {
871                                 var cn = new egCore.idl.acn();
872                                 cn.id( --itemSvc.new_cn_id );
873                                 cn.isnew( true );
874                                 cn.owning_lib( proto.owner || egCore.auth.user().ws_ou() );
875                                 cn.record( $scope.record_id );
876                                 if (proto.label) cn.label( proto.label );
877
878                                 var cp = new egCore.idl.acp();
879                                 cp.call_number( cn );
880                                 cp.id( --itemSvc.new_cp_id );
881                                 cp.isnew( true );
882                                 cp.circ_lib( $scope.record_id );
883                                 if (proto.barcode) cp.barcode( proto.barcode );
884
885                                 itemSvc.addCopy(cp)
886                             }
887     
888                         }
889                     );
890
891                     return itemSvc.copies;
892                 }
893             }
894
895         }).then( function() {
896             $scope.data = itemSvc;
897             $scope.workingGridDataProvider.refresh();
898         });
899
900         $scope.focusNextFirst = function(prev_lib) {
901             var n;
902             var yep = false;
903             angular.forEach(Object.keys($scope.data.tree).sort(), function (lib) {
904                 console.log('checking lib '+lib);
905                 if (n) return;
906
907                 if (lib == prev_lib) {
908                     console.log('prev is '+lib);
909                     yep = true;
910                     return;
911                 }
912                 console.log('prev is not '+lib);
913
914                 if (yep) n = lib;
915             });
916
917             console.log('found '+n);
918             if (n) {
919                 var first_cn = Object.keys($scope.data.tree[n])[0];
920                 var next = '#' + first_cn + '_' + $scope.data.tree[n][first_cn][0].id();
921                 var el = $(next);
922                 if (el) el.focus()
923             } else {
924                 $scope.focusNext($scope.lib);
925             }
926         }
927
928         $scope.in_item_select = false;
929         $scope.afterItemSelect = function() { $scope.in_item_select = false };
930         $scope.handleItemSelect = function (item_list) {
931             if (item_list && item_list.length > 0) {
932                 $scope.in_item_select = true;
933
934                 angular.forEach(Object.keys($scope.defaults.attributes), function (attr) {
935
936                     var value_hash = {};
937                     angular.forEach(item_list, function (item) {
938                         if (item[attr]) {
939                             var v = item[attr]()
940                             if (angular.isObject(v)) {
941                                 if (v.id) v = v.id();
942                                 else if (v.code) v = v.code();
943                             }
944                             value_hash[v] = 1;
945                         }
946                     });
947
948                     if (Object.keys(value_hash).length == 1) {
949                         if (attr == 'circ_lib') {
950                             $scope.working[attr] = egCore.org.get(item_list[0][attr]());
951                         } else {
952                             $scope.working[attr] = item_list[0][attr]();
953                         }
954                     } else {
955                         $scope.working[attr] = undefined;
956                     }
957                 });
958
959                 angular.forEach($scope.statcats, function (sc) {
960
961                     var counter = -1;
962                     var value_hash = {};
963                     var none = false;
964                     angular.forEach(item_list, function (item) {
965                         if (item.stat_cat_entries()) {
966                             if (item.stat_cat_entries().length > 0) {
967                                 var right_sc = item.stat_cat_entries().filter(function (e) {
968                                     return e.stat_cat() == sc.id() && !Boolean(e.isdeleted());
969                                 });
970
971                                 if (right_sc.length > 0) {
972                                     value_hash[right_sc[0].stat_cat_entry()] = right_sc[0].stat_cat_entry();
973                                 } else {
974                                     none = true;
975                                 }
976                             }
977                         } else {
978                             none = true;
979                         }
980                     });
981
982                     if (!none && Object.keys(value_hash).length == 1) {
983                         $scope.working.statcats[sc.id()] = value_hash[Object.keys(value_hash)[0]];
984                     } else {
985                         $scope.working.statcats[sc.id()] = undefined;
986                     }
987                 });
988
989             } else {
990                 $scope.clearWorking();
991             }
992
993         }
994
995         $scope.$watch('data.copies.length', function () {
996             if ($scope.data.copies) {
997                 var base_orgs = $scope.data.copies.map(function(cp){
998                     return cp.circ_lib()
999                 }).concat(
1000                     $scope.data.copies.map(function(cp){
1001                         return cp.call_number().owning_lib()
1002                     })
1003                 ).concat(
1004                     [egCore.auth.user().ws_ou()]
1005                 ).filter(function(e,i,a){
1006                     return a.lastIndexOf(e) === i;
1007                 });
1008
1009                 var all_orgs = [];
1010                 angular.forEach(base_orgs, function(o) {
1011                     all_orgs = all_orgs.concat( egCore.org.fullPath(o, true) );
1012                 });
1013
1014                 var final_orgs = all_orgs.filter(function(e,i,a){
1015                     return a.lastIndexOf(e) === i;
1016                 }).sort(function(a, b){return parseInt(a)-parseInt(b)});
1017
1018                 if ($scope.location_orgs.toString() != final_orgs.toString()) {
1019                     $scope.location_orgs = final_orgs;
1020                     if ($scope.location_orgs.length) {
1021                         itemSvc.get_locations($scope.location_orgs).then(function(list){
1022                             angular.forEach(list, function(l) {
1023                                 $scope.location_cache[ ''+l.id() ] = l;
1024                             });
1025                             $scope.location_list = list;
1026                         });
1027
1028                         $scope.statcat_filter_list = [];
1029                         angular.forEach($scope.location_orgs, function (o) {
1030                             $scope.statcat_filter_list.push(egCore.org.get(o));
1031                         });
1032
1033                         itemSvc.get_statcats($scope.location_orgs).then(function(list){
1034                             $scope.statcats = list;
1035                             angular.forEach($scope.statcats, function (s) {
1036
1037                                 if (!$scope.working)
1038                                     $scope.working = { statcats: {}, statcat_filter: undefined};
1039                                 if (!$scope.working.statcats)
1040                                     $scope.working.statcats = {};
1041
1042                                 if (!$scope.in_item_select) {
1043                                     $scope.working.statcats[s.id()] = undefined;
1044                                 }
1045                                 createStatcatUpdateWatcher(s.id());
1046                             });
1047                             $scope.in_item_select = false;
1048                         });
1049                     }
1050                 }
1051             }
1052
1053             $scope.workingGridDataProvider.refresh();
1054         });
1055
1056         $scope.suffix_list = [];
1057         itemSvc.get_suffixes(egCore.auth.user().ws_ou()).then(function(list){
1058             $scope.suffix_list = list;
1059         });
1060
1061         $scope.prefix_list = [];
1062         itemSvc.get_prefixes(egCore.auth.user().ws_ou()).then(function(list){
1063             $scope.prefix_list = list;
1064         });
1065
1066         $scope.classification_list = [];
1067         itemSvc.get_classifications().then(function(list){
1068             $scope.classification_list = list;
1069         });
1070
1071         $scope.$watch('completed_copies.length', function () {
1072             $scope.completedGridDataProvider.refresh();
1073         });
1074
1075         $scope.location_list = [];
1076         itemSvc.get_locations().then(function(list){
1077             $scope.location_list = list;
1078         });
1079         createSimpleUpdateWatcher('location');
1080
1081         $scope.status_list = [];
1082         itemSvc.get_statuses().then(function(list){
1083             $scope.status_list = list;
1084         });
1085         createSimpleUpdateWatcher('status');
1086
1087         $scope.circ_modifier_list = [];
1088         itemSvc.get_circ_mods().then(function(list){
1089             $scope.circ_modifier_list = list;
1090         });
1091         createSimpleUpdateWatcher('circ_modifier');
1092
1093         $scope.circ_type_list = [];
1094         itemSvc.get_circ_types().then(function(list){
1095             $scope.circ_type_list = list;
1096         });
1097         createSimpleUpdateWatcher('circ_as_type');
1098
1099         $scope.age_protect_list = [];
1100         itemSvc.get_age_protects().then(function(list){
1101             $scope.age_protect_list = list;
1102         });
1103         createSimpleUpdateWatcher('age_protect');
1104
1105         createSimpleUpdateWatcher('circ_lib');
1106         createSimpleUpdateWatcher('circulate');
1107         createSimpleUpdateWatcher('holdable');
1108         createSimpleUpdateWatcher('fine_level');
1109         createSimpleUpdateWatcher('loan_duration');
1110         createSimpleUpdateWatcher('cost');
1111         createSimpleUpdateWatcher('deposit');
1112         createSimpleUpdateWatcher('deposit_amount');
1113         createSimpleUpdateWatcher('mint_condition');
1114         createSimpleUpdateWatcher('opac_visible');
1115         createSimpleUpdateWatcher('ref');
1116
1117         $scope.saveCompletedCopies = function (and_exit) {
1118             var cnHash = {};
1119             var perCnCopies = {};
1120             angular.forEach( egCore.idl.Clone($scope.completed_copies), function (cp) {
1121                 var cn_id = cp.call_number().id();
1122                 if (!cnHash[cn_id]) {
1123                     cnHash[cn_id] = cp.call_number();
1124                     perCnCopies[cn_id] = [cp];
1125                 } else {
1126                     perCnCopies[cn_id].push(cp);
1127                 }
1128                 cp.call_number(cn_id); // prevent loops in JSON-ification
1129             });
1130
1131             angular.forEach(perCnCopies, function (v, k) {
1132                 cnHash[k].copies(v);
1133             });
1134
1135             cnList = [];
1136             angular.forEach(cnHash, function (v, k) {
1137                 cnList.push(v);
1138             });
1139
1140             egNet.request(
1141                 'open-ils.cat',
1142                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
1143                 egCore.auth.token(), cnList, 1, { auto_merge_vols : 1, create_parts : 1 }
1144             ).then(function(update_count) {
1145                 if (and_exit) {
1146                     $scope.dirty = false;
1147                     $timeout(function(){$window.close()});
1148                 }
1149             });
1150         }
1151
1152         $scope.saveAndContinue = function () {
1153             $scope.saveCompletedCopies(false);
1154         }
1155
1156         $scope.saveAndExit = function () {
1157             $scope.saveCompletedCopies(true);
1158         }
1159
1160     }
1161
1162     $scope.copy_notes_dialog = function(copy_list) {
1163         var default_pub = Boolean($scope.defaults.copy_notes_pub);
1164         if (!angular.isArray(copy_list)) copy_list = [copy_list];
1165
1166         return $modal.open({
1167             templateUrl: './cat/volcopy/t_copy_notes',
1168             animation: true,
1169             controller:
1170                    ['$scope','$modalInstance',
1171             function($scope , $modalInstance) {
1172                 $scope.focusNote = true;
1173                 $scope.note = {
1174                     creator : egCore.auth.user().id(),
1175                     title   : '',
1176                     value   : '',
1177                     pub     : default_pub,
1178                 };
1179
1180                 $scope.require_initials = false;
1181                 egCore.org.settings([
1182                     'ui.staff.require_initials.copy_notes'
1183                 ]).then(function(set) {
1184                     $scope.require_initials = Boolean(set['ui.staff.require_initials.copy_notes']);
1185                 });
1186
1187                 $scope.note_list = [];
1188                 if (copy_list.length == 1) {
1189                     $scope.note_list = copy_list[0].notes();
1190                 }
1191
1192                 $scope.ok = function(note) {
1193
1194                     if (note.initials) note.value += ' [' + note.initials + ']';
1195                     angular.forEach(copy_list, function (cp) {
1196                         var n = new egCore.idl.acpn();
1197                         n.creator(note.creator);
1198                         n.pub(note.pub);
1199                         n.title(note.title);
1200                         n.value(note.value);
1201                         n.owning_copy(cp.id());
1202                         cp.notes().push( n );
1203                     });
1204
1205                     $modalInstance.close();
1206                 }
1207
1208                 $scope.cancel = function($event) {
1209                     $modalInstance.dismiss();
1210                     $event.preventDefault();
1211                 }
1212             }]
1213         });
1214     }
1215
1216 }])
1217
1218 .directive("egVolTemplate", function () {
1219     return {
1220         restrict: 'E',
1221         replace: true,
1222         template: '<div ng-include="'+"'/eg/staff/cat/volcopy/t_attr_edit'"+'"></div>',
1223         scope: { },
1224         controller : ['$scope','$window','itemSvc','egCore',
1225             function ( $scope , $window , itemSvc , egCore ) {
1226
1227                 $scope.defaults = { // If defaults are not set at all, allow everything
1228                     statcats : true,
1229                     copy_notes : true,
1230                     attributes : {
1231                         status : true,
1232                         loan_duration : true,
1233                         fine_level : true,
1234                         cost : true,
1235                         alerts : true,
1236                         deposit : true,
1237                         deposit_amount : true,
1238                         opac_visible : true,
1239                         price : true,
1240                         circulate : true,
1241                         mint_condition : true,
1242                         circ_lib : true,
1243                         ref : true,
1244                         circ_modifier : true,
1245                         circ_as_type : true,
1246                         location : true,
1247                         holdable : true,
1248                         age_protect : true
1249                     }
1250                 };
1251
1252                 $scope.fetchDefaults = function () {
1253                     egCore.hatch.getItem('cat.copy.defaults').then(function(t) {
1254                         if (t) {
1255                             $scope.defaults = t;
1256                             $scope.working.statcat_filter = $scope.defaults.statcat_filter;
1257                         }
1258                     });
1259                 }
1260                 $scope.fetchDefaults();
1261
1262                 $scope.dirty = false;
1263                 $scope.$watch('dirty',
1264                     function(newVal, oldVal) {
1265                         if (newVal && newVal != oldVal) {
1266                             $($window).on('beforeunload.template', function(){
1267                                 return 'There is unsaved template data!'
1268                             });
1269                         } else {
1270                             $($window).off('beforeunload.template');
1271                         }
1272                     }
1273                 );
1274
1275                 $scope.template_controls = true;
1276
1277                 $scope.fetchTemplates = function () {
1278                     egCore.hatch.getItem('cat.copy.templates').then(function(t) {
1279                         if (t) {
1280                             $scope.templates = t;
1281                             $scope.template_name_list = Object.keys(t);
1282                         }
1283                     });
1284                 }
1285                 $scope.fetchTemplates();
1286             
1287                 $scope.applyTemplate = function (n) {
1288                     angular.forEach($scope.templates[n], function (v,k) {
1289                         if (!angular.isObject(v)) {
1290                             $scope.working[k] = angular.copy(v);
1291                         } else {
1292                             angular.forEach(v, function (sv,sk) {
1293                                 $scope.working[k][sk] = angular.copy(sv);
1294                             });
1295                         }
1296                     });
1297                     $scope.template_name = '';
1298                 }
1299
1300                 $scope.deleteTemplate = function (n) {
1301                     if (n) {
1302                         delete $scope.templates[n]
1303                         $scope.template_name_list = Object.keys($scope.templates);
1304                         $scope.template_name = '';
1305                         egCore.hatch.setItem('cat.copy.templates', $scope.templates);
1306                         $scope.$parent.fetchTemplates();
1307                     }
1308                 }
1309
1310                 $scope.saveTemplate = function (n) {
1311                     if (n) {
1312                         var tmpl = {};
1313             
1314                         angular.forEach($scope.working, function (v,k) {
1315                             if (angular.isObject(v)) { // we'll use the pkey
1316                                 if (v.id) v = v.id();
1317                                 else if (v.code) v = v.code();
1318                             }
1319             
1320                             tmpl[k] = v;
1321                         });
1322             
1323                         $scope.templates[n] = tmpl;
1324                         $scope.template_name_list = Object.keys($scope.templates);
1325             
1326                         egCore.hatch.setItem('cat.copy.templates', $scope.templates);
1327                         $scope.$parent.fetchTemplates();
1328
1329                         $scope.dirty = false;
1330                     }
1331                 }
1332             
1333                 $scope.templates = {};
1334                 $scope.template_name = '';
1335                 $scope.template_name_list = [];
1336             
1337                 $scope.tracker = function (x,f) { if (x) return x[f]() };
1338                 $scope.idTracker = function (x) { if (x) return $scope.tracker(x,'id') };
1339                 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
1340             
1341                 $scope.orgById = function (id) { return egCore.org.get(id) }
1342                 $scope.statusById = function (id) {
1343                     return $scope.status_list.filter( function (s) { return s.id() == id } )[0];
1344                 }
1345                 $scope.locationById = function (id) {
1346                     return $scope.location_cache[''+id];
1347                 }
1348             
1349                 createSimpleUpdateWatcher = function (field) {
1350                     $scope.$watch('working.' + field, function () {
1351                         var newval = $scope.working[field];
1352             
1353                         if (typeof newval != 'undefined') {
1354                             $scope.dirty = true;
1355                             if (angular.isObject(newval)) { // we'll use the pkey
1356                                 if (newval.id) $scope.working[field] = newval.id();
1357                                 else if (newval.code) $scope.working[field] = newval.code();
1358                             }
1359             
1360                             if (""+newval == "" || newval == null) {
1361                                 $scope.working[field] = undefined;
1362                             }
1363             
1364                         }
1365                     });
1366                 }
1367             
1368                 $scope.working = {
1369                     statcats: {},
1370                     statcat_filter: undefined
1371                 };
1372             
1373                 createStatcatUpdateWatcher = function (id) {
1374                     return $scope.$watch('working.statcats[' + id + ']', function () {
1375                         if ($scope.working.statcats) {
1376                             var newval = $scope.working.statcats[id];
1377                 
1378                             if (typeof newval != 'undefined') {
1379                                 $scope.dirty = true;
1380                                 if (angular.isObject(newval)) { // we'll use the pkey
1381                                     newval = newval.id();
1382                                 }
1383                 
1384                                 if (""+newval == "" || newval == null) {
1385                                     $scope.working.statcats[id] = undefined;
1386                                     newval = null;
1387                                 }
1388                 
1389                             }
1390                         }
1391                     });
1392                 }
1393
1394                 $scope.clearWorking = function () {
1395                     angular.forEach($scope.working, function (v,k,o) {
1396                         if (!angular.isObject(v)) {
1397                             if (typeof v != 'undefined')
1398                                 $scope.working[k] = undefined;
1399                         } else if (k != 'circ_lib') {
1400                             angular.forEach(v, function (sv,sk) {
1401                                 $scope.working[k][sk] = undefined;
1402                             });
1403                         }
1404                     });
1405                     $scope.working.circ_lib = undefined; // special
1406                     $scope.dirty = false;
1407                 }
1408
1409                 $scope.working = {};
1410                 $scope.location_orgs = [];
1411                 $scope.location_cache = {};
1412             
1413                 $scope.location_list = [];
1414                 itemSvc.get_locations(
1415                     egCore.org.fullPath( egCore.auth.user().ws_ou(), true )
1416                 ).then(function(list){
1417                     $scope.location_list = list;
1418                 });
1419                 createSimpleUpdateWatcher('location');
1420
1421                 $scope.statcat_filter_list = egCore.org.fullPath( egCore.auth.user().ws_ou() );
1422
1423                 $scope.statcats = [];
1424                 itemSvc.get_statcats(
1425                     egCore.org.fullPath( egCore.auth.user().ws_ou(), true )
1426                 ).then(function(list){
1427                     $scope.statcats = list;
1428                     angular.forEach($scope.statcats, function (s) {
1429
1430                         if (!$scope.working)
1431                             $scope.working = { statcats: {}, statcat_filter: undefined};
1432                         if (!$scope.working.statcats)
1433                             $scope.working.statcats = {};
1434
1435                         $scope.working.statcats[s.id()] = undefined;
1436                         createStatcatUpdateWatcher(s.id());
1437                     });
1438                 });
1439             
1440                 $scope.status_list = [];
1441                 itemSvc.get_statuses().then(function(list){
1442                     $scope.status_list = list;
1443                 });
1444                 createSimpleUpdateWatcher('status');
1445             
1446                 $scope.circ_modifier_list = [];
1447                 itemSvc.get_circ_mods().then(function(list){
1448                     $scope.circ_modifier_list = list;
1449                 });
1450                 createSimpleUpdateWatcher('circ_modifier');
1451             
1452                 $scope.circ_type_list = [];
1453                 itemSvc.get_circ_types().then(function(list){
1454                     $scope.circ_type_list = list;
1455                 });
1456                 createSimpleUpdateWatcher('circ_as_type');
1457             
1458                 $scope.age_protect_list = [];
1459                 itemSvc.get_age_protects().then(function(list){
1460                     $scope.age_protect_list = list;
1461                 });
1462                 createSimpleUpdateWatcher('age_protect');
1463             
1464                 createSimpleUpdateWatcher('circ_lib');
1465                 createSimpleUpdateWatcher('circulate');
1466                 createSimpleUpdateWatcher('holdable');
1467                 createSimpleUpdateWatcher('fine_level');
1468                 createSimpleUpdateWatcher('loan_duration');
1469                 createSimpleUpdateWatcher('cost');
1470                 createSimpleUpdateWatcher('deposit');
1471                 createSimpleUpdateWatcher('deposit_amount');
1472                 createSimpleUpdateWatcher('mint_condition');
1473                 createSimpleUpdateWatcher('opac_visible');
1474                 createSimpleUpdateWatcher('ref');
1475
1476                 $scope.suffix_list = [];
1477                 itemSvc.get_suffixes(egCore.auth.user().ws_ou()).then(function(list){
1478                     $scope.suffix_list = list;
1479                 });
1480
1481                 $scope.prefix_list = [];
1482                 itemSvc.get_prefixes(egCore.auth.user().ws_ou()).then(function(list){
1483                     $scope.prefix_list = list;
1484                 });
1485
1486                 $scope.classification_list = [];
1487                 itemSvc.get_classifications().then(function(list){
1488                     $scope.classification_list = list;
1489                 });
1490
1491                 createSimpleUpdateWatcher('working.callnumber.classification');
1492                 createSimpleUpdateWatcher('working.callnumber.prefix');
1493                 createSimpleUpdateWatcher('working.callnumber.suffix');
1494             }
1495         ]
1496     }
1497 })
1498
1499