Esempio n. 1
0
        protected void DownloadTemp(User user, string usFileRename, string token)
        {
            string sFileName = GetSafeFile(usFileRename);

            string contentType = SiteFileSystem.GetContentType(sFileName);

            if (sFileName != "" && contentType != "" && Crypt.IsTokenGood(token))
            {
                string eTempFile = Path.Combine(SiteFileSystem.GetTempFileDirectory(), token.Substring(32));
                byte[] file      = Crypt.DecryptFileToArray(user, eTempFile, token);
                if (file.Length > 0)
                {
                    File.Delete(eTempFile);
                    WriteFileToResponse(sFileName, contentType, file);
                }
                else
                {
                    Response.Redirect(SiteMaster.Path404);
                }
            }
            else
            {
                Response.Redirect(SiteMaster.Path404);
            }
        }
Esempio n. 2
0
        public static bool DecryptTempFileToFile(User user, string oFileName, string token)
        {
            if (!IsTokenGood(token))
            {
                return(false);
            }
            string iFile = Path.Combine(SiteFileSystem.GetTempFileDirectory(), token.Substring(32));

            return(DecryptFileToFile(user, iFile, oFileName, token));
        }
Esempio n. 3
0
        public static string GenerateReport(User user, DateTime startDate, DateTime endDate)
        {
            try
            {
                DBConnect   connection = ConnectionsMgr.GetSharedConnection(user, _Database.ECGB);
                Application app        = new Application();
                app.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
                Workbook  xlWB = app.Workbooks.Add();
                Worksheet ws   = xlWB.Worksheets.Add();
                app.ActiveWindow.SplitRow    = 1;
                app.ActiveWindow.FreezePanes = true;

                //string queryReport = "SELECT sum(d.quantity) AS PidQty,d.unitprice AS GLC,d.chgprice AS MilCost,d.retailprc AS MilRetail,d.vendornum AS PID,trim(h.ponumber) AS contract,trim(h.custorder) AS PO,h.department AS Dept,h.deptname AS Brand,h.arrivdate AS INDCDate" +
                //    " FROM gdetl855 AS d JOIN ghead855 AS h ON d.uniquekey=h.uniquekey WHERE arrivdate BETWEEN '{0}' AND '{1}' GROUP BY d.vendornum,h.ponumber ORDER BY h.ponumber,d.upcnum";
                string queryReport = "SELECT sum(d.quantity) AS PidQty,d.unitprice AS GLC,d.chgprice AS MilCost,d.retailprc AS MilRetail,d.vendornum AS PID,c.COLORCODE, c.ITEMCOLOR,trim(h.ponumber) AS contract,trim(h.custorder) AS PO,h.department AS Dept,h.deptname AS Brand,h.arrivdate AS INDCDate" +
                                     " FROM gdetl855 AS d JOIN ghead855 AS h ON d.uniquekey=h.uniquekey " +
                                     " LEFT join catinfo c on d.VENDORNUM=c.VENDORNUM and d.UPCNUM = c.UPCNUM and h.PONUMBER = c.PONUMBER " +
                                     " WHERE h.arrivdate BETWEEN '{0}' AND '{1}' " +
                                     //" GROUP BY d.vendornum,h.ponumber ORDER BY h.ponumber,d.upcnum";
                                     " GROUP BY h.ponumber,d.vendornum,c.colorcode ORDER BY h.ponumber,d.vendornum,c.colorcode";
                string   formatReport = string.Format(queryReport, startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"));
                DBResult resultReport = connection.Query(formatReport);

                char   colLastLetter = (char)('A' + resultReport.FieldCount - 1);
                string rowLastIndex  = (2 + resultReport.AffectedRows - 1).ToString();

                object[,] reportData = new object[resultReport.AffectedRows, resultReport.FieldCount];
                for (int rowIndex = 0; rowIndex < resultReport.AffectedRows; rowIndex++)
                {
                    resultReport.Read();
                    for (int columnIndex = 0; columnIndex < resultReport.FieldCount; columnIndex++)
                    {
                        reportData[rowIndex, columnIndex] = resultReport.Field2(columnIndex, "'---");
                    }
                }

                var rangeHead = ws.Range["A1", colLastLetter + "1"];
                var rangeBody = ws.Range["A2", colLastLetter + rowLastIndex];
                rangeHead.Value = new[] { "PidQty", "GLC", "MilCost", "MilRetail", "PID", "Color Code", "Color Description", "Contract", "PO #", "Dept.", "Brand", "INDCDate" };
                rangeBody.Value = reportData;
                ws.Columns.AutoFit();
                string diFileName = SiteFileSystem.GetTempFileName();
                xlWB.SaveCopyAs(diFileName);
                xlWB.Close(false);
                string eoToken = Crypt.EncryptFileToFile(user, diFileName);
                File.Delete(diFileName);
                return(eoToken);
            }
            catch (Exception e)
            {
                ProgramLog.LogError(user, "GVMReport", "GenerateReport", e.Message);
                return("");
            }
        }
Esempio n. 4
0
        private static string SaveReportToExcelFile(User user, string[] columnHeaders, object[,] reportData, int rowCount, int colCount, string[] currencyCols)
        {
            try
            {
                Application app = new Application();
                app.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
                Workbook  xlWB = app.Workbooks.Add();
                Worksheet ws   = xlWB.ActiveSheet;
                app.ActiveWindow.SplitRow    = 1;
                app.ActiveWindow.FreezePanes = true;

                char   colLastLetter = (char)('A' + colCount - 1);
                string rowLastIndex  = (2 + rowCount - 1).ToString();

                var rangeHead = ws.Range["A1", colLastLetter + "1"];
                var rangeBody = ws.Range["A2", colLastLetter + rowLastIndex];
                rangeBody.NumberFormat = "@";

                rangeHead.Value = columnHeaders;
                rangeBody.Value = reportData;

                foreach (var col in currencyCols)
                {
                    ws.Range[col].NumberFormat = "$#,##0.00";
                }
                ws.Columns.AutoFit();

                string diFileName = SiteFileSystem.GetTempFileName();
                xlWB.SaveCopyAs(diFileName);
                xlWB.Close(false);
                app.Quit();
                Marshal.ReleaseComObject(ws);
                Marshal.ReleaseComObject(xlWB);
                Marshal.ReleaseComObject(app);
                string eoToken = Crypt.EncryptFileToFile(user, diFileName);
                File.Delete(diFileName);
                return(eoToken);
            }
            catch (Exception e)
            {
                Log(user, nameof(SaveReportToExcelFile), e);
                return("");
            }
        }
Esempio n. 5
0
 private void Upload_Click(object sender, EventArgs e)
 {
     if (SiteMaster.VerifyRequest(HttpContext.Current.Session))
     {
         try
         {
             User user = Session[SKeys.User] as User;
             if (fileUpload.HasFile)
             {
                 if (fileUpload.PostedFile.ContentLength >= MaxFileSize)
                 {
                     Session[SKeys.UploadResponse] = ApiResponse.JSONError(ResponseType.ErrorUploadFileTooLarge);
                     return;
                 }
                 if (!(SiteFileSystem.IsExtensionAllowed(fileUpload.FileName) && SiteFileSystem.IsContentTypeAllowed(fileUpload.PostedFile.ContentType)))
                 {
                     Session[SKeys.UploadResponse] = ApiResponse.JSONError(ResponseType.ErrorUploadFileFormatNotSupported);
                     return;
                 }
                 string token = Crypt.EncryptStreamToTempFile(user, fileUpload.FileContent);
                 if (string.IsNullOrWhiteSpace(token))
                 {
                     Session[SKeys.UploadResponse] = ApiResponse.JSONError(ResponseType.ErrorUploadUnknown);
                     return;
                 }
                 else
                 {
                     UploadDetail detail = new UploadDetail()
                     {
                         Extension = Path.GetExtension(fileUpload.FileName), Token = token
                     };
                     Session[SKeys.UploadResponse] = ApiResponse.JSONSuccess(detail);
                 }
             }
         }
         catch
         {
             Session[SKeys.UploadResponse] = ApiResponse.JSONError(ResponseType.ErrorUploadUnknown);
         }
     }
 }
Esempio n. 6
0
        public static string EncryptStreamToTempFile(User user, Stream diStream)
        {
            try
            {
                if (!diStream.CanRead)
                {
                    return("");
                }

                byte[] iv  = new byte[IVSizeBytes];
                byte[] key = new byte[KeySizeBytes];
                using (RNGCryptoServiceProvider rcsp = new RNGCryptoServiceProvider())
                {
                    rcsp.GetBytes(iv);
                    rcsp.GetBytes(key);
                }
                string ivAsString  = ByteArrayToString(iv);
                string keyAsString = ByteArrayToString(key);
                string oFileName   = Path.Combine(SiteFileSystem.GetTempFileDirectory(), ivAsString);
                using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
                {
                    aesProvider.KeySize = KeySizeBits;
                    aesProvider.IV      = iv;
                    aesProvider.Key     = key;
                    using (FileStream eoStream = File.Create(oFileName))
                        using (CryptoStream cryptoStream = new CryptoStream(eoStream, aesProvider.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            diStream.CopyTo(cryptoStream);
                        }
                }
                return(keyAsString + ivAsString);
            }
            catch (Exception e)
            {
                ProgramLog.LogError(user, "Crypt", "EncryptStreamToTempFile", e.Message);
                return("");
            }
        }
Esempio n. 7
0
 public static string GetTemplateLink(string version, string data)
 {
     if (SiteMaster.VerifyRequest(HttpContext.Current.Session))
     {
         var    user       = HttpContext.Current.Session[SKeys.User] as User;
         var    isTest     = (bool?)HttpContext.Current.Session[SKeys.IsTest] == true;
         string tplPath    = SiteFileSystem.GetTemplateDocFilePath(user, isTest, "855.xlsx");
         string linkReturn = "";
         if (tplPath != "")
         {
             linkReturn = "doc/template/855.xlsx";
         }
         else
         {
             linkReturn = "";
         }
         return(ApiResponse.JSONSuccess(linkReturn));
     }
     else
     {
         return(ApiResponse.JSONError(ResponseType.ErrorAuth));
     }
 }
Esempio n. 8
0
        protected void DownloadTemplate(User user, string usFileName)
        {
            string sFileName   = GetSafeFile(usFileName);
            string contentType = SiteFileSystem.GetContentType(sFileName);

            if (!SiteFileSystem.IsExtensionAllowed(sFileName))
            {
                Response.Redirect(SiteMaster.Path404);
                return;
            }
            var    isTest  = (bool?)HttpContext.Current.Session[SKeys.IsTest] == true;
            string tplPath = SiteFileSystem.GetTemplateDocFilePath(user, isTest, sFileName);

            if (tplPath != "")
            {
                byte[] file = File.ReadAllBytes(tplPath);
                WriteFileToResponse(sFileName, contentType, file);
            }
            else
            {
                Response.Redirect(SiteMaster.Path404);
            }
        }
Esempio n. 9
0
        public static string CheckUpload(string version, string data)
        {
            if (SiteMaster.VerifyRequest(HttpContext.Current.Session))
            {
                var user    = HttpContext.Current.Session[SKeys.User] as User;
                var session = HttpContext.Current.Session;
                if (session[SKeys.UploadResponse] == null)
                {
                    // No upload response.
                    return(ApiResponse.JSONSuccess());
                }
                var respStr = session[SKeys.UploadResponse] as string;
                if (string.IsNullOrWhiteSpace(respStr))
                {
                    // Key set, but no content.
                    return(ApiResponse.JSONSuccess());
                }
                session.Remove(SKeys.UploadResponse);

                ApiResponse response = JsonConvert.DeserializeObject <ApiResponse>(respStr);
                if (!response.success)
                {
                    // Error happened, so return that.
                    return(respStr);
                }

                // Get upload data.
                UploadDetail up = ((JObject)response.data).ToObject <UploadDetail>();
                if (up == null || string.IsNullOrWhiteSpace(up.Extension) || string.IsNullOrWhiteSpace(up.Token) || !SiteFileSystem.IsExtensionAllowed(up.Extension))
                {
                    // Response data was bad for some reason.
                    return(ApiResponse.JSONSuccess());
                }
                // Check upload data.
                if (Crypt.IsTokenGood(up.Token))
                {
                    string outFile = Path.Combine(SiteFileSystem.GetTempFileDirectory(), Path.GetRandomFileName());
                    string ecFile  = Path.Combine(SiteFileSystem.GetTempFileDirectory(), up.Token.Substring(32));
                    if (Crypt.DecryptTempFileToFile(user, outFile, up.Token))
                    {
                        // Remove old file.
                        File.Delete(ecFile);
                        // Then do verification.
                        ResponseType resp = POAcknowledgeManager.VerifyFile(user, outFile);
                        if (resp == ResponseType.SuccessAPO || resp == ResponseType.WarningAPOUnverifiedAccept)
                        {
                            // Good response, move the file
                            var isTest = (HttpContext.Current.Session[SKeys.IsTest] as bool?) == true;
                            try
                            {
                                var uploadFilePath = SiteFileSystem.GetUploadFileName(user, isTest, "855", up.Extension);
                                File.Move(outFile, uploadFilePath);
                                ProcessQueue.CreateUploadRecord(user, DateTime.Now, "855", Path.GetFileName(uploadFilePath));
                            }
                            catch
                            {
                                return(ApiResponse.JSONError(ResponseType.ErrorAPOUnknown));
                            }
                            if (resp == ResponseType.SuccessAPO)
                            {
                                return(ApiResponse.JSONSuccess(ResponseDescription.Get(resp)));
                            }
                            else
                            {
                                return(ApiResponse.JSONWarning(ResponseDescription.Get(resp)));
                            }
                        }
                        else
                        {
                            // Fail response, delete the file.
                            File.Delete(outFile);
                            return(ApiResponse.JSONError(resp));
                        }
                    }
                    else
                    {
                        return(ApiResponse.JSONError(ResponseType.ErrorAPOUnknown));
                    }
                }
                else
                {
                    // Bad token.
                    return(ApiResponse.JSONError(ResponseType.ErrorAPOUnknown));
                }
            }
            else
            {
                return(ApiResponse.JSONError(ResponseType.ErrorAuth));
            }
        }