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
54 changes: 33 additions & 21 deletions barcode_stock/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
137 changes: 127 additions & 10 deletions barcode_stock/static/src/js/screens/internal_transfer_screen.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -52,6 +66,7 @@ export class InternalTransferScreen extends Component {
if (!line.lots) {
line.lots = [];
}
ensureLineUid(line);
}
this.state.lines = nextLines;
if (
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
);
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
}
Expand Down
43 changes: 43 additions & 0 deletions barcode_stock/static/src/js/screens/move_wizard_screen.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions barcode_stock/static/src/js/screens/picking_screen.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Loading
Loading