protected void btnImport_Click(object sender, EventArgs e) { btnImport.Enabled = false; fuImport.Enabled = false; btnImport.Text = "Processing..."; if (!fuImport.HasFile) ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Chọn file để nhập');", true); else { string ext = fuImport.FileName.Substring(fuImport.FileName.LastIndexOf('.') + 1).ToLower(); if (!ext.Equals("xls") && !ext.Equals("xlsx")) ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Định dạng file nhập ko đúng');", true); else { string newFileName = Guid.NewGuid().ToString("N") + "." + ext; string pathToFile = Server.MapPath("~\\Import") + "\\" + newFileName; fuImport.SaveAs(pathToFile); try { ObjLogin adm = (ObjLogin)Session["objLogin"]; int smsquota = int.Parse(ConfigurationManager.AppSettings["SMSQuota"]); double expiredDateConfig = Convert.ToDouble(ConfigurationManager.AppSettings["ExpiredDate"]); DateTime expiredDate = Convert.ToDateTime(DateTime.Now.AddDays(expiredDateConfig)); var GRepo = new GroupsRepository(); var RegionRepo = new RegionsRepository(); var AreaRepo = new AreasRepository(); var LocalRepo = new LocalsRepository(); var ChRepo = new ChannelRepository(); var SaleRepo = new SalesmanRepository(); var CLogRepo = new CustomersLogRepository(); var CRepo = new CustomersRepository(); var CTRepo = new CustomerTypeRepository(); var DisRepo = new DistrictsRepository(); var proviceRepo = new ProvincesRepository(); var sectionRepo = new SectionRepository(); SpreadsheetInfo.SetLicense("E24D-D739-F65A-4E00"); ExcelFile ef = new ExcelFile(); ef.LoadXls(pathToFile); ExcelWorksheet ws = ef.Worksheets[0]; for (int i = 2; i < ws.Rows.Count; i++) { try { vwMasterList vmMasterItem = new vwMasterList(ws.Rows[i].Cells[4].Value.ToString(), ws.Rows[i].Cells[5].Value.ToString(), ws.Rows[i].Cells[6].Value.ToString(), ws.Rows[i].Cells[7].Value.ToString(), ws.Rows[i].Cells[8].Value.ToString(), ws.Rows[i].Cells[9].Value.ToString(), ws.Rows[i].Cells[10].Value.ToString(), ws.Rows[i].Cells[11].Value.ToString(), ws.Rows[i].Cells[12].Value.ToString(), ws.Rows[i].Cells[13].Value.ToString(), ws.Rows[i].Cells[14].Value.ToString(), ws.Rows[i].Cells[15].Value.ToString(), ws.Rows[i].Cells[17].Value.ToString(), ws.Rows[i].Cells[18].Value.ToString(), ws.Rows[i].Cells[19].Value.ToString(), ws.Rows[i].Cells[20].Value.ToString(), ws.Rows[i].Cells[22].Value.ToString(), ws.Rows[i].Cells[23].Value.ToString(), ws.Rows[i].Cells[24].Value.ToString()); // Add Group - Region - Area - Local var groupId = GRepo.Import("", vmMasterItem.Group, ""); var regionId = RegionRepo.Import("", vmMasterItem.Region, "", groupId); var areaId = AreaRepo.Import("", vmMasterItem.Area, "", regionId); var localId = LocalRepo.Import("", vmMasterItem.Local, "", areaId); // Add section var sectionId = sectionRepo.Import(vmMasterItem.Area); // Add Province var provinceId = proviceRepo.Import(vmMasterItem.Local, sectionId); // Add District var districtId = DisRepo.Import(vmMasterItem.Local, provinceId); // Add Channel ChRepo.Insert("", vmMasterItem.Channel1, 0); ChRepo.Insert("", vmMasterItem.Channel2, ChRepo.GetChannelIdByName(vmMasterItem.Channel1)); ChRepo.Insert("", vmMasterItem.Channel3, ChRepo.GetChannelIdByName(vmMasterItem.Channel2)); // Add Customer Type CTRepo.Add("", vmMasterItem.Channel3); // Add Salesmen var tromId = SaleRepo.ImportSalesmen("", vmMasterItem.TROM, "", (int)SalesmenRole.TROM, smsquota, expiredDate, -1); var tpsId = SaleRepo.ImportSalesmen("", vmMasterItem.TPS, "", (int)SalesmenRole.TPS, smsquota, expiredDate, tromId); var tprId = SaleRepo.ImportSalesmen("", vmMasterItem.TPR, "", (int)SalesmenRole.TPR, smsquota, expiredDate, tpsId); var eromId = SaleRepo.ImportSalesmen("", vmMasterItem.EROM, "", (int)SalesmenRole.EROM, smsquota, expiredDate, -1); var pss1Id = SaleRepo.ImportSalesmen("", vmMasterItem.PSS1, "", (int)SalesmenRole.PSS1, smsquota, expiredDate, eromId); var psr1Id = SaleRepo.ImportSalesmen("", vmMasterItem.PSR1, "", (int)SalesmenRole.PSR1, smsquota, expiredDate, pss1Id); var erom2Id = SaleRepo.ImportSalesmen("", vmMasterItem.EROM2, "", (int)SalesmenRole.EROM2, smsquota, expiredDate, -1); var pss2Id = SaleRepo.ImportSalesmen("", vmMasterItem.PSS2, "", (int)SalesmenRole.PSS2, smsquota, expiredDate, erom2Id); var psr2Id = SaleRepo.ImportSalesmen("", vmMasterItem.PSR2, "", (int)SalesmenRole.PSR2, smsquota, expiredDate, pss2Id); // Add Salesgroup - salesregion - salesarea - saleslocal ImportSalesGroup(tromId, tpsId, tprId, eromId, pss1Id, psr1Id, erom2Id, pss2Id, psr2Id, GRepo, groupId); ImportSalesRegion(tromId, tpsId, tprId, eromId, pss1Id, psr1Id, erom2Id, pss2Id, psr2Id, RegionRepo, regionId); ImportSalesArea(tromId, tpsId, tprId, eromId, pss1Id, psr1Id, erom2Id, pss2Id, psr2Id, AreaRepo, areaId); ImportSalesLocal(tromId, tpsId, tprId, eromId, pss1Id, psr1Id, erom2Id, pss2Id, psr2Id, LocalRepo, localId); // Add Customer - Customer Log int CustomerId = CRepo.InsertCustomer(vmMasterItem.CustomerCode, vmMasterItem.Customername, vmMasterItem.Customeraddress, "", "", "", "", CTRepo.GetCustomerTypeIdByName(vmMasterItem.Channel3), ChRepo.GetChannelIdByName(vmMasterItem.Channel3), districtId, localId, DateTime.Now, DateTime.Now, true, false); CLogRepo.InsertCustomer(vmMasterItem.CustomerCode, vmMasterItem.Customername, vmMasterItem.Customeraddress, "", "", "", "", CTRepo.GetCustomerTypeIdByName(vmMasterItem.Channel3), ChRepo.GetChannelIdByName(vmMasterItem.Channel3), districtId, localId, DateTime.Now, DateTime.Now, true, CustomerId, false, 0, adm.Id, string.Empty); lstCustomer.Add(vmMasterItem); } catch (Exception ex) { // write log here => TBD } } CustomerList.DataSource = lstCustomer; CustomerList.DataBind(); } catch (Exception ex) { ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('" + ex.Message + "');", true); } finally { } } } btnImport.Enabled = true; fuImport.Enabled = true; btnImport.Text = "Import"; }