From 89388096073c47f07ddefc0fe8555f349ce48a97 Mon Sep 17 00:00:00 2001 From: Alessio Renda Date: Fri, 10 May 2024 12:31:51 +0200 Subject: [PATCH 1/2] [IMP] sale_input_barcode: scan directly on sale.order --- sale_input_barcode/README.rst | 8 +++++ .../models/product_barcode_line_mixin.py | 14 ++++++--- sale_input_barcode/models/sale_order.py | 31 ++++++++++++++++--- sale_input_barcode/readme/CONTIBUTORS.rst | 2 ++ sale_input_barcode/readme/USAGE.rst | 8 +++++ .../static/description/index.html | 6 ++++ sale_input_barcode/views/sale.xml | 3 ++ 7 files changed, 64 insertions(+), 8 deletions(-) diff --git a/sale_input_barcode/README.rst b/sale_input_barcode/README.rst index b1c951f2b645..7044126d151f 100644 --- a/sale_input_barcode/README.rst +++ b/sale_input_barcode/README.rst @@ -47,9 +47,17 @@ Usage #. Press the button in the header with the QR Code icon #. Use a barcode scanner to scan a barcode. +Alternatively, +#. Navigate to a Sales Order. +#. Activate "Edit Mode" +#. Use a barcode scanner to scan a barcode. + If a product is found using the barcode, a new line with that product will be added to the Sales Order. +If a product is already inside the sale order lines, +the quantity will be updated. + Bug Tracker =========== diff --git a/sale_input_barcode/models/product_barcode_line_mixin.py b/sale_input_barcode/models/product_barcode_line_mixin.py index e128b73df8a4..e8a3b9a60a55 100644 --- a/sale_input_barcode/models/product_barcode_line_mixin.py +++ b/sale_input_barcode/models/product_barcode_line_mixin.py @@ -26,9 +26,16 @@ def _populate_vals(self, product, barcode_dict): Builds a dictionary to use in the `create` function Hook for customizations """ - vals = {"product_id": product.id} + vals = { + "product_id": product.id, + "product_uom_qty": 1, + } if "order_id" in self._fields: - vals["order_id"] = self.env.context.get("order_id") + order_id = self.env.context.get("order_id") + if isinstance(order_id, models.NewId): + vals["order_id"] = order_id.origin + else: + vals["order_id"] = order_id return vals def _process_barcode_on_product_line(self, raw_barcode): @@ -48,5 +55,4 @@ def _process_barcode_on_product_line(self, raw_barcode): _("No product found matching this barcode %s" % barcode_str) ) - vals = self._populate_vals(product, barcode_dict) - self.create(vals) + return self._populate_vals(product, barcode_dict) diff --git a/sale_input_barcode/models/sale_order.py b/sale_input_barcode/models/sale_order.py index 88fbbd744f94..6806790171f0 100644 --- a/sale_input_barcode/models/sale_order.py +++ b/sale_input_barcode/models/sale_order.py @@ -9,11 +9,34 @@ class SaleOrderLine(models.Model): class SaleOrder(models.Model): - _inherit = "sale.order" + _name = "sale.order" + _inherit = ["sale.order", "barcodes.barcode_events_mixin"] + + def on_barcode_scanned(self, barcode): + self.process_barcode(barcode) def action_sale_line_barcode(self, barcode): """Create a sale line according barcode information""" self.ensure_one() - self.env["sale.order.line"].with_context( - order_id=self.id, company_id=self.company_id.id - )._process_barcode_on_product_line(barcode) + self.process_barcode(barcode) + + def process_barcode(self, barcode): + barcode = barcode.rstrip() + line_vals = ( + self.env["sale.order.line"] + .with_context(order_id=self.id, company_id=self.company_id.id) + ._process_barcode_on_product_line(barcode) + ) + + product_order_line = self.order_line.filtered( + lambda x: x.product_id.id == line_vals.get("product_id") + )[:1] + if product_order_line: + product_order_line.product_uom_qty += 1 + else: + product_order_line = self.env["sale.order.line"].new(line_vals) + product_order_line.product_id_change() + sale_line_vals = product_order_line._convert_to_write( + product_order_line._cache + ) + self.write({"order_line": [(0, 0, sale_line_vals)]}) diff --git a/sale_input_barcode/readme/CONTIBUTORS.rst b/sale_input_barcode/readme/CONTIBUTORS.rst index cdfadc279cbd..7a6b2fbe8aac 100644 --- a/sale_input_barcode/readme/CONTIBUTORS.rst +++ b/sale_input_barcode/readme/CONTIBUTORS.rst @@ -5,6 +5,8 @@ * PyTech SRL : - Alessandro Uffreduzzi + - Alessio Renda + * Ooops404 : diff --git a/sale_input_barcode/readme/USAGE.rst b/sale_input_barcode/readme/USAGE.rst index 67088566d7ef..614338b0e13a 100644 --- a/sale_input_barcode/readme/USAGE.rst +++ b/sale_input_barcode/readme/USAGE.rst @@ -2,5 +2,13 @@ #. Press the button in the header with the QR Code icon #. Use a barcode scanner to scan a barcode. +Alternatively, +#. Navigate to a Sales Order. +#. Activate "Edit Mode" +#. Use a barcode scanner to scan a barcode. + If a product is found using the barcode, a new line with that product will be added to the Sales Order. + +If a product is already inside the sale order lines, +the quantity will be updated. diff --git a/sale_input_barcode/static/description/index.html b/sale_input_barcode/static/description/index.html index 994849ae3d41..9e300637687a 100644 --- a/sale_input_barcode/static/description/index.html +++ b/sale_input_barcode/static/description/index.html @@ -394,8 +394,14 @@

