public void Calculate_Product_Net_Cost_With_Amortized_And_Dollar_Amount_Fee()
        {
            var product = new Product() { ProductCost = 1, ProductAnnualSalesProjection = 100 };
            product.Fees = new List<Fee>();
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = 1 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Amortized), FeeAmortizedCharge = 100, FeeAmortizedType = MyExtensions.GetEnumDescription(AmortizedTypeList.Annual_Sales_Projection)});

            var feeCalculator = new FeeCalculator(product);
            feeCalculator.ComputeNetCost();

            Assert.AreEqual(3, product.ProductNetCost);
        }
        public void Calculate_Product_Sell_Prices_And_Fees_With_Division_And_Multiplication_Percent_Amortized_And_Dollar_Amount_Fee()
        {
            var product = new Product() { ProductCost = 1, ProductAnnualSalesProjection = 100 };
            product.Fees = new List<Fee>();
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = 1 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Amortized), FeeAmortizedCharge = 100, FeeAmortizedType = MyExtensions.GetEnumDescription(AmortizedTypeList.Annual_Sales_Projection) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Division) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
            product.ProductSellPrices = new List<ProductSellPrice>();
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Employee", SellPriceMarginPercent = 50, SellPriceLevel = 1 });
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Retail", SellPriceMarginPercent = 50, SellPriceLevel = 2 });

            foreach(var sellPrice in product.ProductSellPrices)
            {
                sellPrice.Fees = new List<Fee>();
                if(sellPrice.SellPriceName == "Retail")
                {
                    sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 20, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
                }
            }

            var feeCalculator = new FeeCalculator(product);
            feeCalculator.ComputeNetCost();

            Assert.AreEqual(3, product.ProductNetCost);
            feeCalculator.ComputeTotalCost();

            // Created to get same value though division
            decimal value = ((decimal)3 / (decimal).9) * (decimal)1.1;
            value = feeCalculator.RoundNumberToDecimalPlace(value, 3);
            Assert.AreEqual(value, product.ProductTotalCost);

            // New Part of Test
            feeCalculator.ComputeSellPrices(false);

            foreach(var sellPrice in product.ProductSellPrices)
            {
                decimal sellPriceValue = value / (decimal)(.50);
                foreach(var fee in sellPrice.Fees)
                {
                    if(fee.FeePercentType == MyExtensions.GetEnumDescription(PercentTypeList.Multiplication))
                    {
                        sellPriceValue = sellPriceValue * (decimal)1.2;
                    }
                    else
                    {
                        sellPriceValue = sellPriceValue / (decimal).8;
                    }
                }

                Assert.AreEqual(feeCalculator.RoundNumberToDecimalPlace(sellPriceValue, 3), sellPrice.SellPriceFinalAmount);
            }
        }
        public void Calculate_Product_Net_Cost_With_Dollar_Amount_Fee()
        {
            var product = new Product();
            product.ProductCost = (decimal)1;
            product.Fees = new List<Fee>();
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = 1 });

            var feeCalculator = new FeeCalculator(product);
            feeCalculator.ComputeNetCost();

            Assert.AreEqual(1, product.ProductCost);

            Assert.AreEqual(2, product.ProductNetCost);
        }
 //non-generated Costructor
 public ProductSellPrice(Product product, string name, int level, decimal defaultMargin)
 {
     // TODO: Complete member initialization
     this.Product = product;
     this.SellPriceName = name;
     this.SellPriceLevel = level;
     this.SellPriceMarginPercent = defaultMargin;
     this.AuditTrails = new HashSet<AuditTrail>();
     this.SellPriceStatus = MyExtensions.GetEnumDescription(Status.Active);
 }
        // Product
        public AuditTrail(DateTime dateTime, string userName, Product product, int id, string comment)
        {
            this.AuditTrailTimeStamp = dateTime;
            this.AuditTrailUserName = userName;
            this.AuditTrailComment = comment;

            if(id > 0)
            {
                this.ProductID = id;
            }
            else
            {
                this.Product = product;
            }
        }
        public ActionResult SaveAndCalculateSellPriceToNearestFiveCents(Product product, HttpPostedFileBase ProductImage, HttpPostedFileBase DecorationImage, IEnumerable<HttpPostedFileBase> Documents, string returnUrl)
        {
            // Sets Viewbag data for dropdowns
            SetViewBagData(returnUrl, product);
            string preSaveStatus = db.Products.Where(p => p.ProductID == product.ProductID).Select(p => p.ProductStatus).FirstOrDefault();

            if (ModelState.IsValid)
            {
                int idIndex = -100;
                foreach (var fee in product.Fees)
                {
                    // IF it's a new fee
                    if (fee.FeeID <= 0)
                    {
                        fee.FeeID = idIndex;
                        idIndex++;
                    }
                }
                foreach (var upcharge in product.ProductUpcharges)
                {
                    if (upcharge.UpchargeID <= 0)
                    {
                        upcharge.UpchargeID = idIndex;
                        idIndex++;
                    }
                }

                foreach (var productDocument in product.ProductDocuments)
                {
                    // if it's a new document
                    if (productDocument.ID <= 0)
                    {
                        productDocument.ID = idIndex;
                        idIndex++;
                    }
                }

                db.Entry(product).State = EntityState.Modified;

                // Remove Fees
                var Fees = product.Fees.ToList();
                foreach (var fee in db.Fees.Where(p => p.ProductID == product.ProductID))
                {
                    if (!Fees.Contains(fee))
                    {
                        fee.FeeStatus = MyExtensions.GetEnumDescription(Status.Archived);
                        db.Entry(fee).State = EntityState.Modified; ;
                    }
                }

                // Remove Upcharges
                var Upcharges = product.ProductUpcharges.ToList();
                foreach (var upcharge in db.ProductUpcharges.Where(p => p.ProductID == product.ProductID))
                {
                    if (!Upcharges.Contains(upcharge))
                    {
                        upcharge.UpchargeStatus = MyExtensions.GetEnumDescription(Status.Archived);
                        db.Entry(upcharge).State = EntityState.Modified; ;
                    }
                }

                // Add/Update Fees
                foreach (var fee in product.Fees)
                {
                    // IF it's a new fee
                    if (fee.FeeID <= 0)
                    {
                        // Create a new Fee
                        db.Fees.Add(fee);
                    }
                    else
                    {
                        // Else update existing Fee
                        db.Entry(fee).State = EntityState.Modified;
                    }
                }

                // update  SellPriceFees
                foreach (var sellPrice in product.ProductSellPrices)
                {
                    // Update sellprice
                    db.Entry(sellPrice).State = EntityState.Modified;

                    //TODO: add/update/remove SellPriceFees
                    foreach (var fee in sellPrice.Fees)
                    {
                        if (fee.FeeID <= 0)
                        {
                            db.Fees.Add(fee);
                        }
                        else
                        {
                            db.Entry(fee).State = EntityState.Modified;
                        }
                    }
                }

                // Document
                // Remove
                var productDocuments = product.ProductDocuments.ToList();
                foreach (var productDocument in db.ProductDocuments.Where(p => p.ProductID == product.ProductID))
                {
                    if (!productDocuments.Contains(productDocument))
                    {
                        productDocument.Status = archived;
                        db.Entry(productDocument).State = EntityState.Modified;
                    }
                }
                //Files
                if (Documents != null)
                {
                    for (int index = 0; index < Documents.Count(); index++)
                    {
                        var productDocument = product.ProductDocuments.Where(p => p.Status != archived).ElementAt(product.ProductDocuments.Where(p => p.Status != archived).Count() - 1 - index);
                        var Document = Documents.ElementAt(index);
                        // file
                        if (Document != null && Document.ContentLength > 0)
                        {
                            byte[] documentBinaryData = new byte[Document.ContentLength];
                            int readresult = Document.InputStream.Read(documentBinaryData, 0, Document.ContentLength);
                            productDocument.Document = documentBinaryData;
                            productDocument.DocumentFileType = Document.ContentType;
                            productDocument.DocumentFileName = Document.FileName;
                        }
                    }
                }
                // Add/Update
                foreach (var productDocument in product.ProductDocuments)
                {
                    // IF it's a new productDocument
                    if (productDocument.ID <= 0)
                    {
                        // Create a new productDocument
                        db.ProductDocuments.Add(productDocument);
                    }
                    else
                    {
                        // Else update existing productDocument
                        db.Entry(productDocument).State = EntityState.Modified;
                    }
                }

                // update ProductAttachmentTypes
                foreach (var productAttachmentType in product.ProductUpcharges)
                {
                    db.Entry(productAttachmentType).State = EntityState.Modified;
                }

                // update Upcharges
                foreach (var upcharge in product.ProductUpcharges)
                {
                    // IF it's a new Upcharge
                    if (upcharge.UpchargeID <= 0)
                    {
                        // Create a new Upcharge
                        db.ProductUpcharges.Add(upcharge);
                        Product thisProduct = db.Products.Where(p => p.ProductID == product.ProductID).FirstOrDefault();
                        foreach (var sellPrice in thisProduct.ProductSellPrices)
                        {
                            UpchargeSellPrice newUpchargeSellPrice = new UpchargeSellPrice(upcharge, sellPrice.SellPriceName, sellPrice.SellPriceLevel);
                            db.UpchargeSellPrices.Add(newUpchargeSellPrice);
                        }
                    }
                    else
                    {
                        // Else update existing Fee
                        db.Entry(upcharge).State = EntityState.Modified;
                    }

                    //TODO: add/update/remove SellPriceFees
                    foreach (var upchargeSellPrice in upcharge.UpchargeSellPrices)
                    {
                        if (upchargeSellPrice.UpchargeSellPriceID <= 0)
                        {
                            db.UpchargeSellPrices.Add(upchargeSellPrice);
                        }
                        else
                        {
                            db.Entry(upchargeSellPrice).State = EntityState.Modified;
                        }
                    }
                }

                // Decorations
                // Remove
                var decorations = product.ProductDecorations.ToList();
                foreach (var decoration in db.ProductDecorations.Where(p => p.ProductID == product.ProductID))
                {
                    if (!decorations.Contains(decoration))
                    {
                        decoration.DecorationStatus = MyExtensions.GetEnumDescription(Status.Archived);
                        db.Entry(decoration).State = EntityState.Modified;
                    }
                }

                // Add/Update
                foreach (var decoration in product.ProductDecorations)
                {
                    if (DecorationImage != null && DecorationImage.ContentLength > 0)
                    {
                        byte[] imageBinaryData = new byte[DecorationImage.ContentLength];
                        int readresult = DecorationImage.InputStream.Read(imageBinaryData, 0, DecorationImage.ContentLength);
                        decoration.DecorationImage = imageBinaryData;
                        decoration.DecorationImageType = DecorationImage.ContentType;
                    }

                    // IF it's a new fee
                    if (decoration.DecorationID <= 0)
                    {
                        // Create a new Fee
                        db.ProductDecorations.Add(decoration);
                    }
                    else
                    {
                        // Else update existing Fee
                        db.Entry(decoration).State = EntityState.Modified;
                    }
                }

                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;
                }

                // Calculator
                Helpers.FeeCalculator newCalculator = new Helpers.FeeCalculator(product);
                try
                {
                    newCalculator.ComputeAllProductPrices(true);
                    newCalculator.ComputeMarginBasedOnSellprice();
                }
                catch (Exception ex)
                {
                    Console.Write("ProductController.cs SaveAndCalculateSellPriceToNearestFiveCents() ComputeAllProductPrices() failure. Exception: " + ex.ToString());
                }

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

                // Send Emails
                #region SendEmails
                // Check previous status
                if (preSaveStatus != product.ProductStatus)
                {
                    List<EmailTo> sendEmailTos = MyExtensions.GetEmailTo(product.ProductStatus);
                    var urlBuilder = new System.UriBuilder(Request.Url.AbsoluteUri) { Path = Url.Action("Edit", "Product") + "/" + product.ProductID, Query = null, };
                    var campaign = db.Campaigns.Where(c => c.CampaignID == product.CampaignID).FirstOrDefault();
                    if (sendEmailTos != null && sendEmailTos.Count > 0)
                    {
                        UserMailer.SendStatusUpdate(sendEmailTos, "Product Updated by: " + MyExtensions.DisplayPrintFriendlyName(User.Identity.Name), urlBuilder.ToString(), db.Companies.Where(c => c.CompanyID == campaign.CompanyID).FirstOrDefault(), campaign, product).Send();
                    }
                }
                #endregion

                db.SaveChanges();

                return RedirectToAction("Edit", new { id = product.ProductID, ReturnUrl = returnUrl });
            }
            else
            {
                int count = 0;
                foreach (var modelStateVal in ModelState.Values)
                {
                    foreach (var error in modelStateVal.Errors)
                    {
                        var errorMessage = error.ErrorMessage;
                        var exception = error.Exception;
                        // You may log the errors if you want
                    }
                    count++;
                }
            }
            // Error
            product.Campaign = db.Campaigns.Where(c => c.CampaignID == product.CampaignID)
                                            .FirstOrDefault();
            return View("Edit", product);
        }
 public ActionResult Close(Product product, HttpPostedFileBase ProductImage, string returnUrl)
 {
     if (returnUrl == null)
     {
         return RedirectToAction("Index");
     }
     return Redirect(returnUrl);
 }
        // Set ViewBag Data
        public void SetViewBagData(string returnUrl, Product product = null)
        {
            ViewBag.ReturnUrl = returnUrl;

            if (product != null)
            {
                var list = db.Campaigns.Where(c => c.CampaignStatus != archived &&
                                                    c.Company.CompanyStatus != archived);

                ViewBag.Campaigns = new SelectList(list, "CampaignID", "CampaignName", product.CampaignID);
                ViewBag.PackagingTypes = new SelectList(db.PackagingTypes.Where(p => p.PackagingTypeStatus != archived).OrderBy(p => p.PackagingTypeName), "PackagingTypeID", "PackagingTypeName", product.PackagingTypeID);
                ViewBag.VendorNames = new SelectList(db.VendorNames.Where(v => v.VendorNameStatus != archived).OrderBy(v => v.VendorNameName), "VendorNameID", "VendorNameName", product.VendorNameID);
                ViewBag.VendorTypes = new SelectList(db.VendorTypes.Where(v => v.VendorTypeStatus != archived), "VendorTypeID", "VendorTypeName", product.VendorTypeID);
                ViewBag.FeeNames = new SelectList(db.FeeNames.Where(f => f.FeeNameStatus != archived), "FeeNameID", "FeeNameName", 0);
                ViewBag.MajorCategories = new SelectList(db.Categories.Where(c => c.CategoryStatus != archived && c.CategoryType == "Major").OrderBy(c => c.CategoryName), "CategoryID", "CategoryName", product.MajorCategoryID);
                ViewBag.MinorCategories = new SelectList(db.Categories.Where(c => c.CategoryStatus != archived && c.CategoryType == "Minor").OrderBy(c => c.CategoryName), "CategoryID", "CategoryName", product.MinorCategoryID);
                ViewBag.DecorationMethodsDB = db.DecorationMethods.Where(d => d.DecorationMethodStatus != archived).OrderBy(d => d.DecorationMethodName);
            }

            else if (product == null)
            {
                ViewBag.ReturnUrl = returnUrl;
                var campaignList = db.Campaigns.Where(c => c.CampaignStatus != archived &&
                                                    c.Company.CompanyStatus != archived);

                ViewBag.Campaigns = new SelectList(campaignList, "CampaignID", "CampaignName");
                ViewBag.PackagingTypes = new SelectList(db.PackagingTypes.Where(p => p.PackagingTypeStatus != archived).OrderBy(p => p.PackagingTypeName), "PackagingTypeID", "PackagingTypeName");
                ViewBag.VendorNames = new SelectList(db.VendorNames.Where(v => v.VendorNameStatus != archived).OrderBy(v => v.VendorNameName), "VendorNameID", "VendorNameName");
                ViewBag.VendorTypes = new SelectList(db.VendorTypes.Where(v => v.VendorTypeStatus != archived), "VendorTypeID", "VendorTypeName");
                ViewBag.FeeNames = new SelectList(db.FeeNames.Where(f => f.FeeNameStatus != archived), "FeeNameID", "FeeNameName", 0);
                ViewBag.MajorCategories = new SelectList(db.Categories.Where(c => c.CategoryStatus != archived && c.CategoryType == "Major").OrderBy(c => c.CategoryName), "CategoryID", "CategoryName");
                ViewBag.MinorCategories = new SelectList(db.Categories.Where(c => c.CategoryStatus != archived && c.CategoryType == "Minor").OrderBy(c => c.CategoryName), "CategoryID", "CategoryName");
                ViewBag.DecorationMethodsDB = db.DecorationMethods.Where(d => d.DecorationMethodStatus != archived).OrderBy(d => d.DecorationMethodName);
            }
        }
