//public async Task<int> SplitUpdate(int Id,InternalTransferOrderViewModel viewModel, InternalTransferOrder Model)
        //{
        //    InternalTransferOrderDetailService internalTransferOrderDetailService = this.ServiceProvider.GetService<InternalTransferOrderDetailService>();
        //    internalTransferOrderDetailService.Username = this.Username;
        //    InternalTransferOrderService service = this.ServiceProvider.GetService<InternalTransferOrderService>();
        //    service.Username = this.Username;

        //    int Updated = 0;
        //    InternalTransferOrder Data = await this.ReadModelById(Id);
        //    InternalTransferOrderViewModel viewM= service.MapToViewModel(Data);

        //    foreach (InternalTransferOrderDetail item in Data.InternalTransferOrderDetails)
        //    {
        //        InternalTransferOrderDetail internalTransferOrderDetail = this.DbContext.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == item.Id);
        //        var dataView = Model.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == item.Id);

        //        if (dataView == null)
        //        {
        //            this.DbContext.InternalTransferOrderDetails.Remove(internalTransferOrderDetail);
        //        }
        //        else
        //        {
        //            if (internalTransferOrderDetail.Quantity != dataView.Quantity)
        //                internalTransferOrderDetail.Quantity -= dataView.Quantity;
        //            internalTransferOrderDetail._LastModifiedBy = this.Username;
        //            internalTransferOrderDetail._LastModifiedUtc = DateTime.UtcNow;
        //            InternalTransferOrderDetailViewModel itemDetail = viewM.InternalTransferOrderDetails.FirstOrDefault(s=>s.Id==item.Id);
        //            viewM.InternalTransferOrderDetails.Remove(itemDetail);
        //            viewM.Id = 0;
        //            viewM.InternalTransferOrderDetails.ForEach(i=>i.Id = 0);
        //        }
        //    }
        //    InternalTransferOrder models=service.MapToModel(viewM);
        //           await service.CreateModel(models);
        //    using (var transaction = this.DbContext.Database.BeginTransaction())
        //    {
        //        try
        //        {
        //            this.DbContext.SaveChanges();
        //            transaction.Commit();
        //        }
        //        catch (Exception e)
        //        {
        //            transaction.Rollback();
        //        }
        //    }

        //    return Updated;
        //}


        public List <InternalTransferOrder> ReadModelUnused(string Order = "{}", List <string> Select = null, string Keyword = null, string Filter = "{}", List <int> CurrentUsed = null)
        {
            IQueryable <InternalTransferOrder> Query = this.DbContext.InternalTransferOrders;

            List <string> SearchAttributes = new List <string>()
            {
                "ITONo", "TRNo"
            };

            Query = ConfigureSearch(Query, SearchAttributes, Keyword);

            List <string> SelectedFields = Select ?? new List <string>()
            {
                "Id", "ITONo", "TRDate", "Active", "_CreatedBy", "TRId", "TRNo", "RequestedArrivalDate", "CategoryName", "DivisionName", "UnitName", "IsPost"
            };

            ExternalTransferOrderItemService externalTransferOrderItemService = this.ServiceProvider.GetService <ExternalTransferOrderItemService>();
            HashSet <int> externalTransferOrderItems = new HashSet <int>(externalTransferOrderItemService.DbSet.Select(p => p.ITOId));

            Query = Query
                    .Select(result => new InternalTransferOrder
            {
                Id     = result.Id,
                ITONo  = result.ITONo,
                TRDate = result.TRDate,
                RequestedArrivalDate = result.RequestedArrivalDate,
                CategoryName         = result.CategoryName,
                UnitName             = result.UnitName,
                DivisionId           = result.DivisionId,
                DivisionName         = result.DivisionName,
                TRId             = result.TRId,
                TRNo             = result.TRNo,
                _CreatedBy       = result._CreatedBy,
                _CreatedUtc      = result._CreatedUtc,
                _LastModifiedUtc = result._LastModifiedUtc
            })
                    .Where(s => s._IsDeleted == false)
                    .Where(s => !externalTransferOrderItems.Contains(s.Id));

            Dictionary <string, string> FilterDictionary = JsonConvert.DeserializeObject <Dictionary <string, string> >(Filter);

            Query = ConfigureFilter(Query, FilterDictionary);

            Dictionary <string, string> OrderDictionary = JsonConvert.DeserializeObject <Dictionary <string, string> >(Order);

            Query = ConfigureOrder(Query, OrderDictionary);

            List <InternalTransferOrder> Data = Query.ToList <InternalTransferOrder>();

            return(Data);
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (this.DeliveryDivision == null || string.IsNullOrWhiteSpace(this.DeliveryDivision._id))
            {
                yield return(new ValidationResult("DeliveryDivision is required", new List <string> {
                    "DeliveryDivision"
                }));
            }

            if (this.OrderDivision == null || string.IsNullOrWhiteSpace(this.OrderDivision._id))
            {
                yield return(new ValidationResult("OrderDivision is required", new List <string> {
                    "OrderDivision"
                }));
            }

            if (this.OrderDate == null || this.OrderDate == DateTime.MinValue)
            {
                yield return(new ValidationResult("OrderDate is required", new List <string> {
                    "OrderDate"
                }));
            }

            if (this.DeliveryDate == null || this.DeliveryDate == DateTime.MinValue)
            {
                yield return(new ValidationResult("DeliveryDate is required", new List <string> {
                    "DeliveryDate"
                }));
            }
            else if (this.DeliveryDate < this.OrderDate)
            {
                yield return(new ValidationResult("DeliveryDate should not be less than OrderDate", new List <string> {
                    "DeliveryDate"
                }));
            }

            int itemErrorCount   = 0;
            int detailErrorCount = 0;

            if (ExternalTransferOrderItems == null || ExternalTransferOrderItems.Count.Equals(0))
            {
                yield return(new ValidationResult("External Transfer Order Item is required", new List <string> {
                    "ExternalTransferOrderItemsCount"
                }));
            }
            else
            {
                string externalTransferOrderItemError = "[";

                foreach (ExternalTransferOrderItemViewModel Item in ExternalTransferOrderItems)
                {
                    externalTransferOrderItemError += "{ ";

                    if (string.IsNullOrWhiteSpace(Item.ITONo))
                    {
                        itemErrorCount++;
                        externalTransferOrderItemError += "InternalTransferOrder: 'TransferRequest is required', ";
                    }
                    else
                    {
                        ExternalTransferOrderItemService externalTransferOrderItemService = (ExternalTransferOrderItemService)validationContext.GetService(typeof(ExternalTransferOrderItemService));
                        //List<ExternalTransferOrderItem> itemsData = externalTransferOrderItemService.ReadModel(Filter: "{ InternalTransferOrderNo : '" + Item.InternalTransferOrderNo + "' }").Item1;
                        //itemsData = itemsData.Where(w => w.ExternalTransferOrderId != this.Id).ToList();
                        List <ExternalTransferOrderItem> itemsData = externalTransferOrderItemService.DbSet.Where(
                            w => w.ETOId != this.Id && w.ITONo.Equals(Item.ITONo)
                            )
                                                                     .ToList();
                        if (itemsData.Count > 0)
                        {
                            itemErrorCount++;
                            externalTransferOrderItemError += "InternalTransferOrder: 'TransferRequest is already used', ";
                        }
                    }

                    string externalTransferOrderDetailError = "[";

                    foreach (ExternalTransferOrderDetailViewModel Detail in Item.ExternalTransferOrderDetails)
                    {
                        externalTransferOrderDetailError += "{ ";

                        //if (Detail.DefaultUom.unit.Equals(Detail.DealUom.unit) && Detail.DefaultQuantity == Detail.DealQuantity && Detail.Convertion != 1)
                        if (Detail.DefaultUom.unit.Equals(Detail.DealUom.unit) && Detail.Convertion != 1)
                        {
                            detailErrorCount++;
                            externalTransferOrderDetailError += "Convertion: 'Convertion should be 1', ";
                        }

                        if (Detail.Price <= 0)
                        {
                            detailErrorCount++;
                            externalTransferOrderDetailError += "Price: 'Price should be more than 0', ";
                        }

                        externalTransferOrderDetailError += " }, ";
                    }

                    externalTransferOrderDetailError += "]";

                    if (detailErrorCount > 0)
                    {
                        itemErrorCount++;
                        externalTransferOrderItemError += string.Concat("ExternalTransferOrderDetails: ", externalTransferOrderDetailError);
                    }

                    externalTransferOrderItemError += " }, ";
                }

                externalTransferOrderItemError += "]";

                if (itemErrorCount > 0)
                {
                    yield return(new ValidationResult(externalTransferOrderItemError, new List <string> {
                        "ExternalTransferOrderItems"
                    }));
                }
            }
        }