0.4.4 • Published 8 months ago
@aptly-as/sdk-algorithm v0.4.4
Aptly Ecommerce SDK
Example
import { AptlyAlgorithm, AptlyOption } from '@aptly-as/types';
import { AlgorithmCalculation } from '@aptly-as/sdk-algorithm';
const option: AptlyOption = { ... };
const optionAlgorithm: AptlyAlgorithm = { ..., vat: 25, fee: 10 };
// Setup algorithm with algorithm and amount.
// option.algorithm must be populated
const algorithm = new AlgorithmCalculation(optionAlgorithm)
// Add amount calculate algorithm pipeline and fee.
algorithm.amount(option.amount)
// Check if option is on sale.
if (option.allowace) {
algorithm.priceAllowance(option.allowance);
}
// Add 1 quantity
option.quantity(1, /* can add baseQuantity here */);
// Adds the allowance and charges if set
if (option.allowanceCharge) {
algorithm.allowanceCharge(option.allowanceCharge);
}
// We can now finalize it and use data we want
const {
extensionAmount,
extensionVatAmount,
invoicedQuantity,
item,
price,
} = algorithm.finalize();
// if we want to se how these calculations are done, we can fetch the pipeline.
// This is done in reverse order so we may have a floating error.
const pipeline = algorithm.pipeline(); // VAT 25% -> FEE 10% -> NETTO ..
;