public skPurchase GetPurchaseDetailsByStockID(int stockid)
        {
            var VendorRepo = new VendorRepo();

            var query = (from P in DB.dtPurcheses
                         from U in DB.dtUsers
                         .Where(x => P.AddedBy == x.uID)
                         .DefaultIfEmpty()
                         join S in DB.dtStocks on P.pID equals S.PurchaseID
                         where S.sID == stockid
                         select new skPurchase
            {
                ID = P.pID,
                Created = P.Created,
                CreatedBy = U.UserName,
                PapPalTransactionID = P.PayPalTransactionID,
                Description = P.ItemDescription,
                Title = P.ItemTitle,
                Invoice = P.InvoiceID,
                Amount = Convert.ToDecimal(P.PurchesedValue),
                Postage = Convert.ToDecimal(P.ShippingCosts),
                VendorObject = VendorRepo.GetVendorDetails(Convert.ToInt32(P.VendorID)),
                PurchaseDate = P.Purchesed_Date,
                Updated = P.Updated
            }).FirstOrDefault();

            var StockQuery = from SL in DB.dtStocks
                             join SD in DB.dtStockDetails on SL.sID equals SD.StockID
                             join ST in DB.dtCategories on SL.CategoryID equals ST.CatID
                             join VB in DB.dtValueBands on SL.ValueBandID equals VB.ivID
                             where SL.PurchaseID == query.ID
                             select new skStock
            {
                Stockid        = SL.sID,
                Name           = SL.ItemTitle,
                Description    = SL.ItemDesc,
                Sold           = SL.Sold,
                Created        = SL.Created,
                SaleValue      = SD.SaleValue,
                purchasedvalue = SD.PurchaseValue,
                CategoryObject = new skCategory
                {
                    Description = ST.Description,
                    Name        = ST.Title,
                    StockTypeID = ST.CatID
                },
                ValueBandObject = new skValueBands
                {
                    Description = VB.Description,
                    HighValue   = VB.HighValue,
                    ID          = VB.ivID,
                    LowValue    = VB.LowValue,
                }
            };

            if (query == null)
            {
                throw new NoRecordException(typeof(skPurchase));
            }
            else
            {
                query.LinkedStock = StockQuery.ToList <skStock>();

                return(query);
            }
        }
        public List <ITransaction> GetTransactionList(SearchFilterObject SearchFilter)
        {
            List <ITransaction> ReturnList = new List <ITransaction>();

            StockRepo   StockRepo   = new StockRepo();
            AddressRepo AddressRepo = new AddressRepo();

            TransactionType TranType = (TransactionType)SearchFilter.ExtraSearchObject1;

            if (TranType == TransactionType.All || TranType == TransactionType.Sale)
            {
                var SalesQuery = (from S in DB.dtSales
                                  join U in DB.dtUsers on S.SoldBy equals U.uID
                                  join UD in DB.dtUserDetails on U.uID equals UD.UserID
                                  join P in DB.dtPersons on S.PersonID equals P.pID

                                  where S.tID.ToString().Equals(SearchFilter.ObjectID == null ? S.tID.ToString() : SearchFilter.ObjectID) &&
                                  S.Title.IndexOf(SearchFilter.ObjectName == null ? S.Title : SearchFilter.ObjectName) >= 0 &&
                                  S.Description.IndexOf(SearchFilter.ObjectDescription == null ? S.Description : SearchFilter.ObjectDescription) >= 0 &&
                                  (S.Created >= SearchFilter.ObjectCreatedFrom && S.Created <= SearchFilter.ObjectCreatedTo)
                                  select new skSales
                {
                    ID = S.tID,
                    Title = S.Title,
                    Description = S.Description,
                    Postage = S.PandP,
                    Amount = S.SoldValue,
                    SaleDate = S.SoldDate,
                    PayPalFees = S.PaypayFees,
                    PayPalTransactionID = S.PayPalTransactionID,
                    Created = S.Created,
                    Updated = S.Updated,
                    TransactionTime = S.SoldDate,
                    SaleMethod = S.SaleMethod,
                    UserObj = new skUser
                    {
                        UserID = U.uID,
                        DOB = UD.DateOfBirth,
                        Email = UD.Email,
                        FistName = UD.FirstName,
                        LastName = UD.LastName,
                        HomePhone = UD.HomePhone,
                        Initials = UD.Initials,
                        UserName = U.UserName,
                        WorkPhone = UD.WorkPhone
                    },
                    PersonObj = new skPerson
                    {
                        pID = P.pID,
                        Created = P.Created,
                        EbayName = P.EbayName,
                        Email = P.Email,
                        FirstName = P.FirstName,
                        HomePhone = P.HomePhone,
                        LinkedAddresses = AddressRepo.GetPersonAddressList(P.pID).ToList(),
                        Mobile = P.WorkPhone,
                        WorkPhone = P.WorkPhone,
                        Surname = P.SureName,
                        Updated = P.Updated
                    }
                    ,
                    StockList = new List <skStock>(StockRepo.GetStockListBySaleID(S.tID))
                }).Take(SearchFilter.RecordsToReturn == 0 ? 10000 : SearchFilter.RecordsToReturn);

                foreach (var item in SalesQuery)
                {
                    ReturnList.Add(item);
                }
            }
            if (TranType == TransactionType.All || TranType == TransactionType.Purchase)
            {
                var VendorRepo = new VendorRepo();

                var PurchaseQuery = (from P in DB.dtPurcheses
                                     from U in DB.dtUsers
                                     .Where(x => P.AddedBy == x.uID)
                                     .DefaultIfEmpty()
                                     where P.pID.ToString().Equals(SearchFilter.ObjectID == null ? P.pID.ToString() : SearchFilter.ObjectID) &&
                                     P.ItemTitle.IndexOf(SearchFilter.ObjectName == null ? P.ItemTitle : SearchFilter.ObjectName) >= 0 &&
                                     P.ItemDescription.IndexOf(SearchFilter.ObjectDescription == null ? P.ItemDescription : SearchFilter.ObjectDescription) >= 0 &&
                                     (P.Created >= SearchFilter.ObjectCreatedFrom && P.Created <= SearchFilter.ObjectCreatedTo)
                                     select new skPurchase
                {
                    ID = P.pID,
                    Created = P.Created,
                    CreatedBy = U.UserName,
                    PapPalTransactionID = P.PayPalTransactionID,
                    Description = P.ItemDescription,
                    Title = P.ItemTitle,
                    Invoice = P.InvoiceID,
                    Amount = Convert.ToDecimal(P.PurchesedValue),
                    Postage = Convert.ToDecimal(P.ShippingCosts),
                    VendorObject = VendorRepo.GetVendorDetails(Convert.ToInt32(P.VendorID)),
                    PurchaseDate = P.Purchesed_Date,
                    TransactionTime = P.Purchesed_Date,
                    Updated = P.Updated,
                    LinkedStock = new List <skStock>(StockRepo.GetStockListByPrucahseID(P.pID))
                }).Take(SearchFilter.RecordsToReturn == 0 ? 10000 : SearchFilter.RecordsToReturn);

                foreach (var item in PurchaseQuery)
                {
                    ReturnList.Add(item);
                }
            }

            if (TranType == TransactionType.All || TranType == TransactionType.Refund)
            {
                var RefundQuery = (from R in DB.dtRefunds
                                   from U in DB.dtUsers
                                   .Where(x => R.RefundedBy == x.uID)
                                   .DefaultIfEmpty()
                                   join UD in DB.dtUserDetails on U.uID equals UD.UserID

                                   join S in DB.dtStocks on R.StockID equals S.sID
                                   join ST in DB.dtCategories on S.CategoryID equals ST.CatID
                                   from SD in DB.dtStockDetails
                                   .Where(o => o.StockID == S.sID)
                                   .DefaultIfEmpty()

                                   select new skRefund
                {
                    Amount = R.Amount,
                    Created = R.Created,
                    Description = R.Reason,
                    Title = S.ItemTitle,
                    ID = R.rID,
                    PayPalTransactionID = R.PayPalTransactionID,
                    SHippingCosts = R.Postage,
                    StockItem = new skStock
                    {
                        Stockid = S.sID,
                        Created = S.Created,
                        Description = S.ItemDesc,
                        Name = S.ItemTitle,
                        purchasedvalue = SD.PurchaseValue,
                        SaleValue = SD.SaleValue,
                        Sold = S.Sold,
                        Updated = S.Updated,
                        CategoryObject = new skCategory
                        {
                            Description = ST.Description,
                            Name = ST.Title,
                            StockTypeID = ST.CatID
                        }
                    },
                    TransactionTime = R.Refunded,
                    TransactionType = TransactionType.Refund,
                    Updated = R.Updated,
                    User = new skUser
                    {
                        UserID = U.uID,
                        DOB = UD.DateOfBirth,
                        Email = UD.Email,
                        FistName = UD.FirstName,
                        LastName = UD.LastName,
                        HomePhone = UD.HomePhone,
                        Initials = UD.Initials,
                        UserName = U.UserName,
                        WorkPhone = UD.WorkPhone
                    }
                }).Take(SearchFilter.RecordsToReturn == 0 ? 10000 : SearchFilter.RecordsToReturn);

                foreach (var item in RefundQuery)
                {
                    ReturnList.Add(item);
                }
            }

            ReturnList = ReturnList.OrderBy(x => x.TransactionTime).Take(SearchFilter.RecordsToReturn == 0? 10000 : SearchFilter.RecordsToReturn).ToList();
            ReturnList = ReturnList.OrderBy(x => x.TransactionTime).Take(SearchFilter.RecordsToReturn == 0 ? 10000 : SearchFilter.RecordsToReturn).ToList();
            return(ReturnList);
        }