Exemple #1
0
        public IActionResult AddToBasket([FromBody] OrderItemDTO newItemDTO)
        {
            if (ModelState.IsValid)
            {
                _basket = _context.Orders.
                          Where(x => x.Id == newItemDTO.OrderId).
                          Where(x => x.OrderStatusId == OrderHelper.StatusId(MasterStrings.Basket, _context)).
                          Include(x => x.OrderItems).
                          SingleOrDefault();

                if (_basket == null)
                {
                    NewBasket();
                }

                var newItem = TransformNewItemDTO(newItemDTO);

                bool isProductInBasket = false;

                foreach (var item in _basket.OrderItems)
                {
                    if (item.ProductId == newItem.ProductId &&
                        item.SizeId == newItem.SizeId &&
                        item.ColourId == newItem.ColourId
                        )
                    {
                        isProductInBasket = true;
                        item.Quantity++;
                    }
                }

                if (isProductInBasket == false)
                {
                    _basket.OrderItems.Add(newItem);
                }

                _context.Update(_basket);

                try
                {
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message, ex.InnerException);
                }

                return(new OkObjectResult(_basket.Id));
            }

            return(new BadRequestResult());
        }
Exemple #2
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 #3
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);
            }
        }
Exemple #4
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 #5
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);
            }
        }
Exemple #6
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 #7
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);
            }
        }
        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);
            }
        }
Exemple #9
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);
            }
        }
        // GET: /<controller>/
        public IActionResult Index(long id, string statusMessage = null, string successMessage = null, string failureMessage = null)
        {
            //return ControllerHelper.RedirectToLocal(this, "/Home/ComingSoon");

            var userId = _userManager.GetUserId(User);
            //var order = _context.Orders.
            //Where(x => x.Id == id).
            //Where(x => x.OrderStatusId == StatusId(MasterStrings.Basket)).
            //SingleOrDefault();
            var order         = OrderHelper.GetOrder(id, _context);
            var isNonGiftCard = true;

            if (order == null)
            {
                return(ControllerHelper.RedirectToLocal(this, "/Home?failureMessage=No basket found"));
            }

            if (order.UserId == null)
            {
                // set basket user id
                order.UserId = userId;

                _context.Update(order);
                _context.SaveChanges();
            }

            var viewModel = new CheckoutViewModel()
            {
                Basket   = OrderHelper.GetOrder(order.Id, _context),
                Contacts = _context.CustomerContacts.
                           Where(x => x.UserId == userId).
                           ToList(),
                StatusMessage  = statusMessage,
                FailureMessage = failureMessage,
                SuccessMessage = successMessage
            };

            foreach (var item in viewModel.Basket.OrderItems)
            {
                if (item.Product.DepartmentId == 13)
                {
                    isNonGiftCard = false;
                }
            }

            if (viewModel.Basket.OrderItems.Count > 2)
            {
                isNonGiftCard = true;
            }

            if (isNonGiftCard)
            {
                viewModel.DeliveryTypes = _context.DeliveryTypes.
                                          Where(x => x.Id > 0).
                                          Where(x => x.Id < 999).
                                          Include(x => x.DeliveryCompany).
                                          ToList();
            }
            else
            {
                viewModel.DeliveryTypes = _context.DeliveryTypes.
                                          Where(x => x.Id == 999).
                                          Include(x => x.DeliveryCompany).
                                          ToList();
            }

            return(View(viewModel));
        }
Exemple #11
0
        public async Task <IActionResult> CancelOrder(long id)
        {
            var order = _context.Orders.
                        Where(x => x.Id == id).
                        Include(x => x.DeliveryType).
                        SingleOrDefault();
            UpdateResult result         = UpdateResult.Error;
            string       failureMessage = null;
            string       successMessage = null;

            if (!string.IsNullOrWhiteSpace(order.StripeSource))
            {
                int amount = Convert.ToInt32((order.OrderTotalPrice) * 100);

                var stripeResult = StripeHelper.RefundCharge(order.PaymentId, amount);

                order.RefundStatus      = stripeResult.Status;
                order.RefundCreatedDate = stripeResult.Created;

                if (stripeResult.Status == MasterStrings.StripeResultSucceeded || stripeResult.Status == MasterStrings.StripeResultPending)
                {
                    order.RefundId = stripeResult.BalanceTransactionId;
                    result         = UpdateResult.Success;
                }

                if (stripeResult.Status == MasterStrings.StripeResultFailed || stripeResult.Status == MasterStrings.StripeResultCancelled)
                {
                    order.RefundId      = stripeResult.FailureBalanceTransactionId;
                    order.RefundMessage = stripeResult.FailureReason;

                    failureMessage = "Refund failed: " + stripeResult.FailureReason;

                    result = UpdateResult.Error;
                }
            }
            else if (!string.IsNullOrWhiteSpace(order.PayPalSaleId))
            {
                var payPalResult = PayPalHelper.RefundPayment(order.PayPalSaleId, order.OrderTotalPrice);

                order.RefundStatus      = payPalResult.state;
                order.RefundCreatedDate = Convert.ToDateTime(payPalResult.create_time);
                order.RefundId          = payPalResult.id;
                order.RefundMessage     = payPalResult.reason_code;

                if (payPalResult.state == MasterStrings.PayPalResultCompleted || payPalResult.state == MasterStrings.PayPalResultPending)
                {
                    result = UpdateResult.Success;
                }

                if (payPalResult.state == MasterStrings.PayPalResultFailed || payPalResult.state == MasterStrings.PayPalResultCancelled)
                {
                    failureMessage = "Refund failed";
                    result         = UpdateResult.Error;
                }
            }
            else
            {
                failureMessage = "Refund couldn't be completed.  Please complete this via payment handler.";
            }

            if (result == UpdateResult.Success)
            {
                order.CancellationCompletedDate = DateTime.Now;
                await UpdateOrder(id, MasterStrings.CancellationCompleted);

                successMessage = "Order updated.";
            }

            _context.Update(order);
            _context.SaveChanges();

            var redirectUrl = string.Format("/ManageOrders/ViewOrder?id={0}", id);

            if (string.IsNullOrWhiteSpace(failureMessage))
            {
                redirectUrl += string.Format("&&failureMessage={0}", failureMessage);
            }
            if (string.IsNullOrWhiteSpace(successMessage))
            {
                redirectUrl += string.Format("&&successMessage={0}", successMessage);
            }

            return(ControllerHelper.RedirectToLocal(this, redirectUrl));
        }
        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);
        }
Exemple #13
0
        public async Task <IActionResult> AddProduct(NewEditProductViewModel input, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            bool    modelValid      = true;
            decimal priceComparison = 0;

            if (string.IsNullOrWhiteSpace(input.Product.Title) ||
                input.Product.Price == priceComparison ||
                string.IsNullOrWhiteSpace(input.Department) ||
                string.IsNullOrWhiteSpace(input.Brand))
            {
                modelValid = false;
            }

            if (modelValid)
            {
                try
                {
                    //Prepare Product
                    input.Product.CreateDate = DateTime.Now;

                    //Department
                    var department = new Department();
                    department = _context.Departments.Where(x => x.DepartmentName == input.Department).SingleOrDefault();
                    input.Product.Department = department;

                    //brand
                    var brand = new ProductBrand();
                    brand = _context.ProductBrands.Where(x => x.Brand == input.Brand).SingleOrDefault();
                    input.Product.ProductBrand = brand;

                    //add sizes
                    List <ProductSize> productSizes = new List <ProductSize>();
                    foreach (var item in input.Size)
                    {
                        var size = new ProductSize()
                        {
                            Size = item.ToString()
                        };

                        productSizes.Add(size);
                    }

                    input.Product.ProductSizes = productSizes;

                    //add sizes
                    List <ProductColour> productColours = new List <ProductColour>();
                    foreach (var item in input.Colour)
                    {
                        var colour = new ProductColour()
                        {
                            Colour = item.ToString()
                        };

                        productColours.Add(colour);
                    }

                    input.Product.ProductColours = productColours;

                    //images
                    List <ProductImage> productImages = new List <ProductImage>();
                    if (input.ImageFiles != null)
                    {
                        foreach (var item in input.ImageFiles)
                        {
                            var filePath = Path.GetTempPath();

                            var filename = filePath + _storeName + '_' + $@"{item.FileName}";

                            using (var stream = new FileStream(filename, FileMode.Create))
                            {
                                await item.CopyToAsync(stream);
                            }

                            S3Helper.UploadToS3(filename, S3Helper.bucketName);

                            var image = new ProductImage()
                            {
                                Uri = "https://d3rlz58riodgu6.cloudfront.net/" + _storeName + '_' + item.FileName.ToString() + "?Authorization"
                            };

                            productImages.Add(image);
                        }
                    }

                    input.Product.ProductImages = productImages;

                    //Create product code
                    //var producCode = string.Concat(input.Product.ProductBrand.BrandCode.ToUpper(), '_', input.Product.Department.DepartmentCode, '_', input.Product.Title.Substring(0, 3).ToUpper());
                    //input.Product.ProductCode = GenerateProductCode(producCode);

                    //Create Product
                    _context.Add(input.Product);

                    _context.SaveChanges();

                    input.Product.ProductCode = string.Concat(input.Product.ProductBrand.Id, '-', input.Product.Department.Id, '-', input.Product.Id);

                    _context.Update(input.Product);

                    _context.SaveChanges();

                    input.SuccessMessage = "Product added";

                    return(ControllerHelper.RedirectToLocal(this, string.Format("/ManageProducts?successMessage={0}", input.SuccessMessage)));
                }
                catch (System.Exception ex)
                {
                    input.FailureMessage = "An Error occured; " + ex.Message;
                }
            }
            input.Brands        = ProductBrand.GetList(_context.ProductBrands.OrderBy(b => b.Brand).ToList());
            input.Colours       = Colour.GetList(_context.Colours.OrderBy(c => c.Value).ToList());
            input.Sizes         = Size.GetList(_context.Sizes.OrderBy(s => s.Value).ToList());
            input.Departments   = Department.GetList(_context.Departments.OrderBy(d => d.DepartmentName).ToList());
            input.StatusMessage = StatusMessage;

            return(View(input));
        }