Skip to content

API reference / Quittio \ Core \ Corrections

CancellationHistory

final readonly class in quittio/core

Quittio\Core\Corrections\CancellationHistory · Source

What the earlier partial cancellations of an invoice cancelled per VAT group (category and rate), so that the VAT of the next partial cancellation is rounded cumulatively.

Price reductions (PriceReduction) credit part of the invoice as well and share the history with its partial cancellations: one history per original holds what its partial cancellations and price reductions credited, less what the cancellations of those reversed (withCancellation(), withPriceReduction(), fromDocuments()). Cancellation::of() and PriceReduction::of() take it, so that a price reduction after a partial cancellation is aligned with it, and vice versa. A price reduction has no gross amount: a group with price reductions keeps the taxable amounts of its gross price cancellations (their VAT is still aligned).

VAT is rounded per document and VAT group (BR-CO-17). Rounded per partial cancellation on its own, the cancellations together can differ from the original by a cent per earlier part (e.g. 9.67 + 9.67 + 9.66 net at 19 %: 3 × 1.84 = 5.52 against 5.51). With a history, per group:

S = taxable amount of the earlier cancellations + taxable amount of this one target = round2(S × rate / 100), half up like the calculator; the original's VAT (BT-117) when this cancellation closes the group (all its lines are then cancelled completely) VAT = target − VAT of the earlier cancellations

After every cancellation the VAT cancelled so far is that of the taxable amount cancelled so far, and once all lines are cancelled exactly the original's VAT (1.84, 1.83, 1.84 = 5.51 in the example). For an original whose VAT another system rounded (BT-117 ≠ round2(BT-116 × rate)), the target is round2(S × BT-117 / BT-116): its rounding difference is spread over the parts in proportion to their taxable amounts instead of falling on the part that closes the group.

In gross price mode the taxable amount follows the same way from the gross amount G of the lines: N = round2(G cancelled so far / (1 + rate / 100)) as for the original (GrossPriceConverter), the original's BT-116 when the group closes, minus the taxable amount already cancelled. The taxable amounts of all cancellations then add up exactly to the original's, and so do the rounding amounts (BT-114).

