public static List <Product> GetProducts(GustafsGalleryStoreContext context, string where = null, string orderBy = "createdate", string orderByModifier = "desc")
 {
     if (string.IsNullOrWhiteSpace(where))
     {
         if (string.IsNullOrWhiteSpace(orderBy))
         {
             return(GetProductsBasic(context));
         }
         else
         {
             return(GetProductsOrderBy(context, orderBy, orderByModifier));
         }
     }
     else
     {
         if (string.IsNullOrWhiteSpace(orderBy))
         {
             return(GetProductsWhere(context, where));
         }
         else
         {
             return(GetProductsWhereOrderBy(context, where, orderBy, orderByModifier));
         }
     }
 }
        public static string CreateFilterSQL(string column, List <string> list, GustafsGalleryStoreContext context)
        {
            var where = "";

            foreach (var item in list)
            {
                if (where.Length == 0)
                {
                    where = string.Format(" {0} in ( ", column);
                }
                else
                {
                    where += ",";
                }

                switch (column)
                {
                case "productbrandid":
                    where += context.ProductBrands.Where(x => x.Brand == item).SingleOrDefault().Id;
                    break;

                case "departmentid":
                    where += context.Departments.Where(x => x.DepartmentName == item).SingleOrDefault().Id;
                    break;

                default:
                    return("");
                }
            }

            return(where += ")");
        }
        public static void UpdateUser(long id, string userId, GustafsGalleryStoreContext context)
        {
            var newOrder = context.Orders.
                           Where(x => x.Id == id).
                           SingleOrDefault();

            var userOrders = context.Orders.
                             Where(x => x.UserId == userId).
                             Where(x => x.Id != id).
                             Where(x => x.OrderStatusId == StatusId("Basket", context)).
                             ToList();

            foreach (var order in userOrders)
            {
                var orderItems = context.OrderItems.
                                 Where(x => x.OrderId == order.Id)
                                 .ToList();

                if (orderItems.Count > 0)
                {
                    foreach (var item in orderItems)
                    {
                        context.Remove(item);
                    }
                }

                context.Remove(order);
            }

            context.SaveChanges();
        }
        public static string ValidateDiscountCode(string discountCode, GustafsGalleryStoreContext context)
        {
            // check is code valid
            Discount discount = context.Discounts.Where(x => x.Code == discountCode).SingleOrDefault();
            DateTime?now      = DateTime.Now;

            if (discount == null)
            {
                return(string.Format("Discount code ({0}) does not exist.", discountCode));
            }

            // check is live
            if (!discount.IsLive ||
                // check has started
                discount.StartDate > now ||
                // check if it has been used too often
                discount.UsageCount >= discount.MaxUsage
                )
            {
                return(string.Format("Discount code ({0}) is not valid.", discountCode));
            }

            // check has ended
            if (discount.EndDate < now)
            {
                return(string.Format("Discount code ({0}) is expired.", discountCode));
            }

            return("");
        }
 private static List <Product> GetProductsBasic(GustafsGalleryStoreContext context)
 {
     return(context.Products.
            Include(p => p.Department).
            Include(p => p.ProductBrand).
            Include(p => p.ProductSizes).
            Include(p => p.ProductImages).
            Include(p => p.ProductColours).
            Where(x => x.Stock > 0).ToList());
 }
 private static List <Product> GetProductsWhere(GustafsGalleryStoreContext context, string where = null)
 {
     return(context.Products.
            FromSql(where).
            Include(p => p.Department).
            Include(p => p.ProductBrand).
            Include(p => p.ProductSizes).
            Include(p => p.ProductImages).
            Include(p => p.ProductColours).
            Where(x => x.Stock > 0).ToList());
 }
Exemple #7
0
 public IndexModel(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     IEmailSender emailSender,
     GustafsGalleryStoreContext context)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _emailSender   = emailSender;
     _context       = context;
 }
        public static long StatusId(string status, GustafsGalleryStoreContext context)
        {
            var statusInDb = context.OrderStatuses.Where(x => x.Status == status).SingleOrDefault();

            if (statusInDb != null)
            {
                return(statusInDb.Id);
            }

            return(0);
        }
        public static Product GetProduct(OrderItem item, GustafsGalleryStoreContext context)
        {
            var productId = item.ProductId;
            var product   = context.Products.Where(x => x.Id == productId).
                            Include(x => x.ProductColours).
                            Include(x => x.ProductSizes).
                            SingleOrDefault();

            product.ProductBrand  = context.ProductBrands.Where(x => x.Id == product.ProductBrandId).SingleOrDefault();
            product.ProductImages = context.ProductImages.Where(x => x.ProductId == item.ProductId).ToList();

            return(product);
        }
Exemple #10
0
        public static UpdateResult AddDiscount(DiscountViewModel model, GustafsGalleryStoreContext context)
        {
            try
            {
                if (model.Id == 0)
                {
                    var discount = new Discount()
                    {
                        Code       = model.Code,
                        IsLive     = model.IsLive,
                        StartDate  = model.StartDate,
                        EndDate    = model.EndDate,
                        Value      = model.Value,
                        Percentage = model.Percentage
                    };

                    List <Discount> discounts = new List <Discount>();

                    discounts = context.Discounts.Where(x => x.Code == model.Code).ToList();
                    if (discounts.Count > 0)
                    {
                        return(UpdateResult.Duplicate);
                    }

                    context.Add(discount);
                }
                else
                {
                    var inDb = context.Discounts.Where(x => x.Id == model.Id).SingleOrDefault();

                    inDb.Code       = model.Code;
                    inDb.IsLive     = model.IsLive;
                    inDb.StartDate  = model.StartDate;
                    inDb.EndDate    = model.EndDate;
                    inDb.Value      = model.Value;
                    inDb.Percentage = model.Percentage;

                    context.Update(inDb);
                }

                context.SaveChanges();

                return(UpdateResult.Success);
            }
            catch
            {
                return(UpdateResult.Error);
            }
        }
Exemple #11
0
        public static UpdateResult AddBrand(BrandViewModel model, GustafsGalleryStoreContext context)
        {
            try
            {
                if (model.Id == 0)
                {
                    var brand = new ProductBrand()
                    {
                        Brand = model.Brand
                    };

                    string brandCode           = model.Brand.ToUpper().Substring(0, 3).Trim();
                    List <ProductBrand> brands = new List <ProductBrand>();

                    brands = context.ProductBrands.Where(x => x.Brand == model.Brand).ToList();
                    if (brands.Count > 0)
                    {
                        return(UpdateResult.Duplicate);
                    }

                    brands = context.ProductBrands.Where(x => x.BrandCode == brandCode).ToList();
                    int i = 0;
                    while (brands.Count > 0)
                    {
                        i++;
                        brandCode = model.Brand.ToUpper().Substring(0, 3).Trim() + i;
                        brands    = context.ProductBrands.Where(x => x.BrandCode == brandCode).ToList();
                    }

                    brand.BrandCode = brandCode;

                    context.Add(brand);
                }
                else
                {
                    var inDb = context.ProductBrands.Where(x => x.Id == model.Id).SingleOrDefault();
                    inDb.Brand = model.Brand;
                    context.Update(inDb);
                }

                context.SaveChanges();

                return(UpdateResult.Success);
            }
            catch
            {
                return(UpdateResult.Error);
            }
        }
 public ManageSiteController(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     ILogger <ManageStaffController> logger,
     IEmailSender emailSender,
     GustafsGalleryStoreContext context,
     RoleManager <IdentityRole> roleManager)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _logger        = logger;
     _emailSender   = emailSender;
     _context       = context;
     _roleManager   = roleManager;
 }
 public RegisterModel(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     ILogger <RegisterModel> logger,
     IEmailSender emailSender,
     GustafsGalleryStoreContext context,
     RoleManager <IdentityRole> roleManager)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _logger        = logger;
     _emailSender   = emailSender;
     _context       = context;
     _roleManager   = roleManager;
 }
