public void CancelOrder(string userId)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                OrderHeader order = db.OrderHeaders.Find(Order.OrderNumber);

                if (order == null)
                {
                    return;
                }

                order.OrderStatus     = "Cancelled";
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();

                foreach (OrderDetail temp in Body.OrderDetails)
                {
                    temp.OrderItemStatus = "Cancelled";
                    db.Entry(temp).State = EntityState.Modified;
                }

                db.SaveChanges();
            }

            Initialize(userId);
        }
        public static void SaveActivatedProducts(Custodian custodian)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                foreach (Product product in custodian.ActivatedProducts.Products)
                {
                    if (product.CustodianActivated)
                    {
                        ProductCustodian productCustodian = db.ProductCustodians
                                                            .Where(c => c.ProductNumber == product.ProductNumber &&
                                                                   c.SupplierNumber == product.SupplierNumber &&
                                                                   c.CustodianNumber == custodian.CustodianNumber)
                                                            .FirstOrDefault();
                        if (productCustodian == null)
                        {
                            productCustodian = new ProductCustodian
                            {
                                AmountLastIncreasedBySupplier = null,
                                CustodianName               = custodian.CustodianName,
                                CustodianNumber             = custodian.CustodianNumber,
                                DateLastIncreasedBySupplier = null,
                                ProductNumber               = product.ProductNumber,
                                SupplierNumber              = product.SupplierNumber,
                                QuantityOnHand              = 0,
                                StockReservedForOrders      = 0,
                                Active = true
                            };

                            db.ProductCustodians.Add(productCustodian);
                            db.SaveChanges();
                        }
                        else
                        {
                            productCustodian.Active          = true;
                            db.Entry(productCustodian).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                    else if (!product.CustodianActivated)
                    {
                        ProductCustodian productCustodian = db.ProductCustodians
                                                            .Where(c => c.ProductNumber == product.ProductNumber &&
                                                                   c.SupplierNumber == product.SupplierNumber &&
                                                                   c.CustodianNumber == custodian.CustodianNumber)
                                                            .FirstOrDefault();
                        if (productCustodian == null)
                        {
                        }
                        else
                        {
                            productCustodian.Active          = false;
                            db.Entry(productCustodian).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }
            }
        }
        public void Save(string userId = null)
        {
            if (Order.OrderNumber == 0)
            {
                using (FreeMarketEntities db = new FreeMarketEntities())
                {
                    db.OrderHeaders.Add(Order);
                    db.SaveChanges();
                }
            }

            // Compare the Session cart to the database cart and resolve differences
            Compare();

            // Re-initialize the Body
            Body = CartBody.GetDetailsForShoppingCart(Order.OrderNumber);

            // Keep the total order value in sync
            UpdateTotal();

            // Save the Order total
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                db.Entry(Order).State = EntityState.Modified;
                db.SaveChanges();
            }

            AuditUser.LogAudit(6, string.Format("Order number: {0}", Order.OrderNumber), userId);
        }
        public static FreeMarketResult UpdateAddress(string userId, string addressName, string addressLine1, string addressLine2,
                                                     string addressLine3, string addressLine4, string addressSuburb, string addressCity, string addressPostalCode)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                CustomerAddress address = db.CustomerAddresses
                                          .Where(c => c.CustomerNumber == userId && c.AddressName == addressName)
                                          .FirstOrDefault();

                if (address == null)
                {
                    return(FreeMarketResult.Failure);
                }

                address.AddressLine1      = addressLine1;
                address.AddressLine2      = addressLine2;
                address.AddressLine3      = addressLine3;
                address.AddressLine4      = addressLine4;
                address.AddressCity       = addressCity;
                address.AddressPostalCode = addressPostalCode;
                address.AddressSuburb     = addressSuburb;

                db.Entry(address).State = EntityState.Modified;
                db.SaveChanges();

                AuditUser.LogAudit(5, string.Format("Address name: {0}", addressName), userId);
            }

            return(FreeMarketResult.Success);
        }
 public static void SaveCourier(Courier courier)
 {
     using (FreeMarketEntities db = new FreeMarketEntities())
     {
         db.Entry(courier).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
 public static void SaveSupplier(Supplier supplier)
 {
     using (FreeMarketEntities db = new FreeMarketEntities())
     {
         db.Entry(supplier).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
Example #7
0
 public static void SaveCustomer(CashCustomer model)
 {
     using (FreeMarketEntities db = new FreeMarketEntities())
     {
         db.Entry(model).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
Example #8
0
 public static void SaveModel(Support support)
 {
     using (FreeMarketEntities db = new FreeMarketEntities())
     {
         db.Entry(support).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
Example #9
0
 public static void SaveModel(Department model)
 {
     using (FreeMarketEntities db = new FreeMarketEntities())
     {
         db.Entry(model).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
        public static void SaveCustodian(Custodian custodian)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                db.Entry(custodian).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                SaveActivatedProducts(custodian);
            }
        }
        public void Compare()
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                // Get a list of items which are on both the Session and database
                List <OrderDetail> existingItems = Body.OrderDetails.FindAll(c => c.ItemNumber != 0);

                if (existingItems != null && existingItems.Count > 0)
                {
                    foreach (OrderDetail temp in existingItems)
                    {
                        OrderDetail tempDb = db.OrderDetails.Find(temp.ItemNumber);

                        if (tempDb != null)
                        {
                            if (!temp.Equals(tempDb))
                            {
                                // If the item has changed update it
                                tempDb.Quantity       = temp.Quantity;
                                tempDb.OrderItemValue = temp.OrderItemValue;

                                db.Entry(tempDb).State = EntityState.Modified;
                                db.SaveChanges();

                                AuditUser.LogAudit(7, string.Format("Order number: {0}", Order.OrderNumber));
                            }
                        }
                    }
                }

                // Get a list of items that are on the Session variable but not in the database
                List <OrderDetail> newItems = Body.OrderDetails.FindAll(c => c.ItemNumber == 0);

                if (newItems != null && newItems.Count > 0)
                {
                    foreach (OrderDetail tempB in newItems)
                    {
                        tempB.OrderNumber = Order.OrderNumber;

                        ReserveStock(tempB.ProductNumber, tempB.SupplierNumber, (int)tempB.CustodianNumber, tempB.Quantity, tempB.SizeType);

                        db.OrderDetails.Add(tempB);
                    }

                    db.SaveChanges();

                    AuditUser.LogAudit(7, string.Format("Order number: {0}", Order.OrderNumber));
                }

                // Ensure the latest prices are used
                UpdatePrices();
            }
        }
        public static TimeFreightCourierFeeReference SaveModel(TimeFreightCourierFeeReference model)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                TimeFreightCourierFeeReference oldConfig = db.TimeFreightCourierFeeReferences.AsNoTracking()
                                                           .Where(c => c.DeliveryCostID == model.DeliveryCostID)
                                                           .FirstOrDefault();

                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                return(oldConfig);
            }
        }
        public static PostalFee SaveModel(PostalFee model)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                PostalFee oldFee = db.PostalFees.AsNoTracking()
                                   .Where(c => c.Id == model.Id)
                                   .FirstOrDefault();

                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                return(oldFee);
            }
        }
Example #14
0
        public static SiteConfiguration SaveConfig(SiteConfiguration config)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                SiteConfiguration oldConfig = db.SiteConfigurations.AsNoTracking()
                                              .Where(c => c.Key == config.Key)
                                              .FirstOrDefault();

                db.Entry(config).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                return(oldConfig);
            }
        }
Example #15
0
        public static void RefundOrder(int id)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                CashOrder order = db.CashOrders.Find(id);

                if (order != null)
                {
                    order.Status          = "Refunded";
                    db.Entry(order).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }
        }
Example #16
0
        public static Special SaveModel(Special model)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                Special oldConfig = db.Specials.AsNoTracking()
                                    .Where(c => c.SpecialID == model.SpecialID)
                                    .FirstOrDefault();

                model.DateModified    = DateTime.Now;
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                return(oldConfig);
            }
        }
Example #17
0
        public static void RemoveStockFromCustodian(int orderId, int productNumber, int supplierNumber, int custodianNumber, int quantity)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                ProductCustodian custodian = db.ProductCustodians
                                             .Where(c => c.CustodianNumber == custodianNumber &&
                                                    c.ProductNumber == productNumber &&
                                                    c.SupplierNumber == supplierNumber)
                                             .FirstOrDefault();

                custodian.QuantityOnHand -= quantity;

                db.Entry(custodian).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }
Example #18
0
        public static void AddStock(int productNumber, int supplierNumber, int custodianNumber, int quantity)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                ProductCustodian custodian = db.ProductCustodians.Where(c => c.CustodianNumber == custodianNumber &&
                                                                        c.ProductNumber == productNumber &&
                                                                        c.SupplierNumber == supplierNumber)
                                             .FirstOrDefault();

                custodian.QuantityOnHand += quantity;
                custodian.DateLastIncreasedBySupplier   = DateTime.Now;
                custodian.AmountLastIncreasedBySupplier = quantity;

                db.Entry(custodian).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }
        public static void SetOrderConfirmedFromNotify(int orderNumber)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                OrderHeader order = db.OrderHeaders.Find(orderNumber);

                if (order == null)
                {
                    return;
                }

                order.OrderStatus     = "Confirmed";
                order.OrderDatePlaced = DateTime.Now;
                order.PaymentReceived = true;
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();

                ReleaseAllStock(order.OrderNumber);
            }
        }
        public void UpdatePrices()
        {
            if (Order != null && Order.OrderNumber != 0)
            {
                using (FreeMarketEntities db = new FreeMarketEntities())
                {
                    var differences = from oh in db.OrderHeaders
                                      join od in db.OrderDetails on oh.OrderNumber equals od.OrderNumber
                                      join ps in db.ProductSuppliers on new { od.ProductNumber, od.SizeType } equals new { ps.ProductNumber, ps.SizeType }
                    where oh.OrderNumber == Order.OrderNumber && ps.PricePerUnit != od.Price
                    select od;

                    if (!differences.Any())
                    {
                        Debug.Write(string.Format("No price difference found."));
                        return;
                    }

                    Debug.Write(string.Format("Updating prices for order {0} ...", Order.OrderNumber));

                    foreach (OrderDetail item in Body.OrderDetails)
                    {
                        var dbItem = db.OrderDetails.Find(item.ItemNumber);

                        if (dbItem != null)
                        {
                            dbItem.Price = db.ProductSuppliers
                                           .Find(item.ProductNumber, item.SupplierNumber, item.SizeType)
                                           .PricePerUnit;
                            db.Entry(dbItem).State = EntityState.Modified;
                        }
                    }

                    db.SaveChanges();
                }

                // Keep the OrderTotal in sync
                UpdateTotal();
            }
        }
        public void SetOrderConfirmed(string userId)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                OrderHeader order = db.OrderHeaders.Find(Order.OrderNumber);

                if (order == null)
                {
                    return;
                }

                order.OrderStatus     = "Confirmed";
                order.OrderDatePlaced = DateTime.Now;
                order.PaymentReceived = true;
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();

                ReleaseAllStock(order.OrderNumber);

                OrderHeader.UpdatePopularItems(order.OrderNumber);

                Initialize(userId);
            }
        }
Example #22
0
        public static void SaveExternalWebsite(ExternalWebsite website)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                try
                {
                    if (website.Url.StartsWith("http://") || website.Url.StartsWith("https://"))
                    {
                    }
                    else
                    {
                        website.Url = website.Url.Insert(0, "http://");
                    }

                    website.Department      = int.Parse(website.SelectedDepartment);
                    db.Entry(website).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    ExceptionLogging.LogException(e);
                }
            }
        }
Example #23
0
        public static FreeMarketObject ModifyOrder(CashOrderViewModel model)
        {
            FreeMarketObject result = new FreeMarketObject {
                Result = FreeMarketResult.NoResult, Argument = null, Message = null
            };

            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                CashCustomer customer = db.CashCustomers.Find(model.Order.CashCustomerId);

                customer.DeliveryAddress = model.Order.CustomerDeliveryAddress;
                customer.Email           = model.Order.CustomerEmail;
                customer.Name            = model.Order.CustomerName;
                customer.PhoneNumber     = model.Order.CustomerPhone;

                db.Entry(customer).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                CashOrder order = db.CashOrders.Find(model.Order.OrderId);

                List <GetCashOrderDetails_Result> orderDetails = db.GetCashOrderDetails(order.OrderId).ToList();

                foreach (Product p in model.Products.Products)
                {
                    if (p.CashQuantity > 0)
                    {
                        decimal price = decimal.Parse(p.SelectedPrice);

                        if (orderDetails.Any(c => c.ProductNumber == p.ProductNumber && c.SupplierNumber == p.SupplierNumber))
                        {
                            CashOrderDetail existingDetail = db.CashOrderDetails
                                                             .Where(c => c.CashOrderId == order.OrderId && c.ProductNumber == p.ProductNumber && c.SupplierNumber == p.SupplierNumber)
                                                             .FirstOrDefault();
                            existingDetail.Price          = price;
                            existingDetail.OrderItemTotal = price * p.CashQuantity;

                            if (existingDetail.Quantity > p.CashQuantity)
                            {
                                int stock = existingDetail.Quantity - p.CashQuantity;
                                AddStockToCustodian(order.OrderId, p.ProductNumber, p.SupplierNumber, model.SelectedCustodian, stock);
                            }
                            else
                            {
                                int stock = p.CashQuantity - existingDetail.Quantity;
                                RemoveStockFromCustodian(order.OrderId, p.ProductNumber, p.SupplierNumber, model.SelectedCustodian, stock);
                            }

                            existingDetail.Quantity = p.CashQuantity;

                            db.Entry(existingDetail).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                        }
                        else
                        {
                            CashOrderDetail detail = new CashOrderDetail
                            {
                                CashOrderId     = order.OrderId,
                                ProductNumber   = p.ProductNumber,
                                SupplierNumber  = p.SupplierNumber,
                                Quantity        = p.CashQuantity,
                                Price           = price,
                                OrderItemTotal  = price * p.CashQuantity,
                                CustodianNumber = model.SelectedCustodian
                            };

                            db.CashOrderDetails.Add(detail);
                            db.SaveChanges();

                            RemoveStockFromCustodian(order.OrderId, p.ProductNumber, p.SupplierNumber, model.SelectedCustodian, p.CashQuantity);
                        }
                    }
                    else
                    {
                        CashOrderDetail toRemove = db.CashOrderDetails
                                                   .Where(c => c.CashOrderId == order.OrderId && c.ProductNumber == p.ProductNumber && c.SupplierNumber == p.SupplierNumber)
                                                   .FirstOrDefault();

                        if (toRemove != null)
                        {
                            db.CashOrderDetails.Remove(toRemove);

                            AddStockToCustodian(order.OrderId, p.ProductNumber, p.SupplierNumber, model.SelectedCustodian, toRemove.Quantity);
                        }
                    }
                }

                db.SaveChanges();

                List <GetCashOrderDetails_Result> details = db.GetCashOrderDetails(order.OrderId).ToList();
                order.Total           = details.Sum(c => c.OrderItemTotal);
                order.DatePlaced      = DateTime.Now;
                db.Entry(order).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                if (customer != null && order != null && db.CashOrderDetails.Any(c => c.CashOrderId == order.OrderId))
                {
                    result.Result = FreeMarketResult.Success;
                }
                else
                {
                    result.Result = FreeMarketResult.Failure;
                }
            }

            return(result);
        }
Example #24
0
        public static FreeMarketResult SaveDepartmentImage(int departmentNumber, PictureSize size, HttpPostedFileBase image)
        {
            if (image != null && departmentNumber != 0)
            {
                using (FreeMarketEntities db = new FreeMarketEntities())
                {
                    DepartmentPicture picture = new DepartmentPicture();

                    Department department = db.Departments.Find(departmentNumber);

                    if (department == null)
                    {
                        return(FreeMarketResult.Failure);
                    }

                    picture = db.DepartmentPictures
                              .Where(c => c.DepartmentNumber == departmentNumber && c.Dimensions == size.ToString())
                              .FirstOrDefault();

                    try
                    {
                        // No picture exists for this dimension/product
                        if (picture == null)
                        {
                            picture            = new DepartmentPicture();
                            picture.Picture    = new byte[image.ContentLength];
                            picture.Annotation = department.DepartmentName;
                            picture.Dimensions = size.ToString();
                            image.InputStream.Read(picture.Picture, 0, image.ContentLength);
                            picture.PictureMimeType  = image.ContentType;
                            picture.DepartmentNumber = departmentNumber;

                            db.DepartmentPictures.Add(picture);
                            db.SaveChanges();

                            AuditUser.LogAudit(9, string.Format("Department: {0}", departmentNumber));
                            return(FreeMarketResult.Success);
                        }
                        else
                        {
                            picture.Annotation = department.DepartmentName;
                            picture.Picture    = new byte[image.ContentLength];
                            image.InputStream.Read(picture.Picture, 0, image.ContentLength);
                            picture.PictureMimeType = image.ContentType;

                            db.Entry(picture).State = EntityState.Modified;
                            db.SaveChanges();

                            AuditUser.LogAudit(9, string.Format("Department: {0}", departmentNumber));
                            return(FreeMarketResult.Success);
                        }
                    }
                    catch (Exception e)
                    {
                        ExceptionLogging.LogException(e);
                        return(FreeMarketResult.Failure);
                    }
                }
            }

            return(FreeMarketResult.Failure);
        }
Example #25
0
        public static FreeMarketObject CreateNewCashOrder(CashOrderViewModel model)
        {
            FreeMarketObject result = new FreeMarketObject {
                Result = FreeMarketResult.NoResult, Argument = null, Message = null
            };

            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                CashCustomer customer = db.CashCustomers.Find(model.Order.CashCustomerId);

                if (customer == null)
                {
                    customer = new CashCustomer
                    {
                        DeliveryAddress = model.Order.CustomerDeliveryAddress,
                        Email           = model.Order.CustomerEmail,
                        Name            = model.Order.CustomerName,
                        PhoneNumber     = model.Order.CustomerPhone
                    };

                    db.CashCustomers.Add(customer);
                    db.SaveChanges();
                }
                else
                {
                    customer.DeliveryAddress = model.Order.CustomerDeliveryAddress;
                    customer.Email           = model.Order.CustomerEmail;
                    customer.Name            = model.Order.CustomerName;
                    customer.PhoneNumber     = model.Order.CustomerPhone;

                    db.Entry(customer).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }

                CashOrder order = new CashOrder
                {
                    CashCustomerId = customer.Id,
                    DatePlaced     = DateTime.Now,
                    Status         = "Completed",
                    Total          = 0
                };

                db.CashOrders.Add(order);
                db.SaveChanges();

                foreach (Product p in model.Products.Products)
                {
                    if (p.CashQuantity > 0)
                    {
                        decimal         price  = decimal.Parse(p.SelectedPrice);
                        CashOrderDetail detail = new CashOrderDetail
                        {
                            CashOrderId     = order.OrderId,
                            ProductNumber   = p.ProductNumber,
                            SupplierNumber  = p.SupplierNumber,
                            Quantity        = p.CashQuantity,
                            Price           = price,
                            OrderItemTotal  = price * p.CashQuantity,
                            CustodianNumber = model.SelectedCustodian
                        };

                        db.CashOrderDetails.Add(detail);
                        db.SaveChanges();

                        order.Total += detail.OrderItemTotal;

                        db.Entry(order).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();

                        ProductCustodian custodian = db.ProductCustodians
                                                     .Where(c => c.CustodianNumber == model.SelectedCustodian &&
                                                            c.ProductNumber == p.ProductNumber &&
                                                            c.SupplierNumber == p.SupplierNumber)
                                                     .FirstOrDefault();

                        custodian.QuantityOnHand -= p.CashQuantity;

                        db.Entry(custodian).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                }

                db.SaveChanges();

                if (customer != null && order != null && db.CashOrderDetails.Any(c => c.CashOrderId == order.OrderId))
                {
                    result.Result = FreeMarketResult.Success;
                }
                else
                {
                    result.Result = FreeMarketResult.Failure;
                }
            }

            return(result);
        }
