//
        // GET: /ProductAttachmentType/Create
        public ActionResult Create()
        {
            SetViewBagData();

            ProductAttachmentType productAttachmentType = new ProductAttachmentType();
            productAttachmentType.OnCreate();

            return View(productAttachmentType);
        }
        public ActionResult Create(ProductAttachmentType productAttachmentType)
        {
            if (ModelState.IsValid)
            {
                // Add Audit Entry
                AuditTrail audit = new AuditTrail(DateTime.Now, User.Identity.Name, productAttachmentType, productAttachmentType.ID, "Create");
                db.AuditTrails.Add(audit);

                db.ProductAttachmentTypes.Add(productAttachmentType);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            SetViewBagData(productAttachmentType);
            return View(productAttachmentType);
        }
        // ProductAttachmentType
        public AuditTrail(DateTime dateTime, string userName, ProductAttachmentType productAttachmentType, int id, string comment)
        {
            this.AuditTrailTimeStamp = dateTime;
            this.AuditTrailUserName = userName;
            this.AuditTrailComment = comment;

            if (id > 0)
            {
                this.ProductAttachmentTypeID = id;
            }
            else
            {
                this.ProductAttachmentType = productAttachmentType;
            }
        }
        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);
        }
        private void SetViewBagData(ProductAttachmentType productAttachmentType = null)
        {
            var attachmentTypeList = db.AttachmentTypes.Where(a => a.Status != archived);
            var productList = db.Products.Where(p => p.ProductStatus != archived &&
                                                    p.Campaign.CampaignStatus != archived &&
                                                    p.Campaign.Company.CompanyStatus != archived);

            if (productAttachmentType != null)
            {
                ViewBag.AttachmentTypes = new SelectList(attachmentTypeList, "ID", "TypeName", productAttachmentType.AttachmentTypeID);
                ViewBag.Products = new SelectList(productList, "ProductID", "ProductName", productAttachmentType.ProductID);
            }
            else
            {
                ViewBag.AttachmentTypes = new SelectList(attachmentTypeList, "ID", "TypeName");
                ViewBag.Products = new SelectList(productList, "ProductID", "ProductName");
            }
        }