Esempio n. 1
0
        public void removeDiscount()
        {
            if (discount == null)
            {
                throw new DoesntExistException("Discount does not exist so it cannot be removed");
            }
            discount = null;

            /* if(discount is Discount)
             * {
             *   if(discount.getId()==discountid)
             *       discount = null;
             * }
             * else
             * {
             *   if (discount.getId() == discountid)
             *       discount = null;
             *   else
             *   {
             *       DiscountComposite dis = (DiscountComposite)discount;
             *       dis.remove(discountid);
             *   }
             *
             * }
             * }*/
        }
Esempio n. 2
0
        public void removeStoreDiscount(int discountID, Store store)
        {
            DiscountComponent d = DBDiscount.getInstance().getDiscountByID(discountID);

            DBDiscount.getInstance().removeDiscount(d);
            store.removeDiscount(discountID);
        }
Esempio n. 3
0
 public void setProductDiscount(Product product, DiscountComponent discount)
 {
     if (!permissions.editDiscount())
     {
         throw new PermissionsException("Error:" + userName.getUsername() +
                                        " has no permission to set product's discount in store " +
                                        store.getStoreName());
     }
     //product.setDiscount(discount);
 }
Esempio n. 4
0
 public Discount getProductDiscount(int storeId, int productId)
 {
     try
     {
         //SqlConnection connection = Connector.getInstance().getSQLConnection();
         lock (connection)
         {
             connection.Open();
             var discountEntry = connection.Query <DiscountEntry>("SELECT * FROM [dbo].[Discount] WHERE storeId=@storeId AND productId=@productId", new { storeId = storeId, productId = productId }).First();
             connection.Close();
             DiscountEntry     d   = (DiscountEntry)discountEntry;
             DiscountComponent dis = getDiscountByID(d.getId());
             if (dis != null)
             {
                 return((Discount)dis);
             }
             int discountId = d.getId();
             DiscountComponentEntry component = (DiscountComponentEntry)connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE id=@id", new { id = discountId }).First();
             bool isPartOfComplex             = false;
             if (component.getIsPartOfComplex() == 1)
             {
                 isPartOfComplex = true;
             }
             if (d.getType() == "Visible")
             {
                 VisibleDiscount v = new VisibleDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getVisibleType(), component.getStoreId());
                 Product         p = DBProduct.getInstance().getProductByID(d.getProductId());
                 v.setProduct(p);
                 discounts.AddFirst(v);
                 connection.Close();
                 return(v);
             }
             else
             {
                 int             productID = d.getProductId();
                 ReliantDiscount r         = null;
                 if (d.getReliantType() == "sameProduct")
                 {
                     Product p = DBProduct.getInstance().getProductByID(productID);
                     r = new ReliantDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getNumOfProducts(), p, component.getStoreId());
                     discounts.AddFirst(r);
                 }
                 return(r);
             }
         }
     }
     catch (Exception)
     {
         if (connection.State != ConnectionState.Closed)
         {
             connection.Close();
         }
         return(null);
     }
 }
Esempio n. 5
0
 /* public void addReliantDiscount(double percentage, String condition, String duration)
  * {
  *   discount = new ReliantDiscount(condition, duration, percentage: percentage);
  * }
  *
  * public void addVisibleDiscount(double percentage, String duration)
  * {
  *   discount = new VisibleDiscount(percentage, duration);
  * }
  * public DiscountComponent getDiscount()
  * {
  *   return this.discount;
  * }
  * public void addReliantDiscount(double percentage, String condition, String duration)
  * {
  *   discount = new ReliantDiscount(percentage, condition, duration);
  * }
  *
  * public void addVisibleDiscount(double percentage, String duration)
  * {
  *   discount = new VisibleDiscount(percentage, duration);
  * }*/
 public void addDiscount(DiscountComponent d)
 {
     if (discount == null)
     {
         this.discount = d;
     }
     else
     {
         List <DiscountComponent> list = new List <DiscountComponent>();
         list.Add(this.discount);
         list.Add(d);
         DiscountComposite composite = new DiscountComposite(list, "or");
         this.discount = composite;
     }
 }
Esempio n. 6
0
        public override string description()
        {
            string str = "(";
            int    i;

            for (i = 0; i < children.Count - 1; i++)
            {
                DiscountComponent dis = children.ElementAt(i);
                str = str + dis.description();
                str = str + " " + type + " ";
            }
            DiscountComponent dis2 = children.ElementAt(i);

            str = str + dis2.description() + ")";
            return(str);
        }
Esempio n. 7
0
 public void removeDiscount(DiscountComponent d)
 {
     try
     {
         //SqlConnection connection = Connector.getInstance().getSQLConnection();
         lock (connection)
         {
             connection.Open();
             connection.Execute("DELETE FROM DiscountComponent WHERE id=@id ", new { id = d.getId() });
             if (d is Discount)
             {
                 connection.Execute("DELETE FROM Discount WHERE id=@id ", new { id = d.getId() });
                 connection.Close();
             }
             else
             {
                 connection.Execute("DELETE FROM DiscountComposite WHERE id=@id ", new { id = d.getId() });
                 DiscountComposite composite = (DiscountComposite)d;
                 connection.Close();
                 foreach (DiscountComponent component in composite.getChildren())
                 {
                     removeDiscount(component);
                 }
             }
             discounts.Remove(d);
         }
     }
     catch (Exception e)
     {
         if (connection.State == ConnectionState.Open)
         {
             connection.Close();
         }
         throw e;
     }
 }
