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