Skip to content

Instantly share code, notes, and snippets.

@hotdang-ca
Created June 20, 2022 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotdang-ca/09488e7ee22821fdb2bb41f77ea7dac2 to your computer and use it in GitHub Desktop.
Save hotdang-ca/09488e7ee22821fdb2bb41f77ea7dac2 to your computer and use it in GitHub Desktop.
fun with tax rates
class ProfitekItem {
late final bool taxable1;
late final bool taxable2;
late final bool taxable3;
late final bool taxable4;
late final num? taxRate1;
late final num? taxRate2;
late final num? taxRate3;
late final num? taxRate4;
late final String productName;
late final num price;
ProfitekItem({
required this.taxable1,
required this.taxable2,
required this.taxable3,
required this.taxable4,
this.taxRate1,
this.taxRate2,
this.taxRate3,
this.taxRate4,
required this.productName,
required this.price,
});
}
void main() {
ProfitekItem item1 = ProfitekItem(
taxable1: true,
taxable2: true,
taxable3: false,
taxable4: false,
taxRate1: 0.05,
taxRate2: 0.07,
taxRate3: null,
taxRate4: null,
productName: 'Weed',
price: 10.00,
);
ProfitekItem item2 = ProfitekItem(
taxable1: true,
taxable2: false,
taxable3: false,
taxable4: false,
taxRate1: 0.05,
taxRate2: null,
taxRate3: null,
taxRate4: null,
productName: 'Vape',
price: 10.00,
);
List<ProfitekItem> cart = [item1, item2];
num? tax1Rate; // almost always GST
num tax1Amount = 0;
num? tax2Rate; // almost always PST
num tax2Amount = 0;
num? tax3Rate; // almost always HST
num tax3Amount = 0;
num? tax4Rate;
num tax4Amount = 0;
// for taxRate1
cart.forEach((cartItem) {
if (cartItem.taxable1) {
tax1Rate = cartItem.taxRate1;
tax1Amount += cartItem.price;
}
if (cartItem.taxable2) {
tax2Rate = cartItem.taxRate2;
tax2Amount += cartItem.price;
}
if (cartItem.taxable3) {
tax3Rate = cartItem.taxRate3;
tax3Amount += cartItem.price;
}
if (cartItem.taxable4) {
tax4Rate = cartItem.taxRate4;
tax4Amount += cartItem.price;
}
});
// assertions
final expectedTax1PreTaxTotal = 20;
final expectedTax1Amount = 1;
final expectedTax2PreTaxTotal = 10;
final expectedTax2Amount = 0.70;
final double? calculatedTax1Total = double.tryParse(tax1Amount.toString());
final double? calculatedTax2Total = double.tryParse(tax2Amount.toString());
if (expectedTax1PreTaxTotal != calculatedTax1Total ) {
print('Pretax: It does not match [$expectedTax1PreTaxTotal] [$calculatedTax1Total]');
}
if (expectedTax2PreTaxTotal != calculatedTax2Total ) {
print('Pretax: It does not match [$expectedTax2PreTaxTotal] [$calculatedTax2Total]');
}
final calculatedTax1 = double.tryParse((tax1Amount * (tax1Rate ?? 0)).toStringAsFixed(2));
final calculatedTax2 = double.tryParse((tax2Amount * (tax2Rate ?? 0)).toStringAsFixed(2));
if (expectedTax1Amount != calculatedTax1) {
print('Tax1: It does not match: [$expectedTax1Amount] [$calculatedTax1]');
}
if (expectedTax2Amount != calculatedTax2) {
print('Tax2: It does not match [$expectedTax2Amount] [$calculatedTax2]');
}
final expectedTotal = 20.0
+ expectedTax1Amount
+ expectedTax2Amount;
final calculatedTotal = 20.0
+ (calculatedTax1 ?? 0)
+ (calculatedTax2 ?? 0);
if (expectedTotal != calculatedTotal) {
print('TOTAL: It does not match [$expectedTotal] [$calculatedTotal]');
}
print('Check any messages above.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment