Exemplo n.º 1
0
        public bool Delete(int idhaodon, int idhang)
        {
            decimal thanhtiencu = 0;

            try
            {
                using (var db = new QLPhongKaraokeEntities())
                {
                    var x = from s in db.CT_HOADONDV
                            where s.ID_HoaDonDV == idhaodon && s.ID_Hang == idhang && s.Deleted == 0
                            select s;
                    var sv   = x.FirstOrDefault();
                    var hang = (from s in db.HANGs where s.ID == idhang select s).FirstOrDefault();

                    if (sv != null)
                    {
                        thanhtiencu = sv.ThanhTien;
                        sv.Deleted  = 1;
                        db.SaveChanges();

                        // cap nhat so luong ton
                        hang.SLTon += sv.SoLuong;
                        db.SaveChanges();
                        //cap nhat tong tien hoa don
                        var hd = new MMHOADONDV();
                        hd.UpdateTongTien(sv.ID_HoaDonDV, hd.GetOne(sv.ID_HoaDonDV).TongTien - thanhtiencu);
                    }
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        public bool Update(CT_HOADONDV item)
        {
            decimal thanhtiencu = 0;

            try
            {
                using (var db = new QLPhongKaraokeEntities())
                {
                    var x = from s in db.CT_HOADONDV
                            where s.ID_HoaDonDV == item.ID_HoaDonDV && s.ID_Hang == item.ID_Hang && s.Deleted == 0
                            select s;
                    var sv   = x.FirstOrDefault();
                    var hang = (from s in db.HANGs where s.ID == sv.ID_Hang select s).FirstOrDefault();

                    if (sv != null)
                    {
                        thanhtiencu = sv.ThanhTien;
                        var soluongcu = sv.SoLuong;

                        sv.SoLuong   = item.SoLuong;
                        sv.DonGia    = item.DonGia;
                        sv.ThanhTien = item.ThanhTien;
                        db.SaveChanges();

                        //cap nhat soluong
                        hang.SLTon = hang.SLTon + soluongcu - sv.SoLuong;
                        db.SaveChanges();

                        var hd       = new MMHOADONDV(); //khi cap nhat thi cap nhat lai tong tien
                        var tongtien = hd.GetOne(sv.ID_HoaDonDV).TongTien - thanhtiencu + item.ThanhTien;
                        hd.UpdateTongTien(sv.ID_HoaDonDV, tongtien);
                    }
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 3
0
        public bool Insert(CT_HOADONDV item)
        {
            try
            {
                using (var db = new QLPhongKaraokeEntities())
                {
                    var x = from s in db.CT_HOADONDV
                            where s.ID_HoaDonDV == item.ID_HoaDonDV && s.ID_Hang == item.ID_Hang
                            select s;
                    var sv   = x.FirstOrDefault();
                    var hang = (from s in db.HANGs where s.ID == item.ID_Hang select s).FirstOrDefault();

                    if (sv != null) //da co thi cap nhat sl chu khong co them
                    {
                        sv.SoLuong   += item.SoLuong;
                        sv.ThanhTien += item.ThanhTien;
                        db.SaveChanges();
                    }
                    else
                    {
                        db.CT_HOADONDV.Add(item);
                        db.SaveChanges();
                    }
                    //cap nhat so luong ton
                    hang.SLTon -= item.SoLuong;
                    db.SaveChanges();
                    //cap nhat tong tien hoadon
                    var hd = new MMHOADONDV();
                    hd.UpdateTongTien(item.ID_HoaDonDV, hd.GetOne(item.ID_HoaDonDV).TongTien + item.ThanhTien);
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }