nsinject.js
[app.wkmirror] / nsinject.js
1 NS = {
2     timeout : 20,
3     
4     rungrabAccounts : function(id)
5     {
6         //id = 282;
7         this.id = id;
8         var timeout = 10;
9         var frameload = function()
10         {
11              
12             
13             
14             
15         }
16         console.log("document.location" + document.location);
17         
18         if (!(window.location + '').match(/netsuite/)) {
19             // try
20             window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=" + id;
21             return;
22         }
23         // do stuff..
24         // we really just want to send a series of download requests..
25         //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
26         
27         
28         
29         var chk = document.getElementById('lines');
30         console.log("lines element?");
31         console.log(chk)
32         console.log('checking form values');
33         
34         
35         if (chk.value !='T') {
36             console.log('SETTING VALUES:chk');
37             chk.value= 'T';
38             console.log('SETTING VALUES:daterange');
39             document.forms[0].daterange.value = 'CUSTOM';
40             console.log('SETTING VALUES:date1');
41             document.getElementById('date1').value = '1/1/2008';
42             console.log('SETTING VALUES:date2');
43             document.getElementById('date2').value = '1/1/2013';
44             console.log('SUBMIT');
45             document.forms[0].submit();
46             
47             return;
48         }
49         
50         console.log('form values - ok - checking if empty');
51         
52          if (   reportTable.oContentProvider.bRequestPending
53                 || !reportTable.isInLoadedState()) {
54                 console.log('report not ready?');
55                 NS.timeout--;
56                 if (NS.timeout < 0) {
57                     console.log('SENDING EMTPY..?');
58                     console.log(
59                          JSON.stringify ({
60                             requesturl : '',
61                             method: 'nsdownloadpage',
62                             filename : NS.id + '.csv',
63                             headers : '',
64                             contentType: '',
65                             data: []
66                         }));
67                     return;
68                     
69                     
70                 }
71                 
72                 window.setTimeout(NS.rungrab,1000);
73                 
74                 return;
75                 
76             }
77         console.log('no empty - trying download');
78             
79         var ar = document.forms[0].elements;
80         var data= {};
81         for(var i =0; i < ar.length; i++) {
82             var e = ar[i];
83             data[e.name] = e.value;
84         }
85         console.log(JSON.stringify(data));
86         console.log(reportTable.getTotalRowCount());
87          NS.downloaddata(
88             'https://system.netsuite.com/app/reporting/reportrunner.nl',
89             {
90                 id: data.id,
91                 reportaction: 'exportcsv',
92                 apptype: 'html',
93                 visibleranges: '0,' +  reportTable.getTotalRowCount()
94             }
95             
96         );
97         
98         
99         
100         return;
101         
102         
103         
104         var ifr = document.createElement('iframe');
105         
106         ifr.setAttribute('src','https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=' + id);
107         ifr.setAttribute('width','900');
108         ifr.setAttribute('height','700');
109         ifr.addEventListener('load', frameload);
110         document.body.innerHTML = '';
111         
112         document.body.appendChild(ifr);
113         
114         /*
115         var ifd = ifr.contentWindow.document;
116         ifd.getElementById('lines_chkbox').
117         
118         var ar = ifd.getElementById('footerform').elements;
119         for(var i =0; i < ar.length; i++) {
120             var e = ar[i];
121             data[e.name] = e.value;
122         }
123         console.log(data);
124         
125         data.lines_chkbox = 'T';
126         data.reportaction = 'exportcsv'
127         //data.visibleranges =
128         data.apptype = 'html';
129         this.downloaddata(
130             'https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403',
131             data
132         )
133         */
134         
135         
136     },
137     rungrab : function () {
138         NS.rungrabInventory(id);
139     },
140     rungrabInventory : function(id)
141     {
142         //id = 282;
143         this.id = id;
144         var timeout = 10;
145         var frameload = function()
146         {
147              
148             
149             
150             
151         }
152         console.log("document.location" + document.location);
153         
154         if (!(window.location + '').match(/netsuite/)) {
155             // try
156             window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?cr=425&customized=T&whence=";
157             return;
158         }
159         // do stuff..
160         // we really just want to send a series of download requests..
161         //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
162         
163         
164         
165         var chk = document.getElementById('lines');
166         console.log("lines element?");
167         console.log(chk)
168         console.log('checking form values');
169         
170         
171         if (chk.value !='T') {
172             console.log('SETTING VALUES:chk');
173             chk.value= 'T';
174             console.log('SETTING VALUES:daterange');
175             document.forms[0].daterange.value = 'CUSTOM';
176             console.log('SETTING VALUES:date1');
177             document.getElementById('date1').value = '1/1/2008';
178             console.log('SETTING VALUES:date2');
179             document.getElementById('date2').value = '1/1/2013';
180             console.log('SUBMIT');
181             document.forms[0].submit();
182             
183             return;
184         }
185         
186         console.log('form values - ok - checking if empty');
187         
188          if (   reportTable.oContentProvider.bRequestPending
189                 || !reportTable.isInLoadedState()) {
190                 console.log('report not ready?');
191                 NS.timeout--;
192                 if (NS.timeout < 0) {
193                     console.log('SENDING EMTPY..?');
194                     console.log(
195                          JSON.stringify ({
196                             requesturl : '',
197                             method: 'nsdownloadpage',
198                             filename : NS.id + '.csv',
199                             headers : '',
200                             contentType: '',
201                             data: []
202                         }));
203                     return;
204                     
205                     
206                 }
207                 
208                 window.setTimeout(NS.rungrab,1000);
209                 
210                 return;
211                 
212             }
213         console.log('no empty - trying download');
214             
215         var ar = document.forms[0].elements;
216         var data= {};
217         for(var i =0; i < ar.length; i++) {
218             var e = ar[i];
219             data[e.name] = e.value;
220         }
221         console.log(JSON.stringify(data));
222         console.log(reportTable.getTotalRowCount());
223          NS.downloaddata(
224             'https://system.netsuite.com/app/reporting/reportrunner.nl',
225             {
226                 id: data.id,
227                 reportaction: 'exportcsv',
228                 apptype: 'html',
229                 visibleranges: '0,' +  reportTable.getTotalRowCount()
230             }
231             
232         );
233         
234         
235         
236         return;
237         
238         
239         
240         var ifr = document.createElement('iframe');
241         
242         ifr.setAttribute('src','https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=' + id);
243         ifr.setAttribute('width','900');
244         ifr.setAttribute('height','700');
245         ifr.addEventListener('load', frameload);
246         document.body.innerHTML = '';
247         
248         document.body.appendChild(ifr);
249         
250         /*
251         var ifd = ifr.contentWindow.document;
252         ifd.getElementById('lines_chkbox').
253         
254         var ar = ifd.getElementById('footerform').elements;
255         for(var i =0; i < ar.length; i++) {
256             var e = ar[i];
257             data[e.name] = e.value;
258         }
259         console.log(data);
260         
261         data.lines_chkbox = 'T';
262         data.reportaction = 'exportcsv'
263         //data.visibleranges =
264         data.apptype = 'html';
265         this.downloaddata(
266             'https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403',
267             data
268         )
269         */
270         
271         
272     },
273     
274     
275     downloaddata : function (url,data) {
276         
277        
278         var xmlhttp = new XMLHttpRequest();
279                     
280         xmlhttp.open("POST", url,true);
281         xmlhttp.responseType = 'arraybuffer';
282         xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
283         var kv = [];
284         for(var i in data) {
285             kv.push(i +'=' + encodeURIComponent(  data[i]));
286         }
287         
288
289         xmlhttp.onload=function() {
290            
291             var ar= [];
292              
293             var r = new Uint8Array(xmlhttp.response);
294             for (i=0;i<r.byteLength;i++) {
295                 ar.push(r[i]);
296             }
297             
298             console.log(
299                 JSON.stringify ({
300                     requesturl : url,
301                     method: 'nsdownloadpage',
302                     filename : NS.id + '.csv',
303                     headers : xmlhttp.getAllResponseHeaders(),
304                     contentType:xmlhttp.getResponseHeader("Content-Type"),
305                     data: ar
306                 }));
307            
308         }
309         
310         xmlhttp.send(kv.join('&'));
311            
312     },
313     login : function(u,p,id)
314     {
315         
316         try {
317             document.forms[0].email.value = u;
318             document.forms[0].password.value = p;
319             
320             
321             
322             document.forms[0].elements['jsenabled'].value = 'T';
323             setLoginCookie(document.forms[0].elements['rememberme'].checked);
324             
325             document.forms[0].submit();
326             
327             
328             
329         } catch( e) {
330             this.rungrab(id);
331             
332         }
333     }
334     
335     
336     
337 }