Esempio n. 8
0
        public void addDiscount(DiscountComponent d)
        {
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    string sql = "INSERT INTO [dbo].[DiscountComponent] (id, percentage, duration, type, storeId, isPartOfComplex)" +
                                 " VALUES (@id,@percentage, @duration, @type, @storeId, @isPartOfComplex)";
                    int isPartOfComplex;
                    if (d.getIsPartOfComplex())
                    {
                        isPartOfComplex = 1;
                    }
                    else
                    {
                        isPartOfComplex = 0;
                    }
                    if (d is Discount)
                    {
                        connection.Execute(sql, new
                        {
                            id         = d.getId(),
                            percentage = d.getPercentage(),
                            duration   = d.getDuration(),
                            type       = "Discount",
                            storeId    = d.getStoreId(),
                            isPartOfComplex
                        });
                        if (d is VisibleDiscount)
                        {
                            VisibleDiscount v = (VisibleDiscount)d;
                            addVisibleDiscount(v);
                        }
                        if (d is ReliantDiscount)
                        {
                            ReliantDiscount r = (ReliantDiscount)d;
                            addReliantDiscount(r);
                        }
                    }
                    if (d is DiscountComposite)
                    {
                        DiscountComposite composite = (DiscountComposite)d;
                        connection.Execute(sql, new
                        {
                            id         = d.getId(),
                            percentage = d.getPercentage(),
                            duration   = d.getDuration(),
                            type       = "Composite",
                            storeId    = d.getStoreId(),
                            isPartOfComplex
                        });
                        foreach (DiscountComponent child in composite.getChildren())
                        {
                            string sql2 = "INSERT INTO [dbo].[DiscountComposite] (id, childid, type)" +
                                          " VALUES (@id, @childid, @type)";

                            connection.Execute(sql2, new
                            {
                                id      = d.getId(),
                                childid = child.getId(),
                                type    = composite.getType()
                            });
                        }
                    }
                    connection.Close();
                    discounts.AddFirst(d);
                }
            }

            catch (Exception e)
            {
                connection.Close();
                throw e;
            }
        }
Esempio n. 9
0
        public LinkedList <DiscountComponent> getStoreDiscountsList(int storeId)
        {
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                LinkedList <DiscountComponent> storeDiscounts = new LinkedList <DiscountComponent>();
                lock (connection)
                {
                    connection.Open();
                    var c = connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE storeId=@storeId AND type=@type", new { storeId = storeId, type = "Discount" }).ToList <DiscountComponentEntry>();
                    connection.Close();
                    List <DiscountComponentEntry> discountList = (List <DiscountComponentEntry>)c;
                    foreach (DiscountComponentEntry d in discountList)
                    {
                        connection.Open();
                        var discountEntry = connection.Query <DiscountEntry>("SELECT * FROM [dbo].[Discount] WHERE id=@id", new { id = d.getId() }).First();
                        connection.Close();

                        DiscountEntry de = (DiscountEntry)discountEntry;


                        if (de.getProductId() != -1)//productDiscount
                        {
                            Discount dis = getProductDiscount(d.getStoreId(), de.getProductId());

                            if (dis.getIsPartOfComplex() == false) //add to store discounts only if it is not part of complex discount
                            {
                                storeDiscounts.AddFirst(dis);
                            }
                        }
                        else//StoreDiscount
                        {
                            Discount dis = getStoreDiscount(d.getId(), d.getStoreId());
                            if (dis.getIsPartOfComplex() == false)
                            {
                                storeDiscounts.AddFirst(dis);
                            }
                            discounts.AddFirst(dis);
                        }
                    }
                    connection.Open();
                    var compositeEntryList = connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE storeId=@storeId AND type=@type", new { storeId = storeId, type = "Composite" }).ToList <DiscountComponentEntry>();
                    List <DiscountComponentEntry> compositeEntryL = (List <DiscountComponentEntry>)compositeEntryList;
                    int i = 0;
                    while (compositeEntryL.Count != 0)
                    {
                        DiscountComponentEntry   di       = compositeEntryL.ElementAt(i);
                        List <DiscountComponent> children = new List <DiscountComponent>();
                        var discountChildList             = connection.Query <DiscountCompositeEntry>("SELECT * FROM [dbo].[DiscountComposite] WHERE id=@id", new { id = di.getId() }).ToList <DiscountCompositeEntry>();
                        connection.Close();
                        List <DiscountCompositeEntry> de = (List <DiscountCompositeEntry>)discountChildList;
                        string type = de.ElementAt(0).getType();
                        bool   childrenPulledFromDB = true;
                        foreach (DiscountCompositeEntry en in de)
                        {
                            if (getDiscountByID(en.getchildid()) == null)
                            {
                                childrenPulledFromDB = false;
                                break;
                            }
                        }
                        if (childrenPulledFromDB)
                        {
                            foreach (DiscountCompositeEntry en in de)
                            {
                                DiscountComponent disc = getDiscountByID(en.getchildid());
                                children.Add(disc);
                            }
                            bool isPartOfComplex = false;
                            if (di.getIsPartOfComplex() == 1)
                            {
                                isPartOfComplex = true;
                            }
                            DiscountComposite compos = new DiscountComposite(di.getId(), children, type, di.getPercentage(), di.getDuration(), di.getStoreId(), isPartOfComplex);
                            discounts.AddFirst(compos);
                            storeDiscounts.AddFirst(compos);
                            compositeEntryL.Remove(di);
                        }
                        if (compositeEntryL.Count != 0)
                        {
                            i = (i + 1) % compositeEntryL.Count;
                        }
                    }
                    connection.Close();
                    return(storeDiscounts);
                }
            }
            catch (Exception)
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
                return(new LinkedList <DiscountComponent>());
            }
        }
Esempio n. 10
0
 public void setProductDiscount(Product product, DiscountComponent discount)
 {
     //product.setDiscount(discount);
 }
Esempio n. 11
0
 public void addDiscount(DiscountComponent d)
 {
     discountList.AddLast(d);
 }