//Select Locker in Change Locker
        public SelectLockerForm(int lockerId)
        {
            InitializeComponent();

            //Get the size of the locker
            var locker  = new Locker();
            var cabinet = new Cabinet();
            var type    = new Type();

            locker  = locker.Get(lockerId);
            cabinet = cabinet.Get(locker.CabinetID);
            type    = type.Get(cabinet.TypeID);

            //Load Locker Type of the Locker ID in rental into Combo Box 1

            _comboBoxItems.Add(type.Id, type.Name);
            comboBox1.DataSource    = new BindingSource(_comboBoxItems, null);
            comboBox1.DisplayMember = "Value";
            comboBox1.ValueMember   = "Key";

            comboBox1.SelectedIndex = -1;    //Trigger SelectedIndexChanged event
            comboBox1.SelectedIndex = 0;     //select the only type in combo box 1
            comboBox1.Enabled       = false; //Disable locker type (Disable comboBox1)

            //Load the cabinet which contains the old locker by default
            List <Cabinet> items = Cabinet.Where(String.Format("id = {0}", cabinet.Id), 0, 1);

            _cabinetId            = items[0].Id;
            textBox1.Text         = items[0].Code;
            textBox2.Text         = locker.Count(String.Format("cabinet_id = {0} AND status = 'Available'", _cabinetId)).ToString();
            lockerPage.PageNumber = 1;
            LockerPage(_cabinetId);
        }
        private void LoadRentalData(Rental item, bool rentalEnd)
        {
            rentalSettingsList = RentalSettings.All(0, 10);
            selectedRental     = item;

            //Rental
            textBox31.Text = item.Id.ToString();
            textBox32.Text = item.StartDate.ToString("dd-MM-yyyy");
            var endDate = item.StartDate.AddDays(item.Duration);

            textBox33.Text = endDate.ToString("dd-MM-yyyy");
            textBox34.Text = item.Duration.ToString();

            //Customer
            var customer = new Customer();

            customer       = customer.Get(item.CustomerID);
            textBox35.Text = customer.Id.ToString();
            textBox36.Text = customer.Name;
            textBox37.Text = customer.Ic;

            //Locker
            var locker  = new Locker();
            var cabinet = new Cabinet();
            var type    = new Type();

            locker  = locker.Get(item.LockerID);
            cabinet = cabinet.Get(locker.CabinetID);
            type    = type.Get(cabinet.TypeID);

            textBox38.Text = locker.Id.ToString();
            textBox39.Text = locker.Code;
            textBox40.Text = cabinet.Code;
            textBox41.Text = type.Name;

            //Additional Payment
            TimeSpan timeSpan = endDate.Date.Subtract(DateTime.Now.Date);
            int      daysLeft = Convert.ToInt32(timeSpan.Days);

            if (daysLeft >= 0)
            {
                _overdueDays = 0;
            }
            else
            {
                _overdueDays         = -daysLeft;
                _totalFine           = _totalFine + rentalSettingsList[1].SettingValue + (type.Rate * _overdueDays);
                numericUpDown8.Value = _totalFine;
                checkBox1.Checked    = true; //Assign Overdue Check Box as Ticked
            }
            textBox43.Text = _overdueDays.ToString();
        }
        private void LoadRentalData(Rental item)
        {
            //Rental
            textBox2.Text  = item.Id.ToString();
            textBox14.Text = item.StartDate.ToString("dd-MM-yyyy");
            DateTime endDate = item.StartDate.Date.AddDays(item.Duration);

            textBox18.Text = endDate.ToString("dd-MM-yyyy");
            textBox19.Text = item.Duration.ToString();
            TimeSpan timeSpan = endDate.Date.Subtract(DateTime.Now.Date);
            int      daysLeft = Convert.ToInt32(timeSpan.Days);

            textBox20.Text = daysLeft.ToString();
            if (daysLeft < 0)
            {
                textBox21.Text = "Overdue";
            }
            else
            {
                textBox21.Text = "Normal";
            }

            //Customer
            var customer = new Customer();

            customer       = customer.Get(item.CustomerID);
            textBox22.Text = item.CustomerID.ToString();
            textBox23.Text = customer.Name;
            textBox24.Text = customer.Ic;

            //Locker
            var locker  = new Locker();
            var cabinet = new Cabinet();
            var type    = new Type();

            locker  = locker.Get(item.LockerID);
            cabinet = cabinet.Get(locker.CabinetID);
            type    = type.Get(cabinet.TypeID);

            textBox25.Text = item.LockerID.ToString();
            textBox26.Text = locker.Code;
            textBox27.Text = cabinet.Code;
            textBox28.Text = type.Name;
            textBox29.Text = type.Rate.ToString("0.00");

            //Payment
            decimal totalPrice = item.Duration * type.Rate;

            textBox30.Text = totalPrice.ToString("0.00");
        }
        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 Button10_Click(object sender, EventArgs e) //Next button
        {
            if (numericUpDown9.Value < numericUpDown8.Value)
            {
                MessageBox.Show("Input Error: Insufficient Payment." + Environment.NewLine +
                                "Payment amount must be equal or higher than total price.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            numericUpDown10.Value = numericUpDown9.Value - numericUpDown8.Value;

            button11.Hide();
            button10.Hide();
            button12.Show();

            //Delete Rental Log
            var log = new AccessLog()
            {
                User   = Login.Username,
                Action = "End",
                Item   = "Rental",
                ItemId = selectedRental.Id.ToString()
            };

            log.Insert();
            log = new AccessLog()
            {
                User   = "******",
                Action = "Delete from database",
                Item   = "Rental",
                ItemId = selectedRental.Id.ToString()
            };
            log.Insert();

            var transactionItem = Transaction.Where(String.Format("rental_id = {0}", selectedRental.Id), 0, 1);

            transactionItem[0].ReturnDate  = DateTime.Now.Date;
            transactionItem[0].OverdueTime = _overdueDays;
            transactionItem[0].Fine        = numericUpDown8.Value;
            transactionItem[0].Save();

            log = new AccessLog()
            {
                User        = "******",
                Action      = "Update",
                Item        = "Transaction",
                ItemId      = transactionItem[0].Id.ToString(),
                Description = "Return Date: " + transactionItem[0].ReturnDate.ToString("dd-MM-yyyy") +
                              "; Overdue Time: " + _overdueDays + " day; Fine: " + numericUpDown8.Value
            };
            log.Insert();

            //Insert transaction return status
            RentalStatus transReturnStatus = new RentalStatus();

            if (_keyLostFineAdded)
            {
                transReturnStatus.TransactionId = transactionItem[0].Id;
                transReturnStatus.StatusId      = 3;
                transReturnStatus.Insert();
                log = new AccessLog()
                {
                    User        = "******",
                    Action      = "Add",
                    Item        = "Rental Status",
                    ItemId      = transReturnStatus.TransactionId + ", " + transReturnStatus.StatusId,
                    Description = "Return Status: Key Lost"
                };
                log.Insert();
            }
            if (_lockerDamagedFineAdded)
            {
                transReturnStatus.TransactionId = transactionItem[0].Id;
                transReturnStatus.StatusId      = 4;
                transReturnStatus.Insert();
                log = new AccessLog()
                {
                    User        = "******",
                    Action      = "Add",
                    Item        = "Rental Status",
                    ItemId      = transReturnStatus.TransactionId + ", " + transReturnStatus.StatusId,
                    Description = "Return Status: Locker Damaged"
                };
                log.Insert();
            }
            if (checkBox1.Checked)
            {
                transReturnStatus.TransactionId = transactionItem[0].Id;
                transReturnStatus.StatusId      = 2;
                transReturnStatus.Insert();
                log = new AccessLog()
                {
                    User        = "******",
                    Action      = "Add",
                    Item        = "Rental Status",
                    ItemId      = transReturnStatus.TransactionId + ", " + transReturnStatus.StatusId,
                    Description = "Return Status: Overdue"
                };
                log.Insert();
            }

            //Release the occupied / overdue locker
            string lockerStatus = "";
            var    locker       = new Locker();

            locker = locker.Get(selectedRental.LockerID);

            if (locker.IsOverdued())
            {
                lockerStatus = "Overdue";
            }
            else
            {
                lockerStatus = "Occupied";
            }
            if (!_keyLostFineAdded && !_lockerDamagedFineAdded)
            {
                locker.Reset();
                log = new AccessLog()
                {
                    User        = "******",
                    Action      = "Update",
                    Item        = "Locker",
                    ItemId      = locker.Id.ToString(),
                    Description = "Code: " + locker.Code + "; Status: " + lockerStatus + " to Available"
                };
                log.Insert();

                //Check is the cabinet full, if yes, set to available
                var cabinet = new Cabinet();
                cabinet = cabinet.Get(locker.CabinetID);
                if (cabinet.IsFull())
                {
                    cabinet.Restore();

                    log = new AccessLog()
                    {
                        User        = "******",
                        Action      = "Update",
                        Item        = "Cabinet",
                        ItemId      = cabinet.Id.ToString(),
                        Description = "Code: " + cabinet.Code + "; Status: Full to Available"
                    };
                    log.Insert();
                }
            }
            else
            {
                locker.NotAvailable();
                string reason = "";
                if (_keyLostFineAdded && !_lockerDamagedFineAdded)
                {
                    reason += "Key Lost";
                }
                else if (!_keyLostFineAdded && _lockerDamagedFineAdded)
                {
                    reason += "Locker Damaged";
                }
                else
                {
                    reason += "Key Lost & Locker Damaged";
                }
                log = new AccessLog()
                {
                    User        = "******",
                    Action      = "Disable",
                    Item        = "Locker",
                    ItemId      = locker.Id.ToString(),
                    Description = "Code: " + locker.Code + "; Status: " + lockerStatus + " to Not Available; Reason: " + reason
                };
                log.Insert();
            }

            //Delete the rental
            selectedRental.Delete();
        }
        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");
            }
        }