TabbedBrowser.js
[app.wkmirror] / nsinject.js
index a5d34f6..2e0a585 100644 (file)
 NS = {
+    timeout : 30,
+    varscheck : false,
     
-    rungrab : function(id)
+    /*
+    rungrabAccounts : function(id)
     {
         //id = 282;
         this.id = id;
-        var timeout = 20;
+        var timeout = 10;
         var frameload = function()
         {
-            var ifd = ifr.contentWindow.document;
-            var chk = ifd.getElementById('lines');
-            if (!ifd.getElementById('rptdataarea') || !ifr.contentWindow.reportTable
-                ) {
+             
+            
+            
+            
+        }
+        console.log("document.location" + document.location);
+        
+        if (!(window.location + '').match(/netsuite/)) {
+            // try
+            window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=" + id;
+            return;
+        }
+        // do stuff..
+        // we really just want to send a series of download requests..
+        //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
+        
+        
+        
+        var chk = document.getElementById('lines');
+        console.log("lines element?");
+        console.log(chk)
+        console.log('checking form values');
+        
+        
+        if (chk.value !='T') {
+            console.log('SETTING VALUES:chk');
+            chk.value= 'T';
+            console.log('SETTING VALUES:daterange');
+            document.forms[0].daterange.value = 'CUSTOM';
+            console.log('SETTING VALUES:date1');
+            document.getElementById('date1').value = '1/1/2008';
+            console.log('SETTING VALUES:date2');
+            document.getElementById('date2').value = '1/1/2013';
+            console.log('SUBMIT');
+            document.forms[0].submit();
+            
+            return;
+        }
+        
+        console.log('form values - ok - checking if empty');
+        
+         if (   reportTable.oContentProvider.bRequestPending
+                || !reportTable.isInLoadedState()) {
                 console.log('report not ready?');
-                window.setTimeout(frameload,1000);
+                NS.timeout--;
+                if (NS.timeout < 0) {
+                    console.log('SENDING EMTPY..?');
+                    console.log(
+                         JSON.stringify ({
+                            requesturl : '',
+                            method: 'nsdownloadpage',
+                            filename : NS.id + '.csv',
+                            headers : '',
+                            contentType: '',
+                            data: []
+                        }));
+                    return;
+                    
+                    
+                }
+                
+                 window.setTimeout( function() {  NS.rungrab(id); } ,1000);
+                
                 return;
                 
             }
+        console.log('no empty - trying download');
+            
+        var ar = document.forms[0].elements;
+        var data= {};
+        for(var i =0; i < ar.length; i++) {
+            var e = ar[i];
+            data[e.name] = e.value;
+        }
+        console.log(JSON.stringify(data));
+        console.log(reportTable.getTotalRowCount());
+         NS.downloaddata(
+            'https://system.netsuite.com/app/reporting/reportrunner.nl',
+            {
+                id: data.id,
+                reportaction: 'exportcsv',
+                apptype: 'html',
+                visibleranges: '0,' +  reportTable.getTotalRowCount()
+            }
+            
+        );
+        
+        
+        
+        return;
+        
+        
+         
+        
+    },
+    
+    */
+    rungrabAccounts  : function(id)
+    {
+        //id = 282;
+        this.id = id;
+        var timeout = 10;
+        
+        console.log("document.location= " + document.location);
+         
+        if (!(window.location + '').match(/netsuite/)) {
+            // try
+            console.log("REDIRECT");
+            window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=52"; // empty account register..
+            return;
+        }
+        
+        
+        // we really just want to send a series of download requests..
+        //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
+        
+         
+        NS.downloadgetdata(
+            'https://system.netsuite.com/app/common/search/searchresults.csv' +
+            '?searchtype=Transaction' + 
+            '&Transaction_ACCOUNT=' + id +
+            '&Transaction_DEPARTMENT=2' + // hk
+            '&sortcol=Transaction_TRANDATE_raw' +
+            '&sortdir=ASC'+
+            '&csv=Export'+
+            '&OfficeXML=F'+
+            '&pdf='+
+            '&style=NORMAL'+
+            '&report='+
+            '&grid='+
+            '&searchid=120'+
+            '&dle=F',
+            
+            {
+                id: id,
+                pref : 'HK-Account'
+                //reportaction: 'exportcsv',
+                //apptype: 'html',
+                //visibleranges: '0,' +  reportTable.getTotalRowCount()
+            }
+            
+        );
+        
+         
+        return;
+        
+        
+         
+        
+    },
+    
+    rungrab : function (id) {
+        //NS.rungrabInventory(id);
+        NS.rungrabAccounts(id);
+    },
+    rungrabInventory : function(id)
+    {
+        //id = 282;
+        this.id = id;
+        var timeout = 10;
+        var frameload = function()
+        {
+             
             
             
-            console.log('report ready?');
             
-            if (chk.value !='T') {
-                chk.value= 'T';
-                ifd.forms[0].daterange.value = 'CUSTOM';
+        }
+        console.log("document.location" + document.location);
+        
+        if (!(window.location + '').match(/netsuite/)) {
+            // try
+            window.location = "https://system.netsuite.com/app/reporting/reportrunner.nl?cr=425&customized=T&whence=";
+            return;
+        }
+        // do stuff..
+        // we really just want to send a series of download requests..
+        //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
+        
+        document.getElementById('filt1').style = '';
+        
+        var set = {
+                crit_4_displayField : '',
+                hddn_crit_41  : '',
+                crit_2_from : '1/1/2007',
+                crit_2_to: '1/1/2014',
+                hddn_crit_32 :getDropdown(document.getElementById('inpt_crit_32')).valueArray[id],
+                inpt_crit_32 :getDropdown(document.getElementById('inpt_crit_32')).valueArray[id],
+                indx_crit_32 : id
 
-                ifd.getElementById('date1').value = '1/1/2008';
-                ifd.getElementById('date2').value = '1/1/2013';
+        }
+        
+        var rneed = false;
+        if (!NS.varscheck ) {
+            
+            for (var  k in set) {
+                if (k == 'inpt_crit_32') {
+                    continue; // this gets abbreivated..
+                }
+                
+                try { 
+                    var cur = document.getElementById(k).value;
+                } catch(e) {
+                    console.log("Can not find " + k);
+                    continue;
+                }
+                console.log("CUR : " + k + '=' + cur + ' ?= ' + set[k]);
+                if (cur != set[k]) {
+                    console.log("DOE NOT MATCH?");
+                    rneed = true;
+                    //document.forms.main_form.elements[k].value = set[k];
+                    document.getElementById(k).value = set[k];
+                    continue;
+                }
                 
-                ifd.getElementById('footerform').submit();
                 
-                return;
             }
+        }
+               
+        if (rneed ) {
             
-            if (   ifr.contentWindow.reportTable.oContentProvider.bRequestPending
-                || !ifr.contentWindow.reportTable.isInLoadedState()) {
+            console.log('SUBMIT');
+            document.forms.main_form.submit();
+            
+            return;
+        }
+        NS.varscheck = true;
+        console.log('form values - ok - checking if empty');
+        
+         if (   reportTable.oContentProvider.bRequestPending
+                || !reportTable.isInLoadedState()) {
                 console.log('report not ready?');
-                window.setTimeout(frameload,1000);
+                NS.timeout--;
+                if (NS.timeout < 0) {
+                    console.log('SENDING EMTPY..?');
+                    console.log(
+                         JSON.stringify ({
+                            requesturl : '',
+                            method: 'nsdownloadpage',
+                            filename : NS.id + '.csv',
+                            headers : '',
+                            contentType: '',
+                            data: []
+                        }));
+                    return;
+                    
+                    
+                }
+                
+                window.setTimeout( function() {  NS.rungrab(id); } ,1000);
+                
                 return;
                 
             }
+        console.log('no empty - trying download');
             
+        
+       
+        console.log(reportTable.getTotalRowCount());
+         NS.downloaddata(
+            'https://system.netsuite.com/app/reporting/reportrunner.nl',
+            {
+                id: document.forms.main_form.elements.id.value,
+                reportaction: 'exportcsv',
+                apptype: 'html',
+                visibleranges: '0,' +  reportTable.getTotalRowCount()
+            }
             
+        );
+        
+       
+        
+        return;
+         
+        
+        
+    },
+    
+    
+    downloadgetdata : function (url,data) {
+        
+       
+        var xmlhttp = new XMLHttpRequest();
+                    
+        xmlhttp.open("GET", url,true);
+        xmlhttp.responseType = 'arraybuffer';
+        //xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
+        //var kv = [];
+        //for(var i in data) {
+        //    kv.push(i +'=' + encodeURIComponent(  data[i]));
+        //}
+        
+
+        xmlhttp.onload=function() {
+           
+            var ar= [];
+             
+            var r = new Uint8Array(xmlhttp.response);
+            for (i=0;i<r.byteLength;i++) {
+                ar.push(r[i]);
+            }
             
-            console.log("TRY downloading!");
+            // location name for location download.
+            var pref = data.pref;
             
-            var ar = ifd.getElementById('footerform').elements;
-            var data= {};
-            for(var i =0; i < ar.length; i++) {
-                var e = ar[i];
-                data[e.name] = e.value;
+            console.log(
+                JSON.stringify ({
+                    requesturl : url,
+                    method: 'nsdownloadpage',
+                    filename : pref+ '-' + NS.id + '.csv',
+                    headers : xmlhttp.getAllResponseHeaders(),
+                    contentType:xmlhttp.getResponseHeader("Content-Type"),
+                    data: ar
+                }));
+           
+            if (data.pref == 'SG-Account') {
+                console.log(
+                    JSON.stringify ({
+                        method : 'exit'
+                    })
+                );
+                return;
             }
-            console.log(data);
-            console.log(ifr.contentWindow.reportTable.getTotalRowCount());
-             NS.downloaddata(
-                'https://system.netsuite.com/app/reporting/reportrunner.nl',
+            NS.downloadgetdata(
+                'https://system.netsuite.com/app/common/search/searchresults.csv' +
+                '?searchtype=Transaction' + 
+                '&Transaction_ACCOUNT=' + data.id +
+                '&Transaction_DEPARTMENT=1' + // sg
+                '&sortcol=Transaction_TRANDATE_raw' +
+                '&sortdir=ASC'+
+                '&csv=Export'+
+                '&OfficeXML=F'+
+                '&pdf='+
+                '&style=NORMAL'+
+                '&report='+
+                '&grid='+
+                '&searchid=120'+
+                '&dle=F',
+                
                 {
                     id: data.id,
-                    reportaction: 'exportcsv',
-                    apptype: 'html',
-                    visibleranges: '0,' +  ifr.contentWindow.reportTable.getTotalRowCount()
+                    pref: 'SG-Account'
+                    //reportaction: 'exportcsv',
+                    //apptype: 'html',
+                    //visibleranges: '0,' +  reportTable.getTotalRowCount()
                 }
                 
             );
             
+           
+           
         }
         
-        // do stuff..
-        // we really just want to send a series of download requests..
-        //https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403
-        
-        var ifr = document.createElement('iframe');
-        
-        ifr.setAttribute('src','https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=' + id);
-        ifr.setAttribute('width','900');
-        ifr.setAttribute('height','700');
-        ifr.addEventListener('load', frameload);
-        document.body.innerHTML = '';
-        
-        document.body.appendChild(ifr);
-        
-        /*
-        var ifd = ifr.contentWindow.document;
-        ifd.getElementById('lines_chkbox').
-        
-        var ar = ifd.getElementById('footerform').elements;
-        for(var i =0; i < ar.length; i++) {
-            var e = ar[i];
-            data[e.name] = e.value;
-        }
-        console.log(data);
-        
-        data.lines_chkbox = 'T';
-        data.reportaction = 'exportcsv'
-        //data.visibleranges =
-        data.apptype = 'html';
-        this.downloaddata(
-            'https://system.netsuite.com/app/reporting/reportrunner.nl?reporttype=REGISTER&acctid=403',
-            data
-        )
-        */
-        
-        
+        xmlhttp.send();
+           
     },
+    
+    
+    
     downloaddata : function (url,data) {
         
        
@@ -125,11 +391,21 @@ NS = {
                 ar.push(r[i]);
             }
             
+            // location name for location download.
+            var pref = '';
+            if (document.getElementById('hddn_crit_32')) {
+                //stock.
+                pref =  document.getElementById('hddn_crit_32').value ;
+            } else {
+                // accounts.
+                pref = document.getElementsByClassName('pt_title')[0].innerText.replace(/\s+$/,'').replace(/\//g, '');
+            }
+            
             console.log(
                 JSON.stringify ({
                     requesturl : url,
                     method: 'nsdownloadpage',
-                    filename : NS.id + '.csv',
+                    filename : pref+ '-' + NS.id + '.csv',
                     headers : xmlhttp.getAllResponseHeaders(),
                     contentType:xmlhttp.getResponseHeader("Content-Type"),
                     data: ar
@@ -139,6 +415,29 @@ NS = {
         
         xmlhttp.send(kv.join('&'));
            
+    },
+    login : function(u,p,id)
+    {
+        
+        try {
+            document.forms[0].email.value = u;
+            document.forms[0].password.value = p;
+            
+            
+            
+            document.forms[0].elements['jsenabled'].value = 'T';
+            setLoginCookie(document.forms[0].elements['rememberme'].checked);
+            
+            document.forms[0].submit();
+            
+            
+            
+        } catch( e) {
+            this.rungrab(id);
+            
+        }
     }
     
+    
+    
 }
\ No newline at end of file