예제 #1
0
        protected bool TrySign(SignedContent content)
        {
            if (Settings.Value.DebugUseTestSign)
            {
                content.SignWithTestSignature = true;
                return(true);
            }
            byte[] sign;
            var    result = TrySign(content.Content, out sign);

            if (result)
            {
                content.Signature = sign;
            }

            return(result);
        }
        public void OutBoundInvoices(List <Message> msgs)
        {
            var invoicesReq = msgs.Where(x => x.Entities[0].AttachmentType == AttachmentType.Invoice);

            foreach (var msg in invoicesReq)
            {
                Entity invoice = msg.Entities.FirstOrDefault(i => i.AttachmentTypeValue == Diadoc.Api.Com.AttachmentType.Invoice &&
                                                             i.DocumentInfo.RevocationStatus == RevocationStatus.RevocationStatusNone);
                if (invoice == null)
                {
                    continue;
                }
                Entity invoiceReciept = msg.Entities.FirstOrDefault(i => i.ParentEntityId == invoice.EntityId &&
                                                                    i.AttachmentTypeValue == Diadoc.Api.Com.AttachmentType.InvoiceConfirmation);

                GeneratedFile invoiceReceipt = api.GenerateInvoiceDocumentReceiptXml(
                    token,
                    box.BoxId,
                    invoice.DocumentInfo.MessageId,
                    invoiceReciept.EntityId,
                    signers.First());

                SignedContent signContentInvoiceReciept = new SignedContent();
                signContentInvoiceReciept.Content = invoiceReceipt.Content;
                signContentInvoiceReciept.SignWithTestSignature = true;

                ReceiptAttachment receiptInvoice = new ReceiptAttachment {
                    ParentEntityId = invoiceReciept.EntityId,
                    SignedContent  = signContentInvoiceReciept
                };

                var patch = new MessagePatchToPost {
                    BoxId     = box.BoxId,
                    MessageId = invoice.DocumentInfo.MessageId
                };
                patch.AddReceipt(receiptInvoice);

                api.PostMessagePatch(token, patch);

                Thread.Sleep(1000);
            }
        }
예제 #3
0
        public async Task Save()
        {
            try {
                BeginAction();
                var patch = Payload.Patch();

                RevocationRequestInfo revinfo = new RevocationRequestInfo();
                revinfo.Comment = Comment.Value;
                revinfo.Signer  = GetSigner();

                GeneratedFile revocationXml = await Async((x) => Payload.Api.GenerateRevocationRequestXml(
                                                              x,
                                                              Payload.BoxId,
                                                              Payload.Message.MessageId,
                                                              Payload.Entity.EntityId,
                                                              revinfo));

                SignedContent revocSignContent = new SignedContent();
                revocSignContent.Content = revocationXml.Content;

                if (!TrySign(revocSignContent))
                {
                    throw new Exception("Ошибка подписи документа TrySign");
                }

                RevocationRequestAttachment revattch = new RevocationRequestAttachment();
                revattch.ParentEntityId = Payload.Entity.EntityId;
                revattch.SignedContent  = revocSignContent;

                patch.AddRevocationRequestAttachment(revattch);
                await Async(x => Payload.Api.PostMessagePatch(x, patch));
                await EndAction();
            }
            catch (Exception e) {
                var error = ErrorHelper.TranslateException(e)
                            ?? "Не удалось выполнить операцию, попробуйте повторить позднее.";
                Manager.Warning(error);
                Log.Error(error, e);
                await EndAction(false);
            }
        }
        public void Revocation(Message revocation)
        {
            RevocationRequestInfo revinfo = new RevocationRequestInfo();

            revinfo.Comment = "АННУЛИРОВНИЕ";
            revinfo.Signer  = signers.First();

            var document = revocation.Entities.First();

            MessagePatchToPost patch = new MessagePatchToPost()
            {
                BoxId     = box.BoxId,
                MessageId = revocation.MessageId
            };

            GeneratedFile revocationXml = api.GenerateRevocationRequestXml(
                token,
                box.BoxId,
                revocation.MessageId,
                document.EntityId,
                revinfo);

            SignedContent revocSignContent = new SignedContent();

            revocSignContent.Content = revocationXml.Content;
            revocSignContent.SignWithTestSignature = true;

            RevocationRequestAttachment revattch = new RevocationRequestAttachment();

            revattch.ParentEntityId = document.EntityId;
            revattch.SignedContent  = revocSignContent;

            patch.AddRevocationRequestAttachment(revattch);

            api.PostMessagePatch(token, patch);
        }
