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