]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/services/billing.js
LP#1350042 Browser client templates/scripts (phase 1)
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / services / billing.js
1 /**
2  * Shared services for patron billing.
3  * 
4  */
5
6 angular.module('egCoreMod')
7
8 .factory('egBilling', 
9        ['$modal','$q','egCore',
10 function($modal , $q , egCore) {
11
12     var service = {};
13
14     // fetch a fleshed money.billable_xact
15     service.fetchXact = function(xact_id) {
16         return egCore.pcrud.retrieve('mbt', xact_id, {
17             flesh : 5,
18             flesh_fields : {
19                 mbt : ['summary','circulation','grocery','reservation'],
20                 circ: ['target_copy'],
21                 acp : ['call_number','location','status','age_protect'],
22                 acn : ['record'],
23                 bre : ['simple_record']
24             },
25             select : {bre : ['id']}}, // avoid MARC
26             {authoritative : true}
27         );
28     }
29
30     // apply a patron billing.  If no xact is provided, a grocery xact is
31     // created.
32     service.billPatron = function(args, xact) {
33         // apply a billing to an existing transaction
34         if (xact) return service.createBilling(xact.id, args);
35
36         // create a new grocery xact, then apply a billing
37         return service.createGroceryXact(args)
38         .then(function(xact_id) { 
39             return service.createBilling(xact_id, args);
40         });
41     }
42
43     // create a new grocery xact
44     service.createGroceryXact = function(args) {
45         var groc = new egCore.idl.mg();
46         groc.billing_location(egCore.auth.user().ws_ou());
47         groc.note(args.note);
48         groc.usr(args.patron_id);
49         
50         // create the xact
51         return egCore.net.request(
52             'open-ils.circ',
53             'open-ils.circ.money.grocery.create',
54             egCore.auth.token(), groc
55
56         // create the billing on the new xact
57         ).then(function(xact_id) {
58             if (evt = egCore.evt.parse(xact_id)) 
59                 return alert(evt);
60             return xact_id;
61         });
62     }
63
64     // fetch the org-focused billing types
65     // Cache on egEnv
66     service.fetchBillingTypes = function() {
67         if (egCore.env.cbt) 
68             return $q.when(egCore.env.cbt.list);
69
70         return egCore.pcrud.search('cbt', 
71             {   // first 100 are reserved for system-generated bills
72                 id : {'>' : 100}, 
73                 owner : egCore.org.ancestors(
74                     egCore.auth.user().ws_ou(), true)
75             }, 
76             {}, {atomic : true}
77         ).then(function(list) {
78             egCore.env.absorbList(list, 'cbt');
79             return list;
80         });
81     }
82
83     // create a patron billing
84     service.createBilling = function(xact_id, args) {
85         var bill = new egCore.idl.mb();
86         bill.xact(xact_id);
87         bill.amount(args.amount);
88         bill.btype(args.billingType);
89         bill.billing_type(egCore.env.cbt.map[args.billingType].name());
90         bill.note(args.note);
91
92         return egCore.net.request(
93             'open-ils.circ', 
94             'open-ils.circ.money.billing.create',
95             egCore.auth.token(), bill
96
97         // check the billing response
98         ).then(function(bill_id) {
99             if (evt = egCore.evt.parse(bill_id)) {
100                 alert(evt);
101             } else {
102                 return bill_id;
103             }
104         });
105     }
106
107
108     // Show the billing dialog.  
109     // Allows users to select amount, billing type, and note.
110     // args:
111     //   xact OR xact_id : if null, creates a grocery xact
112     //   patron OR patron_id
113     service.showBillDialog = function(args) {
114
115         return $modal.open({
116             templateUrl: './circ/share/t_bill_patron_dialog',
117             controller: 
118                    ['$scope','$modalInstance','$timeout','billingTypes','xact','patron',
119             function($scope , $modalInstance , $timeout , billingTypes , xact , patron) {
120                 console.debug('billing patron ' + patron.id());
121                 $scope.focus = true;
122                 if (xact && xact._isfieldmapper)
123                     xact = egCore.idl.toHash(xact);
124                 $scope.xact = xact;
125                 $scope.patron = patron;
126                 $scope.billingTypes = billingTypes;
127                 $scope.location = egCore.org.get(egCore.auth.user().ws_ou()),
128                 $scope.billArgs = {
129                     billingType : 101, // default to stock Misc. billing type
130                     xact : xact,
131                     patron_id : patron.id()
132                 }
133                 $scope.ok = function(args) { $modalInstance.close(args) }
134                 $scope.cancel = function () { $modalInstance.dismiss() }
135                 $scope.updateDefaultPrice = function() {
136                     var type = billingTypes.filter(function(t) {
137                         return t.id() == $scope.billArgs.billingType })[0];
138                     if (type.default_price() && !$scope.billArgs.amount) 
139                         $scope.billArgs.amount = Number(type.default_price());
140                 }
141             }],
142             resolve : {
143                 // if we don't already have them, fetch the billing types
144                 billingTypes : function() {
145                     return service.fetchBillingTypes();
146                 }, 
147
148                 xact : function() {
149                     if (args.xact) return $q.when(args.xact);
150                     if (args.xact_id) return service.fetchXact(args.xact_id);
151                     return $q.when();
152                 },
153
154                 patron : function() {
155                     if (args.patron) return $q.when(args.patron);
156                     return  egCore.pcrud.retrieve('au', args.patron_id,
157                         {flesh : 1, flesh_fields : {au : ['card']}});
158                 }
159
160             }
161         }).result.then(
162             function(args) {
163                 // send the billing to the server using the arguments
164                 // provided in the billing dialog, then refresh
165                 return service.billPatron(args, args.xact);
166             }
167         );
168     }
169
170     return service;
171 }]);
172
173
174
175