public override bool UpdateData(int id, ref DTO.QuotationMng.Quotation dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (QuotationMngEntities context = CreateContext())
                {
                    Quotation dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new Quotation();
                        context.Quotation.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.Quotation.FirstOrDefault(o => o.QuotationID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Quotation not found!";
                        return(false);
                    }
                    else
                    {
                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoItem.ConcurrencyFlag_String)))
                        {
                            throw new Exception(DALBase.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        converter.DTO2DB(dtoItem, ref dbItem);

                        // generate latest code - table locks required
                        if (id == 0)
                        {
                            using (DbContextTransaction scope = context.Database.BeginTransaction())
                            {
                                context.Database.ExecuteSqlCommand("SELECT TOP 1 * FROM Quotation WITH (TABLOCKX, HOLDLOCK)");
                                int    factoryID = dtoItem.FactoryID.Value;
                                string season    = dtoItem.Season;
                                try
                                {
                                    var quotations = context.Quotation.Where(o => o.FactoryID.HasValue && o.FactoryID.Value == factoryID && o.Season == season).ToList();
                                    int lastNumber = 1;
                                    if (quotations.Count > 0)
                                    {
                                        lastNumber = quotations.Max(o => Convert.ToInt32(o.QuotationUD.Substring(0, 3))) + 1;
                                    }
                                    dbItem.QuotationUD = Library.Common.Helper.formatIndex(lastNumber.ToString(), 3, "0") + "/" + dtoItem.FactoryUD.Substring(0, 3) + "/" + dtoItem.Season.Replace("/", "-");
                                    context.SaveChanges();
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }
                                finally
                                {
                                    scope.Commit();
                                }
                            }
                        }

                        context.QuotationDetail.Local.Where(o => o.Quotation == null).ToList().ForEach(o => context.QuotationDetail.Remove(o));
                        context.QuotationOffer.Local.Where(o => o.Quotation == null).ToList().ForEach(o => context.QuotationOffer.Remove(o));
                        context.QuotationOfferDetail.Local.Where(o => o.QuotationOffer == null || o.QuotationDetail == null).ToList().ForEach(o => context.QuotationOfferDetail.Remove(o));
                        context.SaveChanges();

                        dtoItem = GetData(dbItem.QuotationID, 0, string.Empty, new List <int>(), 0, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = Library.Helper.GetInnerException(ex).Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Exemple #2
0
        public void DTO2DB(DTO.QuotationMng.Quotation dtoItem, ref Quotation dbItem)
        {
            DateTime tmpDate;

            System.Globalization.CultureInfo cInfo = new System.Globalization.CultureInfo("vi-VN");

            // map fields
            AutoMapper.Mapper.Map <DTO.QuotationMng.Quotation, Quotation>(dtoItem, dbItem);
            dbItem.UpdatedBy   = dtoItem.UpdatedBy;
            dbItem.UpdatedDate = DateTime.Now;

            // map load quotation detail
            if (dtoItem.QuotationDetails != null)
            {
                // check for child rows deleted
                foreach (QuotationDetail dbDetail in dbItem.QuotationDetail.ToArray())
                {
                    if (!dtoItem.QuotationDetails.Select(o => o.QuotationDetailID).Contains(dbDetail.QuotationDetailID))
                    {
                        dbItem.QuotationDetail.Remove(dbDetail);
                    }
                }

                // map child rows
                foreach (DTO.QuotationMng.QuotationDetail dtoDetail in dtoItem.QuotationDetails)
                {
                    QuotationDetail dbDetail;
                    if (dtoDetail.QuotationDetailID <= 0)
                    {
                        dbDetail = new QuotationDetail();
                        dbItem.QuotationDetail.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.QuotationDetail.FirstOrDefault(o => o.QuotationDetailID == dtoDetail.QuotationDetailID);
                    }

                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QuotationMng.QuotationDetail, QuotationDetail>(dtoDetail, dbDetail);
                        if (DateTime.TryParse(dtoDetail.StatusUpdatedDate, cInfo, System.Globalization.DateTimeStyles.None, out tmpDate))
                        {
                            dbDetail.StatusUpdatedDate = tmpDate;
                        }

                        // work around for quotation offer detail
                        dtoDetail.DBObject = dbDetail;
                    }
                }
            }

            // map load quotation offer
            if (dtoItem.QuotationOffers != null)
            {
                // check for child rows deleted
                foreach (QuotationOffer dbOffer in dbItem.QuotationOffer.ToArray())
                {
                    if (!dtoItem.QuotationOffers.Select(o => o.QuotationOfferID).Contains(dbOffer.QuotationOfferID))
                    {
                        dbItem.QuotationOffer.Remove(dbOffer);
                    }
                }

                // map child rows
                foreach (DTO.QuotationMng.QuotationOffer dtoOffer in dtoItem.QuotationOffers)
                {
                    QuotationOffer dbOffer;
                    if (dtoOffer.QuotationOfferID <= 0)
                    {
                        dbOffer = new QuotationOffer();
                        dbItem.QuotationOffer.Add(dbOffer);
                    }
                    else
                    {
                        dbOffer = dbItem.QuotationOffer.FirstOrDefault(o => o.QuotationOfferID == dtoOffer.QuotationOfferID);
                    }

                    if (dbOffer != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QuotationMng.QuotationOffer, QuotationOffer>(dtoOffer, dbOffer);
                        if (DateTime.TryParse(dtoOffer.QuotationOfferDate, cInfo, System.Globalization.DateTimeStyles.None, out tmpDate))
                        {
                            dbOffer.QuotationOfferDate = tmpDate;
                        }
                        if (DateTime.TryParse(dtoOffer.UpdatedDate, cInfo, System.Globalization.DateTimeStyles.None, out tmpDate))
                        {
                            dbOffer.UpdatedDate = tmpDate;
                        }
                    }

                    // map quotation offer detail
                    if (dtoOffer.QuotationOfferDetails != null)
                    {
                        // check for child rows deleted
                        foreach (QuotationOfferDetail dbOfferDetail in dbOffer.QuotationOfferDetail.ToArray())
                        {
                            if (!dtoOffer.QuotationOfferDetails.Select(o => o.QuotationOfferDetailID).Contains(dbOfferDetail.QuotationOfferDetailID))
                            {
                                dbOffer.QuotationOfferDetail.Remove(dbOfferDetail);
                            }
                        }

                        // map child rows
                        QuotationDetail dbQuotationDetail = null;
                        foreach (DTO.QuotationMng.QuotationOfferDetail dtoOfferDetail in dtoOffer.QuotationOfferDetails)
                        {
                            QuotationOfferDetail dbOfferDetail;
                            if (dtoOfferDetail.QuotationOfferDetailID <= 0)
                            {
                                dbOfferDetail = new QuotationOfferDetail();
                                dbOffer.QuotationOfferDetail.Add(dbOfferDetail);
                                dbQuotationDetail = null;
                                if (dtoItem.QuotationDetails.FirstOrDefault(o => o.QuotationDetailID == dtoOfferDetail.QuotationDetailID) != null)
                                {
                                    dbQuotationDetail = (QuotationDetail)dtoItem.QuotationDetails.FirstOrDefault(o => o.QuotationDetailID == dtoOfferDetail.QuotationDetailID).DBObject;
                                    dbQuotationDetail.QuotationOfferDetail.Add(dbOfferDetail);
                                    //((QuotationDetail)dtoItem.QuotationDetails.FirstOrDefault(o => o.QuotationDetailID == dtoOfferDetail.QuotationDetailID).DBObject).QuotationOfferDetail.Add(dbOfferDetail);
                                }
                            }
                            else
                            {
                                dbOfferDetail = dbOffer.QuotationOfferDetail.FirstOrDefault(o => o.QuotationOfferDetailID == dtoOfferDetail.QuotationOfferDetailID);
                            }

                            if (dbOfferDetail != null)
                            {
                                AutoMapper.Mapper.Map <DTO.QuotationMng.QuotationOfferDetail, QuotationOfferDetail>(dtoOfferDetail, dbOfferDetail);
                                if (dbQuotationDetail != null)
                                {
                                    dbQuotationDetail.PriceUpdatedDate     = dbOffer.UpdatedDate;
                                    dbQuotationDetail.PriceUpdatedBy       = dbOffer.UpdatedBy;
                                    dbQuotationDetail.LastOfferDirectionID = dbOffer.QuotationOfferDirectionID;
                                }
                            }
                        }
                    }
                }
            }
        }