public DTO.PurchasingCreditNote GetPurchasingCreditNote(int userId, int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { //check permission on booking if (fwFactory.CheckPurchasingCreditNotePermission(userId, id) == 0) { throw new Exception("You do not have access permission on this invoice"); } using (PurchasingCreditNoteEntities context = CreateContext()) { DTO.PurchasingCreditNote dtoItem = new DTO.PurchasingCreditNote(); var dbItem = context.PurchasingCreditNoteMng_PurchasingCreditNote_View .Include("PurchasingCreditNoteMng_PurchasingCreditNoteDetail_View") .Include("PurchasingCreditNoteMng_PurchasingCreditNoteSparepartDetail_View") .Include("PurchasingCreditNoteMng_PurchasingCreditNoteExtendDetail_View") .FirstOrDefault(o => o.PurchasingCreditNoteID == id); dtoItem = converter.DB2DTO_PurchasingCreditNote(dbItem); return(dtoItem); } } 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(new DTO.PurchasingCreditNote()); } }
public void DTO2DB_PurchasingCreditNote(DTO.PurchasingCreditNote dtoItem, ref PurchasingCreditNote dbItem) { if (dtoItem.PurchasingCreditNoteDetails != null) { List <PurchasingCreditNoteDetail> product_tobedeleted = new List <PurchasingCreditNoteDetail>(); foreach (var item in dbItem.PurchasingCreditNoteDetail.Where(o => !dtoItem.PurchasingCreditNoteDetails.Select(s => s.PurchasingCreditNoteDetailID).Contains(o.PurchasingCreditNoteDetailID))) { product_tobedeleted.Add(item); } foreach (var item in product_tobedeleted) { dbItem.PurchasingCreditNoteDetail.Remove(item); } foreach (var item in dtoItem.PurchasingCreditNoteDetails) { PurchasingCreditNoteDetail dbDetail; if (item.PurchasingCreditNoteDetailID < 0) { dbDetail = new PurchasingCreditNoteDetail(); dbItem.PurchasingCreditNoteDetail.Add(dbDetail); } else { dbDetail = dbItem.PurchasingCreditNoteDetail.FirstOrDefault(o => o.PurchasingCreditNoteDetailID == item.PurchasingCreditNoteDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.PurchasingCreditNoteDetail, PurchasingCreditNoteDetail>(item, dbDetail); } } } if (dtoItem.PurchasingCreditNoteSparepartDetails != null) { List <PurchasingCreditNoteSparepartDetail> product_tobedeleted = new List <PurchasingCreditNoteSparepartDetail>(); foreach (var item in dbItem.PurchasingCreditNoteSparepartDetail.Where(o => !dtoItem.PurchasingCreditNoteSparepartDetails.Select(s => s.PurchasingCreditNoteSparepartDetailID).Contains(o.PurchasingCreditNoteSparepartDetailID))) { product_tobedeleted.Add(item); } foreach (var item in product_tobedeleted) { dbItem.PurchasingCreditNoteSparepartDetail.Remove(item); } foreach (var item in dtoItem.PurchasingCreditNoteSparepartDetails) { PurchasingCreditNoteSparepartDetail dbDetail; if (item.PurchasingCreditNoteSparepartDetailID < 0) { dbDetail = new PurchasingCreditNoteSparepartDetail(); dbItem.PurchasingCreditNoteSparepartDetail.Add(dbDetail); } else { dbDetail = dbItem.PurchasingCreditNoteSparepartDetail.FirstOrDefault(o => o.PurchasingCreditNoteSparepartDetailID == item.PurchasingCreditNoteSparepartDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.PurchasingCreditNoteSparepartDetail, PurchasingCreditNoteSparepartDetail>(item, dbDetail); } } } if (dtoItem.PurchasingCreditNoteExtendDetails != null) { List <PurchasingCreditNoteExtendDetail> product_tobedeleted = new List <PurchasingCreditNoteExtendDetail>(); foreach (var item in dbItem.PurchasingCreditNoteExtendDetail.Where(o => !dtoItem.PurchasingCreditNoteExtendDetails.Select(s => s.PurchasingCreditNoteExtendDetailID).Contains(o.PurchasingCreditNoteExtendDetailID))) { product_tobedeleted.Add(item); } foreach (var item in product_tobedeleted) { dbItem.PurchasingCreditNoteExtendDetail.Remove(item); } foreach (var item in dtoItem.PurchasingCreditNoteExtendDetails) { PurchasingCreditNoteExtendDetail dbDetail; if (item.PurchasingCreditNoteExtendDetailID < 0) { dbDetail = new PurchasingCreditNoteExtendDetail(); dbItem.PurchasingCreditNoteExtendDetail.Add(dbDetail); } else { dbDetail = dbItem.PurchasingCreditNoteExtendDetail.FirstOrDefault(o => o.PurchasingCreditNoteExtendDetailID == item.PurchasingCreditNoteExtendDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.PurchasingCreditNoteExtendDetail, PurchasingCreditNoteExtendDetail>(item, dbDetail); } } } AutoMapper.Mapper.Map <DTO.PurchasingCreditNote, PurchasingCreditNote>(dtoItem, dbItem); if (dtoItem.PurchasingCreditNoteID > 0) { dbItem.UpdatedDate = DateTime.Now; dbItem.UpdatedBy = dtoItem.UpdatedBy; } else { dbItem.CreatedDate = DateTime.Now; dbItem.CreatedBy = dtoItem.UpdatedBy; } dbItem.CreditNoteDate = dtoItem.CreditNoteDate.ConvertStringToDateTime(); }
public bool UpdatePurchasingCreditNote(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.PurchasingCreditNote dtoCreditNote = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PurchasingCreditNote>(); dtoCreditNote.UpdatedBy = userId; try { using (PurchasingCreditNoteEntities context = CreateContext()) { PurchasingCreditNote dbItem; if (id > 0) { //check permission on booking if (fwFactory.CheckPurchasingCreditNotePermission(userId, id) == 0) { throw new Exception("You do not have access permission on this invoice"); } dbItem = context.PurchasingCreditNote.Where(o => o.PurchasingCreditNoteID == id).FirstOrDefault(); //check credit note is confirmed if (dbItem != null && dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed == true) { throw new Exception("Credit note has been confirmed. You can not change."); } } else { //check permission if (dtoCreditNote.PurchasingInvoiceID.HasValue || dtoCreditNote.SupplierID.HasValue) { //check permission on invoice if (dtoCreditNote.PurchasingInvoiceID.HasValue && fwFactory.CheckPurchasingInvoicePermission(userId, dtoCreditNote.PurchasingInvoiceID.Value) == 0) { throw new Exception("You do not have access permission on this invoice"); } if (dtoCreditNote.SupplierID.HasValue && fwFactory.CheckSupplierPermission(userId, dtoCreditNote.SupplierID.Value) == 0) { throw new Exception("You do not have access permission on this factory"); } } else { throw new Exception("There are not invoice or supplier to check permission to create credit note"); } dbItem = new PurchasingCreditNote(); context.PurchasingCreditNote.Add(dbItem); } if (dbItem != null) { //check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoCreditNote.ConcurrencyFlag_String))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } //check put in factory balance ? if (string.IsNullOrEmpty(dtoCreditNote.Season)) { throw new Exception("You need fill-in season to create credit note"); } if (context.PurchasingCreditNoteMng_function_CheckCreditNotePutInBalance(dtoCreditNote.PurchasingInvoiceID, dtoCreditNote.FactoryID, dtoCreditNote.Season).FirstOrDefault().Value > 0) { throw new Exception("This supplier was make balance sheet in season " + dtoCreditNote.Season + ". You can not create credit note"); } //check permission product item List <int?> supplierIDs = fwFactory.GetListSupplierByUser(userId); List <int?> dtoInvoiceDetailIDs = dtoCreditNote.PurchasingCreditNoteDetails.Where(o => o.PurchasingCreditNoteDetailID < 0).Select(s => s.PurchasingInvoiceDetailID).ToList(); List <int?> dtoCreditNoteDetailIDs = dtoCreditNote.PurchasingCreditNoteDetails.Where(o => o.PurchasingCreditNoteDetailID > 0).Select(s => s.PurchasingCreditNoteDetailID).ToList(); if (dtoInvoiceDetailIDs.Count() > 0 && context.PurchasingCreditNoteMng_PurchasingInvoiceDetail_View.Where(o => dtoInvoiceDetailIDs.Contains(o.PurchasingInvoiceDetailID)).Select(s => s.SupplierID).ToList().Except(supplierIDs).Count() > 0) { throw new Exception("You do not have access permission on these products that you added to credit note"); } if (dtoCreditNoteDetailIDs.Count() > 0 && context.PurchasingCreditNoteMng_PurchasingCreditNoteDetail_View.Where(o => dtoCreditNoteDetailIDs.Contains(o.PurchasingCreditNoteDetailID)).Select(s => s.SupplierID).ToList().Except(supplierIDs).Count() > 0) { throw new Exception("You do not have access permission on these products that you added to credit note"); } //check permission sparepart detail List <int?> dtoInvoiceSparepartDetailIDs = dtoCreditNote.PurchasingCreditNoteSparepartDetails.Where(o => o.PurchasingCreditNoteSparepartDetailID < 0).Select(s => s.PurchasingInvoiceSparepartDetailID).ToList(); List <int?> dtoCreditNoteSparepartDetailIDs = dtoCreditNote.PurchasingCreditNoteSparepartDetails.Where(o => o.PurchasingCreditNoteSparepartDetailID > 0).Select(s => s.PurchasingCreditNoteSparepartDetailID).ToList(); if (dtoInvoiceSparepartDetailIDs.Count() > 0 && context.PurchasingCreditNoteMng_PurchasingInvoiceSparepartDetail_View.Where(o => dtoInvoiceSparepartDetailIDs.Contains(o.PurchasingInvoiceSparepartDetailID)).Select(s => s.SupplierID).ToList().Except(supplierIDs).Count() > 0) { throw new Exception("You do not have access permission on these sparepart that you added to credit note"); } if (dtoCreditNoteSparepartDetailIDs.Count() > 0 && context.PurchasingCreditNoteMng_PurchasingCreditNoteSparepartDetail_View.Where(o => dtoCreditNoteSparepartDetailIDs.Contains(o.PurchasingCreditNoteSparepartDetailID)).Select(s => s.SupplierID).ToList().Except(supplierIDs).Count() > 0) { throw new Exception("You do not have access permission on these sparepart that you added to credit note"); } //check quantity foreach (var item in dtoCreditNote.PurchasingCreditNoteDetails.Where(o => o.Quantity.HasValue)) { if (item.UnitPrice.HasValue && item.UnitPrice.Value < 0) { throw new Exception("Price must be not negavitve"); } if (item.Quantity > 0) { throw new Exception("Quantity in credit note must be negative"); } else { int?compareValue = 0; if (item.PurchasingCreditNoteDetailID < 0) { compareValue = item.Quantity; // -2 } else { var objOldValue = context.PurchasingCreditNoteDetail.Where(o => o.PurchasingCreditNoteDetailID == item.PurchasingCreditNoteDetailID).FirstOrDefault(); //-2 compareValue = item.Quantity - objOldValue.Quantity; // -10 - (-2) = -8 } var objRemaining = context.PurchasingCreditNoteMng_PurchasingInvoiceDetail_View.Where(o => o.PurchasingInvoiceDetailID == item.PurchasingInvoiceDetailID); int?remaintQnt = (objRemaining != null && objRemaining.Count() > 0 ? objRemaining.FirstOrDefault().Quantity : 0); if (-compareValue > remaintQnt) { throw new Exception("Credit note quantity must be less than purchasing invoice quantity remaining"); } } } //check quantity foreach (var item in dtoCreditNote.PurchasingCreditNoteSparepartDetails.Where(o => o.Quantity.HasValue)) { if (item.UnitPrice.HasValue && item.UnitPrice.Value < 0) { throw new Exception("Price must be not negavitve"); } if (item.Quantity > 0) { throw new Exception("Quantity in credit note must be negative"); } else { int?compareValue = 0; if (item.PurchasingCreditNoteSparepartDetailID < 0) { compareValue = item.Quantity; // -2 } else { var objOldValue = context.PurchasingCreditNoteSparepartDetail.Where(o => o.PurchasingCreditNoteSparepartDetailID == item.PurchasingCreditNoteSparepartDetailID).FirstOrDefault(); //-2 compareValue = item.Quantity - objOldValue.Quantity; // -10 - (-2) = -8 } var objRemaining = context.PurchasingCreditNoteMng_PurchasingInvoiceSparepartDetail_View.Where(o => o.PurchasingInvoiceSparepartDetailID == item.PurchasingInvoiceSparepartDetailID); int?remaintQnt = (objRemaining != null && objRemaining.Count() > 0 ? objRemaining.FirstOrDefault().Quantity : 0); if (-compareValue > remaintQnt) { throw new Exception("Credit note quantity must be less than purchasing invoice quantity remaining"); } } } foreach (var item in dtoCreditNote.PurchasingCreditNoteExtendDetails.Where(o => o.Amount.HasValue)) { if (item.Amount > 0) { throw new Exception("Amount in credit note must be negative"); } } converter.DTO2DB_PurchasingCreditNote(dtoCreditNote, ref dbItem); //remove orphan item context.PurchasingCreditNoteDetail.Local.Where(o => o.PurchasingCreditNote == null).ToList().ForEach(o => context.PurchasingCreditNoteDetail.Remove(o)); context.PurchasingCreditNoteSparepartDetail.Local.Where(o => o.PurchasingCreditNote == null).ToList().ForEach(o => context.PurchasingCreditNoteSparepartDetail.Remove(o)); context.PurchasingCreditNoteExtendDetail.Local.Where(o => o.PurchasingCreditNote == null).ToList().ForEach(o => context.PurchasingCreditNoteExtendDetail.Remove(o)); context.SaveChanges(); dtoItem = GetPurchasingCreditNote(userId, dbItem.PurchasingCreditNoteID, out notification); } else { throw new Exception(Library.Helper.TEXT_UPDATED_UNSUCCESS_NOTFOUND); } 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); } }