Esempio n. 1
0
        public void AddOrEdit(ProjectProcurementDto dto)
        {
            ProjectProcurement entity = null;

            if (dto.ID == 0)
            {
                entity = new ProjectProcurement();
                dto.ProjectedAs(entity);
                entity.Created(this);
                repository.AddProcurement(entity);
            }
            else
            {
                entity = repository.GetProcurement(dto.ID);
                if (entity == null)
                {
                    throw new BingoX.LogicException("編號不存在");
                }
                dto.ProjectedAs(entity);
                repository.UpdateProcurement(entity);
            }
            var project = repository.GetProjectMaster(dto.ProjectId);

            if (project == null)
            {
                throw new BingoX.LogicException("项目不存在");
            }
            var ProcurementAmount         = dto.Items.Sum(n => n.ProcurementAmount);         //採購金額
            var BidAmount                 = dto.Items.Sum(n => n.BidAmount);                 //中標總計
            var ProcurementEndAmount      = dto.Items.Sum(n => n.ProcurementEndAmount);      //已採購金額
            var ProcurementEndTotalAmount = dto.Items.Sum(n => n.ProcurementEndTotalAmount); //總採購金額
            var OtherTotal                = dto.Items.Sum(n => n.Total);                     //其他暫估 總計

            entity.OtherAmount           = OtherTotal;
            entity.ItemAmount            = BidAmount;
            entity.ProfitsRate           = 1 - (ProcurementAmount + OtherTotal) / BidAmount;
            entity.ThisProcurementAmount = ProcurementAmount;
            switch (entity.ProcurementType)
            {
            case ProcurementType.Materials:
            {
                if (string.IsNullOrEmpty(entity.MaterialUsage))
                {
                    throw new BingoX.LogicException("材料用途 为空");
                }
                // if (entity.AllProcurementAmount <= 0) throw new BingoX.LogicException("總採購金額 不爲正數");

                entity.BeenProcurementAmount = ProcurementEndAmount;
                entity.AllProcurementAmount  = ProcurementAmount + ProcurementEndAmount;
                break;
            }

            case ProcurementType.Outsourcing:
            {
                //   if (entity.TotalPurchaseAmount <= 0) throw new BingoX.LogicException("總採購金額 不爲正數");
                break;
            }

            case ProcurementType.Other:
            {
                // if (entity.TotalPurchaseAmount <= 0) throw new BingoX.LogicException("總採購金額 不爲正數");
                break;
            }

            case ProcurementType.Paid:
            {
                //  if (entity.TotalPurchaseAmount <= 0) throw new BingoX.LogicException("總採購金額 不爲正數");
                break;
            }

            default:
                break;
            }
            if (dto.AddFiles.HasAny())
            {
                var files = repository.GetFiles(dto.AddFiles);

                foreach (var item in files)
                {
                    item.FileType  = ProjectFileType.Procurement;
                    item.ProjectId = dto.ProjectId;
                    item.ForeignID = entity.ID;
                    var path = Path.Combine("project", project.Code, item.FileType.GetHashCode().ToString(), Bounded.Generator.New() + item.FileName);

                    fileStorageServie.Move(item.StoragePath, path);
                    repository.Update(item);
                }
            }

            entity.State = ProcurementState.Save;
            var items = dto.Items.Where(n => n.Id == 0).Select(n =>
            {
                var en             = n.ProjectedAs <ProcurementBQItem>();
                en.ProcurementId   = entity.ID;
                en.ProjectId       = entity.ProjectId;
                en.ProcurementType = entity.ProcurementType;
                en.Created(this);
                return(en);
            }).ToList();


            repository.AddBQItems(items);

            repository.UnitOfWork.Commit();
        }
        public void Add(ProjectMasterDto dto)
        {
            Check(dto);
            if (dto.CreateFileId <= 0)
            {
                throw new LogicException("立项目文件为空");
            }
            if (dto.CompanyId <= 0)
            {
                throw new LogicException("公司爲空");
            }
            Supplier supplier = repository.GetSupplier(dto.CompanyId);

            if (supplier == null)
            {
                throw new LogicException("選擇的公司無效");
            }
            if (!supplier.IsCompany || string.IsNullOrEmpty(supplier.Code))
            {
                throw new LogicException("選擇的公司無效");
            }

            ProjectMaster entity = dto.ProjectedAs <ProjectMaster>();

            if (entity.BeginDate.HasValue && entity.EndDate.HasValue && entity.EndDate < entity.BeginDate)
            {
                throw new LogicException("實際開始日期不能大於實際結束日期");
            }


            if (entity.EstimatedBeginDate.HasValue && entity.EstimatedEndDate.HasValue && entity.EstimatedEndDate < entity.EstimatedBeginDate)
            {
                throw new LogicException("評估開始日期不能大於評估結束日期");
            }

            ProjectAboutFile savefile = repository.GetFile(dto.CreateFileId);

            if (savefile == null)
            {
                throw new LogicException("立项目文件不存在");
            }
            entity.Code = serialNumberProvider.Dequeue(supplier);
            if (string.IsNullOrEmpty(entity.Code))
            {
                throw new LogicException("生成項目編號失敗");
            }



            var path = Path.Combine("project", entity.Code, savefile.FileType.GetHashCode().ToString(), Bounded.Generator.New() + savefile.FileName);

            fileStorageServie.Move(savefile.StoragePath, path);
            savefile.StoragePath = path;
            savefile.ProjectId   = entity.ID;
            savefile.FileType    = ProjectFileType.Create;
            entity.State         = CommonState.Enabled;
            entity.Created(this);



            var standingbook = new ProjectStandingbook();

            standingbook.ProjectId = entity.ID;
            standingbook.Created(this);

            var targetCost = new ProjectTargetCost();

            targetCost.ProjectId = entity.ID;
            targetCost.Created(this);


            var calculation = new ProjectCalculation();

            calculation.ProjectId = entity.ID;
            calculation.Created(this);

            repository.AddMaster(entity);
            repository.AddCalculation(calculation);
            repository.AddStandingbook(standingbook);
            repository.AddTargetCost(targetCost);
            repository.Update(savefile);

            repository.UnitOfWork.Commit();
        }