]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/in_house_use/app.js
webstaff: add print template and action for in-house uses list
[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                     $scope.copyNotFound = true;
83                     return;
84                 }
85
86                 coArgs.copyid = copy.id();
87
88                 performCheckout(
89                     'open-ils.circ.in_house_use.create',
90                     coArgs, {copy:copy}
91                 );
92             });
93
94         } else {
95             coArgs.non_cat_type = args.noncat_type;
96             performCheckout(
97                 'open-ils.circ.non_cat_in_house_use.create',
98                 coArgs, {title : $scope.selectedNcType()}
99             );
100         }
101         $scope.args.barcode='';
102     }
103
104     function performCheckout(method, args, data) {
105
106         // FIXME: make this API stream
107         egCore.net.request(
108             'open-ils.circ', method, egCore.auth.token(), args
109
110         ).then(function(resp) {
111             if (evt = egCore.evt.parse(resp)) return alert(evt);
112
113             var item = {num_uses : resp.length};
114             item.copy = data.copy;
115             item.title = data.title || 
116                 data.copy.call_number().record().simple_record().title();
117             item.index = checkouts.length;
118
119             checkouts.unshift(item);
120             provider.refresh();
121         });
122     }
123
124     $scope.print_list = function() {
125         var print_data = { in_house_uses : [] };
126
127         if (checkouts.length == 0) return $q.when();
128
129         angular.forEach(checkouts, function(ihu) {
130             print_data.in_house_uses.push({
131                 num_uses : ihu.num_uses,
132                 copy : egCore.idl.toHash(ihu.copy),
133                 title : ihu.title
134             })
135         });
136
137         return egCore.print.print({
138             template : 'in_house_use_list',
139             scope : print_data
140         });
141     }
142
143 }])