/// <summary> /// Clones the week. /// </summary> /// <param name="ownerId">The owner id.</param> /// <param name="srcStartDate">The SRC start date.</param> /// <param name="destStartDate">The dest start date.</param> public static void CloneWeek(int ownerId, DateTime srcStartDate, DateTime destStartDate) { if (!Mediachase.Ibn.License.TimeTrackingModule) throw new Mediachase.Ibn.LicenseRestrictionException(); srcStartDate = srcStartDate.Date; destStartDate = destStartDate.Date; TimeTrackingBlock[] srcBlocks = TimeTrackingBlock.List(FilterElement.EqualElement("OwnerId", ownerId), FilterElement.EqualElement("StartDate", srcStartDate)); using (TransactionScope tran = DataContext.Current.BeginTransaction()) { foreach (TimeTrackingBlock srcBlock in srcBlocks) { TimeTrackingBlock destBlock = new TimeTrackingBlock(); #region Copy TimeTrackingBlock Properties // Set Start Date destBlock.StartDate = destStartDate; // Copy References destBlock.BlockTypeInstanceId = srcBlock.BlockTypeInstanceId; destBlock.mc_StateMachineId = srcBlock.mc_StateMachineId; destBlock.OwnerId = srcBlock.OwnerId; destBlock.ProjectId = srcBlock.ProjectId; // Copy Card destBlock.Card = srcBlock.Card; // Copy Extended properties foreach (MetaObjectProperty exProperty in srcBlock.ExtendedProperties) { if (!exProperty.IsReadOnly) { destBlock.Properties[exProperty.Name].Value = exProperty.Value; } } // Copy Fields destBlock.Title = srcBlock.Title; destBlock.Day1 = 0; destBlock.Day2 = 0; destBlock.Day3 = 0; destBlock.Day4 = 0; destBlock.Day5 = 0; destBlock.Day6 = 0; destBlock.Day7 = 0; #endregion destBlock.Save(); TimeTrackingEntry[] srcEntries = TimeTrackingEntry.List(FilterElement.EqualElement("ParentBlockId", srcBlock.PrimaryKeyId.Value)); foreach (TimeTrackingEntry srcEntry in srcEntries) { TimeTrackingEntry destEntry = new TimeTrackingEntry(); #region Copy TimeTrackingEntry Properties // Set Start Date //destEntry.StartDate = destStartDate; // Referienced Field From TimeTrackingBlock // Copy References destEntry.ParentBlockId = destBlock.PrimaryKeyId.Value; destEntry.BlockTypeInstanceId = srcEntry.BlockTypeInstanceId; destEntry.OwnerId = srcEntry.OwnerId; // Copy Card destEntry.Card = srcEntry.Card; // Copy Extended properties foreach (MetaObjectProperty exProperty in srcEntry.ExtendedProperties) { if (!exProperty.IsReadOnly) { destEntry.Properties[exProperty.Name].Value = exProperty.Value; } } // Copy Fields destEntry.Title = srcEntry.Title; destEntry.Day1 = 0; destEntry.Day2 = 0; destEntry.Day3 = 0; destEntry.Day4 = 0; destEntry.Day5 = 0; destEntry.Day6 = 0; destEntry.Day7 = 0; #endregion destEntry.Save(); } } tran.Commit(); } }
/// <summary> /// Add the TTEntry Item /// </summary> /// <returns></returns> public static TimeTrackingEntry AddEntry(int blockTypeInstanceId, DateTime startDate, int ownerId, string title) { if (!Mediachase.Ibn.License.TimeTrackingModule) throw new Mediachase.Ibn.LicenseRestrictionException(); startDate = startDate.Date; TimeTrackingEntry retVal; using (TransactionScope tran = DataContext.Current.BeginTransaction()) { MetaObject blockInstance = MetaObjectActivator.CreateInstance<MetaObject>(BlockTypeInstanceMetaClassName, blockTypeInstanceId); int blockTypeId = (PrimaryKeyId)blockInstance.Properties[BlockTypeInstanceFieldName_BlockType].Value; MetaObject blockType = MetaObjectActivator.CreateInstance<MetaObject>(BlockTypeMetaClassName, blockTypeId); TimeTrackingBlock block = null; TimeTrackingBlock[] mas = TimeTrackingBlock.List(FilterElement.EqualElement("OwnerId", ownerId), FilterElement.EqualElement("BlockTypeInstanceId", blockTypeInstanceId), FilterElement.EqualElement("StartDate", startDate)); if (mas.Length > 0) { block = mas[0]; if (!Security.CanWrite(block)) throw new AccessDeniedException(); } else { block = new TimeTrackingBlock(); int stateMachineId = (PrimaryKeyId)blockType.Properties[BlockTypeFieldName_StateMachine].Value; string stateName = StateMachineManager.GetStateMachine(BlockMetaClassName, stateMachineId).States[0].Name; MetaObject stateInstance = StateMachineManager.GetState(BlockMetaClassName, stateName); block.mc_StateMachineId = stateMachineId; block.mc_StateId = stateInstance.PrimaryKeyId ?? -1; block.BlockTypeInstanceId = blockTypeInstanceId; block.OwnerId = ownerId; block.StartDate = startDate; block.Title = blockInstance.Properties[BlockTypeFieldName_Title].Value.ToString(); if (blockInstance.Properties[BlockTypeInstanceFieldName_Project].Value != null) block.ProjectId = (PrimaryKeyId)blockInstance.Properties[BlockTypeInstanceFieldName_Project].Value; if (!String.IsNullOrEmpty(blockType.Properties[BlockTypeFieldName_BlockCard].Value.ToString())) block.Properties["Card"].Value = blockType.Properties[BlockTypeFieldName_BlockCard].Value; block.Save(); // Add Owner to Block TimeTrackingManager.AddOwner(block.PrimaryKeyId.Value, ownerId); } TimeTrackingEntry entry = new TimeTrackingEntry(); entry.ParentBlockId = block.PrimaryKeyId ?? -1; entry.Title = title; entry.BlockTypeInstanceId = blockTypeInstanceId; entry.OwnerId = ownerId; if (!String.IsNullOrEmpty(blockType.Properties[BlockTypeFieldName_EntryCard].Value.ToString())) entry.Properties["Card"].Value = blockType.Properties[BlockTypeFieldName_EntryCard].Value; entry.Save(); retVal = entry; tran.Commit(); } return retVal; }
public static void UnRegisterFinances(TimeTrackingBlock block) { if (!Configuration.TimeTrackingModule) throw new Mediachase.Ibn.LicenseRestrictionException(); if (!(bool)block.Properties["AreFinancesRegistered"].Value) throw new NotSupportedException("Finances are not registered."); if (!TimeTrackingBlock.CheckUserRight(block, TimeTrackingManager.Right_UnRegFinances)) throw new Mediachase.Ibn.AccessDeniedException(); using (DbTransaction tran = DbTransaction.Begin()) { ActualFinances.DeleteByBlockId(block.PrimaryKeyId.Value); // O.R. [2008-07-29]: We don't need to check the "Write" right for Block using (SkipSecurityCheckScope scope = Mediachase.Ibn.Data.Services.Security.SkipSecurityCheck()) { block.Properties["AreFinancesRegistered"].Value = false; block.Save(); } // Recalculate TotalMinutes and TotalApproved RecalculateProjectAndObjects(block); tran.Commit(); } }
private static TimeTrackingBlock GetTimeTrackingBlock(int blockTypeInstanceId, DateTime startDate, int ownerId, bool createNew) { startDate = startDate.Date; TimeTrackingBlock block = null; TimeTrackingBlock[] mas = TimeTrackingBlock.List(FilterElement.EqualElement("OwnerId", ownerId), FilterElement.EqualElement("BlockTypeInstanceId", blockTypeInstanceId), FilterElement.EqualElement("StartDate", startDate)); if (mas.Length > 0) { block = mas[0]; } else if (createNew) { block = new TimeTrackingBlock(); MetaObject blockInstance = MetaObjectActivator.CreateInstance<MetaObject>(GetBlockTypeInstanceMetaClass(), blockTypeInstanceId); int blockTypeId = (PrimaryKeyId)blockInstance.Properties["BlockTypeId"].Value; MetaObject blockType = MetaObjectActivator.CreateInstance<MetaObject>(GetBlockTypeMetaClass(), blockTypeId); MetaClass mcBlock = GetBlockMetaClass(); int stateMachineId = (PrimaryKeyId)blockType.Properties["StateMachineId"].Value; string stateName = StateMachineManager.GetStateMachine(mcBlock, stateMachineId).States[0].Name; MetaObject stateInstance = StateMachineManager.GetState(mcBlock, stateName); block.mc_StateMachineId = stateMachineId; block.mc_StateId = stateInstance.PrimaryKeyId ?? -1; block.BlockTypeInstanceId = blockTypeInstanceId; block.OwnerId = ownerId; block.StartDate = startDate; block.Title = blockInstance.Properties["Title"].Value.ToString(); if (blockInstance.Properties["ProjectId"].Value != null) block.ProjectId = (PrimaryKeyId)blockInstance.Properties["ProjectId"].Value; if (!String.IsNullOrEmpty(blockType.Properties["BlockCard"].Value.ToString())) block.Properties["Card"].Value = blockType.Properties["BlockCard"].Value; block.Save(); // Add Owner to Block AddOwner(block.PrimaryKeyId.Value, ownerId); } return block; }
public static void RegisterFinances(TimeTrackingBlock block, string rowId, DateTime regDate) { if (!Configuration.TimeTrackingModule) throw new Mediachase.Ibn.LicenseRestrictionException(); if ((bool)block.Properties["AreFinancesRegistered"].Value) throw new NotSupportedException("Finances are already registered."); if (!TimeTrackingBlock.CheckUserRight(block, TimeTrackingManager.Right_RegFinances)) throw new Mediachase.Ibn.AccessDeniedException(); /// TimeTrackingEntryId, UserId, ObjectTypeId, ObjectId, Title, /// Day1, Day2, Day3, Day4, Day5, Day6, Day7, Total, TotalApproved, Rate, Cost DataTable dt = GetListTimeTrackingItemsForFinances_DataTable(block.BlockTypeInstanceId, block.StartDate, block.OwnerId); using (DbTransaction tran = DbTransaction.Begin()) { // O.R. [2008-07-29]: We don't need to check the "Write" right for Entry and Block using (SkipSecurityCheckScope scope = Mediachase.Ibn.Data.Services.Security.SkipSecurityCheck()) { foreach (DataRow row in dt.Rows) { // O.R. [2008-07-28]: Rate and TotalApproved may contain null values, so we need do assign them TimeTrackingEntry entry = MetaObjectActivator.CreateInstance<TimeTrackingEntry>(TimeTrackingEntry.GetAssignedMetaClass(), (int)row["TimeTrackingEntryId"]); entry.Properties["Rate"].Value = row["Rate"]; entry.Properties["TotalApproved"].Value = row["TotalApproved"]; entry.Save(); double cost = (double)row["Cost"]; if (cost == 0d) continue; // O.R. [2008-07-24] Now we use ProjectId /* ObjectTypes objectType = ObjectTypes.Timesheet; int objectId = (int)row["TimeTrackingEntryId"]; */ ObjectTypes objectType = ObjectTypes.Project; int objectId = block.ProjectId.Value; if (row["ObjectTypeId"] != DBNull.Value && row["ObjectId"] != DBNull.Value) { objectType = (ObjectTypes)row["ObjectTypeId"]; objectId = (int)row["ObjectId"]; } // row["TotalApproved"] alwas has not null value ActualFinances.Create(objectId, objectType, regDate, rowId, cost, row["Title"].ToString(), block.PrimaryKeyId.Value, (double)row["TotalApproved"], (int)block.OwnerId); } block.Properties["AreFinancesRegistered"].Value = true; block.Save(); } // Recalculate TotalMinutes and TotalApproved RecalculateProjectAndObjects(block); tran.Commit(); } }