private void Button2_Click(object sender, EventArgs e) //Select Locker button
        {
            var SelectLockerForm = new SelectLockerForm();

            SelectLockerForm.ShowDialog();

            int lockerId  = SelectLockerForm.LockerID;
            int cabinetId = SelectLockerForm.CabinetID;
            int typeId    = SelectLockerForm.TypeID;

            _typeList    = Type.Where(String.Format("id = {0}", typeId), 0, 1);
            _cabinetList = Cabinet.Where(String.Format("id = {0}", cabinetId), 0, 1);
            _lockerList  = Locker.Where(String.Format("id = {0}", lockerId), 0, 1);

            if (!_typeList.Any() || !_cabinetList.Any() || !_lockerList.Any())
            {
                return;
            }

            textBox6.Text        = _lockerList[0].Code;  //Code
            textBox7.Text        = _cabinetList[0].Code; //Cabinet
            textBox8.Text        = _typeList[0].Name;    //Size
            numericUpDown6.Value = _typeList[0].Rate;    //Rate
            _setLocker           = true;
        }
        private void Button9_Click(object sender, EventArgs e) //Change Locker button
        {
            var result = MessageBox.Show("Do you want to change the locker for this rental?", "Change Locker",
                                         MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (result == DialogResult.Yes)
            {   //Check if the rental overdue. If yes, show error message and return.
                var      endDate  = selectedRental.StartDate.AddDays(selectedRental.Duration);
                TimeSpan timeSpan = endDate.Date.Subtract(DateTime.Now.Date);
                int      daysLeft = Convert.ToInt32(timeSpan.Days);
                if (daysLeft < 0)
                {
                    MessageBox.Show("Access Error: Rental Overdued." + Environment.NewLine +
                                    "You cannot change details for an overdued rental.", "Access Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //Assign the old rental data to a temp variable
                int oldLockerId = selectedRental.LockerID;
                var oldLocker   = new Locker();
                oldLocker = oldLocker.Get(oldLockerId);

                //Open Select Locker Form
                var ChangeLockerForm = new SelectLockerForm(selectedRental.LockerID);
                ChangeLockerForm.ShowDialog();

                //If cancel select, return.
                if (!ChangeLockerForm.LockerSelected)
                {
                    return;
                }

                //Get the new selected type, cabinet and locker for the selected locker
                _typeList    = Type.Where(String.Format("id = {0}", ChangeLockerForm.TypeID), 0, 1);
                _cabinetList = Cabinet.Where(String.Format("id = {0}", ChangeLockerForm.CabinetID), 0, 1);
                _lockerList  = Locker.Where(String.Format("id = {0}", ChangeLockerForm.LockerID), 0, 1);

                //Assign the new locker into rental, and save access log
                selectedRental.LockerID = ChangeLockerForm.LockerID;
                selectedRental.Save();
                var log = new AccessLog()
                {
                    User        = Login.Username,
                    Action      = "Update",
                    Item        = "Rental",
                    ItemId      = selectedRental.Id.ToString(),
                    Description = "Locker: " + oldLocker.Code + " to " + _lockerList[0].Code
                };
                log.Insert();

                //Release the old locker (status = available) and insert into access log
                oldLocker.Reset();
                log.User        = "******";
                log.Action      = "Update";
                log.Item        = "Locker";
                log.ItemId      = oldLocker.Id.ToString();
                log.Description = "Code: " + oldLocker.Code + "; Status: Occupied to Available";
                log.Insert();

                //Check if the old cabinet is full. If yes, set the cabinet to available.
                var oldCabinet = new Cabinet();
                oldCabinet = oldCabinet.Get(oldLocker.CabinetID);
                if (oldCabinet.IsFull())
                {
                    oldCabinet.Restore();
                    log.User        = "******";
                    log.Action      = "Update";
                    log.Item        = "Cabinet";
                    log.ItemId      = oldLocker.CabinetID.ToString();
                    log.Description = "Code: " + oldCabinet.Code + "; Status: Full to Available";
                    log.Insert();
                }

                //Set the new locker is occupied, and insert into access log
                _lockerList[0].Occupied();
                log.User        = "******";
                log.Action      = "Update";
                log.Item        = "Locker";
                log.ItemId      = selectedRental.LockerID.ToString();
                log.Description = "Code: " + _lockerList[0].Code + "; Status: Available to Occupied";
                log.Insert();

                //Check if the new cabinet full. If yes, set cabinet to full, and insert into access log.
                var locker        = new Locker();
                int EmptyLockerNo = locker.Count(String.Format("cabinet_id = {0} AND status = 'Available'",
                                                               _cabinetList[0].Id));
                if (EmptyLockerNo <= 0)
                {
                    _cabinetList[0].Full();
                    log.User        = "******";
                    log.Action      = "Update";
                    log.Item        = "Cabinet";
                    log.ItemId      = _cabinetList[0].Id.ToString();
                    log.Description = "Code: " + _cabinetList[0].Code + "; Status: Available to Full";
                    log.Insert();
                }

                //Change the details in transaction and save in access log
                var selectedTrans = Transaction.Where(String.Format("rental_id = {0}", selectedRental.Id), 0, 1);
                selectedTrans[0].LockerID = _lockerList[0].Id;
                selectedTrans[0].ChangeLocker();
                log.User        = "******";
                log.Action      = "Update";
                log.Item        = "Transaction";
                log.ItemId      = selectedTrans[0].Id.ToString();
                log.Description = "Locker: " + oldLocker.Code + " to " + _lockerList[0].Code;
                log.Insert();

                //Change the locker details in the View Rental Details
                textBox25.Text = _lockerList[0].Id.ToString();
                textBox26.Text = _lockerList[0].Code;
                textBox27.Text = _cabinetList[0].Code;
                textBox28.Text = _typeList[0].Name;
                textBox29.Text = _typeList[0].Rate.ToString("0.00");
            }
        }