//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);
        }
        //Select Locker in Add Rental
        public SelectLockerForm()
        {
            InitializeComponent();

            //Load Locker Type Name into Combo Box 1
            _typeList = Type.Where("status <> 'Disabled'", 0, 100);
            _comboBoxItems.Add(0, "All");
            foreach (Type t in _typeList)
            {
                _comboBoxItems.Add(t.Id, t.Name);
            }
            comboBox1.DataSource    = new BindingSource(_comboBoxItems, null);
            comboBox1.DisplayMember = "Value";
            comboBox1.ValueMember   = "Key";

            comboBox1.SelectedIndex = -1;   //Trigger SelectedIndexChanged event
            comboBox1.SelectedIndex = 0;    //default select all cabinet (ignore locker type)

            //Default select the first available cabinet to load
            List <Cabinet> items = Cabinet.Where("status = 'Available'", 0, 1);

            if (!items.Any())
            {
                LockerPage(0);
            }
            else
            {
                _cabinetId    = items[0].Id;
                textBox1.Text = items[0].Code;
                var locker = new Locker();
                textBox2.Text         = locker.Count(String.Format("cabinet_id = {0} AND status = 'Available'", _cabinetId)).ToString();
                lockerPage.PageNumber = 1;
                LockerPage(_cabinetId);
            }
        }
        private void Button1_Click(object sender, EventArgs e) //Select Cabinet button
        {
            if (listView2.SelectedItems.Count <= 0)
            {
                return;
            }
            ListViewItem lvi = listView2.SelectedItems[0];

            _cabinetId    = Convert.ToInt32(lvi.Text);
            textBox1.Text = lvi.SubItems[1].Text;
            var locker = new Locker();

            textBox2.Text         = locker.Count(String.Format("cabinet_id = {0} AND status = 'Available'", _cabinetId)).ToString();
            lockerPage.PageNumber = 1;
            LockerPage(_cabinetId);
        }
        private void LockerPage(int cabinetId)
        {
            string condition = String.Format("cabinet_id = {0}", cabinetId);

            var locker = new Locker();

            lockerPage.FinalIndex = Convert.ToDouble(locker.Count(condition));
            lockerPage.LastPage   = Convert.ToInt32(Math.Ceiling(lockerPage.FinalIndex / lockerPage.MaxItems));
            lockerPage.PageSetting();
            if (lockerPage.FinalIndex == 0)
            {
                lockerPage.FirstIndex = 0;
                lockerPage.LastIndex  = 0;
                lockerPage.LastPage   = 1;
            }
            if (lockerPage.PageNumber == lockerPage.LastPage)
            {
                lockerPage.LastIndex = (int)lockerPage.FinalIndex;
            }
            toolStripLabel2.Text = String.Format("Page {0} / {1}", lockerPage.PageNumber, lockerPage.LastPage);
            toolStripLabel3.Text = String.Format("Showing result {0}~{1}", lockerPage.FirstIndex, lockerPage.LastIndex);
            ReloadLockerList(lockerPage.IndexLimit, lockerPage.MaxItems, condition);
        }
        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");
            }
        }
        private void Button6_Click(object sender, EventArgs e) //Confirm Payment button
        {
            if (numericUpDown4.Value < numericUpDown3.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;
            }
            numericUpDown5.Value = numericUpDown4.Value - numericUpDown3.Value;

            button5.Hide();
            button6.Hide();
            button7.Show();

            var rental = new Rental
            {
                StartDate  = dateTimePicker1.Value,
                Duration   = Convert.ToInt32(numericUpDown2.Value),
                CustomerID = _customerList[0].Id,
                LockerID   = _lockerList[0].Id,
            };

            rental.Save();

            //Insert access_log for rental
            var log = new AccessLog
            {
                User   = Login.Username,
                Action = "Add",
                Item   = "Rental",
                ItemId = textBox9.Text
            };

            log.Insert();

            _insertComplete = true;

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

            //Check if the 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();
            }

            //Insert rental details into Transaction
            var transaction = new Transaction
            {
                RentalID   = Convert.ToInt32(textBox3.Text),
                CustomerID = rental.CustomerID,
                LockerID   = rental.LockerID,
                TypeName   = _typeList[0].Name,
                TypeRate   = _typeList[0].Rate,
                StartDate  = rental.StartDate,
                Duration   = rental.Duration,
            };

            transaction.Save();
            log = new AccessLog
            {
                User   = "******",
                Action = "Add",
                Item   = "Transaction",
                ItemId = textBox9.Text
            };
            log.Insert();
        }