5 angular.module('egVolCopy',
6 ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
8 .filter('boolText', function(){
14 .config(function($routeProvider, $locationProvider, $compileProvider) {
15 $locationProvider.html5Mode(true);
16 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
19 delay : ['egStartup', function(egStartup) { return egStartup.go(); }]
22 $routeProvider.when('/cat/volcopy/:dataKey', {
23 templateUrl: './cat/volcopy/t_view',
24 controller: 'EditCtrl',
28 $routeProvider.when('/cat/volcopy/:dataKey/:mode', {
29 templateUrl: './cat/volcopy/t_view',
30 controller: 'EditCtrl',
37 function(egCore , $q) {
40 currently_generating : false,
41 auto_gen_barcode : false,
42 barcode_checkdigit : false,
45 tree : {}, // holds lib->cn->copy hash stack
46 copies : [] // raw copy list
49 service.nextBarcode = function(bc) {
50 service.currently_generating = true;
51 return egCore.net.request(
53 'open-ils.cat.item.barcode.autogen',
55 bc, 1, { checkdigit: service.barcode_checkdigit }
56 ).then(function(resp) { // get_barcodes
57 var evt = egCore.evt.parse(resp);
58 if (!evt) return resp[0];
63 service.checkBarcode = function(bc) {
64 if (!service.barcode_checkdigit) return true;
65 if (bc != Number(bc)) return false;
67 // "16.00" == Number("16.00"), but the . is bad.
68 // Throw out any barcode that isn't just digits
69 if (bc.search(/\D/) != -1) return false;
70 var last_digit = bc.substr(bc.length-1);
71 var stripped_barcode = bc.substr(0,bc.length-1);
72 return service.barcodeCheckdigit(stripped_barcode).toString() == last_digit;
75 service.barcodeCheckdigit = function(bc) {
76 var reverse_barcode = bc.toString().split('').reverse();
77 var check_sum = 0; var multiplier = 2;
78 for (var i = 0; i < reverse_barcode.length; i++) {
79 var digit = reverse_barcode[i];
80 var product = digit * multiplier; product = product.toString();
82 for (var j = 0; j < product.length; j++) {
83 temp_sum += Number( product[j] );
85 check_sum += Number( temp_sum );
86 multiplier = ( multiplier == 2 ? 1 : 2 );
88 check_sum = check_sum.toString();
89 var next_multiple_of_10 = (check_sum.match(/(\d*)\d$/)[1] * 10) + 10;
90 var check_digit = next_multiple_of_10 - Number(check_sum); if (check_digit == 10) check_digit = 0;
94 // returns a promise resolved with the list of circ mods
95 service.get_classifications = function() {
97 return $q.when(egCore.env.acnc.list);
99 return egCore.pcrud.retrieveAll('acnc', null, {atomic : true})
100 .then(function(list) {
101 egCore.env.absorbList(list, 'acnc');
106 service.get_prefixes = function(org) {
107 return egCore.pcrud.search('acnp',
108 {owning_lib : egCore.org.fullPath(org, true)},
109 null, {atomic : true}
114 service.get_statcats = function(orgs) {
115 return egCore.pcrud.search('asc',
119 asc : ['owner','entries']
126 service.get_locations = function(orgs) {
127 return egCore.pcrud.search('acpl',
129 null, {atomic : true}
133 service.get_suffixes = function(org) {
134 return egCore.pcrud.search('acns',
135 {owning_lib : egCore.org.fullPath(org, true)},
136 null, {atomic : true}
141 service.get_statuses = function() {
143 return $q.when(egCore.env.ccs.list);
145 return egCore.pcrud.retrieveAll('ccs', {}, {atomic : true}).then(
147 egCore.env.absorbList(list, 'ccs');
154 service.get_circ_mods = function() {
156 return $q.when(egCore.env.ccm.list);
158 return egCore.pcrud.retrieveAll('ccm', {}, {atomic : true}).then(
160 egCore.env.absorbList(list, 'ccm');
167 service.get_circ_types = function() {
169 return $q.when(egCore.env.citm.list);
171 return egCore.pcrud.retrieveAll('citm', {}, {atomic : true}).then(
173 egCore.env.absorbList(list, 'citm');
180 service.get_age_protects = function() {
181 if (egCore.env.crahp)
182 return $q.when(egCore.env.crahp.list);
184 return egCore.pcrud.retrieveAll('crahp', {}, {atomic : true}).then(
186 egCore.env.absorbList(list, 'crahp');
193 service.bmp_parts = {};
194 service.get_parts = function(rec) {
195 if (service.bmp_parts[rec])
196 return $q.when(service.bmp_parts[rec]);
198 return egCore.pcrud.search('bmp',
199 {record : rec, deleted : 'f'},
200 null, {atomic : true}
201 ).then(function(list) {
202 service.bmp_parts[rec] = list;
211 acp : ['call_number','parts','stat_cat_entries', 'notes'],
212 acn : ['label_class','prefix','suffix']
216 service.addCopy = function (cp) {
218 if (!cp.parts()) cp.parts([]); // just in case...
220 var lib = cp.call_number().owning_lib();
221 var cn = cp.call_number().id();
223 if (!service.tree[lib]) service.tree[lib] = {};
224 if (!service.tree[lib][cn]) service.tree[lib][cn] = [];
226 service.tree[lib][cn].push(cp);
227 service.copies.push(cp);
230 service.fetchIds = function(idList) {
231 service.tree = {}; // clear the tree on fetch
232 service.copies = []; // clear the copy list on fetch
233 return egCore.pcrud.search('acp', { 'id' : idList }, service.flesh).then(null,null,
235 service.addCopy(copy);
243 .directive("egVolCopyEdit", function () {
249 '<div class="col-xs-5" ng-class="{'+"'has-error'"+':barcode_has_error}">'+
250 '<input id="{{callNumber.id()}}_{{copy.id()}}"'+
251 ' eg-enter="nextBarcode(copy.id())" class="form-control"'+
252 ' type="text" ng-model="barcode" ng-change="updateBarcode()"/>'+
254 '<div class="col-xs-3"><input class="form-control" type="number" ng-model="copy_number" ng-change="updateCopyNo()"/></div>'+
255 '<div class="col-xs-4"><eg-basic-combo-box eg-disabled="record == 0" list="parts" selected="part"></eg-basic-combo-box></div>'+
258 scope: { focusNext: "=", copy: "=", callNumber: "=", index: "@", record: "@" },
259 controller : ['$scope','itemSvc','egCore',
260 function ( $scope , itemSvc , egCore ) {
261 $scope.new_part_id = 0;
262 $scope.barcode_has_error = false;
264 $scope.nextBarcode = function (i) {
265 $scope.focusNext(i, $scope.barcode);
268 $scope.updateBarcode = function () {
269 if ($scope.barcode != '')
270 $scope.barcode_has_error = !Boolean(itemSvc.checkBarcode($scope.barcode));
271 $scope.copy.barcode($scope.barcode);
272 $scope.copy.ischanged(1);
273 if (itemSvc.currently_generating)
274 $scope.focusNext($scope.copy.id(), $scope.barcode);
277 $scope.updateCopyNo = function () { $scope.copy.copy_number($scope.copy_number); $scope.copy.ischanged(1); };
278 $scope.updatePart = function () {
280 var p = $scope.part_list.filter(function (x) {
281 return x.label() == $scope.part
283 if (p.length > 0) { // preexisting part
285 } else { // create one...
286 var part = new egCore.idl.bmp();
287 part.id( --$scope.new_part_id );
289 part.label( $scope.part );
290 part.record( $scope.callNumber.record() );
291 $scope.copy.parts([part]);
292 $scope.copy.ischanged(1);
295 $scope.copy.parts([]);
298 $scope.$watch('part', $scope.updatePart);
300 $scope.barcode = $scope.copy.barcode();
301 $scope.copy_number = $scope.copy.copy_number();
303 if ($scope.copy.parts()) {
304 $scope.part = $scope.copy.parts()[0];
305 if ($scope.part) $scope.part = $scope.part.label();
309 $scope.part_list = [];
311 itemSvc.get_parts($scope.callNumber.record()).then(function(list){
312 $scope.part_list = list;
313 angular.forEach(list, function(p){ $scope.parts.push(p.label()) });
314 $scope.parts = angular.copy($scope.parts);
323 .directive("egVolRow", function () {
330 '<div class="col-xs-2">'+
331 '<select ng-disabled="record == 0" class="form-control" ng-model="classification" ng-change="updateClassification()" ng-options="cl.name() for cl in classification_list"/>'+
333 '<div class="col-xs-1">'+
334 '<select ng-disabled="record == 0" class="form-control" ng-model="prefix" ng-change="updatePrefix()" ng-options="p.label() for p in prefix_list"/>'+
336 '<div class="col-xs-2"><input ng-disabled="record == 0" class="form-control" type="text" ng-change="updateLabel()" ng-model="label"/></div>'+
337 '<div class="col-xs-1">'+
338 '<select ng-disabled="record == 0" class="form-control" ng-model="suffix" ng-change="updateSuffix()" ng-options="s.label() for s in suffix_list"/>'+
340 '<div ng-hide="onlyVols" class="col-xs-1"><input ng-disabled="record == 0" class="form-control" type="number" ng-model="copy_count" min="{{orig_copy_count}}" ng-change="changeCPCount()"></div>'+
341 '<div ng-hide="onlyVols" class="col-xs-5">'+
342 '<eg-vol-copy-edit record="{{record}}" ng-repeat="cp in copies track by idTracker(cp)" focus-next="focusNextBarcode" copy="cp" call-number="callNumber"></eg-vol-copy-edit>'+
346 scope: {focusNext: "=", allcopies: "=", copies: "=", onlyVols: "=", record: "@" },
347 controller : ['$scope','itemSvc','egCore',
348 function ( $scope , itemSvc , egCore ) {
349 $scope.callNumber = $scope.copies[0].call_number();
351 $scope.idTracker = function (x) { if (x && x.id) return x.id() };
353 // XXX $() is not working! arg
354 $scope.focusNextBarcode = function (i, prev_bc) {
357 angular.forEach($scope.copies, function (cp) {
365 if (yep) n = cp.id();
369 var next = '#' + $scope.callNumber.id() + '_' + n;
372 if (!itemSvc.currently_generating) el.focus();
373 if (prev_bc && itemSvc.auto_gen_barcode && el.val() == "") {
374 itemSvc.nextBarcode(prev_bc).then(function(bc){
377 el.trigger('change');
380 itemSvc.currently_generating = false;
384 $scope.focusNext($scope.callNumber.id(),prev_bc)
388 $scope.suffix_list = [];
389 itemSvc.get_suffixes($scope.callNumber.owning_lib()).then(function(list){
390 $scope.suffix_list = list;
391 $scope.$watch('callNumber.suffix()', function (v) {
392 if (angular.isObject(v)) v = v.id();
393 $scope.suffix = $scope.suffix_list.filter( function (s) {
399 $scope.updateSuffix = function () {
400 angular.forEach($scope.copies, function(cp) {
401 cp.call_number().suffix($scope.suffix);
402 cp.call_number().ischanged(1);
406 $scope.prefix_list = [];
407 itemSvc.get_prefixes($scope.callNumber.owning_lib()).then(function(list){
408 $scope.prefix_list = list;
409 $scope.$watch('callNumber.prefix()', function (v) {
410 if (angular.isObject(v)) v = v.id();
411 $scope.prefix = $scope.prefix_list.filter(function (p) {
417 $scope.updatePrefix = function () {
418 angular.forEach($scope.copies, function(cp) {
419 cp.call_number().prefix($scope.prefix);
420 cp.call_number().ischanged(1);
423 $scope.$watch('callNumber.owning_lib()', function(oldLib, newLib) {
424 if (oldLib == newLib) return;
425 var currentPrefix = $scope.callNumber.prefix();
426 if (angular.isObject(currentPrefix)) currentPrefix = currentPrefix.id();
427 itemSvc.get_prefixes($scope.callNumber.owning_lib()).then(function(list){
428 $scope.prefix_list = list;
429 $scope.prefix = $scope.prefix_list.filter(function (p) {
430 return p.id() == currentPrefix;
432 if ($scope.prefix != currentPrefix) {
433 $scope.callNumber.prefix($scope.prefix);
436 var currentSuffix = $scope.callNumber.suffix();
437 if (angular.isObject(currentSuffix)) currentSuffix = currentSuffix.id();
438 itemSvc.get_suffixes($scope.callNumber.owning_lib()).then(function(list){
439 $scope.suffix_list = list;
440 $scope.suffix = $scope.suffix_list.filter(function (s) {
441 return s.id() == currentSuffix;
443 if ($scope.suffix != currentSuffix) {
444 $scope.callNumber.suffix($scope.suffix);
449 $scope.classification_list = [];
450 itemSvc.get_classifications().then(function(list){
451 $scope.classification_list = list;
452 $scope.$watch('callNumber.label_class()', function (v) {
453 if (angular.isObject(v)) v = v.id();
454 $scope.classification = $scope.classification_list.filter(function (c) {
460 $scope.updateClassification = function () {
461 angular.forEach($scope.copies, function(cp) {
462 cp.call_number().label_class($scope.classification);
463 cp.call_number().ischanged(1);
467 $scope.updateLabel = function () {
468 angular.forEach($scope.copies, function(cp) {
469 cp.call_number().label($scope.label);
470 cp.call_number().ischanged(1);
474 $scope.$watch('callNumber.label()', function (v) {
478 $scope.prefix = $scope.callNumber.prefix();
479 $scope.suffix = $scope.callNumber.suffix();
480 $scope.classification = $scope.callNumber.label_class();
481 $scope.label = $scope.callNumber.label();
483 $scope.copy_count = $scope.copies.length;
484 $scope.orig_copy_count = $scope.copy_count;
486 $scope.changeCPCount = function () {
487 while ($scope.copy_count > $scope.copies.length) {
488 var cp = new egCore.idl.acp();
489 cp.id( --itemSvc.new_cp_id );
491 cp.circ_lib( $scope.callNumber.owning_lib() );
492 cp.call_number( $scope.callNumber );
495 cp.deposit_amount(0);
496 cp.fine_level(2); // Normal
497 cp.loan_duration(2); // Normal
498 cp.location(1); // Stacks
501 cp.opac_visible('t');
503 cp.mint_condition('t');
504 $scope.copies.push( cp );
505 $scope.allcopies.push( cp );
509 if ($scope.copy_count >= $scope.orig_copy_count) {
510 var how_many = $scope.copies.length - $scope.copy_count;
512 var dead = $scope.copies.splice($scope.copy_count,how_many);
513 $scope.callNumber.copies($scope.copies);
515 // Trimming the global list is a bit more tricky
516 angular.forEach( dead, function (d) {
517 angular.forEach( $scope.allcopies, function (l, i) {
518 if (l === d) $scope.allcopies.splice(i,1);
531 .directive("egVolEdit", function () {
537 '<div class="col-xs-1"><eg-org-selector alldisabled="{{record == 0}}" selected="owning_lib" disable-test="cant_have_vols"></eg-org-selector></div>'+
538 '<div class="col-xs-1"><input ng-disabled="record == 0" class="form-control" type="number" min="{{orig_cn_count}}" ng-model="cn_count" ng-change="changeCNCount()"/></div>'+
539 '<div class="col-xs-10">'+
540 '<eg-vol-row only-vols="onlyVols" record="{{record}}"'+
541 'ng-repeat="(cn,copies) in struct | orderBy:cn track by cn" '+
542 'focus-next="focusNextFirst" copies="copies" allcopies="allcopies">'+
547 scope: { focusNext: "=", allcopies: "=", struct: "=", lib: "@", record: "@", onlyVols: "=" },
548 controller : ['$scope','itemSvc','egCore',
549 function ( $scope , itemSvc , egCore ) {
550 $scope.first_cn = Object.keys($scope.struct)[0];
551 $scope.full_cn = $scope.struct[$scope.first_cn][0].call_number();
553 $scope.defaults = {};
554 egCore.hatch.getItem('cat.copy.defaults').then(function(t) {
560 $scope.focusNextFirst = function(prev_cn,prev_bc) {
563 angular.forEach(Object.keys($scope.struct).sort(), function (cn) {
575 var next = '#' + n + '_' + $scope.struct[n][0].id();
578 if (!itemSvc.currently_generating) el.focus();
579 if (prev_bc && itemSvc.auto_gen_barcode && el.val() == "") {
580 itemSvc.nextBarcode(prev_bc).then(function(bc){
583 el.trigger('change');
586 itemSvc.currently_generating = false;
590 $scope.focusNext($scope.lib, prev_bc);
594 $scope.cn_count = Object.keys($scope.struct).length;
595 $scope.orig_cn_count = $scope.cn_count;
597 $scope.owning_lib = egCore.org.get($scope.lib);
598 $scope.$watch('owning_lib', function (oldLib, newLib) {
599 if (oldLib == newLib) return;
600 angular.forEach( Object.keys($scope.struct), function (cn) {
601 $scope.struct[cn][0].call_number().owning_lib( $scope.owning_lib.id() );
602 $scope.struct[cn][0].call_number().ischanged(1);
606 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
608 $scope.$watch('cn_count', function (n) {
609 var o = Object.keys($scope.struct).length;
610 if (n > o) { // adding
611 for (var i = o; o < n; o++) {
612 var cn = new egCore.idl.acn();
613 cn.id( --itemSvc.new_cn_id );
615 cn.prefix( $scope.defaults.prefix || -1 );
616 cn.suffix( $scope.defaults.suffix || -1 );
617 cn.label_class( $scope.defaults.classification || 1 );
618 cn.owning_lib( $scope.owning_lib.id() );
619 cn.record( $scope.full_cn.record() );
621 var cp = new egCore.idl.acp();
622 cp.call_number( cn );
623 cp.id( --itemSvc.new_cp_id );
628 cp.deposit_amount(0);
629 cp.fine_level(2); // Normal
630 cp.loan_duration(2); // Normal
631 cp.location(1); // Stacks
634 cp.opac_visible('t');
636 cp.mint_condition('t');
638 cp.circ_lib( $scope.owning_lib.id() );
639 cp.call_number( cn );
641 $scope.struct[cn.id()] = [cp];
642 $scope.allcopies.push(cp);
644 } else if (n < o && n >= $scope.orig_cn_count) { // removing
645 var how_many = o - n;
648 .sort(function(a, b){return parseInt(a)-parseInt(b)})
649 .filter(function(x){ return parseInt(x) <= 0 });
650 for (var i = 0; i < how_many; i++) {
651 // Trimming the global list is a bit more tricky
652 angular.forEach($scope.struct[list[i]], function (d) {
653 angular.forEach( $scope.allcopies, function (l, j) {
654 if (l === d) $scope.allcopies.splice(j,1);
657 delete $scope.struct[list[i]];
670 .controller('EditCtrl',
671 ['$scope','$q','$window','$routeParams','$location','$timeout','egCore','egNet','egGridDataProvider','itemSvc','$modal',
672 function($scope , $q , $window , $routeParams , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc , $modal) {
674 $scope.defaults = { // If defaults are not set at all, allow everything
675 barcode_checkdigit : false,
676 auto_gen_barcode : false,
681 loan_duration : true,
686 deposit_amount : true,
690 mint_condition : true,
693 circ_modifier : true,
701 $scope.embedded = ($routeParams.mode && $routeParams.mode == 'embedded') ? true : false;
703 $scope.saveDefaults = function () {
704 egCore.hatch.setItem('cat.copy.defaults', $scope.defaults);
707 $scope.fetchDefaults = function () {
708 egCore.hatch.getItem('cat.copy.defaults').then(function(t) {
711 if (!$scope.batch) $scope.batch = {};
712 $scope.batch.classification = $scope.defaults.classification;
713 $scope.batch.prefix = $scope.defaults.prefix;
714 $scope.batch.suffix = $scope.defaults.suffix;
715 $scope.working.statcat_filter = $scope.defaults.statcat_filter;
716 if ($scope.defaults.always_volumes) $scope.show_vols = true;
717 if ($scope.defaults.barcode_checkdigit) itemSvc.barcode_checkdigit = true;
718 if ($scope.defaults.auto_gen_barcode) itemSvc.auto_gen_barcode = true;
722 $scope.fetchDefaults();
724 $scope.$watch('defaults.auto_gen_barcode', function (n,o) {
725 itemSvc.auto_gen_barcode = n
728 $scope.$watch('defaults.barcode_checkdigit', function (n,o) {
729 itemSvc.barcode_checkdigit = n
732 $scope.dirty = false;
733 $scope.$watch('dirty',
734 function(newVal, oldVal) {
735 if (newVal && newVal != oldVal) {
736 $($window).on('beforeunload.edit', function(){
737 return 'There is unsaved data!'
740 $($window).off('beforeunload.edit');
745 $scope.only_vols = false;
746 $scope.show_vols = true;
747 $scope.show_copies = true;
749 $scope.tracker = function (x,f) { if (x) return x[f]() };
750 $scope.idTracker = function (x) { if (x) return $scope.tracker(x,'id') };
751 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
753 $scope.orgById = function (id) { return egCore.org.get(id) }
754 $scope.statusById = function (id) {
755 return $scope.status_list.filter( function (s) { return s.id() == id } )[0];
757 $scope.locationById = function (id) {
758 return $scope.location_cache[''+id];
761 $scope.workingToComplete = function () {
762 angular.forEach( $scope.workingGridControls.selectedItems(), function (c) {
763 angular.forEach( itemSvc.copies, function (w, i) {
765 $scope.completed_copies = $scope.completed_copies.concat(itemSvc.copies.splice(i,1));
772 $scope.completeToWorking = function () {
773 angular.forEach( $scope.completedGridControls.selectedItems(), function (c) {
774 angular.forEach( $scope.completed_copies, function (w, i) {
776 itemSvc.copies = itemSvc.copies.concat($scope.completed_copies.splice(i,1));
783 createSimpleUpdateWatcher = function (field) {
784 return $scope.$watch('working.' + field, function () {
785 var newval = $scope.working[field];
787 if (typeof newval != 'undefined') {
788 if (angular.isObject(newval)) { // we'll use the pkey
789 if (newval.id) newval = newval.id();
790 else if (newval.code) newval = newval.code();
793 if (""+newval == "" || newval == null) {
794 $scope.working[field] = undefined;
798 if ($scope.workingGridControls && $scope.workingGridControls.selectedItems) {
800 $scope.workingGridControls.selectedItems(),
802 if (cp[field]() !== newval) {
816 statcat_filter: undefined
819 $scope.statcatUpdate = function (id) {
820 var newval = $scope.working.statcats[id];
822 if (typeof newval != 'undefined') {
823 if (angular.isObject(newval)) { // we'll use the pkey
824 newval = newval.id();
827 if (""+newval == "" || newval == null) {
828 $scope.working.statcats[id] = undefined;
832 if (!$scope.in_item_select && $scope.workingGridControls && $scope.workingGridControls.selectedItems) {
834 $scope.workingGridControls.selectedItems(),
839 angular.forEach( cp.stat_cat_entries(), function (e) {
840 if (e.stat_cat() == id) { // mark deleted
847 var e = new egCore.idl.ascecm();
849 e.owning_copy( cp.id() );
851 e.stat_cat_entry( newval );
854 cp.stat_cat_entries().concat([ e ])
859 cp.stat_cat_entries( // trim out ephemeral deleted ones
860 cp.stat_cat_entries().filter(function (e) {
861 if (Boolean(e.isnew())) {
862 if (Boolean(e.isdeleted())) {
877 var dataKey = $routeParams.dataKey;
878 console.debug('dataKey: ' + dataKey);
880 if (dataKey && dataKey.length > 0) {
882 $scope.templates = {};
883 $scope.template_name = '';
884 $scope.template_name_list = [];
886 $scope.fetchTemplates = function () {
887 egCore.hatch.getItem('cat.copy.templates').then(function(t) {
889 $scope.templates = t;
890 $scope.template_name_list = Object.keys(t);
893 egCore.hatch.getItem('cat.copy.last_template').then(function(t) {
894 if (t) $scope.template_name = t;
897 $scope.fetchTemplates();
899 $scope.applyTemplate = function (n) {
900 angular.forEach($scope.templates[n], function (v,k) {
901 if (k == 'circ_lib') {
902 $scope.working[k] = egCore.org.get(v);
903 } else if (!angular.isObject(v)) {
904 $scope.working[k] = angular.copy(v);
906 angular.forEach(v, function (sv,sk) {
907 if (k == 'callnumber') {
908 angular.forEach(v, function (cnv,cnk) {
909 $scope.batch[cnk] = cnv;
911 $scope.applyBatchCNValues();
913 $scope.working[k][sk] = angular.copy(sv);
914 if (k == 'statcats') $scope.statcatUpdate(sk);
919 egCore.hatch.setItem('cat.copy.last_template', n);
922 $scope.copytab = 'working';
924 $scope.summaryRecord = null;
925 $scope.record_id = null;
927 $scope.completed_copies = [];
928 $scope.location_orgs = [];
929 $scope.location_cache = {};
930 $scope.statcats = [];
931 if (!$scope.batch) $scope.batch = {};
933 $scope.applyBatchCNValues = function () {
934 if ($scope.data.tree) {
935 angular.forEach($scope.data.tree, function(cn_hash) {
936 angular.forEach(cn_hash, function(copies) {
937 angular.forEach(copies, function(cp) {
938 if (typeof $scope.batch.classification != 'undefined' && $scope.batch.classification != '') {
939 var label_class = $scope.classification_list.filter(function(p){ return p.id() == $scope.batch.classification })[0];
940 cp.call_number().label_class(label_class);
941 cp.call_number().ischanged(1);
944 if (typeof $scope.batch.prefix != 'undefined' && $scope.batch.prefix != '') {
945 var prefix = $scope.prefix_list.filter(function(p){ return p.id() == $scope.batch.prefix })[0];
946 cp.call_number().prefix(prefix);
947 cp.call_number().ischanged(1);
950 if (typeof $scope.batch.label != 'undefined' && $scope.batch.label != '') {
951 cp.call_number().label($scope.batch.label);
952 cp.call_number().ischanged(1);
955 if (typeof $scope.batch.suffix != 'undefined' && $scope.batch.suffix != '') {
956 var suffix = $scope.suffix_list.filter(function(p){ return p.id() == $scope.batch.suffix })[0];
957 cp.call_number().suffix(suffix);
958 cp.call_number().ischanged(1);
967 $scope.clearWorking = function () {
968 angular.forEach($scope.working, function (v,k,o) {
969 if (!angular.isObject(v)) {
970 if (typeof v != 'undefined')
971 $scope.working[k] = undefined;
972 } else if (k != 'circ_lib') {
973 angular.forEach(v, function (sv,sk) {
974 if (typeof v != 'undefined')
975 $scope.working[k][sk] = undefined;
979 $scope.working.circ_lib = undefined; // special
982 $scope.completedGridDataProvider = egGridDataProvider.instance({
983 get : function(offset, count) {
984 //return provider.arrayNotifier(itemSvc.copies, offset, count);
985 return this.arrayNotifier($scope.completed_copies, offset, count);
989 $scope.completedGridControls = {};
991 $scope.workingGridDataProvider = egGridDataProvider.instance({
992 get : function(offset, count) {
993 //return provider.arrayNotifier(itemSvc.copies, offset, count);
994 return this.arrayNotifier(itemSvc.copies, offset, count);
998 $scope.workingGridControls = {};
999 $scope.add_vols_copies = false;
1000 $scope.is_fast_add = false;
1004 'open-ils.actor.anon_cache.get_value',
1005 dataKey, 'edit-these-copies'
1006 ).then(function (data) {
1009 if (data.hide_vols && !$scope.defaults.always_volumes) $scope.show_vols = false;
1010 if (data.hide_copies) {
1011 $scope.show_copies = false;
1012 $scope.only_vols = true;
1015 $scope.record_id = data.record_id;
1017 function fetchRaw () {
1018 if (!$scope.only_vols) $scope.dirty = true;
1019 $scope.add_vols_copies = true;
1021 /* data.raw data structure looks like this:
1023 * callnumber : $cn_id, // optional, to add a copy to a cn
1024 * owner : $org, // optional, defaults to ws_ou
1025 * label : $cn_label, // optional, to supply a label on a new cn
1026 * barcode : $cp_barcode // optional, to supply a barcode on a new cp
1027 * fast_add : boolean // optional, to specify whether this came
1031 * All can be left out and a completely empty vol/copy combo will be vivicated.
1037 if (proto.fast_add) $scope.is_fast_add = true;
1038 if (proto.callnumber) {
1039 return egCore.pcrud.retrieve('acn', proto.callnumber)
1040 .then(function(cn) {
1041 var cp = new egCore.idl.acp();
1042 cp.call_number( cn );
1043 cp.id( --itemSvc.new_cp_id );
1044 if (!$scope.only_vols) cp.isnew( true );
1045 cp.circ_lib( proto.owner || egCore.auth.user().ws_ou() );
1049 cp.deposit_amount(0);
1050 cp.fine_level(2); // Normal
1051 cp.loan_duration(2); // Normal
1052 cp.location(1); // Stacks
1055 cp.opac_visible('t');
1057 cp.mint_condition('t');
1059 if (proto.barcode) cp.barcode( proto.barcode );
1064 var cn = new egCore.idl.acn();
1065 cn.id( --itemSvc.new_cn_id );
1067 cn.prefix( $scope.defaults.prefix || -1 );
1068 cn.suffix( $scope.defaults.suffix || -1 );
1069 cn.label_class( $scope.defaults.classification || 1 );
1070 cn.owning_lib( proto.owner || egCore.auth.user().ws_ou() );
1071 cn.record( $scope.record_id );
1072 if (proto.label) cn.label( proto.label );
1074 var cp = new egCore.idl.acp();
1075 cp.call_number( cn );
1076 cp.id( --itemSvc.new_cp_id );
1081 cp.deposit_amount(0);
1082 cp.fine_level(2); // Normal
1083 cp.loan_duration(2); // Normal
1084 cp.location(1); // Stacks
1087 cp.opac_visible('t');
1089 cp.mint_condition('t');
1091 cp.circ_lib( proto.owner || egCore.auth.user().ws_ou() );
1092 if (proto.barcode) cp.barcode( proto.barcode );
1100 return itemSvc.copies;
1103 if (data.copies && data.copies.length)
1104 return itemSvc.fetchIds(data.copies).then(fetchRaw);
1110 }).then( function() {
1111 $scope.data = itemSvc;
1112 if ($scope.add_vols_copies) {
1113 var status_setting = $scope.is_fast_add ?
1114 'cat.default_copy_status_fast' :
1115 'cat.default_copy_status_normal';
1116 egCore.org.settings([
1118 ]).then(function(set) {
1119 $scope.default_ccs = set[status_setting] ||
1120 ($scope.is_fast_add ? 0 : 5); // 0 is Available, 5 is In Process
1121 angular.forEach($scope.data.copies, function (cp) {
1122 cp.status($scope.default_ccs);
1124 $scope.workingGridDataProvider.refresh();
1129 $scope.focusNextFirst = function(prev_lib,prev_bc) {
1132 angular.forEach(Object.keys($scope.data.tree).sort(), function (lib) {
1135 if (lib == prev_lib) {
1144 var first_cn = Object.keys($scope.data.tree[n])[0];
1145 var next = '#' + first_cn + '_' + $scope.data.tree[n][first_cn][0].id();
1148 if (!itemSvc.currently_generating) el.focus();
1149 if (prev_bc && itemSvc.auto_gen_barcode && el.val() == "") {
1150 itemSvc.nextBarcode(prev_bc).then(function(bc){
1153 el.trigger('change');
1156 itemSvc.currently_generating = false;
1162 $scope.in_item_select = false;
1163 $scope.afterItemSelect = function() { $scope.in_item_select = false };
1164 $scope.handleItemSelect = function (item_list) {
1165 if (item_list && item_list.length > 0) {
1166 $scope.in_item_select = true;
1168 angular.forEach(Object.keys($scope.defaults.attributes), function (attr) {
1170 var value_hash = {};
1171 angular.forEach(item_list, function (item) {
1173 var v = item[attr]()
1174 if (angular.isObject(v)) {
1175 if (v.id) v = v.id();
1176 else if (v.code) v = v.code();
1182 if (Object.keys(value_hash).length == 1) {
1183 if (attr == 'circ_lib') {
1184 $scope.working[attr] = egCore.org.get(item_list[0][attr]());
1186 $scope.working[attr] = item_list[0][attr]();
1189 $scope.working[attr] = undefined;
1193 angular.forEach($scope.statcats, function (sc) {
1196 var value_hash = {};
1198 angular.forEach(item_list, function (item) {
1199 if (item.stat_cat_entries()) {
1200 if (item.stat_cat_entries().length > 0) {
1201 var right_sc = item.stat_cat_entries().filter(function (e) {
1202 return e.stat_cat() == sc.id() && !Boolean(e.isdeleted());
1205 if (right_sc.length > 0) {
1206 value_hash[right_sc[0].stat_cat_entry()] = right_sc[0].stat_cat_entry();
1216 if (!none && Object.keys(value_hash).length == 1) {
1217 $scope.working.statcats[sc.id()] = value_hash[Object.keys(value_hash)[0]];
1219 $scope.working.statcats[sc.id()] = undefined;
1224 $scope.clearWorking();
1229 $scope.$watch('data.copies.length', function () {
1230 if ($scope.data.copies) {
1231 var base_orgs = $scope.data.copies.map(function(cp){
1232 return cp.circ_lib()
1234 $scope.data.copies.map(function(cp){
1235 return cp.call_number().owning_lib()
1238 [egCore.auth.user().ws_ou()]
1239 ).filter(function(e,i,a){
1240 return a.lastIndexOf(e) === i;
1244 angular.forEach(base_orgs, function(o) {
1245 all_orgs = all_orgs.concat( egCore.org.fullPath(o, true) );
1248 var final_orgs = all_orgs.filter(function(e,i,a){
1249 return a.lastIndexOf(e) === i;
1250 }).sort(function(a, b){return parseInt(a)-parseInt(b)});
1252 if ($scope.location_orgs.toString() != final_orgs.toString()) {
1253 $scope.location_orgs = final_orgs;
1254 if ($scope.location_orgs.length) {
1255 itemSvc.get_locations($scope.location_orgs).then(function(list){
1256 angular.forEach(list, function(l) {
1257 $scope.location_cache[ ''+l.id() ] = l;
1259 $scope.location_list = list;
1262 $scope.statcat_filter_list = [];
1263 angular.forEach($scope.location_orgs, function (o) {
1264 $scope.statcat_filter_list.push(egCore.org.get(o));
1267 itemSvc.get_statcats($scope.location_orgs).then(function(list){
1268 $scope.statcats = list;
1269 angular.forEach($scope.statcats, function (s) {
1271 if (!$scope.working)
1272 $scope.working = { statcats: {}, statcat_filter: undefined};
1273 if (!$scope.working.statcats)
1274 $scope.working.statcats = {};
1276 if (!$scope.in_item_select) {
1277 $scope.working.statcats[s.id()] = undefined;
1279 createStatcatUpdateWatcher(s.id());
1281 $scope.in_item_select = false;
1287 $scope.workingGridDataProvider.refresh();
1290 $scope.suffix_list = [];
1291 itemSvc.get_suffixes(egCore.auth.user().ws_ou()).then(function(list){
1292 $scope.suffix_list = list;
1295 $scope.prefix_list = [];
1296 itemSvc.get_prefixes(egCore.auth.user().ws_ou()).then(function(list){
1297 $scope.prefix_list = list;
1300 $scope.classification_list = [];
1301 itemSvc.get_classifications().then(function(list){
1302 $scope.classification_list = list;
1305 $scope.$watch('completed_copies.length', function () {
1306 $scope.completedGridDataProvider.refresh();
1309 $scope.location_list = [];
1310 itemSvc.get_locations().then(function(list){
1311 $scope.location_list = list;
1313 createSimpleUpdateWatcher('location');
1315 $scope.status_list = [];
1316 itemSvc.get_statuses().then(function(list){
1317 $scope.status_list = list;
1319 createSimpleUpdateWatcher('status');
1321 $scope.circ_modifier_list = [];
1322 itemSvc.get_circ_mods().then(function(list){
1323 $scope.circ_modifier_list = list;
1325 createSimpleUpdateWatcher('circ_modifier');
1327 $scope.circ_type_list = [];
1328 itemSvc.get_circ_types().then(function(list){
1329 $scope.circ_type_list = list;
1331 createSimpleUpdateWatcher('circ_as_type');
1333 $scope.age_protect_list = [];
1334 itemSvc.get_age_protects().then(function(list){
1335 $scope.age_protect_list = list;
1337 createSimpleUpdateWatcher('age_protect');
1339 createSimpleUpdateWatcher('circ_lib');
1340 createSimpleUpdateWatcher('circulate');
1341 createSimpleUpdateWatcher('holdable');
1342 createSimpleUpdateWatcher('fine_level');
1343 createSimpleUpdateWatcher('loan_duration');
1344 createSimpleUpdateWatcher('price');
1345 createSimpleUpdateWatcher('cost');
1346 createSimpleUpdateWatcher('deposit');
1347 createSimpleUpdateWatcher('deposit_amount');
1348 createSimpleUpdateWatcher('mint_condition');
1349 createSimpleUpdateWatcher('opac_visible');
1350 createSimpleUpdateWatcher('ref');
1352 $scope.saveCompletedCopies = function (and_exit) {
1354 var perCnCopies = {};
1355 angular.forEach( $scope.completed_copies, function (cp) {
1356 var cn = cp.call_number();
1357 var cn_cps = cp.call_number().copies();
1358 cp.call_number().copies([]);
1359 var cn_id = cp.call_number().id();
1360 cp.call_number(cn_id); // prevent loops in JSON-ification
1361 if (!cnHash[cn_id]) {
1362 cnHash[cn_id] = egCore.idl.Clone(cn);
1363 perCnCopies[cn_id] = [egCore.idl.Clone(cp)];
1365 perCnCopies[cn_id].push(egCore.idl.Clone(cp));
1367 cp.call_number(cn); // put the data back
1368 cp.call_number().copies(cn_cps);
1369 if (typeof cnHash[cn_id].prefix() == 'object')
1370 cnHash[cn_id].prefix(cnHash[cn_id].prefix().id()); // un-object-ize some fields
1371 if (typeof cnHash[cn_id].suffix() == 'object')
1372 cnHash[cn_id].suffix(cnHash[cn_id].suffix().id()); // un-object-ize some fields
1375 angular.forEach(perCnCopies, function (v, k) {
1376 cnHash[k].copies(v);
1380 angular.forEach(cnHash, function (v, k) {
1386 'open-ils.cat.asset.volume.fleshed.batch.update.override',
1387 egCore.auth.token(), cnList, 1, { auto_merge_vols : 1, create_parts : 1 }
1388 ).then(function(update_count) {
1390 $scope.dirty = false;
1391 $timeout(function(){$window.close()});
1396 $scope.saveAndContinue = function () {
1397 $scope.saveCompletedCopies(false);
1400 $scope.workingSaveAndExit = function () {
1401 $scope.workingToComplete();
1402 $scope.saveAndExit();
1405 $scope.saveAndExit = function () {
1406 $scope.saveCompletedCopies(true);
1411 $scope.copy_notes_dialog = function(copy_list) {
1412 var default_pub = Boolean($scope.defaults.copy_notes_pub);
1413 if (!angular.isArray(copy_list)) copy_list = [copy_list];
1415 return $modal.open({
1416 templateUrl: './cat/volcopy/t_copy_notes',
1419 ['$scope','$modalInstance',
1420 function($scope , $modalInstance) {
1421 $scope.focusNote = true;
1423 creator : egCore.auth.user().id(),
1429 $scope.require_initials = false;
1430 egCore.org.settings([
1431 'ui.staff.require_initials.copy_notes'
1432 ]).then(function(set) {
1433 $scope.require_initials = Boolean(set['ui.staff.require_initials.copy_notes']);
1436 $scope.note_list = [];
1437 if (copy_list.length == 1) {
1438 $scope.note_list = copy_list[0].notes();
1441 $scope.ok = function(note) {
1443 if (note.initials) note.value += ' [' + note.initials + ']';
1444 angular.forEach(copy_list, function (cp) {
1445 if (!angular.isArray(cp.notes())) cp.notes([]);
1446 var n = new egCore.idl.acpn();
1448 n.creator(note.creator);
1450 n.title(note.title);
1451 n.value(note.value);
1452 n.owning_copy(cp.id());
1453 cp.notes().push( n );
1456 $modalInstance.close();
1459 $scope.cancel = function($event) {
1460 $modalInstance.dismiss();
1461 $event.preventDefault();
1469 .directive("egVolTemplate", function () {
1473 template: '<div ng-include="'+"'/eg/staff/cat/volcopy/t_attr_edit'"+'"></div>',
1475 controller : ['$scope','$window','itemSvc','egCore',
1476 function ( $scope , $window , itemSvc , egCore ) {
1478 $scope.defaults = { // If defaults are not set at all, allow everything
1479 barcode_checkdigit : false,
1480 auto_gen_barcode : false,
1485 loan_duration : true,
1490 deposit_amount : true,
1491 opac_visible : true,
1494 mint_condition : true,
1497 circ_modifier : true,
1498 circ_as_type : true,
1505 $scope.fetchDefaults = function () {
1506 egCore.hatch.getItem('cat.copy.defaults').then(function(t) {
1508 $scope.defaults = t;
1509 $scope.working.statcat_filter = $scope.defaults.statcat_filter;
1513 $scope.fetchDefaults();
1515 $scope.dirty = false;
1516 $scope.$watch('dirty',
1517 function(newVal, oldVal) {
1518 if (newVal && newVal != oldVal) {
1519 $($window).on('beforeunload.template', function(){
1520 return 'There is unsaved template data!'
1523 $($window).off('beforeunload.template');
1528 $scope.template_controls = true;
1530 $scope.fetchTemplates = function () {
1531 egCore.hatch.getItem('cat.copy.templates').then(function(t) {
1533 $scope.templates = t;
1534 $scope.template_name_list = Object.keys(t);
1538 $scope.fetchTemplates();
1540 $scope.applyTemplate = function (n) {
1541 angular.forEach($scope.templates[n], function (v,k) {
1542 if (k == 'circ_lib') {
1543 $scope.working[k] = egCore.org.get(v);
1544 } else if (!angular.isObject(v)) {
1545 $scope.working[k] = angular.copy(v);
1547 angular.forEach(v, function (sv,sk) {
1548 $scope.working[k][sk] = angular.copy(sv);
1552 $scope.template_name = '';
1555 $scope.deleteTemplate = function (n) {
1557 delete $scope.templates[n]
1558 $scope.template_name_list = Object.keys($scope.templates);
1559 $scope.template_name = '';
1560 egCore.hatch.setItem('cat.copy.templates', $scope.templates);
1561 $scope.$parent.fetchTemplates();
1565 $scope.saveTemplate = function (n) {
1569 angular.forEach($scope.working, function (v,k) {
1570 if (angular.isObject(v)) { // we'll use the pkey
1571 if (v.id) v = v.id();
1572 else if (v.code) v = v.code();
1578 $scope.templates[n] = tmpl;
1579 $scope.template_name_list = Object.keys($scope.templates);
1581 egCore.hatch.setItem('cat.copy.templates', $scope.templates);
1582 $scope.$parent.fetchTemplates();
1584 $scope.dirty = false;
1588 $scope.templates = {};
1589 $scope.template_name = '';
1590 $scope.template_name_list = [];
1592 $scope.tracker = function (x,f) { if (x) return x[f]() };
1593 $scope.idTracker = function (x) { if (x) return $scope.tracker(x,'id') };
1594 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
1596 $scope.orgById = function (id) { return egCore.org.get(id) }
1597 $scope.statusById = function (id) {
1598 return $scope.status_list.filter( function (s) { return s.id() == id } )[0];
1600 $scope.locationById = function (id) {
1601 return $scope.location_cache[''+id];
1604 createSimpleUpdateWatcher = function (field) {
1605 $scope.$watch('working.' + field, function () {
1606 var newval = $scope.working[field];
1608 if (typeof newval != 'undefined') {
1609 $scope.dirty = true;
1610 if (angular.isObject(newval)) { // we'll use the pkey
1611 if (newval.id) $scope.working[field] = newval.id();
1612 else if (newval.code) $scope.working[field] = newval.code();
1615 if (""+newval == "" || newval == null) {
1616 $scope.working[field] = undefined;
1625 statcat_filter: undefined
1628 createStatcatUpdateWatcher = function (id) {
1629 return $scope.$watch('working.statcats[' + id + ']', function () {
1630 if ($scope.working.statcats) {
1631 var newval = $scope.working.statcats[id];
1633 if (typeof newval != 'undefined') {
1634 $scope.dirty = true;
1635 if (angular.isObject(newval)) { // we'll use the pkey
1636 newval = newval.id();
1639 if (""+newval == "" || newval == null) {
1640 $scope.working.statcats[id] = undefined;
1649 $scope.clearWorking = function () {
1650 angular.forEach($scope.working, function (v,k,o) {
1651 if (!angular.isObject(v)) {
1652 if (typeof v != 'undefined')
1653 $scope.working[k] = undefined;
1654 } else if (k != 'circ_lib') {
1655 angular.forEach(v, function (sv,sk) {
1656 $scope.working[k][sk] = undefined;
1660 $scope.working.circ_lib = undefined; // special
1661 $scope.dirty = false;
1664 $scope.working = {};
1665 $scope.location_orgs = [];
1666 $scope.location_cache = {};
1668 $scope.location_list = [];
1669 itemSvc.get_locations(
1670 egCore.org.fullPath( egCore.auth.user().ws_ou(), true )
1671 ).then(function(list){
1672 $scope.location_list = list;
1674 createSimpleUpdateWatcher('location');
1676 $scope.statcat_filter_list = egCore.org.fullPath( egCore.auth.user().ws_ou() );
1678 $scope.statcats = [];
1679 itemSvc.get_statcats(
1680 egCore.org.fullPath( egCore.auth.user().ws_ou(), true )
1681 ).then(function(list){
1682 $scope.statcats = list;
1683 angular.forEach($scope.statcats, function (s) {
1685 if (!$scope.working)
1686 $scope.working = { statcats: {}, statcat_filter: undefined};
1687 if (!$scope.working.statcats)
1688 $scope.working.statcats = {};
1690 $scope.working.statcats[s.id()] = undefined;
1691 createStatcatUpdateWatcher(s.id());
1695 $scope.status_list = [];
1696 itemSvc.get_statuses().then(function(list){
1697 $scope.status_list = list;
1699 createSimpleUpdateWatcher('status');
1701 $scope.circ_modifier_list = [];
1702 itemSvc.get_circ_mods().then(function(list){
1703 $scope.circ_modifier_list = list;
1705 createSimpleUpdateWatcher('circ_modifier');
1707 $scope.circ_type_list = [];
1708 itemSvc.get_circ_types().then(function(list){
1709 $scope.circ_type_list = list;
1711 createSimpleUpdateWatcher('circ_as_type');
1713 $scope.age_protect_list = [];
1714 itemSvc.get_age_protects().then(function(list){
1715 $scope.age_protect_list = list;
1717 createSimpleUpdateWatcher('age_protect');
1719 createSimpleUpdateWatcher('circulate');
1720 createSimpleUpdateWatcher('holdable');
1721 createSimpleUpdateWatcher('fine_level');
1722 createSimpleUpdateWatcher('loan_duration');
1723 createSimpleUpdateWatcher('cost');
1724 createSimpleUpdateWatcher('deposit');
1725 createSimpleUpdateWatcher('deposit_amount');
1726 createSimpleUpdateWatcher('mint_condition');
1727 createSimpleUpdateWatcher('opac_visible');
1728 createSimpleUpdateWatcher('ref');
1730 $scope.suffix_list = [];
1731 itemSvc.get_suffixes(egCore.auth.user().ws_ou()).then(function(list){
1732 $scope.suffix_list = list;
1735 $scope.prefix_list = [];
1736 itemSvc.get_prefixes(egCore.auth.user().ws_ou()).then(function(list){
1737 $scope.prefix_list = list;
1740 $scope.classification_list = [];
1741 itemSvc.get_classifications().then(function(list){
1742 $scope.classification_list = list;
1745 createSimpleUpdateWatcher('working.callnumber.classification');
1746 createSimpleUpdateWatcher('working.callnumber.prefix');
1747 createSimpleUpdateWatcher('working.callnumber.suffix');