6abdc69226b6b8a7176051464db61ce4d0140d34
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / picklist / upload.js
1 dojo.require('dojo.data.ItemFileReadStore');
2 dojo.require('dojo.data.ItemFileWriteStore');
3 dojo.require('dijit.ProgressBar');
4 dojo.require('dijit.form.CheckBox');
5 dojo.require('dijit.form.TextBox');
6 dojo.require('dijit.form.FilteringSelect');
7 dojo.require('dijit.form.ComboBox');
8 dojo.require('dijit.form.Button');
9 dojo.require("dojo.io.iframe");
10 dojo.require('openils.User');
11 dojo.require('openils.widget.AutoFieldWidget');
12 dojo.require('openils.acq.Picklist');
13 dojo.require('openils.XUL');
14 dojo.require('openils.PermaCrud');
15
16 var VANDELAY_URL = '/vandelay-upload';
17 var providerWidget;
18 var orderAgencyWidget;
19 var vlAgent;
20 var usingNewPl = false;
21
22 function init() {
23     dojo.byId('acq-pl-upload-ses').value = openils.User.authtoken;
24     vlAgent = new VLAgent();
25     vlAgent.init(init2);
26 }
27
28 function init2() {
29
30     loadYearSelector();
31
32     new openils.widget.AutoFieldWidget({
33         fmClass : 'acqpo',
34         fmField : 'provider',
35         orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
36         parentNode : dojo.byId('acq-pl-upload-provider'),
37     }).build(
38         function(w) { 
39             providerWidget = w;
40             vlAgent.readCachedValue(w, 'provider', true);
41         }
42     );
43
44     new openils.widget.AutoFieldWidget({
45         fmClass : 'acqpo',
46         fmField : 'ordering_agency',
47         orgLimitPerms : ['CREATE_PICKLIST', 'CREATE_PURCHASE_ORDER'],
48         parentNode : dojo.byId('acq-pl-upload-agency'),
49     }).build(
50         function(w) { 
51             orderAgencyWidget = w 
52             vlAgent.readCachedValue(w, 'ordering_agency');
53             dojo.connect(orderAgencyWidget, 'onChange', setDefaultFiscalYear);
54         }
55     );
56
57     vlAgent.readCachedValue(acqPlUploadCreatePo, 'create_po');
58     vlAgent.readCachedValue(acqPlUploadActivatePo, 'activate_po');
59
60     fieldmapper.standardRequest(
61         ['open-ils.acq', 'open-ils.acq.picklist.user.retrieve.atomic'],
62         {   async: true,
63             params: [openils.User.authtoken], 
64             oncomplete : function(r) {
65                 var list = openils.Util.readResponse(r);
66                 acqPlUploadPlSelector.store = 
67                     new dojo.data.ItemFileWriteStore({data:acqpl.toStoreData(list)});
68             }
69         }
70     );
71 }
72
73 function setDefaultFiscalYear(org) {
74     org = org || orderAgencyWidget.attr('value');
75
76     // NOTE: Evergreen does not yet offer an interface for managing
77     // fiscal years.  For now, make the fiscal year selector persistant
78     vlAgent.readCachedValue(acqUploadYearSelector, 'fiscal_year');
79     return;
80
81     if (org) {
82
83         fieldmapper.standardRequest(
84             ['open-ils.acq', 'open-ils.acq.org_unit.current_fiscal_year'],
85             {   params : [openils.User.authtoken, org],
86                 async : true,
87                 oncomplete : function(r) {
88                     var year = openils.Util.readResponse(r);
89                     acqUploadYearSelector.attr('value', year);
90                 }
91             }
92         );
93     }
94 }
95
96 function acqUploadRecords() {
97
98     // persist widget values
99     vlAgent.writeCachedValue(acqPlUploadCreatePo, 'create_po');
100     vlAgent.writeCachedValue(acqPlUploadActivatePo, 'activate_po');
101     vlAgent.writeCachedValue(providerWidget, 'provider');
102     vlAgent.writeCachedValue(orderAgencyWidget, 'ordering_agency');
103     vlAgent.writeCachedValue(acqUploadYearSelector, 'fiscal_year');
104
105     openils.Util.show('acq-pl-upload-progress');
106     var picklist = acqPlUploadPlSelector.attr('value');
107     if(picklist) {
108         // ComboBox value is the display string.  find the actual picklist
109         // and create a new one if necessary
110         acqPlUploadPlSelector.store.fetch({
111             query : {name:picklist}, 
112             onComplete : function(items) {
113                 if(items.length == 0) {
114                     // create a new picklist for these items
115                     usingNewPl = true;
116                     openils.acq.Picklist.create(
117                         {name:picklist, org_unit: orderAgencyWidget.attr('value')},
118                         function(plId) { acqSendUploadForm({picklist:plId}) }
119                     );
120                 } else {
121                     usingNewPl = false;
122                     acqSendUploadForm({picklist:items[0].id[0]});
123                 }
124             }
125         });
126     } else {
127         acqSendUploadForm({picklist:null});
128     }
129 }
130
131 function acqSendUploadForm(args) {
132     dojo.io.iframe.send({
133         url: VANDELAY_URL,
134         method: "post",
135         handleAs: "html",
136         form: dojo.byId('acq-pl-upload-form'),
137         handle: function(data, ioArgs){
138             acqHandlePostUpload(data.documentElement.textContent, args.picklist);
139         }
140     });
141 }
142
143
144 function acqHandlePostUpload(key, plId) {
145
146     var args = {
147         picklist : plId,
148         provider : providerWidget.attr('value'),
149         ordering_agency : orderAgencyWidget.attr('value'),
150         create_po : acqPlUploadCreatePo.attr('value'),
151         activate_po : acqPlUploadActivatePo.attr('value'),
152         vandelay : vlAgent.values(),
153         fiscal_year : acqUploadYearSelector.attr('value')
154     };
155
156     fieldmapper.standardRequest(
157         ['open-ils.acq', 'open-ils.acq.process_upload_records'],
158         {   async: true,
159             params: [openils.User.authtoken, key, args],
160             onresponse : function(r) {
161
162                 vlAgent.handleResponse(
163                     openils.Util.readResponse(r),
164                     function(resp, res) {
165
166                         openils.Util.hide('acq-pl-upload-complete-pl');
167                         openils.Util.hide('acq-pl-upload-complete-po');
168                         openils.Util.hide('acq-pl-upload-complete-q');
169                         openils.Util.hide('acq-pl-upload-progress-bar');
170                         openils.Util.show('acq-pl-upload-complete');
171
172                         function activateLink(link, url, name) {
173                             link = dojo.byId(link);
174                             openils.Util.show(link);
175                             if (name) link.innerHTML = name;
176                             if (typeof xulG == 'undefined') { // browser
177                                 link.setAttribute('href', url); 
178                             } else {
179                                 link.setAttribute('href', 'javascript:;'); // for linky-ness
180                                 if (window.IAMBROWSER) {
181                                     link.onclick = function() { xulG.relay_url(url) };
182                                 } else {
183                                     link.onclick = function() { openils.XUL.newTabEasy(url, null, null, true) };
184                                 }
185                             }
186                         }
187                             
188                         if(res.picklist_url) {
189                             activateLink('acq-pl-upload-complete-pl', res.picklist_url);
190
191                             // if the user entered a new picklist, refetch the set to pick
192                             // up the ID and redraw the list with the new one selected
193                             if (usingNewPl) {
194                                 var newPl = new openils.PermaCrud().retrieve('acqpl', resp.picklist.id());
195                                 acqPlUploadPlSelector.store.newItem(newPl.toStoreItem());
196                                 acqPlUploadPlSelector.attr('value', newPl.name());
197                             }
198                         } 
199
200                         if(res.po_url) {
201                             activateLink('acq-pl-upload-complete-po', res.po_url);
202                         }
203
204                         if (res.queue_url) {
205                             activateLink('acq-pl-upload-complete-q', res.queue_url);
206                         }
207                     }
208                 );
209             },
210         }
211     );
212 }
213
214 function loadYearSelector() {
215
216     fieldmapper.standardRequest(
217         ['open-ils.acq', 'open-ils.acq.fund.org.years.retrieve'],
218         {   async : true,
219             params : [openils.User.authtoken, {}, {limit_perm : 'VIEW_FUND'}],
220             oncomplete : function(r) {
221
222                 var yearList = openils.Util.readResponse(r);
223                 if(!yearList) return;
224                 yearList = yearList.map(function(year){return {year:year+''};}); // dojo wants strings
225
226                 var yearStore = {identifier:'year', name:'year', items:yearList};
227                 yearStore.items = yearStore.items.sort().reverse();
228                 acqUploadYearSelector.store = new dojo.data.ItemFileReadStore({data:yearStore});
229
230                 // until an ordering agency is selected, default to the 
231                 // fiscal year of the workstation
232                 setDefaultFiscalYear(new openils.User().user.ws_ou());
233             }
234         }
235     );
236 }
237
238
239
240 openils.Util.addOnLoad(init);
241