]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/in_house_use/app.js
2183d96fe4b39999f6b81f1f01211e1b28749a2a
[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',
13 function($scope,  egCore,  egGridDataProvider , egConfirmDialog) {
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         ]).then(function(set) {
36             countWarn = set['ui.circ.in_house_use.entry_warn'] || 20;
37             $scope.countMax = countMax = 
38                 set['ui.circ.in_house_use.entry_cap'] || 99;
39         });
40     });
41
42     $scope.useFocus = true;
43     $scope.args = {noncat_type : 'barcode', num_uses : 1};
44     var checkouts = [];
45
46     var provider = egGridDataProvider.instance({});
47     provider.get = function(offset, count) {
48         return provider.arrayNotifier(checkouts, offset, count);
49     }
50     $scope.gridDataProvider = provider;
51
52     // currently selected non-cat type
53     $scope.selectedNcType = function() {
54         if (!egCore.env.cnct) return null; // too soon
55         var type = egCore.env.cnct.map[$scope.args.noncat_type];
56         return type ? type.name() : null;
57     }
58
59     $scope.checkout = function(args) {
60         $scope.copyNotFound = false;
61
62         var coArgs = {
63             count : args.num_uses,
64             'location' : egCore.auth.user().ws_ou()
65         };
66
67         if (args.noncat_type == 'barcode') {
68
69             egCore.pcrud.search('acp',
70                 {barcode : args.barcode, deleted : 'f'},
71                 {   flesh : 3, 
72                     flesh_fields : {
73                         acp : ['call_number','location'],
74                         acn : ['record'],
75                         bre : ['simple_record']
76                     },
77                     select : { bre : ['id'] } // avoid fleshing MARC
78                 }
79             ).then(function(copy) {
80
81                 if (!copy) {
82                     egCore.audio.play('error.in_house.copy_not_found');
83                     $scope.copyNotFound = true;
84                     return;
85                 }
86
87                 coArgs.copyid = copy.id();
88
89                 performCheckout(
90                     'open-ils.circ.in_house_use.create',
91                     coArgs, {copy:copy}
92                 );
93             });
94
95         } else {
96             coArgs.non_cat_type = args.noncat_type;
97             performCheckout(
98                 'open-ils.circ.non_cat_in_house_use.create',
99                 coArgs, {title : $scope.selectedNcType()}
100             );
101         }
102         $scope.args.barcode='';
103     }
104
105     function performCheckout(method, args, data) {
106
107         // FIXME: make this API stream
108         egCore.net.request(
109             'open-ils.circ', method, egCore.auth.token(), args
110
111         ).then(function(resp) {
112             if (evt = egCore.evt.parse(resp)) {
113                 egCore.audio.play('error.in_house');
114                 return alert(evt);
115             }
116
117             egCore.audio.play('success.in_house');
118
119             var item = {num_uses : resp.length};
120             item.copy = data.copy;
121             item.title = data.title || 
122                 data.copy.call_number().record().simple_record().title();
123             item.index = checkouts.length;
124
125             checkouts.unshift(item);
126             provider.refresh();
127         });
128     }
129
130     $scope.print_list = function() {
131         var print_data = { in_house_uses : [] };
132
133         if (checkouts.length == 0) return $q.when();
134
135         angular.forEach(checkouts, function(ihu) {
136             print_data.in_house_uses.push({
137                 num_uses : ihu.num_uses,
138                 copy : egCore.idl.toHash(ihu.copy),
139                 title : ihu.title
140             })
141         });
142
143         return egCore.print.print({
144             template : 'in_house_use_list',
145             scope : print_data
146         });
147     }
148
149 }])