Exemple #1
0
            public async Task <RequestDetailDto> Handle(Command message)
            {
                var requestDto = message.RequestDetailDto;
                var request    = _mapper.Map <RequestDetailDto, Domain.Entities.Request.Request>(requestDto);

                _numberGenerator.GenerateIncomingNum(request);
                _numberGenerator.GenerateBarcode(request);
                InitializeFieldsByDefault(request);

                _context.Requests.Add(request);

                try
                {
                    await _context.SaveChangesAsync();

                    await _workflowApplier.ApplyInitialAsync(request, message.UserId);

                    await _context.SaveChangesAsync();

                    var requestWithIncludes = await _context.Requests
                                              .Include(r => r.Addressee)
                                              .Include(r => r.Workflows).ThenInclude(r => r.FromStage)
                                              .Include(r => r.Workflows).ThenInclude(r => r.CurrentStage)
                                              .Include(r => r.Workflows).ThenInclude(r => r.FromUser)
                                              .Include(r => r.Workflows).ThenInclude(r => r.CurrentUser)
                                              .Include(r => r.Workflows).ThenInclude(r => r.Route)
                                              .SingleAsync(r => r.Id == request.Id);

                    return(_mapper.Map <Domain.Entities.Request.Request, RequestDetailDto>(requestWithIncludes));
                }
                catch (Exception e)
                {
                    throw new DatabaseException(e.InnerException?.Message ?? e.Message);
                }
            }
Exemple #2
0
            public async Task <ContractDetailDto> Handle(Command message)
            {
                var contractDto = message.ContractDetailDto;
                var contract    = _mapper.Map <ContractDetailDto, Domain.Entities.Contract.Contract>(contractDto);

                contract.RegDate     = DateTimeOffset.Now;
                contract.ContractNum = _numberGenerator.Generate("ContractNum").ToString();
                _numberGenerator.GenerateBarcode(contract);

                _context.Contracts.Add(contract);

                try
                {
                    await _context.SaveChangesAsync();

                    await _workflowApplier.ApplyInitialAsync(contract, message.UserId);

                    await _context.SaveChangesAsync();

                    await _requestRelationUpdater.UpdateAsync(contract.Id, contractDto.RequestRelations);

                    var contractWithIncludes = await _context.Contracts
                                               .Include(r => r.CurrentWorkflow).ThenInclude(cw => cw.CurrentStage)
                                               .Include(r => r.CurrentWorkflow).ThenInclude(cw => cw.FromStage)
                                               .Include(r => r.ProtectionDocType)
                                               .Include(r => r.ContractType)
                                               .Include(r => r.ContractCustomers).ThenInclude(c => c.CustomerRole)
                                               .Include(r => r.ContractCustomers).ThenInclude(c => c.Customer).ThenInclude(c => c.Type)
                                               .Include(r => r.ContractCustomers).ThenInclude(c => c.Customer).ThenInclude(c => c.Country)
                                               .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.Tariff)
                                               .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.PaymentUses)
                                               .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.Status)
                                               .Include(r => r.Workflows).ThenInclude(r => r.FromStage)
                                               .Include(r => r.Workflows).ThenInclude(r => r.CurrentStage)
                                               .Include(r => r.Workflows).ThenInclude(r => r.FromUser)
                                               .Include(r => r.Workflows).ThenInclude(r => r.CurrentUser)
                                               .Include(r => r.Workflows).ThenInclude(r => r.Route)
                                               .Include(r => r.RequestsForProtectionDoc).ThenInclude(r => r.Request).ThenInclude(r => r.ProtectionDocType)
                                               .Include(r => r.RequestsForProtectionDoc).ThenInclude(r => r.ContractRequestICGSRequests).ThenInclude(r => r.ICGSRequest).ThenInclude(r => r.Icgs)
                                               .Include(r => r.ProtectionDocs)
                                               .SingleAsync(c => c.Id == contract.Id);

                    return(_mapper.Map <Domain.Entities.Contract.Contract, ContractDetailDto>(contractWithIncludes));
                }
                catch (Exception e)
                {
                    throw new DatabaseException(e.InnerException?.Message ?? e.Message);
                }
            }
