예제 #1
0
파일: SKUHandler.cs 프로젝트: ohel/kerppi
 public void RemoveSKU(DataModel.SKU sku)
 {
     sku.Delete();
     SKUList.Remove(SKUList.First(s => s.Id == sku.Id));
     foreach (var c in SKUCollectionList)
     {
         var match = c.SKUs.FirstOrDefault(s => s.Id == sku.Id);
         if (match != null)
         {
             c.SKUs.Remove(match);
         }
     }
     NotifyPropertyChanged(() => SKUList);
     NotifyPropertyChanged(() => SKUCollectionList);
 }
예제 #2
0
파일: SKUHandler.cs 프로젝트: ohel/kerppi
        public void Import(string filename)
        {
            var skus = DataModel.SKU.LoadAllFromFile(filename);

            foreach (var sku in skus)
            {
                var match = SKUList.FirstOrDefault(s => s.Code == sku.Code);
                if (match != null)
                {
                    sku.Id = match.Id;                // Update existing ones.
                }
                sku.Save();
            }
            SKUList = new ObservableCollection <DataModel.SKU>(skus);
        }
        public (decimal actualPrice, decimal promoPrice) PromoCalculator()
        {
            decimal actualPrice = 0m, promoPrice = 0m, sku1Price = 0m, sku2Price = 0m;
            bool    promoApplied = false;

            try
            {
                foreach (var cartItem in CartList.FindAll(c => !c.IsCheckedOut))
                {
                    promoApplied = false;
                    sku1Price    = SKUList.Find(sku => sku.SKUId == cartItem.SKU).SKUPrice;
                    actualPrice += sku1Price * cartItem.Quantity;

                    //If item is already checkedout, pick next.
                    if (cartItem.IsCheckedOut)
                    {
                        continue;
                    }

                    foreach (var promotion in PromoList)
                    {
                        //For promotions with single SKU item
                        if (promotion.SKU2.Equals('\0'))
                        {
                            if (promotion.SKU1 == cartItem.SKU && promotion.SKU1Unit <= cartItem.Quantity)
                            {
                                //Items with promo
                                promoPrice += promotion.PromoPrice * (cartItem.Quantity / promotion.SKU1Unit);

                                //Items without promo
                                promoPrice += (cartItem.Quantity % promotion.SKU1Unit) * sku1Price;

                                promoApplied = true;

                                //Check out items
                                cartItem.IsCheckedOut = true;
                                break;
                            }
                        }
                        //For promotions with multiple SKU items
                        else
                        {
                            if (promotion.SKU1 == cartItem.SKU && promotion.SKU1Unit <= cartItem.Quantity)
                            {
                                foreach (var cartItem2 in CartList.FindAll(c => !c.IsCheckedOut))
                                {
                                    if (promotion.SKU2 == cartItem2.SKU && promotion.SKU2Unit <= cartItem2.Quantity)
                                    {
                                        sku2Price = SKUList.Find(sku => sku.SKUId == cartItem2.SKU).SKUPrice;

                                        //Number of times a given promotion needs to be applied.
                                        var totalPromo = Math.Min((cartItem.Quantity / promotion.SKU1Unit), (cartItem2.Quantity / promotion.SKU2Unit));

                                        //SKU1 and SKU2 items wit promo
                                        promoPrice += promotion.PromoPrice * totalPromo;

                                        //SKU1 items without promo
                                        promoPrice += (cartItem.Quantity % promotion.SKU1Unit) * sku1Price;

                                        //SKU2 items without promo
                                        promoPrice += (cartItem2.Quantity % promotion.SKU2Unit) * sku2Price;

                                        promoApplied = true;

                                        //Check out items
                                        cartItem.IsCheckedOut = true;
                                        CartList.Find(c => c.SKU == cartItem2.SKU).IsCheckedOut = true;
                                        cartItem2.IsCheckedOut = true;

                                        break;
                                    }
                                }
                            }
                        }
                    }

                    //When no promotion is applied, calculate actual price
                    if (!promoApplied)
                    {
                        promoPrice += (cartItem.Quantity) * sku1Price;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(actualPrice, promoPrice);
        }