Example #1
0
        public bool ImportQuotationDetail(int userId, object dtoItems, out Library.DTO.Notification notification)
        {
            List <DTO.ImportQuotationDetailDTO> dtoImports = ((Newtonsoft.Json.Linq.JArray)dtoItems).ToObject <List <DTO.ImportQuotationDetailDTO> >();

            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            //try to get data
            try
            {
                using (FactoryQuotation2MngEntities context = CreateContext())
                {
                    foreach (var item in dtoImports)
                    {
                        context.FactoryQuotation2Mng_action_AddQuotationOffer(item.QuotationDetailID, item.FactoryPrice, item.NewComment, userId);
                        context.FW_function_RefreshPriceCacheRow(item.Season, item.QuotationDetailID, null);
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Example #2
0
        public bool UpdateData(int userId, object dtoItems, out Library.DTO.Notification notification)
        {
            List <DTO.FactoryQuotationSearchResultDTO> dtoOffers = ((Newtonsoft.Json.Linq.JArray)dtoItems).ToObject <List <DTO.FactoryQuotationSearchResultDTO> >();

            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            List <int> confirmedQuotationDetailIDs = new List <int>();

            try
            {
                //
                // check factory access permission
                //
                foreach (DTO.FactoryQuotationSearchResultDTO dtoOffer in dtoOffers)
                {
                    if (fwFactory.CheckFactoryPermission(userId, dtoOffer.FactoryID.Value) <= 0)
                    {
                        throw new Exception("You dont have permission for accessing factory data for [" + dtoOffer.FactoryUD + "]!");
                    }
                }

                using (FactoryQuotation2MngEntities context = CreateContext())
                {
                    // get similar item: same offerlineid, same factoryid
                    int[] existingIDs = dtoOffers.Where(o => o.NewPurchasingPrice.HasValue).Select(o => o.QuotationDetailID.Value).ToArray();
                    List <DTO.FactoryQuotationSearchResultDTO> additionalItems = new List <DTO.FactoryQuotationSearchResultDTO>();
                    foreach (DTO.FactoryQuotationSearchResultDTO dtoItem in dtoOffers.Where(o => o.NewPurchasingPrice.HasValue && o.OfferSeasonDetailID.HasValue && o.StatusID != 3).ToList())
                    {
                        additionalItems = converter.DB2DTO_SimilarItems(context.FactoryQuotation2Mng_SimilarItem_View.Where(o =>
                                                                                                                            !existingIDs.Contains(o.QuotationDetailID) &&
                                                                                                                            o.OfferSeasonDetailID == dtoItem.OfferSeasonDetailID &&
                                                                                                                            o.FactoryID == dtoItem.FactoryID &&
                                                                                                                            o.StatusID != 3).ToList());

                        if (additionalItems.Count() > 0)
                        {
                            additionalItems.ForEach(o => { o.NewPurchasingPrice = dtoItem.NewPurchasingPrice.Value; o.NewPurchasingComment = dtoItem.NewPurchasingComment; });
                            dtoOffers.AddRange(additionalItems);
                        }
                    }

                    foreach (DTO.FactoryQuotationSearchResultDTO dtoItem in dtoOffers.Where(o => o.NewPurchasingPrice.HasValue && o.QuotationStatusNM != "WAITING REJECT"))
                    {
                        QuotationDetail dbQuotationDetail = context.QuotationDetail.FirstOrDefault(o => o.QuotationDetailID == dtoItem.QuotationDetailID);
                        if (dbQuotationDetail == null)
                        {
                            throw new Exception("Quotation item: [" + dtoItem.ArticleCode + "] [" + dtoItem.FactoryUD + "] not found!");
                        }
                        var dbDiff = context.PriceDifference.FirstOrDefault(o => o.PriceDifferenceUD == dtoItem.PriceDifferenceCode && o.Season == dtoItem.Season);
                        if (dbDiff == null)
                        {
                            throw new Exception("Quality code: [" + dtoItem.PriceDifferenceCode + "] [" + dtoItem.Season + "] not found!");
                        }

                        dbQuotationDetail.PurchasingPrice  = dtoItem.NewPurchasingPrice.Value;
                        dbQuotationDetail.PriceUpdatedDate = DateTime.Now;
                        dbQuotationDetail.SalePrice        = dtoItem.NewPurchasingPrice.Value * (1 + Math.Round(dbDiff.Rate.Value / 100, 2, MidpointRounding.AwayFromZero));

                        // check if can confirm
                        if (dbQuotationDetail.TargetPrice == null)
                        {
                            dbQuotationDetail.TargetPrice = 0;
                        }
                        if (dbQuotationDetail.SalePrice == Math.Round(dbQuotationDetail.TargetPrice.Value / (1 + dbDiff.Rate.Value), 2, MidpointRounding.AwayFromZero))
                        {
                            dbQuotationDetail.StatusID          = 3; // confirm status;
                            dbQuotationDetail.StatusUpdatedBy   = userId;
                            dbQuotationDetail.StatusUpdatedDate = DateTime.Now;
                            //confirmedQuotationDetailIDs.Add(dbQuotationDetail.QuotationDetailID);
                            if (dbQuotationDetail.OfferSeasonQuotationRequestDetailID.HasValue)
                            {
                                confirmedQuotationDetailIDs.Add(dbQuotationDetail.QuotationDetailID);
                            }
                        }

                        QuotationOffer dbOffer = new QuotationOffer();
                        context.QuotationOffer.Add(dbOffer);
                        dbOffer.QuotationOfferVersion     = context.QuotationOffer.Count(o => o.QuotationID == dbQuotationDetail.QuotationID) + 1;
                        dbOffer.QuotationID               = dbQuotationDetail.QuotationID;
                        dbOffer.QuotationOfferDate        = DateTime.Now;
                        dbOffer.QuotationOfferDirectionID = 1;
                        dbOffer.UpdatedBy   = userId;
                        dbOffer.UpdatedDate = DateTime.Now;

                        QuotationOfferDetail dbOfferDetail = new QuotationOfferDetail();
                        dbOffer.QuotationOfferDetail.Add(dbOfferDetail);
                        dbOfferDetail.QuotationDetailID = dtoItem.QuotationDetailID;
                        dbOfferDetail.Price             = dtoItem.NewPurchasingPrice.Value;
                        if (!string.IsNullOrEmpty(dtoItem.NewPurchasingComment))
                        {
                            dbOfferDetail.Remark = dtoItem.NewPurchasingComment;
                        }
                    }

                    foreach (DTO.FactoryQuotationSearchResultDTO dtoItem in dtoOffers.Where(o => o.QuotationStatusNM == "WAITING REJECT"))
                    {
                        QuotationDetail dbQuotationDetail = context.QuotationDetail.FirstOrDefault(o => o.QuotationDetailID == dtoItem.QuotationDetailID);
                        if (dbQuotationDetail == null)
                        {
                            throw new Exception("Quotation item: [" + dtoItem.ArticleCode + "] [" + dtoItem.FactoryUD + "] not found!");
                        }

                        dbQuotationDetail.StatusID          = 5; //REJECTED
                        dbQuotationDetail.StatusUpdatedBy   = userId;
                        dbQuotationDetail.StatusUpdatedDate = DateTime.Now;

                        //reset data OfferSeasonDetail
                        context.FactoryQuotation2Mng_function_AfterUnConfirm(dtoItem.QuotationDetailID);
                    }
                    context.SaveChanges();

                    // update offer season planing purchasing price
                    foreach (int quotationDetailID in confirmedQuotationDetailIDs)
                    {
                        context.FW_function_UpdateOfferSeasonDetailPurchasingPriceFromQuotationConfirmed(quotationDetailID, userId);
                    }

                    // refresh cached data
                    foreach (DTO.FactoryQuotationSearchResultDTO dtoItem in dtoOffers.ToList())
                    {
                        context.FW_function_RefreshPriceCacheRow(dtoItem.Season, dtoItem.QuotationDetailID, null);
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }