public SysSupplierAgent SetToBussinessObject(Sys_SupplierAgent Entity) { SysSupplierAgent Model = new SysSupplierAgent(); Model.SupplierAgentID = Entity.SupplierAgentID; Model.AgentID = Entity.AgentID; Model.SupplierID = Entity.SupplierID; Model.SupplierName = _context.Sys_Supplier.Where(m => m.SupplierID == Entity.AgentID).FirstOrDefault().SupplierName; Model.AgentType = Entity.AgentType; Model.IsActive = Entity.IsActive ? "Active" : "Inactive"; return(Model); }
public Sys_SupplierAgent SetToModelObject(SysSupplierAgent model) { Sys_SupplierAgent Entity = new Sys_SupplierAgent(); Entity.SupplierAgentID = model.SupplierAgentID; Entity.AgentID = model.AgentID; Entity.SupplierID = model.SupplierID; Entity.AgentType = model.AgentType; Entity.IsActive = model.IsActive == "Active"; Entity.IsDelete = false; Entity.SetOn = DateTime.Now; Entity.SetBy = model.SetBy; Entity.IPAddress = string.Empty; return(Entity); }
public ValidationMsg Save(SysSupplier model, int userid) { _vmMsg = new ValidationMsg(); try { using (var tx = new TransactionScope()) { using (_context) { //var exitSupplierCode = _context.Sys_Supplier.Where(m => m.SupplierCode == model.SupplierCode).ToList(); //if (exitSupplierCode.Count > 0) //{ // _vmMsg.Type = Enums.MessageType.Error; // _vmMsg.Msg = "Supplier Code Already Exit."; //} //else //{ #region Supplier model.SupplierCode = DalCommon.GetPreDefineNextCodeByUrl("Supplier/Supplier");//DalCommon.GetPreDefineValue("1", "00045"); model.SetBy = userid; Sys_Supplier tblSysSupplier = SetToModelObject(model); _context.Sys_Supplier.Add(tblSysSupplier); _context.SaveChanges(); SupplierID = tblSysSupplier.SupplierID; #endregion #region Save Supplier Address if (model.SupplierAddressList != null) { if (model.SupplierAddressList.Count > 1) { supplieraddress = 1; } else { foreach (SysSupplierAddress objSysSupplierAddress in model.SupplierAddressList) { objSysSupplierAddress.SupplierID = SupplierID; objSysSupplierAddress.SetBy = userid; objSysSupplierAddress.IsActive = string.IsNullOrEmpty(objSysSupplierAddress.IsActive) ? "Active" : objSysSupplierAddress.IsActive; Sys_SupplierAddress tblSupplierAddress = SetToModelObject(objSysSupplierAddress); _context.Sys_SupplierAddress.Add(tblSupplierAddress); } #region Save Supplier Agent if (model.SupplierAgentList != null) { foreach (SysSupplierAgent objSupplierAgent in model.SupplierAgentList) { if (objSupplierAgent.SupplierID != 0) { if (objSupplierAgent.AgentType == "Local Agent") { ++localAgent; } else if (objSupplierAgent.AgentType == "Foreign Agent") { ++foreignAgent; } if (localAgent == 1 || foreignAgent == 1) { objSupplierAgent.SupplierAgentID = SupplierID; objSupplierAgent.SupplierID = SupplierID; objSupplierAgent.SetBy = userid; objSupplierAgent.IsActive = string.IsNullOrEmpty(objSupplierAgent.IsActive) ? "Active" : objSupplierAgent.IsActive; Sys_SupplierAgent tblSysSupplierAgent = SetToModelObject(objSupplierAgent); _context.Sys_SupplierAgent.Add(tblSysSupplierAgent); } } } } #endregion #region Transaction if (supplieraddress == 1) { _vmMsg.Type = Enums.MessageType.Error; _vmMsg.Msg = "Please Enter only one Active Supplier Address."; } if (localAgent > 1 || foreignAgent > 1) { _vmMsg.Type = Enums.MessageType.Error; _vmMsg.Msg = "Please Enter Same Agent Type Only Once."; } if (supplieraddress == 0 && localAgent == 1 && foreignAgent == 1) { _context.SaveChanges(); tx.Complete(); SupplierCode = model.SupplierCode; _vmMsg.Type = Enums.MessageType.Success; _vmMsg.Msg = "Saved Successfully."; } if (supplieraddress == 0 && localAgent == 0 && foreignAgent == 1) { _context.SaveChanges(); tx.Complete(); SupplierCode = model.SupplierCode; _vmMsg.Type = Enums.MessageType.Success; _vmMsg.Msg = "Saved Successfully."; } if (supplieraddress == 0 && localAgent == 1 && foreignAgent == 0) { _context.SaveChanges(); tx.Complete(); SupplierCode = model.SupplierCode; _vmMsg.Type = Enums.MessageType.Success; _vmMsg.Msg = "Saved Successfully."; } if (supplieraddress == 0 && localAgent == 0 && foreignAgent == 0) { _context.SaveChanges(); tx.Complete(); SupplierCode = model.SupplierCode; _vmMsg.Type = Enums.MessageType.Success; _vmMsg.Msg = "Saved Successfully."; } #endregion } } else { _vmMsg.Type = Enums.MessageType.Error; _vmMsg.Msg = "Please Enter Supplier Address."; } #endregion //} } } } catch { _vmMsg.Type = Enums.MessageType.Error; _vmMsg.Msg = "Failed to save."; } return(_vmMsg); }
public ValidationMsg Update(SysSupplier model, int userid) { _vmMsg = new ValidationMsg(); try { using (var tx = new TransactionScope()) { using (_context) { Sys_Supplier CurrentEntity = SetToModelObject(model); var OriginalEntity = _context.Sys_Supplier.First(m => m.SupplierID == model.SupplierID); OriginalEntity.SupplierID = CurrentEntity.SupplierID; OriginalEntity.SupplierCode = CurrentEntity.SupplierCode; OriginalEntity.SupplierName = CurrentEntity.SupplierName; OriginalEntity.SupplierCategory = CurrentEntity.SupplierCategory; OriginalEntity.SupplierType = CurrentEntity.SupplierType; OriginalEntity.IsActive = CurrentEntity.IsActive; OriginalEntity.ModifiedBy = userid; OriginalEntity.ModifiedOn = DateTime.Now; #region Save Supplier Address if (model.SupplierAddressList != null) { foreach (SysSupplierAddress objsupplierAddress in model.SupplierAddressList) { if (objsupplierAddress.SupplierAddressID == 0) { var exitData = _context.Sys_SupplierAddress.Where(m => m.SupplierID == model.SupplierID && !m.IsDelete && m.IsActive).ToList(); if (exitData.Count > 0) { supplieraddress = 1; } else { objsupplierAddress.SupplierID = model.SupplierID; objsupplierAddress.SetBy = userid; objsupplierAddress.IsActive = string.IsNullOrEmpty(objsupplierAddress.IsActive) ? "Active" : objsupplierAddress.IsActive; Sys_SupplierAddress tblPurchaseYearPeriod = SetToModelObject(objsupplierAddress); _context.Sys_SupplierAddress.Add(tblPurchaseYearPeriod); _context.SaveChanges(); } } else { if (objsupplierAddress.IsActive == "Inactive") { Sys_SupplierAddress CurEntity = SetToModelObject(objsupplierAddress); var OrgEntity = _context.Sys_SupplierAddress.First(m => m.SupplierAddressID == objsupplierAddress.SupplierAddressID); OrgEntity.SupplierAddressID = CurEntity.SupplierAddressID; OrgEntity.SupplierID = CurEntity.SupplierID; OrgEntity.Address = CurEntity.Address; OrgEntity.ContactPerson = CurEntity.ContactPerson; OrgEntity.ContactNumber = CurEntity.ContactNumber; OrgEntity.EmailAddress = CurEntity.EmailAddress; OrgEntity.FaxNo = CurEntity.FaxNo; OrgEntity.PhoneNo = CurEntity.PhoneNo; OrgEntity.SkypeID = CurEntity.SkypeID; OrgEntity.Website = CurEntity.Website; OrgEntity.IsActive = CurEntity.IsActive; OrgEntity.ModifiedBy = userid; OrgEntity.ModifiedOn = DateTime.Now; } else { var exitData = _context.Sys_SupplierAddress.Where(m => m.SupplierID == model.SupplierID && !m.IsDelete && m.IsActive).ToList(); if ((exitData.Count > 0) && (model.SupplierAddressList.Count > 1)) { supplieraddress = 1; } else { Sys_SupplierAddress CurEntity = SetToModelObject(objsupplierAddress); var OrgEntity = _context.Sys_SupplierAddress.First( m => m.SupplierAddressID == objsupplierAddress.SupplierAddressID); OrgEntity.SupplierAddressID = CurEntity.SupplierAddressID; OrgEntity.SupplierID = CurEntity.SupplierID; OrgEntity.Address = CurEntity.Address; OrgEntity.ContactPerson = CurEntity.ContactPerson; OrgEntity.ContactNumber = CurEntity.ContactNumber; OrgEntity.EmailAddress = CurEntity.EmailAddress; OrgEntity.FaxNo = CurEntity.FaxNo; OrgEntity.PhoneNo = CurEntity.PhoneNo; OrgEntity.SkypeID = CurEntity.SkypeID; OrgEntity.Website = CurEntity.Website; OrgEntity.IsActive = CurEntity.IsActive; OrgEntity.ModifiedBy = userid; OrgEntity.ModifiedOn = DateTime.Now; } } } } } #endregion #region Save Supplier Agent if (model.SupplierAgentList != null) { foreach (SysSupplierAgent objSupplierAgent in model.SupplierAgentList) { if (objSupplierAgent.SupplierAgentID == 0) { if (objSupplierAgent.SupplierID != 0) { if (objSupplierAgent.AgentType == "Local Agent") { ++localAgent; var exitData = _context.Sys_SupplierAgent.Where(m => m.SupplierID == model.SupplierID && !m.IsDelete && m.IsActive && m.AgentType == "Local Agent").ToList(); if (exitData.Count > 0) { ++localAgent; } } else if (objSupplierAgent.AgentType == "Foreign Agent") { ++foreignAgent; var exitData = _context.Sys_SupplierAgent.Where(m => m.SupplierID == model.SupplierID && !m.IsDelete && m.IsActive && m.AgentType == "Foreign Agent").ToList(); if (exitData.Count > 0) { ++foreignAgent; } } if (localAgent == 1 || foreignAgent == 1) { objSupplierAgent.SupplierID = model.SupplierID; objSupplierAgent.SetBy = userid; objSupplierAgent.IsActive = string.IsNullOrEmpty(objSupplierAgent.IsActive) ? "Active" : objSupplierAgent.IsActive; Sys_SupplierAgent tblSysSupplierAgent = SetToModelObject(objSupplierAgent); _context.Sys_SupplierAgent.Add(tblSysSupplierAgent); } } } else { if (objSupplierAgent.AgentType == "Local Agent") { ++localAgent; } else if (objSupplierAgent.AgentType == "Foreign Agent") { ++foreignAgent; } if (localAgent == 1 || foreignAgent == 1) { Sys_SupplierAgent CurEntity = SetToModelObject(objSupplierAgent); var OrgEntity = _context.Sys_SupplierAgent.First( m => m.SupplierAgentID == objSupplierAgent.SupplierAgentID); OrgEntity.SupplierAgentID = CurEntity.SupplierAgentID; OrgEntity.AgentID = CurEntity.AgentID; OrgEntity.SupplierID = CurEntity.SupplierID; OrgEntity.AgentType = CurEntity.AgentType; OrgEntity.IsActive = CurEntity.IsActive; OrgEntity.ModifiedBy = userid; OrgEntity.ModifiedOn = DateTime.Now; } } } } #endregion if (supplieraddress == 1) { _vmMsg.Type = Enums.MessageType.Error; _vmMsg.Msg = "Please Enter only one Active Supplier Address."; } if (localAgent > 1 || foreignAgent > 1) { _vmMsg.Type = Enums.MessageType.Error; _vmMsg.Msg = "Please Enter Same Agent Type Only Once."; } if (supplieraddress == 0 && localAgent == 1 && foreignAgent == 1) { _context.SaveChanges(); tx.Complete(); _vmMsg.Type = Enums.MessageType.Update; _vmMsg.Msg = "Updated Successfully."; } if (supplieraddress == 0 && localAgent == 0 && foreignAgent == 1) { _context.SaveChanges(); tx.Complete(); _vmMsg.Type = Enums.MessageType.Update; _vmMsg.Msg = "Updated Successfully."; } if (supplieraddress == 0 && localAgent == 1 && foreignAgent == 0) { _context.SaveChanges(); tx.Complete(); _vmMsg.Type = Enums.MessageType.Update; _vmMsg.Msg = "Updated Successfully."; } if (supplieraddress == 0 && localAgent == 0 && foreignAgent == 0) { _context.SaveChanges(); tx.Complete(); _vmMsg.Type = Enums.MessageType.Update; _vmMsg.Msg = "Updated Successfully."; } } } } catch (Exception ex) { if (ex.InnerException.InnerException.Message.Contains("UNIQUE KEY")) { _vmMsg.Type = Enums.MessageType.Error; _vmMsg.Msg = "HSCode Already Exit.."; } else { _vmMsg.Type = Enums.MessageType.Error; _vmMsg.Msg = "Failed to Update."; } } return(_vmMsg); }