|
| 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, useBus } from "@web/core/utils/hooks"; |
| 5 | + |
| 6 | +patch(ProductCatalogKanbanController.prototype, { |
| 7 | + /** |
| 8 | + * @override |
| 9 | + */ |
| 10 | + setup() { |
| 11 | + super.setup(); |
| 12 | + this.orm = useService("orm"); |
| 13 | + this.notification = useService("notification"); |
| 14 | + this.barcodeService = useService('barcode'); |
| 15 | + useBus(this.barcodeService.bus, 'barcode_scanned', (ev) => this._processBarcode(ev.detail.barcode)); |
| 16 | + }, |
| 17 | + |
| 18 | + /** |
| 19 | + * Processes the scanned barcode to find the corresponding product and update the order. |
| 20 | + * |
| 21 | + * @param {string} scannedBarcode The barcode string to process. |
| 22 | + */ |
| 23 | + async _processBarcode(scannedBarcode) { |
| 24 | + // An order must be selected to add products. |
| 25 | + if (!this.orderId) { |
| 26 | + this.notification.add("Please select an order first.", { type: "warning" }); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + try { |
| 31 | + // Search for a product with the scanned barcode. |
| 32 | + const products = await this.orm.searchRead( |
| 33 | + "product.product", |
| 34 | + [["barcode", "=", scannedBarcode]], |
| 35 | + ["id", "name"] |
| 36 | + ); |
| 37 | + |
| 38 | + if (!products.length) { |
| 39 | + this.notification.add("No product found for this barcode.", { type: "warning" }); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const product = products[0]; |
| 44 | + |
| 45 | + let orderLineModel, quantityField; |
| 46 | + // Determine the correct model and field names based on the order type. |
| 47 | + if (this.orderResModel === "sale.order") { |
| 48 | + orderLineModel = "sale.order.line"; |
| 49 | + quantityField = "product_uom_qty"; |
| 50 | + } else if (this.orderResModel === "purchase.order") { |
| 51 | + orderLineModel = "purchase.order.line"; |
| 52 | + quantityField = "product_qty"; |
| 53 | + } else { |
| 54 | + // Log an error if the order model is not supported. |
| 55 | + console.error("Unsupported order model for barcode scanning:", this.orderResModel); |
| 56 | + this.notification.add("Barcode scanning is not supported for this document type.", { type: "danger" }); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Check if there is an existing order line for this product. |
| 61 | + const existingOrderLines = await this.orm.searchRead( |
| 62 | + orderLineModel, |
| 63 | + [["order_id", "=", this.orderId], ["product_id", "=", product.id]], |
| 64 | + ["id", quantityField] |
| 65 | + ); |
| 66 | + |
| 67 | + // If a line exists, increment its quantity; otherwise, set quantity to 1. |
| 68 | + const updatedQuantity = existingOrderLines.length ? existingOrderLines[0][quantityField] + 1 : 1; |
| 69 | + |
| 70 | + // Call the backend to create or update the order line. |
| 71 | + await rpc("/product/catalog/update_order_line_info", { |
| 72 | + res_model: this.orderResModel, |
| 73 | + order_id: this.orderId, |
| 74 | + product_id: product.id, |
| 75 | + quantity: updatedQuantity, |
| 76 | + }); |
| 77 | + |
| 78 | + // Notify the user of the successful addition. |
| 79 | + this.notification.add( |
| 80 | + `Added ${product.name} (Qty: ${updatedQuantity})`, |
| 81 | + { type: "success" } |
| 82 | + ); |
| 83 | + |
| 84 | + // Reload the view to show the updated order line information. |
| 85 | + this.model.load(); |
| 86 | + |
| 87 | + } catch (error) { |
| 88 | + console.error("Error processing barcode scan:", error); |
| 89 | + this.notification.add("An error occurred while processing the barcode.", { type: "danger" }); |
| 90 | + } |
| 91 | + }, |
| 92 | +}); |
0 commit comments