Skip to content

[ADD] barcode_scan: Added barcode scanning in SO/PO #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 18.0
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions barcode_scan/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
'name': "Barcode Scanning In SO/PO",
'version': '0.1',
'summary': 'Adds barcode scanning capabilities to product catalog',
'depends': ['sale_management', 'stock_barcode', 'purchase'],
'application': True,
'installable': True,
"assets": {
'web.assets_backend': [
'barcode_scan/static/src/**/*',
],
},
'license': 'AGPL-3'
}
55 changes: 55 additions & 0 deletions barcode_scan/static/src/product_catalog/kanban_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { patch } from "@web/core/utils/patch";
import { rpc } from "@web/core/network/rpc";
import { useService, useBus } from "@web/core/utils/hooks";
import { ProductCatalogKanbanController } from "@product/product_catalog/kanban_controller";

patch(ProductCatalogKanbanController.prototype, {
setup(){
super.setup();
this.orm = useService("orm");
this.barcodeService = useService("barcode");
useBus(this.barcodeService.bus, "barcode_scanned", (ev) => this.onBarcodeScannedHandler(ev.detail.barcode));
this.resModel = this.props.context.product_catalog_order_model;
this.notification = useService("notification");
},

async onBarcodeScannedHandler(barcode) {
const [product] = await this.orm.searchRead(
"product.product",
[["barcode", "=", barcode]],
["id"],
{ limit: 1 }
);
if (!product) {
this.notification.add(("No product found with barcode: ") + barcode, { type: "danger" });
return;
}

const quantityFieldMap = {
"sale.order": "product_uom_qty",
"purchase.order": "product_qty",
};
const productQuantity = quantityFieldMap[this.resModel];
if (!productQuantity) {
this.notification.add("Unsupported model: " + this.resModel, { type: "danger" });
}

const orderLines = await this.orm.searchRead(
this.props.context.active_model,
[["order_id", "=", this.props.context.order_id],
["product_id", "=", product.id]],
["id", productQuantity, "product_id"]
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not { limit: 1 } ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If in any case there are more than 1 order line for any specific product, and if the limit: 1 is used, then the total quantity of the product will be shown wrong.


const existingQty = orderLines[0]?.[productQuantity] || 0;
const finallyQuantity = existingQty + 1;
await rpc("/product/catalog/update_order_line_info", {
order_id: this.props.context.order_id,
product_id: product.id,
quantity: finallyQuantity,
res_model: this.resModel,
});

this.model.load();
}
});
36 changes: 36 additions & 0 deletions barcode_scan/static/src/product_catalog/kanban_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ProductCatalogKanbanModel } from "@product/product_catalog/kanban_model";
import { getFieldsSpec } from "@web/model/relational_model/utils";
import { rpc } from "@web/core/network/rpc";

export class CustomProductCatalogKanbanModel extends ProductCatalogKanbanModel {
async _loadUngroupedList(config) {
const ProductIds = await this.orm.search(config.resModel, config.domain);
if (!ProductIds.length) {
return { records: [], length: 0 };
}

let orderLinesInfo = {};
if (config.context.order_id && config.context.product_catalog_order_model) {
orderLinesInfo = await rpc("/product/catalog/order_lines_info", {
order_id: config.context.order_id,
product_ids: ProductIds,
res_model: config.context.product_catalog_order_model,
});
ProductIds.sort((a, b) => (orderLinesInfo[b].quantity || 0) - (orderLinesInfo[a].quantity || 0));
}

const catalogPage = ProductIds.slice(config.offset, config.offset + config.limit);

const kwargs = {
specification: getFieldsSpec(config.activeFields, config.fields, config.context),
};

const result = await this.orm.webSearchRead(config.resModel, [["id", "in", catalogPage]], kwargs);

result.records.sort((a, b) => (orderLinesInfo[b.id].quantity || 0) - (orderLinesInfo[a.id].quantity || 0));
return {
length: ProductIds.length,
records: result.records,
};
}
}
11 changes: 11 additions & 0 deletions barcode_scan/static/src/product_catalog/kanban_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { registry } from "@web/core/registry";
import { productCatalogKanbanView } from "@product/product_catalog/kanban_view";
import { CustomProductCatalogKanbanModel } from "./kanban_model";

export const customProductCatalogKanbanView = {
...productCatalogKanbanView,
Model: CustomProductCatalogKanbanModel,
};

registry.category("views").remove("product_kanban_catalog");
registry.category("views").add("product_kanban_catalog", customProductCatalogKanbanView);