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         if (!(window.location + '').match(/netsuite/)) {
119             // try
120             console.log("REDIRECT");
121             window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=" + id;
122             return;
123         }
124         
125         
126         // we really just want to send a series of download requests..
127         //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
128         
129          
130         NS.downloadgetdata(
131             'https://system.netsuite.com/app/common/search/searchresults.csv' +
132             '?searchtype=Transaction' + 
133             '&Transaction_ACCOUNT=' + id +
134             '&sortcol=Transaction_TRANDATE_raw' +
135             '&sortdir=ASC'+
136             '&csv=Export'+
137             '&OfficeXML=F'+
138             '&pdf='+
139             '&style=NORMAL'+
140             '&report='+
141             '&grid='+
142             '&searchid=118'+
143             '&dle=F',
144             {
145                 id: data.id,
146                 //reportaction: 'exportcsv',
147                 //apptype: 'html',
148                 //visibleranges: '0,' +  reportTable.getTotalRowCount()
149             }
150             
151         );
152         
153         
154         
155         return;
156         
157         
158          
159         
160     },
161     
162     rungrab : function (id) {
163         //NS.rungrabInventory(id);
164         NS.rungrabAccounts(id);
165     },
166     rungrabInventory : function(id)
167     {
168         //id = 282;
169         this.id = id;
170         var timeout = 10;
171         var frameload = function()
172         {
173              
174             
175             
176             
177         }
178         console.log("document.location" + document.location);
179         
180         if (!(window.location + '').match(/netsuite/)) {
181             // try
182             window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?cr=425&customized=T&whence=";
183             return;
184         }
185         // do stuff..
186         // we really just want to send a series of download requests..
187         //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
188         
189         document.getElementById('filt1').style = '';
190         
191         var set = {
192                 crit_4_displayField : '',
193                 hddn_crit_41  : '',
194                 crit_2_from : '1/1/2007',
195                 crit_2_to: '1/1/2014',
196                 hddn_crit_32 :getDropdown(document.getElementById('inpt_crit_32')).valueArray[id],
197                 inpt_crit_32 :getDropdown(document.getElementById('inpt_crit_32')).valueArray[id],
198                 indx_crit_32 : id
199
200         }
201         
202         var rneed = false;
203         if (!NS.varscheck ) {
204             
205             for (var  k in set) {
206                 if (k == 'inpt_crit_32') {
207                     continue; // this gets abbreivated..
208                 }
209                 
210                 try { 
211                     var cur = document.getElementById(k).value;
212                 } catch(e) {
213                     console.log("Can not find " + k);
214                     continue;
215                 }
216                 console.log("CUR : " + k + '=' + cur + ' ?= ' + set[k]);
217                 if (cur != set[k]) {
218                     console.log("DOE NOT MATCH?");
219                     rneed = true;
220                     //document.forms.main_form.elements[k].value = set[k];
221                     document.getElementById(k).value = set[k];
222                     continue;
223                 }
224                 
225                 
226             }
227         }
228                
229         if (rneed ) {
230             
231             console.log('SUBMIT');
232             document.forms.main_form.submit();
233             
234             return;
235         }
236         NS.varscheck = true;
237         console.log('form values - ok - checking if empty');
238         
239          if (   reportTable.oContentProvider.bRequestPending
240                 || !reportTable.isInLoadedState()) {
241                 console.log('report not ready?');
242                 NS.timeout--;
243                 if (NS.timeout < 0) {
244                     console.log('SENDING EMTPY..?');
245                     console.log(
246                          JSON.stringify ({
247                             requesturl : '',
248                             method: 'nsdownloadpage',
249                             filename : NS.id + '.csv',
250                             headers : '',
251                             contentType: '',
252                             data: []
253                         }));
254                     return;
255                     
256                     
257                 }
258                 
259                 window.setTimeout( function() {  NS.rungrab(id); } ,1000);
260                 
261                 return;
262                 
263             }
264         console.log('no empty - trying download');
265             
266         
267        
268         console.log(reportTable.getTotalRowCount());
269          NS.downloaddata(
270             'https://system.netsuite.com/app/reporting/reportrunner.nl',
271             {
272                 id: document.forms.main_form.elements.id.value,
273                 reportaction: 'exportcsv',
274                 apptype: 'html',
275                 visibleranges: '0,' +  reportTable.getTotalRowCount()
276             }
277             
278         );
279         
280        
281         
282         return;
283          
284         
285         
286     },
287     
288     
289     downloadgetdata : function (url,data) {
290         
291        
292         var xmlhttp = new XMLHttpRequest();
293                     
294         xmlhttp.open("GET", url,true);
295         xmlhttp.responseType = 'arraybuffer';
296         //xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
297         //var kv = [];
298         //for(var i in data) {
299         //    kv.push(i +'=' + encodeURIComponent(  data[i]));
300         //}
301         
302
303         xmlhttp.onload=function() {
304            
305             var ar= [];
306              
307             var r = new Uint8Array(xmlhttp.response);
308             for (i=0;i<r.byteLength;i++) {
309                 ar.push(r[i]);
310             }
311             
312             // location name for location download.
313             var pref = '';
314             if (document.getElementById('hddn_crit_32')) {
315                 //stock.
316                 pref =  document.getElementById('hddn_crit_32').value ;
317             } else {
318                 // accounts.
319                 pref = document.getElementsByClassName('pt_title')[0].innerText.replace(/\s+$/,'').replace(/\//g, '');
320             }
321             
322             console.log(
323                 JSON.stringify ({
324                     requesturl : url,
325                     method: 'nsdownloadpage',
326                     filename : pref+ '-' + NS.id + '.csv',
327                     headers : xmlhttp.getAllResponseHeaders(),
328                     contentType:xmlhttp.getResponseHeader("Content-Type"),
329                     data: ar
330                 }));
331            
332         }
333         
334         xmlhttp.send();
335            
336     },
337     
338     
339     
340     downloaddata : function (url,data) {
341         
342        
343         var xmlhttp = new XMLHttpRequest();
344                     
345         xmlhttp.open("POST", url,true);
346         xmlhttp.responseType = 'arraybuffer';
347         xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
348         var kv = [];
349         for(var i in data) {
350             kv.push(i +'=' + encodeURIComponent(  data[i]));
351         }
352         
353
354         xmlhttp.onload=function() {
355            
356             var ar= [];
357              
358             var r = new Uint8Array(xmlhttp.response);
359             for (i=0;i<r.byteLength;i++) {
360                 ar.push(r[i]);
361             }
362             
363             // location name for location download.
364             var pref = '';
365             if (document.getElementById('hddn_crit_32')) {
366                 //stock.
367                 pref =  document.getElementById('hddn_crit_32').value ;
368             } else {
369                 // accounts.
370                 pref = document.getElementsByClassName('pt_title')[0].innerText.replace(/\s+$/,'').replace(/\//g, '');
371             }
372             
373             console.log(
374                 JSON.stringify ({
375                     requesturl : url,
376                     method: 'nsdownloadpage',
377                     filename : pref+ '-' + NS.id + '.csv',
378                     headers : xmlhttp.getAllResponseHeaders(),
379                     contentType:xmlhttp.getResponseHeader("Content-Type"),
380                     data: ar
381                 }));
382            
383         }
384         
385         xmlhttp.send(kv.join('&'));
386            
387     },
388     login : function(u,p,id)
389     {
390         
391         try {
392             document.forms[0].email.value = u;
393             document.forms[0].password.value = p;
394             
395             
396             
397             document.forms[0].elements['jsenabled'].value = 'T';
398             setLoginCookie(document.forms[0].elements['rememberme'].checked);
399             
400             document.forms[0].submit();
401             
402             
403             
404         } catch( e) {
405             this.rungrab(id);
406             
407         }
408     }
409     
410     
411     
412 }