Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
I have product with options and extras in cart that line cost is 200

Bass product cost -20 and option 1-50 and option2 -30 and extra1-30 and extra2 -70. I had applied 16 percentage discount for 200 pounds i.e 32 pounds

discount at the time linecost 168(200-32) and cart total 168(200-32).

Then finally i want to remove option or extras from cart one by one whatever

the want. For every option or extra remove cart recalculate line cost and

cart total but i got negative values in cart and please suggest whatver i had
done correct or not.

My linecost function and cart total function
C#
public decimal CalculateLineCost(Product product, List<Option> options, List<Extra> extras, decimal? discount)
       {
           decimal Linesalesprice = 0.00M;

           if (!HasOptions(product.Id) && !HasExtrs(product.Id))
           {
               if (product.DiscountType == 0)
               {
                   product.SubTotal = Convert.ToDecimal((product.EatInSalesPrice - (discount ?? 0)) * product.Qty);
                   Linesalesprice = product.SubTotal;
               }
               else if(product.DiscountType > 0)
               {
                   product.SubAmountAfterDiscount = Convert.ToDecimal(product.SubTotal - product.DiscountSubTotal);
                   Linesalesprice = Convert.ToDecimal(product.SubAmountAfterDiscount);
               }

           }
           else
           {
               if (product.DiscountType == 0)
               {
                   decimal lineSubTotal = Convert.ToDecimal(product.EatInSalesPrice + options.SelectMany(option => option.SubProductOptions).Sum(s => s.EatInSalesPrice) + extras.Sum(e => e.EatInSalesPrice));
                   product.SubTotal = Convert.ToDecimal((lineSubTotal - (discount ?? 0)) * product.Qty);
                   Linesalesprice = product.SubTotal;
               }
               else if (product.DiscountType > 0)
               {
                   product.SubAmountAfterDiscount = Convert.ToDecimal(product.SubTotal - product.DiscountSubTotal);
                   Linesalesprice = Convert.ToDecimal(product.SubAmountAfterDiscount);
               }



           }

           return Linesalesprice;
       }

       public decimal CalculateCartTotal(List<Product> products,List<Option> options, List<Extra> extras, decimal? discount)
       {
           decimal carttotal = 0.00M;

           foreach (var item in products)
           {
               decimal linecost = CalculateLineCost(item,item.options, item.Extras,item.DiscountSubTotal);
               carttotal += linecost;
           }
            shoppingmodel.Total = carttotal;

            return shoppingmodel.Total;
       }


I had used when remove option or extra from cart
C#
product.SubAmountAfterDiscount = Convert.ToDecimal(product.SubTotal - product.DiscountSubTotal);
.

Please give this procedure is correct or not and i dont want negative values in cart

What I have tried:

I had used when remove option or extra from cart
C#
product.SubAmountAfterDiscount = Convert.ToDecimal(product.SubTotal - product.DiscountSubTotal);
.

Please give this procedure is correct or not and i dont want negative values
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900