getSalesTaxRate function

SalesTaxRate? getSalesTaxRate({
  1. required SalesTaxKind kind,
  2. String? province,
  3. Object? date,
})

Returns the standard sales tax rate for goods (federal, Sales Tax Act 1990) or for services in a province or Islamabad Capital Territory, on a given date.

province accepts the province names and KP, KPK, ICT and Islamabad; for goods it may be left out. date is a YYYY-MM-DD String or a DateTime (its local calendar date), and defaults to today.

Only standard rates are covered, from 1 July 2019. Reduced, higher and sector rates are not.

Returns null for an unknown province, services without a province, goods in Azad Jammu and Kashmir or Gilgit-Baltistan, an invalid date, or a date before 1 July 2019. Never throws.

getSalesTaxRate(kind: SalesTaxKind.goods, date: '2026-11-18')?.rate; // 18
getSalesTaxRate(
    kind: SalesTaxKind.services, province: 'Sindh', date: '2024-06-30')
  ?.rate; // 13

Implementation

SalesTaxRate? getSalesTaxRate({
  required SalesTaxKind kind,
  String? province,
  Object? date,
}) {
  final iso = isoDate(date ?? DateTime.now());
  if (iso == null) return null;

  String? name;
  if (province != null) {
    name = _provinces[normalizeKey(province)];
    if (name == null) return null;
  }
  if (kind == SalesTaxKind.goods) {
    if (name != null && _outsideFederal.contains(name)) return null;
  } else if (name == null) {
    return null;
  }

  for (final r in _rates) {
    if (r.kind == kind &&
        (kind == SalesTaxKind.goods || r.province == name) &&
        r.from.compareTo(iso) <= 0 &&
        (r.to == null || iso.compareTo(r.to!) <= 0)) {
      return r;
    }
  }
  return null;
}