]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/conify/global/actor/org_unit.js
thow and error on, well, error for easier debugging in firebug
[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             throw 'update error';
103                 },
104                 oncomplete : function (r) {
105                         var res = r.recv();
106                         if ( res && res.content() ) {
107                 commit.send();
108                         } else {
109                                 highlighter.editor_pane.red.play();
110                                 status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
111                 pCRUD.disconnect();
112                 throw 'update error';
113                         }
114                 },
115         });
116
117         var begin = pCRUD.request({
118                 method : 'open-ils.pcrud.transaction.begin',
119                 timeout : 10,
120                 params : [ ses, modified_ou ],
121                 onerror : function (r) {
122                         highlighter.editor_pane.red.play();
123                         status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
124             throw 'begin error';
125                 },
126                 oncomplete : function (r) {
127                         var res = r.recv();
128                         if ( res && res.content() ) {
129                 update.send();
130                         } else {
131                                 highlighter.editor_pane.red.play();
132                                 status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
133                 pCRUD.disconnect();
134                 throw 'begin error';
135                         }
136                 },
137         });
138
139     begin.send();
140 }
141         
142 function hoo_load () {
143         // empty result not coming through ...
144         current_ou_hoo = new aouhoo().fromHash({id:ou_list_store.getValue( current_ou, 'id' )});
145         current_ou_hoo.isnew(1);
146
147         pCRUD.request({
148                 method : 'open-ils.pcrud.retrieve.aouhoo',
149                 params : [ ses, ou_list_store.getValue( current_ou, 'id' ) ],
150                 onerror : function (r) { 
151                         throw dojo.string.substitute(aou_strings.ERROR_FETCHING_HOURS, [ou_list_store.getValue( current_ou, 'name' )]);
152                 },
153                 oncomplete : function (r) {
154                         current_ou_hoo = null;
155
156                         var res = r.recv();
157                         if (res) {
158                                 if (res.content()) current_ou_hoo = res.content();
159                         }
160
161                         if (!current_ou_hoo) {
162                                 current_ou_hoo = new aouhoo().fromHash({id:ou_list_store.getValue( current_ou, 'id' )});
163                                 current_ou_hoo.isnew(1);
164                                 for (var i = 0; i < 7; i++) {
165                                         current_ou_hoo['dow_' + i + '_open']('09:00:00');
166                                         current_ou_hoo['dow_' + i + '_close']('17:00:00');
167                                 }
168                         }
169
170                         for (var i = 0; i < 7; i++) {
171                                 window['dow_' + i + '_open'].setValue(
172                                         dojo.date.stamp.fromISOString( 'T' + current_ou_hoo['dow_' + i + '_open']() )
173                                 );
174                                 window['dow_' + i + '_close'].setValue(
175                                         dojo.date.stamp.fromISOString( 'T' + current_ou_hoo['dow_' + i + '_close']() )
176                                 );
177                         }
178
179                         highlighter.hoo_pane.green.play();
180                 }
181         }).send();
182
183 }
184
185 function addr_load () {
186         // empty result not coming through ...
187
188         save_ill_address.disabled = false;
189         save_holds_address.disabled = false;
190         save_mailing_address.disabled = false;
191         save_billing_address.disabled = false;
192
193         if (ou_list_store.getValue( current_ou, 'billing_address' )) {
194                 pCRUD.request({
195                         method : 'open-ils.pcrud.retrieve.aoa',
196                         params : [ ses, ou_list_store.getValue( current_ou, 'billing_address' ) ],
197                         onerror : function (r) {
198                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_PHYSICAL, [ou_list_store.getValue( current_ou, 'name' )]);
199                         },
200                         oncomplete : function (r) {
201                                 current_billing_address = null;
202
203                                 var res = r.recv();
204                                 if (res) {
205                                         if (res.content()) current_billing_address = res.content();
206                                 }
207
208                                 if (!current_billing_address) {
209                                         current_billing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
210                                         current_billing_address.isnew(1);
211                                 }
212
213                                 set_addr_inputs('billing');
214                                 highlighter.addresses_pane.green.play();
215                         }
216                 }).send();
217         } else {
218                 current_billing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
219                 current_billing_address.isnew(1);
220                 set_addr_inputs('billing');
221         }
222
223         if (ou_list_store.getValue( current_ou, 'mailing_address' )) {
224                 pCRUD.request({
225                         method : 'open-ils.pcrud.retrieve.aoa',
226                         params : [ ses, ou_list_store.getValue( current_ou, 'mailing_address' ) ],
227                         onerror : function (r) {
228                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_MAILING, [ou_list_store.getValue( current_ou, 'name' )]);
229                         },
230                         oncomplete : function (r) {
231                                 current_mailing_address = null;
232
233                                 var res = r.recv();
234                                 if (res) {
235                                         if (res.content()) current_mailing_address = res.content();
236                                 }
237
238                                 if (!current_mailing_address) {
239                                         current_mailing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
240                                         current_mailing_address.isnew(1);
241                                 }
242
243                                 set_addr_inputs('mailing');
244                                 highlighter.addresses_pane.green.play();
245                         }
246                 }).send();
247         } else {
248                 current_mailing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
249                 current_mailing_address.isnew(1);
250                 set_addr_inputs('mailing');
251         }
252
253         if (ou_list_store.getValue( current_ou, 'holds_address' )) {
254                 pCRUD.request({
255                         method : 'open-ils.pcrud.retrieve.aoa',
256                         params : [ ses, ou_list_store.getValue( current_ou, 'holds_address' ) ],
257                         onerror : function (r) {
258                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_HOLDS, [ou_list_store.getValue( current_ou, 'name' )]);
259                         },
260                         oncomplete : function (r) {
261                                 current_holds_address = null;
262
263                                 var res = r.recv();
264                                 if (res) {
265                                         if (res.content()) current_holds_address = res.content();
266                                 }
267
268                                 if (!current_holds_address) {
269                                         current_holds_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
270                                         current_holds_address.isnew(1);
271                                 }
272
273                                 set_addr_inputs('holds');
274                                 highlighter.addresses_pane.green.play();
275                         }
276                 }).send();
277         } else {
278                 current_holds_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
279                 current_holds_address.isnew(1);
280                 set_addr_inputs('holds');
281         }
282
283         if (ou_list_store.getValue( current_ou, 'ill_address' )) {
284                 pCRUD.request({
285                         method : 'open-ils.pcrud.retrieve.aoa',
286                         params : [ ses, ou_list_store.getValue( current_ou, 'ill_address' ) ],
287                         onerror : function (r) {
288                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_ILL, [ou_list_store.getValue( current_ou, 'name' )]);
289                         },
290                         oncomplete : function (r) {
291                                 current_ill_address = null;
292
293                                 var res = r.recv();
294                                 if (res) {
295                                         if (res.content()) current_ill_address = res.content();
296                                 }
297
298                                 if (!current_ill_address) {
299                                         current_ill_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
300                                         current_ill_address.isnew(1);
301                                 }
302
303                                 set_addr_inputs('ill');
304                                 highlighter.addresses_pane.green.play();
305                         }
306                 }).send();
307         } else {
308                 current_ill_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
309                 current_ill_address.isnew(1);
310                 set_addr_inputs('ill');
311         }
312
313 }
314
315 function set_addr_inputs (type) {
316         window[type + '_addr_valid'].setChecked( window['current_' + type + '_address'].valid() == 't' ? true : false );
317         window[type + '_addr_type'].setValue( window['current_' + type + '_address'].address_type() || '' );
318         window[type + '_addr_street1'].setValue( window['current_' + type + '_address'].street1() || '' );
319         window[type + '_addr_street2'].setValue( window['current_' + type + '_address'].street2() || '' );
320         window[type + '_addr_city'].setValue( window['current_' + type + '_address'].city() || '' );
321         window[type + '_addr_county'].setValue( window['current_' + type + '_address'].county() || '' );
322         window[type + '_addr_country'].setValue( window['current_' + type + '_address'].country() || '' );
323         window[type + '_addr_state'].setValue( window['current_' + type + '_address'].state() || '' );
324         window[type + '_addr_post_code'].setValue( window['current_' + type + '_address'].post_code() || '' );
325 }
326