Skip to content
Closed
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
Comment thread
luisDIXMIT marked this conversation as resolved.
Resolved
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=True))

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
175 changes: 150 additions & 25 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 All @@ -32,6 +46,7 @@ export class InternalTransferScreen extends Component {
isCheckingAvailability: false,
isValidating: false,
});
this.lotsCache = new Map();
useBarcodeHandler({
onScan: (barcode, parsedData) => {
this.onBarcodeScanned(barcode, parsedData);
Expand All @@ -52,6 +67,7 @@ export class InternalTransferScreen extends Component {
if (!line.lots) {
line.lots = [];
}
ensureLineUid(line);
}
this.state.lines = nextLines;
if (
Expand Down Expand Up @@ -148,6 +164,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 @@ -157,7 +176,7 @@ export class InternalTransferScreen extends Component {
const match = (lots) =>
lots.find((lot) => (lot.name || "").toUpperCase() === wanted) || null;
// Prefer a lot actually on hand at the origin...
const atOrigin = match((await this.fetchLots(productId)) || []);
const atOrigin = match(await this.getProductLots(productId));
if (atOrigin) {
return atOrigin;
}
Expand All @@ -168,33 +187,84 @@ export class InternalTransferScreen extends Component {
}

async addLine(product, lotId, lotName, qty) {
if (product.tracking === "serial") {
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") {
if (!existing.lots) {
existing.lots = [];
}
if (!existing.lots.length) {
existing.lots = (await this.fetchLots(product.id)) || [];
}
if (existing.tracking !== "none" && !existing.lots?.length) {
existing.lots = [...lots];
}
return;
}
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],
})
);
}

getProductLots(productId) {
if (!this.lotsCache.has(productId)) {
const promise = this.fetchLots(productId)
.then((lots) => lots || [])
.catch((error) => {
this.lotsCache.delete(productId);
throw error;
});
this.lotsCache.set(productId, promise);
}
return this.lotsCache.get(productId);
}

/**
* 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) {
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(
(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: [...lots],
})
);
}

async fetchAllLots(productId) {
Expand Down Expand Up @@ -237,11 +307,8 @@ export class InternalTransferScreen extends Component {
async loadLotsForTrackedLines() {
for (const line of this.state.lines) {
if (line.tracking !== "none") {
if (!line.lots) {
line.lots = [];
}
if (!line.lots.length) {
line.lots = (await this.fetchLots(line.product_id)) || [];
if (!line.lots?.length) {
line.lots = [...(await this.getProductLots(line.product_id))];
}
this.updateLineLotName(line);
}
Expand Down Expand Up @@ -350,6 +417,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 +438,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 +558,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
40 changes: 40 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,15 @@ 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 (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 +943,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
Loading
Loading