public string Save(Product products)
 {
     bool checkProductCode = IsProductCodeExists(products.ProductCode);
     bool checkProductCodeAndName = IsProductCodeAndNameExists(products.ProductName);
     if (checkProductCode)
     {
         if (checkProductCodeAndName)
         {
             if (productGateway.UpdateProductQuantity(products, products.ProductName) > 0)
             {
                 return "Product Quantity Increament!";
             }
             else
             {
                 return "Could Not Product Quantity Increament!";
             }
             //return "Product Name Allready Exists!";
         }
         return "Product Code Allready Exists!";
     }
     if (productGateway.Save(products) > 0)
     {
         return "Added Successfully!";
     }
     else
     {
         return "Could Not Added!";
     }
 }
        public List<Product> GetAllProducts()
        {
            SqlConnection connection = new SqlConnection(databaseConString);
            string query = "SELECT * FROM view_productCategory";
            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

            List<Product> productList = new List<Product>();

            while (reader.Read())
            {
                Product products = new Product();
                products.ProductId = int.Parse(reader["product_id"].ToString());
                products.ProductCode = reader["product_code"].ToString();
                products.ProductName = reader["product_name"].ToString();
                products.ProductQuantity = int.Parse(reader["product_quantity"].ToString());
                products.ProductCategoryName = reader["category_name"].ToString();

                productList.Add(products);
            }
            reader.Close();
            connection.Close();

            return productList;
        }
        public Product CheckProductQuantity(int productID)
        {
            SqlConnection connection = new SqlConnection(databaseConString);
            string query = "SELECT * FROM tbl_product WHERE product_id = '" + productID + "'";
            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

            Product products = new Product();

            while (reader.Read())
            {
                products.ProductId = int.Parse(reader["product_id"].ToString());
                products.ProductCode = reader["product_code"].ToString();
                products.ProductName = reader["product_name"].ToString();
                products.ProductQuantity = int.Parse(reader["product_quantity"].ToString());
            }
            reader.Close();
            connection.Close();

            return products;
        }
        public string Sale(Product products, int productID)
        {
            //int productQuantity = productGateway.CheckProductQuantity();

            Product aProduct = productGateway.CheckProductQuantity(productID);

            if (aProduct.ProductQuantity < products.ProductQuantity)
            {
                return "Insufficient Product Quantity!";
            }

            aProduct.ProductQuantity = products.ProductQuantity;

            if (productGateway.Sale(products, productID) > 0 && productGateway.SaleProductSave(aProduct) > 0)
            {
                return "Sale Successfully!";
            }

            else
            {
                return "Could Not Sale!";
            }
        }
        public int UpdateProductQuantity(Product products, string productName)
        {
            SqlConnection connection = new SqlConnection(databaseConString);
            string query = "UPDATE tbl_product SET product_quantity = product_quantity + '" + products.ProductQuantity +
                           "' WHERE product_name = '" + productName + "'";

            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();
            int rowAffected = command.ExecuteNonQuery();
            connection.Close();

            return rowAffected;
        }
        public int Save(Product products)
        {
            SqlConnection connection = new SqlConnection(databaseConString);
            string query = "INSERT INTO tbl_product VALUES ('" + products.ProductCode + "','" + products.ProductName +
                           "','" + products.ProductQuantity + "','" + products.ProductCategoryId + "')";
            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();
            int rowAffected = command.ExecuteNonQuery();
            connection.Close();

            return rowAffected;
        }
        public int Sale(Product products, int productID)
        {
            SqlConnection connection = new SqlConnection(databaseConString);

            string query = "UPDATE tbl_product SET product_quantity = product_quantity - '" + products.ProductQuantity +
                           "' WHERE product_id = '" + productID + "'";

            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();
            int rowAffected = command.ExecuteNonQuery();
            connection.Close();

            return rowAffected;
        }
        public Product GetProductCodeAndName(string productName)
        {
            SqlConnection connection = new SqlConnection(databaseConString);
            string query = "SELECT * FROM tbl_product WHERE product_name = '" +
                           productName + "'";
            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

            Product products = null;

            while (reader.Read())
            {
                if (products == null)
                {
                    products = new Product();
                }
                products.ProductCode = reader["product_code"].ToString();
                products.ProductName = reader["product_name"].ToString();
                products.ProductQuantity = int.Parse(reader["product_quantity"].ToString());
                products.ProductCategoryId = int.Parse(reader["product_categoryId"].ToString());
            }
            reader.Close();
            connection.Close();

            return products;
        }
 public string Update(Product products, int productID)
 {
     if (productGateway.Update(products, productID) > 0)
     {
         return "Updated Successfully!";
     }
     else
     {
         return "Could Not Update!";
     }
 }