2 * Shared services for patron billing.
6 angular.module('egCoreMod')
9 ['$uibModal','$q','egCore',
10 function($uibModal , $q , egCore) {
14 // fetch a fleshed money.billable_xact
15 service.fetchXact = function(xact_id) {
16 return egCore.pcrud.retrieve('mbt', xact_id, {
19 mbt : ['summary','circulation','grocery','reservation'],
20 circ: ['target_copy'],
21 acp : ['call_number','location','status','age_protect'],
23 bre : ['simple_record']
25 select : {bre : ['id']}}, // avoid MARC
26 {authoritative : true}
30 // apply a patron billing. If no xact is provided, a grocery xact is
32 service.billPatron = function(args, xact) {
33 // apply a billing to an existing transaction
34 if (xact) return service.createBilling(xact.id, args);
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);
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());
48 groc.usr(args.patron_id);
51 return egCore.net.request(
53 'open-ils.circ.money.grocery.create',
54 egCore.auth.token(), groc
56 // create the billing on the new xact
57 ).then(function(xact_id) {
58 if (evt = egCore.evt.parse(xact_id))
64 // fetch the org-focused billing types
66 service.fetchBillingTypes = function() {
68 return $q.when(egCore.env.cbt.list);
71 return egCore.net.request(
73 'open-ils.circ.billing_type.ranged.retrieve.all',
75 egCore.auth.user().ws_ou()
76 ).then(function(list) {
77 list = list.filter(function(item) {
78 // first 100 are reserved for system-generated bills
79 return item.id() > 100;
81 egCore.env.absorbList(list, 'cbt');
86 // create a patron billing
87 service.createBilling = function(xact_id, args) {
88 var bill = new egCore.idl.mb();
90 bill.amount(args.amount);
91 bill.btype(args.billingType);
92 bill.billing_type(egCore.env.cbt.map[args.billingType].name());
95 return egCore.net.request(
97 'open-ils.circ.money.billing.create',
98 egCore.auth.token(), bill
100 // check the billing response
101 ).then(function(bill_id) {
102 if (evt = egCore.evt.parse(bill_id)) {
111 // Show the billing dialog.
112 // Allows users to select amount, billing type, and note.
114 // xact OR xact_id : if null, creates a grocery xact
115 // patron OR patron_id
116 service.showBillDialog = function(args) {
118 return $uibModal.open({
119 templateUrl: './circ/share/t_bill_patron_dialog',
122 ['$scope','$uibModalInstance','$timeout','billingTypes','xact','patron',
123 function($scope , $uibModalInstance , $timeout , billingTypes , xact , patron) {
124 console.debug('billing patron ' + patron.id());
126 if (xact && xact._isfieldmapper)
127 xact = egCore.idl.toHash(xact);
129 $scope.patron = patron;
130 $scope.billingTypes = billingTypes;
131 $scope.location = egCore.org.get(egCore.auth.user().ws_ou()),
133 billingType : 101, // default to stock Misc. billing type
135 patron_id : patron.id()
137 $scope.ok = function(args) { $uibModalInstance.close(args) }
138 $scope.cancel = function () { $uibModalInstance.dismiss() }
139 $scope.updateDefaultPrice = function() {
140 var type = billingTypes.filter(function(t) {
141 return t.id() == $scope.billArgs.billingType })[0];
142 if (type.default_price() && !$scope.billArgs.amount)
143 $scope.billArgs.amount = Number(type.default_price());
147 // if we don't already have them, fetch the billing types
148 billingTypes : function() {
149 return service.fetchBillingTypes();
153 if (args.xact) return $q.when(args.xact);
154 if (args.xact_id) return service.fetchXact(args.xact_id);
158 patron : function() {
159 if (args.patron) return $q.when(args.patron);
160 return egCore.pcrud.retrieve('au', args.patron_id,
161 {flesh : 1, flesh_fields : {au : ['card']}});
167 // send the billing to the server using the arguments
168 // provided in the billing dialog, then refresh
169 return service.billPatron(args, args.xact);