public int CreateNewBusinessTrip(BusinessTripDTO businessTrip, string userName)
        {
            User user = repo.Users.UsersQueryable.FirstOrDefault(u => u.UserName == userName);

            BusinessTrip trip = repo.BusinessTrips.Create(new BusinessTrip() {
                Title = businessTrip.Title,
                Date = businessTrip.Date.ParseAppString(),
                BusinessReason = businessTrip.BusinessReason,
                BusinessPurpose = businessTrip.BusinessPurpose,
                Notes = businessTrip.Notes,
                User = user
            });

            if (businessTrip.Subsistence != null)
            {
                CreateBusinessTripSubsistence(trip, businessTrip);
            }

            List<Expense> expenses = new List<Expense>();
            if (businessTrip.Expenses != null)
            {
                foreach (ExpenseDTO expDto in businessTrip.Expenses)
                {
                    Expense expense = new Expense();
                    expense.Trip = trip;
                    expense.Type = repo.Dictionaries.GetExpenseType(expDto.ExpenseTypeId);
                    expense.Date = expDto.Date.ParseAppString();
                    expense.City = expDto.City;
                    expense.Amount = expDto.Amount;
                    expense.AmountPLN = expDto.AmountPLN;
                    expense.Country = repo.Dictionaries.GetCountry(expDto.CountryId);
                    expense.CurrencyCode = expDto.CurrencyCode;
                    expense.ExchangeRate = expDto.ExchangeRate;
                    CurrencyRate systemRate = currenciesTasks.GetCurrencyRateForDay(expense.CurrencyCode, expense.Date.Date);
                    if (Math.Abs(systemRate.ExchangeRate - expense.ExchangeRate) > 0.0001)
                        expense.ExchangeRateModifiedByUser = true;
                    else
                        expense.ExchangeRateModifiedByUser = false;

                    expense.VATRate = expDto.VATRate;
                    expense.Notes = expDto.Notes;
                    expense.DoNotReturn = expDto.DoNotReturn;
                    expense.DocumentType = repo.Dictionaries.GetExpenseDocumentType(expDto.ExpenseDocumentTypeId);
                    expenses.Add(expense);
                }

                repo.Expenses.CreateSet(expenses);
            }

            if (businessTrip.MileageAllowances != null)
            {
                List<MileageAllowance> mileageAllowances = new List<MileageAllowance>();
                foreach (MileageAllowanceDTO maDto in businessTrip.MileageAllowances)
                {
                    MileageAllowance ma = new MileageAllowance();
                    ma.Trip = trip;
                    ma.Date = maDto.Date.ParseAppString();
                    ma.Distance = maDto.Distance;
                    ma.Amount = maDto.Amount;
                    ma.Notes = maDto.Notes;
                    ma.Type = repo.Dictionaries.GetVehicleType(maDto.VehicleTypeId);

                    mileageAllowances.Add(ma);
                }

                repo.MileageAllowances.CreateSet(mileageAllowances);
            }

            this.repo.SaveChanges();
            return trip.Id;
        }
 public Expense Create(Expense expense)
 {
     return this.context.Expenses.Add(expense);
 }
        public void UpdateBusinessTripExpenses(BusinessTrip trip, BusinessTripDTO businessTripDto)
        {
            if (businessTripDto.Expenses != null)
            {
                foreach (Expense expense in trip.Expenses
                    .Where(e => businessTripDto.Expenses
                        .Any(edto => edto.ExpenseId == e.Id)))
                {//Update expenses that exist both in database and in dto
                    ExpenseDTO edto = businessTripDto.Expenses.First(expenseDto => expenseDto.ExpenseId == expense.Id);
                    expense.Type = repo.Dictionaries.GetExpenseType(edto.ExpenseTypeId);
                    expense.Date = edto.Date.ParseAppString();
                    expense.City = edto.City;
                    expense.Amount = edto.Amount;
                    expense.AmountPLN = edto.AmountPLN;
                    //expense.CountryId = expDto.CountryId;
                    expense.Country = repo.Dictionaries.GetCountry(edto.CountryId);
                    expense.CurrencyCode = edto.CurrencyCode;
                    expense.ExchangeRate = edto.ExchangeRate;
                    expense.ExchangeRateModifiedByUser = edto.ExchangeRateModifiedByUser;
                    expense.VATRate = edto.VATRate;
                    expense.Notes = edto.Notes;
                    expense.DocumentType = repo.Dictionaries.GetExpenseDocumentType(edto.ExpenseDocumentTypeId);
                }

                //Remove those that exist in db but don't exist in dto
                repo.Expenses.RemoveSet(trip.Expenses
                    .Where(e => !businessTripDto.Expenses
                        .Any(edto => edto.ExpenseId == e.Id)));

                foreach (ExpenseDTO expDto in businessTripDto.Expenses.Where(edto => !trip.Expenses.Any(exp => exp.Id == edto.ExpenseId)))
                {//Add those that exist in dto but don't exist in db
                    Expense expense = new Expense();
                    expense.Trip = trip;
                    expense.Type = repo.Dictionaries.GetExpenseType(expDto.ExpenseTypeId);
                    expense.Date = expDto.Date.ParseAppString();
                    expense.City = expDto.City;
                    expense.Amount = expDto.Amount;
                    expense.AmountPLN = expDto.AmountPLN;
                    //expense.CountryId = expDto.CountryId;
                    expense.Country = repo.Dictionaries.GetCountry(expDto.CountryId);
                    expense.CurrencyCode = expDto.CurrencyCode;
                    expense.ExchangeRate = expDto.ExchangeRate;
                    expense.ExchangeRateModifiedByUser = expDto.ExchangeRateModifiedByUser;
                    expense.VATRate = expDto.VATRate;
                    expense.Notes = expDto.Notes;
                    expense.DoNotReturn = expDto.DoNotReturn;
                    expense.DocumentType = repo.Dictionaries.GetExpenseDocumentType(expDto.ExpenseDocumentTypeId);
                    trip.Expenses.Add(expense);
                }
            }
        }