Example #26
0
        public static void SaveProduct(Product product)
        {
            Product productDb = new Product();
            List <ProductSupplier> productSupplierDb = new List <ProductSupplier>();

            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                productDb = db.Products.Find(product.ProductNumber);

                if (productDb != null)
                {
                    productDb.Activated        = product.Activated;
                    productDb.DateModified     = DateTime.Now;
                    productDb.DepartmentNumber = int.Parse(product.SelectedDepartment);
                    productDb.Description      = product.Description;
                    productDb.LongDescription  = product.LongDescription;
                    productDb.Size             = product.Size;
                    productDb.Weight           = product.Weight;
                    productDb.IsVirtual        = product.IsVirtual;
                    db.Entry(productDb).State  = EntityState.Modified;
                    db.SaveChanges();
                }

                // For each productsize entry on the form
                foreach (ProductSize x in product.SizeVariations)
                {
                    // Query the productsupplier table to get a record
                    ProductSupplier temp = db.ProductSuppliers
                                           .Where(c => c.ProductNumber == product.ProductNumber &&
                                                  c.SupplierNumber == product.SupplierNumber && c.SizeType == x.SizeId)
                                           .FirstOrDefault();

                    // If a record does not exist
                    if (temp == null)
                    {
                        // Check the activated field of the form and add a new record if it is true
                        if (x.Activated == true)
                        {
                            ProductSupplier prodSup = new ProductSupplier
                            {
                                PricePerUnit   = x.PricePerUnit,
                                ProductNumber  = product.ProductNumber,
                                SupplierNumber = product.SupplierNumber,
                                SizeType       = x.SizeId,
                            };

                            db.ProductSuppliers.Add(prodSup);
                            db.SaveChanges();
                        }
                    }

                    // If a record does exist
                    else
                    {
                        // Check the activated field and remove it if it is false
                        if (x.Activated == false)
                        {
                            db.ProductSuppliers.Remove(temp);
                            db.SaveChanges();
                        }
                        else
                        {
                            // If the prices differ, record it in the pricehistory table
                            if (x.PricePerUnit != temp.PricePerUnit)
                            {
                                PriceHistory history = new PriceHistory()
                                {
                                    OldPrice       = temp.PricePerUnit,
                                    NewPrice       = x.PricePerUnit,
                                    ProductNumber  = product.ProductNumber,
                                    SupplierNumber = product.SupplierNumber,
                                    Date           = DateTime.Now,
                                    Type           = x.SizeId.ToString()
                                };

                                db.PriceHistories.Add(history);

                                // Update the record
                                temp.PricePerUnit    = x.PricePerUnit;
                                db.Entry(temp).State = EntityState.Modified;

                                db.SaveChanges();
                            }
                        }
                    }
                }
            }
        }