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