Example #1
0
 public ActionResult Country_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]  IEnumerable <CRM_Location_Countries> listEx)
 {
     if (asset.Update)
     {
         try
         {
             if (listEx != null)
             {
                 foreach (var regis in listEx)
                 {
                     if (String.IsNullOrEmpty(regis.CountryName))
                     {
                         ModelState.AddModelError("", "Please input Country Name ");
                         return(Json(listEx.ToDataSourceResult(request, ModelState)));
                     }
                     var write = new CRM_Location_Countries();
                     var check = CRM_Location_Countries.GetCRM_Location_Countries("1=1", "").Where(s => s.CountryName.Trim().ToLower() == regis.CountryName.Trim().ToLower() && s.AliasID == regis.AliasID && s.Active == regis.Active);
                     if (check.Count() > 0)
                     {
                         ModelState.AddModelError("", "Country Name is exists.");
                         return(Json(listEx.ToDataSourceResult(request, ModelState)));
                     }
                     write.CountryID          = regis.CountryID;
                     write.CountryName        = regis.CountryName.Trim();
                     write.Active             = regis.Active;
                     write.RowLastUpdatedTime = DateTime.Now;
                     write.RowLastUpdatedUser = currentUser.UserName;
                     write.AliasID            = regis.AliasID != null ? regis.AliasID : "";
                     write.Update();
                 }
             }
             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 update record");
         return(Json(listEx.ToDataSourceResult(request, ModelState)));
     }
     return(Json(listEx.ToDataSourceResult(request)));
 }
Example #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\DC_Location_Country.xlsx"));
                        template.CopyTo(errorFileLocation);
                        FileInfo _fileInfo = new FileInfo(errorFileLocation);
                        var      _excelPkg = new ExcelPackage(_fileInfo);

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

                        //remove row
                        int totalRows = oSheet.Dimension.End.Row;
                        for (int i = 2; i <= totalRows; i++)
                        {
                            // get data sheet Staff
                            string countryID   = oSheet.Cells[i, 1].Value != null ? oSheet.Cells[i, 1].Value.ToString() : "";
                            string countryName = oSheet.Cells[i, 2].Value != null ? oSheet.Cells[i, 2].Value.ToString() : "";
                            string alias       = oSheet.Cells[i, 3].Value != null ? oSheet.Cells[i, 3].Value.ToString() : "";
                            string active      = oSheet.Cells[i, 4].Value != null ? oSheet.Cells[i, 4].Value.ToString() : "TRUE";
                            string id_Alias    = !String.IsNullOrEmpty(alias) ? alias.Substring(0, alias.LastIndexOf("-")).Trim() : "";

                            try
                            {
                                var write            = new CRM_Location_Countries();
                                var checkCountryName = CRM_Location_Countries.GetCRM_Location_Countries("[CountryName] = N'" + countryName + "' AND [AliasID] = '" + id_Alias + "'", "").FirstOrDefault();
                                var checkAlias       = DC_Location_Alias.GetDC_Location_Alias("[AliasID] = '" + id_Alias + "'", "").FirstOrDefault();
                                if (string.IsNullOrEmpty(countryName.ToString()) || string.IsNullOrEmpty(alias.ToString()))
                                {
                                    eSheet.Cells[rownumber, 1].Value = countryName;
                                    eSheet.Cells[rownumber, 2].Value = alias;
                                    eSheet.Cells[rownumber, 3].Value = active;
                                    eSheet.Cells[rownumber, 8].Value = "countryName, Alias required";
                                    rownumber++;
                                }
                                else if (checkCountryName != null)
                                {
                                    //eSheet.Cells[rownumber, 1].Value = countryName;
                                    //eSheet.Cells[rownumber, 2].Value = alias;
                                    //eSheet.Cells[rownumber, 3].Value = active;
                                    //eSheet.Cells[rownumber, 8].Value = "countryName & Alias exist in system";
                                    //rownumber++;
                                    write.CountryID          = countryID;
                                    write.CountryName        = countryName.Trim();
                                    write.Active             = Convert.ToBoolean(active);
                                    write.RowLastUpdatedTime = DateTime.Now;
                                    write.RowLastUpdatedUser = currentUser.UserName;
                                    write.AliasID            = id_Alias;
                                    write.Update();
                                }
                                else if (checkAlias == null)
                                {
                                    eSheet.Cells[rownumber, 2].Value = countryName;
                                    eSheet.Cells[rownumber, 3].Value = alias;
                                    eSheet.Cells[rownumber, 4].Value = active;
                                    eSheet.Cells[rownumber, 9].Value = "Alias not exist in system";
                                    rownumber++;
                                }
                                else
                                {
                                    string id = "";

                                    var checkID = CRM_Location_Countries.GetCRM_Location_Countries("1=1", "").OrderByDescending(m => m.RowID).FirstOrDefault();
                                    if (checkID != null)
                                    {
                                        var nextNo = Int32.Parse(checkID.CountryID.Substring(1, checkID.CountryID.Length - 1)) + 1;
                                        id = "C" + String.Format("{0:0000}", nextNo);
                                    }
                                    else
                                    {
                                        id = "C0001";
                                    }

                                    write.CountryID      = id;
                                    write.CountryName    = countryName.Trim();
                                    write.Active         = bool.Parse(active);
                                    write.RowCreatedTime = DateTime.Now;
                                    write.RowCreatedUser = currentUser.UserName;
                                    write.AliasID        = id_Alias;
                                    write.Save();
                                }
                                total++;
                            }

                            catch (Exception e)
                            {
                                eSheet.Cells[rownumber, 2].Value = countryName;
                                eSheet.Cells[rownumber, 3].Value = alias;
                                eSheet.Cells[rownumber, 4].Value = active;
                                eSheet.Cells[rownumber, 9].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 }));
            }
        }