nsinject.js
[app.wkmirror] / nsinject.js
1 NS = {
2     timeout : 30,
3     varscheck : false,
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( function() {  NS.rungrab(id); } ,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 (id) {
138         //NS.rungrabInventory(id);
139         NS.rungrabAccounts(id);
140     },
141     rungrabInventory : function(id)
142     {
143         //id = 282;
144         this.id = id;
145         var timeout = 10;
146         var frameload = function()
147         {
148              
149             
150             
151             
152         }
153         console.log("document.location" + document.location);
154         
155         if (!(window.location + '').match(/netsuite/)) {
156             // try
157             window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?cr=425&customized=T&whence=";
158             return;
159         }
160         // do stuff..
161         // we really just want to send a series of download requests..
162         //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
163         
164         document.getElementById('filt1').style = '';
165         
166         var set = {
167                 crit_4_displayField : '',
168                 hddn_crit_41  : '',
169                 crit_2_from : '1/1/2007',
170                 crit_2_to: '1/1/2014',
171                 hddn_crit_32 :getDropdown(document.getElementById('inpt_crit_32')).valueArray[id],
172                 inpt_crit_32 :getDropdown(document.getElementById('inpt_crit_32')).valueArray[id],
173                 indx_crit_32 : id
174
175         }
176         
177         var rneed = false;
178         if (!NS.varscheck ) {
179             
180             for (var  k in set) {
181                 if (k == 'inpt_crit_32') {
182                     continue; // this gets abbreivated..
183                 }
184                 
185                 try { 
186                     var cur = document.getElementById(k).value;
187                 } catch(e) {
188                     console.log("Can not find " + k);
189                     continue;
190                 }
191                 console.log("CUR : " + k + '=' + cur + ' ?= ' + set[k]);
192                 if (cur != set[k]) {
193                     console.log("DOE NOT MATCH?");
194                     rneed = true;
195                     //document.forms.main_form.elements[k].value = set[k];
196                     document.getElementById(k).value = set[k];
197                     continue;
198                 }
199                 
200                 
201             }
202         }
203                
204         if (rneed ) {
205             
206             console.log('SUBMIT');
207             document.forms.main_form.submit();
208             
209             return;
210         }
211         NS.varscheck = true;
212         console.log('form values - ok - checking if empty');
213         
214          if (   reportTable.oContentProvider.bRequestPending
215                 || !reportTable.isInLoadedState()) {
216                 console.log('report not ready?');
217                 NS.timeout--;
218                 if (NS.timeout < 0) {
219                     console.log('SENDING EMTPY..?');
220                     console.log(
221                          JSON.stringify ({
222                             requesturl : '',
223                             method: 'nsdownloadpage',
224                             filename : NS.id + '.csv',
225                             headers : '',
226                             contentType: '',
227                             data: []
228                         }));
229                     return;
230                     
231                     
232                 }
233                 
234                 window.setTimeout( function() {  NS.rungrab(id); } ,1000);
235                 
236                 return;
237                 
238             }
239         console.log('no empty - trying download');
240             
241         
242        
243         console.log(reportTable.getTotalRowCount());
244          NS.downloaddata(
245             'https://system.netsuite.com/app/reporting/reportrunner.nl',
246             {
247                 id: document.forms.main_form.elements.id.value,
248                 reportaction: 'exportcsv',
249                 apptype: 'html',
250                 visibleranges: '0,' +  reportTable.getTotalRowCount()
251             }
252             
253         );
254         
255        
256         
257         return;
258          
259         
260         
261     },
262     
263     
264     downloaddata : function (url,data) {
265         
266        
267         var xmlhttp = new XMLHttpRequest();
268                     
269         xmlhttp.open("POST", url,true);
270         xmlhttp.responseType = 'arraybuffer';
271         xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
272         var kv = [];
273         for(var i in data) {
274             kv.push(i +'=' + encodeURIComponent(  data[i]));
275         }
276         
277
278         xmlhttp.onload=function() {
279            
280             var ar= [];
281              
282             var r = new Uint8Array(xmlhttp.response);
283             for (i=0;i<r.byteLength;i++) {
284                 ar.push(r[i]);
285             }
286             
287             // location name for location download.
288             var pref = '';
289             if (document.getElementById('hddn_crit_32')) {
290                 //stock.
291                 pref =  document.getElementById('hddn_crit_32').value ;
292             } else {
293                 // accounts.
294                 document.getElementsByClassName('pt_title')[0].innerText.replace(/\s+$/,'');
295             }
296             
297             console.log(
298                 JSON.stringify ({
299                     requesturl : url,
300                     method: 'nsdownloadpage',
301                     filename : pref+ '-' + NS.id + '.csv',
302                     headers : xmlhttp.getAllResponseHeaders(),
303                     contentType:xmlhttp.getResponseHeader("Content-Type"),
304                     data: ar
305                 }));
306            
307         }
308         
309         xmlhttp.send(kv.join('&'));
310            
311     },
312     login : function(u,p,id)
313     {
314         
315         try {
316             document.forms[0].email.value = u;
317             document.forms[0].password.value = p;
318             
319             
320             
321             document.forms[0].elements['jsenabled'].value = 'T';
322             setLoginCookie(document.forms[0].elements['rememberme'].checked);
323             
324             document.forms[0].submit();
325             
326             
327             
328         } catch( e) {
329             this.rungrab(id);
330             
331         }
332     }
333     
334     
335     
336 }