Exemple #14
0
        public static UpdateResult AddDepartment(DepartmentViewModel model, GustafsGalleryStoreContext context)
        {
            try
            {
                if (model.Id == 0)
                {
                    var dept = new Department()
                    {
                        DepartmentName = model.DepartmentName
                    };

                    string            deptCode = model.DepartmentName.ToUpper().Substring(0, 3).Trim();
                    List <Department> depts    = new List <Department>();

                    depts = context.Departments.Where(x => x.DepartmentName == model.DepartmentName).ToList();
                    if (depts.Count > 0)
                    {
                        return(UpdateResult.Duplicate);
                    }

                    depts = context.Departments.Where(x => x.DepartmentCode == deptCode).ToList();
                    int i = 0;
                    while (depts.Count > 0)
                    {
                        i++;
                        deptCode = model.DepartmentName.ToUpper().Substring(0, 3).Trim() + i;
                        depts    = context.Departments.Where(x => x.DepartmentCode == deptCode).ToList();
                    }

                    dept.DepartmentCode = deptCode;

                    context.Add(dept);
                }
                else
                {
                    var inDb = context.Departments.Where(x => x.Id == model.Id).SingleOrDefault();
                    inDb.DepartmentName = model.DepartmentName;
                    context.Update(inDb);
                }
                context.SaveChanges();

                return(UpdateResult.Success);
            }
            catch
            {
                return(UpdateResult.Error);
            }
        }
Exemple #15
0
        public static UpdateResult AddDeliveryType(DeliveryViewModel model, GustafsGalleryStoreContext context)
        {
            try
            {
                if (model.Id == 0)
                {
                    var company = context.DeliveryCompanies.Where(c => c.Company == model.Company).SingleOrDefault();
                    var type    = new DeliveryType()
                    {
                        Type            = model.Type,
                        Price           = model.Price,
                        Time            = model.Time,
                        DeliveryCompany = company
                    };

                    List <DeliveryType> types = new List <DeliveryType>();

                    types = context.DeliveryTypes.
                            Where(x => x.DeliveryCompanyId == type.DeliveryCompany.Id).
                            Where(x => x.Type == type.Type).
                            ToList();

                    if (types.Count > 0)
                    {
                        return(UpdateResult.Duplicate);
                    }

                    context.Add(type);
                }
                else
                {
                    var inDb = context.DeliveryTypes.Where(x => x.Id == model.Id).SingleOrDefault();
                    inDb.Type  = model.Type;
                    inDb.Price = model.Price;
                    inDb.Time  = model.Time;
                    context.Update(inDb);
                }

                context.SaveChanges();

                return(UpdateResult.Success);
            }
            catch
            {
                return(UpdateResult.Error);
            }
        }
 public CheckoutController(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     ILogger <ManageStaffController> logger,
     IEmailSender emailSender,
     IHostingEnvironment hostingEnvironment,
     GustafsGalleryStoreContext context,
     RoleManager <IdentityRole> roleManager)
 {
     _userManager        = userManager;
     _signInManager      = signInManager;
     _logger             = logger;
     _emailSender        = emailSender;
     _context            = context;
     _roleManager        = roleManager;
     _hostingEnvironment = hostingEnvironment;
 }
        public static bool AddDiscountItem(long basketId, string discountCode, GustafsGalleryStoreContext context)
        {
            Order        order        = OrderHelper.GetOrder(basketId, context);
            Discount     discount     = DiscountCodeHelper.GetDiscount(discountCode, context);
            DiscountItem discountItem = new DiscountItem()
            {
                OrderId = order.Id, DiscountId = discount.Id
            };

            if (!order.Discounts.Contains(discountItem))
            {
                context.Add(discountItem);
                context.SaveChanges();
                return(true);
            }

            return(false);
        }
Exemple #18
0
        public static UpdateResult AddDeliveryCompany(DeliveryViewModel model, GustafsGalleryStoreContext context)
        {
            try
            {
                if (model.Id == 0)
                {
                    var company = new DeliveryCompany()
                    {
                        Company = model.Company
                    };

                    List <DeliveryCompany> companies = new List <DeliveryCompany>();

                    companies = context.DeliveryCompanies.Where(x => x.Company == model.Company).ToList();

                    if (companies.Count > 0)
                    {
                        return(UpdateResult.Duplicate);
                    }

                    context.Add(company);
                }
                else
                {
                    var inDb = context.DeliveryCompanies.Where(x => x.Id == model.Id).SingleOrDefault();
                    inDb.Company = model.Company;
                    context.Update(inDb);
                }

                context.SaveChanges();

                return(UpdateResult.Success);
            }
            catch
            {
                return(UpdateResult.Error);
            }
        }
Exemple #19
0
        public static UpdateResult AddColour(ColourViewModel model, GustafsGalleryStoreContext context)
        {
            try
            {
                if (model.Id == 0)
                {
                    var colour = new Colour()
                    {
                        Value = model.Colour
                    };

                    List <Colour> colours = new List <Colour>();

                    colours = context.Colours.Where(x => x.Value == model.Colour).ToList();
                    if (colours.Count > 0)
                    {
                        return(UpdateResult.Duplicate);
                    }

                    context.Add(colour);
                }
                else
                {
                    var inDb = context.Colours.Where(x => x.Id == model.Id).SingleOrDefault();
                    inDb.Value = model.Colour;
                    context.Update(inDb);
                }

                context.SaveChanges();

                return(UpdateResult.Success);
            }
            catch
            {
                return(UpdateResult.Error);
            }
        }
Exemple #20
0
        public static UpdateResult AddSize(SizeViewModel model, GustafsGalleryStoreContext context)
        {
            try
            {
                if (model.Id == 0)
                {
                    var size = new Size()
                    {
                        Value = model.Size
                    };

                    List <Size> sizes = new List <Size>();

                    sizes = context.Sizes.Where(x => x.Value == model.Size).ToList();
                    if (sizes.Count > 0)
                    {
                        return(UpdateResult.Duplicate);
                    }

                    context.Add(size);
                }
                else
                {
                    var inDb = context.Sizes.Where(x => x.Id == model.Id).SingleOrDefault();
                    inDb.Value = model.Size;
                    context.Update(inDb);
                }
                context.SaveChanges();

                return(UpdateResult.Success);
            }
            catch
            {
                return(UpdateResult.Error);
            }
        }
        public static bool IncreaseUsageCount(long orderId, GustafsGalleryStoreContext context)
        {
            // get order and increase usage of each discount
            Order order = OrderHelper.GetOrder(orderId, context);

            try
            {
                foreach (var discount in order.Discounts)
                {
                    var inDb = context.Discounts.Where(x => x.Id == discount.Discount.Id).SingleOrDefault();

                    inDb.UsageCount++;

                    context.Update(inDb);
                    context.SaveChanges();
                }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        public static Order GetOrder(long id, GustafsGalleryStoreContext context)
        {
            var order = context.Orders.
                        Where(x => x.Id == id).
                        SingleOrDefault();

            if (order == null)
            {
                return(order);
            }

            order.OrderItems = context.OrderItems.
                               Where(x => x.OrderId == id).
                               ToList();

            // expand orderitems
            List <OrderItem> orderItems = new List <OrderItem>();
            decimal          totalPrice = 0;

            if (order.OrderItems != null)
            {
                foreach (var item in order.OrderItems)
                {
                    var productId = item.ProductId;
                    var product   = context.Products.Where(x => x.Id == productId).
                                    Include(c => c.ProductBrand).
                                    Include(c => c.ProductColours).
                                    Include(c => c.ProductSizes).
                                    Include(c => c.ProductImages).
                                    SingleOrDefault();

                    product.ProductBrand = context.ProductBrands.
                                           Where(x => x.Id == product.ProductBrandId).
                                           SingleOrDefault();

                    product.ProductSizes = context.ProductSizes.
                                           Where(x => x.ProductId == item.ProductId).
                                           ToList();

                    product.ProductColours = context.ProductColours.
                                             Where(x => x.ProductId == item.ProductId).
                                             ToList();

                    product.ProductImages = context.ProductImages.
                                            Where(x => x.ProductId == item.ProductId).
                                            ToList();

                    item.Product = product;
                    item.Colour  = context.Colours.
                                   Where(x => x.Id == item.ColourId).
                                   SingleOrDefault();

                    item.Size = context.Sizes.
                                Where(x => x.Id == item.SizeId).
                                SingleOrDefault();

                    orderItems.Add(item);

                    totalPrice += (product.Price * item.Quantity);
                }
            }

            order.OrderSubTotalPrice = totalPrice;
            order.OrderItems         = orderItems;

            order.CustomerContact = context.CustomerContacts.
                                    Where(x => x.Id == order.CustomerContactId).
                                    SingleOrDefault();

            order.DeliveryType = context.DeliveryTypes.
                                 Where(x => x.Id == order.DeliveryTypeId).
                                 SingleOrDefault();

            if (order.DeliveryType != null)
            {
                order.OrderTotalPostagePrice = order.DeliveryType.Price;
            }
            else
            {
                order.OrderTotalPostagePrice = 0;
            }

            List <DiscountItem> discountItems = context.DiscountItems.
                                                Where(x => x.OrderId == order.Id).
                                                ToList();

            decimal discountTotal = 0;

            foreach (var discountItem in discountItems)
            {
                Discount inDb = context.Discounts.Where(x => x.Id == discountItem.DiscountId).SingleOrDefault();
                if (inDb.Percentage > 0)
                {
                    inDb.Value = order.OrderSubTotalPrice * (inDb.Percentage / 100);
                }
                discountItem.Discount = inDb;
                discountTotal        += inDb.Value;
            }

            order.OrderDiscountSubTotalPrice = discountTotal;

            order.Discounts = discountItems;

            order.OrderTotalPrice = (order.OrderSubTotalPrice + order.OrderTotalPostagePrice - order.OrderDiscountSubTotalPrice);

            order.DeliveryType.DeliveryCompany = context.DeliveryCompanies.
                                                 Where(x => x.Id == order.DeliveryType.DeliveryCompanyId).
                                                 SingleOrDefault();

            order.OrderStatus = context.OrderStatuses.
                                Where(x => x.Id == order.OrderStatusId).
                                SingleOrDefault();

            return(order);
        }
        public static UpdateResult UpdateOrderStatus(long id, long statusId, GustafsGalleryStoreContext context)
        {
            var inDb = context.Orders.Where(x => x.Id == id).SingleOrDefault();

            if (inDb != null)
            {
                var status = context.OrderStatuses.Where(x => x.Id == statusId).SingleOrDefault();

                var history = new OrderHistory()
                {
                    OrderId     = id,
                    OrderStatus = status,
                    DateStamp   = DateTime.Now
                };

                context.Add(history);

                inDb.OrderStatus = status;

                switch (status.Status)
                {
                case "Order Placed":
                    inDb.OrderPlacedDate = DateTime.Now;
                    break;

                case "Order Dispatched":
                    inDb.OrderCompleteDate = DateTime.Now;
                    break;

                case "Order Cancelled":
                    inDb.CancellationRequestedDate = DateTime.Now;
                    break;

                case "Cancellation Completed":
                    inDb.CancellationCompletedDate = DateTime.Now;
                    break;

                case "Return Completed":
                    inDb.OrderCompleteDate = DateTime.Now;
                    break;

                case "Awaiting Return":
                    inDb.ReturnRequestedDate = DateTime.Now;
                    break;

                case "Order Returned":
                    inDb.ReturnReceivedDate = DateTime.Now;
                    inDb.OrderCompleteDate  = DateTime.Now;
                    break;

                default:
                    break;
                }

                context.Update(inDb);

                try
                {
                    context.SaveChanges();
                    return(UpdateResult.Success);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }

            return(UpdateResult.Error);
        }
 public static decimal GetMinSpend(string discountCode, GustafsGalleryStoreContext context)
 {
     return(context.Discounts.Where(x => x.Code == discountCode).SingleOrDefault().MinSpend);
 }
Exemple #25
0
        private static List <Transaction> GetTransactions(long id, List <OrderItem> products, decimal shipping, string guid, GustafsGalleryStoreContext context)
        {
            var     list     = new List <Item>();
            decimal subtotal = 0;

            foreach (var item in products)
            {
                var product = context.Products.
                              Where(x => x.Id == item.ProductId).
                              SingleOrDefault();

                var newItem = new Item()
                {
                    name     = product.Title,
                    currency = "GBP",
                    price    = product.Price.ToString(),
                    quantity = item.Quantity.ToString()
                };

                list.Add(newItem);
                subtotal += product.Price;
            }

            var itemList = new ItemList()
            {
                items = list
            };

            var details = new Details()
            {
                shipping = shipping.ToString(),
                subtotal = subtotal.ToString()
            };

            var amount = new Amount()
            {
                currency = "GBP",
                total    = (subtotal + shipping).ToString(), // Total must be equal to sum of shipping, tax and subtotal.
                details  = details
            };

            var transactionList = new List <Transaction>();

            transactionList.Add(new Transaction()
            {
                description    = "GUSTAFS GALLERY ONLINE - " + id.ToString(),
                invoice_number = guid,
                amount         = amount,
                item_list      = itemList
            });

            return(transactionList);
        }
Exemple #26
0
        public static Payment CreatePayment(long id, List <OrderItem> products, decimal shipping, GustafsGalleryStoreContext context)
        {
            var clientId  = MasterStrings.PayPalClientId;
            var secretKey = MasterStrings.PayPalSecretKey;

            var payer = new Payer()
            {
                payment_method = "paypal"
            };

            var guid = Guid.NewGuid();

            var redirectUrls = new RedirectUrls()
            {
                cancel_url = "http://localhost:5000/Checkout?id=" + id,
                return_url = "http://localhost:5000/Checkout/PayPalComplete"
            };


            var payment = new Payment()
            {
                intent        = "sale",
                payer         = payer,
                redirect_urls = redirectUrls,
                transactions  = GetTransactions(id, products, shipping, guid.ToString(), context)
            };

            var createdPayment = payment.Create(GetAPIContext(clientId, secretKey));

            return(createdPayment);
        }
 public static Discount GetDiscount(string discountCode, GustafsGalleryStoreContext context)
 {
     return(context.Discounts.Where(x => x.Code == discountCode).SingleOrDefault());
 }
 public static decimal GetMinSpend(long id, GustafsGalleryStoreContext context)
 {
     return(context.Discounts.Where(x => x.Id == id).SingleOrDefault().MinSpend);
 }
Exemple #29
0
        public static Payment ChargePayment(string paymentId, string token, string payerId, string guid, long id, List <OrderItem> products, decimal shipping, GustafsGalleryStoreContext context)
        {
            var clientId  = MasterStrings.PayPalClientId;
            var secretKey = MasterStrings.PayPalSecretKey;

            // Using the information from the redirect, setup the payment to execute.
            var payment = new Payment()
            {
                id = paymentId
            };

            var transactionList = GetTransactions(id, products, shipping, guid.ToString(), context);
            var transactions    = new List <Transaction>();
            var transaction     = new Transaction()
            {
                amount = transactionList[0].amount
            };

            transactions.Add(transaction);

            var paymentExecution = new PaymentExecution()
            {
                payer_id     = payerId,
                transactions = transactions
            };

            try
            {
                // Execute the payment.
                var executedPayment = payment.Execute(GetAPIContext(clientId, secretKey, null), paymentExecution);

                return(executedPayment);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }
 public static Discount GetDiscount(long id, GustafsGalleryStoreContext context)
 {
     return(context.Discounts.Where(x => x.Id == id).SingleOrDefault());
 }