Skip to content
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

[14.0] [IMP] sale_input_barcode: scan directly on sale.order #669

Open
wants to merge 2 commits into
base: 14.0
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions sale_input_barcode/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,22 @@ 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.

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
===========

Expand Down
4 changes: 1 addition & 3 deletions sale_input_barcode/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
"sale_management",
"barcode_action",
],
"data": [
"views/sale.xml",
],
"data": ["views/sale.xml", "views/sale_input_settings_view.xml"],
"demo": [],
}
1 change: 1 addition & 0 deletions sale_input_barcode/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import product_barcode_line_mixin
from . import res_config_settings
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick (non-blocking): unless there are reasons to do otherwise, prefer listing the files in alphabetical order.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can suggest to add isort (and black) to your environment for that.

Note this is not an OCA requirement.

Copy link
Author

Choose a reason for hiding this comment

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

I ordered the files in alphabetical order like you suggested.

from . import sale_order
14 changes: 10 additions & 4 deletions sale_input_barcode/models/product_barcode_line_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@
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

Check warning on line 36 in sale_input_barcode/models/product_barcode_line_mixin.py

View check run for this annotation

Codecov / codecov/patch

sale_input_barcode/models/product_barcode_line_mixin.py#L36

Added line #L36 was not covered by tests
else:
vals["order_id"] = order_id
return vals

def _process_barcode_on_product_line(self, raw_barcode):
Expand All @@ -48,5 +55,4 @@
_("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)
12 changes: 12 additions & 0 deletions sale_input_barcode/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -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,
)
40 changes: 36 additions & 4 deletions sale_input_barcode/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,43 @@


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)

Check warning on line 16 in sale_input_barcode/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

sale_input_barcode/models/sale_order.py#L16

Added line #L16 was not covered by tests

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]

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

Check warning on line 44 in sale_input_barcode/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

sale_input_barcode/models/sale_order.py#L44

Added line #L44 was not covered by tests
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)]})
2 changes: 2 additions & 0 deletions sale_input_barcode/readme/CONTIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* PyTech SRL <[email protected]>:

- Alessandro Uffreduzzi <[email protected]>
- Alessio Renda


* Ooops404 <[email protected]>:

Expand Down
13 changes: 13 additions & 0 deletions sale_input_barcode/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,18 @@
#. 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.

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.
22 changes: 18 additions & 4 deletions sale_input_barcode/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

/*
:Author: David Goodger ([email protected])
: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.
Expand Down Expand Up @@ -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 }
Expand All @@ -300,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -394,8 +395,19 @@ <h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
<li>Press the button in the header with the QR Code icon</li>
<li>Use a barcode scanner to scan a barcode.</li>
</ol>
<p>Alternatively,
#. Navigate to a Sales Order.
#. Activate “Edit Mode”
#. Use a barcode scanner to scan a barcode.</p>
<p>If a product is found using the barcode,
a new line with that product will be added to the Sales Order.</p>
<p>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.</p>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
Expand All @@ -416,7 +428,9 @@ <h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<p>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.</p>
Expand Down
3 changes: 3 additions & 0 deletions sale_input_barcode/views/sale.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
context="{'default_model': 'sale.order', 'default_method': 'action_sale_line_barcode', 'default_res_id': active_id}"
/>
</xpath>
<field name="name" position="after">
<field name="_barcode_scanned" widget="barcode_handler" />
</field>
</field>
</record>

Expand Down
20 changes: 20 additions & 0 deletions sale_input_barcode/views/sale_input_settings_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<odoo>
<record id="view_res_config_settings_sale_input_barcode" model="ir.ui.view">
<field name="name">res.config.settings.sale.input.barcode</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="sale.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//div[@id='no_edit_order']" position="inside">
<div class="o_setting_left_pane">
<field name="sale_barcode_update_existing_line" />
</div>
<div class="o_setting_right_pane">
<label for="sale_barcode_update_existing_line" />
<div class="text-muted">
Instead of creating a new sale order line it increases the quantity for each barcode scan
</div>
</div>
</xpath>
</field>
</record>
</odoo>
Loading