Beispiel #1
0
 private void detach_tblPhieuThues(tblPhieuThue entity)
 {
     this.SendPropertyChanging();
     entity.tblKhach = null;
 }
Beispiel #2
0
 partial void DeletetblPhieuThue(tblPhieuThue instance);
Beispiel #3
0
 partial void InserttblPhieuThue(tblPhieuThue instance);
Beispiel #4
0
 partial void UpdatetblPhieuThue(tblPhieuThue instance);
        /*
         * private void lvPhong_MouseClick(object sender, MouseEventArgs e)
         * {
         *  foreach (ListViewItem item in lvPhong.Items)
         *  {
         *      if(item.Checked== true)
         *      {
         *          int a = int.Parse(item.SubItems[1].Text);
         *          txtTen.Text = a.ToString();
         *      }
         *  }
         * }
         */
        private void btnLuu_Click(object sender, EventArgs e)
        {
            /*foreach (ListViewItem item in lvPhong.Items)
             * {
             *  if(item.Checked== true)
             *  {
             *      int a = int.Parse(item.SubItems[1].Text);
             *      txtTen.Text = a.ToString();
             *  }
             * }*/
            KhachHangDAO    dao_kh  = new KhachHangDAO();
            ChiTietPhieuDAO dao_ctp = new ChiTietPhieuDAO();
            PhieuThueDAO    dao_pt  = new PhieuThueDAO();

            if (luu == choice_them)
            {
                // them khach hang
                tblKhach k = new tblKhach();
                k.TenKhach = txtTen.Text;
                k.SDT      = txtSDT.Text;
                k.DiaChi   = txtDiaChi.Text;
                k.SoNguoi  = int.Parse(txtSoLuongNguoi.Text);
                if (db.tblKhaches.Count() == db.tblKhaches.Max(x => x.IDKhach))
                {
                    var temp = db.tblKhaches.Max(x => x.IDKhach) + 1;
                    k.IDKhach = temp;
                }
                if (db.tblKhaches.Count() != db.tblKhaches.Max(x => x.IDKhach))
                {
                    while (true)
                    {
                        if (db.tblKhaches.Min(x => x.IDKhach) > 1)
                        {
                            var temp1 = db.tblKhaches.Min(x => x.IDKhach) - 1;
                            k.IDKhach = temp1;
                            break;
                        }
                        for (int i = db.tblKhaches.Min(x => x.IDKhach); i <= db.tblKhaches.Max(x => x.IDKhach); i++)
                        {
                            if (db.tblKhaches.SingleOrDefault(x => x.IDKhach == i) != null)
                            {
                                continue;
                            }
                            if (db.tblKhaches.SingleOrDefault(x => x.IDKhach == i) == null)
                            {
                                k.IDKhach = i;
                                break;
                            }
                        }
                        break;
                    }
                }

                dao_kh.Insert(k);

                // them vao trong phieuthue
                tblPhieuThue pt = new tblPhieuThue();
                pt.IDKhach      = k.IDKhach;
                pt.ThoiGianThue = DateTime.Now;
                pt.ThoiGianTra  = DateTime.Now;
                pt.TongTien     = 0;

                /*for( int i=1; i<db.tblPhieuThues.Max(x=>x.IDPhieuThue);i++)
                 * {
                 *  if( db.tblPhieuThues.SingleOrDefault(x=>x.IDPhieuThue==i)==null)
                 *  {
                 *      pt.IDPhieuThue = i;
                 *  }
                 * }*/
                if (db.tblPhieuThues.Count() == db.tblPhieuThues.Max(x => x.IDPhieuThue))
                {
                    var temp = db.tblPhieuThues.Max(x => x.IDPhieuThue) + 1;
                    pt.IDPhieuThue = temp;
                }
                if (db.tblPhieuThues.Count() != db.tblPhieuThues.Max(x => x.IDPhieuThue))
                {
                    while (true)
                    {
                        if (db.tblPhieuThues.Min(x => x.IDPhieuThue) > 1)
                        {
                            var temp1 = db.tblPhieuThues.Min(x => x.IDPhieuThue) - 1;
                            pt.IDPhieuThue = temp1;
                            break;
                        }
                        for (int i = db.tblPhieuThues.Min(x => x.IDPhieuThue); i <= db.tblPhieuThues.Max(x => x.IDPhieuThue); i++)
                        {
                            if (db.tblPhieuThues.SingleOrDefault(x => x.IDPhieuThue == i) != null)
                            {
                                continue;
                            }
                            if (db.tblPhieuThues.SingleOrDefault(x => x.IDPhieuThue == i) == null)
                            {
                                pt.IDPhieuThue = i;
                                break;
                            }
                        }
                        break;
                    }
                }
                //

                dao_pt.Insert(pt);


                // them phong voi id khach da cho va them vao o tong tien
                // them vao chitietphieuthue
                foreach (ListViewItem item in lvPhong.Items)
                {
                    if (item.Checked == true)
                    {
                        int i_room = int.Parse(item.SubItems[1].Text);

                        tblChiTietPhieu ctp = new tblChiTietPhieu();
                        ctp.IDPhong     = i_room;
                        ctp.IDDichVu    = 0;
                        ctp.IDDoDung    = 0;
                        ctp.SoLuong     = 0;
                        ctp.ThanhTien   = 0;
                        ctp.IDPhieuThue = pt.IDPhieuThue;

                        if (db.tblChiTietPhieux.Count() == db.tblChiTietPhieux.Max(x => x.IDChiTietPhieu))
                        {
                            var temp = db.tblChiTietPhieux.Max(x => x.IDChiTietPhieu) + 1;
                            ctp.IDChiTietPhieu = temp;
                        }
                        if (db.tblChiTietPhieux.Count() != db.tblChiTietPhieux.Max(x => x.IDChiTietPhieu))
                        {
                            while (true)
                            {
                                if (db.tblChiTietPhieux.Min(x => x.IDChiTietPhieu) > 1)
                                {
                                    var temp1 = db.tblChiTietPhieux.Min(x => x.IDChiTietPhieu) - 1;
                                    ctp.IDChiTietPhieu = temp1;
                                    break;
                                }
                                for (int i = db.tblChiTietPhieux.Min(x => x.IDChiTietPhieu); i <= db.tblChiTietPhieux.Max(x => x.IDChiTietPhieu); i++)
                                {
                                    if (db.tblChiTietPhieux.SingleOrDefault(x => x.IDChiTietPhieu == i) != null)
                                    {
                                        continue;
                                    }
                                    if (db.tblChiTietPhieux.SingleOrDefault(x => x.IDChiTietPhieu == i) == null)
                                    {
                                        ctp.IDChiTietPhieu = i;
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                        dao_ctp.Insert(ctp);
                    }
                }
                //luu xong
                luu = 0;
                MoKhoaAll();
                KhoiTaoDataGridView();
                ResetAll();
            }

            if (luu == choice_sua)
            {
                // sua trong bang khachhang
                // sua trong bang co idphong, tuc la bang chitietphieuthue( ma muon co bang chi tiet phieu thue de sua thi lai
                // phai lay duoc idphieuthue co ma khach hang
                DataGridViewRow dr = dgvKhachHang.SelectedRows[0];
                int             id = int.Parse(dr.Cells["IDKhach"].Value.ToString());
                tblKhach        k  = new tblKhach();
                k.IDKhach  = id;
                k.SDT      = txtSDT.Text;
                k.SoNguoi  = int.Parse(txtSoLuongNguoi.Text);
                k.TenKhach = txtTen.Text;
                k.DiaChi   = txtDiaChi.Text;
                dao_kh.Edit(k);
                MessageBox.Show("Bạn sửa thành công!");

                /*
                 * // lay idphieu thue co makhach hang
                 * var phieu = from u in db.tblPhieuThues
                 *          join u1 in db.tblKhaches on u.IDKhach equals u1.IDKhach
                 *          where u1.IDKhach == id
                 *          select new
                 *          {
                 *              u.IDPhieuThue
                 *          };
                 * foreach( var u in phieu)
                 * {
                 *  int a = u.IDPhieuThue;
                 *  // sua trong bang chitietphieuthue co IDPhieuThue, IDPhong=
                 * }
                 */
                luu = 0;
                MoKhoaAll();
                KhoiTaoDataGridView();
                ResetAll();
            }

            if (luu == choice_xoa)
            {
                // xoa thi phai xoa o trong khach, phieu, chitietphieu
                DataGridViewRow dr = dgvKhachHang.SelectedRows[0];
                int             id = int.Parse(dr.Cells["IDKhach"].Value.ToString());
                if (DialogResult.Yes == MessageBox.Show("Bạn chắc không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    //xoa kh
                    dao_kh.Delete(id);
                    //xoa  phieu
                    var phieu = from u in db.tblPhieuThues
                                join u1 in db.tblKhaches on u.IDKhach equals u1.IDKhach
                                where u1.IDKhach == id
                                select new
                    {
                        u.IDPhieuThue
                    };
                    foreach (var u in phieu)
                    {
                        int a = u.IDPhieuThue;
                        // sua trong bang chitietphieuthue co IDPhieuThue, IDPhong=
                        dao_pt.Delete(a);
                        // xoa trong chi tiet phieu
                        dao_ctp.Delete(a);
                    }
                }
                luu = 0;
                MoKhoaAll();
                KhoiTaoDataGridView();
                ResetAll();
            }
        }