Exemple #3
0
 private void ResolveSpecificLogicTask(WorkflowTaskQueue task)
 {
     if (task.ConditionStage.Code == "DK02.4.0")
     {
         var contract = task.Contract;
         if (contract == null)
         {
             return;
         }
         ContractDocument newContractDocument = GenerateDocument(contract);
         if (contract.CurrentWorkflow.CurrentUserId != null)
         {
             _documentWorkflowApplier.ApplyInitialAsync(newContractDocument.Document,
                                                        contract.CurrentWorkflow.CurrentUserId.Value);
         }
         contract.Documents.Add(newContractDocument);
         contract.IsRead = false;
         _context.SaveChangesAsync().Wait();
     }
 }
Exemple #4
0
            public async Task <MaterialInternalDetailDto> Handle(Command message)
            {
                var document = _mapper.Map <Document>(message.MaterialDetailDto);

                document.DocumentType = DocumentType.Internal;
                using (var transaction = await _context.Database.BeginTransactionAsync())
                {
                    _numberGenerator.GenerateBarcode(document);

                    await _context.AddAsync(document);

                    await _context.SaveChangesAsync();

                    foreach (var materialOwnerDto in message.MaterialDetailDto.Owners)
                    {
                        switch (materialOwnerDto.OwnerType)
                        {
                        case Owner.Type.Request:
                        {
                            var request = await _context.Requests.SingleAsync(r => r.Id == materialOwnerDto.OwnerId);

                            request.Documents.Add(new RequestDocument {
                                    Document = document
                                });
                            break;
                        }

                        case Owner.Type.Contract:
                            var contract = await _context.Contracts.SingleAsync(c => c.Id == materialOwnerDto.OwnerId);

                            contract.Documents.Add(new ContractDocument {
                                Document = document
                            });
                            break;

                        case Owner.Type.ProtectionDoc:
                            var protectionDoc =
                                await _context.ProtectionDocs.SingleAsync(pd => pd.Id == materialOwnerDto.OwnerId);

                            protectionDoc.Documents.Add(new ProtectionDocDocument {
                                Document = document
                            });
                            break;
                        }
                    }

                    await _documentWorkflowApplier.ApplyInitialAsync(document, message.UserId);

                    var documentGenerator = _templateGeneratorFactory.Create(message.MaterialDetailDto.UserInput.Code);
                    var generatedFile     = documentGenerator?.Process(new Dictionary <string, object>
                    {
                        { "UserId", message.UserId },
                        { "RequestId", message.MaterialDetailDto.UserInput.OwnerId },
                        { "DocumentId", document.Id },
                        { "UserInputFields", message.MaterialDetailDto.UserInput.Fields },
                        { "SelectedRequestIds", message.MaterialDetailDto.UserInput.SelectedRequestIds },
                        { "PageCount", message.MaterialDetailDto.PageCount }
                    });

                    message.MaterialDetailDto.UserInput.DocumentId = document.Id;
                    message.MaterialDetailDto.UserInput.PageCount  = generatedFile?.PageCount;

                    var input = new DocumentUserInput
                    {
                        DocumentId = document.Id,
                        UserInput  = JsonConvert.SerializeObject(message.MaterialDetailDto.UserInput)
                    };
                    _context.DocumentUserInputs.Add(input);
                    await _context.SaveChangesAsync();

                    await _requestDocumentApplier.ApplyAsync(document.Id);

                    await _contractDocumentApplier.ApplyAsync(document.Id);

                    var documentWithIncludes = await _context.Documents
                                               .Include(d => d.Addressee)
                                               .Include(d => d.Workflows).ThenInclude(w => w.FromStage)
                                               .Include(d => d.Workflows).ThenInclude(w => w.CurrentStage)
                                               .Include(d => d.Workflows).ThenInclude(w => w.FromUser)
                                               .Include(d => d.Workflows).ThenInclude(w => w.CurrentUser)
                                               .Include(d => d.Workflows).ThenInclude(w => w.Route)
                                               .Include(d => d.MainAttachment)
                                               .Include(d => d.Requests)
                                               .Include(d => d.Contracts)
                                               .SingleOrDefaultAsync(r => r.Id == document.Id);

                    transaction.Commit();
                    var result                 = _mapper.Map <MaterialInternalDetailDto>(documentWithIncludes);
                    var requestsOwnerDtos      = _mapper.Map <RequestDocument[], MaterialOwnerDto[]>(documentWithIncludes.Requests.ToArray());
                    var contractOwnerDtos      = _mapper.Map <ContractDocument[], MaterialOwnerDto[]>(documentWithIncludes.Contracts.ToArray());
                    var protectionDocOwnerDtos = _mapper.Map <ProtectionDocDocument[], MaterialOwnerDto[]>(documentWithIncludes.ProtectionDocs.ToArray());
                    result.Owners = requestsOwnerDtos.Concat(contractOwnerDtos.Concat(protectionDocOwnerDtos)).ToArray();
                    return(result);
                }
            }
Exemple #5
0
            public async Task <ProtectionDocDetailsDto> Handle(Command message)
            {
                var protectionDocDetailsDto = message.ProtectionDocDetailsDto;
                var protectionDoc           = _mapper.Map <ProtectionDocDetailsDto, Domain.Entities.ProtectionDoc.ProtectionDoc>(protectionDocDetailsDto);

                using (var transaction = await _context.Database.BeginTransactionAsync())
                {
                    protectionDoc.StatusId =
                        _context.DicProtectionDocStatuses.SingleOrDefault(pds => pds.Code.Equals("D"))?.Id;

                    _numberGenerator.GenerateBarcode(protectionDoc);

                    _context.ProtectionDocs.Add(protectionDoc);

                    try
                    {
                        await _context.SaveChangesAsync();

                        var requestId = 0;

                        switch (protectionDoc.TypeId)
                        {
                        case 1:
                            requestId = 834;
                            break;

                        case 4:
                            requestId = 740;
                            break;

                        default:
                            requestId = 740;
                            break;
                        }

                        var request = _context.Requests.Include(r => r.ColorTzs)
                                      .Include(r => r.EarlyRegs)
                                      .Include(r => r.ICGSRequests)
                                      .Include(r => r.ICISRequests)
                                      .Include(r => r.IPCRequests)
                                      .Include(r => r.PaymentInvoices).ThenInclude(pi => pi.PaymentUses)
                                      .Include(r => r.RequestConventionInfos)
                                      .Include(r => r.RequestCustomers).ThenInclude(rc => rc.CustomerRole)
                                      .Single(r => r.Id == requestId);
                        request.ProtectionDocId = protectionDoc.Id;
                        protectionDoc.RegNumber = request.RequestNum;
                        protectionDoc.RegDate   = request.DateCreate;
                        protectionDoc.ValidDate = request.DateCreate.AddYears(10);

                        var icfems = request.Icfems.Select(i => new DicIcfemProtectionDocRelation
                        {
                            DicIcfemId      = i.DicIcfemId,
                            ProtectionDocId = protectionDoc.Id
                        });
                        await _context.DicIcfemProtectionDocRelations.AddRangeAsync(icfems);

                        var colorTzs = request.ColorTzs.Select(c => new DicColorTZProtectionDocRelation
                        {
                            ColorTzId       = c.ColorTzId,
                            ProtectionDocId = protectionDoc.Id
                        });
                        await _context.DicColorTzProtectionDocRelations.AddRangeAsync(colorTzs);

                        var icgs = _mapper.Map <ICGSRequest[], ICGSProtectionDoc[]>(request.ICGSRequests.ToArray());
                        foreach (var icgsProtectionDoc in icgs)
                        {
                            protectionDoc.IcgsProtectionDocs.Add(icgsProtectionDoc);
                        }
                        var ipcs = _mapper.Map <IPCRequest[], IPCProtectionDoc[]>(request.IPCRequests.ToArray());
                        foreach (var ipcProtectionDoc in ipcs)
                        {
                            protectionDoc.IpcProtectionDocs.Add(ipcProtectionDoc);
                        }
                        var icis = _mapper.Map <ICISRequest[], ICISProtectionDoc[]>(request.ICISRequests.ToArray());
                        foreach (var icisProtectionDoc in icis)
                        {
                            protectionDoc.IcisProtectionDocs.Add(icisProtectionDoc);
                        }
                        var earlyRegs = _mapper.Map <RequestEarlyReg[], ProtectionDocEarlyReg[]>(request.EarlyRegs.ToArray());
                        foreach (var protectionDocEarlyReg in earlyRegs)
                        {
                            protectionDoc.EarlyRegs.Add(protectionDocEarlyReg);
                        }
                        var conventionInfos = _mapper.Map <RequestConventionInfo[], ProtectionDocConventionInfo[]>(request.RequestConventionInfos.ToArray());
                        foreach (var protectionDocConventionInfo in conventionInfos)
                        {
                            protectionDoc.ProtectionDocConventionInfos.Add(protectionDocConventionInfo);
                        }
                        var info = _mapper.Map <RequestInfo, ProtectionDocInfo>(request.RequestInfo);
                        protectionDoc.ProtectionDocInfo = info;
                        var subjects = _mapper.Map <RequestCustomer[], ProtectionDocCustomer[]>(request.RequestCustomers.ToArray());
                        foreach (var protectionDocCustomer in subjects)
                        {
                            protectionDoc.ProtectionDocCustomers.Add(protectionDocCustomer);
                        }
                        var invoices = request.PaymentInvoices.Select(pi => new PaymentInvoice
                        {
                            TariffId        = pi.TariffId,
                            Coefficient     = pi.Coefficient,
                            Nds             = pi.Nds,
                            ProtectionDocId = protectionDoc.Id,
                            PenaltyPercent  = pi.PenaltyPercent,
                            TariffCount     = pi.TariffCount,
                            IsComplete      = pi.IsComplete,
                            OverdueDate     = pi.OverdueDate,
                            DateFact        = pi.DateFact,
                            DateComplete    = pi.DateComplete,
                            ApplicantTypeId = pi.ApplicantTypeId,
                            CreateUserId    = pi.CreateUserId,
                            WriteOffUserId  = pi.WriteOffUserId,
                            WhoBoundUserId  = pi.WhoBoundUserId,
                            PaymentUses     = pi.PaymentUses,
                            StatusId        = pi.StatusId
                        });
                        await _context.PaymentInvoices.AddRangeAsync(invoices);

                        await _context.SaveChangesAsync();

                        await _workflowApplier.ApplyInitialAsync(protectionDoc, message.UserId);

                        await _context.SaveChangesAsync();

                        var protectionDocWithIncludesWithIncludes = await _context.ProtectionDocs
                                                                    .Include(r => r.Workflows).ThenInclude(r => r.FromStage)
                                                                    .Include(r => r.Workflows).ThenInclude(r => r.CurrentStage)
                                                                    .Include(r => r.Workflows).ThenInclude(r => r.FromUser)
                                                                    .Include(r => r.Workflows).ThenInclude(r => r.CurrentUser)
                                                                    .Include(r => r.Workflows).ThenInclude(r => r.Route)
                                                                    .SingleAsync(r => r.Id == protectionDoc.Id);

                        transaction.Commit();
                        return(_mapper.Map <Domain.Entities.ProtectionDoc.ProtectionDoc, ProtectionDocDetailsDto>(protectionDocWithIncludesWithIncludes));
                    }
                    catch (Exception e)
                    {
                        throw new DatabaseException(e.InnerException?.Message ?? e.Message);
                    }
                }
            }
