Esempio n. 1
0
        public ActionResult ImportCustomerContacts(HttpPostedFileBase postedFile)
        {
            try
            {
                string filePath = string.Empty;
                if (postedFile != null)
                {
                    string path = Server.MapPath("~/ImportCustomerContacts/");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    filePath = path + Path.GetFileName(postedFile.FileName);
                    string extension = Path.GetExtension(postedFile.FileName);
                    postedFile.SaveAs(filePath);

                    DataTable dtToExport = new DataTable();
                    int       cnt        = 0;
                    using (StreamReader sr = new StreamReader(filePath))
                    {
                        while (!sr.EndOfStream)
                        {
                            //string[] col = sr.ReadLine().Split(',');
                            string[] col = Regex.Split(sr.ReadLine(), ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
                            DataRow  dr  = dtToExport.NewRow();
                            if (col.Length > 0)
                            {
                                for (int i = 0; i < col.Length; i++)
                                {
                                    if (cnt == 0)
                                    {
                                        DataColumn dc = new DataColumn();
                                        dc.ColumnName = col[i].ToString();
                                        dtToExport.Columns.Add(dc);
                                    }
                                    else
                                    {
                                        dr[i] = col[i].ToString();
                                    }
                                }

                                if (cnt > 0)
                                {
                                    dtToExport.Rows.Add(dr);
                                }
                                if (cnt == 0)
                                {
                                    cnt++;
                                }
                            }
                        }
                    }


                    string[] columnNames = dtToExport.Columns.Cast <DataColumn>().Select(x => x.ColumnName).ToArray();
                    if (columnNames[0] != "Customer Name" && columnNames[1] != "Customer Contact" && columnNames[2] != "Contact Phone" && columnNames[3] != "Contact Email")
                    {
                        ViewBag.Message = "CSV file not in correct format. Please select valid CSV file and try again!";
                        return(View("ManageCustomerContacts"));
                    }
                    CustomerRepository _respository = new CustomerRepository();
                    for (int i = 0; i < dtToExport.Rows.Count; i++)
                    {
                        CustomerViewModel cst = new CustomerViewModel();
                        cst.CustomerName = dtToExport.Rows[i][0].ToString().Replace(@"""", "");

                        cst.InActive = false;
                        cst.Notes    = "";

                        int CustomerId;
                        CustomerId = _respository.AddUpdateCustomerImport(cst);
                        // Call SaveActivityLog("frmImportCustomers", "Import Customer", "Customer " + cst.CustomerName + " added from csv file " + postedFile.FileName + " by user " + Convert.ToString(Session["User"]) + ".")
                        var act = new ActivityRepository();
                        act.AddActivityLog(Convert.ToString(Session["User"]), "Import Customer", "ImportCustomerContacts", "Customer " + cst.CustomerName + " added from csv file " + postedFile.FileName + " by user " + Convert.ToString(Session["User"]) + ".");
                        if (dtToExport.Rows[i][1].ToString() != "" && dtToExport.Rows[i][2].ToString() != " && dtToExport.Rows[i][3].ToString() != ")
                        {
                            CustomerContactViewModel cstAdd = new CustomerContactViewModel();
                            cstAdd.CustomerId       = CustomerId;
                            cstAdd.CustomerContact  = dtToExport.Rows[i][1].ToString();
                            cstAdd.ContactPhone     = dtToExport.Rows[i][2].ToString();
                            cstAdd.ContactEmail     = dtToExport.Rows[i][3].ToString();
                            cstAdd.IsPrimaryContact = false;
                            // cstAdd.DateAdded = System.DateTime.Now;
                            CommonFunctions c = new CommonFunctions();
                            cstAdd.DateAdded = c.GetCurrentDate();

                            _respository.AddUpdateCustomerContact(cstAdd, "Add");
                            act = new ActivityRepository();
                            act.AddActivityLog(Convert.ToString(Session["User"]), "Import Customer Contacts", "ImportCustomer", "Contacts added for customer " + cst.CustomerName + " from csv file " + postedFile.FileName + " by user " + Convert.ToString(Session["User"]) + ".");
                        }
                    }
                }
                else
                {
                    ViewBag.Message = "Please select file to import";
                }

                return(View("ManageCustomerContacts"));
            }
            catch (Exception ex)
            {
                ViewBag.Message = "CSV file not in correct format. Please select valid CSV file and try again!";
                return(View("ManageCustomerContacts"));
            }
        }