/// <summary>
        /// Delete an entity.
        /// </summary>
        /// <param name="model"></param>
        public void Delete(PurchaseRebateViewModel model)
        {
            var entity = model.ToEntity();

            this._PurchaseRebatesRepository.Delete(entity);

            #region Commit Changes
            this._unitOfWork.Commit();
            #endregion
        }
        /// <summary>
        /// Throw an exception if name is exist.
        /// </summary>
        /// <param name="model">PurchaseRebate view model</param>
        public void ThrowExceptionIfExist(PurchaseRebateViewModel model)
        {
            ConditionFilter <PurchaseRebate, long> condition = new ConditionFilter <PurchaseRebate, long>
            {
                Query = (entity =>
                         entity.Code == model.Code &&
                         entity.Id != model.Id)
            };
            var existEntity = this._PurchaseRebatesRepository.Get(condition).FirstOrDefault();

            if (existEntity != null)
            {
                throw new ItemAlreadyExistException();
            }
        }
        /// <summary>
        /// Update an entity.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public PurchaseRebateViewModel Update(PurchaseRebateViewModel model)
        {
            this.ThrowExceptionIfExist(model);
            this._closedMonthsService.ValidateIfMonthIsClosed(model.Date.Value);


            var entity = model.ToEntity();

            entity = this._PurchaseRebatesRepository.Update(entity);

            #region Commit Changes
            this._unitOfWork.Commit();
            #endregion

            model = entity.ToModel();
            return(model);
        }
        /// <summary>
        /// Add an entity.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public PurchaseRebateViewModel Add(PurchaseRebateViewModel model)
        {
            if (model.Journal == null)
            {
                //this.ThrowExceptionIfExist(model);
                this._closedMonthsService.ValidateIfMonthIsClosed(model.Date.Value);


                DateTime CurrentDate = DateTime.Now;
                var      entity      = model.ToEntity();
                entity.CreationDate = CurrentDate;

                foreach (var item in entity.PurchaseRebateProducts)
                {
                    item.CreationDate = CurrentDate;
                }

                foreach (var item in entity.PurchaseRebateCostCenters)
                {
                    item.CreationDate = CurrentDate;
                }

                entity = this._PurchaseRebatesRepository.Add(entity);


                #region Update Products
                try
                {
                    if (string.IsNullOrEmpty(model.Code) == false)
                    {
                        long code     = long.Parse(model.Code);
                        var  products = this._productsRepository.Get(null).Where(x => x.PurchaseInvoiceId == code);

                        if (products.Count() > 0)
                        {
                            foreach (var item in entity.PurchaseRebateProducts)
                            {
                                var product = this._productsRepository.Get().Where(x => x.BrandId == item.BrandId && x.MeasurementUnitId == item.MeasurementUnitId);                                 //model.PurchaseRebateProducts.FirstOrDefault(x => x.ProductId == item.ProductId);
                                foreach (var p in product)
                                {
                                    if (p.Quantity < item.Quantity)
                                    {
                                        throw new Exception();
                                    }
                                    else if (p.Quantity > item.Quantity)
                                    {
                                        p.Quantity = item.Quantity;
                                        this._productsRepository.Update(p);
                                    }
                                    else if (p.Quantity == item.Quantity)
                                    {
                                        this._productsRepository.Delete(p);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                #endregion


                #region Commit Changes
                this._unitOfWork.Commit();
                #endregion

                //this._journalPostingsService.TryPostAutomatic(entity.Id, MovementType.PurchaseRebate);

                model = entity.ToModel();

                model.Journal = this._journalPostingsService.Post(model.Id, MovementType.PurchaseRebate);

                model.Journal.Date = model.Date.Value;

                foreach (var Journal in model.Journal.journalDetails)
                {
                    Journal.AccountFullCode = this._accountChartsRepository.Get().FirstOrDefault(x => x.Id == Journal.AccountId).FullCode;
                }
                //model.Journal.DescriptionAr = model.DescriptionAr;
                //model.Journal.DescriptionEn = model.DescriptionEn;
            }
            else if (model.Journal.PostingStatus == PostingStatus.Approved)
            {
                this._journalsService.AddJournal(model.Journal, PostingStatus.NeedAprove);

                var entity = this._PurchaseRebatesRepository.Get(model.Id);
                entity.IsPosted       = false;
                entity.PostingDate    = DateTime.Now;
                entity.PostedByUserId = this._currentUserService.CurrentUserId;
                entity = this._PurchaseRebatesRepository.Update(entity);
                this._unitOfWork.Commit();
            }
            else if (model.Journal.PostingStatus == PostingStatus.Rejected)
            {
            }

            return(model);
        }