public frmProduct(Product aProduct)
     : this()
 {
     btnSave.Text = @"Update";
     label6.Text = "Add Quantity";
     label7.Visible = true;
     availableQuantityTextBox.Visible = true;
     FillFieldsWith(aProduct);
     productID = aProduct.ProductID;
 }
 public frmInvoice(Product product)
     : this()
 {
     this.product = product;
     if (product != null)
     {
         txtProductName.Text = product.ProductName;
         txtAvailableQty.Text = product.Quantity.ToString();
         txtSaleQty.Text = "1";
         discountTextBox.Text = product.Discount.ToString();
         txtPrice.Text = product.Price.ToString();
         totalPriceTextBox.Text = (product.Price - (product.Price * product.Discount / 100)).ToString();
         barCodeTextBox.Text = product.BarCode;
         addToCateButton.PerformClick();
     }
 }
        public bool AddProduct(Product product)
        {
            try
            {
                using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
                {
                    dataContext.Products.Add(product);
                    dataContext.SaveChanges();
                    return true;
                }
            }
            catch (Exception exception)
            {

                throw new Exception(exception.Message);
            }
        }
        public bool CheckProduct(Product product)
        {
            try
            {
                using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
                {
                    Product aProduct = dataContext.Products.FirstOrDefault(x => x.ProductName == product.ProductName && x.CategoryID == product.CategoryID);
                    if (aProduct == null)
                    {
                        return false;
                    }
                    return true;
                }
            }
            catch (Exception exception)
            {

                throw new Exception(exception.Message);
            }
        }
 public bool DeleteProduct(Product product)
 {
     try
     {
         using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
         {
             dataContext.Products.Attach(product);
             dataContext.Entry(product).State = EntityState.Deleted;
             dataContext.SaveChanges();
             return true;
         }
     }
     catch (Exception exception)
     {
         throw new Exception(exception.Message);
     }
 }
        public bool UpdateProduct(Product product)
        {
            try
            {
                using (SalesAndInventorySystemDataContext dataContext = new SalesAndInventorySystemDataContext())
                {
                    int noOfRowUpdated =
                        dataContext.Database.ExecuteSqlCommand("Update student set studentname ='changed student by command' where studentid=1");

                    //dataContext.Products.Attach(product);
                    //dataContext.Entry<Product>(product).Property()

                    return true;
                }
            }

            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }

            //_cnt.Users.Attach(user);
            //_cnt.Entry<User>(user).Property(u => u.PasswordHash).IsModified = true;
            //_cnt.SaveChanges();
        }
 private void FillFieldsWith(Product aProduct)
 {
     barcodeNoTextBox.Text = aProduct.BarCode;
     txtProductName.Text = aProduct.ProductName;
     descriptionTextBox.Text = aProduct.Description;
     priceTextBox.Text = 0.0.ToString();
     quantityTextBox.Text = 0.ToString();
     cmbCompany.SelectedValue = aProduct.Category.PersonType.ID;
     cmbCategory.SelectedValue = aProduct.Category.CategoryID;
     availableQuantityTextBox.Text = aProduct.Quantity.ToString();
     oldPriceTextBox.Text = aProduct.Price.ToString();
 }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (barcodeNoTextBox.Text == string.Empty)
            {
                MessageBox.Show(@"Please enter Barcode No", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                barcodeNoTextBox.Focus();
                return;
            }
            if (txtProductName.Text == "")
            {
                MessageBox.Show(@"Please enter product name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtProductName.Focus();
                return;
            }
            if (cmbCompany.SelectedIndex == 0)
            {
                MessageBox.Show(@"Please select company", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmbCompany.Focus();
                return;
            }
            if (cmbCategory.SelectedIndex == 0)
            {
                MessageBox.Show(@"Please select category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmbCategory.Focus();
                return;
            }
            if (priceTextBox.Text == string.Empty)
            {
                MessageBox.Show(@"Please enter price.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                priceTextBox.Focus();
                return;
            }
            if (quantityTextBox.Text == string.Empty)
            {
                MessageBox.Show(@"Please enter quantity.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                quantityTextBox.Focus();
                return;
            }

            if (sizeTextBox.Text==String.Empty)
            {
                MessageBox.Show(@"Please enter size of product.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                sizeTextBox.Focus();
                return;
            }

            if (colorComboBox.SelectedIndex==0)
            {
                MessageBox.Show(@"Please select color of product.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                colorComboBox.Focus();
                return;
            }
            if (buyPriceextBox.Text == String.Empty)
            {
                MessageBox.Show(@"Please enter buy price of product.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                buyPriceextBox.Focus();
                return;
            }
            try
            {
                product=new Product()
                {
                    ProductID = productID,
                    BarCode = barcodeNoTextBox.Text,
                    ProductName = txtProductName.Text,
                    Description = descriptionTextBox.Text,
                    InsertDate =DateTime.Parse( DateTime.Now.ToString("dd-MM-yyyy")),
                    UpdateDate = DateTime.Parse(DateTime.Now.ToString("dd-MM-yyyy")),
                    Price = Double.Parse(priceTextBox.Text),
                    Quantity = Convert.ToInt32(quantityTextBox.Text),
                    CategoryID = Convert.ToInt32(cmbCategory.SelectedValue),
                    Color=colorComboBox.SelectedText,
                    Size = sizeTextBox.Text,
                    BuyPrice = Double.Parse(buyPriceextBox.Text),
                    Discount = Double.Parse(discountTextBox.Text),
                    Vat = 0

                };
                if (btnSave.Text == "Save")
                {
                    if (productGateway.CheckBarCode(product))
                    {
                        MessageBox.Show(@"Barcode NO Already Exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtProductName.Focus();
                        return;
                    }

                    if (productGateway.CheckProduct(product))
                    {
                        MessageBox.Show(@"Product Name Already Exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtProductName.Focus();
                        return;
                    }

                    if (productGateway.AddProduct(product))
                    {
                        txtProductName.Text = string.Empty;
                        MessageBox.Show(@"Successfully saved.", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Reset();
                        return;
                    }
                }
                else if(btnSave.Text=="Update")
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 private void GetDataFromForm2(Product aNewProduct)
 {
     if (aNewProduct != null)
     {
         txtProductName.Text = aNewProduct.ProductName;
         txtAvailableQty.Text = aNewProduct.Quantity.ToString();
         txtSaleQty.Text = "1";
         discountTextBox.Text = aNewProduct.Discount.ToString();
         txtPrice.Text = aNewProduct.Price.ToString();
         totalPriceTextBox.Text = (aNewProduct.Price - (aNewProduct.Price * aNewProduct.Discount / 100)).ToString();
         barCodeTextBox.Text = aNewProduct.BarCode;
     }
 }
        private List<Product> GetAllProductONListView()
        {
            List<Product> allProducts=new List<Product>();
            try
            {
                if (resultListView.Items.Count>0)
                {
                    for (int i = 0; i < resultListView.Items.Count; i++)
                    {
                        ListViewItem item = resultListView.Items[i];
                        Product newProduct = (Product)item.Tag;
                        Product aProduct = new Product() { ProductID = newProduct.ProductID, ProductName = newProduct.ProductName, BarCode = newProduct.BarCode, CategoryID = newProduct.CategoryID, Description = newProduct.Description, InsertDate = newProduct.InsertDate, UpdateDate = newProduct.UpdateDate, Price = Convert.ToDouble(resultListView.Items[i].SubItems[6].Text), Quantity = Convert.ToInt32(resultListView.Items[i].SubItems[5].Text), Size = resultListView.Items[i].SubItems[4].Text, Discount = Convert.ToDouble(resultListView.Items[i].SubItems[3].Text) };
                        allProducts.Add(aProduct);
                    }
                }
                return allProducts;

            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }
        private void barCodeTextBox_TextChanged(object sender, EventArgs e)
        {
            string barCode = barCodeTextBox.Text;
            if (barCode!=string.Empty)
            {
                product = productGateway.GetDataBarCode(barCode);
                if (product==null)
                {
                    Reset();
                    return;
                }
                product.Category = categoryGateway.GetCategoryByID(product.CategoryID);
                if (product.Category==null)
                {
                    //Reset();
                    return;
                }

                product.Category.PersonType = companyGateway.GetCompaniesByID(product.Category.CompanyID);
                if (product!=null)
                {
                    txtProductName.Text = product.ProductName;
                    txtAvailableQty.Text = product.Quantity.ToString();
                    txtSaleQty.Text = "1";
                    discountTextBox.Text = product.Discount.ToString();
                    txtPrice.Text = product.Price.ToString();
                    //if ()
                    //{

                    //}
                    totalPriceTextBox.Text = (product.Price - (product.Price * product.Discount/100)).ToString();
                    addToCateButton.PerformClick();
                }
                else
                {
                    Reset();
                }

            }
            else
            {
                Reset();
            }
        }