示例#1
0
 private void btnSave_Click(object sender, EventArgs e)
 {
     try
     {
         int quantity = txtQuantity.Value;
         if (quantity <= 0)
         {
             MessageBox.Show("Số lượng phải là số nguyên dương!", "Thông báo", MessageBoxButtons.OK);
             return;
         }
     }
     catch (Exception)
     {
         MessageBox.Show("Yêu cầu nhập đúng định dạng của thông tin!", "Thông báo", MessageBoxButtons.OK);
     }
     try
     {
         san_pham product = db.san_pham.Find(int.Parse(cbProduct.SelectedValue.ToString()));
         if (txtQuantity.Value > product.so_luong)
         {
             MessageBox.Show("Số lượng sản phẩm không đủ!", "Thông báo", MessageBoxButtons.OK);
             return;
         }
         //Thêm chi tiết hóa đơn
         chi_tiet_hoa_don_ban entity = new chi_tiet_hoa_don_ban();
         entity.ma_hoa_don  = selectedSaleInvoice.ma_hoa_don;
         entity.ma_san_pham = product.ma_san_pham;
         entity.so_luong    = txtQuantity.Value;
         entity.don_gia     = product.gia_san_pham;
         entity.thanh_tien  = entity.so_luong * entity.don_gia;
         db.chi_tiet_hoa_don_ban.Add(entity);
         db.SaveChanges();
         //Cập nhật số lượng sản phẩm
         product.so_luong  -= entity.so_luong;
         product.tinh_trang = (product.so_luong > 0) ? true : false;
         db.SaveChanges();
         //Cập nhật tổng tiền của hóa đơn
         hoa_don_ban saleInvoice = db.hoa_don_ban.Find(selectedSaleInvoice.ma_hoa_don);
         List <chi_tiet_hoa_don_ban> listDetail = db.chi_tiet_hoa_don_ban.Where(x => x.ma_hoa_don == selectedSaleInvoice.ma_hoa_don).ToList();
         double sum = 0;
         foreach (chi_tiet_hoa_don_ban item in listDetail)
         {
             sum += item.thanh_tien;
         }
         saleInvoice.tong_tien = sum;
         db.SaveChanges();
         load();
     }
     catch (Exception)
     {
         MessageBox.Show("Sản phẩm không tồn tại hoặc đã có trong hóa đơn!", "Thông báo", MessageBoxButtons.OK);
     }
 }
示例#2
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            san_pham             product = db.san_pham.Find(int.Parse(cbProduct.SelectedValue.ToString()));
            chi_tiet_hoa_don_ban entity  = db.chi_tiet_hoa_don_ban.SingleOrDefault(x => x.ma_hoa_don == selectedSaleInvoice.ma_hoa_don && x.ma_san_pham == product.ma_san_pham);

            db.chi_tiet_hoa_don_ban.Remove(entity);
            db.SaveChanges();
            //Cập nhật tổng tiền của hóa đơn
            hoa_don_ban saleInvoice = db.hoa_don_ban.Find(selectedSaleInvoice.ma_hoa_don);
            List <chi_tiet_hoa_don_ban> listDetail = db.chi_tiet_hoa_don_ban.Where(x => x.ma_hoa_don == selectedSaleInvoice.ma_hoa_don).ToList();
            double sum = 0;

            foreach (chi_tiet_hoa_don_ban item in listDetail)
            {
                sum += item.thanh_tien;
            }
            saleInvoice.tong_tien = sum;
            db.SaveChanges();
            MessageBox.Show("Xóa dữ liệu thành công!", "Thông báo", MessageBoxButtons.OK);
            load();
        }