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