public override bool Reset(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryCreditNote dtoFactoryCreditNote = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryCreditNote>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryCreditNoteMngEntities context = CreateContext()) { FactoryCreditNote dbItem = context.FactoryCreditNote.FirstOrDefault(o => o.FactoryCreditNoteID == id); if (dbItem == null) { throw new Exception("Credit note not found!"); } dbItem.IsConfirmed = null; dbItem.ConfirmedDate = null; dbItem.ConfirmedBy = null; context.SaveChanges(); dtoItem = GetData(userId, dbItem.FactoryCreditNoteID, -1, string.Empty, out notification).Data; return(true); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public void DTO2DB(DTO.FactoryCreditNote dtoItem, ref FactoryCreditNote dbItem) { decimal subtotal = 0; // map fields AutoMapper.Mapper.Map <DTO.FactoryCreditNote, FactoryCreditNote>(dtoItem, dbItem); // map detail if (dtoItem.FactoryCreditNoteDetails != null) { // check for child rows deleted foreach (FactoryCreditNoteDetail dbDetail in dbItem.FactoryCreditNoteDetail.ToArray()) { if (!dtoItem.FactoryCreditNoteDetails.Select(o => o.FactoryCreditNoteDetailID).Contains(dbDetail.FactoryCreditNoteDetailID)) { dbItem.FactoryCreditNoteDetail.Remove(dbDetail); } } // map child rows foreach (DTO.FactoryCreditNoteDetail dtoDetail in dtoItem.FactoryCreditNoteDetails) { FactoryCreditNoteDetail dbDetail; if (dtoDetail.FactoryCreditNoteDetailID <= 0) { dbDetail = new FactoryCreditNoteDetail(); dbItem.FactoryCreditNoteDetail.Add(dbDetail); } else { dbDetail = dbItem.FactoryCreditNoteDetail.FirstOrDefault(o => o.FactoryCreditNoteDetailID == dtoDetail.FactoryCreditNoteDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.FactoryCreditNoteDetail, FactoryCreditNoteDetail>(dtoDetail, dbDetail); if (dtoDetail.CreditAmount.HasValue) { subtotal += Math.Round(dtoDetail.CreditAmount.Value); } } } } dbItem.TotalAmount = subtotal; }
public bool DeleteData(int userId, int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { // check permission if (id > 0 && fwFactory.CheckFactoryCreditNotePermission(userId, id) == 0) { throw new Exception("Current user don't have access permission for the selected credit note data"); } using (FactoryCreditNoteMngEntities context = CreateContext()) { FactoryCreditNote dbItem = context.FactoryCreditNote.FirstOrDefault(o => o.FactoryCreditNoteID == id); if (dbItem == null) { throw new Exception("Credit note not found!"); } // check if invoice already confirmed if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value) { throw new Exception("Can not delete the confirmed credit note!"); } // everything ok, delete the invoice context.FactoryCreditNote.Remove(dbItem); context.SaveChanges(); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } return(true); }
public override bool Approve(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryCreditNote dtoFactoryCreditNote = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryCreditNote>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryCreditNoteMngEntities context = CreateContext()) { FactoryCreditNote dbItem = context.FactoryCreditNote.FirstOrDefault(o => o.FactoryCreditNoteID == id); if (dbItem == null) { throw new Exception("Credit note not found!"); } // validate before approve if (string.IsNullOrEmpty(dbItem.CreditNoteNo)) { throw new Exception("Credit note number is required!"); } if (!dbItem.IssuedDate.HasValue) { throw new Exception("Credit note date is required!"); } dbItem.IsConfirmed = true; dbItem.ConfirmedDate = DateTime.Now; dbItem.ConfirmedBy = userId; context.SaveChanges(); dtoItem = GetData(userId, dbItem.FactoryCreditNoteID, -1, string.Empty, out notification).Data; return(true); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryCreditNote dtoFactoryCreditNote = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryCreditNote>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; int number; string indexName; try { // check permission if (fwFactory.CheckSupplierPermission(userId, dtoFactoryCreditNote.SupplierID.Value) == 0) { throw new Exception("Current user don't have access permission for the selected supplier data"); } if (id > 0 && fwFactory.CheckFactoryCreditNotePermission(userId, id) == 0) { throw new Exception("Current user don't have access permission for the selected credit note data"); } using (FactoryCreditNoteMngEntities context = CreateContext()) { FactoryCreditNote dbItem = null; if (id == 0) { dbItem = new FactoryCreditNote(); context.FactoryCreditNote.Add(dbItem); } else { dbItem = context.FactoryCreditNote.FirstOrDefault(o => o.FactoryCreditNoteID == id); } if (dbItem == null) { notification.Message = "Factory credit note not found!"; return(false); } else { // check if credit note is confirmed if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value) { throw new Exception("Can not edit the confirmed credit note!"); } // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFactoryCreditNote.ConcurrencyFlag))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } converter.DTO2DB(dtoFactoryCreditNote, ref dbItem); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; // remove orphan context.FactoryCreditNoteDetail.Local.Where(o => o.FactoryCreditNote == null).ToList().ForEach(o => context.FactoryCreditNoteDetail.Remove(o)); context.SaveChanges(); } dtoItem = GetData(userId, dbItem.FactoryCreditNoteID, -1, string.Empty, out notification).Data; return(true); } } catch (System.Data.DataException dEx) { notification.Type = Library.DTO.NotificationType.Error; Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName); if (number == 2601 && !string.IsNullOrEmpty(indexName)) { switch (indexName) { case "CreditNoteNoUnique": notification.Message = "Duplicate credit note number (credit note number must be unique)!"; break; default: notification.Message = dEx.Message; break; } } else { notification.Message = dEx.Message; } return(false); } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }