|
| 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 | + /** |
| 9 | + * @override |
| 10 | + */ |
| 11 | + setup() { |
| 12 | + super.setup(); |
| 13 | + this.orm = useService("orm"); |
| 14 | + this.notification = useService("notification"); |
| 15 | + this.orderId = this.props.context.order_id; |
| 16 | + // The resModel of the order |
| 17 | + this.orderResModel = this.props.context.product_catalog_order_model; |
| 18 | + |
| 19 | + // Properties for keyboard-based barcode scanning |
| 20 | + this.lastInputTime = 0; |
| 21 | + this.barcodeBuffer = ""; |
| 22 | + |
| 23 | + // Bind the keydown handler to this component instance |
| 24 | + this._onKeyDown = this._onKeyDown.bind(this); |
| 25 | + |
| 26 | + // Add and remove the event listener when the component is mounted and unmounted. |
| 27 | + onMounted(() => { |
| 28 | + document.addEventListener("keydown", this._onKeyDown); |
| 29 | + }); |
| 30 | + |
| 31 | + onWillUnmount(() => { |
| 32 | + document.removeEventListener("keydown", this._onKeyDown); |
| 33 | + }); |
| 34 | + }, |
| 35 | + |
| 36 | + /** |
| 37 | + * Handles the 'keydown' event to capture barcode scans from a keyboard/scanner. |
| 38 | + * |
| 39 | + * @param {KeyboardEvent} ev |
| 40 | + */ |
| 41 | + async _onKeyDown(ev) { |
| 42 | + // Ignore key events if they originate from an input, textarea, or select element |
| 43 | + // to avoid interfering with user typing. |
| 44 | + const targetTagName = ev.target.tagName; |
| 45 | + if (targetTagName === 'INPUT' || targetTagName === 'TEXTAREA' || targetTagName === 'SELECT') { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const currentTime = new Date().getTime(); |
| 50 | + // If the last keypress was more than 500ms ago, reset the buffer. |
| 51 | + // This prevents accidental concatenation of different barcodes. |
| 52 | + if (currentTime - this.lastInputTime > 500) { |
| 53 | + this.barcodeBuffer = ""; |
| 54 | + } |
| 55 | + |
| 56 | + // If the 'Enter' key is pressed, process the buffered input as a barcode. |
| 57 | + if (ev.key === "Enter") { |
| 58 | + if (this.barcodeBuffer.length > 1) { |
| 59 | + this._processBarcode(this.barcodeBuffer); |
| 60 | + this.barcodeBuffer = ""; // Reset buffer after processing |
| 61 | + } |
| 62 | + } else if (ev.key.length === 1) { |
| 63 | + // Append the character to our buffer if it's a single character key. |
| 64 | + this.barcodeBuffer += ev.key; |
| 65 | + } |
| 66 | + |
| 67 | + // Update the timestamp of the last keypress. |
| 68 | + this.lastInputTime = currentTime; |
| 69 | + }, |
| 70 | + |
| 71 | + /** |
| 72 | + * Processes the scanned barcode to find the corresponding product and update the order. |
| 73 | + * |
| 74 | + * @param {string} scannedBarcode The barcode string to process. |
| 75 | + */ |
| 76 | + async _processBarcode(scannedBarcode) { |
| 77 | + // An order must be selected to add products. |
| 78 | + if (!this.orderId) { |
| 79 | + this.notification.add("Please select an order first.", { type: "warning" }); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + // Search for a product with the scanned barcode. |
| 85 | + const products = await this.orm.searchRead( |
| 86 | + "product.product", |
| 87 | + [["barcode", "=", scannedBarcode]], |
| 88 | + ["id", "name"] |
| 89 | + ); |
| 90 | + |
| 91 | + if (!products.length) { |
| 92 | + this.notification.add("No product found for this barcode.", { type: "warning" }); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + const product = products[0]; |
| 97 | + |
| 98 | + let orderLineModel, quantityField; |
| 99 | + // Determine the correct model and field names based on the order type. |
| 100 | + if (this.orderResModel === "sale.order") { |
| 101 | + orderLineModel = "sale.order.line"; |
| 102 | + quantityField = "product_uom_qty"; |
| 103 | + } else if (this.orderResModel === "purchase.order") { |
| 104 | + orderLineModel = "purchase.order.line"; |
| 105 | + quantityField = "product_qty"; |
| 106 | + } else { |
| 107 | + // Log an error if the order model is not supported. |
| 108 | + console.error("Unsupported order model for barcode scanning:", this.orderResModel); |
| 109 | + this.notification.add("Barcode scanning is not supported for this document type.", { type: "danger" }); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // Check if there is an existing order line for this product. |
| 114 | + const existingOrderLines = await this.orm.searchRead( |
| 115 | + orderLineModel, |
| 116 | + [["order_id", "=", this.orderId], ["product_id", "=", product.id]], |
| 117 | + ["id", quantityField] |
| 118 | + ); |
| 119 | + |
| 120 | + // If a line exists, increment its quantity; otherwise, set quantity to 1. |
| 121 | + const updatedQuantity = existingOrderLines.length ? existingOrderLines[0][quantityField] + 1 : 1; |
| 122 | + |
| 123 | + // Call the backend to create or update the order line. |
| 124 | + await rpc("/product/catalog/update_order_line_info", { |
| 125 | + res_model: this.orderResModel, |
| 126 | + order_id: this.orderId, |
| 127 | + product_id: product.id, |
| 128 | + quantity: updatedQuantity, |
| 129 | + }); |
| 130 | + |
| 131 | + // Notify the user of the successful addition. |
| 132 | + this.notification.add( |
| 133 | + `Added ${product.name} (Qty: ${updatedQuantity})`, |
| 134 | + { type: "success" } |
| 135 | + ); |
| 136 | + |
| 137 | + // Reload the view to show the updated order line information. |
| 138 | + this.model.load(); |
| 139 | + |
| 140 | + } catch (error) { |
| 141 | + console.error("Error processing barcode scan:", error); |
| 142 | + this.notification.add("An error occurred while processing the barcode.", { type: "danger" }); |
| 143 | + } |
| 144 | + }, |
| 145 | +}); |
0 commit comments