예제 #5
0
        public async Task Save()
        {
            try {
                BeginAction();
                var patch = Payload.Patch();

                if (ReqRevocationSign)
                {
                    Entity revocreq = Payload.Message.Entities.FirstOrDefault(x =>
                                                                              x.AttachmentTypeValue == Diadoc.Api.Com.AttachmentType.RevocationRequest);
                    SignatureRejectionInfo signrejinfo = new SignatureRejectionInfo();
                    signrejinfo.Signer       = GetSigner();
                    signrejinfo.ErrorMessage = Comment.Value;
                    GeneratedFile revocRejectSign = await Async((x) => Payload.Api.GenerateSignatureRejectionXml(
                                                                    x,
                                                                    Payload.BoxId,
                                                                    Payload.Message.MessageId,
                                                                    revocreq.EntityId,
                                                                    signrejinfo));

                    XmlSignatureRejectionAttachment signrejattch = new XmlSignatureRejectionAttachment();
                    signrejattch.ParentEntityId = revocreq.EntityId;
                    SignedContent signcontent = new SignedContent();
                    signcontent.Content = revocRejectSign.Content;
                    if (!TrySign(signcontent))
                    {
                        throw new Exception("Ошибка подписи документа TrySign");
                    }
                    signrejattch.SignedContent = signcontent;
                    patch.AddXmlSignatureRejectionAttachment(signrejattch);
                }
                else
                {
                    var content = new SignedContent();
                    //комментарий должен быть всегда
                    if (!String.IsNullOrEmpty(Comment.Value))
                    {
                        content.Content = Encoding.UTF8.GetBytes(Comment.Value);
                    }
                    else
                    {
                        content.Content = Encoding.UTF8.GetBytes(" ");
                    }

                    if (!TrySign(content))
                    {
                        throw new Exception("Ошибка подписи документа TrySign");
                    }
                    patch.RequestedSignatureRejections.Add(new RequestedSignatureRejection {
                        ParentEntityId = Payload.Entity.EntityId,
                        SignedContent  = content
                    });
                }
                await Async(x => Payload.Api.PostMessagePatch(x, patch));
                await EndAction();
            }
            catch (Exception e) {
                var error = ErrorHelper.TranslateException(e)
                            ?? "Не удалось выполнить операцию, попробуйте повторить позднее.";
                Manager.Warning(error);
                Log.Error(error, e);
                await EndAction(false);
            }
        }
