Commit 052622b2 by tdgiang

Only combine products with the same VAT rate in one order

Real catalog has 3 different VAT rates (5%, 8%, 10%) - the algorithm
previously mixed products across rates freely within one combo. Each
attempt now picks a random pivot product to fix that attempt's tax rate,
then only combines within that same-rate subset. Pivot selection is
weighted naturally by group size (uniform draw over all candidates), so
larger rate groups are proportionally more likely to be tried first.

Verified against the real catalog: all previously-working target amounts
still succeed, every returned combo uses exactly one VAT rate.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017KPzWwuTEeX2vXGXvyGn4q
parent e52e1a3a
......@@ -14,12 +14,16 @@ function shuffle(list, random) {
return arr;
}
function computeLineTotal(product, qty) {
function getTaxRate(product) {
var rate = product.tax ? Number(product.tax.rate) : 0;
if (!isFinite(rate)) {
rate = 0;
}
return Math.round(qty * product.price * (1 + rate / 100));
return rate;
}
function computeLineTotal(product, qty) {
return Math.round(qty * product.price * (1 + getTaxRate(product) / 100));
}
function pickProductCombo(products, targetAmount, options) {
......@@ -39,7 +43,17 @@ function pickProductCombo(products, targetAmount, options) {
var minAcceptable = Math.max(1, targetAmount - tolerance);
for (var attempt = 0; attempt < maxAttempts; attempt++) {
var shuffled = shuffle(candidates, random);
// Mỗi đơn chỉ được ghép từ sản phẩm CÙNG một mức thuế VAT - chọn ngẫu
// nhiên 1 sản phẩm "mốc" để xác định mức thuế của đơn này cho lượt thử
// này (xác suất tự nhiên tỷ lệ theo số lượng sản phẩm mỗi mức thuế),
// rồi chỉ ghép trong đúng nhóm thuế đó.
var pivot = candidates[Math.floor(random() * candidates.length)];
var pivotRate = getTaxRate(pivot);
var sameRateCandidates = candidates.filter(function (p) {
return getTaxRate(p) === pivotRate;
});
var shuffled = shuffle(sameRateCandidates, random);
var total = 0;
var lines = [];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment