public SyncResponseMasterDataInfo<InventoryDTO> GetInventory(QueryMasterData myQuery)
 {
     var response = new SyncResponseMasterDataInfo<InventoryDTO>();
     response.MasterData = new SyncMasterDataInfo<InventoryDTO>();
     response.MasterData.EntityName = "Inventory";
     try
     {
         var query = _context.tblInventory.AsQueryable();
         var costCentre = GetSyncCostCentre(myQuery.ApplicationId);
         if (costCentre != null)
         {
             switch (costCentre.CostCentreType)
             {
                 case CostCentreType.Distributor:
                  var inventoryIds =_context.ExecuteStoreQuery<Guid>(string.Format(SyncResources.SyncResource.DHubInventory,costCentre.Id)).ToList();
                  query = query.Where(n => inventoryIds.Contains(n.id) ).OrderBy(s => s.IM_DateCreated);
                     break;
                 case CostCentreType.DistributorSalesman:
                     
                     break;
             }
         }
         query = query.OrderBy(s => s.IM_DateCreated);
         if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
             query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);
         response.MasterData.MasterDataItems = query.ToList().Select(n => Map(n)).ToArray();
         response.ErrorInfo = "Success";
     }
     catch (Exception ex)
     {
         response.ErrorInfo = ex.Message;
     }
     response.MasterData.LastSyncTimeStamp = DateTime.Now;
     return response;
 }
        public SyncResponseMasterDataInfo<ProductFlavourDTO> GetProductFlavour(QueryMasterData myQuery)
        {
            var response = new SyncResponseMasterDataInfo<ProductFlavourDTO>();
            response.MasterData = new SyncMasterDataInfo<ProductFlavourDTO>();
            response.MasterData.EntityName = MasterDataCollective.ProductFlavour.ToString();
            try
            {
                var query = _context.tblProductFlavour.AsQueryable();
                query = query.Where(n => n.IM_DateLastUpdated > myQuery.From
                    && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive))
                    .OrderBy(n => n.IM_DateCreated);

                var deletedQuery = _context.tblProductFlavour.AsQueryable();
                deletedQuery = deletedQuery.Where(n => n.IM_DateLastUpdated > myQuery.From
                    && (n.IM_Status == (int)EntityStatus.Deleted))
                    .OrderBy(n => n.IM_DateCreated);

                if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                    query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);

                if (myQuery.Skip.HasValue && myQuery.Skip.Value == 0)
                {
                    response.DeletedItems = deletedQuery.Select(s => s.id).ToList();
                }
                response.MasterData.MasterDataItems = query.ToList().Select(n => Map(n)).ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;      
        }
        public SyncResponseMasterDataInfo<CommodityOwnerTypeDTO> GetCommodityOwnerType(QueryMasterData q)
        {
            var response = new SyncResponseMasterDataInfo<CommodityOwnerTypeDTO>();
            response.MasterData = new SyncMasterDataInfo<CommodityOwnerTypeDTO>(); ;
            response.MasterData.EntityName = MasterDataCollective.CommodityOwnerType.ToString();
            try
            {
                var query = _context.tblCommodityOwnerType.AsQueryable();
                query = query.Where(n => n.IM_DateLastUpdated > q.From 
                    && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive))
                    .OrderBy(s => s.IM_DateCreated);

                var deletedQuery = _context.tblCommodityOwnerType.AsQueryable();
                deletedQuery = deletedQuery.Where(n => n.IM_DateLastUpdated > q.From
                    && (n.IM_Status == (int)EntityStatus.Deleted))
                    .OrderBy(s => s.IM_DateCreated);

                if (q.Skip.HasValue && q.Take.HasValue)
                    query = query.Skip(q.Skip.Value).Take(q.Take.Value);

                if (q.Skip.HasValue && q.Skip.Value == 0)
                {
                    response.DeletedItems = deletedQuery.Select(s => s.Id).ToList();
                }
                response.MasterData.MasterDataItems = query.ToList().Select(s => Map(s)).ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;
        }
        public SyncResponseMasterDataInfo<OutletDTO> GetOutlet(QueryMasterData myQuery)
        {
            var response = new SyncResponseMasterDataInfo<OutletDTO>();
            response.MasterData = new SyncMasterDataInfo<OutletDTO>();
            response.MasterData.EntityName = MasterDataCollective.Outlet.ToString();
            try
            {
                var costCentre = GetSyncCostCentre(myQuery.ApplicationId);
                var query = _context.tblCostCentre.AsQueryable();
                query = query.Where(n => n.CostCentreType == (int)CostCentreType.Outlet
                    && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive || n.IM_Status == (int)EntityStatus.New));

                var deletedQuery = _context.tblCostCentre.AsQueryable();
                deletedQuery = deletedQuery.Where(n => n.CostCentreType == (int)CostCentreType.Outlet
                    && (n.IM_Status == (int)EntityStatus.Deleted));
                if (costCentre != null)
                {
                    switch (costCentre.CostCentreType)
                    {
                        case CostCentreType.Distributor:
                            query = query.Where(n => n.ParentCostCentreId == costCentre.Id
                                && n.IM_DateLastUpdated > myQuery.From);
                            deletedQuery = deletedQuery.Where(n => n.ParentCostCentreId == costCentre.Id
                                && n.IM_DateLastUpdated > myQuery.From);
                            break;
                        case CostCentreType.DistributorSalesman:
                            var routeIds = GetRouteIds(costCentre, myQuery.From);
                            query = query.Where(n => routeIds.Contains(n.RouteId.Value));
                            deletedQuery = deletedQuery.Where(n => routeIds.Contains(n.RouteId.Value));
                            break;
                    }
                }
                query = query.OrderBy(s => s.IM_DateCreated);
                deletedQuery = deletedQuery.OrderBy(s => s.IM_DateCreated);

                if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                    query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);

                if (myQuery.Skip.HasValue && myQuery.Skip.Value == 0)
                {
                    response.DeletedItems = deletedQuery.Select(s => s.Id).ToList();
                }

                var list = new ArrayList();
                foreach (var item in query.ToArray())
                {
                    list.Add(Map(item));
                }
                response.MasterData.MasterDataItems = list.Cast<OutletDTO>().ToArray();
               // response.MasterData.MasterDataItems = query.ToList().Select(n => Map(n)).ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;
        }
        public SyncResponseMasterDataInfo<ContactDTO> GetContact(QueryMasterData myQuery)
        {
            var response = new SyncResponseMasterDataInfo<ContactDTO>();
            response.MasterData = new SyncMasterDataInfo<ContactDTO>();
            response.MasterData.EntityName = MasterDataCollective.Contact.ToString();
            try
            {
                var costCentre = GetSyncCostCentre(myQuery.ApplicationId);
                var query = _context.tblContact.AsQueryable();
                query = query.Where(n => n.IM_DateLastUpdated > myQuery.From
                    && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive));

                var deletedQuery = _context.tblContact.AsQueryable();
                deletedQuery = deletedQuery.Where(n => n.IM_DateLastUpdated > myQuery.From
                    && (n.IM_Status == (int)EntityStatus.Deleted));

                if (costCentre != null)
                {
                    List<Guid> reqCostCentreIds;
                    switch (costCentre.CostCentreType)
                    {
                        case CostCentreType.Distributor:
                        case CostCentreType.Hub:
                            
                            var contactIds =
                                _context.ExecuteStoreQuery<Guid>(string.Format(SyncResources.SyncResource.DHubContact,
                                    costCentre.Id)).ToList();
                            query = query.Where(n => contactIds.Contains(n.id) || n.id != null ).OrderBy(s => s.IM_DateCreated);
                            deletedQuery = deletedQuery.Where(n => contactIds.Contains(n.id)).OrderBy(s => s.IM_DateCreated);
                            break;
                        case CostCentreType.DistributorSalesman:
                        case CostCentreType.PurchasingClerk:
                            var parentId = costCentre.TblCostCentre.ParentCostCentreId;
                            string sql = string.Format(SyncResources.SyncResource.SalesmanContact,
                                costCentre.TblCostCentre.Id);
                             var salesmanContactIds =
                                _context.ExecuteStoreQuery<Guid>(sql).ToList();
                            query = query.Where(n => salesmanContactIds.Contains(n.id)).OrderBy(s => s.IM_DateCreated);
                            deletedQuery = deletedQuery.Where(n => salesmanContactIds.Contains(n.CostCenterId)).OrderBy(s => s.IM_DateCreated);
                            break;
                    }
                }
                if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                    query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);

                if (myQuery.Skip.HasValue && myQuery.Skip.Value == 0)
                {
                    response.DeletedItems = deletedQuery.Select(s => s.id).ToList();
                }
                response.MasterData.MasterDataItems = query.ToList().Select(n => Map(n)).ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;
        }
        public SyncResponseMasterDataInfo<WeighScaleDTO> GetWeighScale(QueryMasterData myQuery)
        {
            var response = new SyncResponseMasterDataInfo<WeighScaleDTO>();
            response.MasterData = new SyncMasterDataInfo<WeighScaleDTO>();
            response.MasterData.EntityName = MasterDataCollective.WeighScale.ToString();
            try
            {
               

                var query = _context.tblEquipment.Where(s => s.EquipmentType == (int)EquipmentType.WeighingScale).AsQueryable();

                var deletedQuery = _context.tblEquipment.Where(s => s.EquipmentType == (int)EquipmentType.WeighingScale).AsQueryable();

                var costCentre = GetSyncCostCentre(myQuery.ApplicationId);
                if (costCentre != null)
                {
                    switch (costCentre.CostCentreType)
                    {
                        
                        case CostCentreType.Hub:
                            query = query.Where(n => n.CostCentreId == costCentre.Id);
                            deletedQuery = deletedQuery.Where(n => n.CostCentreId == costCentre.Id);
                            break;
                        case CostCentreType.PurchasingClerk:
                           
                            query = query.Where(n => n.CostCentreId==costCentre.TblCostCentre.ParentCostCentreId);
                            deletedQuery = deletedQuery.Where(n => n.CostCentreId == costCentre.TblCostCentre.ParentCostCentreId);
                            break;
                    }
                }
                query = query.Where(n => n.IM_DateLastUpdated > myQuery.From
                    && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive))
                    .OrderBy(n => n.IM_DateCreated);
                deletedQuery = deletedQuery.Where(n => n.IM_DateLastUpdated > myQuery.From
                    && (n.IM_Status == (int)EntityStatus.Deleted))
                    .OrderBy(n => n.IM_DateCreated);

                if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                    query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);

                if (myQuery.Skip.HasValue && myQuery.Skip.Value == 0)
                {
                    response.DeletedItems = deletedQuery.Select(s => s.Id).ToList();
                }
                response.MasterData.MasterDataItems = query.ToList().Select(s => Map(s)).ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;
        }
        public SyncResponseMasterDataInfo<ProductGroupDiscountDTO> GetProductGroupDiscount(QueryMasterData myQuery)
        {
            var response = new SyncResponseMasterDataInfo<ProductGroupDiscountDTO>
                               {
                                   MasterData =
                                       new SyncMasterDataInfo<ProductGroupDiscountDTO>
                                           {EntityName = MasterDataCollective.ProductGroupDiscount.ToString()}
                               };
            try
            {

                var query = _context.tblProductDiscountGroup.Where(s=>s.ProductRef.HasValue).AsQueryable();
                var deletedQuery = _context.tblProductDiscountGroup.AsQueryable();
                var syncostcentre = GetSyncCostCentre(myQuery.ApplicationId);
                if (syncostcentre != null)
                {
                    var discountGroups = DiscountGroups(syncostcentre);
                    query = query.Where(s => discountGroups.Contains(s.DiscountGroup));
                    deletedQuery = deletedQuery.Where(s => discountGroups.Contains(s.DiscountGroup));
                }
                query = query.Where(n => n.IM_DateLastUpdated > myQuery.From
                    && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive))
                    .OrderBy(s => s.IM_DateCreated);

                deletedQuery = deletedQuery.Where(n => n.IM_DateLastUpdated > myQuery.From
                    && (n.IM_Status == (int)EntityStatus.Deleted))
                    .OrderBy(s => s.IM_DateCreated);


                if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                    query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);

                if (myQuery.Skip.HasValue && myQuery.Skip.Value == 0)
                {
                    response.DeletedItems = deletedQuery.Select(s => s.id).ToList();
                }

                var list = new ArrayList();
                foreach (var item in query.ToArray())
                {
                    list.Add(Map(item));
                }
                response.MasterData.MasterDataItems = list.Cast<ProductGroupDiscountDTO>().ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;

            return response;
            
        }
        public SyncResponseMasterDataInfo<CommodityProducerDTO> GetCommodityProducer(QueryMasterData q)
        {
            var response = new SyncResponseMasterDataInfo<CommodityProducerDTO>();
            response.MasterData = new SyncMasterDataInfo<CommodityProducerDTO>(); ;
            response.MasterData.EntityName = MasterDataCollective.CommodityProducer.ToString();
            try
            {
                var costCentre = GetSyncCostCentre(q.ApplicationId);
                var query = _context.tblCommodityProducer.AsQueryable();
                query = query.Where(n => n.IM_DateLastUpdated > q.From
                    && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive));

                var deletedQuery = _context.tblCommodityProducer.AsQueryable();
                deletedQuery = deletedQuery.Where(n => n.IM_DateLastUpdated > q.From
                    && (n.IM_Status == (int)EntityStatus.Deleted));

                switch (costCentre.CostCentreType)
                {
                    case CostCentreType.Hub:
                        var supplierIds = _context.tblCostCentre.Where(n => 
                            n.CostCentreType == (int) CostCentreType.CommoditySupplier && n.ParentCostCentreId == costCentre.Id)
                            .Select(n => n.Id);
                        query = query.Where(n => n.CostCentreId != Guid.Empty && supplierIds.Contains(n.CostCentreId))
                            .OrderBy(n => n.IM_DateCreated);
                        deletedQuery = deletedQuery.Where(n => n.CostCentreId != Guid.Empty && supplierIds.Contains(n.CostCentreId))
                            .OrderBy(n => n.IM_DateCreated);
                        break;
                    case CostCentreType.PurchasingClerk:
                        var hub = _context.tblCostCentre.FirstOrDefault(n => n.Id == costCentre.TblCostCentre.ParentCostCentreId);
                        supplierIds = _context.tblCostCentre.Where(n => n.CostCentreType == (int) CostCentreType.CommoditySupplier 
                            && n.ParentCostCentreId == hub.Id).Select(n => n.Id);
                        query = query.Where(n => supplierIds.Contains(n.CostCentreId)).OrderBy(n => n.IM_DateCreated);
                        deletedQuery = deletedQuery.Where(n => supplierIds.Contains(n.CostCentreId)).OrderBy(n => n.IM_DateCreated);
                        break;
                }
                if (q.Skip.HasValue && q.Take.HasValue)
                    query = query.Skip(q.Skip.Value).Take(q.Take.Value);

                if (q.Skip.HasValue && q.Skip.Value == 0)
                {
                    response.DeletedItems = deletedQuery.Select(s => s.Id).ToList();
                }

                response.MasterData.MasterDataItems = query.ToList().Select(s => Map(s)).ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;
        }
        public SyncResponseMasterDataInfo<ReorderLevelDTO> GetReorderLevel(QueryMasterData myQuery)
        {
            var response = new SyncResponseMasterDataInfo<ReorderLevelDTO>();
            response.MasterData = new SyncMasterDataInfo<ReorderLevelDTO>();
            response.MasterData.EntityName = MasterDataCollective.ReorderLevel.ToString();
            try
            {
                var query = _context.tblReOrderLevel.AsQueryable();
                query = query.Where(n => n.IM_DateLastUpdated > myQuery.From
                                         &&
                                         (n.IM_Status == (int) EntityStatus.Active ||
                                          n.IM_Status == (int) EntityStatus.Inactive))
                    .OrderBy(s => s.IM_DateCreated);

               
                   
                var costCentre = GetSyncCostCentre(myQuery.ApplicationId);
                if (costCentre != null)
                {
                    switch (costCentre.CostCentreType)
                    {
                        case CostCentreType.Distributor:
                            query = query.Where(n => n.DistributorId == costCentre.Id
                                && n.IM_DateLastUpdated > myQuery.From);
                           
                            break;
                        default:
                            break;
                    }
                }

            if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                    query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);

                if (myQuery.Skip.HasValue && myQuery.Skip.Value == 0)
                {
                     var deletedQuery = _context.tblReOrderLevel.AsQueryable();
                     deletedQuery = deletedQuery.Where(n => n.DistributorId == costCentre.Id
                                && n.IM_DateLastUpdated > myQuery.From);
                    deletedQuery = deletedQuery.Where(n => n.IM_DateLastUpdated > myQuery.From
                                                           && (n.IM_Status == (int) EntityStatus.Deleted));
                    response.DeletedItems = deletedQuery.Select(s => s.id).ToList();
                }
                response.MasterData.MasterDataItems = query.ToList().Select(n => Map(n)).ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;
        }
        public SyncResponseMasterDataInfo<SalesmanSupplierDTO> GetSalesmanSupplier(QueryMasterData myQuery)
        {

            var response = new SyncResponseMasterDataInfo<SalesmanSupplierDTO>();
            response.MasterData = new SyncMasterDataInfo<SalesmanSupplierDTO>();
            response.MasterData.EntityName = MasterDataCollective.SalesmanSupplier.ToString();
            try
            {
                var costCentre = GetSyncCostCentre(myQuery.ApplicationId);
                var query = _context.tblSalesmanSupplier.AsQueryable();
                query = query.Where(n => n.IM_DateLastUpdated > myQuery.From
                    && n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive)
                    .OrderBy(s => s.IM_DateCreated);
                var queryDeleted = _context.tblSalesmanSupplier.AsQueryable();
                queryDeleted = queryDeleted.Where(n =>
                    n.IM_Status == (int)EntityStatus.Deleted);
                if (costCentre != null)
                {
                    switch (costCentre.CostCentreType)
                    {
                        case CostCentreType.Distributor:
                        case CostCentreType.Hub:
                            var salesmanIds = _context.tblCostCentre.Where(p => p.ParentCostCentreId == costCentre.Id && p.CostCentreType == (int)CostCentreType.DistributorSalesman).Select(s => s.Id).ToList();
                            query = query.Where(n => salesmanIds.Contains(n.CostCentreId));
                            queryDeleted = queryDeleted.Where(n => salesmanIds.Contains(n.CostCentreId) && n.IM_DateLastUpdated > myQuery.From);
                            break;
                        case CostCentreType.DistributorSalesman:
                        case CostCentreType.PurchasingClerk:

                            query = query.Where(n => n.CostCentreId == costCentre.Id);
                            queryDeleted = queryDeleted.Where(n => n.CostCentreId == costCentre.Id);
                            break;
                    }

                }
                if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                    query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);
                response.MasterData.MasterDataItems = query.ToList().Select(n => Map(n)).ToArray();
                if (myQuery.Skip.HasValue && myQuery.Skip.Value == 0)
                {
                    response.DeletedItems = queryDeleted.Select(s => s.id).ToList();
                }
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;
        }
        public SyncResponseMasterDataInfo<ProductPricingDTO> GetPricing(QueryMasterData q)
        {
            var response = new SyncResponseMasterDataInfo<ProductPricingDTO>();
            response.MasterData = new SyncMasterDataInfo<ProductPricingDTO>();;
            response.MasterData.EntityName = MasterDataCollective.Pricing.ToString();
            try
            {
                var query = _context.tblPricing.AsQueryable();
                query = query.Where(n => 
                    n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive);

                var deletedQuery = _context.tblPricing.AsQueryable();
                deletedQuery = deletedQuery.Where(n =>
                    n.IM_Status == (int)EntityStatus.Deleted);

                var syncostcentre = GetSyncCostCentre(q.ApplicationId);
                if (syncostcentre != null)
                {
                    var pricingTiers = GetApplicablePricingTiers(syncostcentre);

                    query = query.Where(s => s.IM_DateLastUpdated > q.From && pricingTiers.Contains(s.Tier));
                    deletedQuery = deletedQuery.Where(s => s.IM_DateLastUpdated > q.From && pricingTiers.Contains(s.Tier));
                }
                query = query.OrderBy(s => s.IM_DateCreated);
                deletedQuery = deletedQuery.OrderBy(s => s.IM_DateCreated);

                if (q.Skip.HasValue && q.Take.HasValue)
                    query = query.Skip(q.Skip.Value).Take(q.Take.Value);

                if (q.Skip.HasValue && q.Skip.Value == 0)
                {
                    response.DeletedItems = deletedQuery.Select(s => s.id).ToList();
                }

                var list = new ArrayList();
                foreach (var item in query.ToArray())
                {
                    list.Add(Map(item));
                }
                response.MasterData.MasterDataItems = list.Cast<ProductPricingDTO>().ToArray();
                
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;
        }
        public SyncResponseMasterDataInfo<StoreDTO> GetStore(QueryMasterData q)
        {
            var response = new SyncResponseMasterDataInfo<StoreDTO>();
            response.MasterData = new SyncMasterDataInfo<StoreDTO>(); ;
            response.MasterData.EntityName = MasterDataCollective.Store.ToString();
            try
            {
                var cct = GetSyncCostCentre(q.ApplicationId);
                var query = _context.tblCostCentre.AsQueryable();
                query = query.Where(n => n.CostCentreType == (int) CostCentreType.Store
                    && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive)
                    && n.IM_DateLastUpdated > q.From).OrderBy(n => n.IM_DateCreated);

                var deletedQuery = _context.tblCostCentre.AsQueryable();
                deletedQuery = deletedQuery.Where(n => n.CostCentreType == (int)CostCentreType.Store
                    && (n.IM_Status == (int)EntityStatus.Deleted)
                    && n.IM_DateLastUpdated > q.From).OrderBy(n => n.IM_DateCreated);

                switch (cct.CostCentreType)
                {
                    case CostCentreType.Hub:
                        query = query.Where(n => n.ParentCostCentreId == cct.Id);
                        deletedQuery = deletedQuery.Where(n => n.ParentCostCentreId == cct.Id);
                        break;
                    case CostCentreType.PurchasingClerk:
                        query = query.Where(n => n.ParentCostCentreId == cct.TblCostCentre.ParentCostCentreId).Select(n => n);
                        deletedQuery = deletedQuery.Where(n => n.ParentCostCentreId == cct.TblCostCentre.ParentCostCentreId).Select(n => n);
                        break;
                }
                if (q.Skip.HasValue && q.Take.HasValue)
                    query = query.Skip(q.Skip.Value).Take(q.Take.Value);

                if (q.Skip.HasValue && q.Skip.Value == 0)
                {
                    response.DeletedItems = deletedQuery.Select(s => s.Id).ToList();
                }
                response.MasterData.MasterDataItems = query.ToList().Select(s => Map(s)).ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;
        }
  public SyncResponseMasterDataInfo<UnderBankingDTO> GetUnderBanking(QueryMasterData q)
 {
     var response = new SyncResponseMasterDataInfo<UnderBankingDTO>();
     response.MasterData = new SyncMasterDataInfo<UnderBankingDTO>();;
     response.MasterData.EntityName = "UnderBanking";
     try
     {
         var costCentre = GetSyncCostCentre(q.ApplicationId);
         var query = _context.tblRecollection.AsQueryable();
         if (costCentre != null)
         {
             switch (costCentre.CostCentreType)
             {
                 case CostCentreType.Distributor:
                     var disCostcentreId =
                         _context.tblCostCentre.Where(s => s.ParentCostCentreId == costCentre.Id).Select(
                             s => s.Id).ToList();
                     query = query.Where(n => disCostcentreId.Contains(n.FromCostCentreId) && n.IM_DateLastUpdated > q.From);
                     break;
                 case CostCentreType.DistributorSalesman:
                     var outletIds =
                         _context.tblSalemanRoute.Where(s => s.SalemanId == costCentre.Id).SelectMany(
                             s => s.tblRoutes.tblCostCentre)
                             .Where(s => s.CostCentreType == 5).Select(s => s.Id).ToList();
                     query = query.Where(n => n.CostCentreId==costCentre.Id);
                     break;
             }
         }
         query = query.Where(n => n.IM_DateLastUpdated > q.From
             && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive))
             .OrderBy(n => n.IM_DateCreated);
         if (q.Skip.HasValue && q.Take.HasValue)
             query = query.Skip(q.Skip.Value).Take(q.Take.Value);
         response.MasterData.MasterDataItems = query.ToList().Select(s => Map(s)).ToArray();
         response.ErrorInfo = "Success";
     }
     catch (Exception ex)
     {
         response.ErrorInfo = ex.Message;
     }
     response.MasterData.LastSyncTimeStamp = DateTime.Now;
     return response;
 }
        public SyncResponseMasterDataInfo<ProductGroupDiscountDTO> GetHubProductGroupDiscount(QueryMasterData myQuery)
        {
            var response = new SyncResponseMasterDataInfo<ProductGroupDiscountDTO>
                                 {
                                     MasterData =
                                         new SyncMasterDataInfo<ProductGroupDiscountDTO>
                                             {EntityName = MasterDataCollective.ProductGroupDiscount.ToString()}
                                 };
          
              try
              {
                 
                  //var deletedData = _context.ExecuteStoreQuery<ProductGroupDiscountDTO>(SyncQueryStringsUtil.GetDeletedProductGroupDiscountQuery).AsQueryable();

                  var syncostcentre = GetSyncCostCentre(myQuery.ApplicationId);
                  if (syncostcentre != null)
                  {
                      string querysql = string.Format(SyncResources.SyncResource.DHubGroupProdcutDiscount,
                          syncostcentre.Id, myQuery.From.ToString("yyyy-MM-dd HH:mm:ss"));
                      var data = _context.ExecuteStoreQuery<ProductGroupDiscountDTO>(querysql).AsQueryable();
                      if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                          data = data.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value).AsQueryable();



                      response.MasterData.MasterDataItems = data.ToArray();
                      response.ErrorInfo = "Success";

                  }

                
              }
              catch (Exception ex)
              {
                  response.ErrorInfo = ex.Message;
              }
              response.MasterData.LastSyncTimeStamp = DateTime.Now;
            
              return response;
            
        }
 public SyncResponseMasterDataInfo<OutletPriorityDTO> GetOutletPriority(QueryMasterData myQuery)
 {
     var response = new SyncResponseMasterDataInfo<OutletPriorityDTO>();
     response.MasterData = new SyncMasterDataInfo<OutletPriorityDTO>();
     response.MasterData.EntityName = MasterDataCollective.OutletPriority.ToString();
     try
     {
         var costCentre = GetSyncCostCentre(myQuery.ApplicationId);
         var query = _context.tblOutletPriority.AsQueryable();
         query = query.Where(n => 
             n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive);
         if (costCentre != null)
         {
             List<Guid> routeIds;
             switch (costCentre.CostCentreType)
             {
                case CostCentreType.Distributor:
                     routeIds =  GetRouteIds(costCentre);
                     query = query.Where(n => routeIds.Contains(n.RouteId));
                     break;
                 case CostCentreType.DistributorSalesman:
                     routeIds = GetRouteIds(costCentre);
                     query = query.Where(n => routeIds.Contains(n.RouteId));
                     break;
             }
         }
         query = query.OrderBy(s => s.IM_DateCreated);
         if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
             query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);
         response.MasterData.MasterDataItems = query.ToList().Select(n => Map(n)).ToArray();
         response.ErrorInfo = "Success";
     }
     catch (Exception ex)
     {
         response.ErrorInfo = ex.Message;
     }
     response.MasterData.LastSyncTimeStamp = DateTime.Now;
     return response;  
 }
 public SyncResponseMasterDataInfo<CommodityProducerCentreAllocationDTO> GetCommodityProducerCentre(QueryMasterData myQuery)
 {
     var response = new SyncResponseMasterDataInfo<CommodityProducerCentreAllocationDTO>();
     response.MasterData = new SyncMasterDataInfo<CommodityProducerCentreAllocationDTO>();
     response.MasterData.EntityName = MasterDataCollective.CommodityProducerCentreAllocation.ToString();
     try
     {
         var query = _context.tblMasterDataAllocation.AsQueryable();
         query = query.Where(n => n.AllocationType == (int) MasterDataAllocationType.CommodityProducerCentreAllocation 
             && n.IM_DateLastUpdated > myQuery.From).OrderBy(n => n.IM_DateCreated);
         if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
             query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);
         response.MasterData.MasterDataItems = query.ToList().Select(s => Map(s)).ToArray();
         response.ErrorInfo = "Success";
     }
     catch (Exception ex)
     {
         response.ErrorInfo = ex.Message;
     }
     response.MasterData.LastSyncTimeStamp = DateTime.Now;
     return response;
 }
 public SyncResponseMasterDataInfo<DistributorPendingDispatchWarehouseDTO> GetDistributorPendingDispatchWarehouse(QueryMasterData myQuery)
 {
     var response = new SyncResponseMasterDataInfo<DistributorPendingDispatchWarehouseDTO>();
     response.MasterData = new SyncMasterDataInfo<DistributorPendingDispatchWarehouseDTO>();
     response.MasterData.EntityName = MasterDataCollective.DistributorPendingDispatchWarehouse.ToString();
     try
     {
         var costCentre = GetSyncCostCentre(myQuery.ApplicationId);
         var query = _context.tblCostCentre.AsQueryable();
         query = query.Where(n => n.CostCentreType == (int) CostCentreType.DistributorPendingDispatchWarehouse
             && (n.IM_Status == (int)EntityStatus.Active || n.IM_Status == (int)EntityStatus.Inactive)
             && n.IM_DateLastUpdated > myQuery.From);
         if (costCentre != null)
         {
             switch (costCentre.CostCentreType)
             {
                 case CostCentreType.Distributor:
                     query = query.Where(n => n.ParentCostCentreId == costCentre.Id).OrderBy(n => n.IM_DateCreated);
                     break;
                 case CostCentreType.DistributorSalesman:
                     query = query.Where(n => n.ParentCostCentreId == costCentre.TblCostCentre.ParentCostCentreId)
                         .OrderBy(n => n.IM_DateCreated);
                     break;
             }
         }
         if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
             query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);
         response.MasterData.MasterDataItems = query.ToList().Select(n => Map(n)).ToArray();
         response.ErrorInfo = "Success";
     }
     catch (Exception ex)
     {
         response.ErrorInfo = ex.Message;
     }
     response.MasterData.LastSyncTimeStamp = DateTime.Now;
     return response;    
 }
