Example #1
0
        public virtual Docflow.IAccountingDocumentBase ImportFormalizedDocument(Docflow.Structures.Module.IByteArray xml, bool requireFtsId)
        {
            var importResult = Functions.Module.Remote.ImportFormalizedDocument(xml, requireFtsId);

            if (!importResult.IsSuccess)
            {
                throw AppliedCodeException.Create(importResult.Error);
            }

            var document = importResult.Document;

            // Вызвать обработчик изменения свойства, чтобы создалась связь с корректируемым документом
            // (с сервера несохранённые связи не передаются для оптимизации).
            if (ClientApplication.ApplicationType == ApplicationType.Desktop)
            {
                document.Corrected = document.Corrected;
            }

            var memory       = new System.IO.MemoryStream(importResult.Body);
            var publicMemory = new System.IO.MemoryStream(importResult.PublicBody);

            var version = document.LastVersion;

            version.Body.Write(memory);
            version.PublicBody.Write(publicMemory);

            return(document);
        }
Example #2
0
        public virtual Structures.Module.IImportResult ImportFormalizedDocument(Docflow.Structures.Module.IByteArray file, bool requireFtsId)
        {
            var result      = Structures.Module.ImportResult.Create();
            var sellerTitle = FormalizeDocumentsParser.Extension.GetDocument <FormalizeDocumentsParser.ISellerTitle>(file.Bytes);

            if (sellerTitle != null)
            {
                var isGoodsTransfer       = sellerTitle.DocumentType == FormalizeDocumentsParser.DocumentType.GoodsTransferDocument;
                var isWorksTransfer       = sellerTitle.DocumentType == FormalizeDocumentsParser.DocumentType.WorksTransferDocument;
                var isUniversalTransfer   = sellerTitle.DocumentType == FormalizeDocumentsParser.DocumentType.UniversalTransferDocument;
                var isUniversalCorrection = sellerTitle.DocumentType == FormalizeDocumentsParser.DocumentType.UniversalCorrectionTransferDocument;
                result.IsSuccess = true;

                Docflow.IAccountingDocumentBase document;
                if (isGoodsTransfer)
                {
                    if (!FinancialArchive.Waybills.AccessRights.CanCreate())
                    {
                        result.Error     = Resources.NoRightsToCreateDocumentFormat(FinancialArchive.Waybills.Info.LocalizedName);
                        result.IsSuccess = false;
                        return(result);
                    }
                    document = FinancialArchive.Waybills.Create();
                }
                else if (isWorksTransfer)
                {
                    if (!FinancialArchive.ContractStatements.AccessRights.CanCreate())
                    {
                        result.Error     = Resources.NoRightsToCreateDocumentFormat(FinancialArchive.ContractStatements.Info.LocalizedName);
                        result.IsSuccess = false;
                        return(result);
                    }
                    document = FinancialArchive.ContractStatements.Create();
                }
                else if (sellerTitle.Function == FormalizeDocumentsParser.UniversalDocumentFunction.Schf)
                {
                    if (!FinancialArchive.OutgoingTaxInvoices.AccessRights.CanCreate())
                    {
                        result.Error     = Resources.NoRightsToCreateDocumentFormat(FinancialArchive.OutgoingTaxInvoices.Info.LocalizedName);
                        result.IsSuccess = false;
                        return(result);
                    }
                    document = FinancialArchive.OutgoingTaxInvoices.Create();
                }
                else
                {
                    if (!FinancialArchive.UniversalTransferDocuments.AccessRights.CanCreate())
                    {
                        result.Error     = Resources.NoRightsToCreateDocumentFormat(FinancialArchive.UniversalTransferDocuments.Info.LocalizedName);
                        result.IsSuccess = false;
                        return(result);
                    }
                    document = FinancialArchive.UniversalTransferDocuments.Create();
                }

                // НОР документа должна подбираться по параметрам, а не от сотрудника.
                document.BusinessUnit = null;

                result.Document = document;

                result.BusinessUnitName = sellerTitle.Seller.GetName();
                result.BusinessUnitTin  = sellerTitle.Seller.Tin;
                result.BusinessUnitTrrc = sellerTitle.Seller.Trrc;
                result.BusinessUnitType = sellerTitle.Seller.Type.ToString();
                result.CounterpartyName = sellerTitle.Buyer.GetName();
                result.CounterpartyTin  = sellerTitle.Buyer.Tin;
                result.CounterpartyTrrc = sellerTitle.Buyer.Trrc;
                result.CounterpartyType = sellerTitle.Buyer.Type.ToString();
                result.SenderFtsId      = sellerTitle.SenderId;
                result.ReceiverFtsId    = sellerTitle.ReceiverId;

                document.IsAdjustment = sellerTitle.IsAdjustment;
                document.IsRevision   = sellerTitle.IsRevision;
                document.TotalAmount  = (double?)sellerTitle.TotalAmount;

                if (sellerTitle.Function.HasValue)
                {
                    switch (sellerTitle.Function.Value)
                    {
                    case FormalizeDocumentsParser.UniversalDocumentFunction.Schf:
                        document.FormalizedFunction = Docflow.AccountingDocumentBase.FormalizedFunction.Schf;
                        document.DocumentKind       = Docflow.PublicFunctions.DocumentKind.GetNativeDocumentKind(Constants.Module.Initialize.OutgoingTaxInvoiceKind);
                        break;

                    case FormalizeDocumentsParser.UniversalDocumentFunction.SchfDop:
                        document.FormalizedFunction = Docflow.AccountingDocumentBase.FormalizedFunction.SchfDop;
                        document.DocumentKind       = Docflow.PublicFunctions.DocumentKind.GetNativeDocumentKind(Constants.Module.Initialize.UniversalTaxInvoiceAndBasicKind);
                        break;

                    case FormalizeDocumentsParser.UniversalDocumentFunction.Dop:
                        document.FormalizedFunction = Docflow.AccountingDocumentBase.FormalizedFunction.Dop;
                        document.DocumentKind       = Docflow.PublicFunctions.DocumentKind.GetNativeDocumentKind(Constants.Module.Initialize.UniversalBasicKind);
                        break;

                    default:
                        throw new Exception("Invalid value for UniversalDocumentFunction");
                    }
                }

                if (document.TotalAmount.HasValue)
                {
                    document.Currency = Commons.Currencies.GetAll().SingleOrDefault(c => c.NumericCode == sellerTitle.CurrencyCode);
                }

                FillBusinessUnitAndCounterparty(document, sellerTitle, requireFtsId);
                result.IsBusinessUnitFound       = document.BusinessUnit != null;
                result.IsCounterpartyFound       = document.Counterparty != null;
                result.IsCounterpartyCanExchange = document.Counterparty != null && document.Counterparty.CanExchange == true;
                if (document.BusinessUnit == null || document.Counterparty == null)
                {
                    if (document.BusinessUnit == null)
                    {
                        result.Error = requireFtsId ?
                                       Resources.ImportDialog_NoBusinessUnitWithFTSIdFormat(sellerTitle.Seller.GetName(), sellerTitle.Seller.Tin, sellerTitle.Seller.Trrc, sellerTitle.SenderId) :
                                       Resources.ImportDialog_NoBusinessUnitWithTINFormat(sellerTitle.Seller.GetName(), sellerTitle.Seller.Tin, sellerTitle.Seller.Trrc);
                        result.IsSuccess = false;
                        return(result);
                    }
                    if (document.Counterparty == null)
                    {
                        result.Error = requireFtsId ?
                                       Resources.ImportDialog_NoCounterpartyWithFTSIdFormat(sellerTitle.Buyer.GetName(), sellerTitle.Buyer.Tin, sellerTitle.Buyer.Trrc, sellerTitle.ReceiverId) :
                                       Resources.ImportDialog_NoCounterpartyWithTINFormat(sellerTitle.Buyer.GetName(), sellerTitle.Buyer.Tin, sellerTitle.Buyer.Trrc);
                        result.IsSuccess = false;
                        return(result);
                    }
                }
                else if (document.Counterparty.CanExchange != true)
                {
                    result.Error     = Resources.ImportDialog_NoExchangeWithCounterpartyFormat(sellerTitle.Buyer.GetName(), sellerTitle.Buyer.Tin, sellerTitle.Buyer.Trrc);
                    result.IsSuccess = false;
                    return(result);
                }

                if (isGoodsTransfer)
                {
                    document.FormalizedServiceType = Docflow.AccountingDocumentBase.FormalizedServiceType.GoodsTransfer;
                }
                else if (isWorksTransfer)
                {
                    document.FormalizedServiceType = Docflow.AccountingDocumentBase.FormalizedServiceType.WorksTransfer;
                }
                else
                {
                    document.FormalizedServiceType = Docflow.AccountingDocumentBase.FormalizedServiceType.GeneralTransfer;
                }

                result.Body = sellerTitle.Body;

                var version = document.Versions.AddNew();
                version.AssociatedApplication     = Content.AssociatedApplications.GetByExtension("pdf");
                version.BodyAssociatedApplication = Content.AssociatedApplications.GetByExtension("xml");
                document.SellerTitleId            = version.Id;

                if (FinancialArchive.Waybills.Is(document) ||
                    FinancialArchive.ContractStatements.Is(document) ||
                    FinancialArchive.UniversalTransferDocuments.Is(document))
                {
                    version.Note = FinancialArchive.Resources.SellerTitleVersionNote;
                }

                document.IsFormalized = true;

                var number       = sellerTitle.IsAdjustment ? sellerTitle.CorrectionNumber : sellerTitle.Number;
                var date         = sellerTitle.IsAdjustment ? sellerTitle.CorrectionDate : sellerTitle.Date;
                var isRegistered = Docflow.PublicFunctions.OfficialDocument.TryExternalRegister(document, number, date);

                document.IsFormalizedSignatoryEmpty = document.IsFormalized == true && !FormalizeDocumentsParser.SellerSignatoryInfo.HasSellerSignatoryInfo(sellerTitle.Body);
                document.Subject = string.Empty;
                if (document.IsRevision == true)
                {
                    document.Note = string.Format(Exchange.Resources.TaxInvoiceRevision, sellerTitle.RevisionNumber, sellerTitle.RevisionDate.Value.Date.ToString("d"));
                }
                else if (isUniversalCorrection)
                {
                    document.Note = string.Format(Exchange.Resources.TaxInvoiceTo, sellerTitle.Number, sellerTitle.Date.Value.Date.ToString("d"));
                }

                if (!isRegistered || document.DocumentKind.NumberingType == Docflow.DocumentKind.NumberingType.NotNumerable)
                {
                    if (isUniversalTransfer)
                    {
                        document.Note = string.Format(Exchange.Resources.TaxInvoice, number, date.Value.Date.ToString("d")) +
                                        Environment.NewLine + document.Note;
                    }
                    else if (isUniversalCorrection)
                    {
                        document.Note = string.Format(Exchange.Resources.TaxInvoiceCorrection, number, date.Value.Date.ToString("d")) +
                                        Environment.NewLine + document.Note;
                    }
                    else
                    {
                        document.Note = string.Format(Exchange.Resources.IncomingNotNumeratedDocumentNote, date.Value.Date.ToString("d"), number) +
                                        Environment.NewLine + document.Note;
                    }
                }
                if (isUniversalCorrection && !string.IsNullOrEmpty(sellerTitle.Number) && sellerTitle.Date != null)
                {
                    if (sellerTitle.Function == FormalizeDocumentsParser.UniversalDocumentFunction.Schf)
                    {
                        document.Corrected = Sungero.FinancialArchive.OutgoingTaxInvoices.GetAll()
                                             .Where(x => x.RegistrationNumber == sellerTitle.Number &&
                                                    x.RegistrationDate == sellerTitle.Date.Value && Equals(x.Counterparty, document.Counterparty))
                                             .FirstOrDefault();
                    }
                    else
                    {
                        document.Corrected = Sungero.FinancialArchive.UniversalTransferDocuments.GetAll()
                                             .Where(x => x.RegistrationNumber == sellerTitle.Number &&
                                                    x.RegistrationDate == sellerTitle.Date.Value && Equals(x.Counterparty, document.Counterparty))
                                             .FirstOrDefault();
                    }
                }

                result.PublicBody = Docflow.PublicFunctions.Module.Remote.GeneratePublicBodyForFormalizedXml(Docflow.Structures.Module.ByteArray.Create(result.Body)).Bytes;
                return(result);
            }
            result.Error     = Resources.ImportDialog_CannotRecognizeDocument;
            result.IsSuccess = false;
            return(result);
        }