//
        // GET: /ProductSellPrice/Create
        public ActionResult Create()
        {
            var list = db.Products.Where(p => p.ProductStatus != archived &&
                                                p.Campaign.CampaignStatus != archived &&
                                                p.Campaign.Company.CompanyStatus != archived);
            ViewBag.ProductID = new SelectList(list, "ProductID", "ProductName");

            // Generate Decoration method for member initialization
            ProductSellPrice productSellPrice = new ProductSellPrice();
            productSellPrice.OnCreate();

            return View(productSellPrice);
        }
        public ActionResult Create(ProductSellPrice productsellprice)
        {
            if (ModelState.IsValid)
            {
                db.ProductSellPrices.Add(productsellprice);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            var list = db.Products.Where(p => p.ProductStatus != archived &&
                                                p.Campaign.CampaignStatus != archived &&
                                                p.Campaign.Company.CompanyStatus != archived);
            ViewBag.ProductID = new SelectList(list, "ProductID", "ProductName", productsellprice.ProductID);
            return View(productsellprice);
        }
 // For sell prices
 public Fee(int feeNameID, ProductSellPrice sellPrice, string feeType, string feeCalculation, decimal? feeDollarAmount, decimal? feeAmortizedCharge, string feeAmortizedType, decimal? feePercent, string feePercentType, int inheritedID)
 {
     this.ProductSellPrice = sellPrice;
     this.FeeType = feeType;
     this.FeeCalculation = feeCalculation;
     this.FeeNameID = feeNameID;
     this.FeeDollarAmount = feeDollarAmount;
     this.FeeAmortizedCharge = feeAmortizedCharge;
     this.FeeAmortizedType = feeAmortizedType;
     this.FeePercent = feePercent;
     this.FeePercentType = feePercentType;
     this.FeeInherited = true;
     this.FeeInheritedFeeID = inheritedID;
     this.FeeLevel = 0;
     this.AuditTrails = new HashSet<AuditTrail>();
     this.FeeStatus = MyExtensions.GetEnumDescription(Status.Active);
 }
        // ProductSellPrice
        public AuditTrail(DateTime dateTime, string userName, ProductSellPrice productSellPrice, int id, string comment)
        {
            this.AuditTrailTimeStamp = dateTime;
            this.AuditTrailUserName = userName;
            this.AuditTrailComment = comment;

            if(id > 0)
            {
                this.SellPriceID = id;
            }
            else
            {
                this.ProductSellPrice = productSellPrice;
            }
        }
        public ActionResult Create([Bind(Exclude = "ProductImage")]Product product, HttpPostedFileBase ProductImage, string returnUrl)
        {
            if (product.ProductInitialOrderQuantity == null)
            {
                product.ProductInitialOrderQuantity = (decimal?)0;
            }
            if (product.ProductGatewayCDIMinumumOrder == null)
            {
                product.ProductGatewayCDIMinumumOrder = (decimal?)0;
            }
            if (ModelState.IsValid)
            {
                // Get product's campaign and loop though that campaigns, companies price tiers
                Campaign thisCampaign = db.Campaigns.Where(c => c.CampaignID == product.CampaignID).FirstOrDefault();
                foreach (var tier in thisCampaign.Company.PricingTiers.Where(p => p.PricingTierStatus != MyExtensions.GetEnumDescription(Status.Archived)))
                {
                    // Create a new sell price for each price tier and add to DB
                    ProductSellPrice newSellPrice = new ProductSellPrice(product, tier.PricingTierName, tier.PricingTierLevel, (decimal)thisCampaign.Company.CompanyDefaultMargin);
                    db.ProductSellPrices.Add(newSellPrice);

                    // Attach fees
                    foreach (var fee in tier.Fees.Where(f => f.FeeStatus != MyExtensions.GetEnumDescription(Status.Archived)))
                    {
                        Fee newFee = new Fee(fee.FeeNameID, newSellPrice, fee.FeeType, fee.FeeCalculation, fee.FeeDollarAmount, fee.FeeAmortizedCharge, fee.FeeAmortizedType, fee.FeePercent, fee.FeePercentType, fee.FeeID);
                        db.Fees.Add(newFee);
                    }
                }

                // Loop though Attachment Types and add a ProductAttachmentType for each Attachment Type
                foreach (var attachmentType in db.AttachmentTypes.Where(a => a.Status != archived).OrderBy(a => a.TypeName))
                {
                    // Create a Product Attament type
                    ProductAttachmentType newProductAttachmentType = new ProductAttachmentType(product.ProductID, attachmentType.ID);
                    db.ProductAttachmentTypes.Add(newProductAttachmentType);
                }

                // Product Image
                if (ProductImage != null && ProductImage.ContentLength > 0)
                {
                    byte[] imageBinaryData = new byte[ProductImage.ContentLength];
                    int readresult = ProductImage.InputStream.Read(imageBinaryData, 0, ProductImage.ContentLength);
                    product.ProductImage = imageBinaryData;
                    product.ProductImageType = ProductImage.ContentType;
                }

                // Add Audit Entry
                AuditTrail audit = new AuditTrail(DateTime.Now, User.Identity.Name, product, product.ProductID, "Create");
                db.AuditTrails.Add(audit);

                db.Products.Add(product);
                db.SaveChanges();

                if (returnUrl == null)
                {
                    return RedirectToAction("Index");
                }
                return RedirectToAction("Edit", new { id = product.ProductID, ReturnUrl = returnUrl });
            }

            // Sets Viewbag data for dropdowns
            SetViewBagData(returnUrl, product);

            return View(product);
        }