4 angular.module('egPatronApp')
7 ['$q','egCore','egWorkLog','patronSvc',
8 function($q , egCore , egWorkLog , patronSvc) {
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','circ.staff_client.do_not_auto_attempt_print']
17 ).then(function(s) {return service.settings = s});
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})
27 service.applyPayment = function(type, payments, note, check, cc_args) {
28 return egCore.net.request(
30 'open-ils.circ.money.payment',
31 egCore.auth.token(), {
32 userid : service.userId,
40 patronSvc.current.last_xact_id()
41 ).then(function(resp) {
42 console.debug('payments: ' + js2JSON(resp));
43 var total = 0; angular.forEach(payments,function(p) { total += p[1]; });
46 case 'cash_payment' : msg = egCore.strings.EG_WORK_LOG_CASH_PAYMENT; break;
47 case 'check_payment' : msg = egCore.strings.EG_WORK_LOG_CHECK_PAYMENT; break;
48 case 'credit_card_payment' : msg = egCore.strings.EG_WORK_LOG_CREDIT_CARD_PAYMENT; break;
49 case 'credit_payment' : msg = egCore.strings.EG_WORK_LOG_CREDIT_PAYMENT; break;
50 case 'work_payment' : msg = egCore.strings.EG_WORK_LOG_WORK_PAYMENT; break;
51 case 'forgive_payment' : msg = egCore.strings.EG_WORK_LOG_FORGIVE_PAYMENT; break;
52 case 'goods_payment' : msg = egCore.strings.EG_WORK_LOG_GOODS_PAYMENT; break;
56 'action' : 'paid_bill',
57 'patron_id' : service.userId,
58 'total_amount' : total
61 if (evt = egCore.evt.parse(resp))
64 // payment API returns the update xact id so we can track it
65 // for future payments without having to refresh the user.
66 patronSvc.current.last_xact_id(resp.last_xact_id);
71 service.fetchBills = function(xact_id) {
73 return egCore.pcrud.search('mb',
74 {xact : xact_id}, null,
75 {authoritative : true}
77 function() {return bills},
79 function(bill) {bills.push(bill); return bill}
83 // TODO: no longer needed?
84 service.fetchPayments = function(xact_id) {
85 return egCore.net.request(
87 'open-ils.circ.money.payment.retrieve.all.authoritative',
88 egCore.auth.token(), xact_id
92 service.voidBills = function(bill_ids) {
93 return egCore.net.requestWithParamList(
95 'open-ils.circ.money.billing.void',
96 [egCore.auth.token()].concat(bill_ids)
97 ).then(function(resp) {
98 if (evt = egCore.evt.parse(resp)) return alert(evt);
103 service.adjustBillsToZero = function(bill_ids) {
104 return egCore.net.request(
106 'open-ils.circ.money.billable_xact.adjust_to_zero',
109 ).then(function(resp) {
110 if (evt = egCore.evt.parse(resp)) return alert(evt);
115 service.updateBillNotes = function(note, ids) {
116 return egCore.net.requestWithParamList(
118 'open-ils.circ.money.billing.note.edit',
119 [egCore.auth.token(), note].concat(ids)
120 ).then(function(resp) {
121 if (evt = egCore.evt.parse(resp)) return alert(evt);
126 service.updatePaymentNotes = function(note, ids) {
127 return egCore.net.requestWithParamList(
129 'open-ils.circ.money.payment.note.edit',
130 [egCore.auth.token(), note].concat(ids)
131 ).then(function(resp) {
132 if (evt = egCore.evt.parse(resp)) return alert(evt);
144 .controller('PatronBillsCtrl',
145 ['$scope','$q','$routeParams','egCore','egConfirmDialog','$location',
146 'egGridDataProvider','billSvc','patronSvc','egPromptDialog', 'egAlertDialog',
147 'egBilling','$uibModal',
148 function($scope , $q , $routeParams , egCore , egConfirmDialog , $location,
149 egGridDataProvider , billSvc , patronSvc , egPromptDialog, egAlertDialog,
150 egBilling , $uibModal) {
152 $scope.initTab('bills', $routeParams.id);
153 billSvc.userId = $routeParams.id;
155 // set up some defaults
156 $scope.check_number = null;
157 $scope.payment_amount = null;
158 $scope.session_voided = 0;
159 $scope.payment_type = 'cash_payment';
160 $scope.focus_payment = true;
161 $scope.annotate_payment = false;
162 $scope.receipt_count = 1;
163 $scope.receipt_on_pay = { isChecked: false };
164 $scope.warn_amount = 1000;
165 $scope.max_amount = 100000;
166 $scope.amount_verified = false;
167 $scope.disable_auto_print = false;
169 // check receipt_on_pay setting default persisted
170 egCore.hatch.getItem('circ.bills.receiptonpay')
171 .then(function(rcptOnPay){
172 if (rcptOnPay) $scope.receipt_on_pay.isChecked = rcptOnPay;
175 // pre-define list-returning funcs in case we access them
176 // before the grid instantiates
177 $scope.gridControls = {
178 focusRowSelector : false,
179 selectedItems : function(){return []},
180 allItems : function(){return []},
181 itemRetrieved : function(item) {
182 item.payment_pending = 0;
184 activateItem : function(item) {
185 $scope.showFullDetails([item]);
187 setQuery : function() {
189 usr : billSvc.userId,
191 'summary.balance_owed' : {'<>' : 0}
194 setSort : function() {
195 return ['xact_start'];
199 // Apply coloring to rows based on fines stop reason
200 $scope.colorizeBillsList = {
201 rowClass: function(item) {
202 if (!item['circulation.checkin_time']) {
203 if (item['circulation.stop_fines'] == 'LOST') {
204 return 'dark-red-row-highlight';
205 } else if (item['circulation.stop_fines'] == 'LONGOVERDUE') {
206 return 'red-row-highlight';
207 } else if (item['circulation.due_date'] && // Still checked out - need feedback on this approach, feel like there's a better way
208 !item['circulation.stop_fines']) {
209 return 'orange-row-highlight';
215 // Status Icon Column definition
216 $scope.statusIconColumn = {
218 template: function(item) {
221 var now = new Date();
223 if (item['circulation.due_date'] &&
224 !item['circulation.checkin_time']) {
225 var due_date = new Date(item['circulation.due_date']);
227 if (item['circulation.stop_fines'] &&
228 item['circulation.stop_fines'] == "LOST") {
229 icons.push('glyphicon-question-sign');
230 } else if (item['circulation.stop_fines'] &&
231 item['circulation.stop_fines'] == "LONGOVERDUE") {
232 icons.push('glyphicon-exclamation-sign');
233 } else if (now >= due_date) {
234 icons.push('glyphicon-time');
238 angular.forEach(icons, function(icon) {
239 template = template + "<i class='glyphicon " + icon + "'></i>"
245 billSvc.fetchSummary().then(function(s) {$scope.summary = s});
247 // given a payment amount, determines how much of that is applied
248 // to selected transactions and how much is left over (change).
249 function pending_payment_info() {
250 var amt = $scope.payment_amount || 0;
251 if (amt >= $scope.owed_selected()) {
253 payment : $scope.owed_selected(),
254 change : amt - $scope.owed_selected()
257 return {payment : amt, change : 0};
260 // calculates amount owed, billed, and paid for selected items
261 // TODO: move me to service
262 function selected_payment_info() {
263 var info = {owed : 0, billed : 0, paid : 0};
264 angular.forEach($scope.gridControls.selectedItems(), function(item) {
265 info.owed += Number(item['summary.balance_owed']) * 100;
266 info.billed += Number(item['summary.total_owed']) * 100;
267 info.paid += Number(item['summary.total_paid']) * 100;
275 $scope.pending_payment = function() {
276 return pending_payment_info().payment;
278 $scope.pending_change = function() {
279 return pending_payment_info().change;
281 $scope.owed_selected = function() {
282 return selected_payment_info().owed;
284 $scope.billed_selected = function() {
285 return selected_payment_info().billed;
287 $scope.paid_selected = function() {
288 return selected_payment_info().paid;
290 $scope.refunds_available = function() {
292 angular.forEach($scope.gridControls.allItems(), function(item) {
293 if (item['summary.balance_owed'] < 0)
294 amount += item['summary.balance_owed'] * 100;
296 return -(amount / 100);
298 $scope.invalid_check_number = function() {
299 return $scope.payment_type == 'check_payment' && ! $scope.check_number;
302 // update the item.payment_pending value each time the user
303 // selects different transactions to pay against.
305 function() {return $scope.gridControls.selectedItems()},
306 function() {updatePendingColumn()},
310 // update the item.payment_pending for each (selected)
311 // transaction any time the user-entered payment amount is modified
312 $scope.$watch('payment_amount', updatePendingColumn);
314 // updates the value of the payment_pending column in the grid.
315 // This has to be managed manually since the display value in the grid
316 // is derived from the value on the stored item and not the contents
317 // of our local scope variables.
318 function updatePendingColumn() {
319 // reset all to zero..
320 angular.forEach($scope.gridControls.allItems(),
321 function(item) {item.payment_pending = 0});
323 var payment_amount = $scope.pending_payment();
325 var selected = $scope.gridControls.selectedItems();
326 for (var i = 0; i < selected.length; i++) { // for/break
327 var item = selected[i];
328 var owed = Number(item['summary.balance_owed']);
330 if (payment_amount > owed) {
331 // pending payment exceeds balance of current item.
332 // pay the entire item.
333 item.payment_pending = owed;
334 payment_amount -= owed;
337 // balance owed on the current item matches or exceeds
338 // the pending payment. Apply the full remainder of
339 // the payment to this item.. and we're done.
340 item.payment_pending = payment_amount;
346 // builds payment arrays ([xact_id, ammount]) for all transactions
347 // which have a pending payment amount.
348 function generatePayments() {
350 angular.forEach($scope.gridControls.selectedItems(), function(item) {
351 if (item.payment_pending == 0) return;
352 payments.push([item.id, item.payment_pending]);
357 function refreshDisplay() {
358 patronSvc.fetchUserStats();
359 billSvc.fetchSummary().then(function(s) {$scope.summary = s});
360 $scope.payment_amount = null;
361 $scope.gridControls.refresh();
364 // generates payments, collects user note if needed, and sends payment
366 function sendPayment(note, cc_args) {
367 var make_payments = generatePayments();
368 billSvc.applyPayment($scope.payment_type,
369 make_payments, note, $scope.check_number, cc_args)
370 .then(function(payment_ids) {
372 if (!$scope.disable_auto_print && $scope.receipt_on_pay.isChecked) {
374 $scope.payment_type, payment_ids, make_payments, note);
381 $scope.onReceiptOnPayChanged = function(){
382 egCore.hatch.setItem('circ.bills.receiptonpay', $scope.receipt_on_pay.isChecked);
385 function printReceipt(type, payment_ids, payments_made, note) {
386 var payment_blobs = [];
387 angular.forEach(payments_made, function(payment) {
388 var xact_id = payment[0];
390 // find the original transaction in the grid..
391 var xact = $scope.gridControls.allItems().filter(
392 function(item) {return item.id == xact_id})[0];
395 xact : egCore.idl.flatToNestedHash(xact),
400 console.log(js2JSON(payment_blobs[0]));
402 // page data not yet refreshed, capture data from current scope
406 previous_balance : Number($scope.summary.balance_owed()),
407 payment_total : Number($scope.payment_amount),
408 payment_applied : $scope.pending_payment(),
409 amount_voided : Number($scope.session_voided),
410 change_given : $scope.pending_change(),
411 payments : payment_blobs,
412 current_location : egCore.idl.toHash(
413 egCore.org.get(egCore.auth.user().ws_ou()))
416 print_data.new_balance = (
417 print_data.previous_balance * 100 -
418 print_data.payment_applied * 100) / 100;
420 for (var i = 0; i < $scope.receipt_count; i++) {
423 template : 'bill_payment',
429 $scope.showHistory = function() {
430 $location.path('/circ/patron/' +
431 patronSvc.current.id() + '/bill_history/transactions');
434 // For now, only adds billing to first selected item.
435 // Could do batches later if needed
436 $scope.addBilling = function(all) {
438 egBilling.showBillDialog({
439 xact : egCore.idl.flatToNestedHash(all[0]),
440 patron : $scope.patron()
441 }).then(refreshDisplay);
445 $scope.showBillDialog = function($event) {
446 egBilling.showBillDialog({
447 patron : $scope.patron()
448 }).then(refreshDisplay);
451 // Select refunds adds all refunds to the existing selection.
452 // It does not /only/ select refunds
453 $scope.selectRefunds = function() {
454 var ids = $scope.gridControls.selectedItems().map(
455 function(i) { return i.id });
456 angular.forEach($scope.gridControls.allItems(), function(item) {
457 if (Number(item['summary.balance_owed']) < 0)
460 $scope.gridControls.selectItems(ids);
464 // determine on initial page load when all of the grid rows should
466 var selectOnLoad = true;
467 billSvc.fetchBillSettings().then(function(s) {
468 if (s['ui.circ.billing.uncheck_bills_and_unfocus_payment_box']) {
469 $scope.focus_payment = false; // de-focus the payment box
470 $scope.gridControls.focusRowSelector = true;
471 selectOnLoad = false;
472 // if somehow the grid finishes rendering before our settings
473 // arrive, manually de-select everything.
474 $scope.gridControls.selectItems([]);
476 if (s['ui.circ.billing.amount_warn']) {
477 $scope.warn_amount = Number(s['ui.circ.billing.amount_warn']);
479 if (s['ui.circ.billing.amount_limit']) {
480 $scope.max_amount = Number(s['ui.circ.billing.amount_limit']);
482 if (s['circ.staff_client.do_not_auto_attempt_print'] && angular.isArray(s['circ.staff_client.do_not_auto_attempt_print'])) {
483 $scope.disable_auto_print = Boolean(
484 s['circ.staff_client.do_not_auto_attempt_print'].indexOf('Bill Pay') > -1
489 $scope.gridControls.allItemsRetrieved = function() {
491 selectOnLoad = false; // only for initial controller load.
492 // select all non-refund items
493 $scope.gridControls.selectItems(
494 $scope.gridControls.allItems()
495 .filter(function(i) {return i['summary.balance_owed'] > 0})
496 .map(function(i){return i.id})
503 $scope.printBills = function(selected) {
504 if (!selected.length) return;
505 // bills print receipt assumes nested hashes, but our grid
506 // stores flattener data. Fetch the selected xacts as
507 // fleshed pcrud objects and hashify.
508 // (Consider an alternate approach..)
509 var ids = selected.map(function(t){ return t.id });
511 egCore.pcrud.search('mbt',
513 {flesh : 5, flesh_fields : {
514 'mbt' : ['summary', 'circulation'],
515 'circ' : ['target_copy'],
516 'acp' : ['call_number'],
518 'bre' : ['simple_record']
521 {authoritative : true}
526 template : 'bills_current',
528 transactions : xacts,
529 current_location : egCore.idl.toHash(
530 egCore.org.get(egCore.auth.user().ws_ou()))
537 billing_total : xact.billing_total(),
538 billings : xact.billings(),
539 grocery : xact.grocery(),
541 payment_total : xact.payment_total(),
542 payments : xact.payments(),
543 summary : egCore.idl.toHash(xact.summary()),
544 unrecovered : xact.unrecovered(),
545 xact_finish : xact.xact_finish(),
546 xact_start : xact.xact_start(),
547 copy_barcode : xact.circulation().target_copy().barcode(),
548 title : xact.circulation().target_copy().call_number().record().simple_record().title()
555 $scope.applyPayment = function() {
557 if ($scope.payment_amount > $scope.max_amount ) {
559 egCore.strings.PAYMENT_OVER_MAX,
560 { max_amount : ''+$scope.max_amount,
562 $scope.payment_amount = 0;
569 verify_payment_amount().then(
570 function() { // amount confirmed
571 add_payment_note().then(function(pay_note) {
572 add_cc_args().then(function(cc_args) {
573 var note_text = pay_note ? pay_note.value || '' : null;
574 sendPayment(note_text, cc_args);
578 function() { // amount rejected
579 console.warn('payment amount rejected');
580 $scope.payment_amount = 0;
585 function verify_payment_amount() {
586 if ($scope.payment_amount < $scope.warn_amount)
589 return egConfirmDialog.open(
590 egCore.strings.PAYMENT_WARN_AMOUNT_TITLE,
591 egCore.strings.PAYMENT_WARN_AMOUNT,
592 {payment_amount : ''+$scope.payment_amount}
596 function add_payment_note() {
597 if (!$scope.annotate_payment) return $q.when();
598 return egPromptDialog.open(
599 egCore.strings.ANNOTATE_PAYMENT_MSG, '').result;
602 function add_cc_args() {
603 if ($scope.payment_type != 'credit_card_payment')
606 return $uibModal.open({
607 templateUrl : './circ/patron/t_cc_payment_dialog',
610 '$scope','$uibModalInstance',
611 function($scope , $uibModalInstance) {
615 where_process : '1', // internal=1 ; external=0
616 type : 'VISA', // external only
617 billing_first : patronSvc.current.first_given_name(),
618 billing_last : patronSvc.current.family_name()
622 var addr = patronSvc.current.billing_address() ||
623 patronSvc.current.mailing_address();
625 var cc = $scope.context.cc;
626 cc.billing_address = addr.street1() +
627 (addr.street2() ? ' ' + addr.street2() : '');
628 cc.billing_city = addr.city();
629 cc.billing_state = addr.state();
630 cc.billing_zip = addr.post_code();
633 $scope.ok = function() {
634 $uibModalInstance.close($scope.context.cc);
637 $scope.cancel = function() {
638 $uibModalInstance.dismiss();
645 $scope.voidAllBillings = function(items) {
649 angular.forEach(items, function(item) {
651 billSvc.fetchBills(item.id).then(function(bills) {
652 angular.forEach(bills, function(b) {
653 if (b.voided() != 't') {
654 cents += b.amount() * 100;
655 bill_ids.push(b.id())
659 if (bill_ids.length == 0) {
668 $q.all(promises).then(function(){
669 egCore.audio.play('warning.circ.void_billings_confirmation');
670 egConfirmDialog.open(
671 egCore.strings.CONFIRM_VOID_BILLINGS, '',
672 { billIds : ''+bill_ids,
673 amount : ''+(cents/100),
675 billSvc.voidBills(bill_ids).then(function() {
676 $scope.session_voided =
677 ($scope.session_voided * 100 + cents) / 100;
686 $scope.adjustToZero = function(items) {
687 if (items.length == 0) return;
689 var ids = items.map(function(item) {return item.id});
691 egCore.audio.play('warning.circ.adjust_to_zero_confirmation');
692 egConfirmDialog.open(
693 egCore.strings.CONFIRM_ADJUST_TO_ZERO, '',
696 billSvc.adjustBillsToZero(ids).then(function() {
705 // note this is functionally equivalent to selecting a neg. transaction
706 // then clicking Apply Payment -- this just adds a speed bump (ditto
708 $scope.refundXact = function(all) {
709 var items = all.filter(function(item) {
710 return item['summary.balance_owed'] < 0
713 if (items.length == 0) return;
715 var ids = items.map(function(item) {return item.id});
717 egCore.audio.play('warning.circ.refund_confirmation');
718 egConfirmDialog.open(
719 egCore.strings.CONFIRM_REFUND_PAYMENT, '',
722 // reset the received payment amount. this ensures
723 // we're not mingling payments with refunds.
724 $scope.payment_amount = 0;
730 // direct the user to the transaction details page
731 $scope.showFullDetails = function(all) {
733 $location.path('/circ/patron/' +
734 patronSvc.current.id() + '/bill/' + all[0].id);
737 $scope.activateBill = function(xact) {
738 $scope.showFullDetails([xact]);
744 * Displays details of a single transaction
746 .controller('XactDetailsCtrl',
747 ['$scope','$q','$routeParams','egCore','egGridDataProvider','patronSvc','billSvc','egPromptDialog','egBilling','egConfirmDialog',
748 function($scope, $q , $routeParams , egCore , egGridDataProvider , patronSvc , billSvc , egPromptDialog , egBilling , egConfirmDialog ) {
750 $scope.initTab('bills', $routeParams.id);
751 var xact_id = $routeParams.xact_id;
753 var xactGrid = $scope.xactGridControls = {
754 setQuery : function() { return {xact : xact_id} },
755 setSort : function() { return ['billing_ts'] }
758 var paymentGrid = $scope.paymentGridControls = {
759 setQuery : function() { return {xact : xact_id} },
760 setSort : function() { return ['payment_ts'] }
764 $scope.voidBillings = function(bill_list) {
767 angular.forEach(bill_list, function(b) {
768 if (b.voided != 't') {
769 cents += b.amount * 100;
774 if (bill_ids.length == 0) {
779 egCore.audio.play('warning.circ.void_billings_confirmation');
780 egConfirmDialog.open(
781 egCore.strings.CONFIRM_VOID_BILLINGS, '',
782 { billIds : ''+bill_ids,
783 amount : ''+(cents/100),
785 billSvc.voidBills(bill_ids).then(function() {
786 // TODO? $scope.session_voided = ...
788 // refresh bills and summary data
789 // note: no need to update payments
790 patronSvc.fetchUserStats();
792 egBilling.fetchXact(xact_id).then(function(xact) {
803 // batch-edit billing and payment notes, depending on 'type'
804 function editNotes(selected, type) {
805 var notes = selected.map(function(b){ return b.note }).join(',');
806 var ids = selected.map(function(b){ return b.id });
808 // show the note edit prompt
810 egCore.strings.EDIT_BILL_PAY_NOTE, notes, {
812 ok : function(value) {
814 var func = 'updateBillNotes';
815 if (type == 'payment') func = 'updatePaymentNotes';
817 billSvc[func](value, ids).then(function() {
818 if (type == 'payment') {
819 paymentGrid.refresh();
829 $scope.editBillNotes = function(selected) {
830 editNotes(selected, 'bill');
833 $scope.editPaymentNotes = function(selected) {
834 editNotes(selected, 'payment');
837 // -- retrieve our data
838 $scope.total_circs = 0; // start with 0 instead of undefined
839 egBilling.fetchXact(xact_id).then(function(xact) {
842 var copyId = xact.circulation().target_copy().id();
844 egCore.pcrud.search('circbyyr',
845 {copy : copyId}, null, {atomic : true})
846 .then(function(counts) {
847 angular.forEach(counts, function(count) {
848 circ_count += Number(count.count());
850 $scope.total_circs = circ_count;
852 // set the title. only needs to be done on initial page load
853 if (xact.circulation()) {
854 if (xact.circulation().target_copy().call_number().id() == -1) {
855 $scope.title = xact.circulation().target_copy().dummy_title();
857 // TODO: shared bib service?
858 $scope.title = xact.circulation().target_copy()
859 .call_number().record().simple_record().title();
860 $scope.title_id = xact.circulation().target_copy()
861 .call_number().record().id();
868 .controller('BillHistoryCtrl',
869 ['$scope','$q','$routeParams','egCore','patronSvc','billSvc','egPromptDialog','$location',
870 function($scope, $q , $routeParams , egCore , patronSvc , billSvc , egPromptDialog , $location) {
872 $scope.initTab('bills', $routeParams.id);
873 billSvc.userId = $routeParams.id;
874 $scope.bill_tab = $routeParams.history_tab;
877 // link page controller actions defined by sub-controllers here
880 var start = new Date(); // now - 1 year
881 start.setFullYear(start.getFullYear() - 1),
884 xact_finish : new Date()
887 $scope.date_range = function() {
888 var start = $scope.dates.xact_start.toISOString().replace(/T.*/,'');
889 var end = $scope.dates.xact_finish.toISOString().replace(/T.*/,'');
890 var today = new Date().toISOString().replace(/T.*/,'');
891 if (end == today) end = 'now';
897 .controller('BillXactHistoryCtrl',
898 ['$scope','$q','egCore','patronSvc','billSvc','egPromptDialog','$location','egBilling',
899 function($scope, $q , egCore , patronSvc , billSvc , egPromptDialog , $location , egBilling) {
901 // generate a grid query with the current date widget values.
902 function current_grid_query() {
905 {'summary.balance_owed' : {'<>' : 0}},
906 {'summary.last_payment_ts' : {'<>' : null}}
908 xact_start : {between : $scope.date_range()},
913 $scope.gridControls = {
914 selectedItems : function(){return []},
915 activateItem : function(item) {
916 $scope.showFullDetails([item]);
918 // this sets the query on page load
919 setQuery : current_grid_query
922 $scope.actions.apply_date_range = function() {
923 // tells the grid to re-draw itself with the new query
924 $scope.gridControls.setQuery(current_grid_query());
927 // TODO; move me to service
928 function selected_payment_info() {
929 var info = {owed : 0, billed : 0, paid : 0};
930 angular.forEach($scope.gridControls.selectedItems(), function(item) {
931 info.owed += Number(item['summary.balance_owed']) * 100;
932 info.billed += Number(item['summary.total_owed']) * 100;
933 info.paid += Number(item['summary.total_paid']) * 100;
941 $scope.totals.selected_billed = function() {
942 return selected_payment_info().billed;
944 $scope.totals.selected_paid = function() {
945 return selected_payment_info().paid;
948 $scope.showFullDetails = function(all) {
950 $location.path('/circ/patron/' +
951 patronSvc.current.id() + '/bill/' + all[0].id);
954 // For now, only adds billing to first selected item.
955 // Could do batches later if needed
956 $scope.addBilling = function(all) {
958 egBilling.showBillDialog({
959 xact : egCore.idl.flatToNestedHash(all[0]),
960 patron : $scope.patron()
962 $scope.gridControls.refresh();
963 patronSvc.fetchUserStats();
968 $scope.printBills = function(selected) { // FIXME: refactor me
969 if (!selected.length) return;
970 // bills print receipt assumes nested hashes, but our grid
971 // stores flattener data. Fetch the selected xacts as
972 // fleshed pcrud objects and hashify.
973 // (Consider an alternate approach..)
974 var ids = selected.map(function(t){ return t.id });
976 egCore.pcrud.search('mbt',
978 {flesh : 5, flesh_fields : {
979 'mbt' : ['summary', 'circulation'],
980 'circ' : ['target_copy'],
981 'acp' : ['call_number'],
983 'bre' : ['simple_record']
986 {authoritative : true}
991 template : 'bills_historical',
993 transactions : xacts,
994 current_location : egCore.idl.toHash(
995 egCore.org.get(egCore.auth.user().ws_ou()))
1002 billing_total : xact.billing_total(),
1003 billings : xact.billings(),
1004 grocery : xact.grocery(),
1006 payment_total : xact.payment_total(),
1007 payments : xact.payments(),
1008 summary : egCore.idl.toHash(xact.summary()),
1009 unrecovered : xact.unrecovered(),
1010 xact_finish : xact.xact_finish(),
1011 xact_start : xact.xact_start(),
1012 copy_barcode : xact.circulation().target_copy().barcode(),
1013 title : xact.circulation().target_copy().call_number().record().simple_record().title()
1015 xacts.push(newXact);
1021 .controller('BillPaymentHistoryCtrl',
1022 ['$scope','$q','egCore','patronSvc','billSvc','$location',
1023 function($scope, $q , egCore , patronSvc , billSvc , $location) {
1025 // generate a grid query with the current date widget values.
1026 function current_grid_query() {
1028 'payment_ts' : {between : $scope.date_range()},
1029 'xact.usr' : billSvc.userId
1033 $scope.gridControls = {
1034 selectedItems : function(){return []},
1035 activateItem : function(item) {
1036 $scope.showFullDetails([item]);
1038 setSort : function() {
1039 return [{'payment_ts' : 'DESC'}, 'id'];
1041 setQuery : current_grid_query
1044 $scope.actions.apply_date_range = function() {
1045 // tells the grid to re-draw itself with the new query
1046 $scope.gridControls.setQuery(current_grid_query());
1049 $scope.showFullDetails = function(all) {
1051 $location.path('/circ/patron/' +
1052 patronSvc.current.id() + '/bill/' + all[0]['xact.id']);
1055 $scope.totals.selected_paid = function() {
1057 angular.forEach($scope.gridControls.selectedItems(), function(payment) {
1058 paid += Number(payment.amount) * 100;