public bool UnConfirmData(int userID, int id, ref object dtoItem, out Library.DTO.Notification notification) { notification = new Notification() { Type = NotificationType.Success }; int quotationDetailID = id; try { using (var context = CreateContext()) { QuotationDetail dbItem = context.QuotationDetail.FirstOrDefault(o => o.QuotationDetailID == quotationDetailID); if (dbItem == null) { notification.Type = NotificationType.Error; notification.Message = "Data is not found!"; return(false); } dbItem.StatusID = 1; // Pending dbItem.StatusUpdatedBy = userID; dbItem.StatusUpdatedDate = DateTime.Now; context.SaveChanges(); dtoItem = AutoMapper.Mapper.Map <PriceQuotationMng_PriceQuotationSearchResult_View, DTO.PriceQuotationSearchResultData>(context.PriceQuotationMng_PriceQuotationSearchResult_View.FirstOrDefault(o => o.QuotationDetailID == quotationDetailID)); return(true); } } catch (Exception ex) { notification.Type = NotificationType.Error; notification.Message = ex.Message; return(false); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { EditOfferQuotationData data = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <EditOfferQuotationData>(); notification = new Notification() { Type = NotificationType.Success }; try { using (var context = CreateContext()) { //bool isCheckFactory = CheckIsFactory(context, userId); bool isCheckFactory = true; // Case change price difference at pop-up quotation in factory var dbQuotation = context.Quotation.FirstOrDefault(o => o.QuotationID == data.QuotationID); var priceDifference = context.PriceDifference.FirstOrDefault(o => o.PriceDifferenceUD == data.PriceDifferenceCode && o.Season == dbQuotation.Season); if (priceDifference == null) { throw new Exception("Quality code is missing!"); } data.PriceDifferenceRate = priceDifference.Rate; if (data.Price != null) { // QuotationOffer. QuotationOffer quotationOffer = new QuotationOffer { QuotationID = data.QuotationID, QuotationOfferDate = DateTime.Now, QuotationOfferVersion = context.QuotationOffer.Where(o => o.QuotationID == data.QuotationID).ToList().Count + 1, QuotationOfferDirectionID = (!isCheckFactory) ? 2 : 1, UpdatedBy = userId, UpdatedDate = DateTime.Now }; context.QuotationOffer.Add(quotationOffer); // QuotationOfferDetail QuotationOfferDetail quotationOfferDetail = new QuotationOfferDetail() { QuotationOfferID = quotationOffer.QuotationOfferID, QuotationDetailID = data.QuotationDetailID, Price = data.Price, Remark = data.Remark }; context.QuotationOfferDetail.Add(quotationOfferDetail); } //QuotationDetail QuotationDetail quotationDetail = context.QuotationDetail.FirstOrDefault(o => o.QuotationDetailID == data.QuotationDetailID); if (data.Price != null) { if (isCheckFactory) { quotationDetail.PurchasingPrice = data.Price; quotationDetail.SalePrice = data.Price * (1 + data.PriceDifferenceRate); if (data.Price == quotationDetail.TargetPrice) // Compare with target price { quotationDetail.StatusID = 3; // 3: Confirm quotationDetail.StatusUpdatedBy = userId; quotationDetail.StatusUpdatedDate = DateTime.Now; } } else { quotationDetail.TargetPrice = data.Price /*/ (1 + data.PriceDifferenceRate)*/; if (quotationDetail.SalePrice != data.SalePrice) { quotationDetail.SalePrice = data.SalePrice; // Update Quotation Offer Detail Last QuotationOfferDetail lastQuotationOfferDetail = context.QuotationOfferDetail.Where(o => o.QuotationDetailID == data.QuotationDetailID).OrderByDescending(o => o.QuotationOfferDetailID).FirstOrDefault(); lastQuotationOfferDetail.Price = data.SalePrice; } if (quotationDetail.TargetPrice == quotationDetail.SalePrice) // Compare with sale price { quotationDetail.StatusID = 3; // 3: Confirm quotationDetail.StatusUpdatedBy = userId; quotationDetail.StatusUpdatedDate = DateTime.Now; } } quotationDetail.PriceUpdatedBy = userId; quotationDetail.PriceUpdatedDate = DateTime.Now; } quotationDetail.Remark = data.Remark; if (!isCheckFactory) { quotationDetail.PriceDifferenceCode = data.PriceDifferenceCode; quotationDetail.PriceDifferenceRate = data.PriceDifferenceRate; quotationDetail.OldPrice1 = data.OldPrice1; quotationDetail.OldPrice2 = data.OldPrice2; quotationDetail.OldPrice3 = data.OldPrice3; quotationDetail.OldPriceRemark = data.OldPriceRemark; } context.SaveChanges(); dtoItem = AutoMapper.Mapper.Map <PriceQuotationMng_PriceQuotationSearchResult_View, DTO.PriceQuotationSearchResultData>(context.PriceQuotationMng_PriceQuotationSearchResult_View.FirstOrDefault(o => o.QuotationDetailID == data.QuotationDetailID)); return(true); } } catch (Exception ex) { notification.Type = NotificationType.Error; notification.Message = ex.Message; return(false); } }
public List <PriceQuotationSearchResultData> UpdateData(int userId, object dtoItems, out Notification notification) { string keyIds = ""; notification = new Notification() { Type = NotificationType.Success }; List <PriceQuotationSearchResultData> dataItems = ((Newtonsoft.Json.Linq.JArray)dtoItems).ToObject <List <PriceQuotationSearchResultData> >(); using (var context = CreateContext()) { //bool isFactory = CheckIsFactory(context, userId); bool isFactory = true; using (var trans = context.Database.BeginTransaction()) { try { for (int i = 0; i < dataItems.Count; i++) { var dataItem = dataItems[i]; if (dataItem.QuotationStatusID.Value == 3) { // skip processing if price already confirmed continue; } if (i > 0) { keyIds = keyIds + ","; } // Quotation Offer QuotationOffer offer = new QuotationOffer() { QuotationID = dataItem.QuotationID, QuotationOfferDate = DateTime.Now, QuotationOfferVersion = context.QuotationOffer.Where(o => o.QuotationID == dataItem.QuotationID).ToList().Count + 1, QuotationOfferDirectionID = (!isFactory) ? 2 : 1, UpdatedBy = userId, UpdatedDate = DateTime.Now }; context.QuotationOffer.Add(offer); // Quotation Offer Detail QuotationOfferDetail offerDetail = new QuotationOfferDetail() { QuotationOfferID = offer.QuotationOfferID, QuotationDetailID = dataItem.QuotationDetailID, Price = dataItem.Price }; context.QuotationOfferDetail.Add(offerDetail); // Quotation Detail QuotationDetail quotationDetail = context.QuotationDetail.FirstOrDefault(o => o.QuotationDetailID == dataItem.QuotationDetailID); if (isFactory) { quotationDetail.PurchasingPrice = dataItem.Price; quotationDetail.SalePrice = dataItem.Price * (1 + quotationDetail.PriceDifferenceRate); if (dataItem.Price == quotationDetail.TargetPrice) { quotationDetail.StatusID = 3; quotationDetail.StatusUpdatedBy = userId; quotationDetail.StatusUpdatedDate = DateTime.Now; } } else { quotationDetail.TargetPrice = dataItem.Price; if (quotationDetail.SalePrice != dataItem.SalePrice) { quotationDetail.SalePrice = dataItem.SalePrice; } if (quotationDetail.TargetPrice == quotationDetail.SalePrice) { quotationDetail.StatusID = 3; quotationDetail.StatusUpdatedBy = userId; quotationDetail.StatusUpdatedDate = DateTime.Now; } } quotationDetail.PriceUpdatedBy = userId; quotationDetail.PriceUpdatedDate = DateTime.Now; keyIds = keyIds + dataItem.QuotationDetailID.ToString(); context.SaveChanges(); } trans.Commit(); return(converter.DB2DTO_PriceQuotationSearchResult(context.PriceQuotationMng_PriceQuotationSearchResult_View.Where(o => keyIds.Contains(o.QuotationDetailID.ToString())).ToList())); } catch (Exception ex) { trans.Rollback(); notification.Type = NotificationType.Error; notification.Message = ex.Message; return(new List <PriceQuotationSearchResultData>()); } } } }