]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/in_house_use/app.js
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};
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.checkout = function(args) {
66         $scope.copyNotFound = false;
67
68         var coArgs = {
69             count : args.num_uses,
70             'location' : egCore.auth.user().ws_ou()
71         };
72
73         if (args.noncat_type == 'barcode') {
74
75             egCore.pcrud.search('acp',
76                 {barcode : args.barcode, deleted : 'f'},
77                 {   flesh : 3, 
78                     flesh_fields : {
79                         acp : ['call_number','location'],
80                         acn : ['record'],
81                         bre : ['simple_record']
82                     },
83                     select : { bre : ['id'] } // avoid fleshing MARC
84                 }
85             ).then(function(copy) {
86
87                 if (!copy) {
88                     egCore.audio.play('error.in_house.copy_not_found');
89                     $scope.copyNotFound = true;
90                     return;
91                 }
92
93                 coArgs.copyid = copy.id();
94
95                 // LP1507807: Display the copy alert if the setting is on.
96                 if ($scope.copyAlert && copy.alert_message()) {
97                     egAlertDialog.open(copy.alert_message()).result;
98                 }
99
100                 // LP1507807: Display the location alert if the setting is on.
101                 if ($scope.checkinAlert && copy.location().checkin_alert() == 't') {
102                     egAlertDialog.open(egCore.strings.LOCATION_ALERT_MSG, {copy: copy}).result;
103                 }
104
105                 performCheckout(
106                     'open-ils.circ.in_house_use.create',
107                     coArgs, {copy:copy}
108                 );
109             });
110
111         } else {
112             coArgs.non_cat_type = args.noncat_type;
113             performCheckout(
114                 'open-ils.circ.non_cat_in_house_use.create',
115                 coArgs, {title : $scope.selectedNcType()}
116             );
117         }
118         $scope.args.barcode='';
119     }
120
121     function performCheckout(method, args, data) {
122
123         // FIXME: make this API stream
124         egCore.net.request(
125             'open-ils.circ', method, egCore.auth.token(), args
126
127         ).then(function(resp) {
128             if (evt = egCore.evt.parse(resp)) {
129                 egCore.audio.play('error.in_house');
130                 return alert(evt);
131             }
132
133             egCore.audio.play('success.in_house');
134
135             var item = {num_uses : resp.length};
136             item.copy = data.copy;
137             item.title = data.title || 
138                 data.copy.call_number().record().simple_record().title();
139             item.index = checkouts.length;
140
141             checkouts.unshift(item);
142             provider.refresh();
143         });
144     }
145
146     $scope.print_list = function() {
147         var print_data = { in_house_uses : [] };
148
149         if (checkouts.length == 0) return $q.when();
150
151         angular.forEach(checkouts, function(ihu) {
152             print_data.in_house_uses.push({
153                 num_uses : ihu.num_uses,
154                 copy : egCore.idl.toHash(ihu.copy),
155                 title : ihu.title
156             })
157         });
158
159         return egCore.print.print({
160             template : 'in_house_use_list',
161             scope : print_data
162         });
163     }
164
165 }])