public JsonResult CreateForTaskPicker(Guid companyId, Guid? contactId, string caption = "")
        {
            using (var rs = new RequestScope(this))
            {
                var company = rs.DC.Kunders.Single(k => k.Kunde_GUID == companyId);

                var systemArbejdeTaskLog = TaskTypeRepository.GetSystemArbejdeTaskType(rs.DC);
                var invoiceType = KundeRepository.FindStandardFakturaType(company);

                var newTask = TaskRepository.CreateTask(
                    companyId,
                    contactId,
                    caption,
                    string.Empty,
                    rs.DC,
                    invoiceType,
                    systemArbejdeTaskLog,
                    rs.Bruger.BrugerGuid,
                    null);

                newTask.Task_WebCreated = 1;
                newTask.Task_WebVisible = 1;

                rs.DC.Tasks.Add(newTask);

                rs.DC.SaveChanges();

                var newTaskSelectionModel = new TaskSelectionModel(rs.DC, newTask);

                return Json(newTaskSelectionModel, JsonRequestBehavior.AllowGet);
            }
        }
        public RegistrationModel(IORunEntities dc, TaskLog taskLog)
        {
            Id = taskLog.TaskLog_GUID;

            StartDate = taskLog.TaskLog_StartDateTime.Value.AsJavascriptDate();
            StartDateAsString = taskLog.TaskLog_StartDateTime.Value.AsDateString();

            if (taskLog.Task != null)
            {
                SelectedTask = new TaskSelectionModel(dc, taskLog.Task);

                IsTaskSelected = true;
            }
            else
            {
                IsTaskSelected = false;
            }

            // HVORFOR var det et krav at contact også skulle være sat??
            //if (taskLog.TaskLog_CompanyGUID.HasValue && taskLog.TaskLog_CompanyGUID.Value != Guid.Empty &&
            //	taskLog.TaskLog_ContactGUID.HasValue && taskLog.TaskLog_ContactGUID.Value != Guid.Empty)
            if (taskLog.TaskLog_CompanyGUID.HasValue && taskLog.TaskLog_CompanyGUID.Value != Guid.Empty)
            {
                var kunde = dc.Kunders.Single(c => c.Kunde_GUID == taskLog.TaskLog_CompanyGUID.Value);

                SelectedCompany = new CompanySelectionModel()
                {
                    Address = kunde.Kunde_Adresse_1,
                    Id = kunde.Kunde_GUID,
                    Name = kunde.Kunde_Navn_1
                };
            }

            if (taskLog.TaskLog_ContactGUID.HasValue && taskLog.TaskLog_ContactGUID.Value != Guid.Empty)
            {
                var kontakt = dc.Contacts.Single(c => c.Contact_GUID == taskLog.TaskLog_ContactGUID.Value);

                SelectedContact = new ContactSelectionModel()
                {
                    Title = kontakt.Contact_Title,
                    Id = kontakt.Contact_GUID,
                    Name = kontakt.Contact_Name
                };
            }

            InternalText = taskLog.TaskLog_InternalText;
            ExternalText = taskLog.TaskLog_ExternalText;

            TaskLogTypeId = taskLog.TaskLog_ActivityTypeGUID.Value;
            TaskLogTypeName = taskLog.TaskLog_ActivityTypeGUID.HasValue && taskLog.TaskLog_ActivityTypeGUID.Value != Guid.Empty ? dc.TaskLogTypes.Single(tlt => tlt.TLT_GUID == taskLog.TaskLog_ActivityTypeGUID.Value).TLT_Text : string.Empty;

            InvoiceTypeId = taskLog.TaskLog_InvoiceTypeGUID ?? Guid.Empty;
            InvoiceTypeName = taskLog.TaskLog_InvoiceTypeGUID.HasValue && taskLog.TaskLog_InvoiceTypeGUID.Value != Guid.Empty ? dc.InvoiceTypes.Single(tlt => tlt.IT_GUID == taskLog.TaskLog_InvoiceTypeGUID.Value).IT_Name : string.Empty;

            FaktorId = taskLog.TaskLog_FactorGUID != null ? taskLog.TaskLog_FactorGUID.Value : Guid.Empty;
            FaktorName = taskLog.TaskLog_FactorGUID.HasValue && taskLog.TaskLog_FactorGUID.Value != Guid.Empty ? dc.Faktors.Single(tlt => tlt.FAK_GUID == taskLog.TaskLog_FactorGUID.Value).FAK_KortBeskrivelse : string.Empty;

            BillableKm = taskLog.TaskLog_BillKm;
            NonBillableKm = taskLog.TaskLog_NoBillKm;
            BillableTime = taskLog.TaskLog_BillTime;
            NonBillableTime = taskLog.TaskLog_NoBillTime;

            InvoiceNumber = taskLog.TaskLog_InvoiceNumber;
            IsReadonly = string.IsNullOrEmpty(InvoiceNumber) == false || (SelectedTask != null && SelectedTask.IsClosed);
        }
        public JsonResult FindTasks(Guid? companyId, Guid? contactId, string query)
        {
            using (var rs = new RequestScope(this))
            {
                var tasksQuery = rs.DC.Tasks.Where(t => t.Task_IsClosed == 0).AsQueryable();

                if (companyId.HasValue)
                {
                    tasksQuery = tasksQuery.Where(t => t.Task_CompanyGUID == companyId.Value);
                }

                if (contactId.HasValue)
                {
                    tasksQuery = tasksQuery.Where(t => t.Task_ContactGUID == contactId.Value);
                }

                if (string.IsNullOrEmpty(query) == false && query != ".")
                {
                    tasksQuery = tasksQuery.Where(t => t.Task_Caption.Contains(query));
                }

                var result = tasksQuery.ToList().Select(k =>
                {
                    var newTaskSelection = new TaskSelectionModel(rs.DC, k);

                    return newTaskSelection;
                }).
                OrderBy(n => n.Name).ToList();

                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }