Esempio n. 1
0
        public static Product GetProduct(int productNumber, int supplierNumber, int sizeType)
        {
            Product product = new Product();

            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                var productInfo = db.GetProduct(productNumber, supplierNumber, sizeType)
                                  .FirstOrDefault();

                product = new Product
                {
                    Activated           = productInfo.Activated,
                    DateAdded           = productInfo.DateAdded,
                    DateModified        = productInfo.DateModified,
                    DepartmentName      = productInfo.DepartmentName,
                    DepartmentNumber    = productInfo.DepartmentNumber,
                    Description         = productInfo.Description,
                    PricePerUnit        = productInfo.PricePerUnit,
                    ProductNumber       = productInfo.ProductNumber,
                    Size                = "",
                    SupplierName        = productInfo.SupplierName,
                    SupplierNumber      = productInfo.SupplierNumberID,
                    SpecialPricePerUnit = productInfo.SpecialPricePerUnit ?? productInfo.PricePerUnit,
                    RetailPricePerUnit  = productInfo.RetailPricePerUnit ?? productInfo.PricePerUnit,
                    Weight              = 0,
                    LongDescription     = productInfo.LongDescription,
                    IsVirtual           = productInfo.IsVirtual
                };

                product.MainImageNumber = db.ProductPictures
                                          .Where(c => c.ProductNumber == product.ProductNumber && c.Dimensions == PictureSize.Medium.ToString())
                                          .Select(c => c.PictureNumber)
                                          .FirstOrDefault();

                product.SecondaryImageNumber = db.ProductPictures
                                               .Where(c => c.ProductNumber == product.ProductNumber && c.Dimensions == PictureSize.Small.ToString())
                                               .Select(c => c.PictureNumber)
                                               .FirstOrDefault();

                product.AdditionalImageNumber = db.ProductPictures
                                                .Where(c => c.ProductNumber == product.ProductNumber && c.Dimensions == PictureSize.Large.ToString())
                                                .Select(c => c.PictureNumber)
                                                .FirstOrDefault();

                product.Departments = db.Departments
                                      .Select(c => new SelectListItem
                {
                    Text     = "(" + c.DepartmentNumber + ") " + c.DepartmentName,
                    Value    = c.DepartmentNumber.ToString(),
                    Selected = c.DepartmentNumber == product.DepartmentNumber ? true : false
                })
                                      .ToList();

                product.Custodians = db.Custodians
                                     .Select(c => new SelectListItem
                {
                    Text     = "(" + c.CustodianNumber + ") " + c.CustodianName,
                    Value    = c.CustodianNumber.ToString(),
                    Selected = c.CustodianNumber == product.SelectedCustodianNumber ? true : false
                })
                                     .ToList();

                product.SizeVariations = ProductSize.GetExistingProductSizes(productNumber, supplierNumber);
            }

            return(product);
        }
        public FreeMarketObject AddItemFromProduct(int productNumber, int supplierNumber, int quantity, int custodianNumber, int sizeType)
        {
            // Validate
            if (productNumber == 0 || supplierNumber == 0)
            {
                return new FreeMarketObject {
                           Result = FreeMarketResult.Failure, Argument = null, Message = "No products could be found."
                }
            }
            ;

            FreeMarketObject res = new FreeMarketObject();

            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                // Check whether the item already exists
                OrderDetail existingItem = Body.OrderDetails
                                           .Where(c => c.ProductNumber == productNumber && c.SupplierNumber == supplierNumber && c.SizeType == sizeType)
                                           .FirstOrDefault();

                if (existingItem != null)
                {
                    // Update the existing item
                    existingItem.Update(quantity);

                    // Setup return object
                    var productInfo = db.GetProduct(productNumber, supplierNumber, sizeType).FirstOrDefault();

                    if (productInfo == null)
                    {
                        return new FreeMarketObject {
                                   Result = FreeMarketResult.Failure, Argument = null, Message = "Product, Supplier or Price does not exist."
                        }
                    }
                    ;

                    res.Result  = FreeMarketResult.Success;
                    res.Message = string.Format("{0} ({1}) has been added to your cart.", productInfo.Description, quantity);
                }
                else
                {
                    // A new OrderDetail must be created
                    var productInfo = db.GetProduct(productNumber, supplierNumber, sizeType)
                                      .FirstOrDefault();

                    if (productInfo == null)
                    {
                        return new FreeMarketObject {
                                   Result = FreeMarketResult.Failure, Argument = null, Message = "No products could be found."
                        }
                    }
                    ;

                    string status = "Unconfirmed";

                    // Add a small image for the CartBody
                    int imageNumber = db.ProductPictures
                                      .Where(c => c.ProductNumber == productInfo.ProductNumber && c.Dimensions == PictureSize.Small.ToString())
                                      .Select(c => c.PictureNumber)
                                      .FirstOrDefault();

                    // Add the new item to the Session variable
                    Body.OrderDetails.Add(
                        new OrderDetail()
                    {
                        CourierName        = null,
                        CustodianNumber    = custodianNumber,
                        OrderItemStatus    = status,
                        OrderItemValue     = productInfo.PricePerUnit * quantity,
                        OrderNumber        = Order.OrderNumber,
                        PaidCourier        = null,
                        PaidSupplier       = null,
                        PayCourier         = null,
                        PaySupplier        = null,
                        Price              = productInfo.PricePerUnit,
                        ProductNumber      = productInfo.ProductNumber,
                        ProductDescription = productInfo.Description,
                        ProductDepartment  = productInfo.DepartmentName,
                        Quantity           = quantity,
                        Settled            = null,
                        SupplierNumber     = productInfo.SupplierNumberID,
                        SupplierName       = productInfo.SupplierName,
                        SizeType           = sizeType,
                        MainImageNumber    = imageNumber
                    });

                    res.Result  = FreeMarketResult.Success;
                    res.Message = string.Format("{0} ({1}) has been added to your cart.", productInfo.Description, quantity);
                }
            }

            // Keep the OrderTotal in sync
            UpdateTotal();

            return(res);
        }