Exemple #6
0
            public async Task <int> Handle(Command message)
            {
                var document = _mapper.Map <Document>(message.MaterialDetailDto);

                document.DocumentType = DocumentType.Incoming;
                using (var transaction = await _context.Database.BeginTransactionAsync())
                {
                    _numberGenerator.GenerateBarcode(document);
                    _numberGenerator.GenerateIncomingNum(document);

                    await _context.AddAsync(document);

                    await _context.SaveChangesAsync();

                    foreach (var materialOwnerDto in message.MaterialDetailDto.Owners)
                    {
                        switch (materialOwnerDto.OwnerType)
                        {
                        case Owner.Type.Request:
                        {
                            var request = await _context.Requests.SingleAsync(r => r.Id == materialOwnerDto.OwnerId);

                            request.Documents.Add(new RequestDocument {
                                    Document = document
                                });
                            break;
                        }

                        case Owner.Type.Contract:
                            var contract = await _context.Contracts.SingleAsync(c => c.Id == materialOwnerDto.OwnerId);

                            contract.Documents.Add(new ContractDocument {
                                Document = document
                            });
                            break;

                        case Owner.Type.ProtectionDoc:
                            var protectionDoc =
                                await _context.ProtectionDocs.SingleAsync(pd => pd.Id == materialOwnerDto.OwnerId);

                            protectionDoc.Documents.Add(new ProtectionDocDocument {
                                Document = document
                            });
                            break;
                        }
                    }

                    await _documentWorkflowApplier.ApplyInitialAsync(document, message.UserId);

                    await _context.SaveChangesAsync();

                    await _requestDocumentApplier.ApplyAsync(document.Id);

                    await _contractDocumentApplier.ApplyAsync(document.Id);

                    await _context.SaveChangesAsync();

                    transaction.Commit();
                }
                return(document.Id);
            }
