LP#1727487 Webstaff In-House-Use uses display fields
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / in_house_use / app.js
1 angular.module('egInHouseUseApp', 
2     ['ngRoute', 'egCoreMod', 'egUiMod', 'egGridMod'])
3
4 .config(function($routeProvider, $locationProvider, $compileProvider) {
5     $locationProvider.html5Mode(true);
6     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
7
8 })
9
10 .controller('InHouseUseCtrl',
11        ['$scope','egCore','egGridDataProvider','egConfirmDialog', 
12         'egAlertDialog','egBibDisplay',
13 function($scope , egCore , egGridDataProvider , egConfirmDialog, 
14          egAlertDialog , egBibDisplay) {
15
16     var countCap;
17     var countMax;
18
19     egCore.startup.go().then(function() {
20
21         // grab our non-cat types after startup
22         egCore.pcrud.search('cnct', 
23             {owning_lib : 
24                 egCore.org.fullPath(egCore.auth.user().ws_ou(), true)},
25             null, {atomic : true}
26         ).then(function(list) { 
27             egCore.env.absorbList(list, 'cnct');
28             $scope.nonCatTypes = list 
29         });
30
31         // org settings for max and warning in-house-use counts
32         
33         egCore.org.settings([
34             'ui.circ.in_house_use.entry_cap',
35             'ui.circ.in_house_use.entry_warn',
36             'circ.in_house_use.copy_alert',
37             'circ.in_house_use.checkin_alert'
38         ]).then(function(set) {
39             countWarn = set['ui.circ.in_house_use.entry_warn'] || 20;
40             $scope.countMax = countMax = 
41                 set['ui.circ.in_house_use.entry_cap'] || 99;
42             $scope.copyAlert = copyAlert =
43                 set['circ.in_house_use.copy_alert'] || false;
44             $scope.checkinAlert = checkinAlert =
45                 set['circ.in_house_use.checkin_alert'] || false;
46         });
47     });
48
49     $scope.useFocus = true;
50     $scope.args = {noncat_type : 'barcode', num_uses : 1, needsCountWarnModal: false };
51     var checkouts = [];
52
53     var provider = egGridDataProvider.instance({});
54     provider.get = function(offset, count) {
55         return provider.arrayNotifier(checkouts, offset, count);
56     }
57     $scope.gridDataProvider = provider;
58
59     // currently selected non-cat type
60     $scope.selectedNcType = function() {
61         if (!egCore.env.cnct) return null; // too soon
62         var type = egCore.env.cnct.map[$scope.args.noncat_type];
63         return type ? type.name() : null;
64     }
65
66     $scope.onNumUsesChanged = function(){
67         $scope.args.needsCountWarnModal = countWarn < $scope.args.num_uses;
68     }
69
70     $scope.checkout = function(args){
71         if ($scope.args.needsCountWarnModal) {
72             // show modal to allow warning/confirmation
73             egConfirmDialog.open(egCore.strings.CONFIRM_IN_HOUSE_NUM_USES_COUNT_TITLE, '',
74                 { num_uses: $scope.args.num_uses }
75             ).result.then(function(){
76                 $scope.args.needsCountWarnModal = false
77                 $scope.checkoutStart(args)
78             });
79         } else {
80             $scope.checkoutStart(args);
81         }
82     }
83
84     $scope.checkoutStart = function(args) {
85         $scope.copyNotFound = false;
86
87         var coArgs = {
88             count : args.num_uses,
89             'location' : egCore.auth.user().ws_ou()
90         };
91
92         if (args.noncat_type == 'barcode') {
93
94             egCore.pcrud.search('acp',
95                 {barcode : args.barcode, deleted : 'f'},
96                 {   flesh : 3, 
97                     flesh_fields : {
98                         acp : ['call_number','location'],
99                         acn : ['record', 'prefix', 'suffix'],
100                         // We don't need to display a wide range of bib
101                         // fields in this UI.  Fetch the flat display since
102                         // it requires less DB-side munging (and as an example).  
103                         bre : ['flat_display_entries']
104                     },
105                     select : { bre : ['id'] } // avoid fleshing MARC
106                 }
107             ).then(function(copy) {
108
109                 if (!copy) {
110                     egCore.audio.play('error.in_house.copy_not_found');
111                     $scope.copyNotFound = true;
112                     return;
113                 }
114
115                 coArgs.copyid = copy.id();
116
117                 copy.call_number().record().flat_display_entries(
118                     egBibDisplay.mfdeToHash(
119                         copy.call_number().record().flat_display_entries())
120                 );
121
122                 // LP1507807: Display the copy alert if the setting is on.
123                 if ($scope.copyAlert && copy.alert_message()) {
124                     egAlertDialog.open(copy.alert_message()).result;
125                 }
126
127                 // LP1507807: Display the location alert if the setting is on.
128                 if ($scope.checkinAlert && copy.location().checkin_alert() == 't') {
129                     egAlertDialog.open(egCore.strings.LOCATION_ALERT_MSG, {copy: copy}).result;
130                 }
131
132                 performCheckout(
133                     'open-ils.circ.in_house_use.create',
134                     coArgs, {copy:copy}
135                 );
136             });
137
138         } else {
139             coArgs.non_cat_type = args.noncat_type;
140             performCheckout(
141                 'open-ils.circ.non_cat_in_house_use.create',
142                 coArgs, {title : $scope.selectedNcType()}
143             );
144         }
145         $scope.args.barcode='';
146     }
147
148     function performCheckout(method, args, data) {
149
150         // FIXME: make this API stream
151         egCore.net.request(
152             'open-ils.circ', method, egCore.auth.token(), args
153
154         ).then(function(resp) {
155             if (evt = egCore.evt.parse(resp)) {
156                 egCore.audio.play('error.in_house');
157                 return alert(evt);
158             }
159
160             egCore.audio.play('success.in_house');
161
162             var item = {num_uses : resp.length};
163             item.copy = data.copy;
164             item.title = data.title || 
165                 data.copy.call_number().record().flat_display_entries().title;
166             item.index = checkouts.length;
167
168             checkouts.unshift(item);
169             provider.refresh();
170         });
171     }
172
173     $scope.print_list = function() {
174         var print_data = { in_house_uses : [] };
175
176         if (checkouts.length == 0) return $q.when();
177
178         angular.forEach(checkouts, function(ihu) {
179             print_data.in_house_uses.push({
180                 num_uses : ihu.num_uses,
181                 copy : egCore.idl.toHash(ihu.copy),
182                 title : ihu.title
183             })
184         });
185
186         return egCore.print.print({
187             template : 'in_house_use_list',
188             scope : print_data
189         });
190     }
191
192 }])