b355845b28c1fd4128899e23d71205f8a169342d
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / bill_wizard.js
1 function $(id) { return document.getElementById(id); }
2
3 function retrieve_mbts() {
4     g.network.simple_request('FM_MBTS_RETRIEVE.authoritative',[ses(),g.mbts_id],
5         function(req) {
6             try {
7                 g.mbts = req.getResultObject();
8                 $('mbts_id').value = g.mbts.id();
9                 $('mbts_xact_type').value = g.mbts.xact_type();
10                 $('mbts_xact_start').value = util.date.formatted_date( g.mbts.xact_start(), '%{localized}' );
11                 $('mbts_xact_finish').value = g.mbts.xact_finish() ? util.date.formatted_date( g.mbts.xact_finish(), '%{localized}' ) : '';
12                 $('mbts_total_owed').value = g.mbts.total_owed() ? util.money.sanitize( g.mbts.total_owed() ) : '';
13                 $('mbts_total_paid').value = g.mbts.total_paid() ? util.money.sanitize( g.mbts.total_paid() ) : '';
14                 $('mbts_balance_owed').value = g.mbts.balance_owed() ? util.money.sanitize( g.mbts.balance_owed() ) : '';
15                 $('xact_type').value = g.mbts.xact_type(); $('xact_type').disabled = true;
16             } catch(E) {
17                 g.error.sdump('D_ERROR',E);
18             }
19         }
20     );
21 }
22
23 function retrieve_circ() {
24     JSAN.use('util.widgets');
25
26     function render_circ(r_circ) {
27
28         $('title_label').hidden = false;
29         $('checked_out_label').hidden = false;
30         $('due_label').hidden = false;
31         $('checked_in_label').hidden = false;
32         $('checked_out').value = r_circ.xact_start() ? util.date.formatted_date( r_circ.xact_start(), '%{localized}' ) : '';
33         $('checked_in').value = r_circ.checkin_time() ? util.date.formatted_date( r_circ.checkin_time(), '%{localized}' ) : '';
34         $('due').value = r_circ.due_date() ? util.date.formatted_date( r_circ.due_date(), '%{localized}' ) : '';
35
36         g.network.simple_request(
37             'MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.authoritative',
38             [ typeof r_circ.target_copy() == 'object' ? r_circ.target_copy().id() : r_circ.target_copy() ],
39             function (rreq) {
40                 var r_mvr = rreq.getResultObject();
41                 if (instanceOf(r_mvr,mvr)) {
42                     util.widgets.remove_children('title');
43                     $('title').appendChild( document.createTextNode( r_mvr.title() ) );
44                 } else {
45                     g.network.simple_request(
46                         'FM_ACP_RETRIEVE',
47                         [ typeof r_circ.target_copy() == 'object' ? r_circ.target_copy().id() : r_circ.target_copy() ],
48                         function (rrreq) {
49                             var r_acp = rrreq.getResultObject();
50                             if (instanceOf(r_acp,acp)) {
51                                 util.widgets.remove_children('title');
52                                 $('title').appendChild( document.createTextNode( r_acp.dummy_title() ) );
53                             }
54                         }
55                     );
56                 }
57             }
58         );
59
60     }
61
62     if (g.circ) {
63         render_circ(g.circ);
64     } else {
65         g.network.simple_request('FM_CIRC_RETRIEVE_VIA_ID', [ ses(), g.mbts_id ],
66             function (req) {
67                 var r_circ = req.getResultObject();
68                 if (instanceOf(r_circ,circ)) {
69                     render_circ(r_circ);
70                 }
71             }
72         );
73     }
74 }
75
76 function retrieve_patron() {
77     JSAN.use('patron.util'); 
78
79     g.patron_id = xul_param('patron_id',{'modal_xulG':true});
80     g.au_obj = xul_param('patron',{'modal_xulG':true});
81
82     if (! g.au_obj) {
83         g.au_obj = patron.util.retrieve_fleshed_au_via_id( ses(), g.patron_id );
84     }
85
86     if (g.au_obj) {
87         $('patron_name').setAttribute('value', 
88             patron.util.format_name( g.au_obj ) + ' : ' + g.au_obj.card().barcode() 
89         );
90     }
91
92 }
93
94 function patron_bill_init() {
95     try {
96         if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
97         JSAN.errorLevel = "die"; // none, warn, or die
98         JSAN.addRepository('/xul/server/');
99         JSAN.use('util.error'); g.error = new util.error();
100         g.error.sdump('D_TRACE','my_init() for patron_display.xul');
101         g.OpenILS = {}; JSAN.use('OpenILS.data'); g.OpenILS.data = new OpenILS.data();
102         g.OpenILS.data.init({'via':'stash'});
103         JSAN.use('util.network'); g.network = new util.network();
104         JSAN.use('util.date');
105         JSAN.use('util.money');
106         JSAN.use('util.widgets');
107         JSAN.use('util.functional');
108         var override_default_billing_type = xul_param('override_default_billing_type',{'modal_xulG':true});
109         var billing_list = util.functional.filter_list( g.OpenILS.data.list.cbt, function (x) { return x.id() >= 100 || x.id() == override_default_billing_type } );
110         var ml = util.widgets.make_menulist(
111             util.functional.map_list(
112                 billing_list.sort( function(a,b) { if (a.name()>b.name()) return 1; if (a.name()<b.name()) return -1; return 0; } ), //g.OpenILS.data.list.billing_type.sort(),
113                 function(obj) { return [ obj.name(), obj.id() ]; } //function(obj) { return [ obj, obj ]; }
114             ),
115             override_default_billing_type || billing_list.sort( function(a,b) { if (a.name()>b.name()) return 1; if (a.name()<b.name()) return -1; return 0; } )[0].id()
116         );
117         ml.setAttribute('id','billing_type');
118         document.getElementById('menu_placeholder').appendChild(ml);
119         ml.addEventListener(
120             'command',
121             function() {
122                 if ( g.OpenILS.data.hash.cbt[ ml.value ] ) {
123                     $('bill_amount').value = g.OpenILS.data.hash.cbt[ ml.value ].default_price();
124                 }
125             },
126             false
127         ); 
128
129         retrieve_patron();
130
131         $('wizard_billing_location').setAttribute('value', g.OpenILS.data.hash.aou[ g.OpenILS.data.list.au[0].ws_ou() ].name() );
132
133         if ( g.OpenILS.data.hash.cbt[ ml.value ] ) {
134             $('bill_amount').value = g.OpenILS.data.hash.cbt[ ml.value ].default_price();
135         }
136         var override_default_price = xul_param('override_default_price',{'modal_xulG':true});
137         if (override_default_price) {
138             $('bill_amount').value = override_default_price;
139         }
140         $('bill_amount').select(); $('bill_amount').focus();
141
142         g.circ = xul_param('circ',{'modal_xulG':true});
143         if (xul_param('xact_id',{'modal_xulG':true})) { 
144             g.mbts_id = xul_param('xact_id',{'modal_xulG':true});
145             $('summary').hidden = false; 
146             retrieve_mbts();
147             retrieve_circ();
148         }
149
150     } catch(E) {
151         var err_msg = $("commonStrings").getFormattedString('common.exception', ['patron/bill_wizard.xul', E]);
152         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
153         alert(err_msg);
154     }
155
156 }
157
158 function patron_bill_finish() {
159     try {
160         var do_not_process_bill = xul_param('do_not_process_bill',{'modal_xulG':true});
161         var xact_id = xul_param('xact_id',{'modal_xulG':true});
162
163         if (do_not_process_bill) {
164
165             update_modal_xulG(
166                 {
167                     'proceed' : true,
168                     'cbt_id' : $('billing_type').value,
169                     'amount' : $('bill_amount').value,
170                     'note' : $('bill_note').value
171                 }
172             );
173
174         } else {
175
176             if (!xact_id) {
177                     var grocery = new mg();
178                         grocery.isnew('1');
179                         grocery.billing_location( g.OpenILS.data.list.au[0].ws_ou() );
180                         grocery.usr( g.au_obj.id() );
181                         grocery.note( $('bill_note').value );
182                     xact_id = g.network.request(
183                         api.FM_MG_CREATE.app,
184                         api.FM_MG_CREATE.method,
185                         [ ses(), grocery ]
186                     );
187             }
188             if (typeof xact_id.ilsevent == 'undefined') {
189                 JSAN.use('util.money');
190                 var billing = new mb();
191                     billing.isnew('1');
192                     billing.note( $('bill_note').value );
193                     billing.xact( xact_id );
194                     billing.amount( util.money.sanitize( $('bill_amount').value ) );
195                     billing.btype( $('billing_type').value );
196                     billing.billing_type( g.OpenILS.data.hash.cbt[$('billing_type').value].name() );
197                 var mb_id = g.network.request(
198                     api.FM_MB_CREATE.app,
199                     api.FM_MB_CREATE.method,
200                     [ ses(), billing ]
201                 );
202                 if (typeof mb_id.ilsevent != 'undefined') throw(mb_id);
203                 //alert($('patronStrings').getString('staff.patron.bill_wizard.patron_bill_finish.billing_added'));
204
205                 update_modal_xulG(
206                     {
207                         mb_id : mb_id,
208                         xact_id : xact_id
209                     }
210                 );
211
212             } else {
213                 throw(xact_id);
214             }
215
216         }
217     } catch(E) {
218         g.error.standard_unexpected_error_alert('bill_wizard',E);
219     }
220 }
221
222