]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/patron/bills.js
c9b6c44e911d5eec68286c10ab83ab34243e68ae
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / bills.js
1
2 /* Billing Service */
3
4 angular.module('egPatronApp')
5
6 .factory('billSvc', 
7        ['$q','egCore','patronSvc',
8 function($q , egCore , patronSvc) {
9
10     var service = {};
11
12     // fetch org unit settings specific to the bills display
13     service.fetchBillSettings = function() {
14         if (service.settings) return $q.when(service.settings);
15         return egCore.org.settings(
16             ['ui.circ.billing.uncheck_bills_and_unfocus_payment_box','ui.circ.billing.amount_warn','ui.circ.billing.amount_limit']
17         ).then(function(s) {return service.settings = s});
18     }
19
20     // user billing summary
21     service.fetchSummary = function() {
22         return egCore.pcrud.retrieve(
23             'mous', service.userId, {}, {authoritative : true})
24         .then(function(summary) {return service.summary = summary})
25     }
26
27     service.applyPayment = function(type, payments, note, check) {
28         return egCore.net.request(
29             'open-ils.circ',
30             'open-ils.circ.money.payment',
31             egCore.auth.token(), {
32                 userid : service.userId,
33                 note : note || '', 
34                 payment_type : type,
35                 check_number : check,
36                 payments : payments,
37                 patron_credit : 0
38             },
39             patronSvc.current.last_xact_id()
40         ).then(function(resp) {
41             console.debug('payments: ' + js2JSON(resp));
42             if (evt = egCore.evt.parse(resp)) 
43                 return alert(evt);
44
45             // payment API returns the update xact id so we can track it
46             // for future payments without having to refresh the user.
47             patronSvc.current.last_xact_id(resp.last_xact_id);
48             return resp.payments;
49         });
50     }
51
52     service.fetchBills = function(xact_id) {
53         var bills = [];
54         return egCore.pcrud.search('mb',
55             {xact : xact_id}, null,
56             {authoritative : true}
57         ).then(
58             function() {return bills},
59             null,
60             function(bill) {bills.push(bill); return bill}
61         );
62     }
63
64     // TODO: no longer needed?
65     service.fetchPayments = function(xact_id) {
66         return egCore.net.request(
67             'open-ils.circ',
68             'open-ils.circ.money.payment.retrieve.all.authoritative',
69             egCore.auth.token(), xact_id
70         );
71     }
72
73     service.voidBills = function(bill_ids) {
74         return egCore.net.requestWithParamList(
75             'open-ils.circ',
76             'open-ils.circ.money.billing.void',
77             [egCore.auth.token()].concat(bill_ids)
78         ).then(function(resp) {
79             if (evt = egCore.evt.parse(resp)) return alert(evt);
80             return resp;
81         });
82     }
83
84     service.updateBillNotes = function(note, ids) {
85         return egCore.net.requestWithParamList(
86             'open-ils.circ',
87             'open-ils.circ.money.billing.note.edit',
88             [egCore.auth.token(), note].concat(ids)
89         ).then(function(resp) {
90             if (evt = egCore.evt.parse(resp)) return alert(evt);
91             return resp;
92         });
93     }
94
95     service.updatePaymentNotes = function(note, ids) {
96         return egCore.net.requestWithParamList(
97             'open-ils.circ',
98             'open-ils.circ.money.payment.note.edit',
99             [egCore.auth.token(), note].concat(ids)
100         ).then(function(resp) {
101             if (evt = egCore.evt.parse(resp)) return alert(evt);
102             return resp;
103         });
104     }
105
106     return service;
107 }])
108
109
110 /**
111  * Manages Bills
112  */
113 .controller('PatronBillsCtrl',
114        ['$scope','$q','$routeParams','egCore','egConfirmDialog','$location',
115         'egGridDataProvider','billSvc','patronSvc','egPromptDialog', 'egAlertDialog',
116         'egBilling',
117 function($scope , $q , $routeParams , egCore , egConfirmDialog , $location,
118          egGridDataProvider , billSvc , patronSvc , egPromptDialog, egAlertDialog,
119          egBilling) {
120
121     $scope.initTab('bills', $routeParams.id);
122     billSvc.userId = $routeParams.id;
123
124     // set up some defaults
125     $scope.check_number = 0;
126     $scope.payment_amount = null;
127     $scope.session_voided = 0;
128     $scope.payment_type = 'cash_payment';
129     $scope.focus_payment = true;
130     $scope.annotate_payment = false;
131     $scope.receipt_count = 1;
132     $scope.receipt_on_pay = false;
133     $scope.warn_amount = 1000;
134     $scope.max_amount = 100000;
135     $scope.amount_verified = false;
136
137     // pre-define list-returning funcs in case we access them
138     // before the grid instantiates
139     $scope.gridControls = {
140         focusRowSelector : false,
141         selectedItems : function(){return []},
142         allItems : function(){return []},
143         itemRetrieved : function(item) {
144             item.payment_pending = 0;
145         },
146         activateItem : function(item) {
147             $scope.showFullDetails([item]);
148         },
149         setQuery : function() {    
150             return {
151                 usr : billSvc.userId, 
152                 xact_finish : null,
153                 'summary.balance_owed' : {'<>' : 0}
154             }
155         }, 
156         setSort : function() {
157             return ['xact_start']; 
158         }
159     }
160
161     billSvc.fetchSummary().then(function(s) {$scope.summary = s});
162
163     // given a payment amount, determines how much of that is applied
164     // to selected transactions and how much is left over (change).
165     function pending_payment_info() {
166         var amt = $scope.payment_amount || 0;
167         if (amt >= $scope.owed_selected()) {
168             return {
169                 payment : $scope.owed_selected(),
170                 change : amt - $scope.owed_selected()
171             }
172         } 
173         return {payment : amt, change : 0};
174     }
175
176     // calculates amount owed, billed, and paid for selected items
177     // TODO: move me to service
178     function selected_payment_info() {
179         var info = {owed : 0, billed : 0, paid : 0};
180         angular.forEach($scope.gridControls.selectedItems(), function(item) {
181             info.owed   += Number(item['summary.balance_owed']) * 100;
182             info.billed += Number(item['summary.total_owed']) * 100;
183             info.paid   += Number(item['summary.total_paid']) * 100;
184         });
185         info.owed /= 100;
186         info.billed /= 100;
187         info.paid /= 100;
188         return info;
189     }
190
191     $scope.pending_payment = function() {
192         return pending_payment_info().payment;
193     }
194     $scope.pending_change = function() {
195         return pending_payment_info().change;
196     }
197     $scope.owed_selected = function() {
198         return selected_payment_info().owed; 
199     }
200     $scope.billed_selected = function() {
201         return selected_payment_info().billed;
202     }
203     $scope.paid_selected = function() {
204         return selected_payment_info().paid;
205     }
206     $scope.refunds_available = function() {
207         var amount = 0;
208         angular.forEach($scope.gridControls.allItems(), function(item) {
209             if (item['summary.balance_owed'] < 0) 
210                 amount += item['summary.balance_owed'] * 100;
211         });
212         return -(amount / 100);
213     }
214
215     // update the item.payment_pending value each time the user
216     // selects different transactions to pay against.
217     $scope.$watch(
218         function() {return $scope.gridControls.selectedItems()},
219         function() {updatePendingColumn()},
220         true
221     );
222
223     // update the item.payment_pending for each (selected) 
224     // transaction any time the user-entered payment amount is modified
225     $scope.$watch('payment_amount', updatePendingColumn);
226
227     // updates the value of the payment_pending column in the grid.
228     // This has to be managed manually since the display value in the grid
229     // is derived from the value on the stored item and not the contents
230     // of our local scope variables.
231     function updatePendingColumn() {
232         // reset all to zero..
233         angular.forEach($scope.gridControls.allItems(), 
234             function(item) {item.payment_pending = 0});
235
236         var payment_amount = $scope.pending_payment();
237
238         var selected = $scope.gridControls.selectedItems();
239         for (var i = 0; i < selected.length; i++) { // for/break
240             var item = selected[i];
241             var owed = Number(item['summary.balance_owed']);
242
243             if (payment_amount > owed) {
244                 // pending payment exceeds balance of current item.
245                 // pay the entire item.
246                 item.payment_pending = owed;
247                 payment_amount -= owed;
248
249             } else {
250                 // balance owed on the current item matches or exceeds
251                 // the pending payment.  Apply the full remainder of
252                 // the payment to this item.. and we're done.
253                 item.payment_pending = payment_amount;
254                 break;
255             }
256         }
257     }
258
259     // builds payment arrays ([xact_id, ammount]) for all transactions
260     // which have a pending payment amount.
261     function generatePayments() {
262         var payments = [];
263         angular.forEach($scope.gridControls.selectedItems(), function(item) {
264             if (item.payment_pending == 0) return;
265             payments.push([item.id, item.payment_pending]);
266         });
267         return payments;
268     }
269
270     function refreshDisplay() {
271         patronSvc.fetchUserStats();
272         billSvc.fetchSummary().then(function(s) {$scope.summary = s});
273         $scope.payment_amount = null;
274         $scope.gridControls.refresh();
275     }
276
277     // generates payments, collects user note if needed, and sends payment
278     // to server.
279     function sendPayment(note) {
280         var make_payments = generatePayments();
281         billSvc.applyPayment(
282             $scope.payment_type, make_payments, note, $scope.check_number)
283         .then(function(payment_ids) {
284
285             if ($scope.receipt_on_pay) {
286                 printReceipt(
287                     $scope.payment_type, payment_ids, make_payments, note);
288             }
289
290             refreshDisplay();
291         })
292     }
293
294     function printReceipt(type, payment_ids, payments_made, note) {
295         var payment_blobs = [];
296         angular.forEach(payments_made, function(payment) {
297             var xact_id = payment[0];
298
299             // find the original transaction in the grid..
300             var xact = $scope.gridControls.allItems().filter(
301                 function(item) {return item.id == xact_id})[0];
302
303             payment_blobs.push({
304                 xact : egCore.idl.flatToNestedHash(xact),
305                 amount : payment[1]
306             });
307         });
308
309         console.log(js2JSON(payment_blobs[0]));
310
311         // page data not yet refreshed, capture data from current scope
312         var print_data = {
313             payment_note : note,
314             previous_balance : Number($scope.summary.balance_owed()),
315             payment_total : Number($scope.payment_amount),
316             payment_applied : $scope.pending_payment(),
317             amount_voided : Number($scope.session_voided),
318             change_given : $scope.pending_change(),
319             payments : payment_blobs,
320             current_location : egCore.idl.toHash(
321                 egCore.org.get(egCore.auth.user().ws_ou()))
322         }
323
324         print_data.new_balance = (
325             print_data.previous_balance * 100 - 
326             print_data.payment_applied * 100) / 100;
327
328         for (var i = 0; i < $scope.receipt_count; i++) {
329             egCore.print.print({
330                 context : 'receipt', 
331                 template : 'bill_payment', 
332                 scope : print_data
333             });
334         }
335     }
336
337     $scope.showHistory = function() {
338         $location.path('/circ/patron/' + 
339             patronSvc.current.id() + '/bill_history/transactions');
340     }
341     
342     // For now, only adds billing to first selected item.
343     // Could do batches later if needed
344     $scope.addBilling = function(all) {
345         if (all[0]) {
346             egBilling.showBillDialog({
347                 xact : egCore.idl.flatToNestedHash(all[0]),
348                 patron : $scope.patron()
349             }).then(refreshDisplay);
350         }
351     }
352
353     $scope.showBillDialog = function($event) {
354         egBilling.showBillDialog({
355             patron : $scope.patron()
356         }).then(refreshDisplay);
357     }
358
359     // Select refunds adds all refunds to the existing selection.
360     // It does not /only/ select refunds
361     $scope.selectRefunds = function() {
362         var ids = $scope.gridControls.selectedItems().map(
363             function(i) { return i.id });
364         angular.forEach($scope.gridControls.allItems(), function(item) {
365             if (Number(item['summary.balance_owed']) < 0)
366                 ids.push(item.id);
367         });
368         $scope.gridControls.selectItems(ids);
369     }
370
371     // -------------
372     // determine on initial page load when all of the grid rows should
373     // be selected.
374     var selectOnLoad = true;
375     billSvc.fetchBillSettings().then(function(s) {
376         if (s['ui.circ.billing.uncheck_bills_and_unfocus_payment_box']) {
377             $scope.focus_payment = false; // de-focus the payment box
378             $scope.gridControls.focusRowSelector = true;
379             selectOnLoad = false;
380             // if somehow the grid finishes rendering before our settings 
381             // arrive, manually de-select everything.
382             $scope.gridControls.selectItems([]);
383         }
384         if (s['ui.circ.billing.amount_warn']) {
385             $scope.warn_amount = Number(s['ui.circ.billing.amount_warn']);
386         }
387         if (s['ui.circ.billing.amount_limit']) {
388             $scope.max_amount = Number(s['ui.circ.billing.amount_limit']);
389         }
390     });
391
392     $scope.gridControls.allItemsRetrieved = function() {
393         if (selectOnLoad) {
394             selectOnLoad = false; // only for initial controller load.
395             // select all non-refund items
396             $scope.gridControls.selectItems( 
397                 $scope.gridControls.allItems()
398                 .filter(function(i) {return i['summary.balance_owed'] > 0})
399                 .map(function(i){return i.id})
400             );
401         }
402     }
403     // -------------
404
405
406     $scope.printBills = function(selected) {
407         if (!selected.length) return;
408         // bills print receipt assumes nested hashes, but our grid
409         // stores flattener data.  Fetch the selected xacts as
410         // fleshed pcrud objects and hashify.  
411         // (Consider an alternate approach..)
412         var ids = selected.map(function(t){ return t.id });
413         var xacts = [];
414         egCore.pcrud.search('mbt', 
415             {id : ids},
416             {flesh : 1, flesh_fields : {'mbt' : ['summary']}},
417             {authoritative : true}
418         ).then(
419             function() {
420                 egCore.print.print({
421                     context : 'receipt', 
422                     template : 'bills_current', 
423                     scope : {   
424                         transactions : xacts,
425                         current_location : egCore.idl.toHash(
426                             egCore.org.get(egCore.auth.user().ws_ou()))
427                     }
428                 });
429             }, 
430             null, 
431             function(xact) {
432                 xacts.push(egCore.idl.toHash(xact));
433             }
434         );
435     }
436
437     $scope.applyPayment = function() {
438
439         if ($scope.payment_amount > $scope.max_amount ) {
440             egAlertDialog.open(
441                 egCore.strings.PAYMENT_OVER_MAX,
442                 {   max_amount : ''+$scope.max_amount,
443                     ok : function() {
444                         $scope.payment_amount = 0;
445                     }
446                 }
447             );
448             return;
449         }
450
451         if (($scope.payment_amount > $scope.warn_amount) && ($scope.amount_verified == false)) {
452             egConfirmDialog.open(
453                 egCore.strings.PAYMENT_WARN_AMOUNT_TITLE, egCore.strings.PAYMENT_WARN_AMOUNT,
454                 {   payment_amount : ''+$scope.payment_amount,
455                     ok : function() {
456                         $scope.amount_verfied = true;
457                         $scope.applyPayment();
458                     },
459                     cancel : function() {
460                         $scope.payment_amount = 0;
461                     }
462                 }
463             );
464             return;
465         }
466
467         $scope.amount_verfied = false;
468
469         if ($scope.annotate_payment) {
470             egPromptDialog.open(
471                 egCore.strings.ANNOTATE_PAYMENT_MSG, '',
472                 {ok : function(value) {sendPayment(value)}}
473             );
474         } else {
475             sendPayment();
476         }
477     }
478
479     $scope.voidAllBillings = function(items) {
480         angular.forEach(items, function(item) {
481
482             billSvc.fetchBills(item.id).then(function(bills) {
483                 var bill_ids = [];
484                 var cents = 0;
485                 angular.forEach(bills, function(b) {
486                     if (b.voided() != 't') {
487                         cents += b.amount() * 100;
488                         bill_ids.push(b.id())
489                     }
490                 });
491
492                 $scope.session_voided = 
493                     ($scope.session_voided * 100 + cents) / 100;
494
495                 if (bill_ids.length == 0) {
496                     // TODO: warn
497                     return;
498                 }
499
500                 // TODO: alert of pending voiding
501
502                 billSvc.voidBills(bill_ids).then(function() {
503                     refreshDisplay();
504                 });
505             });
506         });
507     }
508
509     // note this is functionally equivalent to selecting a neg. transaction
510     // then clicking Apply Payment -- this just adds a speed bump (ditto
511     // the XUL client).
512     $scope.refundXact = function(all) {
513         var items = all.filter(function(item) {
514             return item['summary.balance_owed'] < 0
515         });
516
517         if (items.length == 0) return;
518
519         var ids = items.map(function(item) {return item.id});
520             
521         egConfirmDialog.open(
522             egCore.strings.CONFIRM_REFUND_PAYMENT, '', 
523             {   xactIds : ''+ids,
524                 ok : function() {
525                     // reset the received payment amount.  this ensures
526                     // we're not mingling payments with refunds.
527                     $scope.payment_amount = 0;
528                 }
529             }
530         );
531     }
532
533     // direct the user to the transaction details page
534     $scope.showFullDetails = function(all) {
535         if (all[0]) 
536             $location.path('/circ/patron/' + 
537                 patronSvc.current.id() + '/bill/' + all[0].id);
538     }
539
540     $scope.activateBill = function(xact) {
541         $scope.showFullDetails([xact]);
542     }
543
544 }])
545
546 /**
547  * Displays details of a single transaction
548  */
549 .controller('XactDetailsCtrl',
550        ['$scope','$q','$routeParams','egCore','egGridDataProvider','patronSvc','billSvc','egPromptDialog','egBilling',
551 function($scope,  $q , $routeParams , egCore , egGridDataProvider , patronSvc , billSvc , egPromptDialog , egBilling) {
552
553     $scope.initTab('bills', $routeParams.id);
554     var xact_id = $routeParams.xact_id;
555
556     var xactGrid = $scope.xactGridControls = {
557         setQuery : function() { return {xact : xact_id} },
558         setSort : function() { return ['billing_ts'] }
559     }
560
561     var paymentGrid = $scope.paymentGridControls = {
562         setQuery : function() { return {xact : xact_id} },
563         setSort : function() { return ['payment_ts'] }
564     }
565
566     // -- actions
567     $scope.voidBillings = function(bill_list) {
568         var bill_ids = [];
569         angular.forEach(bill_list, function(b) {
570             if (b.voided != 't') bill_ids.push(b.id);
571         });
572
573         if (bill_ids.length == 0) {
574             // TODO: warn
575             return;
576         }
577
578         billSvc.voidBills(bill_ids).then(function() {
579
580             // refresh bills and summary data
581             // note: no need to update payments
582             patronSvc.fetchUserStats();
583
584             egBilling.fetchXact(xact_id).then(function(xact) {
585                 $scope.xact = xact
586             });
587
588             xactGrid.refresh();
589         });
590     }
591
592     // batch-edit billing and payment notes, depending on 'type'
593     function editNotes(selected, type) {
594         var notes = selected.map(function(b){ return b.note }).join(',');
595         var ids = selected.map(function(b){ return b.id });
596
597         // show the note edit prompt
598         egPromptDialog.open(
599             egCore.strings.EDIT_BILL_PAY_NOTE, notes, {
600                 ids : ''+ids,
601                 ok : function(value) {
602
603                     var func = 'updateBillNotes';
604                     if (type == 'payment') func = 'updatePaymentNotes';
605
606                     billSvc[func](value, ids).then(function() {
607                         if (type == 'payment') {
608                             paymentGrid.refresh();
609                         } else {
610                             xactGrid.refresh();
611                         }
612                     });
613                 }
614             }
615         );
616     }
617
618     $scope.editBillNotes = function(selected) {
619         editNotes(selected, 'bill');
620     }
621
622     $scope.editPaymentNotes = function(selected) {
623         editNotes(selected, 'payment');
624     }
625
626     // -- retrieve our data
627     $scope.total_circs = 0; // start with 0 instead of undefined
628     egBilling.fetchXact(xact_id).then(function(xact) {
629         $scope.xact = xact;
630
631         var copyId = xact.circulation().target_copy().id();
632         var circ_count = 0;
633         egCore.pcrud.search('circbyyr',
634             {copy : copyId}, null, {atomic : true})
635         .then(function(counts) {
636             angular.forEach(counts, function(count) {
637                 circ_count += Number(count.count());
638             });
639             $scope.total_circs = circ_count;
640         });
641         // set the title.  only needs to be done on initial page load
642         if (xact.circulation()) {
643             if (xact.circulation().target_copy().call_number().id() == -1) {
644                 $scope.title = xact.circulation().target_copy().dummy_title();
645             } else  {
646                 // TODO: shared bib service?
647                 $scope.title = xact.circulation().target_copy()
648                     .call_number().record().simple_record().title();
649                 $scope.title_id = xact.circulation().target_copy()
650                     .call_number().record().id();
651             }
652         }
653     });
654 }])
655
656
657 .controller('BillHistoryCtrl',
658        ['$scope','$q','$routeParams','egCore','patronSvc','billSvc','egPromptDialog','$location',
659 function($scope,  $q , $routeParams , egCore , patronSvc , billSvc , egPromptDialog , $location) {
660
661     $scope.initTab('bills', $routeParams.id);
662     billSvc.userId = $routeParams.id;
663     $scope.bill_tab = $routeParams.history_tab;
664     $scope.totals = {};
665
666     // link page controller actions defined by sub-controllers here
667     $scope.actions = {};
668
669     var start = new Date(); // now - 1 year
670     start.setFullYear(start.getFullYear() - 1),
671     $scope.dates = {
672         xact_start : start,
673         xact_finish : new Date()
674     }
675
676     $scope.date_range = function() {
677         var start = $scope.dates.xact_start.toISOString().replace(/T.*/,'');
678         var end = $scope.dates.xact_finish.toISOString().replace(/T.*/,'');
679         var today = new Date().toISOString().replace(/T.*/,'');
680         if (end == today) end = 'now';
681         return [start, end];
682     }
683 }])
684
685
686 .controller('BillXactHistoryCtrl',
687        ['$scope','$q','egCore','patronSvc','billSvc','egPromptDialog','$location','egBilling',
688 function($scope,  $q , egCore , patronSvc , billSvc , egPromptDialog , $location , egBilling) {
689
690     // generate a grid query with the current date widget values.
691     function current_grid_query() {
692         return {
693             '-or' : [
694                 {'summary.balance_owed' : {'<>' : 0}},
695                 {'summary.last_payment_ts' : {'<>' : null}}
696             ],
697             xact_start : {between : $scope.date_range()},
698             usr : billSvc.userId
699         }
700     }
701
702     $scope.gridControls = {
703         selectedItems : function(){return []},
704         activateItem : function(item) {
705             $scope.showFullDetails([item]);
706         },
707         // this sets the query on page load
708         setQuery : current_grid_query
709     }
710
711     $scope.actions.apply_date_range = function() {
712         // tells the grid to re-draw itself with the new query
713         $scope.gridControls.setQuery(current_grid_query());
714     }
715
716     // TODO; move me to service
717     function selected_payment_info() {
718         var info = {owed : 0, billed : 0, paid : 0};
719         angular.forEach($scope.gridControls.selectedItems(), function(item) {
720             info.owed   += Number(item['summary.balance_owed']) * 100;
721             info.billed += Number(item['summary.total_owed']) * 100;
722             info.paid   += Number(item['summary.total_paid']) * 100;
723         });
724         info.owed /= 100;
725         info.billed /= 100;
726         info.paid /= 100;
727         return info;
728     }
729
730     $scope.totals.selected_billed = function() {
731         return selected_payment_info().billed;
732     }
733     $scope.totals.selected_paid = function() {
734         return selected_payment_info().paid;
735     }
736
737     $scope.showFullDetails = function(all) {
738         if (all[0]) 
739             $location.path('/circ/patron/' + 
740                 patronSvc.current.id() + '/bill/' + all[0].id);
741     }
742
743     // For now, only adds billing to first selected item.
744     // Could do batches later if needed
745     $scope.addBilling = function(all) {
746         if (all[0]) {
747             egBilling.showBillDialog({
748                 xact : egCore.idl.flatToNestedHash(all[0]),
749                 patron : $scope.patron()
750             }).then(function() { 
751                 $scope.gridControls.refresh();
752                 patronSvc.fetchUserStats();
753             })
754         }
755     }
756 }])
757
758 .controller('BillPaymentHistoryCtrl',
759        ['$scope','$q','egCore','patronSvc','billSvc','$location',
760 function($scope,  $q , egCore , patronSvc , billSvc , $location) {
761
762     // generate a grid query with the current date widget values.
763     function current_grid_query() {
764         return {
765             'payment_ts' : {between : $scope.date_range()},
766             'xact.usr' : billSvc.userId
767         }
768     }
769
770     $scope.gridControls = {
771         selectedItems : function(){return []},
772         activateItem : function(item) {
773             $scope.showFullDetails([item]);
774         },
775         setSort : function() {
776             return [{'payment_ts' : 'DESC'}, 'id'];
777         },
778         setQuery : current_grid_query
779     }
780
781     $scope.actions.apply_date_range = function() {
782         // tells the grid to re-draw itself with the new query
783         $scope.gridControls.setQuery(current_grid_query());
784     }
785
786     $scope.showFullDetails = function(all) {
787         if (all[0]) 
788             $location.path('/circ/patron/' + 
789                 patronSvc.current.id() + '/bill/' + all[0]['xact.id']);
790     }
791
792     $scope.totals.selected_paid = function() {
793         var paid = 0;
794         angular.forEach($scope.gridControls.selectedItems(), function(payment) {
795             paid += Number(payment.amount) * 100;
796         });
797         return paid / 100;
798     }
799 }])
800
801
802