Exemplo n.º 1
0
        /// <summary>
        /// Отправка последней версии документа контрагенту.
        /// </summary>
        /// <param name="document">Документ для отправки.</param>
        /// <param name="documentInfo">Информация о документе, связанная с коммуникацией с контрагентом.</param>
        /// <param name="service">Сервис обмена.</param>
        /// <param name="addenda">Приложения.</param>
        public static void SendDocumentToCounterparty(Docflow.IOfficialDocument document,
                                                      Sungero.Exchange.Structures.Module.SendToCounterpartyInfo documentInfo,
                                                      ExchangeCore.IExchangeService service,
                                                      List <Docflow.IOfficialDocument> addenda)
        {
            if (!documentInfo.CanApprove && !documentInfo.HasApprovalSignature)
            {
                Dialogs.NotifyMessage(Resources.SendCounterpartyNotApproved);
                return;
            }

            var dialog = Dialogs.CreateInputDialog(Resources.SendCounterpartyDialogTitle);

            dialog.HelpCode = Constants.Module.HelpCodes.SendDocument;

            var counterparty = dialog.AddSelect(Resources.SendCounterpartyReceiver, true, documentInfo.DefaultCounterparty)
                               .From(documentInfo.Counterparties)
                               .Where(x => x.Status == CoreEntities.DatabookEntry.Status.Active);

            var defaultBox = documentInfo.Boxes.FirstOrDefault(x => Equals(x.ExchangeService, service)) ?? documentInfo.DefaultBox;

            var box = dialog.AddSelect(Resources.SendCounterpartySender, true, defaultBox)
                      .From(documentInfo.Boxes)
                      .Where(x => x.Status == CoreEntities.DatabookEntry.Status.Active);

            box.IsEnabled = !(Docflow.AccountingDocumentBases.Is(document) && Docflow.AccountingDocumentBases.As(document).IsFormalized == true);
            var users = documentInfo.Certificates.Certificates.Select(c => c.Owner).ToList();

            var signatureOwner = dialog.AddSelect(Docflow.OfficialDocuments.Info.Properties.OurSignatory.LocalizedName, false, users.FirstOrDefault())
                                 .From(users);

            signatureOwner.IsEnabled = documentInfo.IsSignedByUs;

            var allowedAddenda         = documentInfo.Addenda.Select(a => a.Addendum).ToList();
            var defaultSelectedAddenda = addenda.Intersect(allowedAddenda).ToArray();
            var selectedAddenda        = dialog.AddSelectMany(Resources.SendCounterpartyAddenda, false, defaultSelectedAddenda)
                                         .From(allowedAddenda);

            selectedAddenda.IsEnabled = documentInfo.HasAddendaToSend;

            var needSign = dialog.AddBoolean(Resources.SendCounterpartyNeedSign, false);

            if (Docflow.AccountingDocumentBases.Is(document) && Docflow.AccountingDocumentBases.As(document).IsFormalized == true)
            {
                var accDocument               = Docflow.AccountingDocumentBases.As(document);
                var isSF                      = accDocument.FormalizedFunction == Docflow.AccountingDocumentBase.FormalizedFunction.Schf;
                var isWaybill                 = FinancialArchive.Waybills.Is(document);
                var isContractStatement       = FinancialArchive.ContractStatements.Is(document);
                var isUPD                     = FinancialArchive.UniversalTransferDocuments.Is(document);
                var isDiadoc                  = accDocument.BusinessUnitBox.ExchangeService.ExchangeProvider == ExchangeCore.ExchangeService.ExchangeProvider.Diadoc;
                var isDiadocUPD               = isUPD && isDiadoc;
                var isDiadocContractStatement = isContractStatement && isDiadoc;
                needSign.Value     = isWaybill || isUPD || isContractStatement;
                needSign.IsEnabled = !(isWaybill || isSF || isDiadocUPD || isDiadocContractStatement);
            }
            else
            {
                needSign.Value = Docflow.ContractualDocumentBases.Is(document);
            }

            var comment = dialog.AddMultilineString(Resources.SendCounterpartyComment, false);

            var sendButton = dialog.Buttons.AddCustom(Resources.SendCounterpartySendButton);

            dialog.Buttons.Default = sendButton;
            dialog.Buttons.AddCancel();

            dialog.SetOnRefresh(x =>
            {
                if (x.IsValid && documentInfo.Certificates.CanSign && signatureOwner.Value == null)
                {
                    x.AddInformation(Resources.SendCounterpartySignAndSendHint);
                }

                if (selectedAddenda.Value != null)
                {
                    var exchangeDocumentsSize = selectedAddenda.Value.Select(s => s.LastVersion.Body.Size).Sum() + document.LastVersion.Body.Size;
                    if (exchangeDocumentsSize >= Constants.Module.ExchangeDocumentMaxSize)
                    {
                        x.AddError(Resources.AddendaOversized);
                    }
                }
                if (counterparty.Value != null)
                {
                    var boxes = counterparty.Value.ExchangeBoxes.Where(b => b.Box.Status == ExchangeCore.BusinessUnitBox.Status.Active &&
                                                                       b.Box.ConnectionStatus == ExchangeCore.BusinessUnitBox.ConnectionStatus.Connected &&
                                                                       b.Status == Parties.CounterpartyExchangeBoxes.Status.Active && b.IsDefault == true);
                    if (!boxes.Any())
                    {
                        x.AddError(Resources.BoxesNotFound);
                    }
                }
            });

            counterparty.SetOnValueChanged(x =>
            {
                if (x.NewValue != x.OldValue)
                {
                    var newCounterpartyList = documentInfo.Counterparties;
                    if (x.NewValue != null)
                    {
                        newCounterpartyList = documentInfo.Counterparties.Where(c => c.Equals(x.NewValue)).ToList();
                    }

                    documentInfo.Boxes = Functions.Module.Remote.GetConnectedExchangeBoxesToCounterparty(document, newCounterpartyList);
                    box = box.From(documentInfo.Boxes);

                    // Если ящик не был выбран или был выбран ящик, через который нет обмена с выбранным контрагентом, или был выбран ящик, отличный от Synerdocs,
                    // заполнить поле первым подходящим ящиком.
                    if (box.Value == null || !documentInfo.Boxes.Contains(box.Value) || box.Value.ExchangeService.ExchangeProvider != ExchangeCore.ExchangeService.ExchangeProvider.Synerdocs)
                    {
                        box.Value = documentInfo.Boxes.FirstOrDefault();
                    }
                }
            });

            box.SetOnValueChanged(x =>
            {
                if (x.NewValue != x.OldValue)
                {
                    if (x.NewValue != null)
                    {
                        documentInfo.Certificates = Functions.Module.Remote.GetDocumentCertificatesToBox(document, x.NewValue);
                        documentInfo.IsSignedByUs = documentInfo.Certificates.Certificates.Any();
                    }
                    else
                    {
                        documentInfo.Certificates = Structures.Module.DocumentCertificatesInfo
                                                    .Create(new List <ICertificate>(), false, new List <ICertificate>());
                        documentInfo.IsSignedByUs = false;
                    }

                    users                    = documentInfo.Certificates.Certificates.Select(c => c.Owner).ToList();
                    signatureOwner           = signatureOwner.From(users);
                    signatureOwner.Value     = users.FirstOrDefault();
                    signatureOwner.IsEnabled = documentInfo.IsSignedByUs;
                }
            });

            dialog.SetOnButtonClick(x =>
            {
                if (x.Button == sendButton && x.IsValid)
                {
                    var hasExchangeWithCounterparty = counterparty.Value.ExchangeBoxes
                                                      .Any(c => Equals(c.Box, box.Value) &&
                                                           Equals(c.Status, Parties.CounterpartyExchangeBoxes.Status.Active) &&
                                                           c.IsDefault == true);
                    if (!hasExchangeWithCounterparty)
                    {
                        x.AddError(Exchange.Resources.NoExchangeThroughThisService);
                        return;
                    }

                    if (signatureOwner.Value == null && documentInfo.IsSignedByUs && !documentInfo.Certificates.CanSign)
                    {
                        x.AddError(Resources.SendCounterpartyCertificateNotSelected);
                        return;
                    }

                    if (IsLocked(document, x))
                    {
                        return;
                    }

                    foreach (var addendumDocument in selectedAddenda.Value)
                    {
                        if (IsLocked(addendumDocument, x))
                        {
                            return;
                        }
                    }

                    if (Functions.ExchangeDocumentInfo.Remote.LastVersionSended(document, box.Value, counterparty.Value))
                    {
                        x.AddError(Resources.DocumentIsAlreadySentToCounterparty);
                        return;
                    }

                    var noSign      = signatureOwner.Value == null;
                    var certificate = documentInfo.Certificates.Certificates.FirstOrDefault(c => Equals(c.Owner, signatureOwner.Value));
                    ICertificate certificateToRejectFirstVersion = null;

                    if (noSign)
                    {
                        if (!documentInfo.Certificates.CanSign)
                        {
                            if (selectedAddenda.Value.Any())
                            {
                                x.AddError(Resources.SendCounterpartyWithAddendaWhenDocumentNotSigned);
                            }
                            else
                            {
                                x.AddError(Resources.SendCounterpartyCanNotSign);
                            }
                            return;
                        }

                        var signDialog  = Dialogs.CreateTaskDialog(Resources.SendCounterpartySignAndSendQuestion);
                        var signButtons = signDialog.Buttons.AddCustom(Resources.SendCounterpartySignAndSendButton);
                        signDialog.Buttons.AddCancel();

                        if (signDialog.Show() == signButtons)
                        {
                            try
                            {
                                if (IsLocked(document, x))
                                {
                                    return;
                                }

                                certificate = GetCurrentUserExchangeCertificate(box.Value, Company.Employees.Current);

                                certificateToRejectFirstVersion = certificate;

                                var selectedAddendaList = new List <Docflow.IOfficialDocument>();
                                if (selectedAddenda.Value != null)
                                {
                                    selectedAddendaList = selectedAddenda.Value.ToList();
                                }

                                if (certificate == null || !Docflow.PublicFunctions.Module
                                    .ApproveWithAddenda(document, selectedAddendaList, certificate, null, true, true, string.Empty))
                                {
                                    x.AddError(Resources.SendCounterpartyCanNotSign);
                                    return;
                                }
                            }
                            catch (Exception ex)
                            {
                                x.AddError(ex.Message);
                                return;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }

                    try
                    {
                        Functions.Module.Remote.SendDocuments(document, selectedAddenda.Value.ToList(), counterparty.Value, box.Value,
                                                              certificate, needSign.Value.Value, comment.Value);
                        if (Equals(certificate.Owner, Company.Employees.Current))
                        {
                            Functions.Module.SendDeliveryConfirmation(box.Value, certificate, false);
                        }
                        else
                        {
                            Functions.Module.SendDeliveryConfirmation(box.Value, null, false);
                        }
                    }
                    catch (AppliedCodeException ex)
                    {
                        x.AddError(ex.Message);
                        return;
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorFormat("Error sending document: ", ex);
                        x.AddError(Resources.ErrorWhileSendingDocToCounterparty);
                        return;
                    }

                    if (documentInfo.NeedRejectFirstVersion)
                    {
                        if (certificateToRejectFirstVersion == null)
                        {
                            certificateToRejectFirstVersion = GetCurrentUserExchangeCertificate(box.Value, Company.Employees.Current);
                        }

                        TryRejectCounterpartyVersion(document, counterparty.Value, box.Value, certificateToRejectFirstVersion);
                    }

                    var addendaToReject =
                        documentInfo.Addenda
                        .Where(a => a.NeedRejectFirstVersion)
                        .Select(a => a.Addendum)
                        .Where(a => selectedAddenda.Value.Contains(a))
                        .ToList();
                    foreach (var addendum in addendaToReject)
                    {
                        if (certificateToRejectFirstVersion == null)
                        {
                            certificateToRejectFirstVersion = GetCurrentUserExchangeCertificate(box.Value, Company.Employees.Current);
                        }

                        TryRejectCounterpartyVersion(addendum, counterparty.Value, box.Value, certificateToRejectFirstVersion);
                    }

                    Dialogs.NotifyMessage(Resources.SendCounterpartySuccessfully);
                }
            });
            dialog.Show();
        }
        /// <summary>
        /// Получить описание способа доставки.
        /// </summary>
        /// <param name="method">Способ доставки.</param>
        /// <param name="service">Сервис обмена.</param>
        /// <param name="isManyAddressees">True, если отправка на несколько адресов, иначе - false.</param>
        /// <returns>Описание способа доставки.</returns>
        public static string GetDeliveryMethodDescription(IMailDeliveryMethod method, ExchangeCore.IExchangeService service, bool isManyAddressees)
        {
            if (isManyAddressees)
            {
                return(ApprovalTasks.Resources.DeliveryMethodToManyAddressees);
            }
            if (method == null)
            {
                return(string.Empty);
            }

            var exchangeServiceDelivery = method.Sid == Constants.MailDeliveryMethod.Exchange;

            if (exchangeServiceDelivery)
            {
                if (service == null)
                {
                    return(string.Empty);
                }

                return(ApprovalTasks.Resources.DeliveryMethodByExchangeFormat(service.Name));
            }

            return(ApprovalTasks.Resources.DeliveryMethodNotByExchangeFormat(method.Name));
        }
Exemplo n.º 3
0
        public static void SendResultToCounterparty(Docflow.IOfficialDocument document,
                                                    ExchangeCore.IExchangeService service,
                                                    List <Docflow.IOfficialDocument> addenda)
        {
            var isNonformalizedTaxInvoice = false;

            if (FinancialArchive.IncomingTaxInvoices.Is(document) || FinancialArchive.OutgoingTaxInvoices.Is(document))
            {
                isNonformalizedTaxInvoice = Docflow.AccountingDocumentBases.As(document).IsFormalized != true;
            }

            if (isNonformalizedTaxInvoice)
            {
                Dialogs.NotifyMessage(Resources.NonFormalizedTaxInvoiceSendingError);
                return;
            }

            var lockInfo = document != null?Locks.GetLockInfo(document) : null;

            if (lockInfo != null && lockInfo.IsLockedByOther)
            {
                Dialogs.NotifyMessage(lockInfo.LockedMessage);
                return;
            }

            var documentInfo = Functions.Module.Remote.GetInfoForSendToCounterparty(document);

            if (documentInfo.HasError)
            {
                Dialogs.NotifyMessage(documentInfo.Error);
                return;
            }

            if (documentInfo.AnswerIsSent)
            {
                Dialogs.NotifyMessage(Resources.AnswerIsAlreadySent);
                return;
            }

            if (document.LastVersion.Body.Size >= Constants.Module.ExchangeDocumentMaxSize)
            {
                Dialogs.NotifyMessage(Resources.DocumentOversized);
                return;
            }
            var isForcedLocked = false;

            if (!lockInfo.IsLocked)
            {
                isForcedLocked = Locks.TryLock(document);
            }
            if (!lockInfo.IsLocked && !isForcedLocked)
            {
                var lockInfoError = document != null?Locks.GetLockInfo(document) : null;

                Dialogs.NotifyMessage(lockInfoError.LockedMessage);
                return;
            }

            if (documentInfo.IsSignedByCounterparty)
            {
                SendAnswerToCounterparty(document, documentInfo, addenda);
            }
            else
            {
                SendDocumentToCounterparty(document, documentInfo, service, addenda);
            }

            if (isForcedLocked)
            {
                Locks.Unlock(document);
            }
        }