Ejemplo n.º 1
0
        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();
            }
        }