Table of Contents

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

entityId string
direction TaxDirection
kind TaxDocumentKind
sourceSystem string
sourceDocumentId string
documentDate DateOnly
currencyCode string
functionalCurrency string
now DateTimeOffset

Properties

CalculatedAt

When tax was last calculated.

public DateTimeOffset? CalculatedAt { get; }

Property Value

DateTimeOffset?

CalculationAttempts

Calculations tried so far (drives the retry back-off).

public int CalculationAttempts { get; }

Property Value

int

CommitRequested

Commit as soon as a calculation succeeds (set while PendingTax).

public bool CommitRequested { get; set; }

Property Value

bool

CommittedAt

When it was committed.

public DateTimeOffset? CommittedAt { get; }

Property Value

DateTimeOffset?

ContentHash

SHA-256 of the normalized request; detects idempotent replays vs changed content.

public string ContentHash { get; set; }

Property Value

string

CounterpartyCode

Customer code (AR) or vendor code (AP).

public string CounterpartyCode { get; set; }

Property Value

string

CounterpartyName

Customer or vendor name, for reports.

public string? CounterpartyName { get; set; }

Property Value

string

CreatedAt

When it was first received.

public DateTimeOffset CreatedAt { get; }

Property Value

DateTimeOffset

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

int

CurrencyCode

Transaction currency.

public string CurrencyCode { get; }

Property Value

string

Direction

Receivable (AR) or payable (AP).

public TaxDirection Direction { get; }

Property Value

TaxDirection

DocumentDate

Document date.

public DateOnly DocumentDate { get; set; }

Property Value

DateOnly

EntityId

Owning entity.

public string EntityId { get; }

Property Value

string

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

string

ExchangeRate

Locked rate from transaction to functional currency.

public decimal? ExchangeRate { get; set; }

Property Value

decimal?

ExemptionNumber

Exemption certificate number, when the caller already knows it.

public string? ExemptionNumber { get; set; }

Property Value

string

FunctionalCurrency

Entity base currency at the time of the document.

public string FunctionalCurrency { get; }

Property Value

string

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

Guid

InvoiceNumber

Invoice, bill or credit memo number.

public string? InvoiceNumber { get; set; }

Property Value

string

IsMutable

Whether content can still change (never once committed or voided).

public bool IsMutable { get; }

Property Value

bool

Kind

Invoice or credit.

public TaxDocumentKind Kind { get; }

Property Value

TaxDocumentKind

Lines

Document lines in line-number order.

public IReadOnlyList<TaxDocumentLine> Lines { get; }

Property Value

IReadOnlyList<TaxDocumentLine>

NextAttemptAt

When the recalculation job should retry a PendingTax document.

public DateTimeOffset? NextAttemptAt { get; }

Property Value

DateTimeOffset?

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

string

ProviderDocumentCode

Document code in the provider (AvaTax code). Stable across recalculations.

public string ProviderDocumentCode { get; }

Property Value

string

ProviderResponse

Raw provider response (jsonb) kept for audit look-back.

public string? ProviderResponse { get; }

Property Value

string

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

string

ShipFrom

Origin (ship-from). Vendor or PVC office. Lines carry the job site as ship-to.

public Address? ShipFrom { get; set; }

Property Value

Address

SourceDocumentId

Caller's document id (invoice id, bill id, credit memo id).

public string SourceDocumentId { get; }

Property Value

string

SourceSystem

Calling system, e.g. legacy-connect, workorders, ap.

public string SourceSystem { get; }

Property Value

string

Status

Lifecycle status.

public TaxDocumentStatus Status { get; }

Property Value

TaxDocumentStatus

StatusReason

Why it is PendingTax or Failed.

public string? StatusReason { get; }

Property Value

string

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

DateOnly?

TotalAmount

Sum of line amounts.

public decimal TotalAmount { get; }

Property Value

decimal

TotalExempt

Sum of exempt amounts.

public decimal TotalExempt { get; }

Property Value

decimal

TotalTax

Total tax.

public decimal TotalTax { get; }

Property Value

decimal

TotalTaxable

Sum of taxable amounts.

public decimal TotalTaxable { get; }

Property Value

decimal

UpdatedAt

When it last changed.

public DateTimeOffset UpdatedAt { get; }

Property Value

DateTimeOffset

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

decimal?

VoidedAt

When it was voided.

public DateTimeOffset? VoidedAt { get; }

Property Value

DateTimeOffset?

WorkOrderNumber

Work order reference.

public string? WorkOrderNumber { get; set; }

Property Value

string

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

provider string
providerTransactionId long?
committed bool
lineResults IReadOnlyList<LineTaxResult>
providerResponse string
now DateTimeOffset

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

now DateTimeOffset

MarkCommitted(DateTimeOffset)

Commits a saved document. Throws unless the document is Saved.

public void MarkCommitted(DateTimeOffset now)

Parameters

now DateTimeOffset

MarkFailed(string, DateTimeOffset)

The provider rejected the document; it waits for a corrected request.

public void MarkFailed(string reason, DateTimeOffset now)

Parameters

reason string
now DateTimeOffset

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

reason string
now DateTimeOffset

MarkVoided(string, DateTimeOffset)

Voids the document. Idempotent.

public void MarkVoided(string reason, DateTimeOffset now)

Parameters

reason string
now DateTimeOffset

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

entityId string

Owning entity.

sourceSystem string

Calling system.

sourceDocumentId string

Caller's document id.

direction TaxDirection

Receivable or payable.

kind TaxDocumentKind

Invoice 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

now DateTimeOffset

Current 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

lines IEnumerable<TaxDocumentLine>
now DateTimeOffset