public override bool DeleteData(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (AVFPurchasingInvoiceMngEntities context = CreateContext()) { AVFPurchasingInvoice dbItem = context.AVFPurchasingInvoice.FirstOrDefault(o => o.AVFPurchasingInvoiceID == id); if (dbItem == null) { notification.Message = "Invoice not found!"; return(false); } else { foreach (AVFPurchasingInvoiceDetail detail in dbItem.AVFPurchasingInvoiceDetail.ToArray()) { context.AVFPurchasingInvoiceDetail.Remove(detail); } context.AVFPurchasingInvoice.Remove(dbItem); context.SaveChanges(); 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 override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.AVFPurchasingInvoice dtoAVFPurchasingInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.AVFPurchasingInvoice>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (AVFPurchasingInvoiceMngEntities context = CreateContext()) { AVFPurchasingInvoice dbItem = null; if (id == 0) { dbItem = new AVFPurchasingInvoice(); context.AVFPurchasingInvoice.Add(dbItem); } else { dbItem = context.AVFPurchasingInvoice.FirstOrDefault(o => o.AVFPurchasingInvoiceID == id); } if (dbItem == null) { notification.Message = "Invoice not found!"; return(false); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoAVFPurchasingInvoice.ConcurrencyFlag_String))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } //convert dto to db converter.DTO2BD(dtoAVFPurchasingInvoice, ref dbItem); //remove orphan item context.AVFPurchasingInvoiceDetail.Local.Where(o => o.AVFPurchasingInvoice == null).ToList().ForEach(o => context.AVFPurchasingInvoiceDetail.Remove(o)); // processing file if (dtoAVFPurchasingInvoice.PDFFileScan_HasChange) { dbItem.PDFFileScan = fwFactory.CreateNoneImageFilePointer(this._tempFolder, dtoAVFPurchasingInvoice.PDFFileScan_NewFile, dtoAVFPurchasingInvoice.PDFFileScan); } dbItem.UpdatedDate = DateTime.Now; dbItem.UpdatedBy = userId; context.SaveChanges(); dtoItem = GetData(dbItem.AVFPurchasingInvoiceID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }