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',
32 function(egCore , $q) {
35 tree : {}, // holds lib->cn->copy hash stack
36 copies : [] // raw copy list
39 // returns a promise resolved with the list of circ mods
40 service.get_classifications = function() {
42 return $q.when(egCore.env.acnc.list);
44 return egCore.pcrud.retrieveAll('acnc', null, {atomic : true})
45 .then(function(list) {
46 egCore.env.absorbList(list, 'acnc');
51 service.get_prefixes = function(org) {
53 return $q.when(egCore.env.acnp.list);
55 return egCore.pcrud.search('acnp',
56 {owning_lib : egCore.org.fullPath(org, true)},
58 ).then(function(list) {
59 egCore.env.absorbList(list, 'acnp');
65 service.get_locations = function(orgs) {
66 return egCore.pcrud.search('acpl',
72 service.get_suffixes = function(org) {
74 return $q.when(egCore.env.acns.list);
76 return egCore.pcrud.search('acns',
77 {owning_lib : egCore.org.fullPath(org, true)},
79 ).then(function(list) {
80 egCore.env.absorbList(list, 'acns');
86 service.get_statuses = function() {
88 return $q.when(egCore.env.ccs.list);
90 return egCore.pcrud.retrieveAll('ccs', {}, {atomic : true}).then(
92 egCore.env.absorbList(list, 'ccs');
99 service.get_circ_mods = function() {
101 return $q.when(egCore.env.ccm.list);
103 return egCore.pcrud.retrieveAll('ccm', {}, {atomic : true}).then(
105 egCore.env.absorbList(list, 'ccm');
112 service.get_circ_types = function() {
114 return $q.when(egCore.env.citm.list);
116 return egCore.pcrud.retrieveAll('citm', {}, {atomic : true}).then(
118 egCore.env.absorbList(list, 'citm');
125 service.get_age_protects = function() {
126 if (egCore.env.crahp)
127 return $q.when(egCore.env.crahp.list);
129 return egCore.pcrud.retrieveAll('crahp', {}, {atomic : true}).then(
131 egCore.env.absorbList(list, 'crahp');
138 service.bmp_parts = {};
139 service.get_parts = function(rec) {
140 if (service.bmp_parts[rec])
141 return $q.when(service.bmp_parts[rec]);
143 return egCore.pcrud.search('bmp',
145 null, {atomic : true}
146 ).then(function(list) {
147 service.bmp_parts[rec] = list;
156 acp : ['call_number','parts'],
157 acn : ['label_class','prefix','suffix']
161 service.addCopy = function (cp) {
163 if (!cp.parts()) cp.parts([]); // just in case...
165 var lib = cp.call_number().owning_lib();
166 var cn = cp.call_number().id();
168 if (!service.tree[lib]) service.tree[lib] = {};
169 if (!service.tree[lib][cn]) service.tree[lib][cn] = [];
171 service.tree[lib][cn].push(cp);
172 service.copies.push(cp);
175 service.fetchIds = function(idList) {
176 service.tree = {}; // clear the tree on fetch
177 service.copies = []; // clear the copy list on fetch
178 return egCore.pcrud.search('acp', { 'id' : idList }, service.flesh).then(null,null,
180 service.addCopy(copy);
188 .directive("egVolCopyEdit", function () {
194 '<div class="col-xs-6"><input type="text" ng-model="barcode" ng-change="updateBarcode()"/></div>'+
195 '<div class="col-xs-2"><input type="number" ng-model="copy_number" ng-change="updateCopyNo()"/></div>'+
196 '<div class="col-xs-4"><eg-basic-combo-box list="parts" selected="part"></eg-basic-combo-box></div>'+
199 scope: { copy: "=", callNumber: "=" },
200 controller : ['$scope','itemSvc',
201 function ( $scope , itemSvc ) {
202 $scope.new_part_id = 0;
204 $scope.updateBarcode = function () { $scope.copy.barcode($scope.barcode); $scope.copy.ischanged(1); };
205 $scope.updateCopyNo = function () { $scope.copy.copy_number($scope.copy_number); $scope.copy.ischanged(1); };
206 $scope.updatePart = function () {
207 var p = angular.filter($scope.part_list, function (x) {
208 return x.label() == $scope.part
210 if (p.length > 0) { // preexisting part
212 } else { // create one...
213 var part = new egCore.idl.bmp();
214 part.id( --$scope.new_part_id );
216 part.label( $scope.part );
217 part.record( $scope.callNumber.owning_lib() );
218 $scope.copy.parts([part]);
219 $scope.copy.ischanged(1);
223 $scope.barcode = $scope.copy.barcode();
224 $scope.copy_number = $scope.copy.copy_number();
226 if ($scope.copy.parts()) {
227 $scope.part = $scope.copy.parts()[0];
228 if ($scope.part) $scope.part = $scope.part.label();
232 $scope.part_list = [];
234 itemSvc.get_parts($scope.callNumber.record()).then(function(list){
235 $scope.part_list = list;
236 angular.forEach(list, function(p){ $scope.parts.push(p.label()) });
245 .directive("egVolRow", function () {
252 '<div class="col-xs-1">'+
253 '<select ng-model="classification" ng-options="cl.name() for cl in classification_list track by idTracker(cl)"/>'+
255 '<div class="col-xs-1">'+
256 '<select ng-model="prefix" ng-change="updatePrefix()" ng-options="p.label() for p in prefix_list track by idTracker(p)"/>'+
258 '<div class="col-xs-3"><input type="text" ng-change="updateLabel()" ng-model="label"/></div>'+
259 '<div class="col-xs-1">'+
260 '<select ng-model="suffix" ng-change="updateSuffix()" ng-options="s.label() for s in suffix_list track by idTracker(s)"/>'+
262 '<div class="col-xs-1"><input type="number" ng-model="copy_count" min="{{orig_copy_count}}" ng-change="changeCPCount()"></div>'+
263 '<div class="col-xs-5">'+
264 '<div class="container-fluid">'+
265 '<eg-vol-copy-edit ng-repeat="cp in copies track by idTracker(cp)" copy="cp" call-number="callNumber"></eg-vol-copy-edit>'+
270 scope: {allcopies: "=", copies: "=" },
271 controller : ['$scope','itemSvc','egCore',
272 function ( $scope , itemSvc , egCore ) {
273 $scope.new_cp_id = 0;
274 $scope.callNumber = $scope.copies[0].call_number();
276 $scope.idTracker = function (x) { if (x) return x.id() };
278 $scope.suffix_list = [];
279 itemSvc.get_suffixes($scope.callNumber.owning_lib()).then(function(list){
280 $scope.suffix_list = list;
282 $scope.updateSuffix = function () {
283 angular.forEach($scope.copies, function(cp) {
284 cp.call_number().suffix($scope.suffix);
285 cp.call_number().ischanged(1);
289 $scope.prefix_list = [];
290 itemSvc.get_prefixes($scope.callNumber.owning_lib()).then(function(list){
291 $scope.prefix_list = list;
293 $scope.updatePrefix = function () {
294 angular.forEach($scope.copies, function(cp) {
295 cp.call_number().prefix($scope.prefix);
296 cp.call_number().ischanged(1);
300 $scope.classification_list = [];
301 itemSvc.get_classifications().then(function(list){
302 $scope.classification_list = list;
304 $scope.updateClassification = function () {
305 angular.forEach($scope.copies, function(cp) {
306 cp.call_number().label_class($scope.classification);
307 cp.call_number().ischanged(1);
311 $scope.classification = $scope.callNumber.label_class();
312 $scope.prefix = $scope.callNumber.prefix();
313 $scope.suffix = $scope.callNumber.suffix();
315 $scope.label = $scope.callNumber.label();
316 $scope.updateLabel = function () {
317 angular.forEach($scope.copies, function(cp) {
318 cp.call_number().label($scope.label);
319 cp.call_number().ischanged(1);
323 $scope.copy_count = $scope.copies.length;
324 $scope.orig_copy_count = $scope.copy_count;
326 $scope.changeCPCount = function () {
327 while ($scope.copy_count > $scope.copies.length) {
328 var cp = new egCore.idl.acp();
329 cp.id( --$scope.new_cp_id );
331 cp.circ_lib( $scope.lib );
332 cp.call_number( $scope.callNumber );
333 $scope.copies.push( cp );
334 $scope.allcopies.push( cp );
337 var how_many = $scope.copies.length - $scope.copy_count;
339 var dead = $scope.copies.splice($scope.copy_count,how_many);
340 $scope.callNumber.copies($scope.copies);
342 // Trimming the global list is a bit more tricky
343 angular.forEach( dead, function (d) {
344 angular.forEach( $scope.allcopies, function (l, i) {
345 if (l === d) $scope.allcopies.splice(i,1);
357 .directive("egVolEdit", function () {
363 '<div class="col-xs-1"><eg-org-selector selected="owning_lib" disableTest="cant_have_vols"></eg-org-selector></div>'+
364 '<div class="col-xs-1"><input type="number" min="{{orig_cn_count}}" ng-model="cn_count" ng-change="changeCNCount()"/></div>'+
365 '<div class="col-xs-10">'+
366 '<div class="container-fluid">'+
367 '<eg-vol-row ng-repeat="(cn,copies) in struct track by cn" copies="copies" allcopies="allcopies"></eg-vol-row>'+
372 scope: { allcopies: "=", struct: "=", lib: "@", record: "@" },
373 controller : ['$scope','itemSvc','egCore',
374 function ( $scope , itemSvc , egCore ) {
375 $scope.new_cn_id = 0;
376 $scope.first_cn = Object.keys($scope.struct)[0];
377 $scope.full_cn = $scope.struct[$scope.first_cn][0].call_number();
379 $scope.cn_count = Object.keys($scope.struct).length;
380 $scope.orig_cn_count = $scope.cn_count;
382 $scope.owning_lib = egCore.org.get($scope.lib);
383 $scope.$watch('owning_lib', function (l) {
384 angular.forEach( $scope.struct[$scope.first_cn], function (cp) {
385 cp.call_number().owning_lib( $scope.owning_lib.id() );
389 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
391 $scope.$watch('cn_count', function (n) {
392 var o = Object.keys($scope.struct).length;
393 if (n > o) { // adding
394 for (var i = o; o < n; o++) {
395 var cn = new egCore.idl.acn();
396 cn.id( --$scope.new_cn_id );
398 cn.owning_lib( $scope.owning_lib.id() );
399 cn.record( $scope.full_cn.record() );
401 var cp = new egCore.idl.acp();
402 cp.id( --$scope.new_cp_id );
404 cp.circ_lib( $scope.owning_lib.id() );
405 cp.call_number( cn );
407 $scope.struct[cn.id()] = [cp];
408 $scope.allcopies.push(cp);
410 } else if (n < o) { // removing
411 var how_many = o - n;
414 .sort(function(a, b){return a-b})
416 for (var i = how_many; i > 0; i--) {
417 // Trimming the global list is a bit more tricky
418 angular.forEach($scope.struct[list[i]], function (d) {
419 angular.forEach( $scope.allcopies, function (l, j) {
420 if (l === d) $scope.allcopies.splice(j,1);
423 delete $scope.struct[list[i]];
436 .controller('EditCtrl',
437 ['$scope','$q','$routeParams','$location','$timeout','egCore','egNet','egGridDataProvider','itemSvc',
438 function($scope , $q , $routeParams , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc) {
440 $scope.show_vols = true;
441 $scope.show_copies = true;
443 $scope.tracker = function (x,f) { if (x) return x[f]() };
444 $scope.idTracker = function (x) { if (x) return $scope.tracker(x,'id') };
445 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
447 $scope.orgById = function (id) { return egCore.org.get(id) }
448 $scope.statusById = function (id) {
449 return $scope.status_list.filter( function (s) { return s.id() == id } )[0];
451 $scope.locationById = function (id) {
452 return $scope.location_cache[''+id];
455 $scope.workingToComplete = function () {
456 angular.forEach( $scope.workingGridControls.selectedItems(), function (c) {
457 angular.forEach( itemSvc.copies, function (w, i) {
459 $scope.completed_copies = $scope.completed_copies.concat(itemSvc.copies.splice(i,1));
464 $scope.completeToWorking = function () {
465 angular.forEach( $scope.completedGridControls.selectedItems(), function (c) {
466 angular.forEach( $scope.completed_copies, function (w, i) {
468 itemSvc.copies = itemSvc.copies.concat($scope.completed_copies.splice(i,1));
473 createSimpleUpdateWatcher = function (field) {
474 $scope.$watch('working.' + field, function () {
475 var newval = $scope.working[field];
476 if (angular.isObject(newval)) { // we'll use the pkey
477 if (newval.id) newval = newval.id();
478 else if (newval.code) newval = newval.code();
481 if ($scope.workingGridControls && $scope.workingGridControls.selectedItems) {
483 $scope.workingGridControls.selectedItems(),
484 function (cp) { cp[field](newval); cp.ischanged(1); }
492 var dataKey = $routeParams.dataKey;
493 console.debug('dataKey: ' + dataKey);
495 if (dataKey && dataKey.length > 0) {
498 $scope.copytab = 'working';
500 $scope.summaryRecord = null;
501 $scope.record_id = null;
503 $scope.completed_copies = [];
504 $scope.location_orgs = [];
505 $scope.location_cache = {};
507 $scope.completedGridDataProvider = egGridDataProvider.instance({
508 get : function(offset, count) {
509 //return provider.arrayNotifier(itemSvc.copies, offset, count);
510 return this.arrayNotifier($scope.completed_copies, offset, count);
514 $scope.completedGridControls = {};
516 $scope.workingGridDataProvider = egGridDataProvider.instance({
517 get : function(offset, count) {
518 //return provider.arrayNotifier(itemSvc.copies, offset, count);
519 return this.arrayNotifier(itemSvc.copies, offset, count);
523 $scope.workingGridControls = {};
527 'open-ils.actor.anon_cache.get_value',
528 dataKey, 'edit-these-copies'
529 ).then(function (data) {
531 if (data.hide_vols) $scope.show_vols = false;
532 if (data.hide_copies) $scope.show_copies = false;
534 $scope.record_id = data.record_id;
536 if (data.copies && data.copies.length)
537 return itemSvc.fetchIds(data.copies);
539 if (data.raw && data.raw.length) {
541 /* data.raw must be an array of copies with (at least)
542 * the call number fleshed on each. For new copies
543 * create from whole cloth, the id for each should
544 * probably be negative and isnew() should return true.
545 * Each /distinct/ call number must have a distinct id
546 * as well, probably negative also if they're new. Clear?
551 function (cp) { itemSvc.addCopy(cp) }
554 return itemSvc.copies;
557 }).then( function() {
558 $scope.data = itemSvc;
559 $scope.workingGridDataProvider.refresh();
562 $scope.$watch('data.copies.length', function () {
563 if ($scope.data.copies) {
564 var base_orgs = $scope.data.copies.map(function(cp){
566 }).filter(function(e,i,a){
567 return a.lastIndexOf(e) === i;
571 angular.forEach(base_orgs, function(o) {
572 all_orgs = all_orgs.concat( egCore.org.fullPath(o, true) );
575 var final_orgs = all_orgs.filter(function(e,i,a){
576 return a.lastIndexOf(e) === i;
577 }).sort(function(a,b){return b-a});
579 if ($scope.location_orgs.toString() != final_orgs.toString()) {
580 $scope.location_orgs = final_orgs;
581 if ($scope.location_orgs.length) {
582 itemSvc.get_locations($scope.location_orgs).then(function(list){
583 angular.forEach(list, function(l) {
584 $scope.location_cache[ ''+l.id() ] = l;
586 $scope.location_list = list;
592 $scope.workingGridDataProvider.refresh();
595 $scope.$watch('completed_copies.length', function () {
596 $scope.completedGridDataProvider.refresh();
599 $scope.location_list = [];
600 itemSvc.get_locations().then(function(list){
601 $scope.location_list = list;
603 createSimpleUpdateWatcher('location');
605 $scope.status_list = [];
606 itemSvc.get_statuses().then(function(list){
607 $scope.status_list = list;
609 createSimpleUpdateWatcher('status');
611 $scope.circ_mod_list = [];
612 itemSvc.get_circ_mods().then(function(list){
613 $scope.circ_mod_list = list;
615 createSimpleUpdateWatcher('circ_modifier');
617 $scope.circ_type_list = [];
618 itemSvc.get_circ_types().then(function(list){
619 $scope.circ_type_list = list;
621 createSimpleUpdateWatcher('circ_as_type');
623 $scope.age_protect_list = [];
624 itemSvc.get_age_protects().then(function(list){
625 $scope.age_protect_list = list;
627 createSimpleUpdateWatcher('age_protect');
629 createSimpleUpdateWatcher('circulate');
630 createSimpleUpdateWatcher('holdable');
631 createSimpleUpdateWatcher('fine_level');
632 createSimpleUpdateWatcher('loan_duration');
633 createSimpleUpdateWatcher('cost');
634 createSimpleUpdateWatcher('deposit');
635 createSimpleUpdateWatcher('deposit_amount');
636 createSimpleUpdateWatcher('mint_condition');
637 createSimpleUpdateWatcher('opac_visible');
638 createSimpleUpdateWatcher('ref');