public bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.DraftCommercialInvoiceDTO dtoDraftCommercialInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.DraftCommercialInvoiceDTO>(); try { ////get companyID //Module.Framework.DAL.DataFactory fw_factory = new Framework.DAL.DataFactory(); //int? companyID = fw_factory.GetCompanyID(userId); using (var context = CreateContext()) { DraftCommercialInvoice dbItem = null; if (id == 0) { dbItem = new DraftCommercialInvoice(); context.DraftCommercialInvoice.Add(dbItem); } else { dbItem = context.DraftCommercialInvoice.Where(o => o.DraftCommercialInvoiceID == id).FirstOrDefault(); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { if (id == 0) { string Season = dtoDraftCommercialInvoice.Season; string Season_Year = Season.Substring(0, 4); string invoiceNoTemplate = Season_Year.Substring(2, 2) + "42"; var invoices = context.DraftCommercialInvoice.Where(o => o.Season == Season && o.InvoiceNo.Substring(0, 4) == Season_Year).OrderByDescending(o => o.InvoiceNo); if (invoices.ToList().Count() == 0) { dtoDraftCommercialInvoice.InvoiceNo = Season_Year + "0001"; } else { var x = invoices.FirstOrDefault(); int iNo = 0; iNo = Convert.ToInt32(x.InvoiceNo.Substring(4, 4)) + 1; dtoDraftCommercialInvoice.InvoiceNo = Season_Year + iNo.ToString().PadLeft(4, '0'); } } // //convert dto to db converter.DTO2DB_DraftCommercialInvoice(dtoDraftCommercialInvoice, ref dbItem); if (id == 0) { dbItem.CreatedBy = userId; dbItem.CreatedDate = DateTime.Now; } else { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; } //save data context.SaveChanges(); //get return data dtoItem = GetData(dbItem.DraftCommercialInvoiceID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(false); } }
public void DTO2DB_DraftCommercialInvoice(DTO.DraftCommercialInvoiceDTO dtoItem, ref DraftCommercialInvoice dbItem) { if (dtoItem.DraftCommercialInvoiceDetails != null) { //delete item in db that exist in dto but not exist in db foreach (var item in dbItem.DraftCommercialInvoiceDetail.ToArray()) { if (!dtoItem.DraftCommercialInvoiceDetails.Select(s => s.DraftCommercialInvoiceDetailID).Contains(item.DraftCommercialInvoiceDetailID)) { //delete Detail Descripton foreach (var wItem in item.DraftCommercialInvoiceDetailDescription.ToArray()) { item.DraftCommercialInvoiceDetailDescription.Remove(wItem); } //delete detail dbItem.DraftCommercialInvoiceDetail.Remove(item); } else { var x = dtoItem.DraftCommercialInvoiceDetails.Where(o => o.DraftCommercialInvoiceDetailID == item.DraftCommercialInvoiceDetailID).FirstOrDefault().DraftCommercialInvoiceDetailDescriptions; //delete detail Descripton foreach (var wItem in item.DraftCommercialInvoiceDetailDescription.ToArray()) { if (!x.Select(s => s.DraftCommercialInvoiceDetailDescriptionID).Contains(wItem.DraftCommercialInvoiceDetailDescriptionID)) { item.DraftCommercialInvoiceDetailDescription.Remove(wItem); } } } } //read from dto to db DraftCommercialInvoiceDetail dbDraftCommercialInvoiceDetail; DraftCommercialInvoiceDetailDescription dbDraftCommercialInvoiceDetailDescription; foreach (var item in dtoItem.DraftCommercialInvoiceDetails) { if (item.DraftCommercialInvoiceDetailID > 0) { dbDraftCommercialInvoiceDetail = dbItem.DraftCommercialInvoiceDetail.Where(o => o.DraftCommercialInvoiceDetailID == item.DraftCommercialInvoiceDetailID).FirstOrDefault(); //read from purchase order workorder detail dto to db if (dbDraftCommercialInvoiceDetail != null && item.DraftCommercialInvoiceDetailDescriptions != null) { foreach (var wItem in item.DraftCommercialInvoiceDetailDescriptions) { if (wItem.DraftCommercialInvoiceDetailDescriptionID > 0) { dbDraftCommercialInvoiceDetailDescription = dbDraftCommercialInvoiceDetail.DraftCommercialInvoiceDetailDescription.Where(o => o.DraftCommercialInvoiceDetailDescriptionID == wItem.DraftCommercialInvoiceDetailDescriptionID).FirstOrDefault(); } else { dbDraftCommercialInvoiceDetailDescription = new DraftCommercialInvoiceDetailDescription(); dbDraftCommercialInvoiceDetail.DraftCommercialInvoiceDetailDescription.Add(dbDraftCommercialInvoiceDetailDescription); } if (dbDraftCommercialInvoiceDetailDescription != null) { AutoMapper.Mapper.Map <DTO.DraftCommercialInvoiceDetailDescriptionDTO, DraftCommercialInvoiceDetailDescription>(wItem, dbDraftCommercialInvoiceDetailDescription); } } } } else { dbDraftCommercialInvoiceDetail = new DraftCommercialInvoiceDetail(); dbItem.DraftCommercialInvoiceDetail.Add(dbDraftCommercialInvoiceDetail); //read from purchase order workorder detail dto to db if (item.DraftCommercialInvoiceDetailDescriptions != null) { foreach (var mItem in item.DraftCommercialInvoiceDetailDescriptions) { dbDraftCommercialInvoiceDetailDescription = new DraftCommercialInvoiceDetailDescription(); dbDraftCommercialInvoiceDetail.DraftCommercialInvoiceDetailDescription.Add(dbDraftCommercialInvoiceDetailDescription); AutoMapper.Mapper.Map <DTO.DraftCommercialInvoiceDetailDescriptionDTO, DraftCommercialInvoiceDetailDescription>(mItem, dbDraftCommercialInvoiceDetailDescription); } } } //read purchase request detail dto to db if (dbDraftCommercialInvoiceDetail != null) { AutoMapper.Mapper.Map <DTO.DraftCommercialInvoiceDetailDTO, DraftCommercialInvoiceDetail>(item, dbDraftCommercialInvoiceDetail); } } } //Description if (dtoItem.DraftCommercialInvoiceDescriptions != null) { foreach (DraftCommercialInvoiceDescription item in dbItem.DraftCommercialInvoiceDescription.ToList()) { if (!dtoItem.DraftCommercialInvoiceDescriptions.Select(s => s.DraftCommercialInvoiceDescriptionID).Contains(item.DraftCommercialInvoiceDescriptionID)) { dbItem.DraftCommercialInvoiceDescription.Remove(item); } } foreach (DTO.DraftCommercialInvoiceDescriptionDTO dto in dtoItem.DraftCommercialInvoiceDescriptions) { DraftCommercialInvoiceDescription item; if (dto.DraftCommercialInvoiceDescriptionID < 0) { item = new DraftCommercialInvoiceDescription(); dbItem.DraftCommercialInvoiceDescription.Add(item); } else { item = dbItem.DraftCommercialInvoiceDescription.FirstOrDefault(s => s.DraftCommercialInvoiceDescriptionID == dto.DraftCommercialInvoiceDescriptionID); } if (item != null) { AutoMapper.Mapper.Map <DTO.DraftCommercialInvoiceDescriptionDTO, DraftCommercialInvoiceDescription>(dto, item); } } } AutoMapper.Mapper.Map <DTO.DraftCommercialInvoiceDTO, DraftCommercialInvoice>(dtoItem, dbItem); dbItem.UpdatedDate = dtoItem.UpdatedDate.ConvertStringToDateTime(); dbItem.CreatedDate = dtoItem.CreatedDate.ConvertStringToDateTime(); dbItem.ConfirmedDate = dtoItem.ConfirmedDate.ConvertStringToDateTime(); dbItem.InvoiceDate = dtoItem.InvoiceDate.ConvertStringToDateTime(); }