Exemple #1
0
        public static void CopyToNewBudgetVersion(object newBudgetId, object oldBudgetId, Dictionary<string, Dictionary<object, object>> replacesByTable)
        {
            var replaceIds = new Dictionary<object, object>();
            var changedDate = DateTime.Now;
            var repository = new DynamicEntityRepository();

            var newVersion = DynamicRepository.GetByEntity("BudgetVersion",
                FilterCriteriaSet.And.Equal(newBudgetId, "BudgetId")
                    .Merge(FilterCriteriaSet.And.Equal(true, "IsCurrent"))).FirstOrDefault();

            var oldVersion = DynamicRepository.GetByEntity("BudgetVersion",
                FilterCriteriaSet.And.Equal(oldBudgetId, "BudgetId")
                    .Merge(FilterCriteriaSet.And.Equal(true, "IsCurrent"))).FirstOrDefault();

            var newBudget = DynamicRepository.GetByEntity("Budget",
                FilterCriteriaSet.And.Equal(newBudgetId, "Id")).FirstOrDefault();

            bool internalTransaction = false;

            if (SharedCommunicationObjects.SharedTransaction == null)
            {
                SharedCommunicationObjects.CreateSharedTransaction();
                internalTransaction = true;
            }

            try
            {
                #region BudgetItems

                var biMetadata = repository.GetEntityMetadataByEntityName("BudgetItem");

                List<dynamic> records =
                    biMetadata.Get(
                        FilterCriteriaSet.And.Equal((Guid) oldVersion.Id, "BudgetVersionId")
                            .NotEqual(true, "IsDeleted")
                            .NotEqual("Deleted", "State")
                            .NotEqual(true, "IsPrevPeriod"));

                foreach (dynamic r in records)
                {
                    var oldId = r.Id;
                    r.Id = Guid.NewGuid();
                    r.BudgetVersionId = newVersion.Id;
                    r.WasBack = false;
                    r.State = "Draft";
                    r.StateName = "Draft";
                    r.EntityRouteId = null;
                    r.ChangedDate = changedDate;

                    //Заменяем ссылки на скопированные в новый бюджет справочники

                    ReplaceToNewIds(replacesByTable, biMetadata, r);

                    replaceIds.Add(oldId, r.Id);
                }

                var dynamicEntities = records.ConvertAll(d=> d as DynamicEntity);

                PrimaryKeyGenerator.SPGenerateKey(biMetadata, dynamicEntities,
                    new Dictionary<string, string>
                    {
                        {"StoredProcedure", "data_get_sequence"},
                        {"ColumnName", "NumberId"},
                        {"Value", "Value"},
                        {"Code", "BudgetItem"}
                    });

                //Перерасчет остатков
                //BudgetItemMethods.InitResidual(biMetadata, null, dynamicEntities);
                DWKitHelper.EMExecute("BudgetItem.InitResidual", biMetadata, null, dynamicEntities);

                DynamicRepository.InsertByEntity("BudgetItem", records);

                #endregion

                //Словарь со всеми заменами Id-ов
                var allReplaces = replacesByTable.Select(replaces => replaces.Value).ToList();
                allReplaces.Add(replaceIds);

                #region Depends
                var tableList = new Dictionary<string, string>
                {
                    {"BudgetItemAllocation", "BudgetItemId"}
                };

                var b = new Budget();
                foreach (var t in tableList)
                {
                    var replaceByPropertyName = new Dictionary<string, Dictionary<object, object>> { { t.Value, replaceIds } };

                    var tMetadata = repository.GetEntityMetadataByEntityName(t.Key);

                    foreach (var att in tMetadata.PlainAttributes.Where(a=>a.IsReference))
                    {
                        if (!replacesByTable.ContainsKey(att.ReferencedTableName))
                            continue;

                        var replace = replacesByTable[att.ReferencedTableName];

                        replaceByPropertyName.Add(att.PropertyName, replace);
                    }

                    var tmpFilter = FilterCriteriaSet.And.Custom(string.Format("{0} in (select Id from BudgetItem WHERE BudgetVersionId = '{1}')", t.Value, (Guid)oldVersion.Id));
                    var replaces = b.CopyDependsEntity(t.Key, (t.Value == "Id" ? null : "Id"), tmpFilter,
                        replaceByPropertyName, true);

                    allReplaces.Add(replaces);

                }
                #endregion

                #region UploadedFiles
                CopyUploadedFiles(replaceIds);
                #endregion

                #region EntityAccess

                //Все замены в один словарь
                var allReplacesDictionary = allReplaces.SelectMany(dict => dict)
                         .ToDictionary(pair => pair.Key, pair => pair.Value);

                CopyEntityAccess(oldVersion, newVersion, allReplacesDictionary);
                #endregion

                #region Created PrevPeriod BudgetItems

                var biIdsForReclc = BudgetItem.SyncFutureBudgetItem((int)newBudget.Name, replaceIds);
                VtbRestCalculator.Calc.RecalculateAccount(biIdsForReclc, newBudget.Id);

                #endregion

                if (internalTransaction)
                    SharedCommunicationObjects.CommitSharedTransaction();
            }
            catch (Exception ex)
            {
                if (internalTransaction)
                    SharedCommunicationObjects.RollbackSharedTransaction();
                throw ex;
            }
            finally
            {
                if (internalTransaction)
                    SharedCommunicationObjects.DestroySharedTransaction();
            }
        }
