public FrmFigureEdit()
        {
            InitializeComponent();
            if (IdFigureEdit <= 0)
            {
                CleanItems();
                ReadOnlyItems(false);
                var medicines = _medicineRepo.GetAll();
                medicines.Insert(0, new Medicine() { Id = 0, Name = "..." });
                this.bdsMedicine.DataSource = medicines;

                this._figure = new Figure
                {
                    Id = 0,
                    Name = "",
                    ClinicId= AppContext.CurrentClinic.Id,
                    LastUpdatedDate = DateTime.Now.Date,
                    Description = "",
                    LastUpdatedUser = AppContext.LoggedInUser.Id,
                    Version = 0
                };

                this._figureDetailList = new List<FigureDetail>();

                this._figure.FigureDetail = this._figureDetailList;
            }
            else
            {
                _figure = figureRepository.GetById(IdFigureEdit);
                FillToItemByID();
                ReadOnlyItems(false);
            }
            Initialize(_figure);
        }
        /// <summary>
        /// Updates the specified user.
        /// </summary>
        /// <param name="user">The user.</param>
        public void Update(Figure figure)
        {
            throw new NotImplementedException();
            //try
            //{

            //    var originalFigure = this.Context.Figures.FirstOrDefault(x => x.Id == figure.Id);
            //    if (originalFigure == null) throw new Exception("Không tồn tại dữ liệu trong CSDL.");

            //    var figureList = this.Context.FigureDetails.Where(x => x.FigureId == figure.Id).ToList();

            //    originalFigure.Name = figure.Name;
            //    originalFigure.Description = figure.Description;
            //    originalFigure.Version++;


            //    foreach (var orginItem in figureList)
            //    {
            //        var item = figure.FigureDetail.FirstOrDefault(x => x.Id == orginItem.Id);
            //        if (item == null)
            //        {
            //            this.Context.FigureDetails.Remove(orginItem);
            //        }
            //        else
            //        {
            //            orginItem.MedicineId = item.MedicineId;
            //            orginItem.FigureId = item.FigureId;
            //            orginItem.Volumn = item.Volumn;
            //            orginItem.Version++;
            //        }

            //    }

            //    foreach (var orginItem in figure.FigureDetail)
            //    {
            //        var item = originalFigure.FigureDetail.FirstOrDefault(x => x.Id == orginItem.Id);
            //        if (item != null) continue;
            //        var newItem = new FigureDetail
            //        {
            //            Volumn = orginItem.Volumn,
            //            FigureId = orginItem.FigureId,
            //            MedicineId = orginItem.MedicineId,
            //            Version = 0
            //        };
            //        originalFigure.FigureDetail.Add(newItem);
            //    }

            //    originalFigure.LastUpdatedDate = DateTime.Now;


            //    this.Context.SaveChanges();
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}
        }
 /// <summary>
 /// Inserts the specified figure.
 /// </summary>
 /// <param name="figure">The figure.</param>
 public void Insert(Figure figure)
 {
     figure.SetInfo(false);
     figure.Version = 0;
     foreach (var item in figure.FigureDetail)
     {
         item.SetInfo(false); // Version = 0);
     }
     this.Context.Figures.Add(figure);
     this.Context.SaveChanges();
 }
 /// <summary>
 /// Initializes the specified prescription.
 /// </summary>
 /// <param name="prescription">The prescription.</param>
 private void Initialize(Figure figure)
 {
     this.bdsFigure.DataSource = figure;
     this.bdsFigureDetail.DataSource = figure.FigureDetail;
 }
        private Figure FillToEntity()
        {
            Figure ent;
            if (IdFigureEdit <= 0)
            {
                ent = new Figure();

            }
            else
            {
                ent = figureRepository.GetById(IdFigureEdit);
            }

            ent.Name = txtFigureName.Text.Trim();
            ent.Description = txtGhiChu.Text.Trim();

            return ent;
        }
        /// <summary>
        /// Updates the specified figure.
        /// </summary>
        /// <param name="figure">The figure.</param>
        public void Update(Figure figure)
        {
            //var oldFigure = this.Context.Figures.FirstOrDefault(x => x.Id == figure.Id);
            //if (oldFigure == null) return;
            //oldFigure.Name = figure.Name;
            //oldFigure.Description = figure.Description;
            //// oldFigure.LastUpdatedUser = AppContext.LoggedInUser.Id;
            //oldFigure.LastUpdatedDate = DateTime.Now;
            //oldFigure.Version++;
            //this.Context.SaveChanges();

            try
            {

                var originalFigure = this.Context.Figures.FirstOrDefault(x => x.Id == figure.Id);
                if (originalFigure == null) throw new Exception("Không tồn tại dữ liệu trong CSDL.");

                var figureList = this.Context.FigureDetails.Where(x => x.FigureId == figure.Id).ToList();

                originalFigure.Name = figure.Name;
                originalFigure.ClinicId = figure.ClinicId;
                originalFigure.Description = figure.Description;
                originalFigure.Version++;

                foreach (var orginItem in figureList)
                {
                    var item = figure.FigureDetail.FirstOrDefault(x => x.Id == orginItem.Id);
                    if (item == null)
                    {
                        this.Context.FigureDetails.Remove(orginItem);
                    }
                    else
                    {
                        orginItem.MedicineId = item.MedicineId;
                        orginItem.FigureId = item.FigureId;
                        orginItem.Volumn = item.Volumn;
                        orginItem.Version++;
                    }

                }

                foreach (var orginItem in figure.FigureDetail)
                {
                    var item = originalFigure.FigureDetail.FirstOrDefault(x => x.Id == orginItem.Id);
                    if (item != null) continue;
                    var newItem = new FigureDetail
                    {
                        Volumn = orginItem.Volumn,
                        FigureId = orginItem.FigureId,
                        MedicineId = orginItem.MedicineId,
                        Version = 0
                    };
                    originalFigure.FigureDetail.Add(newItem);
                }

                originalFigure.LastUpdatedDate = DateTime.Now;

                this.Context.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }