]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/bills.js
array not object
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / bills.js
1 dump('entering patron.bills.js\n');
2
3 if (typeof patron == 'undefined') patron = {};
4 patron.bills = function (params) {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.network'); this.network = new util.network();
8         this.OpenILS = {}; JSAN.use('OpenILS.data'); this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init({'via':'stash'});
9 }
10
11 patron.bills.prototype = {
12
13         'current_payments' : [],
14
15         'init' : function( params ) {
16
17                 var obj = this;
18
19                 obj.session = params['session'];
20                 obj.patron_id = params['patron_id'];
21
22                 JSAN.use('util.list'); obj.list = new util.list('bill_list');
23
24                 function getString(s) { return obj.OpenILS.data.entities[s]; }
25                 obj.list.init(
26                         {
27                                 'columns' : [
28                                                 {
29                                                         'id' : 'checkbox', 'label' : '', 'flex' : 0, 'primary' : false, 'hidden' : false,
30                                                         'render' : 'document.createElement("checkbox")'
31                                                 },
32                                                 {
33                                                         'id' : 'xact_dates', 'label' : getString('staff.bills_xact_dates_label'), 'flex' : 0,
34                                                         'primary' : false, 'hidden' : false, 'render' : 'xact_dates_box(my.mbts)'
35                                                 },
36                                                 {
37                                                         'id' : 'notes', 'label' : getString('staff.bills_information'), 'flex' : 0,
38                                                         'primary' : false, 'hidden' : false, 'render' : 'info_box(my.mbts)'
39                                                 },
40                                                 {
41                                                         'id' : 'money', 'label' : getString('staff.bills_money_label'), 'flex' : 0,
42                                                         'primary' : false, 'hidden' : false, 'render' : 'money_box(my.mbts)'
43                                                 },
44                                                 {
45                                                         'id' : 'current_pay', 'label' : getString('staff.bills_current_payment_label'), 'flex' : 0, 
46                                                         'render' : 'document.createElement("textbox")'
47                                                 }
48                                 ],
49                                 'map_row_to_column' : function(row,col) {
50                                         // row contains { 'my' : { 'mbts' : ... } }
51                                         // col contains one of the objects listed above in columns
52
53                                         var obj = {}; obj.OpenILS = {}; 
54                                         JSAN.use('util.error'); obj.error = new util.error();
55                                         //JSAN.use('OpenILS.data'); obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
56
57                                         var my = row.my;
58                                         var value;
59                                         try {
60                                                 value = eval( col.render );
61                                         } catch(E) {
62                                                 obj.error.sdump('D_ERROR','map_row_to_column: ' + E);
63                                                 value = '???';
64                                         }
65                                         return value;
66                                 },
67                         }
68                 );
69
70                 JSAN.use('util.controller'); obj.controller = new util.controller();
71                 obj.controller.init(
72                         {
73                                 'control_map' : {
74                                         'cmd_broken' : [
75                                                 ['command'],
76                                                 function() { alert('Not Yet Implemented'); }
77                                         ],
78                                         'cmd_bill_wizard' : [
79                                                 ['command'],
80                                                 function() { alert('Not Yet Implemented'); }
81                                         ],
82                                         'cmd_change_to_credit' : [
83                                                 ['command'],
84                                                 function() {
85                                                         obj.change_to_credit();
86                                                 }
87                                         ],
88                                         'cmd_bill_apply_payment' : [
89                                                 ['command'],
90                                                 function() {
91                                                         obj.apply_payment();
92                                                 }
93                                         ],
94                                         'bill_total_owed' : [
95                                                 ['render'],
96                                                 function(e) { return function() {}; }
97                                         ],
98                                         'payment_type' : [
99                                                 ['render'],
100                                                 function(e) { return function() {}; }
101                                         ],
102                                         'bill_payment_amount' : [
103                                                 ['change'],
104                                                 function(ev) {
105                                                         JSAN.use('util.money');
106                                                         var tb = ev.target;
107                                                         tb.value = util.money.cents_as_dollars( util.money.dollars_float_to_cents_integer( tb.value ) );
108                                                         var total = util.money.dollars_float_to_cents_integer( tb.value );
109                                                         for (var i = 0; i < obj.current_payments.length; i++) {
110                                                                 var bill = obj.current_payments[i];
111                                                                 if (bill.checkbox.checked) {
112                                                                         var bo = util.money.dollars_float_to_cents_integer( bill.balance_owed );
113                                                                         if ( bo > total ) {
114                                                                                 bill.textbox.value = util.money.cents_as_dollars( total );
115                                                                                 total = 0;
116                                                                         } else {
117                                                                                 bill.textbox.value = util.money.cents_as_dollars( bo );
118                                                                                 total = total - bo;
119                                                                         }
120                                                                 } else {
121                                                                         bill.textbox.value = '0.00';
122                                                                 }
123                                                         }
124                                                         obj.update_payment_applied();
125                                                 }; 
126                                         ],
127                                         'bill_payment_applied' : [
128                                                 ['render'],
129                                                 function(e) { return function() {}; }
130                                         ],
131                                         'bill_change_amount' : [
132                                                 ['change'],
133                                                 function(ev) {
134                                                         JSAN.use('util.money');
135                                                         var tb = ev.target;
136                                                         var proposed_change = util.money.dollars_float_to_cents_integer( tb.value );
137                                                         var proposed_credit = 0;
138                                                         obj.update_payment_applied();
139                                                         var real_change = util.money.dollars_float_to_cents_integer( tb.value );
140                                                         if ( proposed_change > real_change ) {
141                                                                 obj.error.sdump('D_ERROR','Someone wanted more money than they deserved\n');
142                                                                 proposed_change = real_change;
143                                                         } else if ( real_change > proposed_change ) {
144                                                                 proposed_credit = real_change - proposed_change;
145                                                         }
146                                                         tb.value = util.money.cents_as_dollars( proposed_change );
147                                                         obj.controller.view.bill_credit_amount.value = util.money.cents_as_dollars( proposed_credit );
148                                                 }
149                                         ],
150                                         'bill_credit_amount' : [
151                                                 ['render'],
152                                                 function(e) { return function() {}; }
153                                         ],
154                                         'bill_new_balance' : [
155                                                 ['render'],
156                                                 function(e) { return function() {}; }
157                                         ],
158                                 }
159                         }
160                 );
161
162         },
163
164         'apply_payment' : function() {
165                 var obj = this;
166                 var payment_blob = {};
167                 payment_blob.userid = obj.patron_id;
168                 payment_blob.note = '';
169                 payment_blob.cash_drawer = 1; // FIXME: get new Config() to work
170                 payment_blob.payment_type = obj.controller.view.payment_type.value;
171                 payment_blob.payments = [];
172                 payment_blob.patron_credit = obj.controller.view.bill_credit_amount.value;
173                 for (var i = 0; i < obj.current_payments.length; i++) {
174                         var tb = obj.current_payments[ i ].textbox;
175                         if ( !(tb.value == '0.00' || tb.value == '') ) {
176                                 payment_blob.payments.push( 
177                                         [
178                                                 obj.current_payments[ i ].mbts_id,
179                                                 tb.value
180                                         ]
181                                 );
182                         }
183                 }
184                 try {
185                         if ( obj.pay( payment_blob ) ) {
186
187                                 //FIXME//Refresh
188
189                         }
190
191                 } catch(E) {
192
193                         obj.error.sdump('D_ERROR',E);
194                 }
195         },
196
197         'pay' : function(payment_blob) {
198                 try {
199                         var robj = this.network.retrieve(
200                                 api.bill_pay.app,       
201                                 api.bill_pay.method,
202                                 [ this.session, payment_blob ]
203                         );
204                         if (robj && robj.ilsevent && robj.ilsevent == 0) {
205                                 return true;
206                         } else {
207                                 throw robj;
208                         }
209                 } catch(E) {
210                         this.error.sdump('D_ERROR','patron.bills.pay: ' + E);
211                         return false;
212                 }
213         },
214
215         'update_payment_applied' : function() {
216                 JSAN.use('util.money');
217                 var obj = this;
218                 var total_applied = 0;
219                 for (var i = 0; i < obj.current_payments.length; i++) {
220                         total_applied += util.money.dollars_float_to_cents_integer( obj.current_payments[ i ].textbox.value );
221                 }
222                 var total_payment = 0;
223                 if (obj.controller.view.bill_payment_amount.value) {
224                         try {
225                                 total_payment = util.money.dollars_float_to_cents_integer( obj.controller.view.bill_payment_amount.value );
226                         } catch(E) {
227                                 obj.error.sdump('D_ERROR',E + '\n');
228                         }
229                 }
230                 if ( total_applied > total_payment ) {
231                         total_payment = total_applied;
232                         obj.controller.view.bill_payment_amount.value = util.money.cents_as_dollars( total_applied );
233                 }
234                 obj.controller.view.bill_payment_applied.value = util.money.cents_as_dollars( total_applied );
235                 obj.controller.view.bill_credit_amount.value = '';
236                 if (total_payment > total_applied ) {
237                         obj.controller.view.bill_change_amount.value = util.money.cents_as_dollars( total_payment - total_applied);
238                         obj.controller.view.bill_credit_amount.value = '0.00';
239                 } else {
240                         obj.controller.view.bill_change_amount.value = '0.00';
241                         obj.controller.view.bill_credit_amount.value = '0.00';
242                 }
243                 var total_owed = util.money.dollars_float_to_cents_integer( obj.controller.view.bill_total_owed.value );
244                 obj.controller.view.bill_new_balance.value = util.money.cents_as_dollars( total_owed - total_applied );
245         },
246
247         'change_to_credit' : function() {
248                 JSAN.use('util.money');
249                 var obj = this;
250                 var tb = obj.controller.view.bill_change_amount;
251                 var proposed_change = 0;
252                 var proposed_credit = util.money.dollars_float_to_cents_integer( tb.value );
253                 obj.update_payment_applied();
254                 var real_change = util.money.dollars_float_to_cents_integer( tb.value );
255                 if ( proposed_change > real_change ) {
256                         obj.error.sdump('D_ERROR','Someone wanted more money than they deserved\n');
257                         proposed_change = real_change;
258                 } else if ( real_change > proposed_change ) {
259                         proposed_credit = real_change - proposed_change;
260                 }
261                 tb.value = util.money.cents_as_dollars( proposed_change );
262                 obj.controller.view.bill_credit_amount.value = util.money.cents_as_dollars( proposed_credit );
263         }
264 }
265
266 dump('exiting patron.bills.js\n');