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