示例#9
0
        public virtual MvcMailMessage SendStatusUpdate(List<EmailTo> emailTos, string title, string link, Company company = null, Campaign campaign = null, Product product = null)
        {
            string subject = "";// Check if we have an object
            if(product != null)
            {
                subject = company.CompanyName + ": Product Status Update " + DateTime.Now.ToString("f") + " : " + product.ProductName;
                ViewBag.Type = "Product";
                ViewBag.Name = product.ProductName;
                ViewBag.Status = product.ProductStatus;
                ViewBag.Link = link;

                ViewBag.CampaignData = "Campaign Name: " + campaign.CampaignName;
                ViewBag.CompanyData = "Company Name: " + company.CompanyName;
            }
            else if(campaign != null)
            {
                subject = company.CompanyName + ": Campaign Status Update - " + campaign.CampaignName;
                ViewBag.Type = "Campaign";
                ViewBag.Name = campaign.CampaignName;
                ViewBag.Status = campaign.CampaignStatus;
                ViewBag.Link = link;

                ViewBag.CompanyData = "Company Name: " + company.CompanyName;
            }
            else if(company != null)
            {
                subject = company.CompanyName + ": Company Status Update";
                ViewBag.Type = "Company";
                ViewBag.Name = company.CompanyName;
                ViewBag.Status = company.CompanyStatus;
                ViewBag.Link = link;
            }
            else
            {
                throw new System.ArgumentException("All Objects are Null", "UserMailer.cs");
            }

            return Populate(x =>
            {
                x.Subject = subject;
                x.ViewName = "SendStatusUpdate";
                foreach(EmailTo emailTo in emailTos)
                {
                    switch(emailTo)
                    {
                        case EmailTo.Account_Manager:
                            x.To.Add(company.CompanyAccountManagerEmail);
                            break;
                        case EmailTo.Inventory_Buyers:
                            x.To.Add("*****@*****.**");
                            break;
                        case EmailTo.Merchandisers:
                            x.To.Add("*****@*****.**");
                            break;
                        case EmailTo.Mentor:
                            x.To.Add(company.CompanyMentorEmail);
                            break;
                        default:
                            break;
                    }
                }
            });
        }
        public void Test_RoundNumberToDecimalPlace_Method()
        {
            var product = new Product();
            product.ProductCost = (decimal)1;
            var feeCalculator = new FeeCalculator(product);

            decimal test = (decimal).6849842136841245;
            Assert.AreEqual((decimal).68498, feeCalculator.RoundNumberToDecimalPlace(test, 5));

            Assert.AreEqual((decimal).685, feeCalculator.RoundNumberToDecimalPlace(test, 3));
        }
        public void Compute_Product_With_Upcharge()
        {
            // Data Input
            var product = new Product() { ProductCost = 1, ProductAnnualSalesProjection = 100 };
            product.ProductUpcharges = new List<ProductUpcharge>();
            ProductUpcharge upcharge1 = new ProductUpcharge();
            upcharge1.UpchargeAmount = 2;
            upcharge1.UpchargeSellPrices = new List<UpchargeSellPrice>();
            upcharge1.UpchargeSellPrices.Add(new UpchargeSellPrice() { UpchargeSellPriceName = "Employee", UpchargeSellPriceLevel = 1 });
            upcharge1.UpchargeSellPrices.Add(new UpchargeSellPrice() { UpchargeSellPriceName = "Retail", UpchargeSellPriceLevel = 2 });

            product.ProductUpcharges.Add(upcharge1);
            product.Fees = new List<Fee>();
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = 1 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Amortized), FeeAmortizedCharge = 100, FeeAmortizedType = MyExtensions.GetEnumDescription(AmortizedTypeList.Annual_Sales_Projection) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Division) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
            product.ProductSellPrices = new List<ProductSellPrice>();
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Employee", SellPriceMarginPercent = 50, SellPriceLevel = 1 });
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Retail", SellPriceMarginPercent = 50, SellPriceLevel = 2 });

            foreach(var sellPrice in product.ProductSellPrices)
            {
                sellPrice.Fees = new List<Fee>();
                if(sellPrice.SellPriceName == "Retail")
                {
                    sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 20, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
                }
            }

            // Run Calculator
            var feeCalculator = new FeeCalculator(product);
            feeCalculator.ComputeAllProductPrices(false);

            // Created to get same value though division
            decimal value = ((decimal)3 / (decimal).9) * (decimal)1.1;
            value = feeCalculator.RoundNumberToDecimalPlace(value, 3);
            Assert.AreEqual(value, product.ProductTotalCost);

            foreach(var sellPrice in product.ProductSellPrices)
            {
                decimal sellPriceValue = value / (decimal)(.50);
                foreach(var fee in sellPrice.Fees)
                {
                    if(fee.FeePercentType == MyExtensions.GetEnumDescription(PercentTypeList.Multiplication))
                    {
                        sellPriceValue = sellPriceValue * (decimal)1.2;
                    }
                    else
                    {
                        sellPriceValue = sellPriceValue / (decimal).8;
                    }
                }

                Assert.AreEqual(feeCalculator.RoundNumberToDecimalPlace(sellPriceValue, 3), sellPrice.SellPriceFinalAmount);
            }

            // Check Upcharge values
            foreach(var upcharge in product.ProductUpcharges)
            {
                // Created to get same value though division
                decimal newValue = (decimal)1;// cost
                newValue += 2; // upcharge
                newValue += 2; // dollar charges
                newValue = (decimal)(newValue / ((decimal)1 - (decimal)((decimal)10 / (decimal)100))); // divide
                newValue = (decimal)(((decimal)10 / (decimal)100) + (decimal)1) * newValue; // multiply
                newValue = feeCalculator.RoundNumberToDecimalPlace(newValue, 3);

                Assert.AreEqual(newValue, upcharge.UpchargeTotalCost);

                foreach(var upchargeSellPrice in upcharge.UpchargeSellPrices)
                {
                    decimal sellPriceValue = newValue / (decimal)(.50);
                    foreach(var fee in product.ProductSellPrices.Where(p => p.SellPriceLevel == upchargeSellPrice.UpchargeSellPriceLevel).FirstOrDefault().Fees)
                    {
                        if(fee.FeePercentType == MyExtensions.GetEnumDescription(PercentTypeList.Multiplication))
                        {
                            sellPriceValue = sellPriceValue * (decimal)1.2;
                        }
                        else
                        {
                            sellPriceValue = sellPriceValue / (decimal).8;
                        }
                    }
                    Assert.AreEqual(feeCalculator.RoundNumberToDecimalPlace(sellPriceValue, 3), upchargeSellPrice.UpchargeSellPriceFinalAmount);
                }
            }
        }
        public void Compute_Product_And_Calculate_Margin_Test_Product_With_Dollar_And_Amortized_SellPrice_Fees()
        {
            // Data Input
            var product = new Product() { ProductCost = (decimal)2.94, ProductGatewayCDIMinumumOrder = 144 };
            product.Fees = new List<Fee>();
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = (decimal).4 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = (decimal).6 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = (decimal).2 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = (decimal).09 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = (decimal).1 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = (decimal).7 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = (decimal).2 });

            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Amortized), FeeAmortizedCharge = 12, FeeAmortizedType = MyExtensions.GetEnumDescription(AmortizedTypeList.GatewayCDI_Minimum_Order) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = (decimal)7.33, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });

            product.ProductSellPrices = new List<ProductSellPrice>();
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Employee/Corporate", SellPriceMarginPercent = 35, SellPriceLevel = 1 });
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Dealer", SellPriceMarginPercent = 35, SellPriceLevel = 2 });
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Retail", SellPriceMarginPercent = 35, SellPriceLevel = 3 });

            foreach(var sellPrice in product.ProductSellPrices)
            {
                sellPrice.Fees = new List<Fee>();
                sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = (decimal)1.5, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
                if(sellPrice.SellPriceName == "Dealer")
                {
                    sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Division) });
                    sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = (decimal)2.3, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
                    sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = (decimal)2 });
                }

                if(sellPrice.SellPriceName == "Retail")
                {
                    sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = (decimal)8.89, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Division) });
                    sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 32, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Division) });
                }
            }

            // Run Calculator
            var feeCalculator = new FeeCalculator(product);
            feeCalculator.ComputeAllProductPrices(false);

            // Check Values
            Assert.AreEqual((decimal)5.703, feeCalculator.RoundNumberToDecimalPlace((decimal)product.ProductTotalCost, 3));

            foreach(var sellPrice in product.ProductSellPrices)
            {
                if(sellPrice.SellPriceName == "Employee/Corporate")
                {
                    Assert.AreEqual((decimal)8.906, sellPrice.SellPriceFinalAmount);
                }
                if(sellPrice.SellPriceName == "Dealer")
                {
                    Assert.AreEqual((decimal)12.123, sellPrice.SellPriceFinalAmount);
                }
                if(sellPrice.SellPriceName == "Retail")
                {
                    Assert.AreEqual((decimal)14.374, sellPrice.SellPriceFinalAmount);
                }
            }

            feeCalculator.ComputeMarginBasedOnSellprice();

            foreach(var sellPrice in product.ProductSellPrices)
            {
                if(sellPrice.SellPriceName == "Employee/Corporate")
                {
                    Assert.AreEqual((decimal)8.906, Math.Truncate((decimal)sellPrice.SellPriceFinalAmount * 1000) / 1000);
                }
                if(sellPrice.SellPriceName == "Dealer")
                {
                    Assert.AreEqual((decimal)12.123, Math.Truncate((decimal)sellPrice.SellPriceFinalAmount * 1000) / 1000);
                }
                if(sellPrice.SellPriceName == "Retail")
                {
                    Assert.AreEqual((decimal)14.374, Math.Truncate((decimal)sellPrice.SellPriceFinalAmount * 1000) / 1000);
                }
            }
        }
        public void Compute_Product_And_Calculate_Margin_Based_On_SellPrice_Using_Same_Sell_Price()
        {
            // Data Input
            var product = new Product() { ProductCost = 1, ProductAnnualSalesProjection = 100 };
            product.Fees = new List<Fee>();
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = 1 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Amortized), FeeAmortizedCharge = 100, FeeAmortizedType = MyExtensions.GetEnumDescription(AmortizedTypeList.Annual_Sales_Projection) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Division) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
            product.ProductSellPrices = new List<ProductSellPrice>();
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Employee", SellPriceMarginPercent = 50, SellPriceLevel = 1 });
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Retail", SellPriceMarginPercent = 50, SellPriceLevel = 2 });

            foreach(var sellPrice in product.ProductSellPrices)
            {
                sellPrice.Fees = new List<Fee>();
                if(sellPrice.SellPriceName == "Retail")
                {
                    sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 20, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
                }
            }

            // Run Calculator
            var feeCalculator = new FeeCalculator(product);
            feeCalculator.ComputeAllProductPrices(false);

            // Created to get same value though division
            decimal value = ((decimal)3 / (decimal).9) * (decimal)1.1;
            value = feeCalculator.RoundNumberToDecimalPlace(value, 3);
            Assert.AreEqual(value, product.ProductTotalCost);

            foreach(var sellPrice in product.ProductSellPrices)
            {
                decimal sellPriceValue = value / (decimal)(.50);
                foreach(var fee in sellPrice.Fees)
                {
                    if(fee.FeePercentType == MyExtensions.GetEnumDescription(PercentTypeList.Multiplication))
                    {
                        sellPriceValue = sellPriceValue * (decimal)1.2;
                    }
                    else
                    {
                        sellPriceValue = sellPriceValue / (decimal).8;
                    }
                }

                Assert.AreEqual(feeCalculator.RoundNumberToDecimalPlace(sellPriceValue, 3), sellPrice.SellPriceFinalAmount);

                // Do Not change final sell price and values SHOULD be the same
                //sellPrice.SellPriceFinalAmount = (decimal)7.75;
            }

            feeCalculator.ComputeMarginBasedOnSellprice();

            foreach(var sellPrice in product.ProductSellPrices)
            {
                // Calculate Margin Values
                decimal sellPriceValue = value / (decimal)(.50);
                foreach(var fee in sellPrice.Fees)
                {
                    if(fee.FeePercentType == MyExtensions.GetEnumDescription(PercentTypeList.Multiplication))
                    {
                        sellPriceValue = sellPriceValue * (decimal)1.2;
                    }
                    else
                    {
                        sellPriceValue = sellPriceValue / (decimal).8;
                    }
                }

                Assert.AreEqual(feeCalculator.RoundNumberToDecimalPlace(sellPriceValue, 3), sellPrice.SellPriceFinalAmount);
            }
        }
        public void Compute_Product_And_Calculate_Margin_Based_On_SellPrice_And_Round_To_Nearest_Five_Cent()
        {
            // Data Input
            var product = new Product() { ProductCost = 1, ProductAnnualSalesProjection = 100 };
            product.Fees = new List<Fee>();
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = 1 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Amortized), FeeAmortizedCharge = 100, FeeAmortizedType = MyExtensions.GetEnumDescription(AmortizedTypeList.Annual_Sales_Projection) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Division) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
            product.ProductSellPrices = new List<ProductSellPrice>();
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Employee", SellPriceMarginPercent = 50, SellPriceLevel = 1 });
            product.ProductSellPrices.Add(new ProductSellPrice() { SellPriceName = "Retail", SellPriceMarginPercent = 50, SellPriceLevel = 2 });

            foreach (var sellPrice in product.ProductSellPrices)
            {
                sellPrice.Fees = new List<Fee>();
                if (sellPrice.SellPriceName == "Retail")
                {
                    sellPrice.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 20, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
                }
            }

            // Run Calculator
            var feeCalculator = new FeeCalculator(product);
            feeCalculator.ComputeAllProductPrices(true);

            // Created to get same value though division
            decimal value = ((decimal)3 / (decimal).9) * (decimal)1.1;
            value = feeCalculator.RoundNumberToDecimalPlace(value, 3);
            Assert.AreEqual(value, product.ProductTotalCost);

            foreach (var sellPrice in product.ProductSellPrices)
            {
                decimal sellPriceValue = value / (decimal)(.50);
                foreach (var fee in sellPrice.Fees)
                {
                    if (fee.FeePercentType == MyExtensions.GetEnumDescription(PercentTypeList.Multiplication))
                    {
                        sellPriceValue = sellPriceValue * (decimal)1.2;
                    }
                    else
                    {
                        sellPriceValue = sellPriceValue / (decimal).8;
                    }
                }

                Assert.AreEqual(feeCalculator.RoundAmountToNearestFiveCents(feeCalculator.RoundNumberToDecimalPlace(sellPriceValue, 3)), sellPrice.SellPriceFinalAmount);

                // Set Sell price for new test
                sellPrice.SellPriceFinalAmount = (decimal)7.75;
            }

            feeCalculator.ComputeMarginBasedOnSellprice();

            int count = 1;
            foreach (var sellPrice in product.ProductSellPrices)
            {
                //Calcuate the margin based on the sell price Final Amount
                decimal finalSellPrice = sellPrice.SellPriceFinalAmount;
                decimal productTotalCost = (decimal)product.ProductTotalCost;

                // Calculate fees
                decimal baseSellPrice = finalSellPrice;

                // Calculate Margin
                decimal newMarginPercent = (100 - ((productTotalCost / baseSellPrice) * 100));

                // Calculate Margin Values
                if (count == 1)
                {
                    Assert.AreEqual((decimal)52.6839, sellPrice.SellPriceMarginPercent);
                    Assert.AreEqual((decimal)7.75, sellPrice.SellPriceFinalAmount);
                }
                if (count == 2)
                {
                    Assert.AreEqual((decimal)7.75, sellPrice.SellPriceFinalAmount);
                    Assert.AreEqual((decimal)43.2207, sellPrice.SellPriceMarginPercent);
                }
                count++;
            }
        }
        public void Calculate_Product_Total_Cost_With_Multiplication_And_Division_Percent_Amortized_And_Dollar_Amount_Fee()
        {
            var product = new Product() { ProductCost = 1, ProductAnnualSalesProjection = 100 };
            product.Fees = new List<Fee>();
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Dollar_Amount), FeeDollarAmount = 1 });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Amortized), FeeAmortizedCharge = 100, FeeAmortizedType = MyExtensions.GetEnumDescription(AmortizedTypeList.Annual_Sales_Projection) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Multiplication) });
            product.Fees.Add(new Fee() { FeeType = MyExtensions.GetEnumDescription(FeeTypeList.Percent), FeePercent = 10, FeePercentType = MyExtensions.GetEnumDescription(PercentTypeList.Division) });

            var feeCalculator = new FeeCalculator(product);
            feeCalculator.ComputeNetCost();

            Assert.AreEqual(3, product.ProductNetCost);

            feeCalculator.ComputeTotalCost();

            // Created to get same value though division
            decimal value = ((decimal)3 * (decimal)1.1) / (decimal).9;
            value = feeCalculator.RoundNumberToDecimalPlace(value, 3);
            Assert.AreEqual(value, product.ProductTotalCost);
        }
 public FeeCalculator(Product product)
 {
     this._product = product;
 }