public bool CheckAuthentication(int id, int userId, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; bool result = false; try { using (FactoryQuotationMngEntities context = CreateContext()) { Quotation dbItem = context.Quotation.FirstOrDefault(o => o.QuotationID == id); if (dbItem != null && dbItem.FactoryID != null) { foreach (Support.DTO.Factory factory in supportFactory.GetFactory(userId)) { if (factory.FactoryID == dbItem.FactoryID.Value) { result = true; break; } } } else { throw new Exception("Quotation does not exists"); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; } return(result); }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { DTO.Quotation dtoQuotation = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.Quotation>(); // check permission if (id > 0 && fwFactory.CheckQuotationPermission(userId, id) == 0) { throw new Exception("Current user don't have access permission for the selected quotation data"); } using (FactoryQuotationMngEntities context = CreateContext()) { Quotation dbItem = context.Quotation.FirstOrDefault(o => o.QuotationID == id); if (dbItem == null) { throw new Exception("Quotation does not exists!"); } using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM QuotationOffer WITH (TABLOCKX, HOLDLOCK); SELECT * FROM QuotationDetail WITH (TABLOCKX, HOLDLOCK);"); try { int?maxVersion = context.QuotationOffer.Where(o => o.QuotationID == id).Max(o => o.QuotationOfferVersion); QuotationOffer dbOfferItem = new QuotationOffer(); dbOfferItem.QuotationID = id; dbOfferItem.QuotationOfferDirectionID = 1; // factory -> furnindo dbOfferItem.UpdatedBy = userId; dbOfferItem.UpdatedDate = DateTime.Now; dbOfferItem.QuotationOfferDate = DateTime.Now; if (maxVersion.HasValue) { dbOfferItem.QuotationOfferVersion = maxVersion.Value + 1; } else { dbOfferItem.QuotationOfferVersion = 1; } bool isOK = false; foreach (DTO.QuotationDetail detail in dtoQuotation.QuotationDetails.Where(o => o.NewOfferPrice.HasValue)) { if (detail.FactoryOrderDetailID.HasValue) { if (fwFactory.CheckFactoryOrderDetailPermission(userId, detail.FactoryOrderDetailID.Value) > 0) { isOK = true; } } if (detail.FactoryOrderSparepartDetailID.HasValue) { if (fwFactory.CheckFactoryOrderSparepartDetailPermission(userId, detail.FactoryOrderSparepartDetailID.Value) > 0) { isOK = true; } } if (!isOK) { throw new Exception("Invalid permission!"); } // update quotation detail purchasing price QuotationDetail dbDetail = context.QuotationDetail.FirstOrDefault(o => o.QuotationDetailID == detail.QuotationDetailID); if (dbDetail != null && Decimal.TryParse(detail.NewOfferPrice.Value.ToString(), out decimal tmpDecimal)) { QuotationOfferDetail dbOfferDetailItem = new QuotationOfferDetail(); dbOfferDetailItem.QuotationOffer = dbOfferItem; dbOfferDetailItem.QuotationDetailID = detail.QuotationDetailID; dbOfferDetailItem.Price = tmpDecimal; dbOfferItem.QuotationOfferDetail.Add(dbOfferDetailItem); dbDetail.StatusID = 1; dbDetail.PurchasingPrice = detail.NewOfferPrice.Value; dbDetail.SalePrice = detail.NewOfferPrice.Value + (detail.NewOfferPrice.Value * dbDetail.PriceDifferenceRate); dbDetail.PriceUpdatedDate = DateTime.Now; dbDetail.PriceUpdatedBy = userId; } } context.QuotationOffer.Add(dbOfferItem); context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } dtoItem = GetData(userId, id, out notification).Data; return(true); } } catch (Newtonsoft.Json.JsonReaderException jex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = "Data input validation failed !"; return(false); } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }