private void ReloadLockerList(int count, int offset, string condition)
        {
            listView1.Items.Clear();
            List <Locker> items = Locker.Where(condition, count, offset);

            foreach (Locker locker in items)
            {
                ListViewItem lvi = new ListViewItem(locker.Code);
                lvi.SubItems.Add(locker.Status);
                if (locker.Status == "Available")
                {
                    lvi.ImageIndex = 0;
                }
                else if (locker.Status == "Occupied")
                {
                    lvi.ImageIndex = 1;
                }
                else if (locker.Status == "Not Available")
                {
                    lvi.ImageIndex = 2;
                }
                else
                {
                    lvi.ImageIndex = 3;
                }
                listView1.Items.Add(lvi);
            }
        }
        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 Button3_Click(object sender, EventArgs e) //Select Locker button
        {
            if (listView1.SelectedItems.Count <= 0)
            {
                return;
            }
            ListViewItem  lvi            = listView1.SelectedItems[0];
            string        lockerCode     = String.Format("code = '{0}'", lvi.Text);
            var           locker         = new Locker();
            List <Locker> lockerList     = Locker.Where(lockerCode, 0, 1);
            var           selectedLocker = locker.Get(lockerList[0].Id);

            if (selectedLocker.IsOccupied())
            {
                MessageBox.Show("Error: Locker Occupied" + Environment.NewLine +
                                "You cannot select an occupied locker for the rental process.", "Locker Occupied",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (selectedLocker.IsNotAvailable())
            {
                MessageBox.Show("Error: Locker Not Available" + Environment.NewLine +
                                "You cannot select a not available locker for the rental process.", "Locker Not Available",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                _lockerId  = selectedLocker.Id;
                _cabinetId = selectedLocker.CabinetID;

                var cab  = new Cabinet();
                var item = cab.Get(_cabinetId);
                _typeId = item.TypeID;

                this.Close();
                _lockerSelected = true;
            }
        }
        private void LoadTransactionData(Transaction item)
        {
            //Rental
            textBox1.Text = item.Id.ToString();
            textBox2.Text = item.RentalID.ToString();
            textBox3.Text = item.StartDate.ToString("dd-MM-yyyy");
            DateTime endDate = item.StartDate.Date.AddDays(item.Duration);

            textBox4.Text        = endDate.ToString("dd-MM-yyyy");
            textBox5.Text        = item.Duration.ToString();
            numericUpDown1.Value = item.TypeRate * item.Duration;

            //Customer
            textBox7.Text = item.CustomerID.ToString();
            var cusList = Customer.Where(String.Format("id = {0}", item.CustomerID), 0, 1);

            if (cusList.Any())
            {
                textBox8.Text = cusList[0].Name;
                textBox9.Text = cusList[0].Ic;
            }

            //Locker
            textBox10.Text = item.LockerID.ToString();
            var lockerList = Locker.Where(String.Format("id = {0}", item.LockerID), 0, 1);

            if (lockerList.Any())
            {
                textBox11.Text = lockerList[0].Code;
                var cabList = Cabinet.Where(String.Format("id = {0}", lockerList[0].CabinetID), 0, 1);
                if (cabList.Any())
                {
                    textBox12.Text = cabList[0].Code;
                }
            }
            textBox13.Text       = item.TypeName;
            numericUpDown2.Value = item.TypeRate;

            //End Rental
            var tempReturnDate = item.ReturnDate.ToString("dd-MM-yyyy");

            //If the return date is not initialized, do not show the date.
            if (tempReturnDate != "01-01-0001")
            {
                textBox15.Text = tempReturnDate;
            }
            textBox16.Text       = item.OverdueTime.ToString();
            numericUpDown3.Value = item.Fine;

            //RentalStatus
            if (item.OverdueTime > 0)
            {
                checkBox1.Checked = true;
            }

            List <RentalStatus> statusList = RentalStatus.Where(String.Format("transaction_id = {0}", item.Id), 0, 10);
            var statuses = from selected in statusList
                           where selected.StatusId.ToString().Contains("3")
                           select selected;

            if (statuses.Any())
            {
                checkBox2.Checked = true;
            }

            statuses = from selected in statusList
                       where selected.StatusId.ToString().Contains("4")
                       select selected;

            if (statuses.Any())
            {
                checkBox3.Checked = 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");
            }
        }