Class TaxDocument
- Namespace
- Integrations.Domain.Tax
- Assembly
- Integrations.Domain.dll
A tax document (AR invoice/credit or AP bill/credit) as recorded by this service, mirrored in the tax provider. Identified by the caller's natural key (SourceSystem, SourceDocumentId) so retries are idempotent.
public sealed class TaxDocument
- Inheritance
-
TaxDocument
- Inherited Members
- Extension Methods
Constructors
TaxDocument(string, TaxDirection, TaxDocumentKind, string, string, DateOnly, string, string, DateTimeOffset)
Starts a document as PendingTax. Its provider document code comes from the caller's natural key (ProviderCodeFor(string, string, string, TaxDirection, TaxDocumentKind)), so every attempt for the same source document addresses the same provider document, including a redelivered message whose first attempt rolled back after the provider was called.
public TaxDocument(string entityId, TaxDirection direction, TaxDocumentKind kind, string sourceSystem, string sourceDocumentId, DateOnly documentDate, string currencyCode, string functionalCurrency, DateTimeOffset now)
Parameters
entityIdstringdirectionTaxDirectionkindTaxDocumentKindsourceSystemstringsourceDocumentIdstringdocumentDateDateOnlycurrencyCodestringfunctionalCurrencystringnowDateTimeOffset
Properties
CalculatedAt
When tax was last calculated.
public DateTimeOffset? CalculatedAt { get; }
Property Value
CalculationAttempts
Calculations tried so far (drives the retry back-off).
public int CalculationAttempts { get; }
Property Value
CommitRequested
Commit as soon as a calculation succeeds (set while PendingTax).
public bool CommitRequested { get; set; }
Property Value
CommittedAt
When it was committed.
public DateTimeOffset? CommittedAt { get; }
Property Value
ContentHash
SHA-256 of the normalized request; detects idempotent replays vs changed content.
public string ContentHash { get; set; }
Property Value
CounterpartyCode
Customer code (AR) or vendor code (AP).
public string CounterpartyCode { get; set; }
Property Value
CounterpartyName
Customer or vendor name, for reports.
public string? CounterpartyName { get; set; }
Property Value
CreatedAt
When it was first received.
public DateTimeOffset CreatedAt { get; }
Property Value
CreditVersion
Bumped by every credit created or replaced against this invoice. The write changes the invoice's row version, so two credits racing for the same invoice conflict instead of both passing the remaining-amount check.
public int CreditVersion { get; }
Property Value
CurrencyCode
Transaction currency.
public string CurrencyCode { get; }
Property Value
Direction
Receivable (AR) or payable (AP).
public TaxDirection Direction { get; }
Property Value
DocumentDate
Document date.
public DateOnly DocumentDate { get; set; }
Property Value
EntityId
Owning entity.
public string EntityId { get; }
Property Value
EntityUseCode
AvaTax entity use code (e.g. direct pay, non-profit), when the whole document is exempt for that reason.
public string? EntityUseCode { get; set; }
Property Value
ExchangeRate
Locked rate from transaction to functional currency.
public decimal? ExchangeRate { get; set; }
Property Value
ExemptionNumber
Exemption certificate number, when the caller already knows it.
public string? ExemptionNumber { get; set; }
Property Value
FunctionalCurrency
Entity base currency at the time of the document.
public string FunctionalCurrency { get; }
Property Value
FxLockId
FX lock used to convert transaction → functional currency. Null when currencies match.
public Guid? FxLockId { get; set; }
Property Value
- Guid?
Id
Document id (UUID v7).
public Guid Id { get; }
Property Value
InvoiceNumber
Invoice, bill or credit memo number.
public string? InvoiceNumber { get; set; }
Property Value
IsMutable
Whether content can still change (never once committed or voided).
public bool IsMutable { get; }
Property Value
Kind
Invoice or credit.
public TaxDocumentKind Kind { get; }
Property Value
Lines
Document lines in line-number order.
public IReadOnlyList<TaxDocumentLine> Lines { get; }
Property Value
NextAttemptAt
When the recalculation job should retry a PendingTax document.
public DateTimeOffset? NextAttemptAt { get; }
Property Value
OriginalDocumentId
For credits: the invoice being credited.
public Guid? OriginalDocumentId { get; set; }
Property Value
- Guid?
Provider
Tax engine that calculated it.
public string? Provider { get; }
Property Value
ProviderDocumentCode
Document code in the provider (AvaTax code). Stable across recalculations.
public string ProviderDocumentCode { get; }
Property Value
ProviderResponse
Raw provider response (jsonb) kept for audit look-back.
public string? ProviderResponse { get; }
Property Value
ProviderTransactionId
Provider's transaction id from the last calculation.
public long? ProviderTransactionId { get; }
Property Value
- long?
PurchaseOrderNumber
PO reference.
public string? PurchaseOrderNumber { get; set; }
Property Value
ShipFrom
Origin (ship-from). Vendor or PVC office. Lines carry the job site as ship-to.
public Address? ShipFrom { get; set; }
Property Value
SourceDocumentId
Caller's document id (invoice id, bill id, credit memo id).
public string SourceDocumentId { get; }
Property Value
SourceSystem
Calling system, e.g. legacy-connect, workorders, ap.
public string SourceSystem { get; }
Property Value
Status
Lifecycle status.
public TaxDocumentStatus Status { get; }
Property Value
StatusReason
Why it is PendingTax or Failed.
public string? StatusReason { get; }
Property Value
TaxDate
Date whose tax rules/rates apply when it differs from DocumentDate. Credits use the original invoice date so the reversal matches what was charged.
public DateOnly? TaxDate { get; set; }
Property Value
TotalAmount
Sum of line amounts.
public decimal TotalAmount { get; }
Property Value
TotalExempt
Sum of exempt amounts.
public decimal TotalExempt { get; }
Property Value
TotalTax
Total tax.
public decimal TotalTax { get; }
Property Value
TotalTaxable
Sum of taxable amounts.
public decimal TotalTaxable { get; }
Property Value
UpdatedAt
When it last changed.
public DateTimeOffset UpdatedAt { get; }
Property Value
VendorChargedTax
AP only: tax the vendor charged on the bill, so the provider can compute under/over-charged use tax.
public decimal? VendorChargedTax { get; set; }
Property Value
VoidedAt
When it was voided.
public DateTimeOffset? VoidedAt { get; }
Property Value
WorkOrderNumber
Work order reference.
public string? WorkOrderNumber { get; set; }
Property Value
Methods
ApplyCalculation(string, long?, bool, IReadOnlyList<LineTaxResult>, string?, DateTimeOffset)
Applies a successful provider calculation. Line results are matched by line number.
public void ApplyCalculation(string provider, long? providerTransactionId, bool committed, IReadOnlyList<LineTaxResult> lineResults, string? providerResponse, DateTimeOffset now)
Parameters
providerstringproviderTransactionIdlong?committedboollineResultsIReadOnlyList<LineTaxResult>providerResponsestringnowDateTimeOffset
BeginCalculation(DateTimeOffset)
Content is about to be (re)calculated. The document is PendingTax until the provider answers; the retry time covers a crash mid-call, so the recalculation job picks it up rather than leaving it stuck.
public void BeginCalculation(DateTimeOffset now)
Parameters
nowDateTimeOffset
MarkCommitted(DateTimeOffset)
Commits a saved document. Throws unless the document is Saved.
public void MarkCommitted(DateTimeOffset now)
Parameters
nowDateTimeOffset
MarkFailed(string, DateTimeOffset)
The provider rejected the document; it waits for a corrected request.
public void MarkFailed(string reason, DateTimeOffset now)
Parameters
reasonstringnowDateTimeOffset
MarkPending(string, DateTimeOffset)
Provider unavailable: keep the document and retry with exponential backoff (capped at 1 hour).
public void MarkPending(string reason, DateTimeOffset now)
Parameters
reasonstringnowDateTimeOffset
MarkVoided(string, DateTimeOffset)
Voids the document. Idempotent.
public void MarkVoided(string reason, DateTimeOffset now)
Parameters
reasonstringnowDateTimeOffset
ProviderCodeFor(string, string, string, TaxDirection, TaxDocumentKind)
The provider document code for a natural key: 32 hex characters of SHA-256 over the key. Deterministic, so a retry can never create a second provider document; unique because the natural key is unique.
public static string ProviderCodeFor(string entityId, string sourceSystem, string sourceDocumentId, TaxDirection direction, TaxDocumentKind kind)
Parameters
entityIdstringOwning entity.
sourceSystemstringCalling system.
sourceDocumentIdstringCaller's document id.
directionTaxDirectionReceivable or payable.
kindTaxDocumentKindInvoice or credit.
Returns
- string
The code to send as the provider's document code.
RecordCredit(DateTimeOffset)
Records that a credit against this invoice is being written (see CreditVersion).
public void RecordCredit(DateTimeOffset now)
Parameters
nowDateTimeOffsetCurrent time.
ReplaceLines(IEnumerable<TaxDocumentLine>, DateTimeOffset)
Replaces every line (a resend with changed content). Throws once the document is immutable.
public void ReplaceLines(IEnumerable<TaxDocumentLine> lines, DateTimeOffset now)
Parameters
linesIEnumerable<TaxDocumentLine>nowDateTimeOffset