简体   繁体   中英

calculate shop discount from cart based on shop order limit in a multi-vendor e-commerce store in asp.net mvc5

I have a multi-vendor e-commerce store in which every store manager can manage their discount on shop orders and items.the cart model is as below:

    namespace myapp.Models
     public class Cart
     {
        [Key]
        public int RecordId { get; set; }
        public string CartId { get; set; }
        public Guid ItemId { get; set; }
        public Guid StoreId { get; set; }

        public Decimal? ShopDiscount { get; set; }
        public long ShopId { get; set; }
        [Display(Name = "Discount Time")]
        public string Duration { get; set; }
        [StringLength(100, ErrorMessage = "Must be less than 100 characters")]
        public string StoreName { get; set; }
        public decimal? Giftback { get; set; }
        [Display(Name = "Min order For Disc")]
        public Decimal? OrderLimit { get; set; }
        public int Count { get; set; }
        public decimal? Discount { get; set; }
        public System.DateTime DateCreated { get; set; }
        public virtual Item Item { get; set; }
      }
    }

i have used entity-framework with code first scaffolding. moreover in my shoppingcart model class i have used shopdeal() method to calculate shoplevel discount but it returns always 0 and does. i think there is problem with this method.

    // shopdeal method to calculate shopdiscount for all items in cart 
     // 

            public decimal? ShopDeal()
        {
            decimal? shopdeal = 0;
           //get record with different shops so that calculate shop total 
     //separatly
            var results = db.Carts.Select(m => new { m.CartId, m.StoreId, 
    m.OrderLimit, m.ShopDiscount, m.Duration }).Distinct().ToList();

              // calculate total price of all items for a particular shop  
            foreach (var item in results)
            {
                decimal? shoptotal = (from cartItems in db.Carts
                                      where cartItems.CartId == ShoppingCartId
                                      && cartItems.Item.StoreId == 
    item.StoreId
                                      select 
    (decimal?)cartItems.Item.Price).Sum();
                if (shoptotal >= item.OrderLimit && item.Duration == 
    "Started")
                {
                    shopdeal = shopdeal + shoptotal * item.ShopDiscount / 100;
                }

            }

            return shopdeal;
        }

in the above method we try to calculate total price of all items of a particular shop than compare shoptotal with shopdiscount (orderlimit) of that shop and check its time period(started or not) if both conditions are true to apply shopdiscount for that shop .if the user buy from more than on shop than apply same if for all shop(usage of foreach loop). please help if there is workaround to get price total for each shop and apply shopdiscount and get total shopdeal(total shopdiscount).the total function is as below :

        {
            // Multiply album price by count of that album to get 
            // the current price for each of those albums in the cart
            // sum all album price totals to get the cart total
            decimal? total = (from cartItems in db.Carts
                              where cartItems.CartId == ShoppingCartId
                              select (int?)cartItems.Count *
                              cartItems.Item.Price * (100 - 
                              cartItems.Item.Discount) / 100).Sum();
             // update total if shopdiscount available
            // why we need to apply shopdeal again? how to save result of 
             //shopdeal()
            total = total - ShopDeal(); 
            return total ?? decimal.Zero;
        }

 the above code works and no error is shown but the shopdiscount is always zero.any workaround to calculate and apply shopdiscount and reduce database queries.

thanks all , after lot of efforts following worked for me as expected, implemented in https://smartbook.pk/Items/DiscountItems


     public decimal? ShopDeal()
        {
            decimal? shopdeal = 0;

            var results = (from cartItems in db.Carts
                           where cartItems.CartId == ShoppingCartId
                           select new { cartItems.StoreId,cartItems.OrderLimit,cartItems.Duration,cartItems.ShopDiscount }).Distinct().ToList();
                //db.Carts.Select(m => new { m.StoreId, m.OrderLimit, m.ShopDiscount, m.Giftback, m.Duration }.where()).Distinct().ToList();


            foreach (var item in results)
            {
                decimal? shoptotal = (from cartItems in db.Carts
                                      where cartItems.CartId == ShoppingCartId
                                      && cartItems.Item.StoreId == item.StoreId
                                      select (decimal?)cartItems.Item.Price).Sum();
                if (shoptotal >= item.OrderLimit && item.Duration == "Started")
                {
                    shopdeal = shopdeal + shoptotal * item.ShopDiscount / 100;
                }

            }

            return shopdeal;
        }

regards 
fiaz ahmed ranjha


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM