/// <summary> /// Creates new <see cref="Payment"/> according to the document's defaults and attaches it to the parent <see cref="Document"/>. /// </summary> /// <returns>A new <see cref="Payment"/>.</returns> public override Payment CreateNew() { //create new Payment object and attach it to the element Document parent = (Document)this.Parent; Payment payment = new Payment(parent); payment.Order = this.Children.Count + 1; DocumentCategory dc = parent.DocumentType.DocumentCategory; CommercialDocument commercialDocument = this.Parent as CommercialDocument; FinancialDocument financialDocument = this.Parent as FinancialDocument; if (dc == DocumentCategory.Sales || dc == DocumentCategory.SalesCorrection) { payment.Direction = -1; } else if (dc == DocumentCategory.Purchase || dc == DocumentCategory.PurchaseCorrection) { payment.Direction = 1; } else if (dc == DocumentCategory.Financial) { FinancialDirection fdc = parent.DocumentType.FinancialDocumentOptions.FinancialDirection; if (fdc == FinancialDirection.Income) { payment.Direction = 1; } else { payment.Direction = -1; } } //Copy payment contractor from source if exists if (this.PaymentContainingSourceDocument != null && this.PaymentContainingSourceDocument.Payments != null && this.PaymentContainingSourceDocument.Payments.Children.Count >= payment.Order) { Payment sourcePayment = this.PaymentContainingSourceDocument.Payments[payment.Order - 1]; payment.Contractor = sourcePayment.Contractor; payment.ContractorAddressId = sourcePayment.ContractorAddressId; } else if (commercialDocument != null) { payment.Contractor = commercialDocument.Contractor; payment.ContractorAddressId = commercialDocument.ContractorAddressId; } else if (financialDocument != null) { payment.Contractor = financialDocument.Contractor; payment.ContractorAddressId = financialDocument.ContractorAddressId; } //add the attribute to the collection this.Children.Add(payment); return(payment); }
private static void GenerateFinancialDocumentToPayment(Payment payment, CommercialDocument document) { DocumentMapper mapper = DependencyContainerManager.Container.Get <DocumentMapper>(); using (DocumentCoordinator coordinator = new DocumentCoordinator(false, false)) { PaymentMethod paymentMethod = DictionaryMapper.Instance.GetPaymentMethod(payment.PaymentMethodId.Value); Guid branchId = SessionManager.User.BranchId; if (DictionaryMapper.Instance.IsPaymentMethodSupportedByRegister(paymentMethod.Id.Value, branchId)) { FinancialRegister register = DictionaryMapper.Instance.GetFinancialRegisterForSpecifiedPaymentMethod(paymentMethod.Id.Value, branchId, payment.PaymentCurrencyId); if (register == null) { throw new ClientException(ClientExceptionId.UnableToIssueFinancialDocument3); } FinancialDirection direction = FinancialDocumentFactory.GetFinancialDirectionForPayment(document, payment); Guid?reportId = mapper.GetOpenedFinancialReportId(register.Id.Value); if (reportId == null) { throw new ClientException(ClientExceptionId.UnableToIssueDocumentToClosedFinancialReport); } FinancialDocument financialDoc = new FinancialDocument(); financialDoc.RelatedCommercialDocument = document; coordinator.TrySaveProfileIdAttribute(financialDoc); if (direction == FinancialDirection.Income) { financialDoc.DocumentTypeId = register.IncomeDocumentTypeId; financialDoc.Number.NumberSettingId = register.IncomeNumberSettingId; } else { financialDoc.DocumentTypeId = register.OutcomeDocumentTypeId; financialDoc.Number.NumberSettingId = register.OutcomeNumberSettingId; } financialDoc.Contractor = payment.Contractor; financialDoc.ContractorAddressId = payment.ContractorAddressId; FinancialReport report = new FinancialReport(); report.Id = reportId; report.FinancialRegisterId = register.Id.Value; financialDoc.FinancialReport = report; financialDoc.DocumentStatus = DocumentStatus.Committed; DuplicableAttributeFactory.DuplicateAttributes(document, financialDoc); Payment pt = financialDoc.Payments.CreateNew(); pt.Amount = Math.Abs(payment.Amount); pt.ExchangeRate = financialDoc.ExchangeRate = document.ExchangeRate; pt.ExchangeScale = financialDoc.ExchangeScale = document.ExchangeScale; pt.ExchangeDate = financialDoc.ExchangeDate = document.ExchangeDate; financialDoc.Amount = pt.Amount; financialDoc.DocumentCurrencyId = payment.PaymentCurrencyId; PaymentSettlement settlement = pt.Settlements.CreateNew(); settlement.IsAutoGenerated = true; settlement.RelatedPayment = payment; settlement.Amount = pt.Amount; document.AddRelatedObject(financialDoc); } } }