예제 #6
0
        public async Task Save()
        {
            try {
                BeginAction();
                MessagePatchToPost patch  = null;
                Signer             signer = GetSigner();
                if (ReqRevocationSign)
                {
                    Entity revocReq = Payload.Message.Entities.FirstOrDefault(x => x.AttachmentTypeValue == Diadoc.Api.Com.AttachmentType.RevocationRequest);
                    patch = Payload.Patch();
                    DocumentSignature acceptSignature = new DocumentSignature();
                    acceptSignature.IsApprovementSignature = true;
                    acceptSignature.ParentEntityId         = revocReq.EntityId;
                    byte[] sign = null;
                    if (!TrySign(revocReq.Content.Data, out sign))
                    {
                        throw new Exception("Ошибка подписи документа TrySign");
                    }
                    if (Settings.Value.DebugUseTestSign)
                    {
                        acceptSignature.SignWithTestSignature = true;
                    }
                    acceptSignature.Signature = sign;
                    patch.AddSignature(acceptSignature);
                    await Async(x => Payload.Api.PostMessagePatch(x, patch));
                }
                else
                {
                    if (Payload.Entity.AttachmentType == AttachmentType.Nonformalized)
                    {
                        patch = Payload.Patch();
                        byte[] sign = null;
                        if (!TrySign(Payload.Entity.Content.Data, out sign))
                        {
                            throw new Exception("Ошибка подписи документа TrySign");
                        }
                        var signature = new DocumentSignature {
                            ParentEntityId = Payload.Entity.EntityId,
                            Signature      = sign
                        };
                        if (Settings.Value.DebugUseTestSign)
                        {
                            signature.SignWithTestSignature = true;
                        }
                        patch.AddSignature(signature);
                        await Async(x => Payload.Api.PostMessagePatch(x, patch));
                    }
                    else if (Payload.Entity.AttachmentType == AttachmentType.XmlTorg12)
                    {
                        if (SaveData.Value)
                        {
                            SignTorg12Autosave autosave = new SignTorg12Autosave();
                            autosave.SignerJobTitle = RcvJobTitle.Value;
                            if (LikeReciever.Value)
                            {
                                autosave.LikeReciever = true;
                            }
                            else
                            {
                                autosave.AcptFirstName  = AcptFirstName.Value;
                                autosave.AcptSurename   = AcptSurename.Value;
                                autosave.AcptPatronimic = AcptPatronimic.Value;
                                autosave.AcptJobTitle   = AcptJobTitle.Value;
                            }
                            if (ByAttorney.Value)
                            {
                                autosave.ByAttorney      = true;
                                autosave.AtrNum          = AtrNum.Value;
                                autosave.AtrDate         = AtrDate.Value.Value;
                                autosave.AtrOrganization = AtrOrganization.Value;
                                autosave.AtrJobTitle     = AtrJobTitle.Value;
                                autosave.AtrSurename     = AtrSurename.Value;
                                autosave.AtrFirstName    = AtrFirstName.Value;
                                autosave.AtrPatronymic   = AtrPatronymic.Value;
                                autosave.AtrAddInfo      = AtrAddInfo.Value;
                            }
                            autosave.Comment = Comment.Value;
                            Session.Save(autosave);
                            for (int i = autosave_max - 1; i < SavedData.Value.Count; i++)
                            {
                                Session.Delete(SavedData.Value[i]);
                            }
                        }
                        Official recived  = GetRevicedOfficial();
                        Official accepted = GetAcceptetOfficial();
                        Attorney attorney = GetAttorney();

                        var inf = new Torg12BuyerTitleInfo()
                        {
                            ReceivedBy          = recived,                    //лицо, получившее груз signer
                            AcceptedBy          = accepted,                   //лицо, принявшее груз
                            Attorney            = attorney,
                            AdditionalInfo      = Comment,
                            ShipmentReceiptDate = RcvDate.Value.ToString("dd.MM.yyyy"),
                            Signer = signer
                        };

                        GeneratedFile torg12XmlForBuyer = await Async((x) => Payload.Api.GenerateTorg12XmlForBuyer(
                                                                          x,
                                                                          inf,
                                                                          Payload.BoxId,
                                                                          Payload.Entity.DocumentInfo.MessageId,
                                                                          Payload.Entity.DocumentInfo.EntityId));

                        SignedContent signContent = new SignedContent();
                        signContent.Content = torg12XmlForBuyer.Content;
                        if (!TrySign(signContent))
                        {
                            throw new Exception("Ошибка подписи документа TrySign");
                        }

                        ReceiptAttachment receipt = new ReceiptAttachment {
                            ParentEntityId = Payload.Entity.DocumentInfo.EntityId,
                            SignedContent  = signContent
                        };
                        patch = Payload.PatchTorg12(receipt);
                        await Async(x => Payload.Api.PostMessagePatch(x, patch));

                        Log.Info($"Документ {patch.MessageId} успешно подписан");
                    }
                    else if (Payload.Entity.AttachmentType == AttachmentType.Invoice)
                    {
                        Entity invoice = Payload.Message.Entities.First(i => i.AttachmentTypeValue == Diadoc.Api.Com.AttachmentType.Invoice);

                        GeneratedFile invoiceReceipt = await Async((x) => Payload.Api.GenerateInvoiceDocumentReceiptXml(
                                                                       x,
                                                                       Payload.BoxId,
                                                                       Payload.Entity.DocumentInfo.MessageId,
                                                                       invoice.EntityId,
                                                                       signer));

                        SignedContent signContentInvoiceReciept = new SignedContent();
                        signContentInvoiceReciept.Content = invoiceReceipt.Content;
                        if (!TrySign(signContentInvoiceReciept))
                        {
                            throw new Exception("Ошибка подписи документа TrySign");
                        }

                        ReceiptAttachment receiptInvoice = new ReceiptAttachment {
                            ParentEntityId = invoice.EntityId,
                            SignedContent  = signContentInvoiceReciept
                        };

                        Entity        invoiceConfirmation        = Payload.Message.Entities.OrderBy(x => x.CreationTime).First(i => i.AttachmentTypeValue == Diadoc.Api.Com.AttachmentType.InvoiceConfirmation);
                        GeneratedFile invoiceConfirmationReceipt = await Async((x) => Payload.Api.GenerateInvoiceDocumentReceiptXml(
                                                                                   x,
                                                                                   Payload.BoxId,
                                                                                   Payload.Entity.DocumentInfo.MessageId,
                                                                                   invoiceConfirmation.EntityId,
                                                                                   signer));

                        SignedContent signContentInvoiceConfirmationReciept = new SignedContent();
                        signContentInvoiceConfirmationReciept.Content = invoiceReceipt.Content;

                        if (!TrySign(signContentInvoiceConfirmationReciept))
                        {
                            throw new Exception("Ошибка подписи документа TrySign");
                        }

                        ReceiptAttachment invoiceConfirmationreceipt = new ReceiptAttachment {
                            ParentEntityId = invoiceConfirmation.EntityId,
                            SignedContent  = signContentInvoiceConfirmationReciept
                        };

                        patch = Payload.Patch(receiptInvoice, invoiceConfirmationreceipt);

                        await Async(x => Payload.Api.PostMessagePatch(x, patch));

                        Log.Info($"Документ {patch.MessageId} receiptInvoice, invoiceConfirmationreceipt отправлены");

                        Entity invoiceDateConfirmation = await Async((x) => {
                            Message msg        = null;
                            Entity dateConfirm = null;
                            int breaker        = 0;
                            do
                            {
                                msg = Payload.Api.GetMessage(Payload.Token,
                                                             Payload.BoxId,
                                                             Payload.Entity.DocumentInfo.MessageId,
                                                             Payload.Entity.EntityId);
                                dateConfirm = GetDateConfirmationStep7(msg);
                                breaker++;
                                TaskEx.Delay(500).Wait();
                            }while (dateConfirm == null && breaker < 10);
                            if (dateConfirm == null)
                            {
                                throw new TimeoutException("Превышено время ожидания ответа, попробуйте повторить позднее.");
                            }
                            LastPatchStamp = msg.LastPatchTimestamp;
                            return(dateConfirm);
                        });

                        GeneratedFile invoiceOperConfirmationReceipt = await Async((x) => Payload.Api.GenerateInvoiceDocumentReceiptXml(
                                                                                       x,
                                                                                       Payload.BoxId,
                                                                                       Payload.Entity.DocumentInfo.MessageId,
                                                                                       invoiceDateConfirmation.EntityId,
                                                                                       signer));

                        SignedContent signContentOperInvoiceConfrmReciept = new SignedContent();
                        signContentOperInvoiceConfrmReciept.Content = invoiceOperConfirmationReceipt.Content;

                        if (!TrySign(signContentOperInvoiceConfrmReciept))
                        {
                            throw new Exception("Ошибка подписи документа TrySign");
                        }

                        ReceiptAttachment receipt = new ReceiptAttachment {
                            ParentEntityId = invoiceDateConfirmation.EntityId,
                            SignedContent  = signContentOperInvoiceConfrmReciept
                        };
                        patch = Payload.Patch(receipt);
                        await Async(x => Payload.Api.PostMessagePatch(x, patch));

                        Log.Info($"Документ {patch.MessageId} invoiceDateConfirmation отправлен");
                    }
                }
                await EndAction();
            }
            catch (Exception e) {
                var error = ErrorHelper.TranslateException(e)
                            ?? "Не удалось выполнить операцию, попробуйте повторить позднее.";
                Manager.Warning(error);
                Log.Error(error, e);
                await EndAction(false);
            }
        }