]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/conify/global/actor/org_unit.js
moving toward the openils.PermaCrud module
[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('openils.PermaCrud');
21 dojo.require('dojo.parser');
22 dojo.require('dojo.data.ItemFileWriteStore');
23 dojo.require('dojo.date.stamp');
24 dojo.require('dijit.form.TextBox');
25 dojo.require('dijit.form.TimeTextBox');
26 dojo.require('dijit.form.ValidationTextBox');
27 dojo.require('dijit.form.CheckBox');
28 dojo.require('dijit.form.FilteringSelect');
29 dojo.require('dijit.Tree');
30 dojo.require('dijit.layout.ContentPane');
31 dojo.require('dijit.layout.TabContainer');
32 dojo.require('dijit.layout.LayoutContainer');
33 dojo.require('dijit.layout.SplitContainer');
34 dojo.require('dojox.widget.Toaster');
35 dojo.require('dojox.fx');
36 dojo.requireLocalization("openils.conify", "conify");
37
38 // some handy globals
39 var cgi = new CGI();
40 var cookieManager = new HTTP.Cookies();
41 var ses = cookieManager.read('ses') || cgi.param('ses');
42 var pCRUD = new OpenSRF.ClientSession('open-ils.pcrud');
43 var pcrud = new openils.PermaCrud({ authtoken : ses });
44
45 var current_ou, current_ou_hoo, ou_list_store
46 var dirtyStore = [];
47 var virgin_ou_id = -1;
48
49 var aou_strings = dojo.i18n.getLocalization('openils.conify', 'conify');
50
51 //var ou_type_store = new dojo.data.ItemFileWriteStore({ data : aout.toStoreData( globalOrgTypes ) });
52
53 var highlighter = {};
54
55 function status_update (markup) {
56         if (parent !== window && parent.status_update) parent.status_update( markup );
57 }
58
59 function save_org () {
60
61         new_kid_button.disabled = false;
62         save_ou_button.disabled = false;
63         delete_ou_button.disabled = false;
64
65         var modified_ou = new aou().fromStoreItem( current_ou );
66         modified_ou.ischanged( 1 );
67
68     pcrud.apply( modified_ou, {
69         timeout : 10, // makes it synchronous
70                 onerror : function (r) {
71                         highlighter.editor_pane.red.play();
72                         status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
73                 },
74                 oncomplete : function (r) {
75                         var res = r.recv();
76                         if ( res && res.content() ) {
77                                 ou_list_store.setValue( current_ou, 'ischanged', 0 );
78                                 highlighter.editor_pane.green.play();
79                                 status_update( dojo.string.substitute( aou_strings.SUCCESS_SAVE, [ou_list_store.getValue( current_ou, 'name' )] ) );
80                         } else {
81                                 highlighter.editor_pane.red.play();
82                                 status_update( dojo.string.substitute( aou_strings.ERROR_SAVING_DATA, [ou_list_store.getValue( current_ou, 'name' )] ) );
83                         }
84                 },
85     });
86
87 }
88         
89 function hoo_load () {
90         // empty result not coming through ...
91         current_ou_hoo = new aouhoo().fromHash({id:ou_list_store.getValue( current_ou, 'id' )});
92         current_ou_hoo.isnew(1);
93
94         pCRUD.request({
95                 method : 'open-ils.pcrud.retrieve.aouhoo',
96                 params : [ ses, ou_list_store.getValue( current_ou, 'id' ) ],
97                 onerror : function (r) { 
98                         throw dojo.string.substitute(aou_strings.ERROR_FETCHING_HOURS, [ou_list_store.getValue( current_ou, 'name' )]);
99                 },
100                 oncomplete : function (r) {
101                         current_ou_hoo = null;
102
103                         var res = r.recv();
104                         if (res) {
105                                 if (res.content()) current_ou_hoo = res.content();
106                         }
107
108                         if (!current_ou_hoo) {
109                                 current_ou_hoo = new aouhoo().fromHash({id:ou_list_store.getValue( current_ou, 'id' )});
110                                 current_ou_hoo.isnew(1);
111                                 for (var i = 0; i < 7; i++) {
112                                         current_ou_hoo['dow_' + i + '_open']('09:00:00');
113                                         current_ou_hoo['dow_' + i + '_close']('17:00:00');
114                                 }
115                         }
116
117                         for (var i = 0; i < 7; i++) {
118                                 window['dow_' + i + '_open'].setValue(
119                                         dojo.date.stamp.fromISOString( 'T' + current_ou_hoo['dow_' + i + '_open']() )
120                                 );
121                                 window['dow_' + i + '_close'].setValue(
122                                         dojo.date.stamp.fromISOString( 'T' + current_ou_hoo['dow_' + i + '_close']() )
123                                 );
124                         }
125
126                         highlighter.hoo_pane.green.play();
127                 }
128         }).send();
129
130 }
131
132 function addr_load () {
133         // empty result not coming through ...
134
135         save_ill_address.disabled = false;
136         save_holds_address.disabled = false;
137         save_mailing_address.disabled = false;
138         save_billing_address.disabled = false;
139
140         if (ou_list_store.getValue( current_ou, 'billing_address' )) {
141                 pCRUD.request({
142                         method : 'open-ils.pcrud.retrieve.aoa',
143                         params : [ ses, ou_list_store.getValue( current_ou, 'billing_address' ) ],
144                         onerror : function (r) {
145                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_PHYSICAL, [ou_list_store.getValue( current_ou, 'name' )]);
146                         },
147                         oncomplete : function (r) {
148                                 current_billing_address = null;
149
150                                 var res = r.recv();
151                                 if (res) {
152                                         if (res.content()) current_billing_address = res.content();
153                                 }
154
155                                 if (!current_billing_address) {
156                                         current_billing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
157                                         current_billing_address.isnew(1);
158                                 }
159
160                                 set_addr_inputs('billing');
161                                 highlighter.addresses_pane.green.play();
162                         }
163                 }).send();
164         } else {
165                 current_billing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
166                 current_billing_address.isnew(1);
167                 set_addr_inputs('billing');
168         }
169
170         if (ou_list_store.getValue( current_ou, 'mailing_address' )) {
171                 pCRUD.request({
172                         method : 'open-ils.pcrud.retrieve.aoa',
173                         params : [ ses, ou_list_store.getValue( current_ou, 'mailing_address' ) ],
174                         onerror : function (r) {
175                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_MAILING, [ou_list_store.getValue( current_ou, 'name' )]);
176                         },
177                         oncomplete : function (r) {
178                                 current_mailing_address = null;
179
180                                 var res = r.recv();
181                                 if (res) {
182                                         if (res.content()) current_mailing_address = res.content();
183                                 }
184
185                                 if (!current_mailing_address) {
186                                         current_mailing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
187                                         current_mailing_address.isnew(1);
188                                 }
189
190                                 set_addr_inputs('mailing');
191                                 highlighter.addresses_pane.green.play();
192                         }
193                 }).send();
194         } else {
195                 current_mailing_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
196                 current_mailing_address.isnew(1);
197                 set_addr_inputs('mailing');
198         }
199
200         if (ou_list_store.getValue( current_ou, 'holds_address' )) {
201                 pCRUD.request({
202                         method : 'open-ils.pcrud.retrieve.aoa',
203                         params : [ ses, ou_list_store.getValue( current_ou, 'holds_address' ) ],
204                         onerror : function (r) {
205                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_HOLDS, [ou_list_store.getValue( current_ou, 'name' )]);
206                         },
207                         oncomplete : function (r) {
208                                 current_holds_address = null;
209
210                                 var res = r.recv();
211                                 if (res) {
212                                         if (res.content()) current_holds_address = res.content();
213                                 }
214
215                                 if (!current_holds_address) {
216                                         current_holds_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
217                                         current_holds_address.isnew(1);
218                                 }
219
220                                 set_addr_inputs('holds');
221                                 highlighter.addresses_pane.green.play();
222                         }
223                 }).send();
224         } else {
225                 current_holds_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
226                 current_holds_address.isnew(1);
227                 set_addr_inputs('holds');
228         }
229
230         if (ou_list_store.getValue( current_ou, 'ill_address' )) {
231                 pCRUD.request({
232                         method : 'open-ils.pcrud.retrieve.aoa',
233                         params : [ ses, ou_list_store.getValue( current_ou, 'ill_address' ) ],
234                         onerror : function (r) {
235                                 throw dojo.string.substitute(aou_strings.ERROR_FETCHING_ILL, [ou_list_store.getValue( current_ou, 'name' )]);
236                         },
237                         oncomplete : function (r) {
238                                 current_ill_address = null;
239
240                                 var res = r.recv();
241                                 if (res) {
242                                         if (res.content()) current_ill_address = res.content();
243                                 }
244
245                                 if (!current_ill_address) {
246                                         current_ill_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
247                                         current_ill_address.isnew(1);
248                                 }
249
250                                 set_addr_inputs('ill');
251                                 highlighter.addresses_pane.green.play();
252                         }
253                 }).send();
254         } else {
255                 current_ill_address = new aoa().fromHash({org_unit:ou_list_store.getValue( current_ou, 'id' )});
256                 current_ill_address.isnew(1);
257                 set_addr_inputs('ill');
258         }
259
260 }
261
262 function set_addr_inputs (type) {
263         window[type + '_addr_valid'].setChecked( window['current_' + type + '_address'].valid() == 't' ? true : false );
264         window[type + '_addr_type'].setValue( window['current_' + type + '_address'].address_type() || '' );
265         window[type + '_addr_street1'].setValue( window['current_' + type + '_address'].street1() || '' );
266         window[type + '_addr_street2'].setValue( window['current_' + type + '_address'].street2() || '' );
267         window[type + '_addr_city'].setValue( window['current_' + type + '_address'].city() || '' );
268         window[type + '_addr_county'].setValue( window['current_' + type + '_address'].county() || '' );
269         window[type + '_addr_country'].setValue( window['current_' + type + '_address'].country() || '' );
270         window[type + '_addr_state'].setValue( window['current_' + type + '_address'].state() || '' );
271         window[type + '_addr_post_code'].setValue( window['current_' + type + '_address'].post_code() || '' );
272 }
273