|
| 1 | +import { patch } from "@web/core/utils/patch"; |
| 2 | +import { ProductCatalogKanbanController } from "@product/product_catalog/kanban_controller"; |
| 3 | +import { rpc } from "@web/core/network/rpc"; |
| 4 | +import { useService } from "@web/core/utils/hooks"; |
| 5 | +import { onMounted, onWillUnmount } from "@odoo/owl"; |
| 6 | + |
| 7 | +patch(ProductCatalogKanbanController.prototype, { |
| 8 | + setup(){ |
| 9 | + super.setup(); |
| 10 | + this.orm = useService("orm"); |
| 11 | + this.notification = useService("notification"); |
| 12 | + this.orderId = this.props.context.order_id; |
| 13 | + this.orderResModel = this.props.context.product_catalog_order_model; |
| 14 | + this.lastInputTime = 0; |
| 15 | + this.barcodeBuffer = ""; |
| 16 | + this._onKeyDown = this._onKeyDown.bind(this); |
| 17 | + onMounted(() => { |
| 18 | + document.addEventListener("keydown", this._onKeyDown); |
| 19 | + }); |
| 20 | + onWillUnmount(() => { |
| 21 | + document.removeEventListener("keydown", this._onKeyDown); |
| 22 | + }); |
| 23 | + }, |
| 24 | + |
| 25 | + async _onKeyDown(res) { |
| 26 | + const targetfield = res.target.tagName; |
| 27 | + if (targetfield === "INPUT" || targetfield === "TEXTAREA" || targetfield === "SELECT") { |
| 28 | + return; |
| 29 | + } |
| 30 | + const currentTime = new Date().getTime(); |
| 31 | + if (currentTime - this.lastInputTime > 800) { |
| 32 | + this.barcodeBuffer = ""; |
| 33 | + } |
| 34 | + if (res.key === "Enter") { |
| 35 | + if (this.barcodeBuffer.length > 1) { |
| 36 | + this._processBarcode(this.barcodeBuffer); |
| 37 | + this.barcodeBuffer = ""; |
| 38 | + } |
| 39 | + } else if (res.key.length === 1) { |
| 40 | + this.barcodeBuffer += res.key; |
| 41 | + } |
| 42 | + this.lastInputTime = currentTime; |
| 43 | + }, |
| 44 | + |
| 45 | + async _processBarcode(scannedBarcode) { |
| 46 | + if (!this.orderId) { |
| 47 | + this.notification.add("Please select an order first.", { |
| 48 | + type: "warning", |
| 49 | + }); |
| 50 | + return; |
| 51 | + } |
| 52 | + try { |
| 53 | + const products = await this.orm.searchRead( |
| 54 | + "product.product", |
| 55 | + [["barcode", "=", scannedBarcode]], |
| 56 | + ["id", "name"] |
| 57 | + ); |
| 58 | + |
| 59 | + if (!products.length) { |
| 60 | + this.notification.add("No product found for this barcode.", { |
| 61 | + type: "warning", |
| 62 | + }); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const product = products[0]; |
| 67 | + |
| 68 | + let orderLineModel, quantityField; |
| 69 | + if (this.orderResModel === "sale.order") { |
| 70 | + orderLineModel = "sale.order.line"; |
| 71 | + quantityField = "product_uom_qty"; |
| 72 | + } else if (this.orderResModel === "purchase.order") { |
| 73 | + orderLineModel = "purchase.order.line"; |
| 74 | + quantityField = "product_qty"; |
| 75 | + } else { |
| 76 | + console.error( |
| 77 | + "Unsupported order model for barcode scanning:", |
| 78 | + this.orderResModel |
| 79 | + ); |
| 80 | + this.notification.add( |
| 81 | + "Barcode scanning is not supported for this type of model.", |
| 82 | + { type: "danger" } |
| 83 | + ); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const existingOrderLines = await this.orm.searchRead( |
| 88 | + orderLineModel, |
| 89 | + [ |
| 90 | + ["order_id", "=", this.orderId], |
| 91 | + ["product_id", "=", product.id], |
| 92 | + ], |
| 93 | + ["id", quantityField] |
| 94 | + ); |
| 95 | + |
| 96 | + const updatedQuantity = existingOrderLines.length ? existingOrderLines[0][quantityField] + 1 : 1; |
| 97 | + |
| 98 | + const response = await rpc("/product/catalog/update_order_line_info", { |
| 99 | + res_model: this.orderResModel, |
| 100 | + order_id: this.orderId, |
| 101 | + product_id: product.id, |
| 102 | + quantity: updatedQuantity, |
| 103 | + }); |
| 104 | + |
| 105 | + if (response && response.success) { |
| 106 | + this.notification.add( |
| 107 | + `successfully ${existingOrderLines ? "Updated" : "Added"} ${product.name} (Qty: ${updatedQuantity})`, |
| 108 | + { type: "success" } |
| 109 | + ); |
| 110 | + this.model.load(); |
| 111 | + } else { |
| 112 | + this.notification.add( |
| 113 | + `failed to ${existingOrderLines ? "update" : "add"} ${product.name}.`, |
| 114 | + { type: "danger" } |
| 115 | + ); |
| 116 | + this.model.load(); |
| 117 | + } |
| 118 | + this.model.load(); |
| 119 | + } catch (error) { |
| 120 | + console.error("Error processing barcode scan:", error); |
| 121 | + this.notification.add("An error occurred while processing the barcode.", { |
| 122 | + type: "danger", |
| 123 | + }); |
| 124 | + } |
| 125 | + }, |
| 126 | +}); |
0 commit comments