Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,19 @@ export class InternalTransferScreen extends Component {
await this.addSerialLine(product, lotId, lotName);
return;
}
const lots =
product.tracking === "none" ? [] : await this.getProductLots(product.id);
const existing = this.state.lines.find(
(l) => l.product_id === product.id && l.lot_id === lotId
);
if (existing) {
// The input hands an edited quantity back as text.
existing.qty = roundQty((parseFloat(existing.qty) || 0) + qty);
if (existing.tracking !== "none" && !existing.lots?.length) {
existing.lots = [...(await this.getProductLots(product.id))];
existing.lots = [...lots];
}
return;
}
const lots =
product.tracking === "none"
? []
: [...(await this.getProductLots(product.id))];
this.state.lines.push(
ensureLineUid({
product_id: product.id,
Expand All @@ -214,7 +212,7 @@ export class InternalTransferScreen extends Component {
tracking: product.tracking,
lot_id: lotId,
lot_name: lotName,
lots: lots,
lots: [...lots],
})
);
}
Expand All @@ -239,6 +237,8 @@ export class InternalTransferScreen extends Component {
* the operator to choose the serial on, never a second unit on another.
*/
async addSerialLine(product, lotId, lotName) {
const lots = await this.getProductLots(product.id);
// From here on there is no await: the check and the push run atomically
if (
lotId &&
this.state.lines.some(
Expand All @@ -262,7 +262,7 @@ export class InternalTransferScreen extends Component {
tracking: product.tracking,
lot_id: lotId,
lot_name: lotName,
lots: [...(await this.getProductLots(product.id))],
lots: [...lots],
})
);
}
Expand Down
79 changes: 79 additions & 0 deletions barcode_stock/static/tests/internal_transfer_double_scan.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {beforeEach, describe, expect, test} from "@odoo/hoot";
import {InternalTransferScreen} from "@barcode_stock/js/screens/internal_transfer_screen.esm";
import {patchTranslations} from "@web/../tests/web_test_helpers";

const LOTS = [
{id: 21, name: "SN-0001"},
{id: 22, name: "SN-0002"},
];

/**
* An internal transfer screen without mounting it, whose lots for a product
* arrive only when `resolveLots` is called: the scans made before that are
* all waiting on the same request, as when a PDA reads a label twice.
*
* @returns {Object} the screen, its notifications and `resolveLots`
*/
function transferScreen() {
const screen = Object.create(InternalTransferScreen.prototype);
screen.state = {lines: []};
screen.lotsCache = new Map();
const notifications = [];
screen.notification = {add: (message) => notifications.push(String(message))};
let resolveLots = null;
const lots = new Promise((resolve) => {
resolveLots = resolve;
});
screen.fetchLots = () => lots;
return {screen, notifications, resolveLots: () => resolveLots(LOTS)};
}

/**
* Scan `times` times without waiting for any scan to finish, then let the
* lots arrive and wait for them all.
*/
async function scanAtOnce({screen, resolveLots}, product, lot, times = 2) {
const scans = [];
for (let i = 0; i < times; i++) {
scans.push(screen.addLine(product, lot?.id || null, lot?.name || null, 1));
}
resolveLots();
await Promise.all(scans);
}

describe("BarcodeStock", () => {
beforeEach(() => patchTranslations());

test("a serial scanned twice at once lands on a single line", async () => {
const transfer = transferScreen();
const product = {id: 7, name: "Drill", tracking: "serial"};
await scanAtOnce(transfer, product, LOTS[0]);
expect(transfer.screen.state.lines).toHaveLength(1);
expect(transfer.screen.state.lines[0]).toMatchObject({
product_id: 7,
lot_id: 21,
qty: 1,
});
expect(transfer.notifications).toEqual([
"Serial number SN-0001 is already in this transfer.",
]);
// Another serial of the same product still gets its own line.
await transfer.screen.addLine(product, LOTS[1].id, LOTS[1].name, 1);
const serials = transfer.screen.state.lines.map((line) => line.lot_id);
expect(serials).toEqual([21, 22]);
});

test("a lot scanned twice at once adds up on a single line", async () => {
const transfer = transferScreen();
const product = {id: 7, name: "Flour", tracking: "lot"};
await scanAtOnce(transfer, product, LOTS[0]);
expect(transfer.screen.state.lines).toHaveLength(1);
expect(transfer.screen.state.lines[0]).toMatchObject({
product_id: 7,
lot_id: 21,
qty: 2,
});
expect(transfer.screen.state.lines[0].lots).toEqual(LOTS);
expect(transfer.notifications).toEqual([]);
});
});
5 changes: 3 additions & 2 deletions barcode_stock/static/tests/quantity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,14 @@ describe("BarcodeStock", () => {

test("an internal transfer line adds up scans and an edited quantity", async () => {
const screen = Object.create(InternalTransferScreen.prototype);
const product = {id: 7, tracking: "none"};
Comment thread
luisDIXMIT marked this conversation as resolved.
Resolved
const line = {product_id: 7, lot_id: false, qty: 0.1, tracking: "none"};
screen.state = {lines: [line]};
await screen.addLine({id: 7}, false, false, 0.2);
await screen.addLine(product, false, false, 0.2);
expect(line.qty).toBe(0.3);
// The input hands an edited quantity back as text.
line.qty = "2";
await screen.addLine({id: 7}, false, false, 1);
await screen.addLine(product, false, false, 1);
expect(line.qty).toBe(3);
});
});
Loading