Beispiel #1
0
        public IActionResult getProduct([FromQuery(Name = "idProduct")] string idProduct)
        {
            bool isValidToken = ValidateToken();

            if (isValidToken)
            {
                Claim          idEmployeeClaim = getClaims()[0];
                TblProductsDAO dao             = TblProductsDAO.getInstance();
                try
                {
                    TblProductsDTO dto = dao.getProduct(idProduct);
                    if (dto != null)
                    {
                        return(Ok(dto));
                    }
                    else
                    {
                        return(BadRequest());
                    }
                }
                catch (Exception e)
                {
                    return(StatusCode(500));
                }
            }
            return(Unauthorized());
        }
Beispiel #2
0
        public IActionResult addProduct([FromBody] TblProductsDTO tblProductsDTO)
        {
            bool isValidToken = ValidateToken();

            if (isValidToken)
            {
                TblProductsDAO dao = TblProductsDAO.getInstance();
                try
                {
                    Claim idEmployeeClaim = getClaims()[0];
                    if (dao.addProduct(tblProductsDTO, idEmployeeClaim.Value))
                    {
                        return(Ok(tblProductsDTO));
                    }
                    else
                    {
                        return(BadRequest());
                    }
                }
                catch (Exception)
                {
                    return(StatusCode(500));
                }
            }
            return(Unauthorized());
        }
        public void deleteProduct()
        {
            if (bool.Parse(form.getStatusProduct().Text))
            {
                DialogResult dr = MessageBox.Show(MessageUtil.DELETE_CONFIRM, "warning", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    try
                    {
                        TblProductsDTO productsDTO = new TblProductsDTO();
                        productsDTO.idProduct = form.getIdProduct().Text;
                        productsDTO.status    = false;

                        if (productModel.setStatusProduct(productsDTO))
                        {
                            MessageBox.Show(MessageUtil.DELETE_SUCCESS);
                            getAllProduct();
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(MessageUtil.ERROR);
                    }
                }
            }
            else
            {
                MessageBox.Show(MessageUtil.DELETE_ALREADY);
            }
        }
        public void AddProductToOrder()
        {
            DataGridView dgvProducts = form.getDgvProduct();
            //Get number of selected grow
            Int32 selectedRowCount = dgvProducts.Rows.GetRowCount(DataGridViewElementStates.Selected);

            if (selectedRowCount > 0)
            {
                for (int i = 0; i < selectedRowCount; i++)
                {
                    //get selected row
                    String row    = dgvProducts.SelectedRows[i].Index.ToString();
                    int    rowInt = int.Parse(row);

                    //get product from list product
                    TblProductsDTO product = listProducts[rowInt];

                    //new list product order
                    if (listProductOrder == null)
                    {
                        listProductOrder = new List <CartItemDTO>();
                    }

                    CartItemDTO item = findProductInOrder(product.idProduct);

                    if (item != null)
                    {
                        if (item.quantity < product.quantity)
                        {
                            //increase quantity
                            item.quantity++;
                            //update totalPrice
                            item.totalPrice = item.quantity * item.price;
                        }
                        else
                        {
                            MessageBox.Show("Quantity of item can't more then quantity of product in stock", "Error");
                        }
                    }
                    else
                    {
                        item = new CartItemDTO()
                        {
                            idProduct  = product.idProduct,
                            name       = product.name,
                            price      = product.price,
                            quantity   = 1,
                            totalPrice = product.price * 1
                        };
                        //add product to list product order
                        listProductOrder.Add(item);
                    }
                }
            }
            else
            {
                MessageBox.Show("Select product you want to add", "Notification");
            }
        }
 private void udpateQuantityProduct(TblProductsDTO dto)
 {
     foreach (var produdct in listProducts)
     {
         if (produdct.idProduct.Equals(dto.idProduct))
         {
             produdct.quantity = dto.quantity;
         }
     }
 }
Beispiel #6
0
        public bool setStatusProduct(TblProductsDTO dto)
        {
            HttpResponseMessage responseMessage = ApiConnection.loadPutJsonObject("product/updateStatus", dto, Program.TokenGlobal);

            if (responseMessage.IsSuccessStatusCode)
            {
                return(true);
            }
            return(false);
        }
        public void CheckoutCart()
        {
            string customerOrder = form.getCustomerOrder().Text;

            if (customerOrder.Equals(""))
            {
                MessageBox.Show(MessageUtil.CUSTOMER_INVALID);
            }
            else if (listProductOrder == null)
            {
                MessageBox.Show(MessageUtil.ITEM_EMPTY);
            }
            else
            {
                //check quantity again
                string error = "";

                foreach (var item in listProductOrder)
                {
                    TblProductsDTO dto = productModel.getProduct(item.idProduct);
                    if (dto == null)
                    {
                        error += item.name + " is not available or out of stock. \n";
                    }
                    else if (dto.quantity < item.quantity)
                    {
                        udpateQuantityProduct(dto);
                        LoadProducts();
                        error += item.name + " is only have " + dto.quantity + ". \n";
                    }
                }

                if (error.Trim().Length != 0)
                {
                    error += "Please remove item or change quantity";
                    MessageBox.Show(error, "Error");
                    return;
                }

                //create order
                CreateOrder();
                //remove list item order
                listProductOrder = null;
                //reload interface
                LoadCustomers();
                LoadProductsOrder();
                LoadProducts();
                form.getAmount().Text        = "";
                form.getDiscount().Text      = "";
                form.getCurrentAmount().Text = "";
                form.getCustomerName().Text  = "";
            }
        }
Beispiel #8
0
        public List <TblProductsDTO> searchProduct(string category, string searchValue)
        {
            string sql = "SELECT p.idProduct, p.name, p.price, p.quantity,p.status,p.idCategory, c.name as categoryName "
                         + "FROM tblProducts p, tblCategory c "
                         + "WHERE p.idCategory = c.idCategory AND p.status = 1  AND p.quantity > 0 ";

            //check category field to create sql string
            if (category.Trim().Length != 0 && category != null)
            {
                sql += "AND c.name = '" + category + "' ";
            }

            //check search value field to create sql string
            if (searchValue.Trim().Length != 0 && searchValue != null)
            {
                sql += "AND p.name like '%" + searchValue + "%' ";
            }

            try
            {
                sqlConnection = DBUtil.MakeConnect();
                if (sqlConnection != null)
                {
                    sqlCommand    = new SqlCommand(sql, sqlConnection);
                    sqlDataReader = sqlCommand.ExecuteReader();
                    List <TblProductsDTO> result = new List <TblProductsDTO>();
                    while (sqlDataReader.Read())
                    {
                        TblProductsDTO product = new TblProductsDTO();
                        product.idProduct    = sqlDataReader["idProduct"].ToString();
                        product.name         = sqlDataReader["name"].ToString();
                        product.price        = float.Parse(sqlDataReader["price"].ToString());
                        product.quantity     = int.Parse(sqlDataReader["quantity"].ToString());
                        product.status       = bool.Parse(sqlDataReader["status"].ToString());
                        product.idCategory   = sqlDataReader["idCategory"].ToString();
                        product.categoryName = sqlDataReader["categoryName"].ToString();
                        result.Add(product);
                    }
                    return(result);
                }
            }
            catch (SqlException e)
            {
                throw new Exception(e.Message);
            } finally
            {
                DBUtil.CloseConnection(sqlDataReader, sqlConnection);
            }
            return(null);
        }
        public void saveProduct(frmProductDetail frmProductDetail)
        {
            if (!validateProduct(frmProductDetail))
            {
                return;
            }
            TblProductsDTO tblProductsDTO = new TblProductsDTO();

            tblProductsDTO.price      = float.Parse(frmProductDetail.getPrice().Text);
            tblProductsDTO.name       = frmProductDetail.getProductName().Text;
            tblProductsDTO.quantity   = int.Parse(frmProductDetail.getQuantity().Text);
            tblProductsDTO.idCategory = frmProductDetail.getComboBoxCategory().SelectedValue.ToString();
            if (frmProductDetail.getRadioButtonFalse().Checked)
            {
                tblProductsDTO.status = false;
            }
            else
            {
                tblProductsDTO.status = true;
            }
            if (!frmProductDetail.getUpdateState())
            {
                if (productModel.addProduct(tblProductsDTO))
                {
                    MessageBox.Show(MessageUtil.SAVE_SUCCESS);
                    getAllProduct();
                }
                else
                {
                    MessageBox.Show(MessageUtil.ERROR + " save Product");
                }
            }
            else
            {
                tblProductsDTO.idProduct = form.getIdProduct().Text;
                if (productModel.updateProduct(tblProductsDTO))
                {
                    MessageBox.Show(MessageUtil.SAVE_SUCCESS);
                    getAllProduct();
                }
                else
                {
                    MessageBox.Show(MessageUtil.ERROR + " save Product");
                }
            }
        }
Beispiel #10
0
        public bool updateStatusProduct(TblProductsDTO dto, string idEmployee)
        {
            string sql = "UPDATE tblProducts " +
                         "SET status=@status " +
                         "WHERE idProduct=@idProduct";
            SqlTransaction transaction = null;

            try
            {
                sqlConnection = DBUtil.MakeConnect();
                if (sqlConnection != null)
                {
                    transaction = sqlConnection.BeginTransaction();
                    sqlCommand  = new SqlCommand(sql, sqlConnection, transaction);
                    sqlCommand.Parameters.AddWithValue("@status", dto.status);
                    sqlCommand.Parameters.AddWithValue("@idProduct", dto.idProduct);
                    bool check = sqlCommand.ExecuteNonQuery() > 0;
                    if (check)
                    {
                        if (dto.status)
                        {
                            check = writeLog(sqlCommand, transaction, sqlConnection, dto.idProduct, STATUS_ACTIVE, idEmployee);
                        }
                        else
                        {
                            check = writeLog(sqlCommand, transaction, sqlConnection, dto.idProduct, STATUS_INACTIVE, idEmployee);
                        }
                    }
                    transaction.Commit();
                    return(check);
                }
            }
            catch (SqlException e)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw new Exception(e.Message);
            }
            finally
            {
                DBUtil.CloseConnection(null, sqlConnection);
            }
            return(false);
        }
