Esempio n. 1
0
        private void RefreshData()  //làm mới dữ liệu
        {
            //thực hiện việc đánh số cho mẫu dữ liệu được tải ra (ở đây sử dụng ordinalNumber như một biến tạm)
            List <string> name = entities.RoomCategories.Select(p => p.name).ToList();

            for (int i = 0; i < name.Count; i++)
            {
                RoomCategory room = entities.RoomCategories.Find(name[i]);
                room.ordinalNumber = i + 1;
            }
            entities.SaveChanges();

            //truy vấn các trường cần thiết và đổ vào DataGridView
            var ambiguosData = (from p in entities.RoomCategories
                                orderby p.id ascending
                                select new { p.ordinalNumber, p.name, p.kind, p.price, p.roomStatus }).ToList();

            source.DataSource      = ambiguosData;
            dgvRoomList.DataSource = source;

            //đặt tên cho tiêu đề mỗi cột
            dgvRoomList.Columns[0].HeaderText = "Số thứ tự";
            dgvRoomList.Columns[1].HeaderText = "Tên phòng";
            dgvRoomList.Columns[2].HeaderText = "Loại phòng";
            dgvRoomList.Columns[3].HeaderText = "Giá";
            dgvRoomList.Columns[4].HeaderText = "Tình trạng";
        }
Esempio n. 2
0
        private bool EditRoom()                                                                                          //hàm sửa phòng, nếu sửa thành công trả về true, ngược lại false
        {
            if (entities.RoomCategories.Count() != 0)                                                                    //không có phòng nào
            {
                string       name         = dgvRoomCategories.SelectedCells[0].OwningRow.Cells["name"].Value.ToString(); //phòng của row hiện tại
                RoomCategory roomCategory = entities.RoomCategories.Find(name);

                if (roomCategory.name != txtRoomName.Text)
                {
                    MessageBox.Show("Không thể sửa tên phòng. Để sửa vui lòng xóa và tạo lại phòng mới!", "Không thể sửa", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return(false);
                }
                if (entities.Customers.Where(p => p.roomName == roomCategory.name).ToList().Count == 0) //nếu phòng đang được thuê thì ai cho sửa
                {
                    roomCategory.kind  = cbRoomKind.SelectedItem.ToString();
                    roomCategory.price = Convert.ToInt32(txtRoomPrice.Text);
                    roomCategory.note  = txtNote.Text;
                    entities.SaveChanges();
                    return(true);
                }
                else
                {
                    MessageBox.Show("Phòng này đang trong tình trạng được thuê.\n" +
                                    "Để sửa vui lòng thanh toán phòng trước!", "Không thể sửa", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            return(false);
        }
Esempio n. 3
0
        private bool AddRoom()                                                                                                            //hàm thêm phòng, nếu thêm thành công trả về true, ngược lại false
        {
            if (string.IsNullOrEmpty(txtRoomName.Text) || string.IsNullOrWhiteSpace(txtRoomName.Text) || cbRoomKind.SelectedItem == null) //tên phòng rỗng hoặc không có phòng nào trong danh sách phòng
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thiếu thông tin", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }

            //tạo một thể hiện mới cho đối tượng RoomCategory
            RoomCategory roomCategory = new RoomCategory()
            {
                name        = txtRoomName.Text,
                kind        = cbRoomKind.SelectedItem.ToString(),
                price       = Convert.ToDouble(txtRoomPrice.Text),
                note        = txtNote.Text,
                roomStatus  = "Trống",
                countRented = 0,
                rentedDay   = null,
                total       = 0
            };
            //xử lý trường hợp tên phòng hiện tại có tồn tại hay chưa
            List <string> roomName = entities.RoomCategories.Select(p => p.name).ToList();

            if (!roomName.Contains(roomCategory.name))
            {
                entities.RoomCategories.Add(roomCategory);
                entities.SaveChanges();
                return(true);
            }
            else
            {
                MessageBox.Show("Vui lòng chọn một tên phòng khác", "Tên đã tồn tại", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }
        }
Esempio n. 4
0
 private bool AddCustomer()  //thêm khách hàng vào phòng (cho thuê), thêm thành công trả về true
 {
     //ràng buộc các trường dữ liệu phía dưới không được là null, ký tự trắng hay rỗng
     if (string.IsNullOrEmpty(txtCustomerName.Text) || string.IsNullOrWhiteSpace(txtCustomerName.Text) ||
         string.IsNullOrEmpty(txtIdentity.Text) || string.IsNullOrWhiteSpace(txtIdentity.Text) ||
         string.IsNullOrEmpty(txtAddress.Text) || string.IsNullOrWhiteSpace(txtAddress.Text) ||
         string.IsNullOrEmpty(cbCustomerKind.SelectedValue.ToString()))
     {
         MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Không thể thêm", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         return(false);
     }
     else
     {   //ràng buộc phòng đã đầy (biến số người tối đa trong phòng được lấy từ form Danh mục)
         if (entities.Customers.Where(p => p.roomName == cbRoom.SelectedValue.ToString()).ToList().Count >= fRoomCategories.maximumCustomer)
         {
             MessageBox.Show("Phòng đã đầy, vui lòng chọn phòng khác", "Không thể thêm", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             return(false);
         }
         else
         {
             if (entities.Customers.Where(p => p.identityNumber == txtIdentity.Text).SingleOrDefault() != null)
             {
                 MessageBox.Show("Chứng minh nhân dân trùng, vui lòng kiểm tra lại thông tin khách hàng", "Không thể thêm", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                 return(false);
             }
             //dắt này mới bắt đầu thuê
             Customer customer = new Customer()
             {
                 name           = txtCustomerName.Text,
                 kind           = cbCustomerKind.SelectedValue.ToString(),
                 identityNumber = txtIdentity.Text,
                 address        = txtAddress.Text,
                 rentedDay      = dtpRentRoom.Value,
                 roomName       = cbRoom.SelectedValue.ToString()
             };
             entities.Customers.Add(customer);
             //đánh đấu một số thuộc tính của phòng là đã có chủ
             RoomCategory room = entities.RoomCategories.Where(p => p.name == customer.roomName).SingleOrDefault();
             room.rentedDay  = customer.rentedDay;
             room.roomStatus = "Đã được thuê";
             entities.SaveChanges();
         }
     }
     return(true);
 }
Esempio n. 5
0
        private bool DeleteRoom()                            //hàm xóa phòng, nếu xóa thành công trả về true, ngược lại false
        {
            int countRoom = entities.RoomCategories.Count(); //đếm số phòng

            if (int.Parse(countRoom.ToString()) != 0)        //số phòng bằng 0 thì không có gì để xóa
            {
                string       name         = dgvRoomCategories.SelectedCells[0].OwningRow.Cells["name"].Value.ToString();
                RoomCategory roomCategory = entities.RoomCategories.Find(name);

                if (entities.Customers.Where(p => p.roomName == roomCategory.name).ToList().Count == 0) //nếu phòng đang được thuê thì ai cho xóa
                {
                    entities.RoomCategories.Remove(roomCategory);
                    entities.SaveChanges();
                    return(true);
                }

                MessageBox.Show("Phòng này đang trong tình trạng được thuê.\n" +
                                "Để xóa vui lòng thanh toán phòng trước!", "Không thể xóa", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(false);
        }
Esempio n. 6
0
        private void RefreshData()
        {
            //lấy ra danh sách những phòng đang được thuê
            List <string> rentingRoomName = entities.RoomCategories.Where(p => p.rentedDay != null).Select(p => p.name).ToList();

            foreach (string currentRoomName in rentingRoomName)
            {
                //tính số ngày thuê, nếu thuê chưa được một ngày thì cũng tính một ngày
                RoomCategory room       = entities.RoomCategories.Where(p => p.name == currentRoomName).SingleOrDefault();
                int          amountDays = int.Parse((DateTime.Now - (DateTime)room.rentedDay).Days.ToString());
                room.countRented = (amountDays != 0) ? amountDays : 1;
                room.total       = 0;
                //duyệt qua hết những người thuê cùng một phòng, tính tổng hệ số của mỗi người
                List <Customer> presentCustomer = entities.Customers.Where(p => p.roomName == room.name).ToList();
                foreach (var item in presentCustomer)
                {
                    room.total += fRoomCategories.kindAndCoefficient[item.kind];
                }
                room.total *= room.price;                                          //nhân với đơn giá của phòng để được chi phí của phòng
                if (presentCustomer.Count() >= fRoomCategories.surchargeBeginning) //bắt đầu phụ thu khi số khách bằng surchargeBeginning
                {
                    room.total *= fRoomCategories.surchargeRatio;
                }
            }

            //thực hiện việc đánh số cho mẫu dữ liệu được tải ra (ở đây sử dụng ordinalNumber như một biến tạm)
            List <string> name = entities.RoomCategories.Select(p => p.name).ToList();

            for (int i = 0; i < name.Count; i++)
            {
                RoomCategory room = entities.RoomCategories.Find(name[i]);
                room.ordinalNumber = i + 1;
            }
            entities.SaveChanges();

            //lấy dữ liệu ra, đổ vào DataGridView
            var discreteData = from p in entities.RoomCategories.AsEnumerable()
                               where p.roomStatus == "Đã được thuê"
                               orderby p.ordinalNumber ascending
                               select new { p.ordinalNumber, p.name, p.countRented, p.price, p.total };

            source.DataSource      = discreteData.ToList();
            dgvRentRoom.DataSource = source;
            //nếu bảng có dữ liệu thì lấy dòng đầu tiên, truy vấn thông tin khách hàng, đổ vào txtCustomer và txtAddress
            if (discreteData.ToList().Count != 0)
            {
                string   tempName = discreteData.ToList().First().name;
                Customer customer = entities.Customers.Where(p => p.roomName == tempName).FirstOrDefault();
                txtCustomer.Text = customer.name;
                txtAddress.Text  = customer.address;
            }
            else    //nếu không thì gán hai textbox này bằng chuỗi rỗng
            {
                txtCustomer.Text = "";
                txtAddress.Text  = "";
            }

            //đặt tên cho tiêu đề của các cột trong bảng
            dgvRentRoom.Columns[0].HeaderText = "Số thứ tự";
            dgvRentRoom.Columns[1].HeaderText = "Phòng";
            dgvRentRoom.Columns[2].HeaderText = "Số ngày thuê";
            dgvRentRoom.Columns[3].HeaderText = "Đơn giá";
            dgvRentRoom.Columns[4].HeaderText = "Thành tiền";
        }