Пример #1
0
 public ActionResult Region_Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]  IEnumerable <CRM_Location_Region> listEx)
 {
     if (asset.Create)
     {
         try
         {
             if (listEx != null)
             {
                 foreach (var regis in listEx)
                 {
                     if (String.IsNullOrEmpty(regis.RegionName))
                     {
                         ModelState.AddModelError("", "Please input Region Name ");
                         return(Json(listEx.ToDataSourceResult(request, ModelState)));
                     }
                     //if (String.IsNullOrEmpty(regis.CountryID))
                     //{
                     //    ModelState.AddModelError("", "Please input Country Name");
                     //    return Json(listEx.ToDataSourceResult(request, ModelState));
                     //}
                     string id      = "";
                     var    write   = new CRM_Location_Region();
                     var    checkID = CRM_Location_Region.GetCRM_Location_Regions("1=1", "").OrderByDescending(m => m.RowID).FirstOrDefault();
                     if (checkID != null)
                     {
                         var nextNo = Int32.Parse(checkID.RegionID.Substring(1, checkID.RegionID.Length - 1)) + 1;
                         id = "R" + String.Format("{0:0000}", nextNo);
                     }
                     else
                     {
                         id = "R0001";
                     }
                     var check = CRM_Location_Region.GetCRM_Location_Regions("1=1", "").Where(s => s.RegionName.Trim().ToLower() == regis.RegionName.Trim().ToLower() && s.CountryID == regis.CountryID && s.Active == regis.Active).FirstOrDefault();
                     if (check != null)
                     {
                         ModelState.AddModelError("", " Region Name  is exists.");
                         return(Json(listEx.ToDataSourceResult(request, ModelState)));
                     }
                     write.RegionID       = id;
                     write.RegionName     = regis.RegionName.Trim();
                     write.Active         = regis.Active;
                     write.RowCreatedTime = DateTime.Now;
                     write.RowCreatedUser = currentUser.UserName;
                     write.CountryID      = regis.CountryID != null ? regis.CountryID : "";
                     write.Save();
                 }
             }
             else
             {
                 ModelState.AddModelError("error", "");
                 return(Json(new { success = false }));
             }
         }
         catch (Exception e)
         {
             ModelState.AddModelError("error", e.Message);
             return(Json(listEx.ToDataSourceResult(request, ModelState)));
         }
     }
     else
     {
         ModelState.AddModelError("", "You don't have permission to create record");
         return(Json(listEx.ToDataSourceResult(request, ModelState)));
     }
     return(Json(listEx.ToDataSourceResult(request)));
 }
Пример #2
0
        public ActionResult ImportFromExcel()
        {
            try
            {
                if (Request.Files["FileUpload"] != null && Request.Files["FileUpload"].ContentLength > 0)
                {
                    string fileExtension =
                        System.IO.Path.GetExtension(Request.Files["FileUpload"].FileName);

                    if (fileExtension == ".xlsx")
                    {
                        string fileLocation      = string.Format("{0}/{1}", Server.MapPath("~/Excel"), "[" + currentUser.UserName + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "]" + Request.Files["FileUpload"].FileName);
                        string errorFileLocation = string.Format("{0}/{1}", Server.MapPath("~/Excel"), "[" + currentUser.UserName + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-Error]" + Request.Files["FileUpload"].FileName);

                        if (System.IO.File.Exists(fileLocation))
                        {
                            System.IO.File.Delete(fileLocation);
                        }

                        Request.Files["FileUpload"].SaveAs(fileLocation);
                        //Request.Files["fileUpload"].SaveAs(errorFileLocation);

                        var rownumber = 2;
                        var total     = 0;

                        FileInfo fileInfo = new FileInfo(fileLocation);
                        var      excelPkg = new ExcelPackage(fileInfo);

                        FileInfo template = new FileInfo(Server.MapPath(@"~\ExportExcelFile\CRM_Location_Region.xlsx"));
                        template.CopyTo(errorFileLocation);
                        FileInfo _fileInfo = new FileInfo(errorFileLocation);
                        var      _excelPkg = new ExcelPackage(_fileInfo);

                        ExcelWorksheet oSheet = excelPkg.Workbook.Worksheets["CRM_Location_Region"];
                        ExcelWorksheet eSheet = _excelPkg.Workbook.Worksheets["CRM_Location_Region"];

                        //remove row
                        int totalRows = oSheet.Dimension.End.Row;
                        for (int i = 2; i <= totalRows; i++)
                        {
                            string regionID    = oSheet.Cells[i, 1].Value != null ? oSheet.Cells[i, 1].Value.ToString() : "";
                            string regionName  = oSheet.Cells[i, 2].Value != null ? oSheet.Cells[i, 2].Value.ToString() : "";
                            string countryName = oSheet.Cells[i, 3].Value != null ? oSheet.Cells[i, 3].Value.ToString() : "";
                            string active      = oSheet.Cells[i, 5].Value != null ? oSheet.Cells[i, 5].Value.ToString() : "TRUE";
                            string id_country  = !String.IsNullOrEmpty(countryName) ? countryName.Substring(0, countryName.LastIndexOf("-")).Trim() : "";

                            try
                            {
                                var write        = new CRM_Location_Region();
                                var checkRegion  = CRM_Location_Region.GetCRM_Location_Regions("[RegionName] = N'" + regionName + "' AND [CountryID] = '" + id_country + "'", "").FirstOrDefault();
                                var checkCountry = DC_Location_Countries.GetDC_Location_Countries("[CountryID] = '" + id_country + "'", "").FirstOrDefault();
                                if (string.IsNullOrEmpty(regionName.ToString()) || string.IsNullOrEmpty(countryName.ToString()))
                                {
                                    eSheet.Cells[rownumber, 2].Value  = regionName;
                                    eSheet.Cells[rownumber, 3].Value  = countryName;
                                    eSheet.Cells[rownumber, 5].Value  = active;
                                    eSheet.Cells[rownumber, 10].Value = "regionName, countryName required";
                                    rownumber++;
                                }
                                else if (checkRegion != null)
                                {
                                    write.RegionID           = regionID;
                                    write.RegionName         = regionName;
                                    write.Active             = Convert.ToBoolean(active);
                                    write.RowLastUpdatedTime = DateTime.Now;
                                    write.RowLastUpdatedUser = currentUser.UserName;
                                    write.CountryID          = id_country;
                                    write.Update();
                                    total++;
                                }
                                else if (checkCountry == null)
                                {
                                    eSheet.Cells[rownumber, 2].Value  = regionName;
                                    eSheet.Cells[rownumber, 3].Value  = countryName;
                                    eSheet.Cells[rownumber, 5].Value  = active;
                                    eSheet.Cells[rownumber, 10].Value = "countryName not exist in system";
                                    rownumber++;
                                }
                                else
                                {
                                    string id      = "";
                                    var    checkID = CRM_Location_Region.GetCRM_Location_Regions("1=1", "").OrderByDescending(m => m.RowID).FirstOrDefault();
                                    if (checkID != null)
                                    {
                                        var nextNo = Int32.Parse(checkID.RegionID.Substring(1, checkID.RegionID.Length - 1)) + 1;
                                        id = "A" + String.Format("{0:0000}", nextNo);
                                    }
                                    else
                                    {
                                        id = "A0001";
                                    }

                                    write.RegionID       = id;
                                    write.RegionName     = regionName;
                                    write.Active         = bool.Parse(active);
                                    write.RowCreatedTime = DateTime.Now;
                                    write.RowCreatedUser = currentUser.UserName;
                                    write.CountryID      = id_country;
                                    write.Save();
                                    total++;
                                }
                            }
                            catch (Exception e)
                            {
                                eSheet.Cells[rownumber, 2].Value  = regionName;
                                eSheet.Cells[rownumber, 3].Value  = countryName;
                                eSheet.Cells[rownumber, 5].Value  = active;
                                eSheet.Cells[rownumber, 10].Value = e.Message;
                                rownumber++;
                                continue;
                            }
                        }
                        _excelPkg.Save();

                        return(Json(new { success = true, total = total, totalError = rownumber - 2, link = errorFileLocation }));
                    }
                    else
                    {
                        return(Json(new { success = false, error = "File extension is not valid. *.xlsx please." }));
                    }
                }
                else
                {
                    return(Json(new { success = false, error = "File upload null" }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new { success = false, error = ex.Message }));
            }
        }