Every cancellation therefore differs from round2(own taxable amount × rate) (or × the original's effective rate) by at most 0.01, which BR-CO-17 tolerates, and its VAT keeps the sign of its taxable amount (0.00 on 0.00); assertFits() rejects a history for which this would not hold. The VAT total in accounting currency (BT-111) follows the same way: round2(VAT cancelled so far × exchange rate), and the original's BT-111 when the cancellation closes all groups.

Amounts are signed in the original's direction: a credit note that cancels an invoice and the invoice that cancels a credit note count with their amounts, a negative invoice (strategy negative_invoice) with its amounts negated.

Properties#

PropertyTypeDescription
$closes readonlylist<string>keys of the VAT groups whose lines are all cancelled completely once this cancellation is issued, including groups closed earlier
$groups readonlyarray<string, array{taxable: Brick\Math\BigDecimal, tax: Brick\Math\BigDecimal, taxInTaxCurrency: ?Brick\Math\BigDecimal, Quittio\Core\Corrections\gross?: ?Brick\Math\BigDecimal}>per VAT group (key: DeclaredAmounts::breakdownKey()) the taxable amount (BT-116) and VAT (BT-117) of the earlier cancellations together, their share of BT-111 (null if unknown) and, in gross price mode, the gross amount of their lines (Σ quantity × price including VAT; null or missing if unknown), in the original's direction

Methods#

MethodDescription
->__construct()
->align()The VAT of a calculated partial cancellation of $original aligned with this history: per VAT group the target minus the VAT already cancelled, and BT-111 the same way. In gross price mode the taxable amount of a group is aligned as well (the net amount of the gross amount cancelled so far minus the taxable amount already cancelled), unless that differs by more than 0.01 from the net amount of the cancellation's own gross amount (a history that does not match): then it keeps its own. Otherwise its taxable amounts stay as they are. Amounts are signed like the cancellation.
->assertFits()Rejects a history that does not fit $original, checked with the calculated partial cancellation $cancellation and its aligned VAT breakdown $aligned (align()):
->closes()Whether the VAT group $key (DeclaredAmounts::breakdownKey()) is closed by the next cancellation, so that its VAT becomes the original's.
::entries()The VAT groups of an issued (calculated) cancellation of $original in the original's direction, with their share of BT-111 (distributed in proportion to their VAT; null without BT-111) and, in gross price mode, the gross amount of their lines (null otherwise).
::fromDocuments()The history of the issued (calculated) documents that credited $original, in the order they were issued: its partial cancellations, its price reductions and the cancellations of those (which count negatively); $closes as in the constructor, for the next document.
->gross()Gross amount of the earlier cancellations of a group in gross price mode; null if unknown.
->matches()Whether an issued cancellation of $original carries the VAT this history gives it; false e.g. after another partial cancellation was issued meanwhile.
->tax()VAT (BT-117) of the earlier cancellations of the VAT group $key together; zero for an unknown group.
->taxable()Taxable amount (BT-116) of the earlier cancellations of the VAT group $key together; zero for an unknown group.
->taxInTaxCurrency()BT-111 of the earlier cancellations together; null if a share is unknown.
->withCancellation()The history after an issued (calculated) cancellation of $original, e.g. to keep it without the Managed layer; $closes as in the constructor, for the next cancellation. The cancellation of a price reduction of $original (an invoice that reverses its credit note) counts negatively.
->withPriceReduction()The history after an issued (calculated) price reduction of $original (PriceReduction), for the next partial cancellation or price reduction; the closed groups stay. PriceReduction::of() closes a group itself once the net amount credited so far reaches the original's taxable amount.

__construct()#

public function __construct(
    array<string, array{taxable: BigDecimal, tax: BigDecimal, taxInTaxCurrency: ?BigDecimal, gross?: ?BigDecimal}> $groups = [],
    list<string> $closes = [],
)
ParameterTypeDescription
$groupsarray<string, array{taxable: Brick\Math\BigDecimal, tax: Brick\Math\BigDecimal, taxInTaxCurrency: ?Brick\Math\BigDecimal, Quittio\Core\Corrections\gross?: ?Brick\Math\BigDecimal}> = []per VAT group (key: DeclaredAmounts::breakdownKey()) the taxable amount (BT-116) and VAT (BT-117) of the earlier cancellations together, their share of BT-111 (null if unknown) and, in gross price mode, the gross amount of their lines (Σ quantity × price including VAT; null or missing if unknown), in the original's direction
$closeslist<string> = []keys of the VAT groups whose lines are all cancelled completely once this cancellation is issued, including groups closed earlier

align()#

public function align(
    InvoiceData $original,
    InvoiceData $cancellation,
): array{breakdown: list<VatBreakdown>, taxInTaxCurrency: ?BigDecimal}

The VAT of a calculated partial cancellation of $original aligned with this history: per VAT group the target minus the VAT already cancelled, and BT-111 the same way. In gross price mode the taxable amount of a group is aligned as well (the net amount of the gross amount cancelled so far minus the taxable amount already cancelled), unless that differs by more than 0.01 from the net amount of the cancellation's own gross amount (a history that does not match): then it keeps its own. Otherwise its taxable amounts stay as they are. Amounts are signed like the cancellation.

ParameterTypeDescription
$originalInvoiceData
$cancellationInvoiceData
Returns
array{breakdown: list<VatBreakdown>, taxInTaxCurrency: ?Brick\Math\BigDecimal} – taxInTaxCurrency is null without VAT accounting currency (BT-6), exchange rate or known BT-111 of the earlier cancellations

assertFits()#

public function assertFits(
    InvoiceData $original,
    InvoiceData $cancellation,
    list<VatBreakdown> $aligned,
): void

Rejects a history that does not fit $original, checked with the calculated partial cancellation $cancellation and its aligned VAT breakdown $aligned (align()):

  • every VAT group of the history and every group it closes must exist in the original,
  • the taxable amount cancelled so far (before and including this cancellation) must not exceed per group the sum of the magnitudes of the original's line net amounts,
  • every aligned VAT amount must differ by at most 0.01 from round2(own taxable amount × rate) (for an original whose VAT another system rounded, × its effective rate BT-117 / BT-116) and keep the sign of its taxable amount (0.00 on a taxable amount of 0.00).

A history built from the earlier cancellations as issued (withCancellation(), the Managed ledger) always fits; after cancelled price reductions the VAT credited so far can be a cent off the cumulative rounding, so that a price reduction may not fit.

ParameterTypeDescription
$originalInvoiceData
$cancellationInvoiceData
$alignedlist<VatBreakdown>
Throws
InvalidArgumentException

closes()#

public function closes(string $key): bool

Whether the VAT group $key (DeclaredAmounts::breakdownKey()) is closed by the next cancellation, so that its VAT becomes the original's.

ParameterTypeDescription
$keystring
Returns
bool

entries()#

public static function entries(
    InvoiceData $original,
    InvoiceData $cancellation,
): list<array{key: string, category: VatCategory, rate: ?BigDecimal, taxable: BigDecimal, tax: BigDecimal, taxInTaxCurrency: ?BigDecimal, gross: ?BigDecimal}>

static

The VAT groups of an issued (calculated) cancellation of $original in the original's direction, with their share of BT-111 (distributed in proportion to their VAT; null without BT-111) and, in gross price mode, the gross amount of their lines (null otherwise).

ParameterTypeDescription
$originalInvoiceData
$cancellationInvoiceData
Returns
list<array{key: string, category: VatCategory, rate: ?Brick\Math\BigDecimal, taxable: Brick\Math\BigDecimal, tax: Brick\Math\BigDecimal, taxInTaxCurrency: ?Brick\Math\BigDecimal, gross: ?Brick\Math\BigDecimal}>

fromDocuments()#

public static function fromDocuments(
    InvoiceData $original,
    iterable<InvoiceData> $documents,
    list<string> $closes = [],
): self

static

The history of the issued (calculated) documents that credited $original, in the order they were issued: its partial cancellations, its price reductions and the cancellations of those (which count negatively); $closes as in the constructor, for the next document.

ParameterTypeDescription
$originalInvoiceData
$documentsiterable<InvoiceData>
$closeslist<string> = []
Returns
self

gross()#

public function gross(string $key): ?BigDecimal

Gross amount of the earlier cancellations of a group in gross price mode; null if unknown.

ParameterTypeDescription
$keystring
Returns
?Brick\Math\BigDecimal

matches()#

public function matches(InvoiceData $original, InvoiceData $cancellation): bool

Whether an issued cancellation of $original carries the VAT this history gives it; false e.g. after another partial cancellation was issued meanwhile.

ParameterTypeDescription
$originalInvoiceData
$cancellationInvoiceData
Returns
bool

tax()#

public function tax(string $key): BigDecimal

VAT (BT-117) of the earlier cancellations of the VAT group $key together; zero for an unknown group.

ParameterTypeDescription
$keystring
Returns
Brick\Math\BigDecimal

taxable()#

public function taxable(string $key): BigDecimal

Taxable amount (BT-116) of the earlier cancellations of the VAT group $key together; zero for an unknown group.

ParameterTypeDescription
$keystring
Returns
Brick\Math\BigDecimal

taxInTaxCurrency()#

public function taxInTaxCurrency(): ?BigDecimal

BT-111 of the earlier cancellations together; null if a share is unknown.

Returns
?Brick\Math\BigDecimal

withCancellation()#

public function withCancellation(
    InvoiceData $original,
    InvoiceData $cancellation,
    list<string> $closes = [],
): self

The history after an issued (calculated) cancellation of $original, e.g. to keep it without the Managed layer; $closes as in the constructor, for the next cancellation. The cancellation of a price reduction of $original (an invoice that reverses its credit note) counts negatively.

ParameterTypeDescription
$originalInvoiceData
$cancellationInvoiceData
$closeslist<string> = []
Returns
self

withPriceReduction()#

public function withPriceReduction(InvoiceData $original, InvoiceData $reduction): self

The history after an issued (calculated) price reduction of $original (PriceReduction), for the next partial cancellation or price reduction; the closed groups stay. PriceReduction::of() closes a group itself once the net amount credited so far reaches the original's taxable amount.

ParameterTypeDescription
$originalInvoiceData
$reductionInvoiceData
Returns
self

The open core packages are MIT licensed. The legal wording of the country packs must be confirmed by a tax adviser.