Usage

  • Press the button in the header with the QR Code icon
  • Use a barcode scanner to scan a barcode.
  • +

    Alternatively, +#. Navigate to a Sales Order. +#. Activate “Edit Mode” +#. Use a barcode scanner to scan a barcode.

    If a product is found using the barcode, a new line with that product will be added to the Sales Order.

    +

    If a product is already inside the sale order lines, +the quantity will be updated.

    Bug Tracker

    diff --git a/sale_input_barcode/views/sale.xml b/sale_input_barcode/views/sale.xml index 0a93a9b12127..25c4224b2830 100644 --- a/sale_input_barcode/views/sale.xml +++ b/sale_input_barcode/views/sale.xml @@ -24,6 +24,9 @@ context="{'default_model': 'sale.order', 'default_method': 'action_sale_line_barcode', 'default_res_id': active_id}" /> + + + From b0ed460ec9dec44e42b3b70c5d2edd5483d24991 Mon Sep 17 00:00:00 2001 From: SoSylwe Date: Wed, 22 Jan 2025 14:54:11 +0100 Subject: [PATCH 2/2] [IMP] sale_input_barcode: add settings for sol management --- sale_input_barcode/README.rst | 9 +++++++-- sale_input_barcode/__manifest__.py | 4 +--- sale_input_barcode/models/__init__.py | 1 + .../models/res_config_settings.py | 12 +++++++++++ sale_input_barcode/models/sale_order.py | 11 +++++++++- sale_input_barcode/readme/USAGE.rst | 9 +++++++-- .../static/description/index.html | 20 +++++++++++++------ .../views/sale_input_settings_view.xml | 20 +++++++++++++++++++ 8 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 sale_input_barcode/models/res_config_settings.py create mode 100644 sale_input_barcode/views/sale_input_settings_view.xml diff --git a/sale_input_barcode/README.rst b/sale_input_barcode/README.rst index 7044126d151f..cc371e8525f9 100644 --- a/sale_input_barcode/README.rst +++ b/sale_input_barcode/README.rst @@ -55,8 +55,13 @@ Alternatively, If a product is found using the barcode, a new line with that product will be added to the Sales Order. -If a product is already inside the sale order lines, -the quantity will be updated. +By default, each product scan adds a new line to the sales order. +However, you can change this behavior in the sales settings. +To have the system increase the quantity of an existing product line +instead of creating a new one, follow these steps: +#. Go to the Sales Settings section. +#. Locate the option labeled "Increase quantity instead of creating a new line". +#. Check the box to activate this option and save to apply the changes. Bug Tracker =========== diff --git a/sale_input_barcode/__manifest__.py b/sale_input_barcode/__manifest__.py index 7e19210b2bbc..b1fec52c9842 100644 --- a/sale_input_barcode/__manifest__.py +++ b/sale_input_barcode/__manifest__.py @@ -15,8 +15,6 @@ "sale_management", "barcode_action", ], - "data": [ - "views/sale.xml", - ], + "data": ["views/sale.xml", "views/sale_input_settings_view.xml"], "demo": [], } diff --git a/sale_input_barcode/models/__init__.py b/sale_input_barcode/models/__init__.py index 8e4dc36757b1..3215559ddf4c 100644 --- a/sale_input_barcode/models/__init__.py +++ b/sale_input_barcode/models/__init__.py @@ -1,2 +1,3 @@ from . import product_barcode_line_mixin +from . import res_config_settings from . import sale_order diff --git a/sale_input_barcode/models/res_config_settings.py b/sale_input_barcode/models/res_config_settings.py new file mode 100644 index 000000000000..56f529240ca4 --- /dev/null +++ b/sale_input_barcode/models/res_config_settings.py @@ -0,0 +1,12 @@ +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + + _inherit = "res.config.settings" + + sale_barcode_update_existing_line = fields.Boolean( + string="Increase quantity instead of creating a new line", + config_parameter="sale_input_barcode.sale_barcode_update_existing_line", + default=False, + ) diff --git a/sale_input_barcode/models/sale_order.py b/sale_input_barcode/models/sale_order.py index 6806790171f0..f9852451a89e 100644 --- a/sale_input_barcode/models/sale_order.py +++ b/sale_input_barcode/models/sale_order.py @@ -31,7 +31,16 @@ def process_barcode(self, barcode): product_order_line = self.order_line.filtered( lambda x: x.product_id.id == line_vals.get("product_id") )[:1] - if product_order_line: + + sale_barcode_update_existing_line = ( + self.env["ir.config_parameter"] + .sudo() + .get_param( + "sale_input_barcode.sale_barcode_update_existing_line", + ) + ) + + if product_order_line and sale_barcode_update_existing_line: product_order_line.product_uom_qty += 1 else: product_order_line = self.env["sale.order.line"].new(line_vals) diff --git a/sale_input_barcode/readme/USAGE.rst b/sale_input_barcode/readme/USAGE.rst index 614338b0e13a..23ef21570add 100644 --- a/sale_input_barcode/readme/USAGE.rst +++ b/sale_input_barcode/readme/USAGE.rst @@ -10,5 +10,10 @@ Alternatively, If a product is found using the barcode, a new line with that product will be added to the Sales Order. -If a product is already inside the sale order lines, -the quantity will be updated. +By default, each product scan adds a new line to the sales order. +However, you can change this behavior in the sales settings. +To have the system increase the quantity of an existing product line +instead of creating a new one, follow these steps: +#. Go to the Sales Settings section. +#. Locate the option labeled "Increase quantity instead of creating a new line". +#. Check the box to activate this option and save to apply the changes. diff --git a/sale_input_barcode/static/description/index.html b/sale_input_barcode/static/description/index.html index 9e300637687a..119a6010571c 100644 --- a/sale_input_barcode/static/description/index.html +++ b/sale_input_barcode/static/description/index.html @@ -8,10 +8,11 @@ /* :Author: David Goodger (goodger@python.org) -:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $ +:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $ :Copyright: This stylesheet has been placed in the public domain. Default cascading style sheet for the HTML output of Docutils. +Despite the name, some widely supported CSS2 features are used. See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to customize this style sheet. @@ -274,7 +275,7 @@ margin-left: 2em ; margin-right: 2em } -pre.code .ln { color: grey; } /* line numbers */ +pre.code .ln { color: gray; } /* line numbers */ pre.code, code { background-color: #eeeeee } pre.code .comment, code .comment { color: #5C6576 } pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold } @@ -300,7 +301,7 @@ span.pre { white-space: pre } -span.problematic { +span.problematic, pre.problematic { color: red } span.section-subtitle { @@ -400,8 +401,13 @@

    Usage

    #. Use a barcode scanner to scan a barcode.

    If a product is found using the barcode, a new line with that product will be added to the Sales Order.

    -

    If a product is already inside the sale order lines, -the quantity will be updated.

    +

    By default, each product scan adds a new line to the sales order. +However, you can change this behavior in the sales settings. +To have the system increase the quantity of an existing product line +instead of creating a new one, follow these steps: +#. Go to the Sales Settings section. +#. Locate the option labeled “Increase quantity instead of creating a new line”. +#. Check the box to activate this option and save to apply the changes.

    Bug Tracker

    @@ -422,7 +428,9 @@

    Authors

    Maintainers

    This module is maintained by the OCA.

    -Odoo Community Association + +Odoo Community Association +

    OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

    diff --git a/sale_input_barcode/views/sale_input_settings_view.xml b/sale_input_barcode/views/sale_input_settings_view.xml new file mode 100644 index 000000000000..0979201b9f33 --- /dev/null +++ b/sale_input_barcode/views/sale_input_settings_view.xml @@ -0,0 +1,20 @@ + + + res.config.settings.sale.input.barcode + res.config.settings + + + +
    + +
    +
    +
    +
    +
    +
    +