Exemple #1
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 #2
0
            public async Task <MaterialInternalDetailDto> Handle(Command message)
            {
                var documentId = message.DocumentId;
                var material   = message.MaterialDetailDto;

                var document = await _context.Documents
                               .SingleOrDefaultAsync(r => r.Id == documentId);

                if (document == null)
                {
                    throw new DataNotFoundException(nameof(Document),
                                                    DataNotFoundException.OperationType.Update, documentId);
                }

                _mapper.Map(material, document);

                await _requestDocumentApplier.ApplyAsync(documentId);

                await _contractDocumentApplier.ApplyAsync(documentId);

                await _context.SaveChangesAsync();

                var requestLinksToRemove = _context.RequestsDocuments
                                           .Where(rd => rd.DocumentId == documentId &&
                                                  !material.Owners.Where(o => o.OwnerType == Owner.Type.Request)
                                                  .Select(o => o.OwnerId).Contains(rd.RequestId));

                var requestLinksToAdd = _context.Requests
                                        .Where(r => material.Owners.Where(o => o.OwnerType == Owner.Type.Request)
                                               .Select(o => o.OwnerId).Contains(r.Id))
                                        .Where(r => r.Documents.All(rd => rd.DocumentId != documentId))
                                        .Select(r => new RequestDocument {
                    RequestId = r.Id, DocumentId = documentId
                })
                                        .ToList();

                _context.RequestsDocuments.RemoveRange((requestLinksToRemove));
                await _context.RequestsDocuments.AddRangeAsync(requestLinksToAdd);

                await _context.SaveChangesAsync();

                var contractLinksToRemove = _context.ContractsDocuments
                                            .Where(rd => rd.DocumentId == documentId &&
                                                   !material.Owners.Where(o => o.OwnerType == Owner.Type.Contract)
                                                   .Select(o => o.OwnerId).Contains(rd.ContractId));

                var contractLinksToAdd = _context.Contracts
                                         .Where(c => material.Owners.Where(o => o.OwnerType == Owner.Type.Contract)
                                                .Select(o => o.OwnerId).Contains(c.Id))
                                         .Where(c => c.Documents.All(cd => cd.DocumentId != documentId))
                                         .Select(c => new ContractDocument {
                    ContractId = c.Id, DocumentId = documentId
                })
                                         .ToList();

                _context.ContractsDocuments.RemoveRange(contractLinksToRemove);
                await _context.ContractsDocuments.AddRangeAsync(contractLinksToAdd);

                await _context.SaveChangesAsync();

                var protectionDocsLinksToRemove = _context.ProtectionDocDocuments
                                                  .Where(pd => pd.DocumentId == documentId &&
                                                         !material.Owners.Where(o => o.OwnerType == Owner.Type.ProtectionDoc)
                                                         .Select(o => o.OwnerId).Contains(pd.ProtectionDocId));

                var protectionDocsLinksToAdd = _context.ProtectionDocs
                                               .Where(pd => material.Owners.Where(o => o.OwnerType == Owner.Type.ProtectionDoc)
                                                      .Select(o => o.OwnerId).Contains(pd.Id))
                                               .Where(c => c.Documents.All(pd => pd.DocumentId != documentId))
                                               .Select(c => new ProtectionDocDocument {
                    ProtectionDocId = c.Id, DocumentId = documentId
                })
                                               .ToList();

                _context.ProtectionDocDocuments.RemoveRange(protectionDocsLinksToRemove);
                await _context.ProtectionDocDocuments.AddRangeAsync(protectionDocsLinksToAdd);

                await _context.SaveChangesAsync();

                var input = await _context.DocumentUserInputs
                            .SingleOrDefaultAsync(i => i.DocumentId == documentId);

                if (input == null)
                {
                    input = new DocumentUserInput
                    {
                        DocumentId = document.Id,
                        UserInput  = JsonConvert.SerializeObject(message.MaterialDetailDto.UserInput)
                    };
                    _context.DocumentUserInputs.Add(input);
                }
                else
                {
                    input.UserInput = JsonConvert.SerializeObject(message.MaterialDetailDto.UserInput);
                }

                await _context.SaveChangesAsync();

                var documentWithIncludes = await _context.Documents
                                           .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)
                                           .Include(r => r.MainAttachment)
                                           .Include(r => r.Requests)
                                           .Include(r => r.Contracts)
                                           .Include(r => r.ProtectionDocs)
                                           .SingleOrDefaultAsync(r => r.Id == documentId);


                var result = _mapper.Map <Document, MaterialInternalDetailDto>(documentWithIncludes);

                result.UserInput = message.MaterialDetailDto.UserInput;
                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 #3
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 #4
0
            public async Task <MaterialOutgoingDetailDto> Handle(Command message)
            {
                var documentId = message.DocumentId;
                var material   = message.MaterialDetailDto;

                var document = await _context.Documents
                               .Include(d => d.Type)
                               .SingleOrDefaultAsync(r => r.Id == documentId);

                if (document == null)
                {
                    throw new DataNotFoundException(nameof(Document),
                                                    DataNotFoundException.OperationType.Update, documentId);
                }

                _mapper.Map(material, document);


                await _requestDocumentApplier.ApplyAsync(documentId);

                await _contractDocumentApplier.ApplyAsync(documentId);

                await _context.SaveChangesAsync();

                var requestLinksToRemove = _context.RequestsDocuments
                                           .Where(rd => rd.DocumentId == documentId &&
                                                  !material.Owners.Where(o => o.OwnerType == Owner.Type.Request)
                                                  .Select(o => o.OwnerId).Contains(rd.RequestId));

                var requestLinksToAdd = _context.Requests
                                        .Where(r => material.Owners.Where(o => o.OwnerType == Owner.Type.Request)
                                               .Select(o => o.OwnerId).Contains(r.Id))
                                        .Where(r => r.Documents.All(rd => rd.DocumentId != documentId))
                                        .Select(r => new RequestDocument {
                    RequestId = r.Id, DocumentId = documentId
                })
                                        .ToList();

                _context.RequestsDocuments.RemoveRange((requestLinksToRemove));
                await _context.RequestsDocuments.AddRangeAsync(requestLinksToAdd);

                await _context.SaveChangesAsync();

                var contractLinksToRemove = _context.ContractsDocuments
                                            .Where(rd => rd.DocumentId == documentId &&
                                                   !material.Owners.Where(o => o.OwnerType == Owner.Type.Contract)
                                                   .Select(o => o.OwnerId).Contains(rd.ContractId));

                var contractLinksToAdd = _context.Contracts
                                         .Where(c => material.Owners.Where(o => o.OwnerType == Owner.Type.Contract)
                                                .Select(o => o.OwnerId).Contains(c.Id))
                                         .Where(c => c.Documents.All(cd => cd.DocumentId != documentId))
                                         .Select(c => new ContractDocument {
                    ContractId = c.Id, DocumentId = documentId
                })
                                         .ToList();

                _context.ContractsDocuments.RemoveRange(contractLinksToRemove);
                await _context.ContractsDocuments.AddRangeAsync(contractLinksToAdd);

                await _context.SaveChangesAsync();

                var protectionDocsLinksToRemove = _context.ProtectionDocDocuments
                                                  .Where(pd => pd.DocumentId == documentId &&
                                                         !material.Owners.Where(o => o.OwnerType == Owner.Type.ProtectionDoc)
                                                         .Select(o => o.OwnerId).Contains(pd.ProtectionDocId));

                var protectionDocsLinksToAdd = _context.ProtectionDocs
                                               .Where(pd => material.Owners.Where(o => o.OwnerType == Owner.Type.ProtectionDoc)
                                                      .Select(o => o.OwnerId).Contains(pd.Id))
                                               .Where(c => c.Documents.All(pd => pd.DocumentId != documentId))
                                               .Select(c => new ProtectionDocDocument {
                    ProtectionDocId = c.Id, DocumentId = documentId
                })
                                               .ToList();

                _context.ProtectionDocDocuments.RemoveRange(protectionDocsLinksToRemove);
                await _context.ProtectionDocDocuments.AddRangeAsync(protectionDocsLinksToAdd);

                await _context.SaveChangesAsync();

                var input = await _context.DocumentUserInputs
                            .SingleOrDefaultAsync(i => i.DocumentId == documentId);

                if (input == null)
                {
                    input = new DocumentUserInput
                    {
                        DocumentId = document.Id,
                        UserInput  = JsonConvert.SerializeObject(message.MaterialDetailDto.UserInput)
                    };
                    _context.DocumentUserInputs.Add(input);
                }
                else
                {
                    input.UserInput = JsonConvert.SerializeObject(message.MaterialDetailDto.UserInput);
                }

                try
                {
                    await _context.SaveChangesAsync();

                    var documentWithIncludes = await _context.Documents
                                               .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)
                                               .Include(r => r.Workflows).ThenInclude(r => r.DocumentUserSignature)
                                               .Include(r => r.MainAttachment)
                                               .Include(r => r.Requests)
                                               .Include(d => d.Contracts)
                                               .Include(d => d.ProtectionDocs)
                                               .SingleOrDefaultAsync(r => r.Id == documentId);

                    var result                 = _mapper.Map <Document, MaterialOutgoingDetailDto>(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();

                    result.UserInput = JsonConvert.DeserializeObject <UserInputDto>(input.UserInput);
                    if (document.MainAttachmentId == null)
                    {
                        var documentGenerator = _templateGeneratorFactory.Create(result.UserInput.Code);
                        var generatedFile     = documentGenerator.Process(new Dictionary <string, object>
                        {
                            { "UserId", message.UserId },
                            { "RequestId", result.UserInput.OwnerId },
                            { "DocumentId", result.UserInput.DocumentId },
                            { "UserInputFields", result.UserInput.Fields },
                            { "SelectedRequestIds", result.UserInput.SelectedRequestIds },
                            { "PageCount", result.PageCount }
                        });
                        result.PageCount = generatedFile.PageCount;
                    }
                    else
                    {
                        result.PageCount = document.MainAttachment.PageCount;
                    }

                    return(result);
                }
                catch (Exception e)
                {
                    throw new DatabaseException(e.InnerException?.Message ?? e.Message);
                }
            }
Exemple #5
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);
            }