Exemple #7
0
            public async Task <int> Handle(Command message)
            {
                var document = _mapper.Map <Document>(message.MaterialDetailDto);

                document.DocumentType = DocumentType.Outgoing;
                using (var transaction = await _context.Database.BeginTransactionAsync())
                {
                    try
                    {
                        _numberGenerator.GenerateBarcode(document);

                        await _context.AddAsync(document);

                        await _context.SaveChangesAsync();

                        _numberGenerator.GenerateNumForRegisters(document);
                        await _context.SaveChangesAsync();

                        message.MaterialDetailDto.UserInput.DocumentId = document.Id;

                        foreach (var materialOwnerDto in message.MaterialDetailDto.Owners)
                        {
                            switch (materialOwnerDto.OwnerType)
                            {
                            case Owner.Type.Request:
                            {
                                var request = await _context.Requests.SingleAsync(r => r.Id == materialOwnerDto.OwnerId);

                                request.Documents.Add(new RequestDocument {
                                        Document = document
                                    });
                                break;
                            }

                            case Owner.Type.Contract:
                                var contract = await _context.Contracts.SingleAsync(c => c.Id == materialOwnerDto.OwnerId);

                                contract.Documents.Add(new ContractDocument {
                                    Document = document
                                });
                                break;

                            case Owner.Type.ProtectionDoc:
                                var protectionDoc =
                                    await _context.ProtectionDocs.SingleAsync(pd => pd.Id == materialOwnerDto.OwnerId);

                                protectionDoc.Documents.Add(new ProtectionDocDocument {
                                    Document = document
                                });
                                break;
                            }
                        }

                        await _documentWorkflowApplier.ApplyInitialAsync(document, message.UserId);

                        await _context.SaveChangesAsync();

                        var input = new DocumentUserInput
                        {
                            DocumentId = document.Id,
                            UserInput  = JsonConvert.SerializeObject(message.MaterialDetailDto.UserInput)
                        };
                        await _context.DocumentUserInputs.AddAsync(input);

                        await _context.SaveChangesAsync();

                        await _requestDocumentApplier.ApplyAsync(document.Id);

                        await _contractDocumentApplier.ApplyAsync(document.Id);

                        await _context.SaveChangesAsync();

                        transaction.Commit();
                    }

                    catch (Exception e)
                    {
                        throw;
                    }
                    finally
                    {
                    }
                }
                return(document.Id);
            }