]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/conify/global/actor/org_unit.js
disconnect on error, at least to clear the translator cache
[working/Evergreen.git] / Open-ILS / web / conify / global / actor / org_unit.js
1 /*
2 # ---------------------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service / Equinox Software, Inc
4 # Mike Rylander <miker@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------------------
16 */
17
18 dojo.require('fieldmapper.dojoData');
19 dojo.require('openils.widget.TranslatorPopup');
20 dojo.require('dojo.parser');
21 dojo.require('dojo.data.ItemFileWriteStore');
22 dojo.require('dojo.date.stamp');
23 dojo.require('dijit.form.TextBox');
24 dojo.require('dijit.form.TimeTextBox');
25 dojo.require('dijit.form.ValidationTextBox');
26 dojo.require('dijit.form.CheckBox');
27 dojo.require('dijit.form.FilteringSelect');
28 dojo.require('dijit.Tree');
29 dojo.require('dijit.layout.ContentPane');
30 dojo.require('dijit.layout.TabContainer');
31 dojo.require('dijit.layout.LayoutContainer');
32 dojo.require('dijit.layout.SplitContainer');
33 dojo.require('dojox.widget.Toaster');
34 dojo.require('dojox.fx');
35 dojo.requireLocalization("openils.conify", "conify");
36
37 // some handy globals
38 var cgi = new CGI();
39 var cookieManager = new HTTP.Cookies();
40 var ses = cookieManager.read('ses') || cgi.param('ses');
41 var pCRUD = new OpenSRF.ClientSession('open-ils.pcrud');
42
43 var current_ou, current_ou_hoo;
44 var virgin_ou_id = -1;
45
46 var aou_strings = dojo.i18n.getLocalization('openils.conify', 'conify');
47
48 //var ou_type_store = new dojo.data.ItemFileWriteStore({ data : aout.toStoreData( globalOrgTypes ) });
49
50 var highlighter = {};
51
52 function status_update (markup) {
53         if (parent !== window && parent.status_update) parent.status_update( markup );
54 }
55
56 function save_org () {
57         var modified_ou = new aou().fromStoreItem( current_ou );
58         modified_ou.ischanged( 1 );
59
60         new_kid_button.disabled = false;
61         save_ou_button.disabled = false;
62         delete_ou_button.disabled = false;
63
64     if (!pCRUD.connect()) {
65                 highlighter.editor_pane.red.play();
66                 status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
67         return null;
68     }
69     
70         var commit = pCRUD.request({
71                 method : 'open-ils.pcrud.transaction.commit',
72                 timeout : 10,
73                 params : [ ses, modified_ou ],
74                 onerror : function (r) {
75                         highlighter.editor_pane.red.play();
76                         status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
77             pCRUD.disconnect();
78             throw 'commit error';
79                 },
80                 oncomplete : function (r) {
81                         var res = r.recv();
82                         if ( res ) {
83                                 ou_list_store.setValue( current_ou, 'ischanged', 0 );
84                                 highlighter.editor_pane.green.play();
85                                 status_update( dojo.string.substitute( aou_strings.SUCCESS_SAVE, [ou_list_store.getValue( current_ou, 'name' )] ) );
86                         } else {
87                                 highlighter.editor_pane.red.play();
88                                 status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
89                 throw 'commit error';
90                         }
91             pCRUD.disconnect();
92                 },
93         });
94
95         var update = pCRUD.request({
96                 method : 'open-ils.pcrud.update.aou',
97                 timeout : 10,
98                 params : [ ses, modified_ou ],
99                 onerror : function (r) {
100                         highlighter.editor_pane.red.play();
101                         status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
102             pCRUD.disconnect();
103             throw 'update error';
104                 },
105                 oncomplete : function (r) {
106                         var res = r.recv();
107                         if ( res && res.content() ) {
108                 commit.send();
109                         } else {
110                                 highlighter.editor_pane.red.play();
111                                 status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
112                 pCRUD.disconnect();
113                 throw 'update error';
114                         }
115                 },
116         });
117
118         var begin = pCRUD.request({
119                 method : 'open-ils.pcrud.transaction.begin',
120                 timeout : 10,
121                 params : [ ses, modified_ou ],
122                 onerror : function (r) {
123                         highlighter.editor_pane.red.play();
124                         status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
125             pCRUD.disconnect();
126             throw 'begin error';
127                 },
128                 oncomplete : function (r) {
129                         var res = r.recv();
130                         if ( res && res.content() ) {
131                 update.send();
132                         } else {
133                                 highlighter.editor_pane.red.play();
134                                 status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
135                 pCRUD.disconnect();
136                 throw 'begin error';
137                         }
138                 },
139         });
140
141     begin.send();
142 }
143         
144 function hoo_load () {
145         // empty result not coming through ...
146         current_ou_hoo = new aouhoo().fromHash({id:ou_list_store.getValue( current_ou, 'id' )});
147         current_ou_hoo.isnew(1);
148
149         pCRUD.request({
150                 method : 'open-ils.pcrud.retrieve.aouhoo',
151                 params : [ ses, ou_list_store.getValue( current_ou, 'id' ) ],
152                 onerror : function (r) { 
153                         throw dojo.string.substitute(aou_strings.ERROR_FETCHING_HOURS, [ou_list_store.getValue( current_ou, 'name' )]);
154                 },
155                 oncomplete : function (r) {
156                         current_ou_hoo = null;
157
158                         var res = r.recv();
159                         if (res) {
160                                 if (res.content()) current_ou_hoo = res.content();
161                         }
162
163                         if (!current_ou_hoo) {
164                                 current_ou_hoo = new aouhoo().fromHash({id:ou_list_store.getValue( current_ou, 'id' )});
165                                 current_ou_hoo.isnew(1);
166                                 for (var i = 0; i < 7; i++) {
167                                         current_ou_hoo['dow_' + i + '_open']('09:00:00');
168                                         current_ou_hoo['dow_' + i + '_close']('17:00:00');
169                                 }
170                         }
171
172                         for (var i = 0; i < 7; i++) {
173                                 window['dow_' + i + '_open'].setValue(
174                                         dojo.date.stamp.fromISOString( 'T' + current_ou_hoo['dow_' + i + '_open']() )
175                                 );
176                                 window['dow_' + i + '_close'].setValue(
177                                         dojo.date.stamp.fromISOString( 'T' + current_ou_hoo['dow_' + i + '_close']() )
178                                 );
179                         }
180
181                         highlighter.hoo_pane.green.play();
182                 }
183         }).send();
184
185 }
186
187 function addr_load () {
188         // empty result not coming through ...
189
190         save_ill_address.disabled = false;
191         save_holds_address.disabled = false;
192         save_mailing_address.disabled = false;
193         save_billing_address.disabled = false;
194
195         if (ou_list_store.getValue( current_ou, 'billing_address' )) {
196                 pCRUD.request({
197                         method : 'open-ils.pcrud.retrieve.aoa',
198                         params : [ ses, ou_list_store.getValue( current_ou, 'billing_address' ) ],
199                         onerror : function (r) {
200                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_PHYSICAL, [ou_list_store.getValue( current_ou, 'name' )]);
201                         },
202                         oncomplete : function (r) {
203                                 current_billing_address = null;
204
205                                 var res = r.recv();
206                                 if (res) {
207                                         if (res.content()) current_billing_address = res.content();
208                                 }
209
210                                 if (!current_billing_address) {
211                                         current_billing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
212                                         current_billing_address.isnew(1);
213                                 }
214
215                                 set_addr_inputs('billing');
216                                 highlighter.addresses_pane.green.play();
217                         }
218                 }).send();
219         } else {
220                 current_billing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
221                 current_billing_address.isnew(1);
222                 set_addr_inputs('billing');
223         }
224
225         if (ou_list_store.getValue( current_ou, 'mailing_address' )) {
226                 pCRUD.request({
227                         method : 'open-ils.pcrud.retrieve.aoa',
228                         params : [ ses, ou_list_store.getValue( current_ou, 'mailing_address' ) ],
229                         onerror : function (r) {
230                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_MAILING, [ou_list_store.getValue( current_ou, 'name' )]);
231                         },
232                         oncomplete : function (r) {
233                                 current_mailing_address = null;
234
235                                 var res = r.recv();
236                                 if (res) {
237                                         if (res.content()) current_mailing_address = res.content();
238                                 }
239
240                                 if (!current_mailing_address) {
241                                         current_mailing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
242                                         current_mailing_address.isnew(1);
243                                 }
244
245                                 set_addr_inputs('mailing');
246                                 highlighter.addresses_pane.green.play();
247                         }
248                 }).send();
249         } else {
250                 current_mailing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
251                 current_mailing_address.isnew(1);
252                 set_addr_inputs('mailing');
253         }
254
255         if (ou_list_store.getValue( current_ou, 'holds_address' )) {
256                 pCRUD.request({
257                         method : 'open-ils.pcrud.retrieve.aoa',
258                         params : [ ses, ou_list_store.getValue( current_ou, 'holds_address' ) ],
259                         onerror : function (r) {
260                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_HOLDS, [ou_list_store.getValue( current_ou, 'name' )]);
261                         },
262                         oncomplete : function (r) {
263                                 current_holds_address = null;
264
265                                 var res = r.recv();
266                                 if (res) {
267                                         if (res.content()) current_holds_address = res.content();
268                                 }
269
270                                 if (!current_holds_address) {
271                                         current_holds_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
272                                         current_holds_address.isnew(1);
273                                 }
274
275                                 set_addr_inputs('holds');
276                                 highlighter.addresses_pane.green.play();
277                         }
278                 }).send();
279         } else {
280                 current_holds_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
281                 current_holds_address.isnew(1);
282                 set_addr_inputs('holds');
283         }
284
285         if (ou_list_store.getValue( current_ou, 'ill_address' )) {
286                 pCRUD.request({
287                         method : 'open-ils.pcrud.retrieve.aoa',
288                         params : [ ses, ou_list_store.getValue( current_ou, 'ill_address' ) ],
289                         onerror : function (r) {
290                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_ILL, [ou_list_store.getValue( current_ou, 'name' )]);
291                         },
292                         oncomplete : function (r) {
293                                 current_ill_address = null;
294
295                                 var res = r.recv();
296                                 if (res) {
297                                         if (res.content()) current_ill_address = res.content();
298                                 }
299
300                                 if (!current_ill_address) {
301                                         current_ill_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
302                                         current_ill_address.isnew(1);
303                                 }
304
305                                 set_addr_inputs('ill');
306                                 highlighter.addresses_pane.green.play();
307                         }
308                 }).send();
309         } else {
310                 current_ill_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
311                 current_ill_address.isnew(1);
312                 set_addr_inputs('ill');
313         }
314
315 }
316
317 function set_addr_inputs (type) {
318         window[type + '_addr_valid'].setChecked( window['current_' + type + '_address'].valid() == 't' ? true : false );
319         window[type + '_addr_type'].setValue( window['current_' + type + '_address'].address_type() || '' );
320         window[type + '_addr_street1'].setValue( window['current_' + type + '_address'].street1() || '' );
321         window[type + '_addr_street2'].setValue( window['current_' + type + '_address'].street2() || '' );
322         window[type + '_addr_city'].setValue( window['current_' + type + '_address'].city() || '' );
323         window[type + '_addr_county'].setValue( window['current_' + type + '_address'].county() || '' );
324         window[type + '_addr_country'].setValue( window['current_' + type + '_address'].country() || '' );
325         window[type + '_addr_state'].setValue( window['current_' + type + '_address'].state() || '' );
326         window[type + '_addr_post_code'].setValue( window['current_' + type + '_address'].post_code() || '' );
327 }
328