Example #18
0
        public SyncResponseMasterDataInfo<PaymentTrackerDTO> GetPayment(QueryMasterData myQuery)
        {
            var response = new SyncResponseMasterDataInfo<PaymentTrackerDTO>();
            response.MasterData = new SyncMasterDataInfo<PaymentTrackerDTO>();
            response.MasterData.EntityName = "Payment";

            try
            {
                var query = _context.tblPaymentTracker.AsQueryable();
                var costCentre = GetSyncCostCentre(myQuery.ApplicationId);
                if (costCentre != null)
                {
                    switch (costCentre.CostCentreType)
                    {
                        case CostCentreType.Distributor:
                            var paymentIds = _context.ExecuteStoreQuery<Guid>(string.Format(SyncResources.SyncResource.DHubPayments, costCentre.Id)).ToList();
                            query = query.Where(n => paymentIds.Contains(n.id));
                            break;
                        case CostCentreType.DistributorSalesman:

                            break;
                    }
                }
                query = query.OrderBy(s => s.Balance).ThenBy(s => s.PendingConfirmBalance);
                if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                    query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);
                response.MasterData.MasterDataItems = query.ToList().Select(Map).ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;
            return response;

        }
        public SyncResponseMasterDataInfo<ProductPricingDTO> GetHubPricing(QueryMasterData myQuery)
        {
            var response = new SyncResponseMasterDataInfo<ProductPricingDTO>
                               {MasterData = new SyncMasterDataInfo<ProductPricingDTO>()};
            response.MasterData.EntityName = MasterDataCollective.Pricing.ToString();
            try
            {

                var query = _context.ExecuteStoreQuery<ProductPricingDTO>(SyncQueryStringsUtil.GetProductPricingQuery).AsQueryable();

                var syncostcentre = GetSyncCostCentre(myQuery.ApplicationId);
                if (syncostcentre != null)
                {
                    var pricingTiers = GetApplicablePricingTiers(syncostcentre);
                    var changeOutletiers = GetChangeOutletiers(syncostcentre, myQuery.From);
                    query = query.Where(s =>( s.DateLastUpdated > myQuery.From || changeOutletiers.Contains(s.ProductPricingTierMasterId)) && pricingTiers.Contains(s.ProductPricingTierMasterId));
                }
                query = query.OrderBy(s => s.DateCreated);
                if (myQuery.Skip.HasValue && myQuery.Take.HasValue)
                    query = query.Skip(myQuery.Skip.Value).Take(myQuery.Take.Value);

                response.MasterData.MasterDataItems = query.ToArray();
                response.ErrorInfo = "Success";
            }
            catch (Exception ex)
            {
                response.ErrorInfo = ex.Message;
            }
            response.MasterData.LastSyncTimeStamp = DateTime.Now;

            return response;
        }