public static TaskLog CreateTaskLog(Guid? optionalTaskId, IORunEntities dc, Guid? contextBrugerId, Guid? contextContactId)
        {
            var now = DateTime.Now;

            var newTaskLog = new TaskLog()
            {
                TaskLog_GUID = Guid.NewGuid(),
                TaskLog_CreatedByUserGUID = contextBrugerId,
                TaskLog_CreatedDate = now,
                TaskLog_EndDateTime = now,
                TaskLog_StartDateTime = now,
                TaskLog_UpdatedByUserDate = now,
                TaskLog_UpdatedByUserGUID = contextBrugerId,
                TaskLog_UserGUID = contextBrugerId,
                TaskLog_ActivityTypeGUID = dc.TaskLogTypes.First().TLT_GUID,
                TaskLog_InternalText = String.Empty,
                TaskLog_ExternalText = String.Empty,
                TaskLog_InvoiceTypeGUID = dc.InvoiceTypes.First().IT_GUID,
                TaskLog_WebCreated = 1,
                TaskLog_CreatedByContactGUID = contextContactId,
                TaskLog_UpdatedByContactGUID = contextContactId
            };

            if (optionalTaskId.HasValue)
            {
                var task = dc.Tasks.Single(l => l.Task_GUID == optionalTaskId.Value);
                var taskLogType = dc.TaskTypes.Single(tt => tt.TT_GUID == task.Task_TaskTypeGUID);
                var company = task.Task_CompanyGUID.HasValue
                    ? dc.Kunders.Single(k => k.Kunde_GUID == task.Task_CompanyGUID.Value)
                    : null;
                var invoiceType = task.Task_InvoiceTypeGUID.HasValue
                    ? task.Task_InvoiceTypeGUID
                    : company != null
                        ? company.KundeExtensions.Any() ? company.KundeExtensions.Single().DefaultInvoiceTypeId : null
                        : null;

                newTaskLog.Task = task;
                newTaskLog.TaskLog_CompanyGUID = task.Task_CompanyGUID;
                newTaskLog.TaskLog_ContactGUID = task.Task_ContactGUID;
                newTaskLog.TaskLog_InvoiceTypeGUID = invoiceType;
                newTaskLog.TaskLog_ActivityTypeGUID = taskLogType.TT_DefaultTasklogTypeGUID;
            }

            // Det er bestemt at alle tasklogs skal fødes med "SY_System arbejde" i stedet for
            // Skal nok laves snedigere med en setting hvor man vælger en
            newTaskLog.TaskLog_ActivityTypeGUID = dc.TaskLogTypes.Single(t => t.TLT_Text == "SY_System arbejde").TLT_GUID;

            return newTaskLog;
        }
        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);
        }