Beispiel #11
0
        public bool addProduct(TblProductsDTO dto, string idEmployee)
        {
            string sql = "insert into tblProducts(idCategory,idProduct,name,price,quantity,status) values(@idCategory,@idProduct,@name,@price,@quantity,@status)";

            sqlConnection = DBUtil.MakeConnect();
            SqlTransaction transaction = null;

            try
            {
                if (sqlConnection != null)
                {
                    string idProduct = Guid.NewGuid().ToString();
                    transaction = sqlConnection.BeginTransaction();
                    sqlCommand  = new SqlCommand(sql, sqlConnection, transaction);
                    sqlCommand.Parameters.AddWithValue("@idCategory", dto.idCategory);
                    sqlCommand.Parameters.AddWithValue("@idProduct", idProduct);
                    sqlCommand.Parameters.AddWithValue("@name", dto.name);
                    sqlCommand.Parameters.AddWithValue("@price", dto.price);
                    sqlCommand.Parameters.AddWithValue("@quantity", dto.quantity);
                    sqlCommand.Parameters.AddWithValue("@status", dto.status);
                    bool check = sqlCommand.ExecuteNonQuery() > 0;
                    if (check)
                    {
                        check = writeLog(sqlCommand, transaction, sqlConnection, idProduct, STATUS_ADD, idEmployee);
                    }
                    transaction.Commit();
                    return(check);
                }
                return(false);
            }
            catch (Exception e)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw new Exception(e.Message);
            }
            finally
            {
                DBUtil.CloseConnection(null, sqlConnection);
            }
        }
Beispiel #12
0
 public TblProductsDTO getProduct(string idProduct)
 {
     try
     {
         Dictionary <string, string> hashParam = new Dictionary <string, string>();
         hashParam.Add("idProduct", idProduct);
         HttpResponseMessage responseMessage = ApiConnection.loadGetJsonObject("product/getProduct", hashParam, Program.TokenGlobal);
         if (responseMessage.IsSuccessStatusCode)
         {
             var            product    = responseMessage.Content.ReadAsStringAsync();
             TblProductsDTO productDTO = JsonConvert.DeserializeObject <TblProductsDTO>(product.Result);
             return(productDTO);
         }
         return(null);
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Beispiel #13
0
        public List <TblProductsDTO> loadProductToSale()
        {
            string sql = "select p.idProduct,p.name,p.price,p.quantity,p.status,p.idCategory,c.name as categoryName " +
                         "from tblProducts p,tblCategory c " +
                         "where p.idCategory = c.idCategory AND p.status = 1 AND p.quantity > 0 ";

            try
            {
                sqlConnection = DBUtil.MakeConnect();
                if (sqlConnection != null)
                {
                    sqlCommand    = new SqlCommand(sql, sqlConnection);
                    sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    List <TblProductsDTO> result = new List <TblProductsDTO>();
                    while (sqlDataReader.Read())
                    {
                        TblProductsDTO product = new TblProductsDTO();
                        product.idProduct    = sqlDataReader["idProduct"].ToString();
                        product.name         = sqlDataReader["name"].ToString();
                        product.price        = float.Parse(sqlDataReader["price"].ToString());
                        product.quantity     = int.Parse(sqlDataReader["quantity"].ToString());
                        product.status       = bool.Parse(sqlDataReader["status"].ToString());
                        product.idCategory   = sqlDataReader["idCategory"].ToString();
                        product.categoryName = sqlDataReader["categoryName"].ToString();
                        result.Add(product);
                    }
                    return(result);
                }
            }
            catch (SqlException e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                DBUtil.CloseConnection(sqlDataReader, sqlConnection);
            }
            return(null);
        }