Build promos for real: engine, storage, editor, and application at the till
The Promo module was a mockup. Three hardcoded rows, a toggle that changed nothing, and no promo code anywhere in lib/domain or lib/data. A cashier looking at it would reasonably conclude promotions were running. Engine (domain/services/promo_engine.dart) - Five campaign types: percent or flat off the bill, percent off a category or a product, and buy-X-get-Y. - Conditions: date range (inclusive of the closing day), days of the week, minimum bill value, and a cap on what a percentage can take off — without one an unusually large trolley gives away more than the campaign was costed for. - Stacking is conservative by default. All stackable campaigns apply together; of the exclusive ones only the single best does, chosen by what it is worth to the shopper with priority breaking ties. Two percentages compounding produce a discount nobody signed off, and the shop finds out at the end of the month. - The total is capped at the subtotal, so no combination of campaign, tier and manual discount can turn a sale into a payout. - buy-X-get-Y counts whole groups only, and prices the free unit at what is actually being charged — a line already carrying a manual discount must not refund more than it took. Kept out of Cart deliberately: Cart owns arithmetic that must never be wrong, this owns policy a shop changes weekly. Storage (schema v6, plus promos_json on orders at v7) - Campaigns persist locally, because a shop mid-promotion with a dead line still has to honour the price on the shelf edge. - A bill records the campaign name and the amount given, not a link to the row. A campaign edited or deleted later cannot change what a past sale shows, and a reprinted receipt still names what the shopper was given. - On read-back the promo amounts are subtracted from the manual discount, because bill_discount already contains them. Restoring both at full value would discount the bill twice — the same shape as the bug that used to overstate synced totals. At the till - Every cart mutation re-evaluates, so a promo cannot survive the line that earned it being removed. - A resumed parked bill is re-evaluated rather than restored: a campaign that has since ended must not be honoured because the bill was parked while it was running. - Campaigns are named individually on the billing panel and the printed receipt, so a shopper who came in for an advertised offer can see it applied. Editor - Full CRUD, admin-only, with validation for the cases that would save happily and then silently never fire — a targeted campaign with no target, a percentage over 100, an end date before the start. Tests: 199 -> 210. Covers each campaign type, the eligibility conditions, the stacking rules, the impossible-to-go-negative guarantee, GST recomputation against the reduced total, round-tripping, and the double-count guard. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -139,7 +139,7 @@ void main() {
|
||||
await AppDatabase.instance.open(overridePath: dbPath);
|
||||
final db = AppDatabase.instance.db;
|
||||
|
||||
expect(await db.getVersion(), 5);
|
||||
expect(await db.getVersion(), 7);
|
||||
|
||||
final rows = await db.query('day_archive');
|
||||
expect(rows, hasLength(1));
|
||||
@@ -154,6 +154,11 @@ void main() {
|
||||
// so an existing shop is never handed accounts it did not create.
|
||||
final staff = await db.query('staff');
|
||||
expect(staff, isEmpty);
|
||||
|
||||
// v6 adds campaigns. Same rule: the table exists, and an upgraded shop is
|
||||
// not handed promotions it never created.
|
||||
final promos = await db.query('promos');
|
||||
expect(promos, isEmpty);
|
||||
expect(row['bill_count'], 12);
|
||||
expect(row['gross_sales'], 8450.0);
|
||||
expect(row['tax_collected'], 620.5);
|
||||
|
||||
449
test/unit/promo_engine_test.dart
Normal file
449
test/unit/promo_engine_test.dart
Normal file
@@ -0,0 +1,449 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:nearle_pos/domain/entities/cart.dart';
|
||||
import 'package:nearle_pos/domain/entities/product.dart';
|
||||
import 'package:nearle_pos/domain/entities/promo.dart';
|
||||
import 'package:nearle_pos/domain/services/promo_engine.dart';
|
||||
|
||||
/// Campaigns give money away automatically, so the rules that decide how much
|
||||
/// are the ones worth pinning down hardest.
|
||||
void main() {
|
||||
Product product({
|
||||
String id = 'p1',
|
||||
ProductCategory category = ProductCategory.beverages,
|
||||
double price = 100,
|
||||
double gstRate = 0.18,
|
||||
}) =>
|
||||
Product(
|
||||
id: id,
|
||||
name: 'Item $id',
|
||||
barcode: 'bc-$id',
|
||||
sku: 'sku-$id',
|
||||
category: category,
|
||||
price: price,
|
||||
stock: 100,
|
||||
gstRate: gstRate,
|
||||
);
|
||||
|
||||
Cart cartOf(List<({Product product, double qty})> items) => Cart(
|
||||
lines: [
|
||||
for (final i in items)
|
||||
CartLine(product: i.product, quantity: i.qty),
|
||||
],
|
||||
);
|
||||
|
||||
final monday = DateTime(2026, 8, 3, 11);
|
||||
|
||||
group('amount', () {
|
||||
test('a percentage off the bill', () {
|
||||
final cart = cartOf([(product: product(), qty: 3)]); // 300
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: '10% off',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 10,
|
||||
);
|
||||
|
||||
expect(PromoEngine.amountFor(promo: promo, cart: cart), 30);
|
||||
});
|
||||
|
||||
test('a flat amount off the bill', () {
|
||||
final cart = cartOf([(product: product(), qty: 3)]);
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: '50 off',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 50,
|
||||
);
|
||||
|
||||
expect(PromoEngine.amountFor(promo: promo, cart: cart), 50);
|
||||
});
|
||||
|
||||
test('a category promo touches only that category', () {
|
||||
final cart = cartOf([
|
||||
(product: product(category: ProductCategory.beverages), qty: 2), // 200
|
||||
(product: product(id: 'p2', category: ProductCategory.snacks), qty: 3),
|
||||
]);
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: '20% off drinks',
|
||||
type: PromoType.percentOffCategory,
|
||||
value: 20,
|
||||
targetId: 'beverages',
|
||||
);
|
||||
|
||||
expect(PromoEngine.amountFor(promo: promo, cart: cart), 40);
|
||||
});
|
||||
|
||||
test('a product promo touches only that product', () {
|
||||
final cart = cartOf([
|
||||
(product: product(), qty: 2),
|
||||
(product: product(id: 'p2'), qty: 4),
|
||||
]);
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: '50% off p2',
|
||||
type: PromoType.percentOffProduct,
|
||||
value: 50,
|
||||
targetId: 'p2',
|
||||
);
|
||||
|
||||
expect(PromoEngine.amountFor(promo: promo, cart: cart), 200);
|
||||
});
|
||||
|
||||
test('a promo whose target is not in the bill is worth nothing', () {
|
||||
final cart = cartOf([(product: product(), qty: 2)]);
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: '20% off Dairy',
|
||||
type: PromoType.percentOffCategory,
|
||||
value: 20,
|
||||
targetId: 'dairy',
|
||||
);
|
||||
|
||||
expect(PromoEngine.amountFor(promo: promo, cart: cart), 0);
|
||||
});
|
||||
|
||||
test('a cap stops a large trolley giving away more than was costed', () {
|
||||
final cart = cartOf([(product: product(), qty: 50)]); // 5000
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: '20% off, max 200',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 20,
|
||||
maxDiscount: 200,
|
||||
);
|
||||
|
||||
expect(PromoEngine.amountFor(promo: promo, cart: cart), 200);
|
||||
});
|
||||
});
|
||||
|
||||
group('buy X get Y', () {
|
||||
const promo = Promo(
|
||||
id: 'bxgy',
|
||||
name: 'Buy 2 get 1',
|
||||
type: PromoType.buyXGetY,
|
||||
targetId: 'p1',
|
||||
buyQuantity: 2,
|
||||
freeQuantity: 1,
|
||||
);
|
||||
|
||||
test('gives nothing until a whole group is in the bill', () {
|
||||
for (final qty in [1.0, 2.0]) {
|
||||
final cart = cartOf([(product: product(), qty: qty)]);
|
||||
expect(PromoEngine.amountFor(promo: promo, cart: cart), 0,
|
||||
reason: '$qty items should not earn a free one',);
|
||||
}
|
||||
});
|
||||
|
||||
test('gives one free per completed group, and no more', () {
|
||||
// Three earns one. Five still earns one — the fifth has not paid for a
|
||||
// second group.
|
||||
expect(
|
||||
PromoEngine.amountFor(
|
||||
promo: promo,
|
||||
cart: cartOf([(product: product(), qty: 3)]),
|
||||
),
|
||||
100,
|
||||
);
|
||||
expect(
|
||||
PromoEngine.amountFor(
|
||||
promo: promo,
|
||||
cart: cartOf([(product: product(), qty: 5)]),
|
||||
),
|
||||
100,
|
||||
);
|
||||
expect(
|
||||
PromoEngine.amountFor(
|
||||
promo: promo,
|
||||
cart: cartOf([(product: product(), qty: 6)]),
|
||||
),
|
||||
200,
|
||||
);
|
||||
});
|
||||
|
||||
test('prices the free unit at what is actually being charged', () {
|
||||
// A line already carrying a manual discount must not refund more than it
|
||||
// took in the first place.
|
||||
final cart = Cart(
|
||||
lines: [
|
||||
CartLine(
|
||||
product: product(),
|
||||
quantity: 3,
|
||||
discount: const Discount(type: DiscountType.percentage, value: 50),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
expect(PromoEngine.amountFor(promo: promo, cart: cart), 50);
|
||||
});
|
||||
|
||||
test('a malformed buy-X-get-Y is worth nothing rather than everything', () {
|
||||
final cart = cartOf([(product: product(), qty: 10)]);
|
||||
const broken = Promo(
|
||||
id: 'b',
|
||||
name: 'Buy 0 get 0',
|
||||
type: PromoType.buyXGetY,
|
||||
targetId: 'p1',
|
||||
);
|
||||
|
||||
expect(PromoEngine.amountFor(promo: broken, cart: cart), 0);
|
||||
});
|
||||
});
|
||||
|
||||
group('eligibility', () {
|
||||
final cart = Cart(
|
||||
lines: [CartLine(product: product(), quantity: 3)],
|
||||
); // 300
|
||||
|
||||
List<AppliedPromo> run(List<Promo> promos, {DateTime? at}) =>
|
||||
PromoEngine.evaluate(cart: cart, promos: promos, at: at ?? monday);
|
||||
|
||||
test('an inactive campaign never fires', () {
|
||||
expect(
|
||||
run([
|
||||
const Promo(
|
||||
id: 'a',
|
||||
name: 'Paused',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 10,
|
||||
isActive: false,
|
||||
),
|
||||
]),
|
||||
isEmpty,
|
||||
);
|
||||
});
|
||||
|
||||
test('a campaign outside its dates never fires', () {
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: 'August',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 10,
|
||||
);
|
||||
|
||||
final august = promo.copyWith(
|
||||
validFrom: DateTime(2026, 8),
|
||||
validTo: DateTime(2026, 8, 31),
|
||||
);
|
||||
|
||||
expect(run([august], at: DateTime(2026, 7, 31, 23)), isEmpty);
|
||||
expect(run([august], at: DateTime(2026, 8, 15)), hasLength(1));
|
||||
|
||||
// Inclusive of the closing day: a campaign "to the 31st" runs all of it.
|
||||
expect(run([august], at: DateTime(2026, 8, 31, 22)), hasLength(1));
|
||||
expect(run([august], at: DateTime(2026, 9, 1)), isEmpty);
|
||||
});
|
||||
|
||||
test('a weekday-restricted campaign fires only on those days', () {
|
||||
const weekend = Promo(
|
||||
id: 'a',
|
||||
name: 'Weekends',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 10,
|
||||
daysOfWeek: {6, 7},
|
||||
);
|
||||
|
||||
expect(run([weekend], at: DateTime(2026, 8, 3)), isEmpty); // Monday
|
||||
expect(run([weekend], at: DateTime(2026, 8, 8)), hasLength(1)); // Sat
|
||||
expect(run([weekend], at: DateTime(2026, 8, 9)), hasLength(1)); // Sun
|
||||
});
|
||||
|
||||
test('a minimum bill value is enforced', () {
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: 'Over 500',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 50,
|
||||
minBillValue: 500,
|
||||
);
|
||||
|
||||
expect(run([promo]), isEmpty, reason: 'the bill is only 300');
|
||||
});
|
||||
});
|
||||
|
||||
group('stacking', () {
|
||||
final cart = Cart(
|
||||
lines: [CartLine(product: product(), quantity: 10)],
|
||||
); // 1000
|
||||
|
||||
List<AppliedPromo> run(List<Promo> promos) =>
|
||||
PromoEngine.evaluate(cart: cart, promos: promos, at: monday);
|
||||
|
||||
test('only the best exclusive campaign applies', () {
|
||||
// Two stacking percentages compound into a discount nobody costed, and
|
||||
// the shop finds out at the end of the month.
|
||||
final applied = run(const [
|
||||
Promo(id: 'a', name: '5%', type: PromoType.percentOffBill, value: 5),
|
||||
Promo(id: 'b', name: '15%', type: PromoType.percentOffBill, value: 15),
|
||||
Promo(id: 'c', name: '10%', type: PromoType.percentOffBill, value: 10),
|
||||
]);
|
||||
|
||||
expect(applied, hasLength(1));
|
||||
expect(applied.single.promo.id, 'b');
|
||||
expect(applied.single.amount, 150);
|
||||
});
|
||||
|
||||
test('the best means best for the shopper, not highest percentage', () {
|
||||
final applied = run(const [
|
||||
Promo(id: 'pct', name: '5%', type: PromoType.percentOffBill, value: 5),
|
||||
Promo(
|
||||
id: 'flat',
|
||||
name: '200 off',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 200,
|
||||
),
|
||||
]);
|
||||
|
||||
expect(applied.single.promo.id, 'flat');
|
||||
expect(applied.single.amount, 200);
|
||||
});
|
||||
|
||||
test('stackable campaigns all apply, alongside one exclusive', () {
|
||||
final applied = run(const [
|
||||
Promo(
|
||||
id: 's1',
|
||||
name: 'Stack 50',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 50,
|
||||
stackable: true,
|
||||
priority: 10,
|
||||
),
|
||||
Promo(
|
||||
id: 's2',
|
||||
name: 'Stack 30',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 30,
|
||||
stackable: true,
|
||||
priority: 20,
|
||||
),
|
||||
Promo(id: 'e1', name: '10%', type: PromoType.percentOffBill, value: 10),
|
||||
Promo(id: 'e2', name: '5%', type: PromoType.percentOffBill, value: 5),
|
||||
]);
|
||||
|
||||
expect(applied.map((a) => a.promo.id), ['s1', 's2', 'e1']);
|
||||
expect(applied.fold<double>(0, (s, a) => s + a.amount), 180);
|
||||
});
|
||||
|
||||
test('priority breaks a tie between equal exclusive campaigns', () {
|
||||
final applied = run(const [
|
||||
Promo(
|
||||
id: 'low',
|
||||
name: 'A',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 100,
|
||||
priority: 50,
|
||||
),
|
||||
Promo(
|
||||
id: 'high',
|
||||
name: 'B',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 100,
|
||||
priority: 10,
|
||||
),
|
||||
]);
|
||||
|
||||
expect(applied.single.promo.id, 'high');
|
||||
});
|
||||
|
||||
test('no combination can drive the bill below zero', () {
|
||||
// The one outcome that must be impossible: a sale that becomes a payout.
|
||||
final applied = run(const [
|
||||
Promo(
|
||||
id: 'a',
|
||||
name: 'Huge',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 900,
|
||||
stackable: true,
|
||||
),
|
||||
Promo(
|
||||
id: 'b',
|
||||
name: 'Also huge',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 900,
|
||||
stackable: true,
|
||||
),
|
||||
]);
|
||||
|
||||
final total = applied.fold<double>(0, (s, a) => s + a.amount);
|
||||
expect(total, lessThanOrEqualTo(1000));
|
||||
expect(total, 1000);
|
||||
});
|
||||
});
|
||||
|
||||
group('on the bill', () {
|
||||
test('a promo reduces the total and is included in savings', () {
|
||||
final base = Cart(lines: [CartLine(product: product(), quantity: 10)]);
|
||||
expect(base.grandTotal, 1000);
|
||||
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: '10% off',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 10,
|
||||
);
|
||||
|
||||
final withPromo = base.copyWith(
|
||||
appliedPromos: [const AppliedPromo(promo: promo, amount: 100)],
|
||||
);
|
||||
|
||||
expect(withPromo.promoDiscountAmount, 100);
|
||||
expect(withPromo.billDiscountTotal, 100);
|
||||
expect(withPromo.grandTotal, 900);
|
||||
expect(withPromo.totalSavings, 100);
|
||||
});
|
||||
|
||||
test('GST is recomputed against the reduced total, not the original', () {
|
||||
// Prices are GST-inclusive, so a discount reduces the tax collected. Left
|
||||
// unapportioned the shop would remit tax on money it never took.
|
||||
final base = Cart(lines: [CartLine(product: product(), quantity: 10)]);
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: '10% off',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 10,
|
||||
);
|
||||
|
||||
final withPromo = base.copyWith(
|
||||
appliedPromos: [const AppliedPromo(promo: promo, amount: 100)],
|
||||
);
|
||||
|
||||
expect(withPromo.taxAmount, lessThan(base.taxAmount));
|
||||
expect(withPromo.taxAmount, closeTo(base.taxAmount * 0.9, 0.02));
|
||||
expect(
|
||||
withPromo.taxableAmount + withPromo.taxAmount,
|
||||
closeTo(withPromo.netAmount, 0.02),
|
||||
);
|
||||
});
|
||||
|
||||
test('a promo and a tier discount both apply, without going negative', () {
|
||||
final base = Cart(lines: [CartLine(product: product(), quantity: 10)]);
|
||||
const promo = Promo(
|
||||
id: 'a',
|
||||
name: 'Everything free',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 5000,
|
||||
);
|
||||
|
||||
final withPromo = base.copyWith(
|
||||
appliedPromos: [const AppliedPromo(promo: promo, amount: 5000)],
|
||||
);
|
||||
|
||||
expect(withPromo.billDiscountTotal, 1000);
|
||||
expect(withPromo.grandTotal, 0);
|
||||
expect(withPromo.netAmount, greaterThanOrEqualTo(0));
|
||||
});
|
||||
|
||||
test('an empty cart earns nothing', () {
|
||||
expect(
|
||||
PromoEngine.evaluate(
|
||||
cart: Cart.empty,
|
||||
promos: const [
|
||||
Promo(id: 'a', name: 'x', type: PromoType.flatOffBill, value: 50),
|
||||
],
|
||||
at: monday,
|
||||
),
|
||||
isEmpty,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
290
test/unit/promo_persistence_test.dart
Normal file
290
test/unit/promo_persistence_test.dart
Normal file
@@ -0,0 +1,290 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:nearle_pos/data/datasources/local_store.dart';
|
||||
import 'package:nearle_pos/data/datasources/seed_data.dart';
|
||||
import 'package:nearle_pos/data/local/promo_dao.dart';
|
||||
import 'package:nearle_pos/data/repositories/product_repository_impl.dart';
|
||||
import 'package:nearle_pos/data/repositories/customer_repository_impl.dart';
|
||||
import 'package:nearle_pos/data/repositories/transaction_repository_impl.dart';
|
||||
import 'package:nearle_pos/domain/entities/cart.dart';
|
||||
import 'package:nearle_pos/domain/entities/promo.dart';
|
||||
import 'package:nearle_pos/domain/entities/transaction.dart';
|
||||
import 'package:nearle_pos/domain/services/promo_engine.dart';
|
||||
import 'package:nearle_pos/domain/usecases/checkout_sale.dart';
|
||||
|
||||
void main() {
|
||||
late LocalStore store;
|
||||
late PromoDao promos;
|
||||
|
||||
setUpAll(() {
|
||||
LocalStore.registerSeed(
|
||||
products: SeedData.products,
|
||||
customers: SeedData.customers,
|
||||
);
|
||||
});
|
||||
|
||||
setUp(() async {
|
||||
store = LocalStore.instance;
|
||||
await store.reset(withCatalogue: true);
|
||||
promos = store.promos;
|
||||
});
|
||||
|
||||
const weekendSaver = Promo(
|
||||
id: '',
|
||||
name: 'Weekend Saver',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 10,
|
||||
minBillValue: 500,
|
||||
maxDiscount: 200,
|
||||
daysOfWeek: {6, 7},
|
||||
);
|
||||
|
||||
group('storage', () {
|
||||
test('a campaign round-trips with every field intact', () async {
|
||||
final saved = await promos.save(weekendSaver.copyWith(
|
||||
validFrom: DateTime(2026, 8),
|
||||
validTo: DateTime(2026, 8, 31),
|
||||
),);
|
||||
|
||||
final read = await promos.findById(saved.id);
|
||||
|
||||
expect(read!.name, 'Weekend Saver');
|
||||
expect(read.type, PromoType.percentOffBill);
|
||||
expect(read.value, 10);
|
||||
expect(read.minBillValue, 500);
|
||||
expect(read.maxDiscount, 200);
|
||||
expect(read.daysOfWeek, {6, 7});
|
||||
expect(read.validFrom, DateTime(2026, 8));
|
||||
expect(read.stackable, isFalse);
|
||||
expect(read.isActive, isTrue);
|
||||
});
|
||||
|
||||
test('editing keeps the id, so nothing points at a stale row', () async {
|
||||
final saved = await promos.save(weekendSaver);
|
||||
final edited = await promos.save(saved.copyWith(value: 15));
|
||||
|
||||
expect(edited.id, saved.id);
|
||||
expect(await promos.all(), hasLength(1));
|
||||
expect(edited.value, 15);
|
||||
});
|
||||
|
||||
test('pausing removes it from what the till applies', () async {
|
||||
final saved = await promos.save(weekendSaver);
|
||||
|
||||
await promos.setActive(saved.id, active: false);
|
||||
|
||||
expect(await promos.all(), hasLength(1));
|
||||
expect(await promos.all(activeOnly: true), isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('validation', () {
|
||||
test('a campaign must give something away', () async {
|
||||
await expectLater(
|
||||
promos.save(const Promo(
|
||||
id: '',
|
||||
name: 'Nothing',
|
||||
type: PromoType.percentOffBill,
|
||||
),),
|
||||
throwsA(isA<PromoException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('a percentage over 100 is refused', () async {
|
||||
// Over 100% is a refund with extra steps.
|
||||
await expectLater(
|
||||
promos.save(const Promo(
|
||||
id: '',
|
||||
name: 'Too much',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 150,
|
||||
),),
|
||||
throwsA(isA<PromoException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('a targeted campaign without a target is refused', () async {
|
||||
// It would save happily and then silently never fire, which is worse
|
||||
// than an error.
|
||||
await expectLater(
|
||||
promos.save(const Promo(
|
||||
id: '',
|
||||
name: 'Category promo',
|
||||
type: PromoType.percentOffCategory,
|
||||
value: 10,
|
||||
),),
|
||||
throwsA(isA<PromoException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('an end date before the start is refused', () async {
|
||||
await expectLater(
|
||||
promos.save(weekendSaver.copyWith(
|
||||
validFrom: DateTime(2026, 9),
|
||||
validTo: DateTime(2026, 8),
|
||||
),),
|
||||
throwsA(isA<PromoException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('a buy-X-get-Y with no quantities is refused', () async {
|
||||
await expectLater(
|
||||
promos.save(const Promo(
|
||||
id: '',
|
||||
name: 'Broken BOGO',
|
||||
type: PromoType.buyXGetY,
|
||||
targetId: 'p1',
|
||||
),),
|
||||
throwsA(isA<PromoException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('on a completed sale', () {
|
||||
test('the discount given is recorded and read back', () async {
|
||||
// A bill stores the amount, not a link to the campaign, so a promo that
|
||||
// is later edited or deleted cannot change what a past sale shows.
|
||||
final products = ProductRepositoryImpl(store);
|
||||
final transactions = TransactionRepositoryImpl(store);
|
||||
final checkout = CheckoutSale(
|
||||
productRepository: products,
|
||||
customerRepository: CustomerRepositoryImpl(store),
|
||||
transactionRepository: transactions,
|
||||
);
|
||||
|
||||
final milk = (await products.findByBarcode('8901234500011'))!;
|
||||
final saved = await promos.save(const Promo(
|
||||
id: '',
|
||||
name: 'Ten off everything',
|
||||
type: PromoType.percentOffBill,
|
||||
value: 10,
|
||||
),);
|
||||
|
||||
var cart = Cart(lines: [CartLine(product: milk, quantity: 10)]);
|
||||
cart = cart.copyWith(
|
||||
appliedPromos: PromoEngine.evaluate(
|
||||
cart: cart,
|
||||
promos: [saved],
|
||||
at: DateTime.now(),
|
||||
),
|
||||
);
|
||||
|
||||
expect(cart.appliedPromos, hasLength(1));
|
||||
expect(cart.promoDiscountAmount, 62);
|
||||
final due = cart.grandTotal;
|
||||
|
||||
await checkout(
|
||||
cart: cart,
|
||||
payments: [
|
||||
PaymentSplit(method: PaymentMethod.cash, amount: due, tendered: due),
|
||||
],
|
||||
cashierName: 'Divya',
|
||||
terminalId: 'T0TEST',
|
||||
);
|
||||
|
||||
final stored = (await transactions.history()).single;
|
||||
|
||||
expect(stored.total, due,
|
||||
reason: 'the figure charged must not move in storage',);
|
||||
expect(stored.cart.appliedPromos, hasLength(1));
|
||||
expect(stored.cart.appliedPromos.single.promo.name, 'Ten off everything');
|
||||
expect(stored.cart.appliedPromos.single.amount, 62);
|
||||
});
|
||||
|
||||
test('a promo and a manual discount are not double-counted on read-back',
|
||||
() async {
|
||||
// bill_discount on the row already contains the promo. Restoring both at
|
||||
// full value would discount the bill twice on the way back in — the exact
|
||||
// shape of the bug that used to overstate synced totals.
|
||||
final products = ProductRepositoryImpl(store);
|
||||
final transactions = TransactionRepositoryImpl(store);
|
||||
final checkout = CheckoutSale(
|
||||
productRepository: products,
|
||||
customerRepository: CustomerRepositoryImpl(store),
|
||||
transactionRepository: transactions,
|
||||
);
|
||||
|
||||
final milk = (await products.findByBarcode('8901234500011'))!;
|
||||
final saved = await promos.save(const Promo(
|
||||
id: '',
|
||||
name: 'Fifty off',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 50,
|
||||
),);
|
||||
|
||||
var cart = Cart(
|
||||
lines: [CartLine(product: milk, quantity: 10)],
|
||||
billDiscount: const Discount(type: DiscountType.flat, value: 30),
|
||||
);
|
||||
cart = cart.copyWith(
|
||||
appliedPromos: PromoEngine.evaluate(
|
||||
cart: cart,
|
||||
promos: [saved],
|
||||
at: DateTime.now(),
|
||||
),
|
||||
);
|
||||
|
||||
expect(cart.billDiscountTotal, 80, reason: '50 promo + 30 manual');
|
||||
final due = cart.grandTotal;
|
||||
|
||||
await checkout(
|
||||
cart: cart,
|
||||
payments: [
|
||||
PaymentSplit(method: PaymentMethod.cash, amount: due, tendered: due),
|
||||
],
|
||||
cashierName: 'Divya',
|
||||
terminalId: 'T0TEST',
|
||||
);
|
||||
|
||||
final stored = (await transactions.history()).single;
|
||||
|
||||
expect(stored.cart.billDiscountTotal, 80);
|
||||
expect(stored.cart.promoDiscountAmount, 50);
|
||||
expect(stored.cart.manualBillDiscountAmount, 30);
|
||||
expect(stored.total, due);
|
||||
});
|
||||
|
||||
test('deleting a campaign does not change a bill already rung', () async {
|
||||
final products = ProductRepositoryImpl(store);
|
||||
final transactions = TransactionRepositoryImpl(store);
|
||||
final checkout = CheckoutSale(
|
||||
productRepository: products,
|
||||
customerRepository: CustomerRepositoryImpl(store),
|
||||
transactionRepository: transactions,
|
||||
);
|
||||
|
||||
final milk = (await products.findByBarcode('8901234500011'))!;
|
||||
final saved = await promos.save(const Promo(
|
||||
id: '',
|
||||
name: 'Doomed campaign',
|
||||
type: PromoType.flatOffBill,
|
||||
value: 100,
|
||||
),);
|
||||
|
||||
var cart = Cart(lines: [CartLine(product: milk, quantity: 10)]);
|
||||
cart = cart.copyWith(
|
||||
appliedPromos: PromoEngine.evaluate(
|
||||
cart: cart,
|
||||
promos: [saved],
|
||||
at: DateTime.now(),
|
||||
),
|
||||
);
|
||||
final due = cart.grandTotal;
|
||||
|
||||
await checkout(
|
||||
cart: cart,
|
||||
payments: [
|
||||
PaymentSplit(method: PaymentMethod.cash, amount: due, tendered: due),
|
||||
],
|
||||
cashierName: 'Divya',
|
||||
terminalId: 'T0TEST',
|
||||
);
|
||||
|
||||
await promos.delete(saved.id);
|
||||
|
||||
final stored = (await transactions.history()).single;
|
||||
expect(stored.total, due);
|
||||
expect(stored.cart.appliedPromos.single.promo.name, 'Doomed campaign');
|
||||
expect(stored.cart.appliedPromos.single.amount, 100);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -71,6 +71,8 @@ void main() {
|
||||
// timer is left pending and trips the binding's leak check. Stubbed
|
||||
// so this test measures rendering, which is what it is for.
|
||||
unsyncedCountProvider.overrideWith((ref) async => 0),
|
||||
promosProvider.overrideWith((ref) async => []),
|
||||
activePromosProvider.overrideWith((ref) async => []),
|
||||
parkedBillsProvider.overrideWith((ref) async => []),
|
||||
orderSyncRowsProvider.overrideWith((ref) async => []),
|
||||
todayReportProvider.overrideWith((ref) async => blankReport()),
|
||||
|
||||
Reference in New Issue
Block a user