Exemple #2
0
        private static void CopyBudgetItemDependsOnlyForBudgetItems(Dictionary<object, object> replaceIds,List<Guid> budgetItemIds,
         out Dictionary<object, object> budgetAllocationReplaceIds)
        {
            var tableList = new Dictionary<string, string>
            {
                {"BudgetItemAllocation", "BudgetItemId"},
                {"MetadataEntityVersion", "BaseEntityId"},
                {"WorkflowMultipleSighting", "ProcessId"},
                {"WorkflowProcessInstance", "Id"},
                {"WorkflowProcessInstancePersistence", "ProcessId"},
                {"WorkflowProcessInstanceStatus", "Id"},
                {"WorkflowProcessScheme", "Id"},
                {"WorkflowProcessTransitionHistory", "ProcessId"},
                {"WorkflowHistory", "ProcessId"},
                {"WorkflowInbox", "ProcessId"}
            };

            var b = new Budget();
            budgetAllocationReplaceIds = null;
            foreach (var t in tableList)
            {
                var param = new Dictionary<string, Dictionary<object, object>>();
                param.Add(t.Value, replaceIds);

                var tmpFilter =
                    FilterCriteriaSet.And.In(budgetItemIds, t.Value);
                var dependsReplaceId = b.CopyDependsEntity(t.Key, (t.Value == "Id" ? null : "Id"), tmpFilter, param);

                if (t.Key == "BudgetItemAllocation")
                {
                    budgetAllocationReplaceIds = dependsReplaceId;
                }
            }
        }
Exemple #3
0
        private static void CopyHistory(dynamic currentBV, Dictionary<object, object> replaceIds, dynamic newBV,
            Dictionary<object, object> budgetAllocationReplaceIds)
        {
            List<dynamic> h_records = DynamicRepository.GetByEntity("H_BudgetItem",
                FilterCriteriaSet.And.Equal((Guid) currentBV.Id, "BudgetVersionId"));
            var h_replaceIds = new Dictionary<object, object>();

            for (int i = h_records.Count - 1; i >= 0; i--)
            {
                if (!replaceIds.ContainsKey(h_records[i].VersioningEntityId))
                    h_records.Remove(h_records[i]);
            }

            foreach (dynamic r in h_records)
            {
                var oldId = r.Id;
                r.Id = Guid.NewGuid();
                r.BudgetVersionId = newBV.Id;
                r.VersioningEntityId = replaceIds[r.VersioningEntityId];
                h_replaceIds.Add(oldId, r.Id);
            }
            DynamicRepository.InsertByEntity("H_BudgetItem", h_records);

            Dictionary<string, Dictionary<object, object>> h_param = new Dictionary<string, Dictionary<object, object>>();
            h_param.Add("VersioningBaseEntityId", h_replaceIds);
            if (budgetAllocationReplaceIds != null)
                h_param.Add("VersioningEntityId", budgetAllocationReplaceIds);

            var h_tmpFilter =
                FilterCriteriaSet.And.Custom(string.Format(
                    "{0} in (select Id from H_BudgetItem WHERE BudgetVersionId = '{1}')", "VersioningBaseEntityId",
                    (Guid) currentBV.Id));
            var b = new Budget();
            b.CopyDependsEntity("H_BudgetItemAllocation", "Id", h_tmpFilter, h_param);
        }
Exemple #4
0
        private static void CopyBudgetItemDepends(Dictionary<object, object> replaceIds, dynamic currentBV,
            out Dictionary<object, object> budgetAllocationReplaceIds)
        {
            var tableList = new Dictionary<string, string>();
            tableList.Add("BudgetItemAllocation", "BudgetItemId");
            tableList.Add("MetadataEntityVersion", "BaseEntityId");
            tableList.Add("WorkflowMultipleSighting", "ProcessId");
            tableList.Add("WorkflowProcessInstance", "Id");
            tableList.Add("WorkflowProcessInstancePersistence", "ProcessId");
            tableList.Add("WorkflowProcessInstanceStatus", "Id");
            tableList.Add("WorkflowProcessScheme", "Id");
            tableList.Add("WorkflowProcessTransitionHistory", "ProcessId");
            tableList.Add("WorkflowHistory", "ProcessId");
            tableList.Add("WorkflowInbox", "ProcessId");

            var b = new Budget();
            budgetAllocationReplaceIds = null;
            foreach (var t in tableList)
            {
                var param = new Dictionary<string, Dictionary<object, object>>();
                param.Add(t.Value, replaceIds);

                var tmpFilter =
                    FilterCriteriaSet.And.Custom(
                        string.Format("{0} in (select Id from BudgetItem WHERE BudgetVersionId = '{1}')", t.Value,
                            (Guid) currentBV.Id));
                var dependsReplaceId = b.CopyDependsEntity(t.Key, (t.Value == "Id" ? null : "Id"), tmpFilter, param);

                if (t.Key == "BudgetItemAllocation")
                {
                    budgetAllocationReplaceIds = dependsReplaceId;
                }
            }
        }