Ejemplo n.º 1
0
        private S_I_ProjectInfo AddProject(T_CP_TaskNotice entity, S_I_Engineering engineering, T_CP_TaskNotice_PhaseDetail singlePhase = null)
        {
            var marketEntities = FormulaHelper.GetEntities <Market.Logic.Domain.MarketEntities>();

            S_I_ProjectInfo projectInfo = entity.Push();

            projectInfo.ModifyDate   = projectInfo.CreateDate;
            projectInfo.ModifyUser   = projectInfo.CreateUser;
            projectInfo.ModifyUserID = projectInfo.CreateUserID;

            //重新修改phaseValue、phaseName、Name、Code等信息
            if (singlePhase != null)
            {
                projectInfo.PhaseValue        = singlePhase.Phase;
                projectInfo.WBSRoot.PhaseCode = singlePhase.Phase;
                var phaseList = BaseConfigFO.GetWBSAttrList(WBSNodeType.Phase);
                var phaseItem = phaseList.FirstOrDefault(d => projectInfo.PhaseValue == d.Code);
                projectInfo.PhaseName      = phaseItem.Name;
                projectInfo.Name           = singlePhase.Name;
                projectInfo.Code           = singlePhase.Code;
                projectInfo.ChargeDeptID   = singlePhase.ChargeDept ?? entity.ChargeDept;
                projectInfo.ChargeDeptName = singlePhase.ChargeDeptName ?? entity.ChargeDeptName;
                projectInfo.ChargeUserID   = singlePhase.ChargeUser ?? entity.ChargeUser;
                projectInfo.ChargeUserName = singlePhase.ChargeUserName ?? entity.ChargeUserName;
                projectInfo.OtherDeptID    = singlePhase.OtherDept ?? entity.OtherDept;
                projectInfo.OtherDeptName  = singlePhase.OtherDeptName ?? entity.OtherDeptName;
                projectInfo.PlanStartDate  = singlePhase.PlanStartDate ?? entity.PlanStartDate;
                projectInfo.PlanFinishDate = singlePhase.PlanFinishDate ?? entity.PlanFinishDate;
            }

            projectInfo.ModifyDate   = projectInfo.CreateDate;
            projectInfo.ModifyUser   = projectInfo.CreateUser;
            projectInfo.ModifyUserID = projectInfo.CreateUserID;
            #region  步创建经营库的项目信息
            S_I_Project project    = null;
            var         marketClue = marketEntities.S_P_MarketClue.Find(entity.RelateID);
            project = marketEntities.S_I_Project.FirstOrDefault(d => d.TasKNoticeID == entity.ID);
            if (project != null)
            {
                throw new Formula.Exceptions.BusinessException("任务单已经下达过项目,无法重复下达");
            }
            project                    = new S_I_Project();
            project.ID                 = projectInfo.ID;
            project.TasKNoticeID       = entity.ID;
            project.TasKNoticeTmplCode = entity.TmplCode;
            project.Name               = projectInfo.Name;
            project.Code               = projectInfo.Code;
            project.Phase              = projectInfo.PhaseValue;
            project.ProjectClass       = projectInfo.ProjectClass;
            project.Customer           = projectInfo.CustomerID;
            project.CustomerName       = projectInfo.CustomerName;
            project.CreateDate         = DateTime.Now;
            project.EngineeringInfo    = entity.EngineeringID;
            project.ChargerDept        = projectInfo.ChargeDeptID;
            project.ChargerDeptName    = projectInfo.ChargeDeptName;
            project.ChargerUser        = projectInfo.ChargeUserID;
            project.ChargerUserName    = projectInfo.ChargeUserName;
            project.Country            = projectInfo.Country;
            project.Province           = projectInfo.Province;
            project.Area               = projectInfo.Area;
            project.City               = projectInfo.City;
            project.State              = projectInfo.State;
            project.ProjectScale       = entity.ProjectLevel;
            if (marketClue != null)
            {
                project.MakertClueID = marketClue.ID;
                marketClue.State     = Market.Logic.ClueState.Succeed.ToString();
            }
            marketEntities.S_I_Project.Add(project);
            entity.MarketProjectID = project.ID;
            #endregion

            #region  步绑定合同

            if (!String.IsNullOrEmpty(entity.ContractInfo))
            {
                var contract = marketEntities.Set <S_C_ManageContract>().Include("S_C_ManageContract_ProjectRelation")
                               .Include("S_C_ManageContract_ReceiptObj").FirstOrDefault(d => d.ID == entity.ContractInfo);
                if (contract != null)
                {
                    var relation = contract.S_C_ManageContract_ProjectRelation.FirstOrDefault(a => a.ProjectID == project.ID);
                    if (relation == null)
                    {
                        relation                      = new S_C_ManageContract_ProjectRelation();
                        relation.ID                   = FormulaHelper.CreateGuid();
                        relation.ProjectID            = project.ID;
                        relation.S_C_ManageContractID = contract.ID;
                        relation.ProjectCode          = project.Code;
                        relation.ProjectName          = project.Name;
                        if (contract.S_C_ManageContract_ProjectRelation.Count == 0)
                        {
                            if (entity.ContractValue.HasValue && contract.ContractRMBAmount > 0)
                            {
                                if (contract.ContractRMBAmount > 0)
                                {
                                    relation.Scale = entity.ContractValue.Value / contract.ContractRMBAmount * 100;
                                }
                                else
                                {
                                    relation.Scale = 0;
                                }
                                relation.ProjectValue = entity.ContractValue;
                                relation.TaxRate      = contract.TaxRate;
                                relation.TaxValue     = relation.ProjectValue / (1 + contract.TaxRate) * contract.TaxRate;
                                relation.ClearValue   = relation.ProjectValue - relation.TaxValue;
                            }
                            else
                            {
                                relation.Scale        = 100;
                                relation.ProjectValue = contract.ContractRMBAmount;
                                var taxRate    = contract.TaxRate.HasValue ? contract.TaxRate.Value : 0;
                                var taxValue   = contract.ContractRMBAmount / (1 + taxRate) * taxRate;
                                var clearVlaue = contract.ContractRMBAmount - taxValue;
                                relation.TaxRate    = taxRate;
                                relation.TaxValue   = taxValue;
                                relation.ClearValue = contract.ContractRMBAmount - taxValue;
                            }
                            foreach (var item in contract.S_C_ManageContract_ReceiptObj.ToList())
                            {
                                item.ProjectInfo     = project.ID;
                                item.ProjectInfoName = project.Name;
                                relation.TaxRate     = contract.TaxRate;
                            }
                        }
                        else
                        {
                            if (entity.ContractValue.HasValue && contract.ContractRMBAmount > 0)
                            {
                                if (contract.ContractRMBAmount > 0)
                                {
                                    relation.Scale = entity.ContractValue.Value / contract.ContractRMBAmount * 100;
                                }
                                else
                                {
                                    relation.Scale = 0;
                                }
                                relation.ProjectValue = entity.ContractValue;
                                relation.TaxRate      = contract.TaxRate;
                                relation.TaxValue     = relation.ProjectValue / (1 + contract.TaxRate) * contract.TaxRate;
                                relation.ClearValue   = relation.ProjectValue - relation.TaxValue;
                            }
                            else
                            {
                                relation.Scale        = 0;
                                relation.ProjectValue = 0;
                                relation.TaxRate      = contract.TaxRate;
                            }
                        }
                        contract.S_C_ManageContract_ProjectRelation.Add(relation);
                        if (String.IsNullOrEmpty(contract.EngineeringInfo) && engineering != null)
                        {
                            contract.EngineeringInfo     = engineering.ID;
                            contract.EngineeringInfoName = engineering.Name;
                        }
                    }
                }
            }
            else
            {
                var contractList = marketEntities.Set <S_C_ManageContract>().Include("S_C_ManageContract_ProjectRelation")
                                   .Include("S_C_ManageContract_ReceiptObj").Where(d => d.EngineeringInfo == entity.EngineeringID).ToList();
                foreach (var contract in contractList)
                {
                    var relation = contract.S_C_ManageContract_ProjectRelation.FirstOrDefault(a => a.ProjectID == project.ID);
                    if (relation == null)
                    {
                        relation                      = new S_C_ManageContract_ProjectRelation();
                        relation.ID                   = FormulaHelper.CreateGuid();
                        relation.ProjectID            = project.ID;
                        relation.S_C_ManageContractID = contract.ID;
                        relation.ProjectCode          = project.Code;
                        relation.ProjectName          = project.Name;
                        if (contract.S_C_ManageContract_ProjectRelation.Count == 0)
                        {
                            relation.Scale        = 100;
                            relation.ProjectValue = contract.ContractRMBAmount;
                            foreach (var item in contract.S_C_ManageContract_ReceiptObj.ToList())
                            {
                                item.ProjectInfo     = project.ID;
                                item.ProjectInfoName = project.Name;
                            }
                        }
                        else
                        {
                            relation.Scale        = 0;
                            relation.ProjectValue = 0;
                        }
                        contract.S_C_ManageContract_ProjectRelation.Add(relation);
                    }
                }
            }


            #endregion

            projectInfo.MarketProjectInfoID = project.ID;

            #region 默认创建EPS结构
            var group = this.BusinessEntities.Set <S_I_ProjectGroup>().FirstOrDefault(d => d.RelateID == entity.EngineeringID && d.Type == "Engineering");
            if (group == null)
            {
                group                      = new S_I_ProjectGroup();
                group.ID                   = Formula.FormulaHelper.CreateGuid();
                group.Name                 = engineering.Name;
                group.Code                 = engineering.Code;
                group.City                 = engineering.City;
                group.Country              = engineering.Country;
                group.Province             = engineering.Province;
                group.Area                 = engineering.Area;
                group.ProjectClass         = engineering.Class;
                group.Investment           = engineering.Investment;
                group.Proportion           = engineering.Proportion;
                group.PhaseContent         = engineering.PhaseContent;
                group.Address              = engineering.Address;
                group.DeptID               = engineering.MainDept;
                group.DeptName             = engineering.MainDeptName;
                group.RelateID             = engineering.ID;
                group.EngineeringSpaceCode = ProjectMode.Standard.ToString();
                group.CreateDate           = DateTime.Now;
                var fo = Formula.FormulaHelper.CreateFO <EPSFO>();
                fo.BuildEngineering(group);
            }
            group.BindingProject(projectInfo);
            entity.GroupID     = group.ID;
            group.PhaseContent = engineering.PhaseContent;
            group.PhaseValue   = engineering.PhaseValue;
            #endregion

            projectInfo.InitDeisgnInputTemplate(true);

            //把设总放进RBS中
            if (projectInfo != null && !string.IsNullOrEmpty(entity.DesignManager))
            {
                projectInfo.WBSRoot.SetUsers(ProjectRole.DesignManager.ToString(), entity.DesignManager.Split(','), true, true, true, true);
            }

            var customer = marketEntities.Set <S_F_Customer>().FirstOrDefault(a => a.ID == entity.Customer);
            if (customer != null)
            {
                if (string.IsNullOrEmpty(entity.Country))
                {
                    project.Country     = customer.Country;
                    projectInfo.Country = customer.Country;
                }
                if (string.IsNullOrEmpty(entity.Province))
                {
                    project.Province     = customer.Province;
                    projectInfo.Province = customer.Province;
                }
                if (string.IsNullOrEmpty(entity.City))
                {
                    project.City     = customer.City;
                    projectInfo.City = customer.City;
                }
                if (string.IsNullOrEmpty(entity.Area))
                {
                    project.Area     = customer.Area;
                    projectInfo.Area = customer.Area;
                }
            }

            return(projectInfo);
        }
        private void SyncContract(S_C_ManageContract_Supplementary entity, bool isDelete = false)
        {
            var contract = this.GetEntityByID <S_C_ManageContract>(entity.ContractInfoID);

            if (contract == null)
            {
                throw new Formula.Exceptions.BusinessException("未能找到指定的收入合同");
            }
            var sumSettle = this.BusinessEntities.Set <S_C_ManageContract_Supplementary>()
                            .Where(a => a.ContractInfoID == contract.ID).Select(a => a.SupplementaryRMBAmount).Sum();

            contract.ContractRMBAmount = (contract.ThisContractRMBAmount ?? 0m) + (sumSettle ?? 0m);
            if (isDelete)
            {
                contract.ContractRMBAmount = contract.ContractRMBAmount - (entity.SupplementaryRMBAmount ?? 0m);
            }

            #region 合同收款项逻辑
            if (!isDelete && entity.SupplementaryRMBAmount > 0)
            {
                S_C_ManageContract_ProjectRelation projectRelation = null;
                if (contract.S_C_ManageContract_ProjectRelation.Count == 1)
                {
                    projectRelation = contract.S_C_ManageContract_ProjectRelation.FirstOrDefault();
                }

                #region 生成收款项,并同步到主合同
                var supplementaryReceiptObjList = entity.S_C_ManageContract_Supplementary_ReceiptObj.ToList();
                if (supplementaryReceiptObjList.Count == 0)
                {
                    //没有收款项得时候默认增加一个收款项
                    var supplementaryReceiptObj = new S_C_ManageContract_Supplementary_ReceiptObj();
                    supplementaryReceiptObj.ID = FormulaHelper.CreateGuid();
                    supplementaryReceiptObj.S_C_ManageContract_SupplementaryID = entity.ID;
                    supplementaryReceiptObj.SortIndex      = 1;
                    supplementaryReceiptObj.ReceiptPercent = 100;
                    supplementaryReceiptObj.Name           = entity.Name;
                    supplementaryReceiptObj.ReceiptValue   = entity.SupplementaryValue;
                    if (projectRelation != null)
                    {
                        supplementaryReceiptObj.ProjectInfo     = projectRelation.ProjectID;
                        supplementaryReceiptObj.ProjectInfoName = projectRelation.ProjectName;
                    }

                    supplementaryReceiptObjList.Add(supplementaryReceiptObj);
                    entity.S_C_ManageContract_Supplementary_ReceiptObj.Add(supplementaryReceiptObj);
                }

                //同步到收入合同的收款项
                foreach (var supplementaryReceiptObj in supplementaryReceiptObjList)
                {
                    var receiptObj = this.GetEntityByID <S_C_ManageContract_ReceiptObj>(supplementaryReceiptObj.ID);
                    if (receiptObj == null)
                    {
                        receiptObj    = new S_C_ManageContract_ReceiptObj();
                        receiptObj.ID = supplementaryReceiptObj.ID;
                        receiptObj.S_C_ManageContractID = contract.ID;
                        receiptObj.SupplementaryID      = entity.ID;
                        receiptObj.SortIndex            = 1;
                        contract.S_C_ManageContract_ReceiptObj.Add(receiptObj);
                        receiptObj.S_C_ManageContract = contract;
                        this.BusinessEntities.Set <S_C_ManageContract_ReceiptObj>().Add(receiptObj);
                    }
                    receiptObj.Name            = supplementaryReceiptObj.Name;
                    receiptObj.ReceiptPercent  = 0;
                    receiptObj.ReceiptValue    = supplementaryReceiptObj.ReceiptValue;
                    receiptObj.ProjectInfo     = supplementaryReceiptObj.ProjectInfo;
                    receiptObj.ProjectInfoName = supplementaryReceiptObj.ProjectInfoName;
                    receiptObj.PlanFinishDate  = supplementaryReceiptObj.PlanFinishDate;
                    receiptObj.SummaryReceiptValue();
                    receiptObj.ResetPlan();
                }
                #endregion
            }
            #endregion

            #region 合同自动按权重计算关联项目明细金额
            var relationList       = contract.S_C_ManageContract_ProjectRelation.OrderBy(a => a.SortIndex).ToList();
            var relationTotalScale = relationList.Sum(a => a.Scale);
            if (relationTotalScale.HasValue)
            {
                var totalValue = Math.Round(Convert.ToDecimal(relationTotalScale * contract.ContractRMBAmount / 100), 2);
                var sumValue   = 0m;
                foreach (var item in relationList)
                {
                    if (item == relationList.LastOrDefault())
                    {
                        item.ProjectValue = totalValue - sumValue;
                    }
                    else
                    {
                        item.ProjectValue = Math.Round(Convert.ToDecimal(item.Scale * contract.ContractRMBAmount / 100), 2);
                    }
                    if (contract.TaxRate.HasValue)
                    {
                        item.TaxValue   = item.ProjectValue / (1 + contract.TaxRate.Value) * contract.TaxRate.Value;
                        item.ClearValue = item.ProjectValue - item.TaxValue;
                    }
                    sumValue += (item.ProjectValue ?? 0m);
                }
            }
            #endregion

            #region 合同自动按权重计算合同拆分子表金额
            var splitList       = contract.S_C_ManageContract_ContractSplit.OrderBy(a => a.SortIndex).ToList();
            var splitTotalScale = splitList.Sum(a => a.Scale);
            if (splitTotalScale.HasValue)
            {
                var totalValue = Math.Round(Convert.ToDecimal(splitTotalScale * contract.ContractRMBAmount / 100), 2);
                var sumValue   = 0m;
                foreach (var item in splitList)
                {
                    if (item == splitList.LastOrDefault())
                    {
                        item.SplitValue = totalValue - sumValue;
                    }
                    else
                    {
                        item.SplitValue = Math.Round(Convert.ToDecimal(item.Scale * contract.ContractRMBAmount / 100), 2);
                    }
                    sumValue += (item.SplitValue ?? 0m);
                }
            }
            #endregion

            #region 重新计算合同部门分解补充协议金额
            var deptList = contract.S_C_ManageContract_DeptRelation.OrderBy(a => a.SortIndex).ToList();
            foreach (var item in deptList)
            {
                var sValue = 0m;
                foreach (var supplementary in contract.S_C_ManageContract_Supplementary.ToList())
                {
                    sValue += Convert.ToDecimal(supplementary.S_C_ManageContract_Supplementary_DeptRelation.Where(a => a.Dept == item.Dept).Sum(a => a.DeptValue));
                }
                item.SumSupplementaryValue = sValue;
            }
            #endregion
        }