private void RefreshDataGridView()
        {
            Area            area;
            Person          person;
            DataGridViewRow row;

            _dgvSearch.Rows.Clear();
            foreach (Booking booking in _filteredList.OrderBy(f => f.CheckIn))
            {
                area   = Area.GetAreaFromId(booking.AreaId, _intBoo.Areas);
                person = Person.GetPersonFromId(booking.UserId, _intBoo.Persons);

                _dgvSearch.Rows.Add();
                row     = _dgvSearch.Rows[_dgvSearch.Rows.Count - 1];
                row.Tag = booking.Id;
                row.Cells[ColumnArea.Index].Value = area != null?area.ToString() : string.Empty;

                row.Cells[ColumnUser.Index].Value = person != null?person.ToString() : string.Empty;

                row.Cells[ColumnConfirmed.Index].Value = booking.Confirmed;
                row.Cells[ColumnPrice.Index].Value     = CRE.GetExpenseFromId(booking.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE)?.Amount;
                row.Cells[ColumnPaid.Index].Value      = CRE.GetExpenseFromId(booking.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE)?.Paid;
                row.Cells[ColumnCheckIn.Index].Value   = booking.CheckIn.ToShortDateString();
                row.Cells[ColumnCheckOut.Index].Value  = booking.CheckOut.ToShortDateString();
                row.Cells[ColumnEdit.Index].Value      = Tools4Libraries.Resources.ResourceIconSet16Default.vcard_edit;
                row.Cells[ColumnDelete.Index].Value    = Tools4Libraries.Resources.ResourceIconSet16Default.vcard_delete;
                row.Cells[ColumnDetails.Index].Value   = Tools4Libraries.Resources.ResourceIconSet16Default.vcard;
            }

            _dgvSearch.Visible = _dgvSearch.Rows.Count != 0;
            _dgvSearch.Height  = (_dgvSearch.Rows.Count * 22) + _dgvSearch.ColumnHeadersHeight;
        }
 public override void RefreshData()
 {
     if (_intBoo != null)
     {
         LoadArea();
         LoadPerson();
         if (_intBoo.CurrentBooking != null)
         {
             comboBoxPlace.SelectedItem   = _intBoo.CurrentBooking.Place;
             dateTimePickerCheckOut.Value = _intBoo.CurrentBooking.CheckOut;
             dateTimePickerCheckIn.Value  = _intBoo.CurrentBooking.CheckIn;
             textBoxPrice.Text            = CRE.GetExpenseFromId(_intBoo.CurrentBooking.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE)?.Amount.ToString();
             checkBoxConfirmed.Checked    = _intBoo.CurrentBooking.Confirmed;
             comboBoxArea.SelectedItem    = Area.GetAreaFromId(_intBoo.CurrentBooking.AreaId, _intBoo.Areas);
             LoadPayment();
             if (_intBoo.CurrentBooking.UserId != null)
             {
                 comboBoxPerson.SelectedItem = Person.GetPersonFromId(_intBoo.CurrentBooking.UserId, _intBoo.Persons);
             }
         }
         else
         {
             dateTimePickerCheckOut.Value = DateTime.Now.AddDays(1);
             dateTimePickerCheckIn.Value  = DateTime.Now;
         }
     }
     GetPrice();
 }
        private void UpdateCurrentBook()
        {
            Movement mov;

            if (_intBoo.CurrentBooking == null)
            {
                _intBoo.CurrentBooking = new Booking();
            }
            if (string.IsNullOrEmpty(_intBoo.CurrentBooking.ExpenseId))
            {
                CRE exp = new CRE();
                _intBoo.CurrentBooking.ExpenseId = exp.Id;
                _intBoo.CurrentFinancialActivity.ListCRE.Add(exp);
            }

            try
            {
                CRE    exp          = CRE.GetExpenseFromId(_intBoo.CurrentBooking.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE);
                Area   filterArea   = (!string.IsNullOrEmpty(comboBoxArea.Text)) ? (Area)comboBoxArea.SelectedItem : null;
                Person filterPerson = (!string.IsNullOrEmpty(comboBoxPerson.Text)) ? (Person)comboBoxPerson.SelectedItem : null;

                if (filterArea != null)
                {
                    _intBoo.CurrentBooking.AreaId = filterArea.Id;
                }
                if (filterPerson != null)
                {
                    _intBoo.CurrentBooking.UserId = filterPerson.Id;
                }
                _intBoo.CurrentBooking.CheckIn   = dateTimePickerCheckIn.Value;
                _intBoo.CurrentBooking.CheckOut  = dateTimePickerCheckOut.Value;
                _intBoo.CurrentBooking.Confirmed = checkBoxConfirmed.Checked;
                _intBoo.CurrentBooking.Place     = (comboBoxPlace.SelectedItem == null) ? string.Empty : comboBoxPlace.SelectedItem.ToString();

                exp.Amount    = (string.IsNullOrEmpty(textBoxPrice.Text)) ? 0 : double.Parse(textBoxPrice.Text);
                exp.StartDate = dateTimePickerCheckIn.Value;
                exp.EndDate   = dateTimePickerCheckOut.Value;

                CRE.GetExpenseFromId(_intBoo.CurrentBooking.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE).Movements.Clear();
                foreach (DataGridViewRow row in _dataGridViewMovement.Rows)
                {
                    mov           = new Movement();
                    mov.StartDate = DateTime.Parse(row.Cells[ColumnDate.Index].Value.ToString());
                    mov.UserId.Add(Person.GetUserByText(row.Cells[ColumnUser.Index].Value, _intBoo.Persons)?.Id);
                    mov.Amount = float.Parse(row.Cells[ColumnAmount.Index].Value.ToString());
                    mov.Gop    = (Movement.GOP)Enum.Parse(typeof(Movement.GOP), row.Cells[ColumnSupport.Index].Value.ToString());
                    CRE.GetExpenseFromId(_intBoo.CurrentBooking.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE).Movements.Add(mov);
                }
            }
            catch (Exception)
            {
            }
        }
        private void Filter()
        {
            Area   filterArea   = comboBoxArea.SelectedItem.ToString() != "All" ? (Area)comboBoxArea.SelectedItem : null;
            Person filterPerson = comboBoxUsers.SelectedItem.ToString() != "All" ? (Person)comboBoxUsers.SelectedItem : null;

            _filteredList = _intBoo.Bookings;

            if (filterArea != null)
            {
                _filteredList = _filteredList.Where(f => f.AreaId.Equals(filterArea.Id)).ToList();
            }
            if (filterPerson != null)
            {
                _filteredList = _filteredList.Where(f => f.UserId.Equals(filterPerson.Id)).ToList();
            }
            if (!dateTimePickerEnd.Value.Equals(dateTimePickerEnd.MaxDate))
            {
                _filteredList = _filteredList.Where(f => f.CheckOut.Date <= dateTimePickerEnd.Value.Date).ToList();
            }
            if (!dateTimePickerStart.Value.Equals(dateTimePickerStart.MinDate))
            {
                _filteredList = _filteredList.Where(f => f.CheckIn.Date >= dateTimePickerStart.Value.Date).ToList();
            }

            if (checkBoxCompletedPaiements.Checked && !checkBoxNonCompletedPaiement.Checked)
            {
                _filteredList = _filteredList.Where(f => CRE.GetExpenseFromId(f.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE)?.Paid >= CRE.GetExpenseFromId(f.Id, _intBoo.CurrentFinancialActivity.ListCRE)?.Amount).ToList();
            }
            if (!checkBoxCompletedPaiements.Checked && checkBoxNonCompletedPaiement.Checked)
            {
                _filteredList = _filteredList.Where(f => CRE.GetExpenseFromId(f.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE)?.Paid < CRE.GetExpenseFromId(f.Id, _intBoo.CurrentFinancialActivity.ListCRE)?.Amount).ToList();
            }
            if (checkBoxConfirmedbooking.Checked && !checkBoxNonConfirmedbooking.Checked)
            {
                _filteredList = _filteredList.Where(f => f.Confirmed == true).ToList();
            }
            if (!checkBoxConfirmedbooking.Checked && checkBoxNonConfirmedbooking.Checked)
            {
                _filteredList = _filteredList.Where(f => f.Confirmed == false).ToList();
            }

            if (numericUpDownMaxPrice.Value != numericUpDownMaxPrice.Maximum)
            {
                _filteredList = _filteredList.Where(f => CRE.GetExpenseFromId(f.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE)?.Amount <= (double)numericUpDownMaxPrice.Value).ToList();
            }
            if (numericUpDownMinPrice.Value != numericUpDownMinPrice.Minimum)
            {
                _filteredList = _filteredList.Where(f => CRE.GetExpenseFromId(f.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE)?.Amount >= (double)numericUpDownMinPrice.Value).ToList();
            }

            RefreshDataGridView();
        }
        private void LoadPayment()
        {
            DataGridViewRow row;

            _dataGridViewMovement.Rows.Clear();
            var expense = CRE.GetExpenseFromId(_intBoo.CurrentBooking.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE);

            if (expense != null)
            {
                foreach (Movement mov in expense.Movements)
                {
                    _dataGridViewMovement.Rows.Add();
                    row = _dataGridViewMovement.Rows[_dataGridViewMovement.Rows.Count - 1];
                    row.Cells[ColumnUser.Index].Value    = Person.GetPersonFromId(mov.UserId?.FirstOrDefault(), _intBoo.Persons);
                    row.Cells[ColumnSupport.Index].Value = mov.Gop;
                    row.Cells[ColumnAmount.Index].Value  = mov.Amount;
                    row.Cells[ColumnDate.Index].Value    = mov.StartDate.ToShortDateString();
                    row.Cells[ColumnEdit.Index].Value    = Tools4Libraries.Resources.ResourceIconSet16Default.cog_edit;
                    row.Cells[ColumnDelete.Index].Value  = Tools4Libraries.Resources.ResourceIconSet16Default.cross;
                }
            }
        }
        private void AddPayment()
        {
            DataGridViewRow row;
            Movement        mov = new Movement();

            _intBoo.CurrentUser = (Person)comboBoxPerson.SelectedItem;
            mov.Amount          = float.Parse(textBoxNewPayment.Text);
            mov.UserId.Add(_intBoo.CurrentUser.Id);
            mov.StartDate = DateTime.Now;
            mov.Gop       = (Movement.GOP)Enum.Parse(typeof(Movement.GOP), comboBoxCash.SelectedItem.ToString());

            if (_intBoo.CurrentBooking.ExpenseId == null)
            {
                CRE exp = new CRE();
                exp.StartDate   = _intBoo.CurrentBooking.CheckIn;
                exp.EndDate     = _intBoo.CurrentBooking.CheckOut;
                exp.Description = textBoxDescription.Text;
                //exp.Save(_intBoo.CurrentFinancialActivity.PathActivity);
                exp.Movements.Add(mov);
                _intBoo.CurrentBooking.ExpenseId = exp.Id;
                _intBoo.CurrentFinancialActivity.ListCRE.Add(exp);
            }
            else
            {
                CRE.GetExpenseFromId(_intBoo.CurrentBooking.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE).Movements.Add(mov);
            }

            _dataGridViewMovement.Rows.Add();
            row = _dataGridViewMovement.Rows[_dataGridViewMovement.Rows.Count - 1];
            row.Cells[ColumnUser.Index].Value    = (Person)comboBoxPerson.SelectedItem;
            row.Cells[ColumnSupport.Index].Value = mov.Gop;
            row.Cells[ColumnAmount.Index].Value  = mov.Amount;
            row.Cells[ColumnDate.Index].Value    = mov.StartDate.ToShortDateString();
            row.Cells[ColumnEdit.Index].Value    = Tools4Libraries.Resources.ResourceIconSet16Default.cog_edit;
            row.Cells[ColumnDelete.Index].Value  = Tools4Libraries.Resources.ResourceIconSet16Default.cross;

            SaveBook();
        }
        private void LoadBookingsPayment()
        {
            int            indexRow;
            int            seuilAM, seuilPM;
            int            paidAM, paidPM;
            int            percentAM, percentPM;
            List <Booking> bookingContinuous;
            List <Booking> bookingCheckIn;
            List <Booking> bookingCheckOut;

            int[] indexColumns;

            foreach (Booking booking in _intBoo.Bookings)
            {
                var res = (from r in _dataGridViewPreview.Rows.Cast <DataGridViewRow>() where (((Area)r.Tag).Id).Equals(booking.AreaId) select r.Index).ToList();
                if (res.Count > 0)
                {
                    indexRow     = res.First();
                    indexColumns = (from c in _dataGridViewPreview.Columns.Cast <DataGridViewColumn>() where ((DateTime)c.Tag) >= booking.CheckIn && ((DateTime)c.Tag) <= booking.CheckOut.AddDays(1) select c.Index).ToArray();

                    foreach (int indexColumn in indexColumns)
                    {
                        bookingContinuous = _intBoo.Bookings.Where(b =>
                                                                   b.CheckIn.Date <((DateTime)_dataGridViewPreview.Columns[indexColumn].Tag).Date &&
                                                                                   b.CheckOut.Date> ((DateTime)_dataGridViewPreview.Columns[indexColumn].Tag).Date&&
                                                                   b.AreaId.Equals(((Area)_dataGridViewPreview.Rows[indexRow].Tag).Id)).ToList();

                        bookingCheckIn = _intBoo.Bookings.Where(b =>
                                                                b.CheckIn.Date == ((DateTime)_dataGridViewPreview.Columns[indexColumn].Tag).Date &&
                                                                b.CheckOut.Date > ((DateTime)_dataGridViewPreview.Columns[indexColumn].Tag).Date &&
                                                                b.AreaId.Equals(((Area)_dataGridViewPreview.Rows[indexRow].Tag).Id)).ToList();
                        bookingCheckIn.AddRange(bookingContinuous);

                        bookingCheckOut = _intBoo.Bookings.Where(b =>
                                                                 b.CheckIn.Date < ((DateTime)_dataGridViewPreview.Columns[indexColumn].Tag).Date &&
                                                                 b.CheckOut.Date == ((DateTime)_dataGridViewPreview.Columns[indexColumn].Tag).Date &&
                                                                 b.AreaId.Equals(((Area)_dataGridViewPreview.Rows[indexRow].Tag).Id)).ToList();
                        bookingCheckOut.AddRange(bookingContinuous);

                        paidPM    = bookingCheckIn.Where(b => CRE.GetExpenseFromId(b.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE).Amount == CRE.GetExpenseFromId(b.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE).Paid).Count();
                        paidAM    = bookingCheckOut.Where(b => CRE.GetExpenseFromId(b.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE).Amount == CRE.GetExpenseFromId(b.ExpenseId, _intBoo.CurrentFinancialActivity.ListCRE).Paid).Count();
                        percentAM = (bookingCheckOut.Count > 0) ? ((paidAM * 100) / bookingCheckOut.Count) : 0;
                        percentPM = (bookingCheckIn.Count > 0) ? ((paidPM * 100) / bookingCheckIn.Count) : 0;

                        if (bookingCheckIn.Count > 0 && percentAM == 100)
                        {
                            seuilAM = 2;
                        }
                        else if (bookingCheckIn.Count > 0 && percentAM > 50)
                        {
                            seuilAM = 0;
                        }
                        else if (bookingCheckIn.Count > 0)
                        {
                            seuilAM = 1;
                        }
                        else
                        {
                            seuilAM = 3;
                        }

                        if (bookingCheckOut.Count > 0 && percentPM == 100)
                        {
                            seuilPM = 2;
                        }
                        else if (bookingCheckOut.Count > 0 && percentPM > 50)
                        {
                            seuilPM = 0;
                        }
                        else if (bookingCheckOut.Count > 0)
                        {
                            seuilPM = 1;
                        }
                        else
                        {
                            seuilPM = 3;
                        }

                        (_dataGridViewPreview.Rows[indexRow].Cells[indexColumn] as TextAndImageCell).Value = string.Format("AM {0} - PM {1}", (bookingCheckOut.Count > 0) ? percentAM.ToString() + "%" : "N/A", (bookingCheckIn.Count > 0) ? percentPM.ToString() + "%" : "N/A");

                        ProcessColoration(bookingCheckIn.Count, bookingCheckOut.Count, seuilAM, seuilPM, indexRow, indexColumn);
                    }
                }
            }
        }