diff --git a/barcode_stock/models/stock_picking.py b/barcode_stock/models/stock_picking.py index ed12b5d..9172d68 100644 --- a/barcode_stock/models/stock_picking.py +++ b/barcode_stock/models/stock_picking.py @@ -302,38 +302,50 @@ def action_barcode_scanner_internal_transfer( ) move_line_model = self.env["stock.move.line"] - move_line_vals_list = [] + + qty_by_product = {} for plan in move_plan: - line = plan["line"] - product = line["product"] - move = self.env["stock.move"].create( + product = plan["line"]["product"] + qty_by_product[product] = qty_by_product.get(product, 0.0) + plan["qty"] + products = list(qty_by_product) + moves = self.env["stock.move"].create( + [ { "name": product.display_name, "product_id": product.id, - "product_uom_qty": plan["qty"], + "product_uom_qty": qty_by_product[product], "product_uom": product.uom_id.id, "picking_id": picking.id, "location_id": origin_location.id, "location_dest_id": destination_location.id, "company_id": picking.company_id.id, } - ) + for product in products + ] + ) + moves_by_product = dict(zip(products, moves, strict=False)) + + move_line_vals_list = [] + for plan in move_plan: + product = plan["line"]["product"] + move = moves_by_product[product] for alloc in plan["allocations"]: - vals = { - "move_id": move.id, - "picking_id": picking.id, - "product_id": product.id, - "product_uom_id": product.uom_id.id, - "quantity": alloc["qty"], - "picked": True, - "location_id": alloc["location"].id, - "location_dest_id": destination_location.id, - "company_id": picking.company_id.id, - "lot_id": alloc["lot"].id if alloc["lot"] else False, - "lot_name": alloc["lot"].name if alloc["lot"] else False, - "qty_picked": alloc["qty"], - } - move_line_vals_list.append(vals) + move_line_vals_list.append( + { + "move_id": move.id, + "picking_id": picking.id, + "product_id": product.id, + "product_uom_id": product.uom_id.id, + "quantity": alloc["qty"], + "picked": True, + "location_id": alloc["location"].id, + "location_dest_id": destination_location.id, + "company_id": picking.company_id.id, + "lot_id": alloc["lot"].id if alloc["lot"] else False, + "lot_name": alloc["lot"].name if alloc["lot"] else False, + "qty_picked": alloc["qty"], + } + ) picking.action_confirm() # Our manually-allocated move lines are authoritative; drop anything the diff --git a/barcode_stock/static/src/js/screens/internal_transfer_screen.esm.js b/barcode_stock/static/src/js/screens/internal_transfer_screen.esm.js index 3e1a4be..96189a6 100644 --- a/barcode_stock/static/src/js/screens/internal_transfer_screen.esm.js +++ b/barcode_stock/static/src/js/screens/internal_transfer_screen.esm.js @@ -10,6 +10,19 @@ import {useBarcodeHandler} from "@barcode_scanner/js/hooks/use_barcode_handler.e import {useBarcodeScanner} from "@barcode_scanner/js/hooks/use_inventory.esm"; import {useService} from "@web/core/utils/hooks"; +let lineSequence = 0; +/** + * Give a line an id of its own for the template's t-key: product and lot + * can't be the key, two lines may share them while being edited. + */ +function ensureLineUid(line) { + if (!line.uid) { + lineSequence += 1; + line.uid = `${Date.now()}_${lineSequence}`; + } + return line; +} + export class InternalTransferScreen extends Component { setup() { this.inventory = useBarcodeScanner(); @@ -22,6 +35,7 @@ export class InternalTransferScreen extends Component { if (!line.lots) { line.lots = []; } + ensureLineUid(line); } this.state = useState({ origin_location: params.origin_location || null, @@ -52,6 +66,7 @@ export class InternalTransferScreen extends Component { if (!line.lots) { line.lots = []; } + ensureLineUid(line); } this.state.lines = nextLines; if ( @@ -148,6 +163,9 @@ export class InternalTransferScreen extends Component { type: "warning", } ); + if (product.tracking === "serial") { + return; + } } await this.addLine(product, lot?.id || null, lot?.name || null, qty); } @@ -168,6 +186,10 @@ export class InternalTransferScreen extends Component { } async addLine(product, lotId, lotName, qty) { + if (product.tracking === "serial") { + await this.addSerialLine(product, lotId, lotName); + return; + } const existing = this.state.lines.find( (l) => l.product_id === product.id && l.lot_id === lotId ); @@ -186,15 +208,52 @@ export class InternalTransferScreen extends Component { } const lots = product.tracking === "none" ? [] : (await this.fetchLots(product.id)) || []; - this.state.lines.push({ - product_id: product.id, - product_name: product.name, - qty: qty, - tracking: product.tracking, - lot_id: lotId, - lot_name: lotName, - lots: lots, - }); + this.state.lines.push( + ensureLineUid({ + product_id: product.id, + product_name: product.name, + qty: qty, + tracking: product.tracking, + lot_id: lotId, + lot_name: lotName, + lots: lots, + }) + ); + } + + /** + * A serial number is a single unit: each one goes on its own line with a + * quantity of 1, and scanning one already in the transfer adds nothing. + * A scan without a serial (the plain product barcode) adds a new line for + * the operator to choose the serial on, never a second unit on another. + */ + async addSerialLine(product, lotId, lotName) { + if ( + lotId && + this.state.lines.some( + (line) => + line.product_id === product.id && Number(line.lot_id) === lotId + ) + ) { + this.notification.add( + _t("Serial number %(lot)s is already in this transfer.", { + lot: lotName, + }), + {type: "warning"} + ); + return; + } + this.state.lines.push( + ensureLineUid({ + product_id: product.id, + product_name: product.name, + qty: 1, + tracking: product.tracking, + lot_id: lotId, + lot_name: lotName, + lots: (await this.fetchLots(product.id)) || [], + }) + ); } async fetchAllLots(productId) { @@ -350,6 +409,7 @@ export class InternalTransferScreen extends Component { this.notification.add(_t("Add at least one product."), {type: "warning"}); return false; } + const serialLotIds = new Set(); for (const line of this.state.lines) { if (!(parseFloat(line.qty) > 0)) { this.notification.add( @@ -370,6 +430,30 @@ export class InternalTransferScreen extends Component { ); return false; } + if (line.tracking === "serial") { + if (parseFloat(line.qty) !== 1) { + this.notification.add( + _t( + "%(product)s is tracked by serial number: the quantity must be 1.", + { + product: line.product_name, + } + ), + {type: "warning"} + ); + return false; + } + if (serialLotIds.has(line.lot_id)) { + this.notification.add( + _t("Serial number %(lot)s is included more than once.", { + lot: line.lot_name, + }), + {type: "warning"} + ); + return false; + } + serialLotIds.add(line.lot_id); + } } return true; } @@ -466,11 +550,44 @@ export class InternalTransferScreen extends Component { } } + /** + * Whether a serial number is already chosen on another line: a serial is + * a single unit, so it can't go on two. + */ + isLotTaken(line, lotId) { + return ( + line.tracking === "serial" && + Boolean(lotId) && + this.state.lines.some( + (other) => + other.uid !== line.uid && + other.product_id === line.product_id && + Number(other.lot_id) === Number(lotId) + ) + ); + } + onLotChanged(line, ev) { - line.lot_id = ev.target.value ? parseInt(ev.target.value, 10) : false; + const lotId = ev.target.value ? parseInt(ev.target.value, 10) : false; + if (this.isLotTaken(line, lotId)) { + const lot = (line.lots || []).find((item) => item.id === lotId); + this.notification.add( + _t("Serial number %(lot)s is already in this transfer.", { + lot: lot?.name || lotId, + }), + {type: "warning"} + ); + ev.target.value = line.lot_id || ""; + return; + } + line.lot_id = lotId; this.updateLineLotName(line); } + isLotSelected(line, lotId) { + return Number(line.lot_id) === lotId; + } + goBack() { this.store.goBack(); } diff --git a/barcode_stock/static/src/js/screens/move_wizard_screen.esm.js b/barcode_stock/static/src/js/screens/move_wizard_screen.esm.js index 34e6989..82312ae 100644 --- a/barcode_stock/static/src/js/screens/move_wizard_screen.esm.js +++ b/barcode_stock/static/src/js/screens/move_wizard_screen.esm.js @@ -208,6 +208,18 @@ export class MoveWizardScreen extends Component { if (!name || !this.isTracked) { return true; } + if (this.isSerialAlreadyPicked(lot?.id || false, name)) { + this.feedback.warning({ + notify: true, + message: _t("Serial number %(lot)s is already in this transfer.", { + lot: name, + }), + }); + return false; + } + if (!name || !this.isTracked) { + return true; + } if (lot && this.canUseExistingLot) { if (!this.state.lots.some((item) => item.id === lot.id)) { this.state.lots = [lot, ...this.state.lots]; @@ -934,7 +946,38 @@ export class MoveWizardScreen extends Component { return lot; } + /** + * Whether a serial number is already picked in this move: a serial is a + * single unit, so picking it again would add a second unit of one item. + * + * @param {Number|false} lotId the lot's id, if it exists + * @param {String} lotName the lot's name + * @returns {Boolean} + */ + isSerialAlreadyPicked(lotId, lotName) { + if (!this.isSerial) { + return false; + } + return (this.state.moveLines || []).some( + (line) => + Number(line.qty_picked) > 0 && + ((lotId && lineLotId(line) === lotId) || + (lotName && + sameLotName(line.lot_name || line.lot_id?.[1], lotName))) + ); + } + async confirmPick() { + const pickedLotName = this.selectedLot?.name || this.state.newLotName; + if (this.isSerialAlreadyPicked(this.selectedLotIdNumber, pickedLotName)) { + this.inventory.notify( + _t("Serial number %(lot)s is already in this transfer.", { + lot: pickedLotName, + }), + {type: "danger"} + ); + return; + } if (this.state.mode === "create_lot" && !this.isIncoming) { this.inventory.notify(_t("Lots can only be created in receptions"), { type: "danger", diff --git a/barcode_stock/static/src/js/screens/picking_screen.esm.js b/barcode_stock/static/src/js/screens/picking_screen.esm.js index 8f261ed..9a43fde 100644 --- a/barcode_stock/static/src/js/screens/picking_screen.esm.js +++ b/barcode_stock/static/src/js/screens/picking_screen.esm.js @@ -568,6 +568,33 @@ export class PickingScreen extends Component { async handleScannedLot(move, normalized, source) { const {barcode, lot, quantity, expiration} = normalized; const lotName = lot?.name || normalized.lotName; + const productId = move.product_id?.[0]; + const tracking = + this.barcodeScannerState.indexes.trackingByProductId[productId]; + if (tracking === "serial") { + const alreadyPicked = this.state.moveLines.some( + (line) => + (line.qty_picked || 0) > 0 && + line.product_id?.[0] === productId && + ((lot && line.lot_id?.[0] === lot.id) || line.lot_name === lotName) + ); + if (alreadyPicked) { + const message = _t( + "Serial number %(lot)s is already in this transfer.", + {lot: lotName} + ); + this.setLastScanContext({ + barcode, + source, + tone: "warning", + message, + move, + lotName, + }); + this.feedback.warning({notify: true, message}); + return; + } + } const isOutgoing = this.state.pickingTypeCode === "outgoing"; const today = new Date().toISOString().slice(0, 10); const expired = diff --git a/barcode_stock/static/src/js/screens/product_selector_screen.esm.js b/barcode_stock/static/src/js/screens/product_selector_screen.esm.js index 11389b7..ccbc33c 100644 --- a/barcode_stock/static/src/js/screens/product_selector_screen.esm.js +++ b/barcode_stock/static/src/js/screens/product_selector_screen.esm.js @@ -3,6 +3,7 @@ import {_t} from "@web/core/l10n/translation"; import {barcodeMatchDomain} from "@barcode_scanner/js/utils/scan_match.esm"; import {barcodeScreens} from "@barcode_scanner/js/registries.esm"; import {recordImageUrl} from "@barcode_stock/js/utils/avatar.esm"; +import {roundQty} from "@barcode_stock/js/utils/quantity.esm"; import {useBarcodeHandler} from "@barcode_scanner/js/hooks/use_barcode_handler.esm"; import {useBarcodeScanner} from "@barcode_scanner/js/hooks/use_inventory.esm"; import {useService} from "@web/core/utils/hooks"; @@ -139,32 +140,51 @@ export class ProductSelectorScreen extends Component { return; } - const qty = parseInt(this.state.qty, 10) || 1; + const qty = roundQty(parseFloat(this.state.qty) || 1); + const tracking = product.tracking || "none"; const lines = this.props.params.lines || []; - const existing = lines.find((l) => l.product_id === product.id); - const lots = await this.inventory.searchRead( - "stock.lot", - [["product_id", "=", product.id]], - ["id", "name"] - ); - if (existing) { - existing.qty += qty; + const lots = + tracking === "none" + ? [] + : await this.inventory.searchRead( + "stock.lot", + [["product_id", "=", product.id]], + ["id", "name"] + ); + const newLine = (lineQty) => ({ + product_id: product.id, + product_name: product.name, + qty: lineQty, + price_unit: product.standard_price || 0, + tracking, + lot_id: null, + lot_name: null, + lots, + default_code: product.default_code || "", + }); + + if (tracking === "serial") { + // A serial number is a single unit: one line per unit, each with its + // own serial to choose, never a second unit on an existing line. + const units = Math.max(1, Math.trunc(qty)); + for (let i = 0; i < units; i++) { + lines.push(newLine(1)); + } } else { - lines.push({ - product_id: product.id, - product_name: product.name, - qty: qty, - price_unit: product.standard_price || 0, - tracking: product.tracking || "none", - lot_id: null, - lots: lots, - default_code: product.default_code || "", - }); + // Untracked: add to the product's line. Lot-tracked: only to a line + // whose lot is not chosen yet, since the added units' lot is unknown. + const existing = lines.find( + (line) => + line.product_id === product.id && + (tracking === "none" || !line.lot_id) + ); + if (existing) { + existing.qty = roundQty((parseFloat(existing.qty) || 0) + qty); + } else { + lines.push(newLine(qty)); + } } - this.store.goBack({ - ...this.props.params, - lines: lines, - }); + this.store.goBack({...this.props.params, lines}); } updateSearch(ev) { diff --git a/barcode_stock/static/src/scss/picking_filters.scss b/barcode_stock/static/src/scss/picking_filters.scss index 045d7e1..ed0dcb3 100644 --- a/barcode_stock/static/src/scss/picking_filters.scss +++ b/barcode_stock/static/src/scss/picking_filters.scss @@ -198,3 +198,9 @@ width: 100%; } } + +.ilx-tx-v2-lot-select option.ilx-tx-v2-lot-option--taken { + color: var(--bs-danger, #d44c59); + background-color: rgba(212, 76, 89, 0.12); + font-style: italic; +} diff --git a/barcode_stock/static/src/xml/barcode_stock_templates.xml b/barcode_stock/static/src/xml/barcode_stock_templates.xml index a80a672..a530518 100644 --- a/barcode_stock/static/src/xml/barcode_stock_templates.xml +++ b/barcode_stock/static/src/xml/barcode_stock_templates.xml @@ -995,11 +995,7 @@
- +