Exemple #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();
        }