//Import Supplier public StatusResponse Import(string filePath, string userName, out int totalRowExcel, List <string> ListCompanyId, ref ImportModel importModel, ref string msg) { totalRowExcel = 0; StatusResponse Response = new StatusResponse(); using (NuWebContext cxt = new NuWebContext()) { using (var transaction = cxt.Database.BeginTransaction()) { DataTable dtSupplier = ReadExcelFile(filePath, "Supplier"); DataTable dtIngSupplier = ReadExcelFile(filePath, "Ingredients_Supplier"); string tmpExcelPath = System.Web.HttpContext.Current.Server.MapPath("~/ImportExportTemplate") + "/SupplierImportTemplate.xlsx"; DataTable dtTmpSupplier = ReadExcelFile(@tmpExcelPath, "Supplier"); DataTable dtTmpSupplierIng = ReadExcelFile(@tmpExcelPath, "Ingredients_Supplier"); if (dtSupplier.Columns.Count != dtTmpSupplier.Columns.Count) { msg = _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(Commons._MsgDoesNotMatchFileExcel); return(Response); } if (dtIngSupplier.Columns.Count != dtTmpSupplierIng.Columns.Count) { msg = _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(Commons._MsgDoesNotMatchFileExcel); return(Response); } //======== var listcountry = cxt.I_Country.Where(x => x.IsActived).ToList(); var listing = cxt.I_Ingredient.Where(x => x.IsActive).ToList(); bool flagInsert = true; string msgError = ""; ImportItem itemErr = null; List <SupplierModels> Models = new List <SupplierModels>(); SupplierModels SupModel = null; foreach (var company in ListCompanyId) { foreach (DataRow item in dtSupplier.Rows) { flagInsert = true; msgError = ""; if (item[0].ToString().Equals("")) { continue; } int index = int.Parse(item[0].ToString()); //var Country = listcountry.Where(x => x.FullName.Trim().ToLower().Equals(item[4].ToString().Trim().ToLower())).FirstOrDefault(); //if (Country == null) //{ // flagInsert = false; // msgError += "<br/>Country is not exsits!"; //} //======================= SupModel = new SupplierModels(); string SupplierId = Guid.NewGuid().ToString(); SupModel.Id = SupplierId; SupModel.CompanyId = company; SupModel.Index = index.ToString(); SupModel.Name = item[1].ToString(); SupModel.Address = item[2].ToString(); SupModel.City = item[3].ToString(); SupModel.Country = item[4].ToString().Trim();//Country == null ? "" : Country.Id; SupModel.ZipCode = item[5].ToString(); SupModel.Phone1 = item[6].ToString(); SupModel.Phone2 = item[7].ToString(); SupModel.Fax = item[8].ToString(); SupModel.Email = item[9].ToString(); SupModel.ContactInfo = item[10].ToString(); SupModel.IsActived = string.IsNullOrEmpty(_AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(item[11].ToString())) ? false : bool.Parse(_AttributeForLanguage.CurrentUser.GetLanguageTextFromKey(item[11].ToString()).ToLower().Equals("true").ToString()) ? true : false; /*List Ingredient Supplier*/ List <Ingredients_SupplierModel> lstSupplierIng = new List <Ingredients_SupplierModel>(); DataRow[] IngSupplier = dtIngSupplier.Select("[" + _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Supplier Index") + "] = " + index + ""); foreach (DataRow ingsupplier in IngSupplier) { var Ingredient = listing.Where(x => x.Code.ToLower().Equals(ingsupplier[3].ToString().ToLower())).FirstOrDefault(); if (string.IsNullOrEmpty(ingsupplier[1].ToString())) { flagInsert = false; msgError += "<br/>" + _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Supplier Index is required"); } //==== if (string.IsNullOrEmpty(ingsupplier[3].ToString())) { flagInsert = false; msgError += "<br/>" + _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Ingredient Code is required"); } else if (Ingredient == null) { flagInsert = false; msgError += "<br/>" + _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Ingredient is not exsits"); } if (string.IsNullOrEmpty(ingsupplier[4].ToString())) { flagInsert = false; msgError += "<br/>" + _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Ingredient Name is required"); } if (flagInsert) { Ingredients_SupplierModel SupplierIngredient = new Ingredients_SupplierModel() { IngredientId = Ingredient.Id }; lstSupplierIng.Add(SupplierIngredient); } } SupModel.ListSupIng = lstSupplierIng; //====================== if (flagInsert) { Models.Add(SupModel); } else { itemErr = new ImportItem(); itemErr.Name = SupModel.Name; itemErr.ListFailStoreName.Add(""); itemErr.ListErrorMsg.Add(_AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Row") + ": " + SupModel.Index + msgError); importModel.ListImport.Add(itemErr); } } } //================= Response.Status = true; try { List <I_Supplier> lstSave = new List <I_Supplier>(); I_Supplier item = null; foreach (var model in Models) { item = new I_Supplier(); item.Id = model.Id; item.CompanyId = model.CompanyId; item.Name = model.Name; item.Address = model.Address; item.City = model.City; item.Country = model.Country; item.ZipCode = model.ZipCode; item.Phone1 = model.Phone1; item.Phone2 = model.Phone2; item.Fax = model.Fax; item.Email = model.Email; item.ContactInfo = model.ContactInfo; item.CreatedBy = userName; item.CreatedDate = DateTime.Now; item.ModifierDate = DateTime.Now; item.ModifierBy = userName; item.IsActived = model.IsActived; item.Status = (int)Commons.EStatus.Actived; lstSave.Add(item); } cxt.I_Supplier.AddRange(lstSave); //Supplier Ingredient foreach (var model in Models) { /*Detail*/ //Insert Ingredient Supplier List <I_Ingredient_Supplier> LstSupplierIng = new List <I_Ingredient_Supplier>(); foreach (var SupIng in model.ListSupIng) { LstSupplierIng.Add(new I_Ingredient_Supplier { Id = Guid.NewGuid().ToString(), IngredientId = SupIng.IngredientId, SupplierId = model.Id, CreatedBy = userName, CreatedDate = DateTime.Now, ModifierDate = DateTime.Now, ModifierBy = userName, IsActived = model.IsActived, }); } if (LstSupplierIng.Count > 0) { cxt.I_Ingredient_Supplier.AddRange(LstSupplierIng); } /*End Detail*/ } cxt.SaveChanges(); transaction.Commit(); if (importModel.ListImport.Count == 0) { ImportItem impItem = new ImportItem(); impItem.Name = _AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Supplier"); impItem.ListSuccessStoreName.Add(_AttributeForLanguage.CurrentUser.GetLanguageTextFromKey("Import Supplier Successful")); importModel.ListImport.Add(impItem); } } catch (Exception ex) { transaction.Rollback(); _logger.Error(ex); } } } return(Response); }
public bool Insert(SupplierModels model, ref string msg) { bool result = true; using (NuWebContext cxt = new NuWebContext()) { using (var transaction = cxt.Database.BeginTransaction()) { try { I_Supplier item = new I_Supplier(); string SupplierId = Guid.NewGuid().ToString(); item.Id = SupplierId; item.CompanyId = model.CompanyId; item.Name = model.Name; item.Address = model.Address; item.City = model.City; item.Country = model.Country; item.ZipCode = model.ZipCode; item.Phone1 = model.Phone1; item.Phone2 = model.Phone2; item.Fax = model.Fax; item.Email = model.Email; item.ContactInfo = model.ContactInfo; item.CreatedBy = model.CreatedBy; item.CreatedDate = model.CreatedDate; item.ModifierDate = model.ModifierDate; item.ModifierBy = model.ModifierBy; item.IsActived = model.IsActived; item.Status = (int)Commons.EStatus.Actived; //Supplier Ingredient List <I_Ingredient_Supplier> LstSupplierIng = new List <I_Ingredient_Supplier>(); foreach (var SupIng in model.ListSupIng) { LstSupplierIng.Add(new I_Ingredient_Supplier { Id = Guid.NewGuid().ToString(), IngredientId = SupIng.IngredientId, SupplierId = SupplierId, CreatedBy = model.CreatedBy, CreatedDate = model.CreatedDate, ModifierDate = model.ModifierDate, ModifierBy = model.ModifierBy, IsActived = true }); } cxt.I_Supplier.Add(item); cxt.I_Ingredient_Supplier.AddRange(LstSupplierIng); cxt.SaveChanges(); transaction.Commit(); /* INSERT OR UPDATE CONTACTS XERO */ InsertOrUpdateContactXero("insert", model, SupplierId); } catch (Exception ex) { NSLog.Logger.Error("Insert Supplier error:", ex); result = false; transaction.Rollback(); } finally { if (cxt != null) { cxt.Dispose(); } } } } return(result); }
//查询单个(查ID) public static int Select(ref M_Supplier Obj, int ID, ref string ErrMsg) { I_Supplier I = (D_Supplier)SimpleFactory.CreateObject(DBType.Supplier); return I.Select(ref Obj, ID, ref ErrMsg); }
//更新单个(查ID) public static int Update(M_Supplier Obj, ref string ErrMsg) { I_Supplier I = (D_Supplier)SimpleFactory.CreateObject(DBType.Supplier); return I.Update(Obj, ref ErrMsg); }
//插入多个 public static int Insert(ref List<M_Supplier> Obj, ref string ErrMsg) { I_Supplier I = (D_Supplier)SimpleFactory.CreateObject(DBType.Supplier); return I.Insert(ref Obj, ref ErrMsg); }
//删除单个(查ID) public static int Delete(int ID, ref string ErrMsg) { I_Supplier I = (D_Supplier)SimpleFactory.CreateObject(DBType.Supplier); return I.Delete(ID, ref ErrMsg); }