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 set = {
166                 crit_4  : '',
167                 crit_2_from : '1/1/2007',
168                 crit_2_to: '1/1/2014',
169                 hddn_crit_32 :getDropdown(document.getElementById('inpt_crit_32')).valueArray[id]
170         }
171         var rneed = false;
172         for (var  k in set) {
173             var cur = document.forms.main_form.elements[k].value;
174             if (cur != set[k]) {
175                 rneed = true;
176                 document.forms.main_form.elements[k].value = set[k];
177                 continue;
178             }
179             
180             
181         }
182            
183         if (rneed ) {
184             
185             console.log('SUBMIT');
186             cument.forms.main_form.submit();
187             
188             return;
189         }
190         
191         console.log('form values - ok - checking if empty');
192         
193          if (   reportTable.oContentProvider.bRequestPending
194                 || !reportTable.isInLoadedState()) {
195                 console.log('report not ready?');
196                 NS.timeout--;
197                 if (NS.timeout < 0) {
198                     console.log('SENDING EMTPY..?');
199                     console.log(
200                          JSON.stringify ({
201                             requesturl : '',
202                             method: 'nsdownloadpage',
203                             filename : NS.id + '.csv',
204                             headers : '',
205                             contentType: '',
206                             data: []
207                         }));
208                     return;
209                     
210                     
211                 }
212                 
213                 window.setTimeout(NS.rungrab,1000);
214                 
215                 return;
216                 
217             }
218         console.log('no empty - trying download');
219             
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: document.forms.main_form.elements.id.value,
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 }