Ejemplo n.º 1
0
        public async Task <ActionResult <GenericValidator> > ProcesseTxNew()
        {
            var gVal = new GenericValidator();
            var form = Request.Form;

            try
            {
                var formFiles = form.Files;
                if (!formFiles.Any())
                {
                    gVal.Code    = -1;
                    gVal.Message = "The requested file is empty";
                    return(gVal);
                }

                var formFile = formFiles[0];
                if (formFile == null || formFile.Length <= 0)
                {
                    gVal.Code    = -1;
                    gVal.Message = "The selected file could not be processed";
                    return(gVal);
                }

                if (!Path.GetExtension(formFile.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
                {
                    gVal.Code    = -1;
                    gVal.Message = "Unknown file extension";
                    return(gVal);
                }

                var list = new List <TXCurrImport>();

                using (var stream = new MemoryStream())
                {
                    await formFile.CopyToAsync(stream);

                    using (var package = new ExcelPackage(stream))
                    {
                        var worksheet = package.Workbook.Worksheets[0];
                        var rowCount  = worksheet.Dimension.Rows;

                        for (int row = 2; row <= rowCount; row++)
                        {
                            var siteId   = worksheet.Cells[row, 1].Value.ToString().Trim();
                            var siteName = worksheet.Cells[row, 2].Value.ToString().Trim();
                            var txNew    = worksheet.Cells[row, 4].Value;
                            var txCurr   = worksheet.Cells[row, 3].Value;

                            long tX_CURR_TARGET = 0;
                            long tX_NEW_TARGET  = 0;

                            if (txNew != null)
                            {
                                tX_NEW_TARGET = Convert.ToInt64(txNew.ToString().Trim().Replace(" ", string.Empty));
                            }
                            if (txCurr != null)
                            {
                                tX_CURR_TARGET = Convert.ToInt64(txCurr.ToString().Trim().Replace(" ", string.Empty));
                            }

                            var fISCAL_YEAR = worksheet.Cells[row, 5].Value.ToString().Trim();

                            if (!string.IsNullOrEmpty(siteId) && !string.IsNullOrEmpty(siteName) && (tX_CURR_TARGET > 0 || tX_NEW_TARGET > 0) && !string.IsNullOrEmpty(fISCAL_YEAR))
                            {
                                list.Add(new TXCurrImport
                                {
                                    SiteId         = siteId,
                                    SiteName       = siteName,
                                    TX_CURR_TARGET = tX_CURR_TARGET,
                                    TX_NEW_TARGET  = tX_NEW_TARGET,
                                    FISCAL_YEAR    = int.Parse(fISCAL_YEAR)
                                });
                            }
                        }

                        if (list.Any())
                        {
                            var t = _siteService.AddTXCurrs(list);
                            gVal.Code    = 5;
                            gVal.Message = t + " entries successfully processed";
                        }
                        else
                        {
                            gVal.Code    = -1;
                            gVal.Message = "File could not be processed. Please try again later";
                        }
                        return(gVal);
                    }
                }
            }
            catch (Exception e)
            {
                gVal.Code    = -1;
                gVal.Message = "File could not be processed. Please try again later";
                return(gVal);
            }
        }