private async Task <bool> ValidateEmployee(CustomerSalesOrder CustomerSalesOrder)
        {
            if (CustomerSalesOrder.SalesEmployeeId == 0)
            {
                CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.SalesEmployee), ErrorCode.SalesEmployeeEmpty);
            }
            else
            {
                AppUserFilter AppUserFilter = new AppUserFilter
                {
                    Skip = 0,
                    Take = 10,
                    Id   = new IdFilter {
                        Equal = CustomerSalesOrder.SalesEmployeeId
                    },
                    StatusId = new IdFilter {
                        Equal = Enums.StatusEnum.ACTIVE.Id
                    },
                    Selects = AppUserSelect.Id | AppUserSelect.Organization
                };

                var count = await UOW.AppUserRepository.Count(AppUserFilter);

                if (count == 0)
                {
                    CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.SalesEmployee), ErrorCode.SalesEmployeeNotExisted);
                }
            }

            return(CustomerSalesOrder.IsValidated);
        }
 private async Task <bool> ValidateEditedPrice(CustomerSalesOrder CustomerSalesOrder)
 {
     if (EditedPriceStatusEnum.ACTIVE.Id != CustomerSalesOrder.EditedPriceStatusId && EditedPriceStatusEnum.INACTIVE.Id != CustomerSalesOrder.EditedPriceStatusId)
     {
         CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.EditedPriceStatus), ErrorCode.EditedPriceStatusNotExisted);
     }
     return(CustomerSalesOrder.IsValidated);
 }
 private async Task <bool> ValidateOrderDate(CustomerSalesOrder CustomerSalesOrder)
 {
     if (CustomerSalesOrder.OrderDate == default(DateTime))
     {
         CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.OrderDate), ErrorCode.OrderDateEmpty);
     }
     return(CustomerSalesOrder.IsValidated);
 }
        private async Task <bool> ValidateCustomerType(CustomerSalesOrder CustomerSalesOrder)
        {
            if (CustomerSalesOrder.CustomerTypeId == 0)
            {
                CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.CustomerType), ErrorCode.CustomerTypeEmpty);
            }

            return(CustomerSalesOrder.IsValidated);
        }
 private async Task <bool> ValidateDeliveryDate(CustomerSalesOrder CustomerSalesOrder)
 {
     if (CustomerSalesOrder.DeliveryDate.HasValue)
     {
         if (CustomerSalesOrder.DeliveryDate.Value < CustomerSalesOrder.OrderDate)
         {
             CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.DeliveryDate), ErrorCode.DeliveryDateInvalid);
         }
     }
     return(CustomerSalesOrder.IsValidated);
 }
        private async Task <bool> ValidateCustomer(CustomerSalesOrder CustomerSalesOrder)
        {
            if (CustomerSalesOrder.CustomerId == 0)
            {
                CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.Customer), ErrorCode.CustomerEmpty);
            }
            else
            {
                CustomerFilter CustomerFilter = new CustomerFilter
                {
                    Skip = 0,
                    Take = 10,
                    Id   = new IdFilter {
                        In = new List <long> {
                            CustomerSalesOrder.CustomerId
                        }
                    },
                    CustomerTypeId = new IdFilter {
                        Equal = CustomerSalesOrder.CustomerTypeId
                    },
                    StatusId = new IdFilter {
                        Equal = Enums.StatusEnum.ACTIVE.Id
                    },
                    Selects = CustomerSelect.Id
                };

                int count = await UOW.CustomerRepository.Count(CustomerFilter);

                if (count == 0)
                {
                    CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.Customer), ErrorCode.CustomerNotExisted);
                }
            }

            return(CustomerSalesOrder.IsValidated);
        }
        public async Task <bool> ValidateId(CustomerSalesOrder CustomerSalesOrder)
        {
            CustomerSalesOrderFilter CustomerSalesOrderFilter = new CustomerSalesOrderFilter
            {
                Skip = 0,
                Take = 10,
                Id   = new IdFilter {
                    Equal = CustomerSalesOrder.Id
                },
                Selects = CustomerSalesOrderSelect.Id
            };

            int count = await UOW.CustomerSalesOrderRepository.Count(CustomerSalesOrderFilter);

            if (count == 0)
            {
                CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.Id), ErrorCode.IdNotExisted);
            }
            return(count == 1);
        }
        private async Task <bool> ValidateContent(CustomerSalesOrder CustomerSalesOrder)
        {
            if (CustomerSalesOrder.CustomerSalesOrderContents != null && CustomerSalesOrder.CustomerSalesOrderContents.Any())
            {
                var         ItemIds    = CustomerSalesOrder.CustomerSalesOrderContents.Select(x => x.ItemId).ToList();
                var         ProductIds = CustomerSalesOrder.CustomerSalesOrderContents.Select(x => x.Item.ProductId).ToList();
                List <Item> Items      = await UOW.ItemRepository.List(new ItemFilter
                {
                    Skip = 0,
                    Take = int.MaxValue,
                    Id   = new IdFilter {
                        In = ItemIds
                    },
                    Selects = ItemSelect.Id | ItemSelect.SalePrice | ItemSelect.ProductId
                });

                var Products = await UOW.ProductRepository.List(new ProductFilter
                {
                    Id = new IdFilter {
                        In = ProductIds
                    },
                    Skip    = 0,
                    Take    = int.MaxValue,
                    Selects = ProductSelect.UnitOfMeasure | ProductSelect.UnitOfMeasureGrouping | ProductSelect.Id | ProductSelect.TaxType
                });

                var UOMGs = await UOW.UnitOfMeasureGroupingRepository.List(new UnitOfMeasureGroupingFilter
                {
                    Skip    = 0,
                    Take    = int.MaxValue,
                    Selects = UnitOfMeasureGroupingSelect.Id | UnitOfMeasureGroupingSelect.UnitOfMeasure | UnitOfMeasureGroupingSelect.UnitOfMeasureGroupingContents
                });

                foreach (var CustomerSalesOrderContent in CustomerSalesOrder.CustomerSalesOrderContents)
                {
                    if (CustomerSalesOrderContent.UnitOfMeasureId == 0)
                    {
                        CustomerSalesOrderContent.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrderContent.UnitOfMeasure), ErrorCode.UnitOfMeasureEmpty);
                    }
                    else
                    {
                        var Item = Items.Where(x => x.Id == CustomerSalesOrderContent.ItemId).FirstOrDefault();
                        if (Item == null)
                        {
                            CustomerSalesOrderContent.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrderContent.Item), ErrorCode.ItemNotExisted);
                        }
                        else
                        {
                            var Product = Products.Where(x => Item.ProductId == x.Id).FirstOrDefault();
                            List <UnitOfMeasure> UnitOfMeasures = new List <UnitOfMeasure>();
                            if (Product.UnitOfMeasureGroupingId.HasValue)
                            {
                                var UOMG = UOMGs.Where(x => x.Id == Product.UnitOfMeasureGroupingId).FirstOrDefault();
                                UnitOfMeasures = UOMG.UnitOfMeasureGroupingContents.Select(x => new UnitOfMeasure
                                {
                                    Id          = x.UnitOfMeasure.Id,
                                    Code        = x.UnitOfMeasure.Code,
                                    Name        = x.UnitOfMeasure.Name,
                                    Description = x.UnitOfMeasure.Description,
                                    StatusId    = x.UnitOfMeasure.StatusId,
                                    Factor      = x.Factor
                                }).ToList();
                            }

                            UnitOfMeasures.Add(new UnitOfMeasure
                            {
                                Id          = Product.UnitOfMeasure.Id,
                                Code        = Product.UnitOfMeasure.Code,
                                Name        = Product.UnitOfMeasure.Name,
                                Description = Product.UnitOfMeasure.Description,
                                StatusId    = Product.UnitOfMeasure.StatusId,
                                Factor      = 1
                            });

                            var UOM = UnitOfMeasures.Where(x => CustomerSalesOrderContent.UnitOfMeasureId == x.Id).FirstOrDefault();
                            if (UOM == null)
                            {
                                CustomerSalesOrderContent.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrderContent.UnitOfMeasure), ErrorCode.UnitOfMeasureNotExisted);
                            }
                            //else
                            //{
                            //    if(CustomerSalesOrder.EditedPriceStatusId == EditedPriceStatusEnum.ACTIVE.Id)
                            //    {
                            //        if(CustomerSalesOrderContent.SalePrice < (Item.SalePrice * UOM.Factor.Value) * 0.9m
                            //            || CustomerSalesOrderContent.SalePrice > (Item.SalePrice * UOM.Factor.Value) * 1.1m)
                            //            CustomerSalesOrderContent.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrderContent.SalePrice), ErrorCode.PriceOutOfRange);
                            //    }
                            //}
                        }

                        if (CustomerSalesOrderContent.Quantity <= 0)
                        {
                            CustomerSalesOrderContent.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrderContent.Quantity), ErrorCode.QuantityEmpty);
                        }
                    }
                }
            }
            else
            {
                if (CustomerSalesOrder.CustomerSalesOrderPromotions == null || !CustomerSalesOrder.CustomerSalesOrderPromotions.Any())
                {
                    CustomerSalesOrder.AddError(nameof(CustomerSalesOrderValidator), nameof(CustomerSalesOrder.Id), ErrorCode.ContentEmpty);
                }
            }

            return(CustomerSalesOrder.IsValidated);
        }