public int CreatePurchaseLine(PurchaseLineModel plm)
        {
            int result = DaoUtilities.NO_CHANGES;

            PURCHASECONTAINS pc = db.PURCHASECONTAINS.Create();

            pc.PURCHASE_ID = plm.PurchaseId;
            pc.PRODUCT_ID = plm.ProductId;
            pc.PRODUCTQUANTITY = plm.Quantity;
            pc.VALIDATIONDATE = plm.ValidationDate;

            db.PURCHASECONTAINS.Add(pc);
            try
            {
                int saveResult = db.SaveChanges();

                if (saveResult == 1)
                    result = DaoUtilities.SAVE_SUCCESSFUL;
            }
            catch (DbUpdateConcurrencyException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.UPDATE_CONCURRENCY_EXCEPTION;
            }
            catch (DbUpdateException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.UPDATE_EXCEPTION;
            }
            catch (DbEntityValidationException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.ENTITY_VALIDATION_EXCEPTION;
            }
            catch (NotSupportedException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.UNSUPPORTED_EXCEPTION;
            }
            catch (ObjectDisposedException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.DISPOSED_EXCEPTION;
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.GetBaseException().ToString());
                result = DaoUtilities.INVALID_OPERATION_EXCEPTION;
            }
            return result;
        }
        public int DeletePurchaseLine(PurchaseLineModel plm)
        {
            int result = DaoUtilities.NO_CHANGES;

            PURCHASECONTAINS pc = db.PURCHASECONTAINS.Find(plm.PurchaseId, plm.ProductId);

            if (pc != null)
            {
                db.PURCHASECONTAINS.Remove(pc);

                try
                {
                    int saveResult = db.SaveChanges();

                    if (saveResult == 1)
                        result = DaoUtilities.SAVE_SUCCESSFUL;
                }
                catch (DbUpdateConcurrencyException e)
                {
                    Console.WriteLine(e.GetBaseException().ToString());
                    result = DaoUtilities.UPDATE_CONCURRENCY_EXCEPTION;
                }
                catch (DbUpdateException e)
                {
                    Console.WriteLine(e.GetBaseException().ToString());
                    result = DaoUtilities.UPDATE_EXCEPTION;
                }
                catch (DbEntityValidationException e)
                {
                    Console.WriteLine(e.GetBaseException().ToString());
                    result = DaoUtilities.ENTITY_VALIDATION_EXCEPTION;
                }
                catch (NotSupportedException e)
                {
                    Console.WriteLine(e.GetBaseException().ToString());
                    result = DaoUtilities.UNSUPPORTED_EXCEPTION;
                }
                catch (ObjectDisposedException e)
                {
                    Console.WriteLine(e.GetBaseException().ToString());
                    result = DaoUtilities.DISPOSED_EXCEPTION;
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine(e.GetBaseException().ToString());
                    result = DaoUtilities.INVALID_OPERATION_EXCEPTION;
                }
            }

            return result;
        }
        public int CreateUserPurchase(string UserId)
        {
            int result = DaoUtilities.NO_CHANGES;

            IProductServices prs = new ProductServices();
            IPurchaseServices pus = new PurchaseServices();
            IPurchaseLineServices pls = new PurchaseLineServices();
            IBasketServices bs = new BasketServices();

            List<BasketModel> bm = FindBasketOfUser(UserId);

            PurchaseModel pm = new PurchaseModel();
            pm.PurchaseId = "";
            pm.PurchaseDate = DateTime.Today;
            pm.UserId = UserId;
            pm.TotalPrice = 0;

            List<PurchaseLineModel> purchaseLines = new List<PurchaseLineModel>();
            List<ProductModel> products = new List<ProductModel>();

            foreach (BasketModel bl in bm)
            {
                ProductModel prm = prs.FindProductWithId(bl.ProductId);
                prm.AvailableQuantity -= ((int)bl.Quantity);
                products.Add(prm);
                pm.TotalPrice += prm.Price * bl.Quantity;

                PurchaseLineModel plm = new PurchaseLineModel();
                plm.ProductId = bl.ProductId;
                plm.Quantity = bl.Quantity;
                purchaseLines.Add(plm);
            }

            result = pus.CreatePurchase(pm);
            if(result == DaoUtilities.SAVE_SUCCESSFUL)
            {
                PURCHASE p = ((PURCHASE)db.PURCHASE.Where(PURCHASE => PURCHASE.PURCHASE_USERID == pm.UserId && PURCHASE.PURCHASE_TOTALPRICE == pm.TotalPrice && PURCHASE.PURCHASE_DATE == pm.PurchaseDate).FirstOrDefault());
                foreach(PurchaseLineModel plm in purchaseLines)
                {
                    plm.PurchaseId = p.PURCHASE_ID;
                    result = pls.CreatePurchaseLine(plm);
                    if (result != DaoUtilities.SAVE_SUCCESSFUL)
                        break;
                }
                if (result == DaoUtilities.SAVE_SUCCESSFUL)
                {
                    foreach (ProductModel prm in products)
                    {
                        result = prs.UpdateProduct(prm);
                        if (result != DaoUtilities.SAVE_SUCCESSFUL)
                            break;
                    }
                    if (result == DaoUtilities.SAVE_SUCCESSFUL)
                    {
                        result = DeleteUserBasket(UserId);
                    }
                    else
                    {
                        foreach (ProductModel prm in products)
                        {
                            ProductModel prm2 = prs.FindProductWithId(prm.ProductId);
                            if(prm == prm2)
                            {
                                BasketModel bm2 = bs.FindBasketLineWithIds(UserId, prm.ProductId);
                                prm.AvailableQuantity += ((int)bm2.Quantity);
                                prs.UpdateProduct(prm);
                            }
                        }
                        db.PURCHASE.Remove(p);
                    }
                }
                else
                {
                    db.PURCHASE.Remove(p);
                }
            }

            return result;
        }
        /// <summary>
        /// Convert a purchaseContains from the database to a PurchaseLineModel
        /// </summary>
        /// <param name="pc"></param>
        /// <returns></returns>
        public PurchaseLineModel ConvertPurchaseContainsToPurchaseLineModel(PURCHASECONTAINS pc)
        {
            PurchaseLineModel plm = new PurchaseLineModel();

            if (pc != null)
            {
                plm.PurchaseId = pc.PURCHASE_ID;
                plm.ProductId = pc.PRODUCT_ID;
                plm.Quantity = pc.PRODUCTQUANTITY;
                plm.ValidationDate = pc.VALIDATIONDATE;
            }
            else
                plm = null;

            return plm;
        }