nsinject.js
[app.wkmirror] / nsinject.js
1 NS = {
2     timeout : 30,
3     varscheck : false,
4     
5     /*
6     rungrabAccounts : function(id)
7     {
8         //id = 282;
9         this.id = id;
10         var timeout = 10;
11         var frameload = function()
12         {
13              
14             
15             
16             
17         }
18         console.log("document.location" + document.location);
19         
20         if (!(window.location + '').match(/netsuite/)) {
21             // try
22             window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=" + id;
23             return;
24         }
25         // do stuff..
26         // we really just want to send a series of download requests..
27         //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
28         
29         
30         
31         var chk = document.getElementById('lines');
32         console.log("lines element?");
33         console.log(chk)
34         console.log('checking form values');
35         
36         
37         if (chk.value !='T') {
38             console.log('SETTING VALUES:chk');
39             chk.value= 'T';
40             console.log('SETTING VALUES:daterange');
41             document.forms[0].daterange.value = 'CUSTOM';
42             console.log('SETTING VALUES:date1');
43             document.getElementById('date1').value = '1/1/2008';
44             console.log('SETTING VALUES:date2');
45             document.getElementById('date2').value = '1/1/2013';
46             console.log('SUBMIT');
47             document.forms[0].submit();
48             
49             return;
50         }
51         
52         console.log('form values - ok - checking if empty');
53         
54          if (   reportTable.oContentProvider.bRequestPending
55                 || !reportTable.isInLoadedState()) {
56                 console.log('report not ready?');
57                 NS.timeout--;
58                 if (NS.timeout < 0) {
59                     console.log('SENDING EMTPY..?');
60                     console.log(
61                          JSON.stringify ({
62                             requesturl : '',
63                             method: 'nsdownloadpage',
64                             filename : NS.id + '.csv',
65                             headers : '',
66                             contentType: '',
67                             data: []
68                         }));
69                     return;
70                     
71                     
72                 }
73                 
74                  window.setTimeout( function() {  NS.rungrab(id); } ,1000);
75                 
76                 return;
77                 
78             }
79         console.log('no empty - trying download');
80             
81         var ar = document.forms[0].elements;
82         var data= {};
83         for(var i =0; i < ar.length; i++) {
84             var e = ar[i];
85             data[e.name] = e.value;
86         }
87         console.log(JSON.stringify(data));
88         console.log(reportTable.getTotalRowCount());
89          NS.downloaddata(
90             'https://system.netsuite.com/app/reporting/reportrunner.nl',
91             {
92                 id: data.id,
93                 reportaction: 'exportcsv',
94                 apptype: 'html',
95                 visibleranges: '0,' +  reportTable.getTotalRowCount()
96             }
97             
98         );
99         
100         
101         
102         return;
103         
104         
105          
106         
107     },
108     
109     */
110     rungrabAccounts  : function(id)
111     {
112         //id = 282;
113         this.id = id;
114         var timeout = 10;
115         
116         console.log("document.location" + document.location);
117         
118         
119         // we really just want to send a series of download requests..
120         //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
121         
122          
123         NS.downloaddata(
124             'https://system.netsuite.com/app/common/search/searchresults.csv' +
125             '?searchtype=Transaction' + 
126             '&Transaction_ACCOUNT=' + id +
127             '&sortcol=Transaction_TRANDATE_raw' +
128             '&sortdir=ASC'+
129             '&csv=Export'+
130             '&OfficeXML=F'+
131             '&pdf='+
132             '&style=NORMAL'+
133             '&report='+
134             '&grid='+
135             '&searchid=118'+
136             '&dle=F',
137             {
138                 id: data.id,
139                 //reportaction: 'exportcsv',
140                 //apptype: 'html',
141                 //visibleranges: '0,' +  reportTable.getTotalRowCount()
142             }
143             
144         );
145         
146         
147         
148         return;
149         
150         
151          
152         
153     },
154     
155     rungrab : function (id) {
156         //NS.rungrabInventory(id);
157         NS.rungrabAccounts(id);
158     },
159     rungrabInventory : function(id)
160     {
161         //id = 282;
162         this.id = id;
163         var timeout = 10;
164         var frameload = function()
165         {
166              
167             
168             
169             
170         }
171         console.log("document.location" + document.location);
172         
173         if (!(window.location + '').match(/netsuite/)) {
174             // try
175             window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?cr=425&customized=T&whence=";
176             return;
177         }
178         // do stuff..
179         // we really just want to send a series of download requests..
180         //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
181         
182         document.getElementById('filt1').style = '';
183         
184         var set = {
185                 crit_4_displayField : '',
186                 hddn_crit_41  : '',
187                 crit_2_from : '1/1/2007',
188                 crit_2_to: '1/1/2014',
189                 hddn_crit_32 :getDropdown(document.getElementById('inpt_crit_32')).valueArray[id],
190                 inpt_crit_32 :getDropdown(document.getElementById('inpt_crit_32')).valueArray[id],
191                 indx_crit_32 : id
192
193         }
194         
195         var rneed = false;
196         if (!NS.varscheck ) {
197             
198             for (var  k in set) {
199                 if (k == 'inpt_crit_32') {
200                     continue; // this gets abbreivated..
201                 }
202                 
203                 try { 
204                     var cur = document.getElementById(k).value;
205                 } catch(e) {
206                     console.log("Can not find " + k);
207                     continue;
208                 }
209                 console.log("CUR : " + k + '=' + cur + ' ?= ' + set[k]);
210                 if (cur != set[k]) {
211                     console.log("DOE NOT MATCH?");
212                     rneed = true;
213                     //document.forms.main_form.elements[k].value = set[k];
214                     document.getElementById(k).value = set[k];
215                     continue;
216                 }
217                 
218                 
219             }
220         }
221                
222         if (rneed ) {
223             
224             console.log('SUBMIT');
225             document.forms.main_form.submit();
226             
227             return;
228         }
229         NS.varscheck = true;
230         console.log('form values - ok - checking if empty');
231         
232          if (   reportTable.oContentProvider.bRequestPending
233                 || !reportTable.isInLoadedState()) {
234                 console.log('report not ready?');
235                 NS.timeout--;
236                 if (NS.timeout < 0) {
237                     console.log('SENDING EMTPY..?');
238                     console.log(
239                          JSON.stringify ({
240                             requesturl : '',
241                             method: 'nsdownloadpage',
242                             filename : NS.id + '.csv',
243                             headers : '',
244                             contentType: '',
245                             data: []
246                         }));
247                     return;
248                     
249                     
250                 }
251                 
252                 window.setTimeout( function() {  NS.rungrab(id); } ,1000);
253                 
254                 return;
255                 
256             }
257         console.log('no empty - trying download');
258             
259         
260        
261         console.log(reportTable.getTotalRowCount());
262          NS.downloaddata(
263             'https://system.netsuite.com/app/reporting/reportrunner.nl',
264             {
265                 id: document.forms.main_form.elements.id.value,
266                 reportaction: 'exportcsv',
267                 apptype: 'html',
268                 visibleranges: '0,' +  reportTable.getTotalRowCount()
269             }
270             
271         );
272         
273        
274         
275         return;
276          
277         
278         
279     },
280     
281     
282     downloadgetdata : function (url,data) {
283         
284        
285         var xmlhttp = new XMLHttpRequest();
286                     
287         xmlhttp.open("GET", url,true);
288         xmlhttp.responseType = 'arraybuffer';
289         //xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
290         //var kv = [];
291         //for(var i in data) {
292         //    kv.push(i +'=' + encodeURIComponent(  data[i]));
293         //}
294         
295
296         xmlhttp.onload=function() {
297            
298             var ar= [];
299              
300             var r = new Uint8Array(xmlhttp.response);
301             for (i=0;i<r.byteLength;i++) {
302                 ar.push(r[i]);
303             }
304             
305             // location name for location download.
306             var pref = '';
307             if (document.getElementById('hddn_crit_32')) {
308                 //stock.
309                 pref =  document.getElementById('hddn_crit_32').value ;
310             } else {
311                 // accounts.
312                 pref = document.getElementsByClassName('pt_title')[0].innerText.replace(/\s+$/,'').replace(/\//g, '');
313             }
314             
315             console.log(
316                 JSON.stringify ({
317                     requesturl : url,
318                     method: 'nsdownloadpage',
319                     filename : pref+ '-' + NS.id + '.csv',
320                     headers : xmlhttp.getAllResponseHeaders(),
321                     contentType:xmlhttp.getResponseHeader("Content-Type"),
322                     data: ar
323                 }));
324            
325         }
326         
327         xmlhttp.send(kv.join('&'));
328            
329     },
330     
331     
332     
333     downloaddata : function (url,data) {
334         
335        
336         var xmlhttp = new XMLHttpRequest();
337                     
338         xmlhttp.open("POST", url,true);
339         xmlhttp.responseType = 'arraybuffer';
340         xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
341         var kv = [];
342         for(var i in data) {
343             kv.push(i +'=' + encodeURIComponent(  data[i]));
344         }
345         
346
347         xmlhttp.onload=function() {
348            
349             var ar= [];
350              
351             var r = new Uint8Array(xmlhttp.response);
352             for (i=0;i<r.byteLength;i++) {
353                 ar.push(r[i]);
354             }
355             
356             // location name for location download.
357             var pref = '';
358             if (document.getElementById('hddn_crit_32')) {
359                 //stock.
360                 pref =  document.getElementById('hddn_crit_32').value ;
361             } else {
362                 // accounts.
363                 pref = document.getElementsByClassName('pt_title')[0].innerText.replace(/\s+$/,'').replace(/\//g, '');
364             }
365             
366             console.log(
367                 JSON.stringify ({
368                     requesturl : url,
369                     method: 'nsdownloadpage',
370                     filename : pref+ '-' + NS.id + '.csv',
371                     headers : xmlhttp.getAllResponseHeaders(),
372                     contentType:xmlhttp.getResponseHeader("Content-Type"),
373                     data: ar
374                 }));
375            
376         }
377         
378         xmlhttp.send(kv.join('&'));
379            
380     },
381     login : function(u,p,id)
382     {
383         
384         try {
385             document.forms[0].email.value = u;
386             document.forms[0].password.value = p;
387             
388             
389             
390             document.forms[0].elements['jsenabled'].value = 'T';
391             setLoginCookie(document.forms[0].elements['rememberme'].checked);
392             
393             document.forms[0].submit();
394             
395             
396             
397         } catch( e) {
398             this.rungrab(id);
399             
400         }
401     }
402     
403     
404     
405 }