public ChiTietHD CT_MatHang(string MaHang)
        {
            ChiTietHD ct_HD = new ChiTietHD();

            var ct_hang = (from ch in qa.CT_MatHangs
                           join h in qa.MATHANGs on ch.TB_MH equals h.MAHANG
                           where ch.MAHANG == MaHang
                           select new { ch.MAHANG, h.TENMH, ch.MaMau, ch.MaSize, h.DONVI, h.GIABANLE }).Skip(0).Take(1);

            foreach (var a in ct_hang)
            {
                ct_HD.MAHANG   = a.MAHANG;
                ct_HD.TENMH    = a.TENMH;
                ct_HD.DONVI    = a.DONVI;
                ct_HD.GIABANLE = a.GIABANLE;
                ct_HD.MaMau    = a.MaMau;
                ct_HD.MaSize   = a.MaSize;
            }
            return(ct_HD);
        }
Ejemplo n.º 2
0
        private void btn_them_Click(object sender, EventArgs e)
        {
            Object[] ob;
            string   MaHang = gl_hang.EditValue.ToString();


            if (MaHang == string.Empty)
            {
                MessageBox.Show("Bạn chưa chọn sản phẩm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int _soLuong = 1;

            int.TryParse(txt_soluong.Text, out _soLuong);

            if (dal.KiemTra_SoLuong(MaHang, _soLuong) == 0)
            {
                return;
            }


            try
            {
                DAL.ChiTietHD ct_HD = new DAL.ChiTietHD();



                if (MaHang.Contains('-') == true)
                {
                    ct_HD = dal.CT_MatHang(MaHang);

                    ob = new object[] { ct_HD.MAHANG, ct_HD.TENMH, ct_HD.MaMau, ct_HD.MaSize, ct_HD.DONVI, _soLuong.ToString(), ct_HD.GIABANLE, ct_HD.GIABANLE };
                }
                else
                {
                    DAL.MATHANG mh = dal.Lay_MatHang(MaHang);
                    ob = new object[] { mh.MAHANG, mh.TENMH, "", "", mh.DONVI, _soLuong.ToString(), mh.GIABANLE, mh.GIABANLE };
                }
                gl_hang.Text = string.Empty;
                try
                {
                    if (dgv_banhang.Rows.Count == 0)
                    {//Thêm sp
                        dgv_banhang.Rows.Add(ob);
                    }
                    else
                    {
                        for (int i = 0; i < dgv_banhang.Rows.Count; i++)
                        {
                            if (String.Compare(dgv_banhang.Rows[i].Cells["MaHang"].Value.ToString(), MaHang.ToString(), true) == 0)
                            {
                                if (dal.KiemTra_SoLuong(MaHang, int.Parse(dgv_banhang.Rows[i].Cells["SoLuong"].Value.ToString()) + _soLuong) == 0)
                                {
                                    return;
                                }
                                dgv_banhang.Rows[i].Cells["SoLuong"].Value = int.Parse(dgv_banhang.Rows[i].Cells["SoLuong"].Value.ToString()) + _soLuong;
                                int    soluong = int.Parse(dgv_banhang.Rows[i].Cells["SoLuong"].Value.ToString());
                                double giaban  = double.Parse(dgv_banhang.Rows[i].Cells["Gia"].Value.ToString());
                                dgv_banhang.Rows[i].Cells["ThanhTien"].Value = soluong * giaban;
                                return;
                            }
                        }
                        dgv_banhang.Rows.Add(ob);
                    }
                }
                catch
                {
                    MessageBox.Show("Bạn chưa chọn sản phẩm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch
            {
                gl_hang.Text = string.Empty;
            }

            txt_soluong.Text = "1";
        }