//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);
            }
        }
        //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 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 ReloadSmallCabinetList(int count, int offset, string condition)
        {
            listView2.Items.Clear();

            List <Cabinet> items = Cabinet.Where(condition, count, offset);

            foreach (Cabinet cab in items)
            {
                ListViewItem lvi = new ListViewItem(cab.Id.ToString());
                lvi.SubItems.Add(cab.Code);
                lvi.SubItems.Add(cab.Status);

                listView2.Items.Add(lvi);
            }
        }
        private void ComboBox1_TextChanged(object sender, EventArgs e) //Auto generate Cabinet Code
        {
            if (comboBox1.SelectedIndex < 0)
            {
                return;
            }

            //Select Code for Cabinet Locker Type
            var item = from selected in _typeList
                       where selected.Name.Contains(comboBox1.Text)
                       select selected;

            _sizeCode = item.First().Code;

            //Auto generate locker code for each cabinet
            int            newCabCodeNo = 0;
            string         cabCond      = "id = (SELECT MAX(id) FROM cabinet WHERE type_id = {0})";
            List <Cabinet> cabList      = Cabinet.Where(String.Format(cabCond, item.First().Id), 0, 1);

            if (!cabList.Any())
            {
                newCabCodeNo = 1;
            }
            else
            {
                string currCabCode   = cabList[0].Code;
                string currCabCodeNo = String.Empty;
                for (int i = 0; i < currCabCode.Length; i++)
                {
                    if (Char.IsDigit(currCabCode[i]))
                    {
                        currCabCodeNo += currCabCode[i];
                    }
                }
                newCabCodeNo = Convert.ToInt32(currCabCodeNo) + 1;
            }
            textBox2.Text = String.Format("{0}-{1}", _sizeCode, newCabCodeNo.ToString("D2"));
        }
        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");
            }
        }