I want to validate a stock picking by scanning a barcode from the sale order name (SOXXXXX). Supose that I have a label with a barcode that identifies a sale order. When I'm in the form view of its corresponding stock picking(s), I would scan the sale order's barcode to validate the stock picking.
Checking the source code, I found that stock.picking has a method call on_barcode_scanned. I tried the following:
class StockPicking(models.Model):
_inherit = ['stock.picking']
def on_barcode_scanned(self, barcode):
picking = self._origin
if picking.origin == barcode:
picking.button_validate()
return
return super(StockPicking, self).on_barcode_scanned(barcode)
The first line (self._origin) is used to access the current record (apparently, in on_chnage events, odoo creates a pseudo-record from the original, with a different ID). Then it checks if the barcode scanned is the same as the stock picking's origin (instead should check for sale_id.name, but I will get to that in a moment). If it's True, then validate move, else return on_barcode_scanned of the parent class.
However, I've been having this issues:
If I use the button_validate method from self (the pseudo-record), it shows the following error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/http.py", line 650, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 310, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 87, in reraise
raise value
File "/usr/lib/python3/dist-packages/odoo/http.py", line 692, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 342, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/service/model.py", line 97, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 335, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 936, in __call__
return self.method(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 515, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 931, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 923, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/api.py", line 689, in call_kw
return call_kw_multi(method, model, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/api.py", line 680, in call_kw_multi
result = method(recs, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5095, in onchange
record._onchange_eval(name, field_onchange[name], result)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 4990, in _onchange_eval
method_res = method(self)
File "/usr/lib/python3/dist-packages/odoo/addons/barcodes/models/barcode_events_mixin.py", line 21, in _on_barcode_scanned
return self.on_barcode_scanned(barcode)
File "/usr/lib/python3/dist-packages/odoo/custom-addons/stock_gls/models/stock_picking.py", line 14, in on_barcode_scanned
self.button_validate()
File "/usr/lib/python3/dist-packages/odoo/custom-addons/sale_gls/models/stock_picking.py", line 72, in button_validate
return super(StockPicking, self).button_validate()
File "/usr/lib/python3/dist-packages/odoo/addons/stock/models/stock_picking.py", line 726, in button_validate
wiz = self.env['stock.immediate.transfer'].create({'pick_ids': [(4, self.id)]})
File "/usr/lib/python3/dist-packages/odoo/models.py", line 3373, in create
record = self.browse(self._create(old_vals))
File "/usr/lib/python3/dist-packages/odoo/models.py", line 3520, in _create
field.write(self.with_context(rel_context), vals[name], create=True)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 2494, in write
cr.execute(query, (records.ids, list(sub_ids), tuple(records.ids)))
File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 155, in wrapper
return f(self, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 232, in execute
res = self._obj.execute(query, params)
psycopg2.ProgrammingError: can't adapt type 'NewId'
For what I can understand, it cannot find a stock.picking with ID=NewId
If I use the button_validate method from the origin record, it raises the following error:
Odoo Server Error - warning. Please add some lines to move
Checking the original record (self._origin), the fields sale_id and move_lines are empty, but the record has those fields defined, I checked the database and those fields are not empty.
So, what's going on?