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 list="parts" selected="part"></eg-basic-combo-box></div>'+
258 scope: { focusNext: "=", copy: "=", callNumber: "=", index: "@" },
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 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 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 class="form-control" type="text" ng-change="updateLabel()" ng-model="label"/></div>'+
337 '<div class="col-xs-1">'+
338 '<select 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 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 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: "=" },
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);
424 $scope.classification_list = [];
425 itemSvc.get_classifications().then(function(list){
426 $scope.classification_list = list;
427 $scope.$watch('callNumber.label_class()', function (v) {
428 if (angular.isObject(v)) v = v.id();
429 $scope.classification = $scope.classification_list.filter(function (c) {
435 $scope.updateClassification = function () {
436 angular.forEach($scope.copies, function(cp) {
437 cp.call_number().label_class($scope.classification);
438 cp.call_number().ischanged(1);
442 $scope.updateLabel = function () {
443 angular.forEach($scope.copies, function(cp) {
444 cp.call_number().label($scope.label);
445 cp.call_number().ischanged(1);
449 $scope.$watch('callNumber.label()', function (v) {
453 $scope.prefix = $scope.callNumber.prefix();
454 $scope.suffix = $scope.callNumber.suffix();
455 $scope.classification = $scope.callNumber.label_class();
456 $scope.label = $scope.callNumber.label();
458 $scope.copy_count = $scope.copies.length;
459 $scope.orig_copy_count = $scope.copy_count;
461 $scope.changeCPCount = function () {
462 while ($scope.copy_count > $scope.copies.length) {
463 var cp = new egCore.idl.acp();
464 cp.id( --itemSvc.new_cp_id );
466 cp.circ_lib( $scope.lib );
467 cp.call_number( $scope.callNumber );
468 $scope.copies.push( cp );
469 $scope.allcopies.push( cp );
472 if ($scope.copy_count >= $scope.orig_copy_count) {
473 var how_many = $scope.copies.length - $scope.copy_count;
475 var dead = $scope.copies.splice($scope.copy_count,how_many);
476 $scope.callNumber.copies($scope.copies);
478 // Trimming the global list is a bit more tricky
479 angular.forEach( dead, function (d) {
480 angular.forEach( $scope.allcopies, function (l, i) {
481 if (l === d) $scope.allcopies.splice(i,1);
494 .directive("egVolEdit", function () {
500 '<div class="col-xs-1"><eg-org-selector selected="owning_lib" disableTest="cant_have_vols"></eg-org-selector></div>'+
501 '<div class="col-xs-1"><input class="form-control" type="number" min="{{orig_cn_count}}" ng-model="cn_count" ng-change="changeCNCount()"/></div>'+
502 '<div class="col-xs-10">'+
503 '<eg-vol-row only-vols="onlyVols"'+
504 'ng-repeat="(cn,copies) in struct | orderBy:cn track by cn" '+
505 'focus-next="focusNextFirst" copies="copies" allcopies="allcopies">'+
510 scope: { focusNext: "=", allcopies: "=", struct: "=", lib: "@", record: "@", onlyVols: "=" },
511 controller : ['$scope','itemSvc','egCore',
512 function ( $scope , itemSvc , egCore ) {
513 $scope.first_cn = Object.keys($scope.struct)[0];
514 $scope.full_cn = $scope.struct[$scope.first_cn][0].call_number();
516 $scope.defaults = {};
517 egCore.hatch.getItem('cat.copy.defaults').then(function(t) {
523 $scope.focusNextFirst = function(prev_cn,prev_bc) {
526 angular.forEach(Object.keys($scope.struct).sort(), function (cn) {
538 var next = '#' + n + '_' + $scope.struct[n][0].id();
541 if (!itemSvc.currently_generating) el.focus();
542 if (prev_bc && itemSvc.auto_gen_barcode && el.val() == "") {
543 itemSvc.nextBarcode(prev_bc).then(function(bc){
546 el.trigger('change');
549 itemSvc.currently_generating = false;
553 $scope.focusNext($scope.lib, prev_bc);
557 $scope.cn_count = Object.keys($scope.struct).length;
558 $scope.orig_cn_count = $scope.cn_count;
560 $scope.owning_lib = egCore.org.get($scope.lib);
561 $scope.$watch('owning_lib', function (l) {
562 angular.forEach( $scope.struct[$scope.first_cn], function (cp) {
563 cp.call_number().owning_lib( $scope.owning_lib.id() );
567 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
569 $scope.$watch('cn_count', function (n) {
570 var o = Object.keys($scope.struct).length;
571 if (n > o) { // adding
572 for (var i = o; o < n; o++) {
573 var cn = new egCore.idl.acn();
574 cn.id( --itemSvc.new_cn_id );
576 cn.prefix( $scope.defaults.prefix || -1 );
577 cn.suffix( $scope.defaults.suffix || -1 );
578 cn.label_class( $scope.defaults.classification || 1 );
579 cn.owning_lib( $scope.owning_lib.id() );
580 cn.record( $scope.full_cn.record() );
582 var cp = new egCore.idl.acp();
583 cp.call_number( cn );
584 cp.id( --itemSvc.new_cp_id );
589 cp.deposit_amount(0);
590 cp.fine_level(2); // Normal
591 cp.loan_duration(2); // Normal
592 cp.location(1); // Stacks
595 cp.opac_visible('t');
597 cp.mint_condition('t');
599 cp.circ_lib( $scope.owning_lib.id() );
600 cp.call_number( cn );
602 $scope.struct[cn.id()] = [cp];
603 $scope.allcopies.push(cp);
605 } else if (n < o && n >= $scope.orig_cn_count) { // removing
606 var how_many = o - n;
609 .sort(function(a, b){return parseInt(a)-parseInt(b)})
610 .filter(function(x){ return parseInt(x) <= 0 });
611 for (var i = 0; i < how_many; i++) {
612 // Trimming the global list is a bit more tricky
613 angular.forEach($scope.struct[list[i]], function (d) {
614 angular.forEach( $scope.allcopies, function (l, j) {
615 if (l === d) $scope.allcopies.splice(j,1);
618 delete $scope.struct[list[i]];
631 .controller('EditCtrl',
632 ['$scope','$q','$window','$routeParams','$location','$timeout','egCore','egNet','egGridDataProvider','itemSvc','$modal',
633 function($scope , $q , $window , $routeParams , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc , $modal) {
635 $scope.defaults = { // If defaults are not set at all, allow everything
636 barcode_checkdigit : false,
637 auto_gen_barcode : false,
642 loan_duration : true,
647 deposit_amount : true,
651 mint_condition : true,
654 circ_modifier : true,
662 $scope.embedded = ($routeParams.mode && $routeParams.mode == 'embedded') ? true : false;
664 $scope.saveDefaults = function () {
665 egCore.hatch.setItem('cat.copy.defaults', $scope.defaults);
668 $scope.fetchDefaults = function () {
669 egCore.hatch.getItem('cat.copy.defaults').then(function(t) {
672 if (!$scope.batch) $scope.batch = {};
673 $scope.batch.classification = $scope.defaults.classification;
674 $scope.batch.prefix = $scope.defaults.prefix;
675 $scope.batch.suffix = $scope.defaults.suffix;
676 $scope.working.statcat_filter = $scope.defaults.statcat_filter;
677 if ($scope.defaults.always_vols) $scope.show_vols = true;
678 if ($scope.defaults.barcode_checkdigit) itemSvc.barcode_checkdigit = true;
679 if ($scope.defaults.auto_gen_barcode) itemSvc.auto_gen_barcode = true;
683 $scope.fetchDefaults();
685 $scope.$watch('defaults.auto_gen_barcode', function (n,o) {
686 itemSvc.auto_gen_barcode = n
689 $scope.$watch('defaults.barcode_checkdigit', function (n,o) {
690 itemSvc.barcode_checkdigit = n
693 $scope.dirty = false;
694 $scope.$watch('dirty',
695 function(newVal, oldVal) {
696 if (newVal && newVal != oldVal) {
697 $($window).on('beforeunload.edit', function(){
698 return 'There is unsaved data!'
701 $($window).off('beforeunload.edit');
706 $scope.only_vols = false;
707 $scope.show_vols = true;
708 $scope.show_copies = true;
710 $scope.tracker = function (x,f) { if (x) return x[f]() };
711 $scope.idTracker = function (x) { if (x) return $scope.tracker(x,'id') };
712 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
714 $scope.orgById = function (id) { return egCore.org.get(id) }
715 $scope.statusById = function (id) {
716 return $scope.status_list.filter( function (s) { return s.id() == id } )[0];
718 $scope.locationById = function (id) {
719 return $scope.location_cache[''+id];
722 $scope.workingToComplete = function () {
723 angular.forEach( $scope.workingGridControls.selectedItems(), function (c) {
724 angular.forEach( itemSvc.copies, function (w, i) {
726 $scope.completed_copies = $scope.completed_copies.concat(itemSvc.copies.splice(i,1));
733 $scope.completeToWorking = function () {
734 angular.forEach( $scope.completedGridControls.selectedItems(), function (c) {
735 angular.forEach( $scope.completed_copies, function (w, i) {
737 itemSvc.copies = itemSvc.copies.concat($scope.completed_copies.splice(i,1));
744 createSimpleUpdateWatcher = function (field) {
745 return $scope.$watch('working.' + field, function () {
746 var newval = $scope.working[field];
748 if (typeof newval != 'undefined') {
749 if (angular.isObject(newval)) { // we'll use the pkey
750 if (newval.id) newval = newval.id();
751 else if (newval.code) newval = newval.code();
754 if (""+newval == "" || newval == null) {
755 $scope.working[field] = undefined;
759 if ($scope.workingGridControls && $scope.workingGridControls.selectedItems) {
761 $scope.workingGridControls.selectedItems(),
763 if (cp[field]() !== newval) {
777 statcat_filter: undefined
780 $scope.statcatUpdate = function (id) {
781 var newval = $scope.working.statcats[id];
783 if (typeof newval != 'undefined') {
784 if (angular.isObject(newval)) { // we'll use the pkey
785 newval = newval.id();
788 if (""+newval == "" || newval == null) {
789 $scope.working.statcats[id] = undefined;
793 if (!$scope.in_item_select && $scope.workingGridControls && $scope.workingGridControls.selectedItems) {
795 $scope.workingGridControls.selectedItems(),
800 angular.forEach( cp.stat_cat_entries(), function (e) {
801 if (e.stat_cat() == id) { // mark deleted
808 var e = new egCore.idl.ascecm();
810 e.owning_copy( cp.id() );
812 e.stat_cat_entry( newval );
815 cp.stat_cat_entries().concat([ e ])
820 cp.stat_cat_entries( // trim out ephemeral deleted ones
821 cp.stat_cat_entries().filter(function (e) {
822 if (Boolean(e.isnew())) {
823 if (Boolean(e.isdeleted())) {
838 var dataKey = $routeParams.dataKey;
839 console.debug('dataKey: ' + dataKey);
841 if (dataKey && dataKey.length > 0) {
843 $scope.templates = {};
844 $scope.template_name = '';
845 $scope.template_name_list = [];
847 $scope.fetchTemplates = function () {
848 egCore.hatch.getItem('cat.copy.templates').then(function(t) {
850 $scope.templates = t;
851 $scope.template_name_list = Object.keys(t);
854 egCore.hatch.getItem('cat.copy.last_template').then(function(t) {
855 if (t) $scope.template_name = t;
858 $scope.fetchTemplates();
860 $scope.applyTemplate = function (n) {
861 angular.forEach($scope.templates[n], function (v,k) {
862 if (k == 'circ_lib') {
863 $scope.working[k] = egCore.org.get(v);
864 } else if (!angular.isObject(v)) {
865 $scope.working[k] = angular.copy(v);
867 angular.forEach(v, function (sv,sk) {
868 if (k == 'callnumber') {
869 angular.forEach(v, function (cnv,cnk) {
870 $scope.batch[cnk] = cnv;
872 $scope.applyBatchCNValues();
874 $scope.working[k][sk] = angular.copy(sv);
875 if (k == 'statcats') $scope.statcatUpdate(sk);
880 egCore.hatch.setItem('cat.copy.last_template', n);
883 $scope.copytab = 'working';
885 $scope.summaryRecord = null;
886 $scope.record_id = null;
888 $scope.completed_copies = [];
889 $scope.location_orgs = [];
890 $scope.location_cache = {};
891 $scope.statcats = [];
892 if (!$scope.batch) $scope.batch = {};
894 $scope.applyBatchCNValues = function () {
895 if ($scope.data.tree) {
896 angular.forEach($scope.data.tree, function(cn_hash) {
897 angular.forEach(cn_hash, function(copies) {
898 angular.forEach(copies, function(cp) {
899 if (typeof $scope.batch.classification != 'undefined' && $scope.batch.classification != '') {
900 var label_class = $scope.classification_list.filter(function(p){ return p.id() == $scope.batch.classification })[0];
901 cp.call_number().label_class(label_class);
902 cp.call_number().ischanged(1);
905 if (typeof $scope.batch.prefix != 'undefined' && $scope.batch.prefix != '') {
906 var prefix = $scope.prefix_list.filter(function(p){ return p.id() == $scope.batch.prefix })[0];
907 cp.call_number().prefix(prefix);
908 cp.call_number().ischanged(1);
911 if (typeof $scope.batch.label != 'undefined' && $scope.batch.label != '') {
912 cp.call_number().label($scope.batch.label);
913 cp.call_number().ischanged(1);
916 if (typeof $scope.batch.suffix != 'undefined' && $scope.batch.suffix != '') {
917 var suffix = $scope.suffix_list.filter(function(p){ return p.id() == $scope.batch.suffix })[0];
918 cp.call_number().suffix(suffix);
919 cp.call_number().ischanged(1);
928 $scope.clearWorking = function () {
929 angular.forEach($scope.working, function (v,k,o) {
930 if (!angular.isObject(v)) {
931 if (typeof v != 'undefined')
932 $scope.working[k] = undefined;
933 } else if (k != 'circ_lib') {
934 angular.forEach(v, function (sv,sk) {
935 if (typeof v != 'undefined')
936 $scope.working[k][sk] = undefined;
940 $scope.working.circ_lib = undefined; // special
943 $scope.completedGridDataProvider = egGridDataProvider.instance({
944 get : function(offset, count) {
945 //return provider.arrayNotifier(itemSvc.copies, offset, count);
946 return this.arrayNotifier($scope.completed_copies, offset, count);
950 $scope.completedGridControls = {};
952 $scope.workingGridDataProvider = egGridDataProvider.instance({
953 get : function(offset, count) {
954 //return provider.arrayNotifier(itemSvc.copies, offset, count);
955 return this.arrayNotifier(itemSvc.copies, offset, count);
959 $scope.workingGridControls = {};
960 $scope.add_vols_copies = false;
964 'open-ils.actor.anon_cache.get_value',
965 dataKey, 'edit-these-copies'
966 ).then(function (data) {
969 if (data.hide_vols && !$scope.defaults.always_vols) $scope.show_vols = false;
970 if (data.hide_copies) {
971 $scope.show_copies = false;
972 $scope.only_vols = true;
975 $scope.record_id = data.record_id;
977 function fetchRaw () {
978 if (!$scope.only_vols) $scope.dirty = true;
979 $scope.add_vols_copies = true;
981 /* data.raw data structure looks like this:
983 * callnumber : $cn_id, // optional, to add a copy to a cn
984 * owner : $org, // optional, defaults to ws_ou
985 * label : $cn_label, // optional, to supply a label on a new cn
986 * barcode : $cp_barcode // optional, to supply a barcode on a new cp
989 * All can be left out and a completely empty vol/copy combo will be vivicated.
995 if (proto.callnumber) {
996 return egCore.pcrud.retrieve('acn', proto.callnumber)
998 var cp = new egCore.idl.acp();
999 cp.call_number( cn );
1000 cp.id( --itemSvc.new_cp_id );
1001 if (!$scope.only_vols) cp.isnew( true );
1002 cp.circ_lib( proto.owner || egCore.auth.user().ws_ou() );
1006 cp.deposit_amount(0);
1007 cp.fine_level(2); // Normal
1008 cp.loan_duration(2); // Normal
1009 cp.location(1); // Stacks
1012 cp.opac_visible('t');
1014 cp.mint_condition('t');
1016 if (proto.barcode) cp.barcode( proto.barcode );
1021 var cn = new egCore.idl.acn();
1022 cn.id( --itemSvc.new_cn_id );
1024 cn.prefix( $scope.defaults.prefix || -1 );
1025 cn.suffix( $scope.defaults.suffix || -1 );
1026 cn.label_class( $scope.defaults.classification || 1 );
1027 cn.owning_lib( proto.owner || egCore.auth.user().ws_ou() );
1028 cn.record( $scope.record_id );
1029 if (proto.label) cn.label( proto.label );
1031 var cp = new egCore.idl.acp();
1032 cp.call_number( cn );
1033 cp.id( --itemSvc.new_cp_id );
1038 cp.deposit_amount(0);
1039 cp.fine_level(2); // Normal
1040 cp.loan_duration(2); // Normal
1041 cp.location(1); // Stacks
1044 cp.opac_visible('t');
1046 cp.mint_condition('t');
1048 cp.circ_lib( proto.owner || egCore.auth.user().ws_ou() );
1049 if (proto.barcode) cp.barcode( proto.barcode );
1057 return itemSvc.copies;
1060 if (data.copies && data.copies.length)
1061 return itemSvc.fetchIds(data.copies).then(fetchRaw);
1067 }).then( function() {
1068 $scope.data = itemSvc;
1069 if ($scope.add_vols_copies) {
1070 egCore.org.settings([
1071 'cat.default_copy_status_fast'
1072 ]).then(function(set) {
1073 $scope.fast_ccs = set['cat.default_copy_status_fast'] || 0;
1074 angular.forEach($scope.data.copies, function (cp) {
1075 cp.status($scope.fast_ccs);
1077 $scope.workingGridDataProvider.refresh();
1082 $scope.focusNextFirst = function(prev_lib,prev_bc) {
1085 angular.forEach(Object.keys($scope.data.tree).sort(), function (lib) {
1088 if (lib == prev_lib) {
1097 var first_cn = Object.keys($scope.data.tree[n])[0];
1098 var next = '#' + first_cn + '_' + $scope.data.tree[n][first_cn][0].id();
1101 if (!itemSvc.currently_generating) el.focus();
1102 if (prev_bc && itemSvc.auto_gen_barcode && el.val() == "") {
1103 itemSvc.nextBarcode(prev_bc).then(function(bc){
1106 el.trigger('change');
1109 itemSvc.currently_generating = false;
1115 $scope.in_item_select = false;
1116 $scope.afterItemSelect = function() { $scope.in_item_select = false };
1117 $scope.handleItemSelect = function (item_list) {
1118 if (item_list && item_list.length > 0) {
1119 $scope.in_item_select = true;
1121 angular.forEach(Object.keys($scope.defaults.attributes), function (attr) {
1123 var value_hash = {};
1124 angular.forEach(item_list, function (item) {
1126 var v = item[attr]()
1127 if (angular.isObject(v)) {
1128 if (v.id) v = v.id();
1129 else if (v.code) v = v.code();
1135 if (Object.keys(value_hash).length == 1) {
1136 if (attr == 'circ_lib') {
1137 $scope.working[attr] = egCore.org.get(item_list[0][attr]());
1139 $scope.working[attr] = item_list[0][attr]();
1142 $scope.working[attr] = undefined;
1146 angular.forEach($scope.statcats, function (sc) {
1149 var value_hash = {};
1151 angular.forEach(item_list, function (item) {
1152 if (item.stat_cat_entries()) {
1153 if (item.stat_cat_entries().length > 0) {
1154 var right_sc = item.stat_cat_entries().filter(function (e) {
1155 return e.stat_cat() == sc.id() && !Boolean(e.isdeleted());
1158 if (right_sc.length > 0) {
1159 value_hash[right_sc[0].stat_cat_entry()] = right_sc[0].stat_cat_entry();
1169 if (!none && Object.keys(value_hash).length == 1) {
1170 $scope.working.statcats[sc.id()] = value_hash[Object.keys(value_hash)[0]];
1172 $scope.working.statcats[sc.id()] = undefined;
1177 $scope.clearWorking();
1182 $scope.$watch('data.copies.length', function () {
1183 if ($scope.data.copies) {
1184 var base_orgs = $scope.data.copies.map(function(cp){
1185 return cp.circ_lib()
1187 $scope.data.copies.map(function(cp){
1188 return cp.call_number().owning_lib()
1191 [egCore.auth.user().ws_ou()]
1192 ).filter(function(e,i,a){
1193 return a.lastIndexOf(e) === i;
1197 angular.forEach(base_orgs, function(o) {
1198 all_orgs = all_orgs.concat( egCore.org.fullPath(o, true) );
1201 var final_orgs = all_orgs.filter(function(e,i,a){
1202 return a.lastIndexOf(e) === i;
1203 }).sort(function(a, b){return parseInt(a)-parseInt(b)});
1205 if ($scope.location_orgs.toString() != final_orgs.toString()) {
1206 $scope.location_orgs = final_orgs;
1207 if ($scope.location_orgs.length) {
1208 itemSvc.get_locations($scope.location_orgs).then(function(list){
1209 angular.forEach(list, function(l) {
1210 $scope.location_cache[ ''+l.id() ] = l;
1212 $scope.location_list = list;
1215 $scope.statcat_filter_list = [];
1216 angular.forEach($scope.location_orgs, function (o) {
1217 $scope.statcat_filter_list.push(egCore.org.get(o));
1220 itemSvc.get_statcats($scope.location_orgs).then(function(list){
1221 $scope.statcats = list;
1222 angular.forEach($scope.statcats, function (s) {
1224 if (!$scope.working)
1225 $scope.working = { statcats: {}, statcat_filter: undefined};
1226 if (!$scope.working.statcats)
1227 $scope.working.statcats = {};
1229 if (!$scope.in_item_select) {
1230 $scope.working.statcats[s.id()] = undefined;
1232 createStatcatUpdateWatcher(s.id());
1234 $scope.in_item_select = false;
1240 $scope.workingGridDataProvider.refresh();
1243 $scope.suffix_list = [];
1244 itemSvc.get_suffixes(egCore.auth.user().ws_ou()).then(function(list){
1245 $scope.suffix_list = list;
1248 $scope.prefix_list = [];
1249 itemSvc.get_prefixes(egCore.auth.user().ws_ou()).then(function(list){
1250 $scope.prefix_list = list;
1253 $scope.classification_list = [];
1254 itemSvc.get_classifications().then(function(list){
1255 $scope.classification_list = list;
1258 $scope.$watch('completed_copies.length', function () {
1259 $scope.completedGridDataProvider.refresh();
1262 $scope.location_list = [];
1263 itemSvc.get_locations().then(function(list){
1264 $scope.location_list = list;
1266 createSimpleUpdateWatcher('location');
1268 $scope.status_list = [];
1269 itemSvc.get_statuses().then(function(list){
1270 $scope.status_list = list;
1272 createSimpleUpdateWatcher('status');
1274 $scope.circ_modifier_list = [];
1275 itemSvc.get_circ_mods().then(function(list){
1276 $scope.circ_modifier_list = list;
1278 createSimpleUpdateWatcher('circ_modifier');
1280 $scope.circ_type_list = [];
1281 itemSvc.get_circ_types().then(function(list){
1282 $scope.circ_type_list = list;
1284 createSimpleUpdateWatcher('circ_as_type');
1286 $scope.age_protect_list = [];
1287 itemSvc.get_age_protects().then(function(list){
1288 $scope.age_protect_list = list;
1290 createSimpleUpdateWatcher('age_protect');
1292 createSimpleUpdateWatcher('circ_lib');
1293 createSimpleUpdateWatcher('circulate');
1294 createSimpleUpdateWatcher('holdable');
1295 createSimpleUpdateWatcher('fine_level');
1296 createSimpleUpdateWatcher('loan_duration');
1297 createSimpleUpdateWatcher('price');
1298 createSimpleUpdateWatcher('cost');
1299 createSimpleUpdateWatcher('deposit');
1300 createSimpleUpdateWatcher('deposit_amount');
1301 createSimpleUpdateWatcher('mint_condition');
1302 createSimpleUpdateWatcher('opac_visible');
1303 createSimpleUpdateWatcher('ref');
1305 $scope.saveCompletedCopies = function (and_exit) {
1307 var perCnCopies = {};
1308 angular.forEach( egCore.idl.Clone($scope.completed_copies), function (cp) {
1309 var cn_id = cp.call_number().id();
1310 if (!cnHash[cn_id]) {
1311 cnHash[cn_id] = cp.call_number();
1312 perCnCopies[cn_id] = [cp];
1314 perCnCopies[cn_id].push(cp);
1316 cp.call_number(cn_id); // prevent loops in JSON-ification
1317 if (typeof cnHash[cn_id].prefix() == 'object')
1318 cnHash[cn_id].prefix(cnHash[cn_id].prefix().id()); // un-object-ize some fields
1319 if (typeof cnHash[cn_id].suffix() == 'object')
1320 cnHash[cn_id].suffix(cnHash[cn_id].suffix().id()); // un-object-ize some fields
1323 angular.forEach(perCnCopies, function (v, k) {
1324 cnHash[k].copies(v);
1328 angular.forEach(cnHash, function (v, k) {
1334 'open-ils.cat.asset.volume.fleshed.batch.update.override',
1335 egCore.auth.token(), cnList, 1, { auto_merge_vols : 1, create_parts : 1 }
1336 ).then(function(update_count) {
1338 $scope.dirty = false;
1339 $timeout(function(){$window.close()});
1344 $scope.saveAndContinue = function () {
1345 $scope.saveCompletedCopies(false);
1348 $scope.workingSaveAndExit = function () {
1349 $scope.workingToComplete();
1350 $scope.saveAndExit();
1353 $scope.saveAndExit = function () {
1354 $scope.saveCompletedCopies(true);
1359 $scope.copy_notes_dialog = function(copy_list) {
1360 var default_pub = Boolean($scope.defaults.copy_notes_pub);
1361 if (!angular.isArray(copy_list)) copy_list = [copy_list];
1363 return $modal.open({
1364 templateUrl: './cat/volcopy/t_copy_notes',
1367 ['$scope','$modalInstance',
1368 function($scope , $modalInstance) {
1369 $scope.focusNote = true;
1371 creator : egCore.auth.user().id(),
1377 $scope.require_initials = false;
1378 egCore.org.settings([
1379 'ui.staff.require_initials.copy_notes'
1380 ]).then(function(set) {
1381 $scope.require_initials = Boolean(set['ui.staff.require_initials.copy_notes']);
1384 $scope.note_list = [];
1385 if (copy_list.length == 1) {
1386 $scope.note_list = copy_list[0].notes();
1389 $scope.ok = function(note) {
1391 if (note.initials) note.value += ' [' + note.initials + ']';
1392 angular.forEach(copy_list, function (cp) {
1393 var n = new egCore.idl.acpn();
1394 n.creator(note.creator);
1396 n.title(note.title);
1397 n.value(note.value);
1398 n.owning_copy(cp.id());
1399 cp.notes().push( n );
1402 $modalInstance.close();
1405 $scope.cancel = function($event) {
1406 $modalInstance.dismiss();
1407 $event.preventDefault();
1415 .directive("egVolTemplate", function () {
1419 template: '<div ng-include="'+"'/eg/staff/cat/volcopy/t_attr_edit'"+'"></div>',
1421 controller : ['$scope','$window','itemSvc','egCore',
1422 function ( $scope , $window , itemSvc , egCore ) {
1424 $scope.defaults = { // If defaults are not set at all, allow everything
1425 barcode_checkdigit : false,
1426 auto_gen_barcode : false,
1431 loan_duration : true,
1436 deposit_amount : true,
1437 opac_visible : true,
1440 mint_condition : true,
1443 circ_modifier : true,
1444 circ_as_type : true,
1451 $scope.fetchDefaults = function () {
1452 egCore.hatch.getItem('cat.copy.defaults').then(function(t) {
1454 $scope.defaults = t;
1455 $scope.working.statcat_filter = $scope.defaults.statcat_filter;
1459 $scope.fetchDefaults();
1461 $scope.dirty = false;
1462 $scope.$watch('dirty',
1463 function(newVal, oldVal) {
1464 if (newVal && newVal != oldVal) {
1465 $($window).on('beforeunload.template', function(){
1466 return 'There is unsaved template data!'
1469 $($window).off('beforeunload.template');
1474 $scope.template_controls = true;
1476 $scope.fetchTemplates = function () {
1477 egCore.hatch.getItem('cat.copy.templates').then(function(t) {
1479 $scope.templates = t;
1480 $scope.template_name_list = Object.keys(t);
1484 $scope.fetchTemplates();
1486 $scope.applyTemplate = function (n) {
1487 angular.forEach($scope.templates[n], function (v,k) {
1488 if (k == 'circ_lib') {
1489 $scope.working[k] = egCore.org.get(v);
1490 } else if (!angular.isObject(v)) {
1491 $scope.working[k] = angular.copy(v);
1493 angular.forEach(v, function (sv,sk) {
1494 $scope.working[k][sk] = angular.copy(sv);
1498 $scope.template_name = '';
1501 $scope.deleteTemplate = function (n) {
1503 delete $scope.templates[n]
1504 $scope.template_name_list = Object.keys($scope.templates);
1505 $scope.template_name = '';
1506 egCore.hatch.setItem('cat.copy.templates', $scope.templates);
1507 $scope.$parent.fetchTemplates();
1511 $scope.saveTemplate = function (n) {
1515 angular.forEach($scope.working, function (v,k) {
1516 if (angular.isObject(v)) { // we'll use the pkey
1517 if (v.id) v = v.id();
1518 else if (v.code) v = v.code();
1524 $scope.templates[n] = tmpl;
1525 $scope.template_name_list = Object.keys($scope.templates);
1527 egCore.hatch.setItem('cat.copy.templates', $scope.templates);
1528 $scope.$parent.fetchTemplates();
1530 $scope.dirty = false;
1534 $scope.templates = {};
1535 $scope.template_name = '';
1536 $scope.template_name_list = [];
1538 $scope.tracker = function (x,f) { if (x) return x[f]() };
1539 $scope.idTracker = function (x) { if (x) return $scope.tracker(x,'id') };
1540 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
1542 $scope.orgById = function (id) { return egCore.org.get(id) }
1543 $scope.statusById = function (id) {
1544 return $scope.status_list.filter( function (s) { return s.id() == id } )[0];
1546 $scope.locationById = function (id) {
1547 return $scope.location_cache[''+id];
1550 createSimpleUpdateWatcher = function (field) {
1551 $scope.$watch('working.' + field, function () {
1552 var newval = $scope.working[field];
1554 if (typeof newval != 'undefined') {
1555 $scope.dirty = true;
1556 if (angular.isObject(newval)) { // we'll use the pkey
1557 if (newval.id) $scope.working[field] = newval.id();
1558 else if (newval.code) $scope.working[field] = newval.code();
1561 if (""+newval == "" || newval == null) {
1562 $scope.working[field] = undefined;
1571 statcat_filter: undefined
1574 createStatcatUpdateWatcher = function (id) {
1575 return $scope.$watch('working.statcats[' + id + ']', function () {
1576 if ($scope.working.statcats) {
1577 var newval = $scope.working.statcats[id];
1579 if (typeof newval != 'undefined') {
1580 $scope.dirty = true;
1581 if (angular.isObject(newval)) { // we'll use the pkey
1582 newval = newval.id();
1585 if (""+newval == "" || newval == null) {
1586 $scope.working.statcats[id] = undefined;
1595 $scope.clearWorking = function () {
1596 angular.forEach($scope.working, function (v,k,o) {
1597 if (!angular.isObject(v)) {
1598 if (typeof v != 'undefined')
1599 $scope.working[k] = undefined;
1600 } else if (k != 'circ_lib') {
1601 angular.forEach(v, function (sv,sk) {
1602 $scope.working[k][sk] = undefined;
1606 $scope.working.circ_lib = undefined; // special
1607 $scope.dirty = false;
1610 $scope.working = {};
1611 $scope.location_orgs = [];
1612 $scope.location_cache = {};
1614 $scope.location_list = [];
1615 itemSvc.get_locations(
1616 egCore.org.fullPath( egCore.auth.user().ws_ou(), true )
1617 ).then(function(list){
1618 $scope.location_list = list;
1620 createSimpleUpdateWatcher('location');
1622 $scope.statcat_filter_list = egCore.org.fullPath( egCore.auth.user().ws_ou() );
1624 $scope.statcats = [];
1625 itemSvc.get_statcats(
1626 egCore.org.fullPath( egCore.auth.user().ws_ou(), true )
1627 ).then(function(list){
1628 $scope.statcats = list;
1629 angular.forEach($scope.statcats, function (s) {
1631 if (!$scope.working)
1632 $scope.working = { statcats: {}, statcat_filter: undefined};
1633 if (!$scope.working.statcats)
1634 $scope.working.statcats = {};
1636 $scope.working.statcats[s.id()] = undefined;
1637 createStatcatUpdateWatcher(s.id());
1641 $scope.status_list = [];
1642 itemSvc.get_statuses().then(function(list){
1643 $scope.status_list = list;
1645 createSimpleUpdateWatcher('status');
1647 $scope.circ_modifier_list = [];
1648 itemSvc.get_circ_mods().then(function(list){
1649 $scope.circ_modifier_list = list;
1651 createSimpleUpdateWatcher('circ_modifier');
1653 $scope.circ_type_list = [];
1654 itemSvc.get_circ_types().then(function(list){
1655 $scope.circ_type_list = list;
1657 createSimpleUpdateWatcher('circ_as_type');
1659 $scope.age_protect_list = [];
1660 itemSvc.get_age_protects().then(function(list){
1661 $scope.age_protect_list = list;
1663 createSimpleUpdateWatcher('age_protect');
1665 createSimpleUpdateWatcher('circulate');
1666 createSimpleUpdateWatcher('holdable');
1667 createSimpleUpdateWatcher('fine_level');
1668 createSimpleUpdateWatcher('loan_duration');
1669 createSimpleUpdateWatcher('cost');
1670 createSimpleUpdateWatcher('deposit');
1671 createSimpleUpdateWatcher('deposit_amount');
1672 createSimpleUpdateWatcher('mint_condition');
1673 createSimpleUpdateWatcher('opac_visible');
1674 createSimpleUpdateWatcher('ref');
1676 $scope.suffix_list = [];
1677 itemSvc.get_suffixes(egCore.auth.user().ws_ou()).then(function(list){
1678 $scope.suffix_list = list;
1681 $scope.prefix_list = [];
1682 itemSvc.get_prefixes(egCore.auth.user().ws_ou()).then(function(list){
1683 $scope.prefix_list = list;
1686 $scope.classification_list = [];
1687 itemSvc.get_classifications().then(function(list){
1688 $scope.classification_list = list;
1691 createSimpleUpdateWatcher('working.callnumber.classification');
1692 createSimpleUpdateWatcher('working.callnumber.prefix');
1693 createSimpleUpdateWatcher('working.callnumber.suffix');