public IActionResult Update(SupplierViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } /**************** Updating Suppliers **************/ var single = _context.Suppliers.Single(x => x.SupplierId == model.SupplierId); single.SupplierName = model.SupplierName; single.Address = model.Address; single.Landline = model.Landline; single.Mobile = model.Mobile; single.OpeningBalance = model.OpeningBalance; single.CurrentBalance = model.CurrentBalance; var flag = _context.SaveChanges(); var supplierId = single.SupplierId; /**************** Updating Suppliers Attachments and Saving **************/ DeleteFiles(model.SupplierId); Miscellaneous.CreateFolder(_hostingEnvironment.WebRootPath + @"\files"); IList <SupplierAttachmentViewModel> supplierAttachmentList = model.SupplierAttachmentList; foreach (var item in supplierAttachmentList) { string randName = DateTime.Now.ToString("MMddyyyyhhmmssfff") + "_" + new Random().Next(0, 10000).ToString("D6") + "_" + item.AttachmentName; string base64 = item.AttachmentFile.Split(",")[1]; byte[] data = Convert.FromBase64String(base64); string path = _hostingEnvironment.WebRootPath + @"\files\" + randName; System.IO.File.WriteAllBytes(path, data.ToArray()); SupplierAttachment supplierAttachment = new SupplierAttachment() { AttachmentName = item.AttachmentName, AttachmentPath = randName, SupplierId = supplierId, MimeType = item.AttachmentFile.Split(",")[0] }; _context.SupplierAttachments.Add(supplierAttachment); _context.SaveChanges(); } return(Ok(true)); }
public IActionResult Create(SupplierViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } /**************** Saving Suppliers **************/ Supplier supplier = _mapper.Map <Supplier>(model); supplier.CurrentStatus = 1; supplier.CreatedDate = DateTime.Now; _context.Suppliers.Add(supplier); var flag = _context.SaveChanges(); var supplierId = supplier.SupplierId; if (flag > 0) { /**************** Creating Supplier Attachments and Saving **************/ Miscellaneous.CreateFolder(_hostingEnvironment.WebRootPath + @"\files"); IList <SupplierAttachmentViewModel> supplierAttachmentList = model.SupplierAttachmentList; foreach (var item in supplierAttachmentList) { string randName = DateTime.Now.ToString("MMddyyyyhhmmssfff") + "_" + new Random().Next(0, 10000).ToString("D6") + "_" + item.AttachmentName; string base64 = item.AttachmentFile.Substring(item.AttachmentFile.IndexOf(',') + 1); byte[] data = Convert.FromBase64String(base64); string path = _hostingEnvironment.WebRootPath + @"\files\" + randName; System.IO.File.WriteAllBytes(path, data.ToArray()); SupplierAttachment supplierAttachment = new SupplierAttachment() { AttachmentName = item.AttachmentName, AttachmentPath = randName, SupplierId = supplierId, MimeType = item.AttachmentFile.Split(",")[0] }; _context.SupplierAttachments.Add(supplierAttachment); _context.SaveChanges(); } return(Ok(true)); } return(Ok(false)); }
public SupplierInfo GetSupplierInfo(string suppliercode) { SupplierInfo objSupplierInfo = new SupplierInfo(); List <SupplierAttachment> objSupplierAttachment = new List <SupplierAttachment>(); List <SupplierBusiness> objSupplierBusiness = new List <SupplierBusiness>(); List <SupplierContact> objSupplierContact = new List <SupplierContact>(); string vSupplierText = @"SELECT [SupplierCode] ,[SupplierID] ,[SupplierName] ,[Email] ,[MobileNumber] ,[EnlistmentDate] ,[SupplierAddress] ,[AlternateEmail] ,[ZipCode] ,[Fax] ,[IsApproved] ,[ApprovalAction] FROM [LSP_PMS_SupplierInfo] where SupplierCode = '" + suppliercode + "'"; string vAttachmentText = @"SELECT [AttachmentCode] ,[SupplierCode] ,[AttachmentName] ,[CertificateNumber] ,[IssueDate] ,[ExpiryDate] ,[FileLocationPath] ,[Remarks] FROM [LSP_PMS_SupplierDocuments] where SupplierCode = '" + suppliercode + "'"; string vContactText = @"SELECT [ContactPersonCode] ,[SupplierCode] ,[ContactName] ,[JobRole] ,[Email] ,[MobileNumber] ,[Designation] ,[TelephoneNumber] FROM [LSP_PMS_SupplierContact] where SupplierCode = '" + suppliercode + "'"; string vBusinessText = @"SELECT [SupplierCode] ,[BusinessTypeCode] ,filename FROM [LSP_PMS_SupplierBusiness] b join general_codefile f on b.[BusinessTypeCode]=f.filecode where SupplierCode = '" + suppliercode + "'"; SqlConnection connection = _supplierDbContext.GetConn(); connection.Open(); SqlCommand objDbCommand = new SqlCommand(vSupplierText, connection); using (SqlDataReader dr = objDbCommand.ExecuteReader()) { if (dr.Read()) { objSupplierInfo = new SupplierInfo(); objSupplierInfo.SupplierCode_PK = dr["SupplierCode"].ToString(); objSupplierInfo.SupplierID = dr["SupplierID"].ToString(); objSupplierInfo.SupplierName = dr["SupplierName"].ToString(); objSupplierInfo.Email = dr["Email"].ToString(); objSupplierInfo.MobileNumber = dr["MobileNumber"].ToString(); objSupplierInfo.EnlistmentDate = dr.GetDateTime(dr.GetOrdinal("EnlistmentDate")).ToString("dd-MM-yyyy"); objSupplierInfo.SupplierAddress = dr["SupplierAddress"].ToString(); objSupplierInfo.AlternateEmail = dr["AlternateEmail"].ToString(); objSupplierInfo.ZipCode = dr["ZipCode"].ToString(); objSupplierInfo.Fax = dr["Fax"].ToString(); objSupplierInfo.IsApproved = Convert.ToInt16(dr["IsApproved"].ToString()); objSupplierInfo.ApprovalAction = Convert.ToInt16(dr["ApprovalAction"].ToString()); } } SqlCommand objDbCommandAttachment = new SqlCommand(vAttachmentText, connection); using (SqlDataReader dr = objDbCommandAttachment.ExecuteReader()) { while (dr.Read()) { SupplierAttachment supplierAttachment = new SupplierAttachment(); supplierAttachment.AttachmentCode_PK = dr["AttachmentCode"].ToString(); supplierAttachment.SupplierCode_FK = dr["SupplierCode"].ToString(); supplierAttachment.AttachmentName = dr["AttachmentName"].ToString(); supplierAttachment.CertificateNumber = dr["CertificateNumber"].ToString(); if (!string.IsNullOrEmpty(dr["IssueDate"].ToString())) { supplierAttachment.IssueDate = dr.GetDateTime(dr.GetOrdinal("IssueDate")).ToString("dd-MM-yyyy"); } if (!string.IsNullOrEmpty(dr["ExpiryDate"].ToString())) { supplierAttachment.ExpiryDate = dr.GetDateTime(dr.GetOrdinal("ExpiryDate")).ToString("dd-MM-yyyy"); } supplierAttachment.FileLocationPath = dr["FileLocationPath"].ToString(); supplierAttachment.Remarks = dr["Remarks"].ToString(); objSupplierAttachment.Add(supplierAttachment); } } SqlCommand objDbCommandBusiness = new SqlCommand(vBusinessText, connection); using (SqlDataReader dr = objDbCommandBusiness.ExecuteReader()) { while (dr.Read()) { SupplierBusiness supplierBusiness = new SupplierBusiness(); supplierBusiness.SupplierCode_FK = dr["SupplierCode"].ToString(); supplierBusiness.BusinessTypeCode_PK = Convert.ToInt16(dr["BusinessTypeCode"].ToString()); supplierBusiness.filename_VW = dr["filename"].ToString(); objSupplierBusiness.Add(supplierBusiness); } } SqlCommand objDbCommandContact = new SqlCommand(vContactText, connection); using (SqlDataReader dr = objDbCommandContact.ExecuteReader()) { while (dr.Read()) { SupplierContact supplierContact = new SupplierContact(); supplierContact.SupplierCode_FK = dr["SupplierCode"].ToString(); supplierContact.ContactPersonCode_PK = dr["ContactPersonCode"].ToString(); supplierContact.ContactName = dr["ContactName"].ToString(); supplierContact.JobRole = dr["JobRole"].ToString(); supplierContact.Email = dr["Email"].ToString(); supplierContact.MobileNumber = dr["MobileNumber"].ToString(); supplierContact.Designation = dr["Designation"].ToString(); supplierContact.TelephoneNumber = dr["TelephoneNumber"].ToString(); objSupplierContact.Add(supplierContact); } } objSupplierInfo.SupplierAttachmentList_VW = objSupplierAttachment; objSupplierInfo.SupplierBusinessList_VW = objSupplierBusiness; objSupplierInfo.SupplierContactList_VW = objSupplierContact; return(objSupplierInfo); }